1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "image.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
22 #include "wx/bitmap.h"
26 #include "wx/filefn.h"
27 #include "wx/wfstream.h"
29 #include "wx/module.h"
39 #include "wx/msw/private.h"
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 class wxImageRefData
: public wxObjectRefData
55 unsigned char *m_data
;
57 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
61 wxImageRefData::wxImageRefData()
65 m_data
= (unsigned char*) NULL
;
73 wxImageRefData::~wxImageRefData()
75 if (m_data
) free( m_data
);
78 wxList
wxImage::sm_handlers
;
80 //-----------------------------------------------------------------------------
82 #define M_IMGDATA ((wxImageRefData *)m_refData)
84 #if !USE_SHARED_LIBRARIES
85 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
92 wxImage::wxImage( int width
, int height
)
94 Create( width
, height
);
97 wxImage::wxImage( const wxString
& name
, long type
)
99 LoadFile( name
, type
);
102 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
)
104 LoadFile( name
, mimetype
);
108 wxImage::wxImage( wxInputStream
& stream
, long type
)
110 LoadFile( stream
, type
);
113 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
)
115 LoadFile( stream
, mimetype
);
117 #endif // wxUSE_STREAMS
119 wxImage::wxImage( const wxImage
& image
)
124 wxImage::wxImage( const wxImage
* image
)
126 if (image
) Ref(*image
);
129 void wxImage::Create( int width
, int height
)
131 m_refData
= new wxImageRefData();
133 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
134 if (M_IMGDATA
->m_data
)
136 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
138 M_IMGDATA
->m_width
= width
;
139 M_IMGDATA
->m_height
= height
;
140 M_IMGDATA
->m_ok
= TRUE
;
148 void wxImage::Destroy()
153 wxImage
wxImage::Scale( int width
, int height
) const
157 wxCHECK_MSG( Ok(), image
, _T("invalid image") );
159 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, _T("invalid image size") );
161 image
.Create( width
, height
);
163 char unsigned *data
= image
.GetData();
165 wxCHECK_MSG( data
, image
, _T("unable to create image") );
167 if (M_IMGDATA
->m_hasMask
)
168 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
170 long old_height
= M_IMGDATA
->m_height
;
171 long old_width
= M_IMGDATA
->m_width
;
173 char unsigned *source_data
= M_IMGDATA
->m_data
;
174 char unsigned *target_data
= data
;
176 for (long j
= 0; j
< height
; j
++)
178 long y_offset
= (j
* old_height
/ height
) * old_width
;
180 for (long i
= 0; i
< width
; i
++)
183 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
192 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
194 wxCHECK_RET( Ok(), _T("invalid image") );
196 int w
= M_IMGDATA
->m_width
;
197 int h
= M_IMGDATA
->m_height
;
199 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), _T("invalid image index") );
201 long pos
= (y
* w
+ x
) * 3;
203 M_IMGDATA
->m_data
[ pos
] = r
;
204 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
205 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
208 unsigned char wxImage::GetRed( int x
, int y
)
210 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
212 int w
= M_IMGDATA
->m_width
;
213 int h
= M_IMGDATA
->m_height
;
215 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
217 long pos
= (y
* w
+ x
) * 3;
219 return M_IMGDATA
->m_data
[pos
];
222 unsigned char wxImage::GetGreen( int x
, int y
)
224 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
226 int w
= M_IMGDATA
->m_width
;
227 int h
= M_IMGDATA
->m_height
;
229 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
231 long pos
= (y
* w
+ x
) * 3;
233 return M_IMGDATA
->m_data
[pos
+1];
236 unsigned char wxImage::GetBlue( int x
, int y
)
238 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
240 int w
= M_IMGDATA
->m_width
;
241 int h
= M_IMGDATA
->m_height
;
243 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
245 long pos
= (y
* w
+ x
) * 3;
247 return M_IMGDATA
->m_data
[pos
+2];
250 bool wxImage::Ok() const
252 return (M_IMGDATA
&& M_IMGDATA
->m_ok
);
255 char unsigned *wxImage::GetData() const
257 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, _T("invalid image") );
259 return M_IMGDATA
->m_data
;
262 void wxImage::SetData( char unsigned *data
)
264 wxCHECK_RET( Ok(), _T("invalid image") );
266 memcpy(M_IMGDATA
->m_data
, data
, M_IMGDATA
->m_width
* M_IMGDATA
->m_height
* 3);
269 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
271 wxCHECK_RET( Ok(), _T("invalid image") );
273 M_IMGDATA
->m_maskRed
= r
;
274 M_IMGDATA
->m_maskGreen
= g
;
275 M_IMGDATA
->m_maskBlue
= b
;
276 M_IMGDATA
->m_hasMask
= TRUE
;
279 unsigned char wxImage::GetMaskRed() const
281 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
283 return M_IMGDATA
->m_maskRed
;
286 unsigned char wxImage::GetMaskGreen() const
288 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
290 return M_IMGDATA
->m_maskGreen
;
293 unsigned char wxImage::GetMaskBlue() const
295 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
297 return M_IMGDATA
->m_maskBlue
;
300 void wxImage::SetMask( bool mask
)
302 wxCHECK_RET( Ok(), _T("invalid image") );
304 M_IMGDATA
->m_hasMask
= mask
;
307 bool wxImage::HasMask() const
309 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
311 return M_IMGDATA
->m_hasMask
;
314 int wxImage::GetWidth() const
316 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
318 return M_IMGDATA
->m_width
;
321 int wxImage::GetHeight() const
323 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
325 return M_IMGDATA
->m_height
;
328 bool wxImage::LoadFile( const wxString
& filename
, long type
)
331 if (wxFileExists(filename
))
333 wxFileInputStream
stream(filename
);
334 return LoadFile(stream
, type
);
338 wxLogError( _T("Can't load image from file '%s': file does not exist."), filename
.c_str() );
342 #else // !wxUSE_STREAMS
344 #endif // wxUSE_STREAMS
347 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
)
350 if (wxFileExists(filename
))
352 wxFileInputStream
stream(filename
);
353 return LoadFile(stream
, mimetype
);
357 wxLogError( _T("Can't load image from file '%s': file does not exist."), filename
.c_str() );
361 #else // !wxUSE_STREAMS
363 #endif // wxUSE_STREAMS
366 bool wxImage::SaveFile( const wxString
& filename
, int type
)
369 wxFileOutputStream
stream(filename
);
371 if ( stream
.LastError() == wxStream_NOERROR
)
372 return SaveFile(stream
, type
);
374 #endif // wxUSE_STREAMS
378 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
)
381 wxFileOutputStream
stream(filename
);
383 if ( stream
.LastError() == wxStream_NOERROR
)
384 return SaveFile(stream
, mimetype
);
386 #endif // wxUSE_STREAMS
391 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
395 m_refData
= new wxImageRefData
;
397 wxImageHandler
*handler
= FindHandler(type
);
401 wxLogWarning( _T("No image handler for type %d defined."), type
);
406 return handler
->LoadFile( this, stream
);
409 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
413 m_refData
= new wxImageRefData
;
415 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
419 wxLogWarning( _T("No image handler for type %s defined."), mimetype
.GetData() );
424 return handler
->LoadFile( this, stream
);
427 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
429 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
431 wxImageHandler
*handler
= FindHandler(type
);
435 wxLogWarning( _T("No image handler for type %d defined."), type
);
440 return handler
->SaveFile( this, stream
);
443 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
445 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
447 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
451 wxLogWarning( _T("No image handler for type %s defined."), mimetype
.GetData() );
456 return handler
->SaveFile( this, stream
);
458 #endif // wxUSE_STREAMS
460 void wxImage::AddHandler( wxImageHandler
*handler
)
462 // make sure that the memory will be freed at the program end
463 sm_handlers
.DeleteContents(TRUE
);
465 sm_handlers
.Append( handler
);
468 void wxImage::InsertHandler( wxImageHandler
*handler
)
470 // make sure that the memory will be freed at the program end
471 sm_handlers
.DeleteContents(TRUE
);
473 sm_handlers
.Insert( handler
);
476 bool wxImage::RemoveHandler( const wxString
& name
)
478 wxImageHandler
*handler
= FindHandler(name
);
481 sm_handlers
.DeleteObject(handler
);
488 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
490 wxNode
*node
= sm_handlers
.First();
493 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
494 if (handler
->GetName().Cmp(name
) == 0) return handler
;
498 return (wxImageHandler
*)NULL
;
501 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
503 wxNode
*node
= sm_handlers
.First();
506 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
507 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
508 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
512 return (wxImageHandler
*)NULL
;
515 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
517 wxNode
*node
= sm_handlers
.First();
520 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
521 if (handler
->GetType() == bitmapType
) return handler
;
527 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
529 wxNode
*node
= sm_handlers
.First();
532 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
533 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
539 void wxImage::InitStandardHandlers()
541 AddHandler( new wxBMPHandler
);
544 void wxImage::CleanUpHandlers()
546 wxNode
*node
= sm_handlers
.First();
549 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
550 wxNode
*next
= node
->Next();
557 //-----------------------------------------------------------------------------
559 //-----------------------------------------------------------------------------
561 #if !USE_SHARED_LIBRARIES
562 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
566 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
) )
571 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
) )
575 #endif // wxUSE_STREAMS
577 //-----------------------------------------------------------------------------
578 // MSW conversion routines
579 //-----------------------------------------------------------------------------
583 wxBitmap
wxImage::ConvertToBitmap() const
588 // sizeLimit is the MS upper limit for the DIB size
589 int sizeLimit
= 1024*768*3;
591 // width and height of the device-dependent bitmap
592 int width
= GetWidth();
593 int bmpHeight
= GetHeight();
595 // calc the number of bytes per scanline and padding
596 int bytePerLine
= width
*3;
597 int sizeDWORD
= sizeof( DWORD
);
598 int lineBoundary
= bytePerLine
% sizeDWORD
;
600 if( lineBoundary
> 0 )
602 padding
= sizeDWORD
- lineBoundary
;
603 bytePerLine
+= padding
;
605 // calc the number of DIBs and heights of DIBs
608 int height
= sizeLimit
/bytePerLine
;
609 if( height
>= bmpHeight
)
613 numDIB
= bmpHeight
/ height
;
614 hRemain
= bmpHeight
% height
;
615 if( hRemain
>0 ) numDIB
++;
618 // set bitmap parameters
620 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
621 bitmap
.SetWidth( width
);
622 bitmap
.SetHeight( bmpHeight
);
623 bitmap
.SetDepth( wxDisplayDepth() );
625 // create a DIB header
626 int headersize
= sizeof(BITMAPINFOHEADER
);
627 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
628 wxCHECK_MSG( lpDIBh
, bitmap
, _T("could not allocate memory for DIB header") );
629 // Fill in the DIB header
630 lpDIBh
->bmiHeader
.biSize
= headersize
;
631 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
632 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
633 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
634 // the general formula for biSizeImage:
635 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
636 lpDIBh
->bmiHeader
.biPlanes
= 1;
637 lpDIBh
->bmiHeader
.biBitCount
= 24;
638 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
639 lpDIBh
->bmiHeader
.biClrUsed
= 0;
640 // These seem not really needed for our purpose here.
641 lpDIBh
->bmiHeader
.biClrImportant
= 0;
642 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
643 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
644 // memory for DIB data
645 unsigned char *lpBits
;
646 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
649 wxFAIL_MSG( _T("could not allocate memory for DIB") );
654 // create and set the device-dependent bitmap
655 HDC hdc
= ::GetDC(NULL
);
656 HDC memdc
= ::CreateCompatibleDC( hdc
);
658 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
659 ::SelectObject( memdc
, hbitmap
);
661 // copy image data into DIB data and then into DDB (in a loop)
662 unsigned char *data
= GetData();
665 unsigned char *ptdata
= data
;
666 unsigned char *ptbits
;
668 for( n
=0; n
<numDIB
; n
++ )
670 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
672 // redefine height and size of the (possibly) last smaller DIB
673 // memory is not reallocated
675 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
676 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
680 for( j
=0; j
<height
; j
++ )
682 for( i
=0; i
<width
; i
++ )
684 *(ptbits
++) = *(ptdata
+2);
685 *(ptbits
++) = *(ptdata
+1);
686 *(ptbits
++) = *(ptdata
);
689 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
691 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
692 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
694 // if numDIB = 1, lines below can also be used
695 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
696 // The above line is equivalent to the following two lines.
697 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
698 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
699 // or the following lines
700 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
701 // HDC memdc = ::CreateCompatibleDC( hdc );
702 // ::SelectObject( memdc, hbitmap);
703 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
704 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
705 // ::SelectObject( memdc, 0 );
706 // ::DeleteDC( memdc );
708 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
710 // similarly, created an mono-bitmap for the possible mask
713 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
714 ::SelectObject( memdc
, hbitmap
);
715 if( numDIB
== 1 ) height
= bmpHeight
;
716 else height
= sizeLimit
/bytePerLine
;
717 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
718 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
720 unsigned char r
= GetMaskRed();
721 unsigned char g
= GetMaskGreen();
722 unsigned char b
= GetMaskBlue();
723 unsigned char zero
= 0, one
= 255;
725 for( n
=0; n
<numDIB
; n
++ )
727 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
729 // redefine height and size of the (possibly) last smaller DIB
730 // memory is not reallocated
732 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
733 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
736 for( int j
=0; j
<height
; j
++ )
738 for(i
=0; i
<width
; i
++ )
740 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
753 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
755 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
756 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
759 // create a wxMask object
760 wxMask
*mask
= new wxMask();
761 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
762 bitmap
.SetMask( mask
);
763 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
764 /* The following can also be used but is slow to run
765 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
766 wxMask *mask = new wxMask( bitmap, colour );
767 bitmap.SetMask( mask );
771 // free allocated resources
772 ::SelectObject( memdc
, 0 );
774 ::ReleaseDC(NULL
, hdc
);
778 // check the wxBitmap object
779 if( bitmap
.GetHBITMAP() )
780 bitmap
.SetOk( TRUE
);
782 bitmap
.SetOk( FALSE
);
787 wxImage::wxImage( const wxBitmap
&bitmap
)
792 wxFAIL_MSG( _T("invalid bitmap") );
796 // create an wxImage object
797 int width
= bitmap
.GetWidth();
798 int height
= bitmap
.GetHeight();
799 Create( width
, height
);
800 unsigned char *data
= GetData();
803 wxFAIL_MSG( _T("could not allocate data for image") );
807 // calc the number of bytes per scanline and padding in the DIB
808 int bytePerLine
= width
*3;
809 int sizeDWORD
= sizeof( DWORD
);
810 int lineBoundary
= bytePerLine
% sizeDWORD
;
812 if( lineBoundary
> 0 )
814 padding
= sizeDWORD
- lineBoundary
;
815 bytePerLine
+= padding
;
818 // create a DIB header
819 int headersize
= sizeof(BITMAPINFOHEADER
);
820 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
823 wxFAIL_MSG( _T("could not allocate data for DIB header") );
827 // Fill in the DIB header
828 lpDIBh
->bmiHeader
.biSize
= headersize
;
829 lpDIBh
->bmiHeader
.biWidth
= width
;
830 lpDIBh
->bmiHeader
.biHeight
= -height
;
831 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
832 lpDIBh
->bmiHeader
.biPlanes
= 1;
833 lpDIBh
->bmiHeader
.biBitCount
= 24;
834 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
835 lpDIBh
->bmiHeader
.biClrUsed
= 0;
836 // These seem not really needed for our purpose here.
837 lpDIBh
->bmiHeader
.biClrImportant
= 0;
838 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
839 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
840 // memory for DIB data
841 unsigned char *lpBits
;
842 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
845 wxFAIL_MSG( _T("could not allocate data for DIB") );
851 // copy data from the device-dependent bitmap to the DIB
852 HDC hdc
= ::GetDC(NULL
);
854 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
855 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
857 // copy DIB data into the wxImage object
859 unsigned char *ptdata
= data
;
860 unsigned char *ptbits
= lpBits
;
861 for( i
=0; i
<height
; i
++ )
863 for( j
=0; j
<width
; j
++ )
865 *(ptdata
++) = *(ptbits
+2);
866 *(ptdata
++) = *(ptbits
+1);
867 *(ptdata
++) = *(ptbits
);
873 // similarly, set data according to the possible mask bitmap
874 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
876 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
877 // memory DC created, color set, data copied, and memory DC deleted
878 HDC memdc
= ::CreateCompatibleDC( hdc
);
879 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
880 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
881 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
883 // background color set to RGB(16,16,16) in consistent with wxGTK
884 unsigned char r
=16, g
=16, b
=16;
887 for( i
=0; i
<height
; i
++ )
889 for( j
=0; j
<width
; j
++ )
903 SetMaskColour( r
, g
, b
);
910 // free allocated resources
911 ::ReleaseDC(NULL
, hdc
);
918 //-----------------------------------------------------------------------------
919 // GTK conversion routines
920 //-----------------------------------------------------------------------------
926 #include "gdk/gdkx.h"
928 #if (GTK_MINOR_VERSION > 0)
929 #include "gdk/gdkrgb.h"
932 wxBitmap
wxImage::ConvertToBitmap() const
936 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
938 int width
= GetWidth();
939 int height
= GetHeight();
941 bitmap
.SetHeight( height
);
942 bitmap
.SetWidth( width
);
944 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
948 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
949 if (visual
== NULL
) visual
= gdk_visual_get_system();
950 int bpp
= visual
->depth
;
952 bitmap
.SetDepth( bpp
);
954 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
955 if (bpp
< 8) bpp
= 8;
957 #if (GTK_MINOR_VERSION > 0)
959 if (!HasMask() && (bpp
> 8))
961 static bool s_hasInitialized
= FALSE
;
963 if (!s_hasInitialized
)
966 s_hasInitialized
= TRUE
;
969 GdkGC
*gc
= gdk_gc_new( bitmap
.GetPixmap() );
971 gdk_draw_rgb_image( bitmap
.GetPixmap(),
986 // Create picture image
988 GdkImage
*data_image
=
989 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
993 GdkImage
*mask_image
= (GdkImage
*) NULL
;
997 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
999 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1001 wxMask
*mask
= new wxMask();
1002 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1004 bitmap
.SetMask( mask
);
1009 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1010 byte_order b_o
= RGB
;
1014 GdkVisual
*visual
= gdk_visual_get_system();
1015 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1016 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1017 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1018 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1019 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1020 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1023 int r_mask
= GetMaskRed();
1024 int g_mask
= GetMaskGreen();
1025 int b_mask
= GetMaskBlue();
1027 unsigned char* data
= GetData();
1030 for (int y
= 0; y
< height
; y
++)
1032 for (int x
= 0; x
< width
; x
++)
1034 int r
= data
[index
];
1036 int g
= data
[index
];
1038 int b
= data
[index
];
1043 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1044 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1046 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1051 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1052 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1054 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1062 if (wxTheApp
->m_colorCube
)
1064 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1068 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1069 GdkColor
*colors
= cmap
->colors
;
1070 int max
= 3 * (65536);
1072 for (int i
= 0; i
< cmap
->size
; i
++)
1074 int rdiff
= (r
<< 8) - colors
[i
].red
;
1075 int gdiff
= (g
<< 8) - colors
[i
].green
;
1076 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1077 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1078 if (sum
< max
) { pixel
= i
; max
= sum
; }
1082 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1088 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1089 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1094 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1095 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1104 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1105 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1106 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1107 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1108 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1109 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1111 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1120 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1122 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1124 gdk_image_destroy( data_image
);
1125 gdk_gc_unref( data_gc
);
1131 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1133 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1135 gdk_image_destroy( mask_image
);
1136 gdk_gc_unref( mask_gc
);
1142 wxImage::wxImage( const wxBitmap
&bitmap
)
1144 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1146 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1148 bitmap
.GetWidth(), bitmap
.GetHeight() );
1150 wxCHECK_RET( gdk_image
, _T("couldn't create image") );
1152 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1153 char unsigned *data
= GetData();
1157 gdk_image_destroy( gdk_image
);
1158 wxFAIL_MSG( _T("couldn't create image") );
1162 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1163 if (bitmap
.GetMask())
1165 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1167 bitmap
.GetWidth(), bitmap
.GetHeight() );
1169 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1172 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1173 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1174 int bpp
= visual
->depth
;
1175 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1177 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1180 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1182 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1184 wxInt32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1185 pixel
= wxINT32_SWAP_ON_BE( pixel
);
1188 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1189 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1190 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1191 } else if (bpp
== 15)
1193 data
[pos
] = (pixel
>> 7) & 0xf8;
1194 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1195 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1196 } else if (bpp
== 16)
1198 data
[pos
] = (pixel
>> 8) & 0xf8;
1199 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1200 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1203 data
[pos
] = (pixel
>> 16) & 0xff;
1204 data
[pos
+1] = (pixel
>> 8) & 0xff;
1205 data
[pos
+2] = pixel
& 0xff;
1210 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1211 if (mask_pixel
== 0)
1223 gdk_image_destroy( gdk_image
);
1224 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1229 //-----------------------------------------------------------------------------
1230 // Motif conversion routines
1231 //-----------------------------------------------------------------------------
1236 #include "wx/utils.h"
1239 wxBitmap
wxImage::ConvertToBitmap() const
1243 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
1245 int width
= GetWidth();
1246 int height
= GetHeight();
1248 bitmap
.SetHeight( height
);
1249 bitmap
.SetWidth( width
);
1251 Display
*dpy
= (Display
*) wxGetDisplay();
1252 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1253 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1257 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1258 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
1260 bitmap
.Create( width
, height
, bpp
);
1265 GdkImage *mask_image = (GdkImage*) NULL;
1269 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1271 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1273 wxMask *mask = new wxMask();
1274 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1276 bitmap.SetMask( mask );
1280 // Retrieve depth info
1282 XVisualInfo vinfo_template
;
1285 vinfo_template
.visual
= vis
;
1286 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1287 vinfo_template
.depth
= bpp
;
1290 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1292 wxCHECK_MSG( vi
, wxNullBitmap
, _T("no visual") );
1296 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1297 if (bpp
< 8) bpp
= 8;
1301 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1302 byte_order b_o
= RGB
;
1306 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
1307 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
1308 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
1309 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
1310 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
1311 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
1315 int r_mask = GetMaskRed();
1316 int g_mask = GetMaskGreen();
1317 int b_mask = GetMaskBlue();
1323 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
1325 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1326 XQueryColors( dpy
, cmap
, colors
, 256 );
1329 unsigned char* data
= GetData();
1332 for (int y
= 0; y
< height
; y
++)
1334 for (int x
= 0; x
< width
; x
++)
1336 int r
= data
[index
];
1338 int g
= data
[index
];
1340 int b
= data
[index
];
1346 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1347 gdk_image_put_pixel( mask_image, x, y, 1 );
1349 gdk_image_put_pixel( mask_image, x, y, 0 );
1359 if (wxTheApp->m_colorCube)
1361 pixel = wxTheApp->m_colorCube
1362 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1367 int max
= 3 * (65536);
1368 for (int i
= 0; i
< 256; i
++)
1370 int rdiff
= (r
<< 8) - colors
[i
].red
;
1371 int gdiff
= (g
<< 8) - colors
[i
].green
;
1372 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1373 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
1374 if (sum
< max
) { pixel
= i
; max
= sum
; }
1379 XPutPixel( data_image
, x
, y
, pixel
);
1384 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1385 XPutPixel( data_image
, x
, y
, pixel
);
1390 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1391 XPutPixel( data_image
, x
, y
, pixel
);
1400 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1401 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1402 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1403 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1404 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1405 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1407 XPutPixel( data_image
, x
, y
, pixel
);
1417 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
1418 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
1419 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
1421 XDestroyImage( data_image
);
1429 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1431 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1433 gdk_image_destroy( mask_image );
1434 gdk_gc_unref( mask_gc );
1441 wxImage::wxImage( const wxBitmap
&bitmap
)
1443 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1445 Display
*dpy
= (Display
*) wxGetDisplay();
1446 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1447 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1449 XImage
*ximage
= XGetImage( dpy
,
1450 (Drawable
)bitmap
.GetPixmap(),
1452 bitmap
.GetWidth(), bitmap
.GetHeight(),
1453 AllPlanes
, ZPixmap
);
1455 wxCHECK_RET( ximage
, _T("couldn't create image") );
1457 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1458 char unsigned *data
= GetData();
1462 XDestroyImage( ximage
);
1463 wxFAIL_MSG( _T("couldn't create image") );
1468 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1469 if (bitmap.GetMask())
1471 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1473 bitmap.GetWidth(), bitmap.GetHeight() );
1475 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1479 // Retrieve depth info
1481 XVisualInfo vinfo_template
;
1484 vinfo_template
.visual
= vis
;
1485 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1486 vinfo_template
.depth
= bpp
;
1489 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1491 wxCHECK_RET( vi
, _T("no visual") );
1493 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1500 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
1502 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1503 XQueryColors( dpy
, cmap
, colors
, 256 );
1507 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1509 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1511 int pixel
= XGetPixel( ximage
, i
, j
);
1514 data
[pos
] = colors
[pixel
].red
>> 8;
1515 data
[pos
+1] = colors
[pixel
].green
>> 8;
1516 data
[pos
+2] = colors
[pixel
].blue
>> 8;
1517 } else if (bpp
== 15)
1519 data
[pos
] = (pixel
>> 7) & 0xf8;
1520 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1521 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1522 } else if (bpp
== 16)
1524 data
[pos
] = (pixel
>> 8) & 0xf8;
1525 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1526 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1529 data
[pos
] = (pixel
>> 16) & 0xff;
1530 data
[pos
+1] = (pixel
>> 8) & 0xff;
1531 data
[pos
+2] = pixel
& 0xff;
1537 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1538 if (mask_pixel == 0)
1551 XDestroyImage( ximage
);
1553 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1558 // A module to allow wxImage initialization/cleanup
1559 // without calling these functions from app.cpp or from
1560 // the user's application.
1562 class wxImageModule
: public wxModule
1564 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1567 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1568 void OnExit() { wxImage::CleanUpHandlers(); };
1571 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)