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
54 unsigned char *m_data
;
56 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
60 wxImageRefData::wxImageRefData()
64 m_data
= (unsigned char*) NULL
;
72 wxImageRefData::~wxImageRefData()
78 wxList
wxImage::sm_handlers
;
80 //-----------------------------------------------------------------------------
82 #define M_IMGDATA ((wxImageRefData *)m_refData)
84 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
90 wxImage::wxImage( int width
, int height
)
92 Create( width
, height
);
95 wxImage::wxImage( const wxString
& name
, long type
)
97 LoadFile( name
, type
);
100 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
)
102 LoadFile( name
, mimetype
);
106 wxImage::wxImage( wxInputStream
& stream
, long type
)
108 LoadFile( stream
, type
);
111 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
)
113 LoadFile( stream
, mimetype
);
115 #endif // wxUSE_STREAMS
117 wxImage::wxImage( const wxImage
& image
)
122 wxImage::wxImage( const wxImage
* image
)
124 if (image
) Ref(*image
);
127 void wxImage::Create( int width
, int height
)
129 m_refData
= new wxImageRefData();
131 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
132 if (M_IMGDATA
->m_data
)
134 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
136 M_IMGDATA
->m_width
= width
;
137 M_IMGDATA
->m_height
= height
;
138 M_IMGDATA
->m_ok
= TRUE
;
146 void wxImage::Destroy()
151 wxImage
wxImage::Scale( int width
, int height
) const
155 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
157 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, wxT("invalid image size") );
159 image
.Create( width
, height
);
161 char unsigned *data
= image
.GetData();
163 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
165 if (M_IMGDATA
->m_hasMask
)
166 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
168 long old_height
= M_IMGDATA
->m_height
;
169 long old_width
= M_IMGDATA
->m_width
;
171 char unsigned *source_data
= M_IMGDATA
->m_data
;
172 char unsigned *target_data
= data
;
174 for (long j
= 0; j
< height
; j
++)
176 long y_offset
= (j
* old_height
/ height
) * old_width
;
178 for (long i
= 0; i
< width
; i
++)
181 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
190 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
194 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
196 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
197 image
, wxT("invalid subimage size") );
199 int subwidth
=rect
.GetWidth();
200 const int subheight
=rect
.GetHeight();
202 image
.Create( subwidth
, subheight
);
204 char unsigned *subdata
= image
.GetData(), *data
=GetData();
206 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
208 if (M_IMGDATA
->m_hasMask
)
209 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
211 const int subleft
=3*rect
.GetLeft();
212 const int width
=3*GetWidth();
215 data
+=rect
.GetTop()*width
+subleft
;
217 for (long j
= 0; j
< subheight
; ++j
)
219 memcpy( subdata
, data
, subwidth
);
227 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
228 unsigned char r2
, unsigned char g2
, unsigned char b2
)
230 wxCHECK_RET( Ok(), wxT("invalid image") );
232 char unsigned *data
= GetData();
234 const int w
= GetWidth();
235 const int h
= GetHeight();
237 for (int j
= 0; j
< h
; j
++)
238 for (int i
= 0; i
< w
; i
++)
240 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
250 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
252 wxCHECK_RET( Ok(), wxT("invalid image") );
254 int w
= M_IMGDATA
->m_width
;
255 int h
= M_IMGDATA
->m_height
;
257 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
259 long pos
= (y
* w
+ x
) * 3;
261 M_IMGDATA
->m_data
[ pos
] = r
;
262 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
263 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
266 unsigned char wxImage::GetRed( int x
, int y
)
268 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
270 int w
= M_IMGDATA
->m_width
;
271 int h
= M_IMGDATA
->m_height
;
273 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
275 long pos
= (y
* w
+ x
) * 3;
277 return M_IMGDATA
->m_data
[pos
];
280 unsigned char wxImage::GetGreen( int x
, int y
)
282 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
284 int w
= M_IMGDATA
->m_width
;
285 int h
= M_IMGDATA
->m_height
;
287 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
289 long pos
= (y
* w
+ x
) * 3;
291 return M_IMGDATA
->m_data
[pos
+1];
294 unsigned char wxImage::GetBlue( int x
, int y
)
296 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
298 int w
= M_IMGDATA
->m_width
;
299 int h
= M_IMGDATA
->m_height
;
301 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
303 long pos
= (y
* w
+ x
) * 3;
305 return M_IMGDATA
->m_data
[pos
+2];
308 bool wxImage::Ok() const
310 return (M_IMGDATA
&& M_IMGDATA
->m_ok
);
313 char unsigned *wxImage::GetData() const
315 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, wxT("invalid image") );
317 return M_IMGDATA
->m_data
;
320 void wxImage::SetData( char unsigned *data
)
322 wxCHECK_RET( Ok(), wxT("invalid image") );
324 wxImageRefData
*newRefData
= new wxImageRefData();
326 newRefData
->m_width
= M_IMGDATA
->m_width
;
327 newRefData
->m_height
= M_IMGDATA
->m_height
;
328 newRefData
->m_data
= data
;
329 newRefData
->m_ok
= TRUE
;
330 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
331 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
332 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
333 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
337 m_refData
= newRefData
;
340 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
342 wxCHECK_RET( Ok(), wxT("invalid image") );
344 M_IMGDATA
->m_maskRed
= r
;
345 M_IMGDATA
->m_maskGreen
= g
;
346 M_IMGDATA
->m_maskBlue
= b
;
347 M_IMGDATA
->m_hasMask
= TRUE
;
350 unsigned char wxImage::GetMaskRed() const
352 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
354 return M_IMGDATA
->m_maskRed
;
357 unsigned char wxImage::GetMaskGreen() const
359 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
361 return M_IMGDATA
->m_maskGreen
;
364 unsigned char wxImage::GetMaskBlue() const
366 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
368 return M_IMGDATA
->m_maskBlue
;
371 void wxImage::SetMask( bool mask
)
373 wxCHECK_RET( Ok(), wxT("invalid image") );
375 M_IMGDATA
->m_hasMask
= mask
;
378 bool wxImage::HasMask() const
380 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
382 return M_IMGDATA
->m_hasMask
;
385 int wxImage::GetWidth() const
387 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
389 return M_IMGDATA
->m_width
;
392 int wxImage::GetHeight() const
394 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
396 return M_IMGDATA
->m_height
;
399 bool wxImage::LoadFile( const wxString
& filename
, long type
)
402 if (wxFileExists(filename
))
404 wxFileInputStream
stream(filename
);
405 wxBufferedInputStream
bstream( stream
);
406 return LoadFile(bstream
, type
);
410 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
414 #else // !wxUSE_STREAMS
416 #endif // wxUSE_STREAMS
419 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
)
422 if (wxFileExists(filename
))
424 wxFileInputStream
stream(filename
);
425 wxBufferedInputStream
bstream( stream
);
426 return LoadFile(bstream
, mimetype
);
430 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
434 #else // !wxUSE_STREAMS
436 #endif // wxUSE_STREAMS
439 bool wxImage::SaveFile( const wxString
& filename
, int type
)
442 wxFileOutputStream
stream(filename
);
444 if ( stream
.LastError() == wxStream_NOERROR
)
446 wxBufferedOutputStream
bstream( stream
);
447 return SaveFile(bstream
, type
);
450 #endif // wxUSE_STREAMS
454 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
)
457 wxFileOutputStream
stream(filename
);
459 if ( stream
.LastError() == wxStream_NOERROR
)
461 wxBufferedOutputStream
bstream( stream
);
462 return SaveFile(bstream
, mimetype
);
465 #endif // wxUSE_STREAMS
469 bool wxImage::CanRead( const wxString
&name
)
472 wxFileInputStream
stream(name
);
473 return CanRead(stream
);
481 bool wxImage::CanRead( wxInputStream
&stream
)
483 wxList
&list
=GetHandlers();
485 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
487 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
488 if (handler
->CanRead( stream
))
495 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
499 m_refData
= new wxImageRefData
;
501 wxImageHandler
*handler
;
503 if (type
==wxBITMAP_TYPE_ANY
)
505 wxList
&list
=GetHandlers();
507 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
509 handler
=(wxImageHandler
*)node
->GetData();
510 if (handler
->CanRead( stream
))
511 return handler
->LoadFile( this, stream
);
515 wxLogWarning( _("No handler found for image type.") );
519 handler
= FindHandler(type
);
523 wxLogWarning( _("No image handler for type %d defined."), type
);
528 return handler
->LoadFile( this, stream
);
531 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
535 m_refData
= new wxImageRefData
;
537 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
541 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
546 return handler
->LoadFile( this, stream
);
549 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
551 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
553 wxImageHandler
*handler
= FindHandler(type
);
557 wxLogWarning( _("No image handler for type %d defined."), type
);
562 return handler
->SaveFile( this, stream
);
565 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
567 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
569 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
573 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
578 return handler
->SaveFile( this, stream
);
580 #endif // wxUSE_STREAMS
582 void wxImage::AddHandler( wxImageHandler
*handler
)
584 // make sure that the memory will be freed at the program end
585 sm_handlers
.DeleteContents(TRUE
);
587 sm_handlers
.Append( handler
);
590 void wxImage::InsertHandler( wxImageHandler
*handler
)
592 // make sure that the memory will be freed at the program end
593 sm_handlers
.DeleteContents(TRUE
);
595 sm_handlers
.Insert( handler
);
598 bool wxImage::RemoveHandler( const wxString
& name
)
600 wxImageHandler
*handler
= FindHandler(name
);
603 sm_handlers
.DeleteObject(handler
);
610 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
612 wxNode
*node
= sm_handlers
.First();
615 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
616 if (handler
->GetName().Cmp(name
) == 0) return handler
;
620 return (wxImageHandler
*)NULL
;
623 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
625 wxNode
*node
= sm_handlers
.First();
628 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
629 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
630 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
634 return (wxImageHandler
*)NULL
;
637 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
639 wxNode
*node
= sm_handlers
.First();
642 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
643 if (handler
->GetType() == bitmapType
) return handler
;
649 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
651 wxNode
*node
= sm_handlers
.First();
654 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
655 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
661 void wxImage::InitStandardHandlers()
663 AddHandler( new wxBMPHandler
);
666 void wxImage::CleanUpHandlers()
668 wxNode
*node
= sm_handlers
.First();
671 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
672 wxNode
*next
= node
->Next();
679 //-----------------------------------------------------------------------------
681 //-----------------------------------------------------------------------------
683 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
686 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
691 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
696 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
701 bool wxImageHandler::CanRead( const wxString
& name
)
703 if (wxFileExists(name
))
705 wxFileInputStream
stream(name
);
706 return CanRead(stream
);
710 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
717 #endif // wxUSE_STREAMS
719 //-----------------------------------------------------------------------------
720 // MSW conversion routines
721 //-----------------------------------------------------------------------------
725 wxBitmap
wxImage::ConvertToBitmap() const
730 // sizeLimit is the MS upper limit for the DIB size
732 int sizeLimit
= 1024*768*3;
734 int sizeLimit
= 0x7fff ;
737 // width and height of the device-dependent bitmap
738 int width
= GetWidth();
739 int bmpHeight
= GetHeight();
741 // calc the number of bytes per scanline and padding
742 int bytePerLine
= width
*3;
743 int sizeDWORD
= sizeof( DWORD
);
744 int lineBoundary
= bytePerLine
% sizeDWORD
;
746 if( lineBoundary
> 0 )
748 padding
= sizeDWORD
- lineBoundary
;
749 bytePerLine
+= padding
;
751 // calc the number of DIBs and heights of DIBs
754 int height
= sizeLimit
/bytePerLine
;
755 if( height
>= bmpHeight
)
759 numDIB
= bmpHeight
/ height
;
760 hRemain
= bmpHeight
% height
;
761 if( hRemain
>0 ) numDIB
++;
764 // set bitmap parameters
766 wxCHECK_MSG( Ok(), bitmap
, wxT("invalid image") );
767 bitmap
.SetWidth( width
);
768 bitmap
.SetHeight( bmpHeight
);
769 bitmap
.SetDepth( wxDisplayDepth() );
771 // create a DIB header
772 int headersize
= sizeof(BITMAPINFOHEADER
);
773 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
774 wxCHECK_MSG( lpDIBh
, bitmap
, wxT("could not allocate memory for DIB header") );
775 // Fill in the DIB header
776 lpDIBh
->bmiHeader
.biSize
= headersize
;
777 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
778 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
779 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
780 // the general formula for biSizeImage:
781 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
782 lpDIBh
->bmiHeader
.biPlanes
= 1;
783 lpDIBh
->bmiHeader
.biBitCount
= 24;
784 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
785 lpDIBh
->bmiHeader
.biClrUsed
= 0;
786 // These seem not really needed for our purpose here.
787 lpDIBh
->bmiHeader
.biClrImportant
= 0;
788 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
789 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
790 // memory for DIB data
791 unsigned char *lpBits
;
792 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
795 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
800 // create and set the device-dependent bitmap
801 HDC hdc
= ::GetDC(NULL
);
802 HDC memdc
= ::CreateCompatibleDC( hdc
);
804 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
805 ::SelectObject( memdc
, hbitmap
);
807 // copy image data into DIB data and then into DDB (in a loop)
808 unsigned char *data
= GetData();
811 unsigned char *ptdata
= data
;
812 unsigned char *ptbits
;
814 for( n
=0; n
<numDIB
; n
++ )
816 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
818 // redefine height and size of the (possibly) last smaller DIB
819 // memory is not reallocated
821 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
822 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
826 for( j
=0; j
<height
; j
++ )
828 for( i
=0; i
<width
; i
++ )
830 *(ptbits
++) = *(ptdata
+2);
831 *(ptbits
++) = *(ptdata
+1);
832 *(ptbits
++) = *(ptdata
);
835 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
837 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
838 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
840 // if numDIB = 1, lines below can also be used
841 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
842 // The above line is equivalent to the following two lines.
843 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
844 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
845 // or the following lines
846 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
847 // HDC memdc = ::CreateCompatibleDC( hdc );
848 // ::SelectObject( memdc, hbitmap);
849 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
850 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
851 // ::SelectObject( memdc, 0 );
852 // ::DeleteDC( memdc );
854 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
856 // similarly, created an mono-bitmap for the possible mask
859 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
860 ::SelectObject( memdc
, hbitmap
);
861 if( numDIB
== 1 ) height
= bmpHeight
;
862 else height
= sizeLimit
/bytePerLine
;
863 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
864 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
866 unsigned char r
= GetMaskRed();
867 unsigned char g
= GetMaskGreen();
868 unsigned char b
= GetMaskBlue();
869 unsigned char zero
= 0, one
= 255;
871 for( n
=0; n
<numDIB
; n
++ )
873 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
875 // redefine height and size of the (possibly) last smaller DIB
876 // memory is not reallocated
878 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
879 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
882 for( int j
=0; j
<height
; j
++ )
884 for(i
=0; i
<width
; i
++ )
886 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
887 unsigned char cr
= (*(ptdata
++)) ;
888 unsigned char cg
= (*(ptdata
++)) ;
889 unsigned char cb
= (*(ptdata
++)) ;
891 if( ( cr
!=r
) || (cg
!=g
) || (cb
!=b
) )
904 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
906 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
907 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
910 // create a wxMask object
911 wxMask
*mask
= new wxMask();
912 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
913 bitmap
.SetMask( mask
);
914 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
915 /* The following can also be used but is slow to run
916 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
917 wxMask *mask = new wxMask( bitmap, colour );
918 bitmap.SetMask( mask );
922 // free allocated resources
923 ::SelectObject( memdc
, 0 );
925 ::ReleaseDC(NULL
, hdc
);
929 #if WXWIN_COMPATIBILITY_2
930 // check the wxBitmap object
931 bitmap
.GetBitmapData()->SetOk();
932 #endif // WXWIN_COMPATIBILITY_2
937 wxImage::wxImage( const wxBitmap
&bitmap
)
942 wxFAIL_MSG( wxT("invalid bitmap") );
946 // create an wxImage object
947 int width
= bitmap
.GetWidth();
948 int height
= bitmap
.GetHeight();
949 Create( width
, height
);
950 unsigned char *data
= GetData();
953 wxFAIL_MSG( wxT("could not allocate data for image") );
957 // calc the number of bytes per scanline and padding in the DIB
958 int bytePerLine
= width
*3;
959 int sizeDWORD
= sizeof( DWORD
);
960 int lineBoundary
= bytePerLine
% sizeDWORD
;
962 if( lineBoundary
> 0 )
964 padding
= sizeDWORD
- lineBoundary
;
965 bytePerLine
+= padding
;
968 // create a DIB header
969 int headersize
= sizeof(BITMAPINFOHEADER
);
970 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
973 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
977 // Fill in the DIB header
978 lpDIBh
->bmiHeader
.biSize
= headersize
;
979 lpDIBh
->bmiHeader
.biWidth
= width
;
980 lpDIBh
->bmiHeader
.biHeight
= -height
;
981 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
982 lpDIBh
->bmiHeader
.biPlanes
= 1;
983 lpDIBh
->bmiHeader
.biBitCount
= 24;
984 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
985 lpDIBh
->bmiHeader
.biClrUsed
= 0;
986 // These seem not really needed for our purpose here.
987 lpDIBh
->bmiHeader
.biClrImportant
= 0;
988 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
989 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
990 // memory for DIB data
991 unsigned char *lpBits
;
992 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
995 wxFAIL_MSG( wxT("could not allocate data for DIB") );
1001 // copy data from the device-dependent bitmap to the DIB
1002 HDC hdc
= ::GetDC(NULL
);
1004 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
1005 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1007 // copy DIB data into the wxImage object
1009 unsigned char *ptdata
= data
;
1010 unsigned char *ptbits
= lpBits
;
1011 for( i
=0; i
<height
; i
++ )
1013 for( j
=0; j
<width
; j
++ )
1015 *(ptdata
++) = *(ptbits
+2);
1016 *(ptdata
++) = *(ptbits
+1);
1017 *(ptdata
++) = *(ptbits
);
1023 // similarly, set data according to the possible mask bitmap
1024 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
1026 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
1027 // memory DC created, color set, data copied, and memory DC deleted
1028 HDC memdc
= ::CreateCompatibleDC( hdc
);
1029 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1030 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1031 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1032 ::DeleteDC( memdc
);
1033 // background color set to RGB(16,16,16) in consistent with wxGTK
1034 unsigned char r
=16, g
=16, b
=16;
1037 for( i
=0; i
<height
; i
++ )
1039 for( j
=0; j
<width
; j
++ )
1053 SetMaskColour( r
, g
, b
);
1060 // free allocated resources
1061 ::ReleaseDC(NULL
, hdc
);
1070 #include <PictUtils.h>
1072 extern CTabHandle
wxMacCreateColorTable( int numColors
) ;
1073 extern void wxMacDestroyColorTable( CTabHandle colors
) ;
1074 extern void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
) ;
1075 extern GWorldPtr
wxMacCreateGWorld( int height
, int width
, int depth
) ;
1076 extern void wxMacDestroyGWorld( GWorldPtr gw
) ;
1078 wxBitmap
wxImage::ConvertToBitmap() const
1080 // width and height of the device-dependent bitmap
1081 int width
= GetWidth();
1082 int height
= GetHeight();
1086 wxBitmap
bitmap( width
, height
, wxDisplayDepth() ) ;
1093 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1095 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1097 wxMask *mask = new wxMask();
1098 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1100 bitmap.SetMask( mask );
1106 int r_mask
= GetMaskRed();
1107 int g_mask
= GetMaskGreen();
1108 int b_mask
= GetMaskBlue();
1111 GDHandle origDevice
;
1113 GetGWorld( &origPort
, &origDevice
) ;
1114 SetGWorld( bitmap
.GetHBITMAP() , NULL
) ;
1116 register unsigned char* data
= GetData();
1119 for (int y
= 0; y
< height
; y
++)
1121 for (int x
= 0; x
< width
; x
++)
1123 unsigned char r
= data
[index
++];
1124 unsigned char g
= data
[index
++];
1125 unsigned char b
= data
[index
++];
1127 color
.red
= ( r
<< 8 ) + r
;
1128 color
.green
= ( g
<< 8 ) + g
;
1129 color
.blue
= ( b
<< 8 ) + b
;
1130 SetCPixel( x
, y
, &color
) ;
1134 SetGWorld( origPort
, origDevice
) ;
1138 wxColour
colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
1139 wxMask
*mask
= new wxMask( bitmap
, colour
);
1140 bitmap
.SetMask( mask
);
1146 wxImage::wxImage( const wxBitmap
&bitmap
)
1151 wxFAIL_MSG( "invalid bitmap" );
1155 // create an wxImage object
1156 int width
= bitmap
.GetWidth();
1157 int height
= bitmap
.GetHeight();
1158 Create( width
, height
);
1160 unsigned char *data = GetData();
1163 wxFAIL_MSG( "could not allocate data for image" );
1167 // calc the number of bytes per scanline and padding in the DIB
1168 int bytePerLine = width*3;
1169 int sizeDWORD = sizeof( DWORD );
1170 div_t lineBoundary = div( bytePerLine, sizeDWORD );
1172 if( lineBoundary.rem > 0 )
1174 padding = sizeDWORD - lineBoundary.rem;
1175 bytePerLine += padding;
1178 // create a DIB header
1179 int headersize = sizeof(BITMAPINFOHEADER);
1180 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
1183 wxFAIL_MSG( "could not allocate data for DIB header" );
1187 // Fill in the DIB header
1188 lpDIBh->bmiHeader.biSize = headersize;
1189 lpDIBh->bmiHeader.biWidth = width;
1190 lpDIBh->bmiHeader.biHeight = -height;
1191 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
1192 lpDIBh->bmiHeader.biPlanes = 1;
1193 lpDIBh->bmiHeader.biBitCount = 24;
1194 lpDIBh->bmiHeader.biCompression = BI_RGB;
1195 lpDIBh->bmiHeader.biClrUsed = 0;
1196 // These seem not really needed for our purpose here.
1197 lpDIBh->bmiHeader.biClrImportant = 0;
1198 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1199 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1200 // memory for DIB data
1201 unsigned char *lpBits;
1202 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
1205 wxFAIL_MSG( "could not allocate data for DIB" );
1211 // copy data from the device-dependent bitmap to the DIB
1212 HDC hdc = ::GetDC(NULL);
1214 hbitmap = (HBITMAP) bitmap.GetHBITMAP();
1215 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
1217 // copy DIB data into the wxImage object
1219 unsigned char *ptdata = data;
1220 unsigned char *ptbits = lpBits;
1221 for( i=0; i<height; i++ )
1223 for( j=0; j<width; j++ )
1225 *(ptdata++) = *(ptbits+2);
1226 *(ptdata++) = *(ptbits+1);
1227 *(ptdata++) = *(ptbits );
1233 // similarly, set data according to the possible mask bitmap
1234 if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() )
1236 hbitmap = (HBITMAP) bitmap.GetMask()->GetMaskBitmap();
1237 // memory DC created, color set, data copied, and memory DC deleted
1238 HDC memdc = ::CreateCompatibleDC( hdc );
1239 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
1240 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
1241 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
1242 ::DeleteDC( memdc );
1243 // background color set to RGB(16,16,16) in consistent with wxGTK
1244 unsigned char r=16, g=16, b=16;
1247 for( i=0; i<height; i++ )
1249 for( j=0; j<width; j++ )
1263 SetMaskColour( r, g, b );
1270 // free allocated resources
1271 ::ReleaseDC(NULL, hdc);
1279 //-----------------------------------------------------------------------------
1280 // GTK conversion routines
1281 //-----------------------------------------------------------------------------
1285 #include <gtk/gtk.h>
1286 #include <gdk/gdk.h>
1287 #include <gdk/gdkx.h>
1289 #if (GTK_MINOR_VERSION > 0)
1290 #include <gdk/gdkrgb.h>
1293 wxBitmap
wxImage::ConvertToMonoBitmap( unsigned char red
, unsigned char green
, unsigned char blue
)
1297 wxCHECK_MSG( Ok(), bitmap
, wxT("invalid image") );
1299 int width
= GetWidth();
1300 int height
= GetHeight();
1302 bitmap
.SetHeight( height
);
1303 bitmap
.SetWidth( width
);
1305 bitmap
.SetBitmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 ) );
1307 bitmap
.SetDepth( 1 );
1309 // Create picture image
1311 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1313 GdkImage
*data_image
=
1314 gdk_image_new_bitmap( gdk_visual_get_system(), data_data
, width
, height
);
1316 // Create mask image
1318 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1322 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1324 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1326 wxMask
*mask
= new wxMask();
1327 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1329 bitmap
.SetMask( mask
);
1332 int r_mask
= GetMaskRed();
1333 int g_mask
= GetMaskGreen();
1334 int b_mask
= GetMaskBlue();
1336 unsigned char* data
= GetData();
1339 for (int y
= 0; y
< height
; y
++)
1341 for (int x
= 0; x
< width
; x
++)
1343 int r
= data
[index
];
1345 int g
= data
[index
];
1347 int b
= data
[index
];
1352 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1353 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1355 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1358 if ((r
== red
) && (b
== blue
) && (g
== green
))
1359 gdk_image_put_pixel( data_image
, x
, y
, 1 );
1361 gdk_image_put_pixel( data_image
, x
, y
, 0 );
1368 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetBitmap() );
1370 gdk_draw_image( bitmap
.GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1372 gdk_image_destroy( data_image
);
1373 gdk_gc_unref( data_gc
);
1379 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1381 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1383 gdk_image_destroy( mask_image
);
1384 gdk_gc_unref( mask_gc
);
1391 wxBitmap
wxImage::ConvertToBitmap() const
1395 wxCHECK_MSG( Ok(), bitmap
, wxT("invalid image") );
1397 int width
= GetWidth();
1398 int height
= GetHeight();
1400 bitmap
.SetHeight( height
);
1401 bitmap
.SetWidth( width
);
1403 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1407 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1408 if (visual
== NULL
) visual
= gdk_visual_get_system();
1409 int bpp
= visual
->depth
;
1411 bitmap
.SetDepth( bpp
);
1413 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1414 if (bpp
< 8) bpp
= 8;
1416 #if (GTK_MINOR_VERSION > 0)
1418 if (!HasMask() && (bpp
> 8))
1420 static bool s_hasInitialized
= FALSE
;
1422 if (!s_hasInitialized
)
1425 s_hasInitialized
= TRUE
;
1428 GdkGC
*gc
= gdk_gc_new( bitmap
.GetPixmap() );
1430 gdk_draw_rgb_image( bitmap
.GetPixmap(),
1434 GDK_RGB_DITHER_NONE
,
1445 // Create picture image
1447 GdkImage
*data_image
=
1448 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1450 // Create mask image
1452 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1456 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1458 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1460 wxMask
*mask
= new wxMask();
1461 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1463 bitmap
.SetMask( mask
);
1468 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1469 byte_order b_o
= RGB
;
1473 GdkVisual
*visual
= gdk_visual_get_system();
1474 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1475 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1476 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1477 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1478 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1479 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1482 int r_mask
= GetMaskRed();
1483 int g_mask
= GetMaskGreen();
1484 int b_mask
= GetMaskBlue();
1486 unsigned char* data
= GetData();
1489 for (int y
= 0; y
< height
; y
++)
1491 for (int x
= 0; x
< width
; x
++)
1493 int r
= data
[index
];
1495 int g
= data
[index
];
1497 int b
= data
[index
];
1502 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1503 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1505 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1513 if (wxTheApp
->m_colorCube
)
1515 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1519 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1520 GdkColor
*colors
= cmap
->colors
;
1521 int max
= 3 * (65536);
1523 for (int i
= 0; i
< cmap
->size
; i
++)
1525 int rdiff
= (r
<< 8) - colors
[i
].red
;
1526 int gdiff
= (g
<< 8) - colors
[i
].green
;
1527 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1528 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1529 if (sum
< max
) { pixel
= i
; max
= sum
; }
1533 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1539 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1540 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1545 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1546 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1555 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1556 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1557 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1558 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1559 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1560 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1562 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1571 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1573 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1575 gdk_image_destroy( data_image
);
1576 gdk_gc_unref( data_gc
);
1582 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1584 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1586 gdk_image_destroy( mask_image
);
1587 gdk_gc_unref( mask_gc
);
1593 wxImage::wxImage( const wxBitmap
&bitmap
)
1595 wxCHECK_RET( bitmap
.Ok(), wxT("invalid bitmap") );
1597 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
1598 if (bitmap
.GetPixmap())
1600 gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1602 bitmap
.GetWidth(), bitmap
.GetHeight() );
1604 if (bitmap
.GetBitmap())
1606 gdk_image
= gdk_image_get( bitmap
.GetBitmap(),
1608 bitmap
.GetWidth(), bitmap
.GetHeight() );
1611 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1614 wxCHECK_RET( gdk_image
, wxT("couldn't create image") );
1616 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1617 char unsigned *data
= GetData();
1621 gdk_image_destroy( gdk_image
);
1622 wxFAIL_MSG( wxT("couldn't create image") );
1626 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1627 if (bitmap
.GetMask())
1629 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1631 bitmap
.GetWidth(), bitmap
.GetHeight() );
1633 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1637 if (bitmap
.GetPixmap())
1639 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1641 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1642 bpp
= visual
->depth
;
1643 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1645 if (bitmap
.GetBitmap())
1650 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1653 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1655 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1657 wxInt32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1672 } else if (bpp
<= 8)
1674 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1675 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1676 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1677 } else if (bpp
== 15)
1679 #if (wxBYTE_ORDER == wxBIG_ENDIAN)
1682 data
[pos
] = (pixel
>> 7) & 0xf8;
1683 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1684 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1685 } else if (bpp
== 16)
1687 #if (wxBYTE_ORDER == wxBIG_ENDIAN)
1690 data
[pos
] = (pixel
>> 8) & 0xf8;
1691 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1692 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1695 #if (wxBYTE_ORDER == wxBIG_ENDIAN)
1696 data
[pos
] = (pixel
) & 0xff; // Red
1697 data
[pos
+1] = (pixel
>> 8) & 0xff; // Green
1698 data
[pos
+2] = (pixel
>> 16) & 0xff; // Blue
1700 data
[pos
] = (pixel
>> 16) & 0xff;
1701 data
[pos
+1] = (pixel
>> 8) & 0xff;
1702 data
[pos
+2] = pixel
& 0xff;
1708 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1709 if (mask_pixel
== 0)
1721 gdk_image_destroy( gdk_image
);
1722 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1727 //-----------------------------------------------------------------------------
1728 // Motif conversion routines
1729 //-----------------------------------------------------------------------------
1733 #pragma message disable nosimpint
1737 #pragma message enable nosimpint
1739 #include "wx/utils.h"
1744 Date: Wed, 05 Jan 2000 11:45:40 +0100
1745 From: Frits Boel <boel@niob.knaw.nl>
1746 To: julian.smart@ukonline.co.uk
1747 Subject: Patch for Motif ConvertToBitmap
1751 I've been working on a wxWin application for image processing. From the
1752 beginning, I was surprised by the (lack of) speed of ConvertToBitmap,
1753 till I looked in the source code of image.cpp. I saw that converting a
1754 wxImage to a bitmap with 8-bit pixels is done with comparing every pixel
1755 to the 256 colors of the palet. A very time-consuming piece of code!
1757 Because I wanted a faster application, I've made a 'patch' for this. In
1758 short: every pixel of the image is compared to a sorted list with
1759 colors. If the color is found in the list, the palette entry is
1760 returned; if the color is not found, the color palette is searched and
1761 then the palette entry is returned and the color added to the sorted
1764 Maybe there is another method for this, namely changing the palette
1765 itself (if the colors are known, as is the case with tiffs with a
1766 colormap). I did not look at this, maybe someone else did?
1768 The code of the patch is attached, have a look on it, and maybe you will
1769 ship it with the next release of wxMotif?
1774 Software engineer at Hubrecht Laboratory, The Netherlands.
1781 wxSearchColor( void );
1782 wxSearchColor( int size
, XColor
*colors
);
1783 ~wxSearchColor( void );
1785 int SearchColor( int r
, int g
, int b
);
1787 int AddColor( unsigned int value
, int pos
);
1791 unsigned int *color
;
1798 wxSearchColor::wxSearchColor( void )
1801 this->colors
= (XColor
*) NULL
;
1802 this->color
= (unsigned int *) NULL
;
1803 this->entry
= (int*) NULL
;
1809 wxSearchColor::wxSearchColor( int size
, XColor
*colors
)
1813 this->colors
= colors
;
1814 this->color
= new unsigned int[size
];
1815 this->entry
= new int [size
];
1817 for (i
= 0; i
< this->size
; i
++ ) {
1818 this->entry
[i
] = -1;
1821 this->bottom
= this->top
= ( size
>> 1 );
1824 wxSearchColor::~wxSearchColor( void )
1826 if ( this->color
) delete this->color
;
1827 if ( this->entry
) delete this->entry
;
1830 int wxSearchColor::SearchColor( int r
, int g
, int b
)
1832 unsigned int value
= ( ( ( r
* 256 ) + g
) * 256 ) + b
;
1833 int begin
= this->bottom
;
1834 int end
= this->top
;
1837 while ( begin
<= end
) {
1839 middle
= ( begin
+ end
) >> 1;
1841 if ( value
== this->color
[middle
] ) {
1842 return( this->entry
[middle
] );
1843 } else if ( value
< this->color
[middle
] ) {
1851 return AddColor( value
, middle
);
1854 int wxSearchColor::AddColor( unsigned int value
, int pos
)
1858 int max
= 3 * (65536);
1859 for ( i
= 0; i
< 256; i
++ ) {
1860 int rdiff
= ((value
>> 8) & 0xFF00 ) - colors
[i
].red
;
1861 int gdiff
= ((value
) & 0xFF00 ) - colors
[i
].green
;
1862 int bdiff
= ((value
<< 8) & 0xFF00 ) - colors
[i
].blue
;
1863 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
1864 if (sum
< max
) { pixel
= i
; max
= sum
; }
1867 if ( this->entry
[pos
] < 0 ) {
1868 this->color
[pos
] = value
;
1869 this->entry
[pos
] = pixel
;
1870 } else if ( value
< this->color
[pos
] ) {
1872 if ( this->bottom
> 0 ) {
1873 for ( i
= this->bottom
; i
< pos
; i
++ ) {
1874 this->color
[i
-1] = this->color
[i
];
1875 this->entry
[i
-1] = this->entry
[i
];
1878 this->color
[pos
-1] = value
;
1879 this->entry
[pos
-1] = pixel
;
1880 } else if ( this->top
< this->size
-1 ) {
1881 for ( i
= this->top
; i
>= pos
; i
-- ) {
1882 this->color
[i
+1] = this->color
[i
];
1883 this->entry
[i
+1] = this->entry
[i
];
1886 this->color
[pos
] = value
;
1887 this->entry
[pos
] = pixel
;
1892 if ( this->top
< this->size
-1 ) {
1893 for ( i
= this->top
; i
> pos
; i
-- ) {
1894 this->color
[i
+1] = this->color
[i
];
1895 this->entry
[i
+1] = this->entry
[i
];
1898 this->color
[pos
+1] = value
;
1899 this->entry
[pos
+1] = pixel
;
1900 } else if ( this->bottom
> 0 ) {
1901 for ( i
= this->bottom
; i
< pos
; i
++ ) {
1902 this->color
[i
-1] = this->color
[i
];
1903 this->entry
[i
-1] = this->entry
[i
];
1906 this->color
[pos
] = value
;
1907 this->entry
[pos
] = pixel
;
1915 wxBitmap
wxImage::ConvertToBitmap() const
1919 wxCHECK_MSG( Ok(), bitmap
, wxT("invalid image") );
1921 int width
= GetWidth();
1922 int height
= GetHeight();
1924 bitmap
.SetHeight( height
);
1925 bitmap
.SetWidth( width
);
1927 Display
*dpy
= (Display
*) wxGetDisplay();
1928 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1929 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1933 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1934 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
1936 bitmap
.Create( width
, height
, bpp
);
1941 GdkImage *mask_image = (GdkImage*) NULL;
1945 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1947 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1949 wxMask *mask = new wxMask();
1950 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1952 bitmap.SetMask( mask );
1956 // Retrieve depth info
1958 XVisualInfo vinfo_template
;
1961 vinfo_template
.visual
= vis
;
1962 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1963 vinfo_template
.depth
= bpp
;
1966 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1968 wxCHECK_MSG( vi
, wxNullBitmap
, wxT("no visual") );
1972 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1973 if (bpp
< 8) bpp
= 8;
1977 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1978 byte_order b_o
= RGB
;
1982 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
1983 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
1984 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
1985 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
1986 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
1987 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
1991 int r_mask = GetMaskRed();
1992 int g_mask = GetMaskGreen();
1993 int b_mask = GetMaskBlue();
1999 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
2001 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
2002 XQueryColors( dpy
, cmap
, colors
, 256 );
2005 wxSearchColor
scolor( 256, colors
);
2006 unsigned char* data
= GetData();
2009 for (int y
= 0; y
< height
; y
++)
2011 for (int x
= 0; x
< width
; x
++)
2013 int r
= data
[index
];
2015 int g
= data
[index
];
2017 int b
= data
[index
];
2023 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
2024 gdk_image_put_pixel( mask_image, x, y, 1 );
2026 gdk_image_put_pixel( mask_image, x, y, 0 );
2034 #if 0 // Old, slower code
2037 if (wxTheApp->m_colorCube)
2039 pixel = wxTheApp->m_colorCube
2040 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
2045 int max
= 3 * (65536);
2046 for (int i
= 0; i
< 256; i
++)
2048 int rdiff
= (r
<< 8) - colors
[i
].red
;
2049 int gdiff
= (g
<< 8) - colors
[i
].green
;
2050 int bdiff
= (b
<< 8) - colors
[i
].blue
;
2051 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
2052 if (sum
< max
) { pixel
= i
; max
= sum
; }
2059 // And this is all to get the 'right' color...
2060 int pixel
= scolor
.SearchColor( r
, g
, b
);
2061 XPutPixel( data_image
, x
, y
, pixel
);
2066 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
2067 XPutPixel( data_image
, x
, y
, pixel
);
2072 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
2073 XPutPixel( data_image
, x
, y
, pixel
);
2082 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
2083 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
2084 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
2085 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
2086 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
2087 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
2089 XPutPixel( data_image
, x
, y
, pixel
);
2099 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
2100 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
2101 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
2103 XDestroyImage( data_image
);
2111 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
2113 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
2115 gdk_image_destroy( mask_image );
2116 gdk_gc_unref( mask_gc );
2123 wxImage::wxImage( const wxBitmap
&bitmap
)
2125 wxCHECK_RET( bitmap
.Ok(), wxT("invalid bitmap") );
2127 Display
*dpy
= (Display
*) wxGetDisplay();
2128 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
2129 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
2131 XImage
*ximage
= XGetImage( dpy
,
2132 (Drawable
)bitmap
.GetPixmap(),
2134 bitmap
.GetWidth(), bitmap
.GetHeight(),
2135 AllPlanes
, ZPixmap
);
2137 wxCHECK_RET( ximage
, wxT("couldn't create image") );
2139 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
2140 char unsigned *data
= GetData();
2144 XDestroyImage( ximage
);
2145 wxFAIL_MSG( wxT("couldn't create image") );
2150 GdkImage *gdk_image_mask = (GdkImage*) NULL;
2151 if (bitmap.GetMask())
2153 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
2155 bitmap.GetWidth(), bitmap.GetHeight() );
2157 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
2161 // Retrieve depth info
2163 XVisualInfo vinfo_template
;
2166 vinfo_template
.visual
= vis
;
2167 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
2168 vinfo_template
.depth
= bpp
;
2171 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
2173 wxCHECK_RET( vi
, wxT("no visual") );
2175 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
2182 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
2184 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
2185 XQueryColors( dpy
, cmap
, colors
, 256 );
2189 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
2191 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
2193 int pixel
= XGetPixel( ximage
, i
, j
);
2196 data
[pos
] = colors
[pixel
].red
>> 8;
2197 data
[pos
+1] = colors
[pixel
].green
>> 8;
2198 data
[pos
+2] = colors
[pixel
].blue
>> 8;
2199 } else if (bpp
== 15)
2201 data
[pos
] = (pixel
>> 7) & 0xf8;
2202 data
[pos
+1] = (pixel
>> 2) & 0xf8;
2203 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2204 } else if (bpp
== 16)
2206 data
[pos
] = (pixel
>> 8) & 0xf8;
2207 data
[pos
+1] = (pixel
>> 3) & 0xfc;
2208 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2211 data
[pos
] = (pixel
>> 16) & 0xff;
2212 data
[pos
+1] = (pixel
>> 8) & 0xff;
2213 data
[pos
+2] = pixel
& 0xff;
2219 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
2220 if (mask_pixel == 0)
2233 XDestroyImage( ximage
);
2235 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
2241 // OS/2 Presentation manager conversion routings
2243 wxBitmap
wxImage::ConvertToBitmap() const
2246 return wxNullBitmap
;
2247 wxBitmap bitmap
; // remove
2250 int sizeLimit = 1024*768*3;
2252 // width and height of the device-dependent bitmap
2253 int width = GetWidth();
2254 int bmpHeight = GetHeight();
2256 // calc the number of bytes per scanline and padding
2257 int bytePerLine = width*3;
2258 int sizeDWORD = sizeof( DWORD );
2259 int lineBoundary = bytePerLine % sizeDWORD;
2261 if( lineBoundary > 0 )
2263 padding = sizeDWORD - lineBoundary;
2264 bytePerLine += padding;
2266 // calc the number of DIBs and heights of DIBs
2269 int height = sizeLimit/bytePerLine;
2270 if( height >= bmpHeight )
2274 numDIB = bmpHeight / height;
2275 hRemain = bmpHeight % height;
2276 if( hRemain >0 ) numDIB++;
2279 // set bitmap parameters
2281 wxCHECK_MSG( Ok(), bitmap, wxT("invalid image") );
2282 bitmap.SetWidth( width );
2283 bitmap.SetHeight( bmpHeight );
2284 bitmap.SetDepth( wxDisplayDepth() );
2286 // create a DIB header
2287 int headersize = sizeof(BITMAPINFOHEADER);
2288 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
2289 wxCHECK_MSG( lpDIBh, bitmap, wxT("could not allocate memory for DIB header") );
2290 // Fill in the DIB header
2291 lpDIBh->bmiHeader.biSize = headersize;
2292 lpDIBh->bmiHeader.biWidth = (DWORD)width;
2293 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
2294 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
2295 // the general formula for biSizeImage:
2296 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
2297 lpDIBh->bmiHeader.biPlanes = 1;
2298 lpDIBh->bmiHeader.biBitCount = 24;
2299 lpDIBh->bmiHeader.biCompression = BI_RGB;
2300 lpDIBh->bmiHeader.biClrUsed = 0;
2301 // These seem not really needed for our purpose here.
2302 lpDIBh->bmiHeader.biClrImportant = 0;
2303 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
2304 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
2305 // memory for DIB data
2306 unsigned char *lpBits;
2307 lpBits = (unsigned char *)malloc( lpDIBh->bmiHeader.biSizeImage );
2310 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
2315 // create and set the device-dependent bitmap
2316 HDC hdc = ::GetDC(NULL);
2317 HDC memdc = ::CreateCompatibleDC( hdc );
2319 hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight );
2320 ::SelectObject( memdc, hbitmap);
2322 // copy image data into DIB data and then into DDB (in a loop)
2323 unsigned char *data = GetData();
2326 unsigned char *ptdata = data;
2327 unsigned char *ptbits;
2329 for( n=0; n<numDIB; n++ )
2331 if( numDIB > 1 && n == numDIB-1 && hRemain > 0 )
2333 // redefine height and size of the (possibly) last smaller DIB
2334 // memory is not reallocated
2336 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
2337 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
2341 for( j=0; j<height; j++ )
2343 for( i=0; i<width; i++ )
2345 *(ptbits++) = *(ptdata+2);
2346 *(ptbits++) = *(ptdata+1);
2347 *(ptbits++) = *(ptdata );
2350 for( i=0; i< padding; i++ ) *(ptbits++) = 0;
2352 ::StretchDIBits( memdc, 0, origin, width, height,\
2353 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
2355 // if numDIB = 1, lines below can also be used
2356 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
2357 // The above line is equivalent to the following two lines.
2358 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
2359 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
2360 // or the following lines
2361 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
2362 // HDC memdc = ::CreateCompatibleDC( hdc );
2363 // ::SelectObject( memdc, hbitmap);
2364 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
2365 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
2366 // ::SelectObject( memdc, 0 );
2367 // ::DeleteDC( memdc );
2369 bitmap.SetHBITMAP( (WXHBITMAP) hbitmap );
2371 // similarly, created an mono-bitmap for the possible mask
2374 hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
2375 ::SelectObject( memdc, hbitmap);
2376 if( numDIB == 1 ) height = bmpHeight;
2377 else height = sizeLimit/bytePerLine;
2378 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
2379 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
2381 unsigned char r = GetMaskRed();
2382 unsigned char g = GetMaskGreen();
2383 unsigned char b = GetMaskBlue();
2384 unsigned char zero = 0, one = 255;
2386 for( n=0; n<numDIB; n++ )
2388 if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 )
2390 // redefine height and size of the (possibly) last smaller DIB
2391 // memory is not reallocated
2393 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
2394 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
2397 for( int j=0; j<height; j++ )
2399 for(i=0; i<width; i++ )
2401 if( (*(ptdata++)!=r) | (*(ptdata++)!=g) | (*(ptdata++)!=b) )
2414 for( i=0; i< padding; i++ ) *(ptbits++) = zero;
2416 ::StretchDIBits( memdc, 0, origin, width, height,\
2417 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
2420 // create a wxMask object
2421 wxMask *mask = new wxMask();
2422 mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
2423 bitmap.SetMask( mask );
2426 // free allocated resources
2427 ::SelectObject( memdc, 0 );
2428 ::DeleteDC( memdc );
2429 ::ReleaseDC(NULL, hdc);
2433 // check the wxBitmap object
2434 if( bitmap.GetHBITMAP() )
2435 bitmap.SetOk( TRUE );
2437 bitmap.SetOk( FALSE );
2442 wxImage::wxImage( const wxBitmap
&bitmap
)
2447 wxFAIL_MSG( wxT("invalid bitmap") );
2451 // create an wxImage object
2452 int width
= bitmap
.GetWidth();
2453 int height
= bitmap
.GetHeight();
2454 Create( width
, height
);
2455 unsigned char *data
= GetData();
2458 wxFAIL_MSG( wxT("could not allocate data for image") );
2462 // calc the number of bytes per scanline and padding in the DIB
2463 int bytePerLine
= width
*3;
2464 int sizeDWORD
= sizeof( DWORD
);
2465 int lineBoundary
= bytePerLine
% sizeDWORD
;
2467 if( lineBoundary
> 0 )
2469 padding
= sizeDWORD
- lineBoundary
;
2470 bytePerLine
+= padding
;
2474 // create a DIB header
2475 int headersize = sizeof(BITMAPINFOHEADER);
2476 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
2479 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
2483 // Fill in the DIB header
2484 lpDIBh->bmiHeader.biSize = headersize;
2485 lpDIBh->bmiHeader.biWidth = width;
2486 lpDIBh->bmiHeader.biHeight = -height;
2487 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
2488 lpDIBh->bmiHeader.biPlanes = 1;
2489 lpDIBh->bmiHeader.biBitCount = 24;
2490 lpDIBh->bmiHeader.biCompression = BI_RGB;
2491 lpDIBh->bmiHeader.biClrUsed = 0;
2492 // These seem not really needed for our purpose here.
2493 lpDIBh->bmiHeader.biClrImportant = 0;
2494 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
2495 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
2496 // memory for DIB data
2497 unsigned char *lpBits;
2498 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
2501 wxFAIL_MSG( wxT("could not allocate data for DIB") );
2507 // copy data from the device-dependent bitmap to the DIB
2508 HDC hdc = ::GetDC(NULL);
2510 hbitmap = (HBITMAP) bitmap.GetHBITMAP();
2511 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
2513 // copy DIB data into the wxImage object
2515 unsigned char *ptdata = data;
2516 unsigned char *ptbits = lpBits;
2517 for( i=0; i<height; i++ )
2519 for( j=0; j<width; j++ )
2521 *(ptdata++) = *(ptbits+2);
2522 *(ptdata++) = *(ptbits+1);
2523 *(ptdata++) = *(ptbits );
2529 // similarly, set data according to the possible mask bitmap
2530 if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() )
2532 hbitmap = (HBITMAP) bitmap.GetMask()->GetMaskBitmap();
2533 // memory DC created, color set, data copied, and memory DC deleted
2534 HDC memdc = ::CreateCompatibleDC( hdc );
2535 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
2536 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
2537 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
2538 ::DeleteDC( memdc );
2539 // background color set to RGB(16,16,16) in consistent with wxGTK
2540 unsigned char r=16, g=16, b=16;
2543 for( i=0; i<height; i++ )
2545 for( j=0; j<width; j++ )
2559 SetMaskColour( r, g, b );
2566 // free allocated resources
2567 ::ReleaseDC(NULL, hdc);
2575 // A module to allow wxImage initialization/cleanup
2576 // without calling these functions from app.cpp or from
2577 // the user's application.
2579 class wxImageModule
: public wxModule
2581 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2584 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
2585 void OnExit() { wxImage::CleanUpHandlers(); };
2588 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2591 //-----------------------------------------------------------------------------
2594 // Counts and returns the number of different colours. Optionally stops
2595 // when it exceeds 'stopafter' different colours. This is useful, for
2596 // example, to see if the image can be saved as 8-bit (256 colour or
2597 // less, in this case it would be invoked as CountColours(256)). Default
2598 // value for stopafter is -1 (don't care).
2600 unsigned long wxImage::CountColours( unsigned long stopafter
)
2605 unsigned char r
, g
, b
, *p
;
2606 unsigned long size
, nentries
, key
;
2609 size
= GetWidth() * GetHeight();
2612 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2617 key
= (r
<< 16) | (g
<< 8) | b
;
2619 hnode
= (wxHNode
*) h
.Get(key
);
2623 h
.Put(key
, (wxObject
*)(new wxHNode
));
2628 // delete all HNodes
2630 while ((node
= h
.Next()) != NULL
)
2631 delete (wxHNode
*)node
->GetData();
2638 // Computes the histogram of the image and fills a hash table, indexed
2639 // with integer keys built as 0xRRGGBB, containing wxHNode objects. Each
2640 // wxHNode contains an 'index' (useful to build a palette with the image
2641 // colours) and a 'value', which is the number of pixels in the image with
2644 unsigned long wxImage::ComputeHistogram( wxHashTable
&h
)
2646 unsigned char r
, g
, b
, *p
;
2647 unsigned long size
, nentries
, key
;
2651 size
= GetWidth() * GetHeight();
2654 for (unsigned long j
= 0; j
< size
; j
++)
2659 key
= (r
<< 16) | (g
<< 8) | b
;
2661 hnode
= (wxHNode
*) h
.Get(key
);
2667 hnode
= new wxHNode();
2668 hnode
->index
= nentries
++;
2671 h
.Put(key
, (wxObject
*)hnode
);