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"
27 #include "../png/png.h"
29 #include "wx/filefn.h"
30 #include "wx/wfstream.h"
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 class wxImageRefData
: public wxObjectRefData
46 ~wxImageRefData(void);
50 unsigned char *m_data
;
52 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
56 wxImageRefData::wxImageRefData(void)
60 m_data
= (unsigned char*) NULL
;
68 wxImageRefData::~wxImageRefData(void)
70 if (m_data
) free( m_data
);
73 wxList
wxImage::sm_handlers
;
75 //-----------------------------------------------------------------------------
77 #define M_IMGDATA ((wxImageRefData *)m_refData)
79 #if !USE_SHARED_LIBRARIES
80 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
87 wxImage::wxImage( int width
, int height
)
89 Create( width
, height
);
92 wxImage::wxImage( const wxString
& name
, long type
)
94 LoadFile( name
, type
);
97 wxImage::wxImage( wxInputStream
& stream
, long type
)
99 LoadFile( stream
, type
);
102 wxImage::wxImage( const wxImage
& image
)
107 wxImage::wxImage( const wxImage
* image
)
109 if (image
) Ref(*image
);
112 void wxImage::Create( int width
, int height
)
114 m_refData
= new wxImageRefData();
116 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
117 if (M_IMGDATA
->m_data
)
119 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
121 M_IMGDATA
->m_width
= width
;
122 M_IMGDATA
->m_height
= height
;
123 M_IMGDATA
->m_ok
= TRUE
;
131 void wxImage::Destroy()
136 wxImage
wxImage::Scale( int width
, int height
)
140 wxCHECK_MSG( Ok(), image
, "invlaid image" );
142 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, "invalid image size" );
144 image
.Create( width
, height
);
146 char unsigned *data
= image
.GetData();
148 wxCHECK_MSG( data
, image
, "unable to create image" );
150 if (M_IMGDATA
->m_hasMask
)
151 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
153 double xscale
= (double)width
/ (double)M_IMGDATA
->m_width
;
154 double yscale
= (double)height
/ (double)M_IMGDATA
->m_height
;
156 for (int j
= 0; j
< height
; j
++)
158 for (int i
= 0; i
< width
; i
++)
160 int new_pos
= 3*(j
*width
+ i
);
161 int old_pos
= 3*((long)(j
/yscale
)*M_IMGDATA
->m_width
+ (long)(i
/xscale
));
162 data
[ new_pos
] = M_IMGDATA
->m_data
[ old_pos
];
163 data
[ new_pos
+1 ] = M_IMGDATA
->m_data
[ old_pos
+1 ];
164 data
[ new_pos
+2 ] = M_IMGDATA
->m_data
[ old_pos
+2 ];
171 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
173 wxCHECK_RET( Ok(), "invalid image" );
175 int w
= M_IMGDATA
->m_width
;
176 int h
= M_IMGDATA
->m_height
;
178 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), "invalid image index" );
180 long pos
= (y
* w
+ x
) * 3;
182 M_IMGDATA
->m_data
[ pos
] = r
;
183 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
184 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
187 unsigned char wxImage::GetRed( int x
, int y
)
189 wxCHECK_MSG( Ok(), 0, "invalid image" );
191 int w
= M_IMGDATA
->m_width
;
192 int h
= M_IMGDATA
->m_height
;
194 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
196 long pos
= (y
* w
+ x
) * 3;
198 return M_IMGDATA
->m_data
[pos
];
201 unsigned char wxImage::GetGreen( int x
, int y
)
203 wxCHECK_MSG( Ok(), 0, "invalid image" );
205 int w
= M_IMGDATA
->m_width
;
206 int h
= M_IMGDATA
->m_height
;
208 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
210 long pos
= (y
* w
+ x
) * 3;
212 return M_IMGDATA
->m_data
[pos
+1];
215 unsigned char wxImage::GetBlue( int x
, int y
)
217 wxCHECK_MSG( Ok(), 0, "invalid image" );
219 int w
= M_IMGDATA
->m_width
;
220 int h
= M_IMGDATA
->m_height
;
222 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
224 long pos
= (y
* w
+ x
) * 3;
226 return M_IMGDATA
->m_data
[pos
+2];
229 bool wxImage::Ok() const
231 return (M_IMGDATA
&& M_IMGDATA
->m_ok
);
234 char unsigned *wxImage::GetData() const
236 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, "invalid image" );
238 return M_IMGDATA
->m_data
;
241 void wxImage::SetData( char unsigned *WXUNUSED(data
) )
243 wxCHECK_RET( Ok(), "invalid image" );
246 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
248 wxCHECK_RET( Ok(), "invalid image" );
250 M_IMGDATA
->m_maskRed
= r
;
251 M_IMGDATA
->m_maskGreen
= g
;
252 M_IMGDATA
->m_maskBlue
= b
;
253 M_IMGDATA
->m_hasMask
= TRUE
;
256 unsigned char wxImage::GetMaskRed() const
258 wxCHECK_MSG( Ok(), 0, "invalid image" );
260 return M_IMGDATA
->m_maskRed
;
263 unsigned char wxImage::GetMaskGreen() const
265 wxCHECK_MSG( Ok(), 0, "invalid image" );
267 return M_IMGDATA
->m_maskGreen
;
270 unsigned char wxImage::GetMaskBlue() const
272 wxCHECK_MSG( Ok(), 0, "invalid image" );
274 return M_IMGDATA
->m_maskBlue
;
277 void wxImage::SetMask( bool mask
)
279 wxCHECK_RET( Ok(), "invalid image" );
281 M_IMGDATA
->m_hasMask
= mask
;
284 bool wxImage::HasMask() const
286 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
288 return M_IMGDATA
->m_hasMask
;
291 int wxImage::GetWidth() const
293 wxCHECK_MSG( Ok(), 0, "invalid image" );
295 return M_IMGDATA
->m_width
;
298 int wxImage::GetHeight() const
300 wxCHECK_MSG( Ok(), 0, "invalid image" );
302 return M_IMGDATA
->m_height
;
305 bool wxImage::LoadFile( const wxString
& filename
, long type
)
307 if (wxFileExists(filename
))
309 wxFileInputStream
stream(filename
);
310 return LoadFile(stream
, type
);
314 wxLogWarning( "Image file does not exist." );
320 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
324 m_refData
= new wxImageRefData
;
326 wxImageHandler
*handler
= FindHandler(type
);
330 wxLogWarning( "No image handler for type %d defined.", type
);
335 return handler
->LoadFile( this, stream
);
338 bool wxImage::SaveFile( const wxString
& filename
, int type
)
340 wxFileOutputStream
stream(filename
);
342 if ( stream
.LastError() == wxStream_NOERROR
)
343 return SaveFile(stream
, type
);
348 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
350 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
352 wxImageHandler
*handler
= FindHandler(type
);
356 wxLogWarning( "No image handler for type %d defined.", type
);
361 return handler
->SaveFile( this, stream
);
364 void wxImage::AddHandler( wxImageHandler
*handler
)
366 // make sure that the memory will be freed at the program end
367 sm_handlers
.DeleteContents(TRUE
);
369 sm_handlers
.Append( handler
);
372 void wxImage::InsertHandler( wxImageHandler
*handler
)
374 // make sure that the memory will be freed at the program end
375 sm_handlers
.DeleteContents(TRUE
);
377 sm_handlers
.Insert( handler
);
380 bool wxImage::RemoveHandler( const wxString
& name
)
382 wxImageHandler
*handler
= FindHandler(name
);
385 sm_handlers
.DeleteObject(handler
);
392 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
394 wxNode
*node
= sm_handlers
.First();
397 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
398 if (handler
->GetName() == name
) return handler
;
401 return (wxImageHandler
*)NULL
;
404 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
406 wxNode
*node
= sm_handlers
.First();
409 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
410 if ( handler
->GetExtension() == extension
&&
411 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
415 return (wxImageHandler
*)NULL
;
418 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
420 wxNode
*node
= sm_handlers
.First();
423 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
424 if (handler
->GetType() == bitmapType
) return handler
;
430 void wxImage::InitStandardHandlers()
432 AddHandler( new wxBMPHandler
);
434 AddHandler( new wxPNGHandler
);
438 void wxImage::CleanUpHandlers()
440 wxNode
*node
= sm_handlers
.First();
443 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
444 wxNode
*next
= node
->Next();
451 //-----------------------------------------------------------------------------
453 //-----------------------------------------------------------------------------
455 #if !USE_SHARED_LIBRARIES
456 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
459 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
) )
464 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
) )
469 //-----------------------------------------------------------------------------
471 //-----------------------------------------------------------------------------
475 #if !USE_SHARED_LIBRARIES
476 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler
,wxImageHandler
)
480 static void _PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
482 ((wxInputStream
*) png_get_io_ptr( png_ptr
)) -> Read(data
, length
);
485 static void _PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
487 ((wxOutputStream
*) png_get_io_ptr( png_ptr
)) -> Write(data
, length
);
490 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
492 // VZ: as this function uses setjmp() the only fool proof error handling
493 // method is to use goto (setjmp is not really C++ dtors friendly...)
495 unsigned char **lines
= (unsigned char **) NULL
;
497 png_infop info_ptr
= (png_infop
) NULL
;
501 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
503 (png_error_ptr
) NULL
,
504 (png_error_ptr
) NULL
);
508 info_ptr
= png_create_info_struct( png_ptr
);
512 if (setjmp(png_ptr
->jmpbuf
))
515 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
518 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
520 png_uint_32 width
,height
;
521 int bit_depth
,color_type
,interlace_type
;
523 png_read_info( png_ptr
, info_ptr
);
524 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
526 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
527 png_set_expand( png_ptr
);
529 png_set_strip_16( png_ptr
);
530 png_set_packing( png_ptr
);
531 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
532 png_set_expand( png_ptr
);
533 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
535 image
->Create( width
, height
);
540 lines
= (unsigned char **)malloc( height
* sizeof(unsigned char *) );
544 for (i
= 0; i
< height
; i
++)
546 if ((lines
[i
] = (unsigned char *)malloc(width
* (sizeof(unsigned char) * 4))) == NULL
)
548 for ( unsigned int n
= 0; n
< i
; n
++ )
554 // loaded successfully!
557 png_read_image( png_ptr
, lines
);
558 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
559 unsigned char *ptr
= image
->GetData();
560 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
561 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
563 for (unsigned int y
= 0; y
< height
; y
++)
565 unsigned char *ptr2
= lines
[y
];
566 for (unsigned int x
= 0; x
< width
; x
++)
568 unsigned char r
= *ptr2
++;
569 unsigned char a
= *ptr2
++;
588 for (unsigned int y
= 0; y
< height
; y
++)
590 unsigned char *ptr2
= lines
[y
];
591 for (unsigned int x
= 0; x
< width
; x
++)
593 unsigned char r
= *ptr2
++;
594 unsigned char g
= *ptr2
++;
595 unsigned char b
= *ptr2
++;
596 unsigned char a
= *ptr2
++;
606 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
615 for ( unsigned int j
= 0; j
< height
; j
++ )
621 image
->SetMaskColour( 255, 0, 255 );
625 image
->SetMask( FALSE
);
632 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
648 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
652 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
658 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
661 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
667 png_infop info_ptr
= png_create_info_struct(png_ptr
);
668 if (info_ptr
== NULL
)
670 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
674 if (setjmp(png_ptr
->jmpbuf
))
676 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
680 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
682 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
683 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
684 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
691 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
692 png_write_info( png_ptr
, info_ptr
);
693 png_set_shift( png_ptr
, &sig_bit
);
694 png_set_packing( png_ptr
);
696 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
699 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
703 for (int y
= 0; y
< image
->GetHeight(); y
++)
705 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
706 for (int x
= 0; x
< image
->GetWidth(); x
++)
708 data
[(x
<< 2) + 0] = *ptr
++;
709 data
[(x
<< 2) + 1] = *ptr
++;
710 data
[(x
<< 2) + 2] = *ptr
++;
711 if ((data
[(x
<< 2) + 0] == image
->GetMaskRed()) &&
712 (data
[(x
<< 2) + 1] == image
->GetMaskGreen()) &&
713 (data
[(x
<< 2) + 2] == image
->GetMaskBlue()))
715 data
[(x
<< 2) + 3] = 0;
719 data
[(x
<< 2) + 3] = 255;
722 png_bytep row_ptr
= data
;
723 png_write_rows( png_ptr
, &row_ptr
, 1 );
727 png_write_end( png_ptr
, info_ptr
);
728 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
737 //-----------------------------------------------------------------------------
739 //-----------------------------------------------------------------------------
741 #if !USE_SHARED_LIBRARIES
742 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
745 bool wxBMPHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
747 unsigned char *data
, *ptr
;
748 int done
, i
, bpp
, planes
, comp
, ncolors
, line
, column
,
749 linesize
, linepos
, rshift
= 0, gshift
= 0, bshift
= 0;
752 long int dbuf
[4], dword
, rmask
= 0, gmask
= 0, bmask
= 0, offset
,
754 off_t start_offset
= stream
.TellI();
758 unsigned char r
, g
, b
;
768 #define BI_BITFIELDS 3
775 * Reading the bmp header
778 stream
.Read(&bbuf
, 2);
780 stream
.Read(dbuf
, 4 * 4);
785 stream
.Read(dbuf
, 4 * 2);
786 int width
= (int)dbuf
[0];
787 int height
= (int)dbuf
[1];
790 wxLogError( "Image width > 32767 pixels for file\n" );
795 wxLogError( "Image height > 32767 pixels for file\n" );
798 stream
.Read(&word
, 2);
800 stream
.Read(&word
, 2);
802 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
&& 16 && bpp
!= 24 && bpp
!= 32)
804 wxLogError( "unknown bitdepth in file\n" );
807 stream
.Read(dbuf
, 4 * 4);
809 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
811 wxLogError( "unknown encoding in Windows BMP file\n" );
814 stream
.Read(dbuf
, 4 * 2);
815 ncolors
= (int)dbuf
[0];
818 /* some more sanity checks */
819 if (((comp
== BI_RLE4
) && (bpp
!= 4)) || ((comp
== BI_RLE8
) && (bpp
!= 8)) || ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
821 wxLogError( "encoding of BMP doesn't match bitdepth\n" );
826 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
830 wxLogError( "Cannot allocate RAM for color map in BMP file\n" );
837 image
->Create( width
, height
);
838 ptr
= image
->GetData();
841 wxLogError( "Cannot allocate RAM for RGB data in file\n" );
848 * Reading the palette, if it exists.
850 if (bpp
< 16 && ncolors
!= 0)
852 for (i
= 0; i
< ncolors
; i
++)
854 stream
.Read(bbuf
, 4);
860 else if (bpp
== 16 || bpp
== 32)
862 if (comp
== BI_BITFIELDS
)
866 stream
.Read(dbuf
, 4 * 3);
870 /* find shift amount.. ugly, but i can't think of a better way */
871 for (bit
= 0; bit
< bpp
; bit
++)
873 if (bmask
& (1 << bit
))
875 if (gmask
& (1 << bit
))
877 if (rmask
& (1 << bit
))
902 * Reading the image data
904 stream
.SeekI(start_offset
+ offset
);
907 /* set the whole image to the background color */
908 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
910 for (i
= 0; i
< width
* height
; i
++)
920 #define poffset (line * width * 3 + column * 3)
923 * BMPs are stored upside down... hmmmmmmmmmm....
926 linesize
= ((width
* bpp
+ 31) / 32) * 4;
927 for (line
= (height
- 1); line
>= 0; line
--)
930 for (column
= 0; column
< width
;)
937 aByte
= stream
.GetC();
942 for (bit
= 0; bit
< 8; bit
++)
944 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
945 ptr
[poffset
] = cmap
[index
].r
;
946 ptr
[poffset
+ 1] = cmap
[index
].g
;
947 ptr
[poffset
+ 2] = cmap
[index
].b
;
955 wxLogError( "can't deal with 4bit encoded yet.\n");
964 for (nibble
= 0; nibble
< 2; nibble
++)
966 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
969 ptr
[poffset
] = cmap
[index
].r
;
970 ptr
[poffset
+ 1] = cmap
[index
].g
;
971 ptr
[poffset
+ 2] = cmap
[index
].b
;
983 aByte
= stream
.GetC();
988 /* column = width; */
997 aByte
= stream
.GetC();
999 linepos
= column
* bpp
/ 8;
1000 aByte
= stream
.GetC();
1005 int absolute
= aByte
;
1007 for (i
= 0; i
< absolute
; i
++)
1010 aByte
= stream
.GetC();
1011 ptr
[poffset
] = cmap
[aByte
].r
;
1012 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1013 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1016 if (absolute
& 0x01)
1017 aByte
= stream
.GetC();
1022 for (i
= 0; i
< first
; i
++)
1024 ptr
[poffset
] = cmap
[aByte
].r
;
1025 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1026 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1034 ptr
[poffset
] = cmap
[aByte
].r
;
1035 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1036 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1044 stream
.Read(&bbuf
, 3);
1046 ptr
[poffset
] = (unsigned char)bbuf
[2];
1047 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
1048 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
1055 stream
.Read(&word
, 2);
1057 temp
= (word
& rmask
) >> rshift
;
1058 ptr
[poffset
] = temp
;
1059 temp
= (word
& gmask
) >> gshift
;
1060 ptr
[poffset
+ 1] = temp
;
1061 temp
= (word
& bmask
) >> gshift
;
1062 ptr
[poffset
+ 2] = temp
;
1069 stream
.Read(&dword
, 4);
1071 temp
= (dword
& rmask
) >> rshift
;
1072 ptr
[poffset
] = temp
;
1073 temp
= (dword
& gmask
) >> gshift
;
1074 ptr
[poffset
+ 1] = temp
;
1075 temp
= (dword
& bmask
) >> bshift
;
1076 ptr
[poffset
+ 2] = temp
;
1080 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
1082 stream
.Read(&aByte
, 1);
1084 if (stream
.LastError() != wxStream_NOERROR
)
1088 if (cmap
) free(cmap
);
1090 image
->SetMask( FALSE
);
1097 wxBitmap
wxImage::ConvertToBitmap() const
1099 // sizeLimit is the MS upper limit for the DIB size
1100 int sizeLimit
= 1024*768*3;
1102 // width and height of the device-dependent bitmap
1103 int width
= GetWidth();
1104 int bmpHeight
= GetHeight();
1106 // calc the number of bytes per scanline and padding
1107 int bytePerLine
= width
*3;
1108 int sizeDWORD
= sizeof( DWORD
);
1109 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
1111 if( lineBoundary
.rem
> 0 )
1113 padding
= sizeDWORD
- lineBoundary
.rem
;
1114 bytePerLine
+= padding
;
1116 // calc the number of DIBs and heights of DIBs
1119 int height
= sizeLimit
/bytePerLine
;
1120 if( height
>= bmpHeight
)
1124 div_t result
= div( bmpHeight
, height
);
1125 numDIB
= result
.quot
;
1126 hRemain
= result
.rem
;
1127 if( hRemain
>0 ) numDIB
++;
1130 // set bitmap parameters
1132 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1133 bitmap
.SetWidth( width
);
1134 bitmap
.SetHeight( bmpHeight
);
1135 bitmap
.SetDepth( wxDisplayDepth() );
1137 // create a DIB header
1138 int headersize
= sizeof(BITMAPINFOHEADER
);
1139 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1140 wxCHECK_MSG( lpDIBh
, bitmap
, "could not allocate memory for DIB header" );
1141 // Fill in the DIB header
1142 lpDIBh
->bmiHeader
.biSize
= headersize
;
1143 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
1144 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1145 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1146 // the general formula for biSizeImage:
1147 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
1148 lpDIBh
->bmiHeader
.biPlanes
= 1;
1149 lpDIBh
->bmiHeader
.biBitCount
= 24;
1150 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1151 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1152 // These seem not really needed for our purpose here.
1153 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1154 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1155 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1156 // memory for DIB data
1157 unsigned char *lpBits
;
1158 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1161 wxFAIL_MSG( "could not allocate memory for DIB" );
1166 // create and set the device-dependent bitmap
1167 HDC hdc
= ::GetDC(NULL
);
1168 HDC memdc
= ::CreateCompatibleDC( hdc
);
1170 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
1171 ::SelectObject( memdc
, hbitmap
);
1173 // copy image data into DIB data and then into DDB (in a loop)
1174 unsigned char *data
= GetData();
1177 unsigned char *ptdata
= data
;
1178 unsigned char *ptbits
;
1180 for( n
=0; n
<numDIB
; n
++ )
1182 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
1184 // redefine height and size of the (possibly) last smaller DIB
1185 // memory is not reallocated
1187 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1188 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1192 for( j
=0; j
<height
; j
++ )
1194 for( i
=0; i
<width
; i
++ )
1196 *(ptbits
++) = *(ptdata
+2);
1197 *(ptbits
++) = *(ptdata
+1);
1198 *(ptbits
++) = *(ptdata
);
1201 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
1203 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1204 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1206 // if numDIB = 1, lines below can also be used
1207 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
1208 // The above line is equivalent to the following two lines.
1209 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1210 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1211 // or the following lines
1212 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1213 // HDC memdc = ::CreateCompatibleDC( hdc );
1214 // ::SelectObject( memdc, hbitmap);
1215 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
1216 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1217 // ::SelectObject( memdc, 0 );
1218 // ::DeleteDC( memdc );
1220 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
1222 // similarly, created an mono-bitmap for the possible mask
1225 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
1226 ::SelectObject( memdc
, hbitmap
);
1227 if( numDIB
== 1 ) height
= bmpHeight
;
1228 else height
= sizeLimit
/bytePerLine
;
1229 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1230 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1232 unsigned char r
= GetMaskRed();
1233 unsigned char g
= GetMaskGreen();
1234 unsigned char b
= GetMaskBlue();
1235 unsigned char zero
= 0, one
= 255;
1237 for( n
=0; n
<numDIB
; n
++ )
1239 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
1241 // redefine height and size of the (possibly) last smaller DIB
1242 // memory is not reallocated
1244 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1245 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1248 for( int j
=0; j
<height
; j
++ )
1250 for( int i
=0; i
<width
; i
++ )
1252 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
1265 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
1267 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1268 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1271 // create a wxMask object
1272 wxMask
*mask
= new wxMask();
1273 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
1274 bitmap
.SetMask( mask
);
1275 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
1276 /* The following can also be used but is slow to run
1277 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
1278 wxMask *mask = new wxMask( bitmap, colour );
1279 bitmap.SetMask( mask );
1283 // free allocated resources
1284 ::SelectObject( memdc
, 0 );
1285 ::DeleteDC( memdc
);
1286 ::ReleaseDC(NULL
, hdc
);
1290 // check the wxBitmap object
1291 if( bitmap
.GetHBITMAP() )
1292 bitmap
.SetOk( TRUE
);
1294 bitmap
.SetOk( FALSE
);
1299 wxImage::wxImage( const wxBitmap
&bitmap
)
1304 wxFAIL_MSG( "invalid bitmap" );
1308 // create an wxImage object
1309 int width
= bitmap
.GetWidth();
1310 int height
= bitmap
.GetHeight();
1311 Create( width
, height
);
1312 unsigned char *data
= GetData();
1315 wxFAIL_MSG( "could not allocate data for image" );
1319 // calc the number of bytes per scanline and padding in the DIB
1320 int bytePerLine
= width
*3;
1321 int sizeDWORD
= sizeof( DWORD
);
1322 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
1324 if( lineBoundary
.rem
> 0 )
1326 padding
= sizeDWORD
- lineBoundary
.rem
;
1327 bytePerLine
+= padding
;
1330 // create a DIB header
1331 int headersize
= sizeof(BITMAPINFOHEADER
);
1332 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1335 wxFAIL_MSG( "could not allocate data for DIB header" );
1339 // Fill in the DIB header
1340 lpDIBh
->bmiHeader
.biSize
= headersize
;
1341 lpDIBh
->bmiHeader
.biWidth
= width
;
1342 lpDIBh
->bmiHeader
.biHeight
= -height
;
1343 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
1344 lpDIBh
->bmiHeader
.biPlanes
= 1;
1345 lpDIBh
->bmiHeader
.biBitCount
= 24;
1346 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1347 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1348 // These seem not really needed for our purpose here.
1349 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1350 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1351 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1352 // memory for DIB data
1353 unsigned char *lpBits
;
1354 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1357 wxFAIL_MSG( "could not allocate data for DIB" );
1363 // copy data from the device-dependent bitmap to the DIB
1364 HDC hdc
= ::GetDC(NULL
);
1366 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
1367 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1369 // copy DIB data into the wxImage object
1371 unsigned char *ptdata
= data
;
1372 unsigned char *ptbits
= lpBits
;
1373 for( i
=0; i
<height
; i
++ )
1375 for( j
=0; j
<width
; j
++ )
1377 *(ptdata
++) = *(ptbits
+2);
1378 *(ptdata
++) = *(ptbits
+1);
1379 *(ptdata
++) = *(ptbits
);
1385 // similarly, set data according to the possible mask bitmap
1386 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
1388 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
1389 // memory DC created, color set, data copied, and memory DC deleted
1390 HDC memdc
= ::CreateCompatibleDC( hdc
);
1391 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1392 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1393 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1394 ::DeleteDC( memdc
);
1395 // background color set to RGB(16,16,16) in consistent with wxGTK
1396 unsigned char r
=16, g
=16, b
=16;
1399 for( i
=0; i
<height
; i
++ )
1401 for( j
=0; j
<width
; j
++ )
1415 SetMaskColour( r
, g
, b
);
1422 // free allocated resources
1423 ::ReleaseDC(NULL
, hdc
);
1432 #include "gtk/gtk.h"
1433 #include "gdk/gdk.h"
1434 #include "gdk/gdkx.h"
1436 wxBitmap
wxImage::ConvertToBitmap() const
1440 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1442 int width
= GetWidth();
1443 int height
= GetHeight();
1445 bitmap
.SetHeight( height
);
1446 bitmap
.SetWidth( width
);
1450 GdkImage
*data_image
=
1451 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1453 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1457 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1461 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1463 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1465 wxMask
*mask
= new wxMask();
1466 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1468 bitmap
.SetMask( mask
);
1473 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1474 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1475 int bpp
= visual
->depth
;
1477 bitmap
.SetDepth( bpp
);
1479 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1480 if (bpp
< 8) bpp
= 8;
1484 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1485 byte_order b_o
= RGB
;
1489 GdkVisual
*visual
= gdk_visual_get_system();
1490 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1491 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1492 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1493 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1494 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1495 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1498 int r_mask
= GetMaskRed();
1499 int g_mask
= GetMaskGreen();
1500 int b_mask
= GetMaskBlue();
1502 unsigned char* data
= GetData();
1505 for (int y
= 0; y
< height
; y
++)
1507 for (int x
= 0; x
< width
; x
++)
1509 int r
= data
[index
];
1511 int g
= data
[index
];
1513 int b
= data
[index
];
1518 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1519 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1521 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1526 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1527 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1529 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1537 if (wxTheApp
->m_colorCube
)
1539 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1543 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1544 GdkColor
*colors
= cmap
->colors
;
1545 int max
= 3 * (65536);
1547 for (int i
= 0; i
< cmap
->size
; i
++)
1549 int rdiff
= (r
<< 8) - colors
[i
].red
;
1550 int gdiff
= (g
<< 8) - colors
[i
].green
;
1551 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1552 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1553 if (sum
< max
) { pixel
= i
; max
= sum
; }
1557 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1563 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1564 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1569 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1570 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1579 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1580 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1581 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1582 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1583 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1584 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1586 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1595 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1597 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1599 gdk_image_destroy( data_image
);
1600 gdk_gc_unref( data_gc
);
1606 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1608 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1610 gdk_image_destroy( mask_image
);
1611 gdk_gc_unref( mask_gc
);
1617 wxImage::wxImage( const wxBitmap
&bitmap
)
1619 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
1621 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1623 bitmap
.GetWidth(), bitmap
.GetHeight() );
1625 wxCHECK_RET( gdk_image
, "couldn't create image" );
1627 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1628 char unsigned *data
= GetData();
1632 gdk_image_destroy( gdk_image
);
1633 wxFAIL_MSG( "couldn't create image" );
1637 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1638 if (bitmap
.GetMask())
1640 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1642 bitmap
.GetWidth(), bitmap
.GetHeight() );
1644 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1647 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1648 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1649 int bpp
= visual
->depth
;
1650 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1652 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1655 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1657 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1659 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1662 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1663 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1664 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1665 } else if (bpp
== 15)
1667 data
[pos
] = (pixel
>> 7) & 0xf8;
1668 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1669 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1670 } else if (bpp
== 16)
1672 data
[pos
] = (pixel
>> 8) & 0xf8;
1673 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1674 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1677 data
[pos
] = (pixel
>> 16) & 0xff;
1678 data
[pos
+1] = (pixel
>> 8) & 0xff;
1679 data
[pos
+2] = pixel
& 0xff;
1684 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1685 if (mask_pixel
== 0)
1697 gdk_image_destroy( gdk_image
);
1698 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1708 #include "wx/utils.h"
1711 wxBitmap
wxImage::ConvertToBitmap() const
1715 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1717 int width
= GetWidth();
1718 int height
= GetHeight();
1720 bitmap
.SetHeight( height
);
1721 bitmap
.SetWidth( width
);
1723 Display
*dpy
= (Display
*) wxGetDisplay();
1724 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1725 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1729 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1730 data_image
->data
= new char[ data_image
->bytes_per_line
* data_image
->height
];
1732 bitmap
.Create( width
, height
, bpp
);
1737 GdkImage *mask_image = (GdkImage*) NULL;
1741 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1743 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1745 wxMask *mask = new wxMask();
1746 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1748 bitmap.SetMask( mask );
1752 // Retrieve depth info
1754 XVisualInfo vinfo_template
;
1757 vinfo_template
.visual
= vis
;
1758 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1759 vinfo_template
.depth
= bpp
;
1762 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1766 printf("no visual.\n" );
1767 return wxNullBitmap
;
1772 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1773 if (bpp
< 8) bpp
= 8;
1777 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1778 byte_order b_o
= RGB
;
1782 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
1783 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
1784 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
1785 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
1786 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
1787 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
1791 int r_mask = GetMaskRed();
1792 int g_mask = GetMaskGreen();
1793 int b_mask = GetMaskBlue();
1799 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
1801 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1802 XQueryColors( dpy
, cmap
, colors
, 256 );
1805 unsigned char* data
= GetData();
1808 for (int y
= 0; y
< height
; y
++)
1810 for (int x
= 0; x
< width
; x
++)
1812 int r
= data
[index
];
1814 int g
= data
[index
];
1816 int b
= data
[index
];
1822 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1823 gdk_image_put_pixel( mask_image, x, y, 1 );
1825 gdk_image_put_pixel( mask_image, x, y, 0 );
1835 if (wxTheApp->m_colorCube)
1837 pixel = wxTheApp->m_colorCube
1838 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1843 int max
= 3 * (65536);
1844 for (int i
= 0; i
< 256; i
++)
1846 int rdiff
= (r
<< 8) - colors
[i
].red
;
1847 int gdiff
= (g
<< 8) - colors
[i
].green
;
1848 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1849 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
1850 if (sum
< max
) { pixel
= i
; max
= sum
; }
1855 XPutPixel( data_image
, x
, y
, pixel
);
1860 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1861 XPutPixel( data_image
, x
, y
, pixel
);
1866 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1867 XPutPixel( data_image
, x
, y
, pixel
);
1876 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1877 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1878 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1879 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1880 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1881 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1883 XPutPixel( data_image
, x
, y
, pixel
);
1893 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
1894 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
1895 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
1897 XDestroyImage( data_image
);
1905 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1907 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1909 gdk_image_destroy( mask_image );
1910 gdk_gc_unref( mask_gc );
1917 wxImage::wxImage( const wxBitmap
&bitmap
)
1919 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
1921 Display
*dpy
= (Display
*) wxGetDisplay();
1922 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1923 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1925 XImage
*ximage
= XGetImage( dpy
,
1926 (Drawable
)bitmap
.GetPixmap(),
1928 bitmap
.GetWidth(), bitmap
.GetHeight(),
1929 AllPlanes
, ZPixmap
);
1931 wxCHECK_RET( ximage
, "couldn't create image" );
1933 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1934 char unsigned *data
= GetData();
1938 XDestroyImage( ximage
);
1939 wxFAIL_MSG( "couldn't create image" );
1944 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1945 if (bitmap.GetMask())
1947 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1949 bitmap.GetWidth(), bitmap.GetHeight() );
1951 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1955 // Retrieve depth info
1957 XVisualInfo vinfo_template
;
1960 vinfo_template
.visual
= vis
;
1961 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1962 vinfo_template
.depth
= bpp
;
1965 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1969 printf("no visual.\n" );
1973 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1980 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
1982 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1983 XQueryColors( dpy
, cmap
, colors
, 256 );
1987 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1989 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1991 int pixel
= XGetPixel( ximage
, i
, j
);
1994 data
[pos
] = colors
[pixel
].red
>> 8;
1995 data
[pos
+1] = colors
[pixel
].green
>> 8;
1996 data
[pos
+2] = colors
[pixel
].blue
>> 8;
1997 } else if (bpp
== 15)
1999 data
[pos
] = (pixel
>> 7) & 0xf8;
2000 data
[pos
+1] = (pixel
>> 2) & 0xf8;
2001 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2002 } else if (bpp
== 16)
2004 data
[pos
] = (pixel
>> 8) & 0xf8;
2005 data
[pos
+1] = (pixel
>> 3) & 0xfc;
2006 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2009 data
[pos
] = (pixel
>> 16) & 0xff;
2010 data
[pos
+1] = (pixel
>> 8) & 0xff;
2011 data
[pos
+2] = pixel
& 0xff;
2017 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
2018 if (mask_pixel == 0)
2031 XDestroyImage( ximage
);
2033 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );