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"
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 class wxImageRefData
: public wxObjectRefData
53 ~wxImageRefData(void);
57 unsigned char *m_data
;
59 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
63 wxImageRefData::wxImageRefData(void)
67 m_data
= (unsigned char*) NULL
;
75 wxImageRefData::~wxImageRefData(void)
77 if (m_data
) free( m_data
);
80 wxList
wxImage::sm_handlers
;
82 //-----------------------------------------------------------------------------
84 #define M_IMGDATA ((wxImageRefData *)m_refData)
86 #if !USE_SHARED_LIBRARIES
87 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
94 wxImage::wxImage( int width
, int height
)
96 Create( width
, height
);
99 wxImage::wxImage( const wxString
& name
, long type
)
101 LoadFile( name
, type
);
104 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
)
106 LoadFile( name
, mimetype
);
110 wxImage::wxImage( wxInputStream
& stream
, long type
)
112 LoadFile( stream
, type
);
115 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
)
117 LoadFile( stream
, mimetype
);
119 #endif // wxUSE_STREAMS
121 wxImage::wxImage( const wxImage
& image
)
126 wxImage::wxImage( const wxImage
* image
)
128 if (image
) Ref(*image
);
131 void wxImage::Create( int width
, int height
)
133 m_refData
= new wxImageRefData();
135 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
136 if (M_IMGDATA
->m_data
)
138 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
140 M_IMGDATA
->m_width
= width
;
141 M_IMGDATA
->m_height
= height
;
142 M_IMGDATA
->m_ok
= TRUE
;
150 void wxImage::Destroy()
155 wxImage
wxImage::Scale( int width
, int height
)
159 wxCHECK_MSG( Ok(), image
, _T("invalid image") );
161 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, _T("invalid image size") );
163 image
.Create( width
, height
);
165 char unsigned *data
= image
.GetData();
167 wxCHECK_MSG( data
, image
, _T("unable to create image") );
169 if (M_IMGDATA
->m_hasMask
)
170 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
172 long old_height
= M_IMGDATA
->m_height
;
173 long old_width
= M_IMGDATA
->m_width
;
175 char unsigned *source_data
= M_IMGDATA
->m_data
;
176 char unsigned *target_data
= data
;
178 for (long j
= 0; j
< height
; j
++)
180 long y_offset
= (j
* old_height
/ height
) * old_width
;
182 for (long i
= 0; i
< width
; i
++)
185 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
194 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
196 wxCHECK_RET( Ok(), _T("invalid image") );
198 int w
= M_IMGDATA
->m_width
;
199 int h
= M_IMGDATA
->m_height
;
201 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), _T("invalid image index") );
203 long pos
= (y
* w
+ x
) * 3;
205 M_IMGDATA
->m_data
[ pos
] = r
;
206 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
207 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
210 unsigned char wxImage::GetRed( int x
, int y
)
212 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
214 int w
= M_IMGDATA
->m_width
;
215 int h
= M_IMGDATA
->m_height
;
217 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
219 long pos
= (y
* w
+ x
) * 3;
221 return M_IMGDATA
->m_data
[pos
];
224 unsigned char wxImage::GetGreen( int x
, int y
)
226 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
228 int w
= M_IMGDATA
->m_width
;
229 int h
= M_IMGDATA
->m_height
;
231 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
233 long pos
= (y
* w
+ x
) * 3;
235 return M_IMGDATA
->m_data
[pos
+1];
238 unsigned char wxImage::GetBlue( int x
, int y
)
240 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
242 int w
= M_IMGDATA
->m_width
;
243 int h
= M_IMGDATA
->m_height
;
245 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
247 long pos
= (y
* w
+ x
) * 3;
249 return M_IMGDATA
->m_data
[pos
+2];
252 bool wxImage::Ok() const
254 return (M_IMGDATA
&& M_IMGDATA
->m_ok
);
257 char unsigned *wxImage::GetData() const
259 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, _T("invalid image") );
261 return M_IMGDATA
->m_data
;
264 void wxImage::SetData( char unsigned *data
)
266 wxCHECK_RET( Ok(), _T("invalid image") );
268 memcpy(M_IMGDATA
->m_data
, data
, M_IMGDATA
->m_width
* M_IMGDATA
->m_height
* 3);
271 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
273 wxCHECK_RET( Ok(), _T("invalid image") );
275 M_IMGDATA
->m_maskRed
= r
;
276 M_IMGDATA
->m_maskGreen
= g
;
277 M_IMGDATA
->m_maskBlue
= b
;
278 M_IMGDATA
->m_hasMask
= TRUE
;
281 unsigned char wxImage::GetMaskRed() const
283 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
285 return M_IMGDATA
->m_maskRed
;
288 unsigned char wxImage::GetMaskGreen() const
290 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
292 return M_IMGDATA
->m_maskGreen
;
295 unsigned char wxImage::GetMaskBlue() const
297 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
299 return M_IMGDATA
->m_maskBlue
;
302 void wxImage::SetMask( bool mask
)
304 wxCHECK_RET( Ok(), _T("invalid image") );
306 M_IMGDATA
->m_hasMask
= mask
;
309 bool wxImage::HasMask() const
311 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
313 return M_IMGDATA
->m_hasMask
;
316 int wxImage::GetWidth() const
318 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
320 return M_IMGDATA
->m_width
;
323 int wxImage::GetHeight() const
325 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
327 return M_IMGDATA
->m_height
;
330 bool wxImage::LoadFile( const wxString
& filename
, long type
)
333 if (wxFileExists(filename
))
335 wxFileInputStream
stream(filename
);
336 return LoadFile(stream
, type
);
340 wxLogError( _T("Can't load image from file '%s': file does not exist."), filename
.c_str() );
344 #else // !wxUSE_STREAMS
346 #endif // wxUSE_STREAMS
349 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
)
352 if (wxFileExists(filename
))
354 wxFileInputStream
stream(filename
);
355 return LoadFile(stream
, mimetype
);
359 wxLogError( _T("Can't load image from file '%s': file does not exist."), filename
.c_str() );
363 #else // !wxUSE_STREAMS
365 #endif // wxUSE_STREAMS
368 bool wxImage::SaveFile( const wxString
& filename
, int type
)
371 wxFileOutputStream
stream(filename
);
373 if ( stream
.LastError() == wxStream_NOERROR
)
374 return SaveFile(stream
, type
);
376 #endif // wxUSE_STREAMS
380 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
)
383 wxFileOutputStream
stream(filename
);
385 if ( stream
.LastError() == wxStream_NOERROR
)
386 return SaveFile(stream
, mimetype
);
388 #endif // wxUSE_STREAMS
393 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
397 m_refData
= new wxImageRefData
;
399 wxImageHandler
*handler
= FindHandler(type
);
403 wxLogWarning( _T("No image handler for type %d defined."), type
);
408 return handler
->LoadFile( this, stream
);
411 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
415 m_refData
= new wxImageRefData
;
417 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
421 wxLogWarning( _T("No image handler for type %s defined."), mimetype
.GetData() );
426 return handler
->LoadFile( this, stream
);
429 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
431 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
433 wxImageHandler
*handler
= FindHandler(type
);
437 wxLogWarning( _T("No image handler for type %d defined."), type
);
442 return handler
->SaveFile( this, stream
);
445 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
447 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
449 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
453 wxLogWarning( _T("No image handler for type %s defined."), mimetype
.GetData() );
458 return handler
->SaveFile( this, stream
);
460 #endif // wxUSE_STREAMS
462 void wxImage::AddHandler( wxImageHandler
*handler
)
464 // make sure that the memory will be freed at the program end
465 sm_handlers
.DeleteContents(TRUE
);
467 sm_handlers
.Append( handler
);
470 void wxImage::InsertHandler( wxImageHandler
*handler
)
472 // make sure that the memory will be freed at the program end
473 sm_handlers
.DeleteContents(TRUE
);
475 sm_handlers
.Insert( handler
);
478 bool wxImage::RemoveHandler( const wxString
& name
)
480 wxImageHandler
*handler
= FindHandler(name
);
483 sm_handlers
.DeleteObject(handler
);
490 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
492 wxNode
*node
= sm_handlers
.First();
495 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
496 if (handler
->GetName().Cmp(name
) == 0) return handler
;
500 return (wxImageHandler
*)NULL
;
503 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
505 wxNode
*node
= sm_handlers
.First();
508 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
509 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
510 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
514 return (wxImageHandler
*)NULL
;
517 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
519 wxNode
*node
= sm_handlers
.First();
522 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
523 if (handler
->GetType() == bitmapType
) return handler
;
529 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
531 wxNode
*node
= sm_handlers
.First();
534 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
535 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
541 void wxImage::InitStandardHandlers()
543 AddHandler( new wxBMPHandler
);
546 void wxImage::CleanUpHandlers()
548 wxNode
*node
= sm_handlers
.First();
551 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
552 wxNode
*next
= node
->Next();
559 //-----------------------------------------------------------------------------
561 //-----------------------------------------------------------------------------
563 #if !USE_SHARED_LIBRARIES
564 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
568 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
) )
573 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
) )
577 #endif // wxUSE_STREAMS
579 //-----------------------------------------------------------------------------
581 //-----------------------------------------------------------------------------
583 #if !USE_SHARED_LIBRARIES
584 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
588 bool wxBMPHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
590 unsigned char *data
, *ptr
;
591 int done
, i
, bpp
, planes
, comp
, ncolors
, line
, column
,
592 linesize
, linepos
, rshift
= 0, gshift
= 0, bshift
= 0;
595 long int dbuf
[4], dword
, rmask
= 0, gmask
= 0, bmask
= 0, offset
,
597 off_t start_offset
= stream
.TellI();
601 unsigned char r
, g
, b
;
611 #define BI_BITFIELDS 3
618 * Reading the bmp header
621 stream
.Read(&bbuf
, 2);
623 stream
.Read(dbuf
, 4 * 4);
628 stream
.Read(dbuf
, 4 * 2);
629 int width
= (int)dbuf
[0];
630 int height
= (int)dbuf
[1];
633 wxLogError( _T("Image width > 32767 pixels for file.") );
638 wxLogError( _T("Image height > 32767 pixels for file.") );
641 stream
.Read(&word
, 2);
643 stream
.Read(&word
, 2);
645 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32)
647 wxLogError( _T("unknown bitdepth in file.") );
650 stream
.Read(dbuf
, 4 * 4);
652 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
654 wxLogError( _T("unknown encoding in Windows BMP file.") );
657 stream
.Read(dbuf
, 4 * 2);
658 ncolors
= (int)dbuf
[0];
661 /* some more sanity checks */
662 if (((comp
== BI_RLE4
) && (bpp
!= 4)) || ((comp
== BI_RLE8
) && (bpp
!= 8)) || ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
664 wxLogError( _T("encoding of BMP doesn't match bitdepth.") );
669 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
673 wxLogError( _T("Cannot allocate RAM for color map in BMP file.") );
680 image
->Create( width
, height
);
681 ptr
= image
->GetData();
684 wxLogError( _T("Cannot allocate RAM for RGB data in file.") );
691 * Reading the palette, if it exists.
693 if (bpp
< 16 && ncolors
!= 0)
695 for (i
= 0; i
< ncolors
; i
++)
697 stream
.Read(bbuf
, 4);
703 else if (bpp
== 16 || bpp
== 32)
705 if (comp
== BI_BITFIELDS
)
709 stream
.Read(dbuf
, 4 * 3);
713 /* find shift amount.. ugly, but i can't think of a better way */
714 for (bit
= 0; bit
< bpp
; bit
++)
716 if (bmask
& (1 << bit
))
718 if (gmask
& (1 << bit
))
720 if (rmask
& (1 << bit
))
745 * Reading the image data
747 stream
.SeekI(start_offset
+ offset
);
750 /* set the whole image to the background color */
751 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
753 for (i
= 0; i
< width
* height
; i
++)
763 #define poffset (line * width * 3 + column * 3)
766 * BMPs are stored upside down... hmmmmmmmmmm....
769 linesize
= ((width
* bpp
+ 31) / 32) * 4;
770 for (line
= (height
- 1); line
>= 0; line
--)
773 for (column
= 0; column
< width
;)
780 aByte
= stream
.GetC();
785 for (bit
= 0; bit
< 8; bit
++)
787 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
788 ptr
[poffset
] = cmap
[index
].r
;
789 ptr
[poffset
+ 1] = cmap
[index
].g
;
790 ptr
[poffset
+ 2] = cmap
[index
].b
;
798 wxLogError( _T("can't deal with 4bit encoded yet.") );
807 for (nibble
= 0; nibble
< 2; nibble
++)
809 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
812 ptr
[poffset
] = cmap
[index
].r
;
813 ptr
[poffset
+ 1] = cmap
[index
].g
;
814 ptr
[poffset
+ 2] = cmap
[index
].b
;
826 aByte
= stream
.GetC();
831 /* column = width; */
840 aByte
= stream
.GetC();
842 linepos
= column
* bpp
/ 8;
843 aByte
= stream
.GetC();
848 int absolute
= aByte
;
850 for (i
= 0; i
< absolute
; i
++)
853 aByte
= stream
.GetC();
854 ptr
[poffset
] = cmap
[aByte
].r
;
855 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
856 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
860 aByte
= stream
.GetC();
865 for (i
= 0; i
< first
; i
++)
867 ptr
[poffset
] = cmap
[aByte
].r
;
868 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
869 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
877 ptr
[poffset
] = cmap
[aByte
].r
;
878 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
879 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
887 stream
.Read(&bbuf
, 3);
889 ptr
[poffset
] = (unsigned char)bbuf
[2];
890 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
891 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
898 stream
.Read(&word
, 2);
900 temp
= (word
& rmask
) >> rshift
;
902 temp
= (word
& gmask
) >> gshift
;
903 ptr
[poffset
+ 1] = temp
;
904 temp
= (word
& bmask
) >> gshift
;
905 ptr
[poffset
+ 2] = temp
;
912 stream
.Read(&dword
, 4);
914 temp
= (dword
& rmask
) >> rshift
;
916 temp
= (dword
& gmask
) >> gshift
;
917 ptr
[poffset
+ 1] = temp
;
918 temp
= (dword
& bmask
) >> bshift
;
919 ptr
[poffset
+ 2] = temp
;
923 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
925 stream
.Read(&aByte
, 1);
927 if (stream
.LastError() != wxStream_NOERROR
)
931 if (cmap
) free(cmap
);
933 image
->SetMask( FALSE
);
937 #endif // wxUSE_STREAMS
941 wxBitmap
wxImage::ConvertToBitmap() const
943 // sizeLimit is the MS upper limit for the DIB size
944 int sizeLimit
= 1024*768*3;
946 // width and height of the device-dependent bitmap
947 int width
= GetWidth();
948 int bmpHeight
= GetHeight();
950 // calc the number of bytes per scanline and padding
951 int bytePerLine
= width
*3;
952 int sizeDWORD
= sizeof( DWORD
);
953 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
955 if( lineBoundary
.rem
> 0 )
957 padding
= sizeDWORD
- lineBoundary
.rem
;
958 bytePerLine
+= padding
;
960 // calc the number of DIBs and heights of DIBs
963 int height
= sizeLimit
/bytePerLine
;
964 if( height
>= bmpHeight
)
968 div_t result
= div( bmpHeight
, height
);
969 numDIB
= result
.quot
;
970 hRemain
= result
.rem
;
971 if( hRemain
>0 ) numDIB
++;
974 // set bitmap parameters
976 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
977 bitmap
.SetWidth( width
);
978 bitmap
.SetHeight( bmpHeight
);
979 bitmap
.SetDepth( wxDisplayDepth() );
981 // create a DIB header
982 int headersize
= sizeof(BITMAPINFOHEADER
);
983 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
984 wxCHECK_MSG( lpDIBh
, bitmap
, _T("could not allocate memory for DIB header") );
985 // Fill in the DIB header
986 lpDIBh
->bmiHeader
.biSize
= headersize
;
987 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
988 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
989 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
990 // the general formula for biSizeImage:
991 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
992 lpDIBh
->bmiHeader
.biPlanes
= 1;
993 lpDIBh
->bmiHeader
.biBitCount
= 24;
994 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
995 lpDIBh
->bmiHeader
.biClrUsed
= 0;
996 // These seem not really needed for our purpose here.
997 lpDIBh
->bmiHeader
.biClrImportant
= 0;
998 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
999 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1000 // memory for DIB data
1001 unsigned char *lpBits
;
1002 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1005 wxFAIL_MSG( _T("could not allocate memory for DIB") );
1010 // create and set the device-dependent bitmap
1011 HDC hdc
= ::GetDC(NULL
);
1012 HDC memdc
= ::CreateCompatibleDC( hdc
);
1014 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
1015 ::SelectObject( memdc
, hbitmap
);
1017 // copy image data into DIB data and then into DDB (in a loop)
1018 unsigned char *data
= GetData();
1021 unsigned char *ptdata
= data
;
1022 unsigned char *ptbits
;
1024 for( n
=0; n
<numDIB
; n
++ )
1026 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
1028 // redefine height and size of the (possibly) last smaller DIB
1029 // memory is not reallocated
1031 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1032 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1036 for( j
=0; j
<height
; j
++ )
1038 for( i
=0; i
<width
; i
++ )
1040 *(ptbits
++) = *(ptdata
+2);
1041 *(ptbits
++) = *(ptdata
+1);
1042 *(ptbits
++) = *(ptdata
);
1045 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
1047 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1048 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1050 // if numDIB = 1, lines below can also be used
1051 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
1052 // The above line is equivalent to the following two lines.
1053 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1054 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1055 // or the following lines
1056 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1057 // HDC memdc = ::CreateCompatibleDC( hdc );
1058 // ::SelectObject( memdc, hbitmap);
1059 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
1060 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1061 // ::SelectObject( memdc, 0 );
1062 // ::DeleteDC( memdc );
1064 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
1066 // similarly, created an mono-bitmap for the possible mask
1069 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
1070 ::SelectObject( memdc
, hbitmap
);
1071 if( numDIB
== 1 ) height
= bmpHeight
;
1072 else height
= sizeLimit
/bytePerLine
;
1073 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1074 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1076 unsigned char r
= GetMaskRed();
1077 unsigned char g
= GetMaskGreen();
1078 unsigned char b
= GetMaskBlue();
1079 unsigned char zero
= 0, one
= 255;
1081 for( n
=0; n
<numDIB
; n
++ )
1083 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
1085 // redefine height and size of the (possibly) last smaller DIB
1086 // memory is not reallocated
1088 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1089 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1092 for( int j
=0; j
<height
; j
++ )
1094 for(i
=0; i
<width
; i
++ )
1096 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
1109 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
1111 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1112 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1115 // create a wxMask object
1116 wxMask
*mask
= new wxMask();
1117 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
1118 bitmap
.SetMask( mask
);
1119 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
1120 /* The following can also be used but is slow to run
1121 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
1122 wxMask *mask = new wxMask( bitmap, colour );
1123 bitmap.SetMask( mask );
1127 // free allocated resources
1128 ::SelectObject( memdc
, 0 );
1129 ::DeleteDC( memdc
);
1130 ::ReleaseDC(NULL
, hdc
);
1134 // check the wxBitmap object
1135 if( bitmap
.GetHBITMAP() )
1136 bitmap
.SetOk( TRUE
);
1138 bitmap
.SetOk( FALSE
);
1143 wxImage::wxImage( const wxBitmap
&bitmap
)
1148 wxFAIL_MSG( _T("invalid bitmap") );
1152 // create an wxImage object
1153 int width
= bitmap
.GetWidth();
1154 int height
= bitmap
.GetHeight();
1155 Create( width
, height
);
1156 unsigned char *data
= GetData();
1159 wxFAIL_MSG( _T("could not allocate data for image") );
1163 // calc the number of bytes per scanline and padding in the DIB
1164 int bytePerLine
= width
*3;
1165 int sizeDWORD
= sizeof( DWORD
);
1166 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
1168 if( lineBoundary
.rem
> 0 )
1170 padding
= sizeDWORD
- lineBoundary
.rem
;
1171 bytePerLine
+= padding
;
1174 // create a DIB header
1175 int headersize
= sizeof(BITMAPINFOHEADER
);
1176 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1179 wxFAIL_MSG( _T("could not allocate data for DIB header") );
1183 // Fill in the DIB header
1184 lpDIBh
->bmiHeader
.biSize
= headersize
;
1185 lpDIBh
->bmiHeader
.biWidth
= width
;
1186 lpDIBh
->bmiHeader
.biHeight
= -height
;
1187 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
1188 lpDIBh
->bmiHeader
.biPlanes
= 1;
1189 lpDIBh
->bmiHeader
.biBitCount
= 24;
1190 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1191 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1192 // These seem not really needed for our purpose here.
1193 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1194 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1195 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1196 // memory for DIB data
1197 unsigned char *lpBits
;
1198 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1201 wxFAIL_MSG( _T("could not allocate data for DIB") );
1207 // copy data from the device-dependent bitmap to the DIB
1208 HDC hdc
= ::GetDC(NULL
);
1210 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
1211 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1213 // copy DIB data into the wxImage object
1215 unsigned char *ptdata
= data
;
1216 unsigned char *ptbits
= lpBits
;
1217 for( i
=0; i
<height
; i
++ )
1219 for( j
=0; j
<width
; j
++ )
1221 *(ptdata
++) = *(ptbits
+2);
1222 *(ptdata
++) = *(ptbits
+1);
1223 *(ptdata
++) = *(ptbits
);
1229 // similarly, set data according to the possible mask bitmap
1230 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
1232 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
1233 // memory DC created, color set, data copied, and memory DC deleted
1234 HDC memdc
= ::CreateCompatibleDC( hdc
);
1235 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1236 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1237 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1238 ::DeleteDC( memdc
);
1239 // background color set to RGB(16,16,16) in consistent with wxGTK
1240 unsigned char r
=16, g
=16, b
=16;
1243 for( i
=0; i
<height
; i
++ )
1245 for( j
=0; j
<width
; j
++ )
1259 SetMaskColour( r
, g
, b
);
1266 // free allocated resources
1267 ::ReleaseDC(NULL
, hdc
);
1276 #include "gtk/gtk.h"
1277 #include "gdk/gdk.h"
1278 #include "gdk/gdkx.h"
1280 #if (GTK_MINOR_VERSION > 0)
1281 #include "gdk/gdkrgb.h"
1284 wxBitmap
wxImage::ConvertToBitmap() const
1288 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
1290 int width
= GetWidth();
1291 int height
= GetHeight();
1293 bitmap
.SetHeight( height
);
1294 bitmap
.SetWidth( width
);
1296 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1300 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1301 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1302 int bpp
= visual
->depth
;
1304 bitmap
.SetDepth( bpp
);
1306 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1307 if (bpp
< 8) bpp
= 8;
1309 #if (GTK_MINOR_VERSION > 0)
1311 if (!HasMask() && (bpp
> 8))
1313 static bool s_hasInitialized
= FALSE
;
1315 if (!s_hasInitialized
)
1318 s_hasInitialized
= TRUE
;
1321 GdkGC
*gc
= gdk_gc_new( bitmap
.GetPixmap() );
1323 gdk_draw_rgb_image( bitmap
.GetPixmap(),
1327 GDK_RGB_DITHER_NONE
,
1338 // Create picture image
1340 GdkImage
*data_image
=
1341 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1343 // Create mask image
1345 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1349 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1351 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1353 wxMask
*mask
= new wxMask();
1354 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1356 bitmap
.SetMask( mask
);
1361 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1362 byte_order b_o
= RGB
;
1366 GdkVisual
*visual
= gdk_visual_get_system();
1367 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1368 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1369 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1370 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1371 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1372 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1375 int r_mask
= GetMaskRed();
1376 int g_mask
= GetMaskGreen();
1377 int b_mask
= GetMaskBlue();
1379 unsigned char* data
= GetData();
1382 for (int y
= 0; y
< height
; y
++)
1384 for (int x
= 0; x
< width
; x
++)
1386 int r
= data
[index
];
1388 int g
= data
[index
];
1390 int b
= data
[index
];
1395 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1396 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1398 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1403 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1404 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1406 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1414 if (wxTheApp
->m_colorCube
)
1416 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1420 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1421 GdkColor
*colors
= cmap
->colors
;
1422 int max
= 3 * (65536);
1424 for (int i
= 0; i
< cmap
->size
; i
++)
1426 int rdiff
= (r
<< 8) - colors
[i
].red
;
1427 int gdiff
= (g
<< 8) - colors
[i
].green
;
1428 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1429 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1430 if (sum
< max
) { pixel
= i
; max
= sum
; }
1434 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1440 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1441 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1446 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1447 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1456 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1457 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1458 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1459 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1460 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1461 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1463 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1472 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1474 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1476 gdk_image_destroy( data_image
);
1477 gdk_gc_unref( data_gc
);
1483 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1485 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1487 gdk_image_destroy( mask_image
);
1488 gdk_gc_unref( mask_gc
);
1494 wxImage::wxImage( const wxBitmap
&bitmap
)
1496 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1498 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1500 bitmap
.GetWidth(), bitmap
.GetHeight() );
1502 wxCHECK_RET( gdk_image
, _T("couldn't create image") );
1504 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1505 char unsigned *data
= GetData();
1509 gdk_image_destroy( gdk_image
);
1510 wxFAIL_MSG( _T("couldn't create image") );
1514 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1515 if (bitmap
.GetMask())
1517 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1519 bitmap
.GetWidth(), bitmap
.GetHeight() );
1521 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1524 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1525 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1526 int bpp
= visual
->depth
;
1527 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1529 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1532 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1534 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1536 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1539 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1540 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1541 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1542 } else if (bpp
== 15)
1544 data
[pos
] = (pixel
>> 7) & 0xf8;
1545 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1546 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1547 } else if (bpp
== 16)
1549 data
[pos
] = (pixel
>> 8) & 0xf8;
1550 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1551 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1554 data
[pos
] = (pixel
>> 16) & 0xff;
1555 data
[pos
+1] = (pixel
>> 8) & 0xff;
1556 data
[pos
+2] = pixel
& 0xff;
1561 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1562 if (mask_pixel
== 0)
1574 gdk_image_destroy( gdk_image
);
1575 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1583 #include "wx/utils.h"
1586 wxBitmap
wxImage::ConvertToBitmap() const
1590 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
1592 int width
= GetWidth();
1593 int height
= GetHeight();
1595 bitmap
.SetHeight( height
);
1596 bitmap
.SetWidth( width
);
1598 Display
*dpy
= (Display
*) wxGetDisplay();
1599 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1600 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1604 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1605 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
1607 bitmap
.Create( width
, height
, bpp
);
1612 GdkImage *mask_image = (GdkImage*) NULL;
1616 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1618 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1620 wxMask *mask = new wxMask();
1621 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1623 bitmap.SetMask( mask );
1627 // Retrieve depth info
1629 XVisualInfo vinfo_template
;
1632 vinfo_template
.visual
= vis
;
1633 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1634 vinfo_template
.depth
= bpp
;
1637 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1639 wxCHECK_MSG( vi
, wxNullBitmap
, _T("no visual") );
1643 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1644 if (bpp
< 8) bpp
= 8;
1648 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1649 byte_order b_o
= RGB
;
1653 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
1654 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
1655 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
1656 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
1657 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
1658 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
1662 int r_mask = GetMaskRed();
1663 int g_mask = GetMaskGreen();
1664 int b_mask = GetMaskBlue();
1670 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
1672 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1673 XQueryColors( dpy
, cmap
, colors
, 256 );
1676 unsigned char* data
= GetData();
1679 for (int y
= 0; y
< height
; y
++)
1681 for (int x
= 0; x
< width
; x
++)
1683 int r
= data
[index
];
1685 int g
= data
[index
];
1687 int b
= data
[index
];
1693 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1694 gdk_image_put_pixel( mask_image, x, y, 1 );
1696 gdk_image_put_pixel( mask_image, x, y, 0 );
1706 if (wxTheApp->m_colorCube)
1708 pixel = wxTheApp->m_colorCube
1709 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1714 int max
= 3 * (65536);
1715 for (int i
= 0; i
< 256; i
++)
1717 int rdiff
= (r
<< 8) - colors
[i
].red
;
1718 int gdiff
= (g
<< 8) - colors
[i
].green
;
1719 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1720 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
1721 if (sum
< max
) { pixel
= i
; max
= sum
; }
1726 XPutPixel( data_image
, x
, y
, pixel
);
1731 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1732 XPutPixel( data_image
, x
, y
, pixel
);
1737 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1738 XPutPixel( data_image
, x
, y
, pixel
);
1747 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1748 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1749 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1750 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1751 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1752 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1754 XPutPixel( data_image
, x
, y
, pixel
);
1764 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
1765 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
1766 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
1768 XDestroyImage( data_image
);
1776 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1778 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1780 gdk_image_destroy( mask_image );
1781 gdk_gc_unref( mask_gc );
1788 wxImage::wxImage( const wxBitmap
&bitmap
)
1790 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1792 Display
*dpy
= (Display
*) wxGetDisplay();
1793 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1794 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1796 XImage
*ximage
= XGetImage( dpy
,
1797 (Drawable
)bitmap
.GetPixmap(),
1799 bitmap
.GetWidth(), bitmap
.GetHeight(),
1800 AllPlanes
, ZPixmap
);
1802 wxCHECK_RET( ximage
, _T("couldn't create image") );
1804 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1805 char unsigned *data
= GetData();
1809 XDestroyImage( ximage
);
1810 wxFAIL_MSG( _T("couldn't create image") );
1815 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1816 if (bitmap.GetMask())
1818 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1820 bitmap.GetWidth(), bitmap.GetHeight() );
1822 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1826 // Retrieve depth info
1828 XVisualInfo vinfo_template
;
1831 vinfo_template
.visual
= vis
;
1832 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1833 vinfo_template
.depth
= bpp
;
1836 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1838 wxCHECK_MSG( vi
, wxNullBitmap
, _T("no visual") );
1840 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1847 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
1849 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1850 XQueryColors( dpy
, cmap
, colors
, 256 );
1854 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1856 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1858 int pixel
= XGetPixel( ximage
, i
, j
);
1861 data
[pos
] = colors
[pixel
].red
>> 8;
1862 data
[pos
+1] = colors
[pixel
].green
>> 8;
1863 data
[pos
+2] = colors
[pixel
].blue
>> 8;
1864 } else if (bpp
== 15)
1866 data
[pos
] = (pixel
>> 7) & 0xf8;
1867 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1868 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1869 } else if (bpp
== 16)
1871 data
[pos
] = (pixel
>> 8) & 0xf8;
1872 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1873 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1876 data
[pos
] = (pixel
>> 16) & 0xff;
1877 data
[pos
+1] = (pixel
>> 8) & 0xff;
1878 data
[pos
+2] = pixel
& 0xff;
1884 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1885 if (mask_pixel == 0)
1898 XDestroyImage( ximage
);
1900 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1905 // A module to allow wxImage initialization/cleanup
1906 // without calling these functions from app.cpp or from
1907 // the user's application.
1909 class wxImageModule
: public wxModule
1911 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1914 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1915 void OnExit() { wxImage::CleanUpHandlers(); };
1918 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)