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"
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 class wxImageRefData
: public wxObjectRefData
52 ~wxImageRefData(void);
56 unsigned char *m_data
;
58 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
62 wxImageRefData::wxImageRefData(void)
66 m_data
= (unsigned char*) NULL
;
74 wxImageRefData::~wxImageRefData(void)
76 if (m_data
) free( m_data
);
79 wxList
wxImage::sm_handlers
;
81 //-----------------------------------------------------------------------------
83 #define M_IMGDATA ((wxImageRefData *)m_refData)
85 #if !USE_SHARED_LIBRARIES
86 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
93 wxImage::wxImage( int width
, int height
)
95 Create( width
, height
);
98 wxImage::wxImage( const wxString
& name
, long type
)
100 LoadFile( name
, type
);
103 wxImage::wxImage( wxInputStream
& stream
, long type
)
105 LoadFile( stream
, type
);
108 wxImage::wxImage( const wxImage
& image
)
113 wxImage::wxImage( const wxImage
* image
)
115 if (image
) Ref(*image
);
118 void wxImage::Create( int width
, int height
)
120 m_refData
= new wxImageRefData();
122 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
123 if (M_IMGDATA
->m_data
)
125 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
127 M_IMGDATA
->m_width
= width
;
128 M_IMGDATA
->m_height
= height
;
129 M_IMGDATA
->m_ok
= TRUE
;
137 void wxImage::Destroy()
142 wxImage
wxImage::Scale( int width
, int height
)
146 wxCHECK_MSG( Ok(), image
, "invlaid image" );
148 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, "invalid image size" );
150 image
.Create( width
, height
);
152 char unsigned *data
= image
.GetData();
154 wxCHECK_MSG( data
, image
, "unable to create image" );
156 if (M_IMGDATA
->m_hasMask
)
157 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
159 double xscale
= (double)width
/ (double)M_IMGDATA
->m_width
;
160 double yscale
= (double)height
/ (double)M_IMGDATA
->m_height
;
162 for (int j
= 0; j
< height
; j
++)
164 for (int i
= 0; i
< width
; i
++)
166 int new_pos
= 3*(j
*width
+ i
);
167 int old_pos
= 3*((long)(j
/yscale
)*M_IMGDATA
->m_width
+ (long)(i
/xscale
));
168 data
[ new_pos
] = M_IMGDATA
->m_data
[ old_pos
];
169 data
[ new_pos
+1 ] = M_IMGDATA
->m_data
[ old_pos
+1 ];
170 data
[ new_pos
+2 ] = M_IMGDATA
->m_data
[ old_pos
+2 ];
177 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
179 wxCHECK_RET( Ok(), "invalid image" );
181 int w
= M_IMGDATA
->m_width
;
182 int h
= M_IMGDATA
->m_height
;
184 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), "invalid image index" );
186 long pos
= (y
* w
+ x
) * 3;
188 M_IMGDATA
->m_data
[ pos
] = r
;
189 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
190 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
193 unsigned char wxImage::GetRed( int x
, int y
)
195 wxCHECK_MSG( Ok(), 0, "invalid image" );
197 int w
= M_IMGDATA
->m_width
;
198 int h
= M_IMGDATA
->m_height
;
200 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
202 long pos
= (y
* w
+ x
) * 3;
204 return M_IMGDATA
->m_data
[pos
];
207 unsigned char wxImage::GetGreen( int x
, int y
)
209 wxCHECK_MSG( Ok(), 0, "invalid image" );
211 int w
= M_IMGDATA
->m_width
;
212 int h
= M_IMGDATA
->m_height
;
214 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
216 long pos
= (y
* w
+ x
) * 3;
218 return M_IMGDATA
->m_data
[pos
+1];
221 unsigned char wxImage::GetBlue( int x
, int y
)
223 wxCHECK_MSG( Ok(), 0, "invalid image" );
225 int w
= M_IMGDATA
->m_width
;
226 int h
= M_IMGDATA
->m_height
;
228 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
230 long pos
= (y
* w
+ x
) * 3;
232 return M_IMGDATA
->m_data
[pos
+2];
235 bool wxImage::Ok() const
237 return (M_IMGDATA
&& M_IMGDATA
->m_ok
);
240 char unsigned *wxImage::GetData() const
242 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, "invalid image" );
244 return M_IMGDATA
->m_data
;
247 void wxImage::SetData( char unsigned *WXUNUSED(data
) )
249 wxCHECK_RET( Ok(), "invalid image" );
252 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
254 wxCHECK_RET( Ok(), "invalid image" );
256 M_IMGDATA
->m_maskRed
= r
;
257 M_IMGDATA
->m_maskGreen
= g
;
258 M_IMGDATA
->m_maskBlue
= b
;
259 M_IMGDATA
->m_hasMask
= TRUE
;
262 unsigned char wxImage::GetMaskRed() const
264 wxCHECK_MSG( Ok(), 0, "invalid image" );
266 return M_IMGDATA
->m_maskRed
;
269 unsigned char wxImage::GetMaskGreen() const
271 wxCHECK_MSG( Ok(), 0, "invalid image" );
273 return M_IMGDATA
->m_maskGreen
;
276 unsigned char wxImage::GetMaskBlue() const
278 wxCHECK_MSG( Ok(), 0, "invalid image" );
280 return M_IMGDATA
->m_maskBlue
;
283 void wxImage::SetMask( bool mask
)
285 wxCHECK_RET( Ok(), "invalid image" );
287 M_IMGDATA
->m_hasMask
= mask
;
290 bool wxImage::HasMask() const
292 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
294 return M_IMGDATA
->m_hasMask
;
297 int wxImage::GetWidth() const
299 wxCHECK_MSG( Ok(), 0, "invalid image" );
301 return M_IMGDATA
->m_width
;
304 int wxImage::GetHeight() const
306 wxCHECK_MSG( Ok(), 0, "invalid image" );
308 return M_IMGDATA
->m_height
;
311 bool wxImage::LoadFile( const wxString
& filename
, long type
)
313 if (wxFileExists(filename
))
315 wxFileInputStream
stream(filename
);
316 return LoadFile(stream
, type
);
320 wxLogWarning( "Image file does not exist." );
326 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
330 m_refData
= new wxImageRefData
;
332 wxImageHandler
*handler
= FindHandler(type
);
336 wxLogWarning( "No image handler for type %d defined.", type
);
341 return handler
->LoadFile( this, stream
);
344 bool wxImage::SaveFile( const wxString
& filename
, int type
)
346 wxFileOutputStream
stream(filename
);
348 if ( stream
.LastError() == wxStream_NOERROR
)
349 return SaveFile(stream
, type
);
354 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
356 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
358 wxImageHandler
*handler
= FindHandler(type
);
362 wxLogWarning( "No image handler for type %d defined.", type
);
367 return handler
->SaveFile( this, stream
);
370 void wxImage::AddHandler( wxImageHandler
*handler
)
372 // make sure that the memory will be freed at the program end
373 sm_handlers
.DeleteContents(TRUE
);
375 sm_handlers
.Append( handler
);
378 void wxImage::InsertHandler( wxImageHandler
*handler
)
380 // make sure that the memory will be freed at the program end
381 sm_handlers
.DeleteContents(TRUE
);
383 sm_handlers
.Insert( handler
);
386 bool wxImage::RemoveHandler( const wxString
& name
)
388 wxImageHandler
*handler
= FindHandler(name
);
391 sm_handlers
.DeleteObject(handler
);
398 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
400 wxNode
*node
= sm_handlers
.First();
403 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
404 if (handler
->GetName().Cmp(name
) == 0) return handler
;
408 return (wxImageHandler
*)NULL
;
411 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
413 wxNode
*node
= sm_handlers
.First();
416 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
417 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
418 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
422 return (wxImageHandler
*)NULL
;
425 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
427 wxNode
*node
= sm_handlers
.First();
430 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
431 if (handler
->GetType() == bitmapType
) return handler
;
437 void wxImage::InitStandardHandlers()
439 AddHandler( new wxBMPHandler
);
441 AddHandler( new wxPNGHandler
);
445 void wxImage::CleanUpHandlers()
447 wxNode
*node
= sm_handlers
.First();
450 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
451 wxNode
*next
= node
->Next();
458 //-----------------------------------------------------------------------------
460 //-----------------------------------------------------------------------------
462 #if !USE_SHARED_LIBRARIES
463 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
466 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
) )
471 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
) )
476 //-----------------------------------------------------------------------------
478 //-----------------------------------------------------------------------------
482 #if !USE_SHARED_LIBRARIES
483 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler
,wxImageHandler
)
487 static void _PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
489 ((wxInputStream
*) png_get_io_ptr( png_ptr
)) -> Read(data
, length
);
492 static void _PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
494 ((wxOutputStream
*) png_get_io_ptr( png_ptr
)) -> Write(data
, length
);
497 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
499 // VZ: as this function uses setjmp() the only fool proof error handling
500 // method is to use goto (setjmp is not really C++ dtors friendly...)
502 unsigned char **lines
= (unsigned char **) NULL
;
504 png_infop info_ptr
= (png_infop
) NULL
;
508 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
510 (png_error_ptr
) NULL
,
511 (png_error_ptr
) NULL
);
515 info_ptr
= png_create_info_struct( png_ptr
);
519 if (setjmp(png_ptr
->jmpbuf
))
522 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
525 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
527 png_uint_32 width
,height
;
528 int bit_depth
,color_type
,interlace_type
;
530 png_read_info( png_ptr
, info_ptr
);
531 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
533 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
534 png_set_expand( png_ptr
);
536 png_set_strip_16( png_ptr
);
537 png_set_packing( png_ptr
);
538 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
539 png_set_expand( png_ptr
);
540 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
542 image
->Create( width
, height
);
547 lines
= (unsigned char **)malloc( height
* sizeof(unsigned char *) );
551 for (i
= 0; i
< height
; i
++)
553 if ((lines
[i
] = (unsigned char *)malloc(width
* (sizeof(unsigned char) * 4))) == NULL
)
555 for ( unsigned int n
= 0; n
< i
; n
++ )
561 // loaded successfully!
564 png_read_image( png_ptr
, lines
);
565 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
566 unsigned char *ptr
= image
->GetData();
567 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
568 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
570 for (unsigned int y
= 0; y
< height
; y
++)
572 unsigned char *ptr2
= lines
[y
];
573 for (unsigned int x
= 0; x
< width
; x
++)
575 unsigned char r
= *ptr2
++;
576 unsigned char a
= *ptr2
++;
595 for (unsigned int y
= 0; y
< height
; y
++)
597 unsigned char *ptr2
= lines
[y
];
598 for (unsigned int x
= 0; x
< width
; x
++)
600 unsigned char r
= *ptr2
++;
601 unsigned char g
= *ptr2
++;
602 unsigned char b
= *ptr2
++;
603 unsigned char a
= *ptr2
++;
613 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
622 for ( unsigned int j
= 0; j
< height
; j
++ )
628 image
->SetMaskColour( 255, 0, 255 );
632 image
->SetMask( FALSE
);
639 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
655 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
659 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
665 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
668 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
674 png_infop info_ptr
= png_create_info_struct(png_ptr
);
675 if (info_ptr
== NULL
)
677 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
681 if (setjmp(png_ptr
->jmpbuf
))
683 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
687 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
689 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
690 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
691 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
698 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
699 png_write_info( png_ptr
, info_ptr
);
700 png_set_shift( png_ptr
, &sig_bit
);
701 png_set_packing( png_ptr
);
703 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
706 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
710 for (int y
= 0; y
< image
->GetHeight(); y
++)
712 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
713 for (int x
= 0; x
< image
->GetWidth(); x
++)
715 data
[(x
<< 2) + 0] = *ptr
++;
716 data
[(x
<< 2) + 1] = *ptr
++;
717 data
[(x
<< 2) + 2] = *ptr
++;
718 if ((data
[(x
<< 2) + 0] == image
->GetMaskRed()) &&
719 (data
[(x
<< 2) + 1] == image
->GetMaskGreen()) &&
720 (data
[(x
<< 2) + 2] == image
->GetMaskBlue()))
722 data
[(x
<< 2) + 3] = 0;
726 data
[(x
<< 2) + 3] = 255;
729 png_bytep row_ptr
= data
;
730 png_write_rows( png_ptr
, &row_ptr
, 1 );
734 png_write_end( png_ptr
, info_ptr
);
735 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
744 //-----------------------------------------------------------------------------
746 //-----------------------------------------------------------------------------
748 #if !USE_SHARED_LIBRARIES
749 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
752 bool wxBMPHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
754 unsigned char *data
, *ptr
;
755 int done
, i
, bpp
, planes
, comp
, ncolors
, line
, column
,
756 linesize
, linepos
, rshift
= 0, gshift
= 0, bshift
= 0;
759 long int dbuf
[4], dword
, rmask
= 0, gmask
= 0, bmask
= 0, offset
,
761 off_t start_offset
= stream
.TellI();
765 unsigned char r
, g
, b
;
775 #define BI_BITFIELDS 3
782 * Reading the bmp header
785 stream
.Read(&bbuf
, 2);
787 stream
.Read(dbuf
, 4 * 4);
792 stream
.Read(dbuf
, 4 * 2);
793 int width
= (int)dbuf
[0];
794 int height
= (int)dbuf
[1];
797 wxLogError( "Image width > 32767 pixels for file\n" );
802 wxLogError( "Image height > 32767 pixels for file\n" );
805 stream
.Read(&word
, 2);
807 stream
.Read(&word
, 2);
809 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
&& 16 && bpp
!= 24 && bpp
!= 32)
811 wxLogError( "unknown bitdepth in file\n" );
814 stream
.Read(dbuf
, 4 * 4);
816 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
818 wxLogError( "unknown encoding in Windows BMP file\n" );
821 stream
.Read(dbuf
, 4 * 2);
822 ncolors
= (int)dbuf
[0];
825 /* some more sanity checks */
826 if (((comp
== BI_RLE4
) && (bpp
!= 4)) || ((comp
== BI_RLE8
) && (bpp
!= 8)) || ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
828 wxLogError( "encoding of BMP doesn't match bitdepth\n" );
833 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
837 wxLogError( "Cannot allocate RAM for color map in BMP file\n" );
844 image
->Create( width
, height
);
845 ptr
= image
->GetData();
848 wxLogError( "Cannot allocate RAM for RGB data in file\n" );
855 * Reading the palette, if it exists.
857 if (bpp
< 16 && ncolors
!= 0)
859 for (i
= 0; i
< ncolors
; i
++)
861 stream
.Read(bbuf
, 4);
867 else if (bpp
== 16 || bpp
== 32)
869 if (comp
== BI_BITFIELDS
)
873 stream
.Read(dbuf
, 4 * 3);
877 /* find shift amount.. ugly, but i can't think of a better way */
878 for (bit
= 0; bit
< bpp
; bit
++)
880 if (bmask
& (1 << bit
))
882 if (gmask
& (1 << bit
))
884 if (rmask
& (1 << bit
))
909 * Reading the image data
911 stream
.SeekI(start_offset
+ offset
);
914 /* set the whole image to the background color */
915 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
917 for (i
= 0; i
< width
* height
; i
++)
927 #define poffset (line * width * 3 + column * 3)
930 * BMPs are stored upside down... hmmmmmmmmmm....
933 linesize
= ((width
* bpp
+ 31) / 32) * 4;
934 for (line
= (height
- 1); line
>= 0; line
--)
937 for (column
= 0; column
< width
;)
944 aByte
= stream
.GetC();
949 for (bit
= 0; bit
< 8; bit
++)
951 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
952 ptr
[poffset
] = cmap
[index
].r
;
953 ptr
[poffset
+ 1] = cmap
[index
].g
;
954 ptr
[poffset
+ 2] = cmap
[index
].b
;
962 wxLogError( "can't deal with 4bit encoded yet.\n");
971 for (nibble
= 0; nibble
< 2; nibble
++)
973 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
976 ptr
[poffset
] = cmap
[index
].r
;
977 ptr
[poffset
+ 1] = cmap
[index
].g
;
978 ptr
[poffset
+ 2] = cmap
[index
].b
;
990 aByte
= stream
.GetC();
995 /* column = width; */
1002 else if (aByte
== 2)
1004 aByte
= stream
.GetC();
1006 linepos
= column
* bpp
/ 8;
1007 aByte
= stream
.GetC();
1012 int absolute
= aByte
;
1014 for (i
= 0; i
< absolute
; i
++)
1017 aByte
= stream
.GetC();
1018 ptr
[poffset
] = cmap
[aByte
].r
;
1019 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1020 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1023 if (absolute
& 0x01)
1024 aByte
= stream
.GetC();
1029 for (i
= 0; i
< first
; i
++)
1031 ptr
[poffset
] = cmap
[aByte
].r
;
1032 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1033 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1041 ptr
[poffset
] = cmap
[aByte
].r
;
1042 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1043 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1051 stream
.Read(&bbuf
, 3);
1053 ptr
[poffset
] = (unsigned char)bbuf
[2];
1054 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
1055 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
1062 stream
.Read(&word
, 2);
1064 temp
= (word
& rmask
) >> rshift
;
1065 ptr
[poffset
] = temp
;
1066 temp
= (word
& gmask
) >> gshift
;
1067 ptr
[poffset
+ 1] = temp
;
1068 temp
= (word
& bmask
) >> gshift
;
1069 ptr
[poffset
+ 2] = temp
;
1076 stream
.Read(&dword
, 4);
1078 temp
= (dword
& rmask
) >> rshift
;
1079 ptr
[poffset
] = temp
;
1080 temp
= (dword
& gmask
) >> gshift
;
1081 ptr
[poffset
+ 1] = temp
;
1082 temp
= (dword
& bmask
) >> bshift
;
1083 ptr
[poffset
+ 2] = temp
;
1087 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
1089 stream
.Read(&aByte
, 1);
1091 if (stream
.LastError() != wxStream_NOERROR
)
1095 if (cmap
) free(cmap
);
1097 image
->SetMask( FALSE
);
1104 wxBitmap
wxImage::ConvertToBitmap() const
1106 // sizeLimit is the MS upper limit for the DIB size
1107 int sizeLimit
= 1024*768*3;
1109 // width and height of the device-dependent bitmap
1110 int width
= GetWidth();
1111 int bmpHeight
= GetHeight();
1113 // calc the number of bytes per scanline and padding
1114 int bytePerLine
= width
*3;
1115 int sizeDWORD
= sizeof( DWORD
);
1116 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
1118 if( lineBoundary
.rem
> 0 )
1120 padding
= sizeDWORD
- lineBoundary
.rem
;
1121 bytePerLine
+= padding
;
1123 // calc the number of DIBs and heights of DIBs
1126 int height
= sizeLimit
/bytePerLine
;
1127 if( height
>= bmpHeight
)
1131 div_t result
= div( bmpHeight
, height
);
1132 numDIB
= result
.quot
;
1133 hRemain
= result
.rem
;
1134 if( hRemain
>0 ) numDIB
++;
1137 // set bitmap parameters
1139 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1140 bitmap
.SetWidth( width
);
1141 bitmap
.SetHeight( bmpHeight
);
1142 bitmap
.SetDepth( wxDisplayDepth() );
1144 // create a DIB header
1145 int headersize
= sizeof(BITMAPINFOHEADER
);
1146 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1147 wxCHECK_MSG( lpDIBh
, bitmap
, "could not allocate memory for DIB header" );
1148 // Fill in the DIB header
1149 lpDIBh
->bmiHeader
.biSize
= headersize
;
1150 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
1151 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1152 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1153 // the general formula for biSizeImage:
1154 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
1155 lpDIBh
->bmiHeader
.biPlanes
= 1;
1156 lpDIBh
->bmiHeader
.biBitCount
= 24;
1157 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1158 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1159 // These seem not really needed for our purpose here.
1160 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1161 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1162 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1163 // memory for DIB data
1164 unsigned char *lpBits
;
1165 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1168 wxFAIL_MSG( "could not allocate memory for DIB" );
1173 // create and set the device-dependent bitmap
1174 HDC hdc
= ::GetDC(NULL
);
1175 HDC memdc
= ::CreateCompatibleDC( hdc
);
1177 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
1178 ::SelectObject( memdc
, hbitmap
);
1180 // copy image data into DIB data and then into DDB (in a loop)
1181 unsigned char *data
= GetData();
1184 unsigned char *ptdata
= data
;
1185 unsigned char *ptbits
;
1187 for( n
=0; n
<numDIB
; n
++ )
1189 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
1191 // redefine height and size of the (possibly) last smaller DIB
1192 // memory is not reallocated
1194 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1195 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1199 for( j
=0; j
<height
; j
++ )
1201 for( i
=0; i
<width
; i
++ )
1203 *(ptbits
++) = *(ptdata
+2);
1204 *(ptbits
++) = *(ptdata
+1);
1205 *(ptbits
++) = *(ptdata
);
1208 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
1210 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1211 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1213 // if numDIB = 1, lines below can also be used
1214 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
1215 // The above line is equivalent to the following two lines.
1216 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1217 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1218 // or the following lines
1219 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1220 // HDC memdc = ::CreateCompatibleDC( hdc );
1221 // ::SelectObject( memdc, hbitmap);
1222 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
1223 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1224 // ::SelectObject( memdc, 0 );
1225 // ::DeleteDC( memdc );
1227 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
1229 // similarly, created an mono-bitmap for the possible mask
1232 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
1233 ::SelectObject( memdc
, hbitmap
);
1234 if( numDIB
== 1 ) height
= bmpHeight
;
1235 else height
= sizeLimit
/bytePerLine
;
1236 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1237 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1239 unsigned char r
= GetMaskRed();
1240 unsigned char g
= GetMaskGreen();
1241 unsigned char b
= GetMaskBlue();
1242 unsigned char zero
= 0, one
= 255;
1244 for( n
=0; n
<numDIB
; n
++ )
1246 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
1248 // redefine height and size of the (possibly) last smaller DIB
1249 // memory is not reallocated
1251 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1252 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1255 for( int j
=0; j
<height
; j
++ )
1257 for( int i
=0; i
<width
; i
++ )
1259 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
1272 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
1274 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1275 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1278 // create a wxMask object
1279 wxMask
*mask
= new wxMask();
1280 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
1281 bitmap
.SetMask( mask
);
1282 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
1283 /* The following can also be used but is slow to run
1284 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
1285 wxMask *mask = new wxMask( bitmap, colour );
1286 bitmap.SetMask( mask );
1290 // free allocated resources
1291 ::SelectObject( memdc
, 0 );
1292 ::DeleteDC( memdc
);
1293 ::ReleaseDC(NULL
, hdc
);
1297 // check the wxBitmap object
1298 if( bitmap
.GetHBITMAP() )
1299 bitmap
.SetOk( TRUE
);
1301 bitmap
.SetOk( FALSE
);
1306 wxImage::wxImage( const wxBitmap
&bitmap
)
1311 wxFAIL_MSG( "invalid bitmap" );
1315 // create an wxImage object
1316 int width
= bitmap
.GetWidth();
1317 int height
= bitmap
.GetHeight();
1318 Create( width
, height
);
1319 unsigned char *data
= GetData();
1322 wxFAIL_MSG( "could not allocate data for image" );
1326 // calc the number of bytes per scanline and padding in the DIB
1327 int bytePerLine
= width
*3;
1328 int sizeDWORD
= sizeof( DWORD
);
1329 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
1331 if( lineBoundary
.rem
> 0 )
1333 padding
= sizeDWORD
- lineBoundary
.rem
;
1334 bytePerLine
+= padding
;
1337 // create a DIB header
1338 int headersize
= sizeof(BITMAPINFOHEADER
);
1339 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1342 wxFAIL_MSG( "could not allocate data for DIB header" );
1346 // Fill in the DIB header
1347 lpDIBh
->bmiHeader
.biSize
= headersize
;
1348 lpDIBh
->bmiHeader
.biWidth
= width
;
1349 lpDIBh
->bmiHeader
.biHeight
= -height
;
1350 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
1351 lpDIBh
->bmiHeader
.biPlanes
= 1;
1352 lpDIBh
->bmiHeader
.biBitCount
= 24;
1353 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1354 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1355 // These seem not really needed for our purpose here.
1356 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1357 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1358 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1359 // memory for DIB data
1360 unsigned char *lpBits
;
1361 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1364 wxFAIL_MSG( "could not allocate data for DIB" );
1370 // copy data from the device-dependent bitmap to the DIB
1371 HDC hdc
= ::GetDC(NULL
);
1373 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
1374 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1376 // copy DIB data into the wxImage object
1378 unsigned char *ptdata
= data
;
1379 unsigned char *ptbits
= lpBits
;
1380 for( i
=0; i
<height
; i
++ )
1382 for( j
=0; j
<width
; j
++ )
1384 *(ptdata
++) = *(ptbits
+2);
1385 *(ptdata
++) = *(ptbits
+1);
1386 *(ptdata
++) = *(ptbits
);
1392 // similarly, set data according to the possible mask bitmap
1393 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
1395 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
1396 // memory DC created, color set, data copied, and memory DC deleted
1397 HDC memdc
= ::CreateCompatibleDC( hdc
);
1398 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1399 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1400 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1401 ::DeleteDC( memdc
);
1402 // background color set to RGB(16,16,16) in consistent with wxGTK
1403 unsigned char r
=16, g
=16, b
=16;
1406 for( i
=0; i
<height
; i
++ )
1408 for( j
=0; j
<width
; j
++ )
1422 SetMaskColour( r
, g
, b
);
1429 // free allocated resources
1430 ::ReleaseDC(NULL
, hdc
);
1439 #include "gtk/gtk.h"
1440 #include "gdk/gdk.h"
1441 #include "gdk/gdkx.h"
1443 wxBitmap
wxImage::ConvertToBitmap() const
1447 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1449 int width
= GetWidth();
1450 int height
= GetHeight();
1452 bitmap
.SetHeight( height
);
1453 bitmap
.SetWidth( width
);
1457 GdkImage
*data_image
=
1458 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1460 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1464 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1468 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1470 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1472 wxMask
*mask
= new wxMask();
1473 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1475 bitmap
.SetMask( mask
);
1480 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1481 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1482 int bpp
= visual
->depth
;
1484 bitmap
.SetDepth( bpp
);
1486 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1487 if (bpp
< 8) bpp
= 8;
1491 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1492 byte_order b_o
= RGB
;
1496 GdkVisual
*visual
= gdk_visual_get_system();
1497 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1498 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1499 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1500 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1501 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1502 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1505 int r_mask
= GetMaskRed();
1506 int g_mask
= GetMaskGreen();
1507 int b_mask
= GetMaskBlue();
1509 unsigned char* data
= GetData();
1512 for (int y
= 0; y
< height
; y
++)
1514 for (int x
= 0; x
< width
; x
++)
1516 int r
= data
[index
];
1518 int g
= data
[index
];
1520 int b
= data
[index
];
1525 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1526 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1528 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1533 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1534 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1536 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1544 if (wxTheApp
->m_colorCube
)
1546 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1550 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1551 GdkColor
*colors
= cmap
->colors
;
1552 int max
= 3 * (65536);
1554 for (int i
= 0; i
< cmap
->size
; i
++)
1556 int rdiff
= (r
<< 8) - colors
[i
].red
;
1557 int gdiff
= (g
<< 8) - colors
[i
].green
;
1558 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1559 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1560 if (sum
< max
) { pixel
= i
; max
= sum
; }
1564 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1570 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1571 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1576 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1577 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1586 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1587 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1588 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1589 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1590 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1591 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1593 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1602 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1604 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1606 gdk_image_destroy( data_image
);
1607 gdk_gc_unref( data_gc
);
1613 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1615 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1617 gdk_image_destroy( mask_image
);
1618 gdk_gc_unref( mask_gc
);
1624 wxImage::wxImage( const wxBitmap
&bitmap
)
1626 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
1628 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1630 bitmap
.GetWidth(), bitmap
.GetHeight() );
1632 wxCHECK_RET( gdk_image
, "couldn't create image" );
1634 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1635 char unsigned *data
= GetData();
1639 gdk_image_destroy( gdk_image
);
1640 wxFAIL_MSG( "couldn't create image" );
1644 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1645 if (bitmap
.GetMask())
1647 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1649 bitmap
.GetWidth(), bitmap
.GetHeight() );
1651 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1654 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1655 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1656 int bpp
= visual
->depth
;
1657 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1659 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1662 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1664 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1666 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1669 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1670 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1671 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1672 } else if (bpp
== 15)
1674 data
[pos
] = (pixel
>> 7) & 0xf8;
1675 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1676 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1677 } else if (bpp
== 16)
1679 data
[pos
] = (pixel
>> 8) & 0xf8;
1680 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1681 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1684 data
[pos
] = (pixel
>> 16) & 0xff;
1685 data
[pos
+1] = (pixel
>> 8) & 0xff;
1686 data
[pos
+2] = pixel
& 0xff;
1691 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1692 if (mask_pixel
== 0)
1704 gdk_image_destroy( gdk_image
);
1705 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1713 #include "wx/utils.h"
1716 wxBitmap
wxImage::ConvertToBitmap() const
1720 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1722 int width
= GetWidth();
1723 int height
= GetHeight();
1725 bitmap
.SetHeight( height
);
1726 bitmap
.SetWidth( width
);
1728 Display
*dpy
= (Display
*) wxGetDisplay();
1729 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1730 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1734 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1735 data_image
->data
= new char[ data_image
->bytes_per_line
* data_image
->height
];
1737 bitmap
.Create( width
, height
, bpp
);
1742 GdkImage *mask_image = (GdkImage*) NULL;
1746 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1748 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1750 wxMask *mask = new wxMask();
1751 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1753 bitmap.SetMask( mask );
1757 // Retrieve depth info
1759 XVisualInfo vinfo_template
;
1762 vinfo_template
.visual
= vis
;
1763 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1764 vinfo_template
.depth
= bpp
;
1767 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1771 printf("no visual.\n" );
1772 return wxNullBitmap
;
1777 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1778 if (bpp
< 8) bpp
= 8;
1782 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1783 byte_order b_o
= RGB
;
1787 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
1788 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
1789 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
1790 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
1791 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
1792 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
1796 int r_mask = GetMaskRed();
1797 int g_mask = GetMaskGreen();
1798 int b_mask = GetMaskBlue();
1804 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
1806 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1807 XQueryColors( dpy
, cmap
, colors
, 256 );
1810 unsigned char* data
= GetData();
1813 for (int y
= 0; y
< height
; y
++)
1815 for (int x
= 0; x
< width
; x
++)
1817 int r
= data
[index
];
1819 int g
= data
[index
];
1821 int b
= data
[index
];
1827 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1828 gdk_image_put_pixel( mask_image, x, y, 1 );
1830 gdk_image_put_pixel( mask_image, x, y, 0 );
1840 if (wxTheApp->m_colorCube)
1842 pixel = wxTheApp->m_colorCube
1843 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1848 int max
= 3 * (65536);
1849 for (int i
= 0; i
< 256; i
++)
1851 int rdiff
= (r
<< 8) - colors
[i
].red
;
1852 int gdiff
= (g
<< 8) - colors
[i
].green
;
1853 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1854 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
1855 if (sum
< max
) { pixel
= i
; max
= sum
; }
1860 XPutPixel( data_image
, x
, y
, pixel
);
1865 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1866 XPutPixel( data_image
, x
, y
, pixel
);
1871 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1872 XPutPixel( data_image
, x
, y
, pixel
);
1881 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1882 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1883 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1884 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1885 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1886 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1888 XPutPixel( data_image
, x
, y
, pixel
);
1898 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
1899 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
1900 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
1902 XDestroyImage( data_image
);
1910 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1912 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1914 gdk_image_destroy( mask_image );
1915 gdk_gc_unref( mask_gc );
1922 wxImage::wxImage( const wxBitmap
&bitmap
)
1924 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
1926 Display
*dpy
= (Display
*) wxGetDisplay();
1927 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1928 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1930 XImage
*ximage
= XGetImage( dpy
,
1931 (Drawable
)bitmap
.GetPixmap(),
1933 bitmap
.GetWidth(), bitmap
.GetHeight(),
1934 AllPlanes
, ZPixmap
);
1936 wxCHECK_RET( ximage
, "couldn't create image" );
1938 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1939 char unsigned *data
= GetData();
1943 XDestroyImage( ximage
);
1944 wxFAIL_MSG( "couldn't create image" );
1949 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1950 if (bitmap.GetMask())
1952 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1954 bitmap.GetWidth(), bitmap.GetHeight() );
1956 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1960 // Retrieve depth info
1962 XVisualInfo vinfo_template
;
1965 vinfo_template
.visual
= vis
;
1966 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1967 vinfo_template
.depth
= bpp
;
1970 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1974 printf("no visual.\n" );
1978 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1985 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
1987 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1988 XQueryColors( dpy
, cmap
, colors
, 256 );
1992 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1994 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1996 int pixel
= XGetPixel( ximage
, i
, j
);
1999 data
[pos
] = colors
[pixel
].red
>> 8;
2000 data
[pos
+1] = colors
[pixel
].green
>> 8;
2001 data
[pos
+2] = colors
[pixel
].blue
>> 8;
2002 } else if (bpp
== 15)
2004 data
[pos
] = (pixel
>> 7) & 0xf8;
2005 data
[pos
+1] = (pixel
>> 2) & 0xf8;
2006 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2007 } else if (bpp
== 16)
2009 data
[pos
] = (pixel
>> 8) & 0xf8;
2010 data
[pos
+1] = (pixel
>> 3) & 0xfc;
2011 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2014 data
[pos
] = (pixel
>> 16) & 0xff;
2015 data
[pos
+1] = (pixel
>> 8) & 0xff;
2016 data
[pos
+2] = pixel
& 0xff;
2022 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
2023 if (mask_pixel == 0)
2036 XDestroyImage( ximage
);
2038 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );