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 "../png/png.h"
28 #include "wx/filefn.h"
29 #include "wx/wfstream.h"
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 class wxImageRefData
: public wxObjectRefData
44 ~wxImageRefData(void);
48 unsigned char *m_data
;
50 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
54 wxImageRefData::wxImageRefData(void)
58 m_data
= (unsigned char*) NULL
;
66 wxImageRefData::~wxImageRefData(void)
68 if (m_data
) free( m_data
);
71 wxList
wxImage::sm_handlers
;
73 //-----------------------------------------------------------------------------
75 #define M_IMGDATA ((wxImageRefData *)m_refData)
77 #if !USE_SHARED_LIBRARIES
78 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
85 wxImage::wxImage( int width
, int height
)
87 Create( width
, height
);
90 wxImage::wxImage( const wxString
& name
, long type
)
92 LoadFile( name
, type
);
95 wxImage::wxImage( wxInputStream
& stream
, long type
)
97 LoadFile( stream
, type
);
100 wxImage::wxImage( const wxImage
& image
)
105 wxImage::wxImage( const wxImage
* image
)
107 if (image
) Ref(*image
);
110 void wxImage::Create( int width
, int height
)
112 m_refData
= new wxImageRefData();
114 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
115 if (M_IMGDATA
->m_data
)
117 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
119 M_IMGDATA
->m_width
= width
;
120 M_IMGDATA
->m_height
= height
;
121 M_IMGDATA
->m_ok
= TRUE
;
129 void wxImage::Destroy()
134 wxImage
wxImage::Scale( int width
, int height
)
138 wxCHECK_MSG( Ok(), image
, "invlaid image" );
140 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, "invalid image size" );
142 image
.Create( width
, height
);
144 char unsigned *data
= image
.GetData();
146 wxCHECK_MSG( data
, image
, "unable to create image" );
148 if (M_IMGDATA
->m_hasMask
)
149 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
151 double xscale
= (double)width
/ (double)M_IMGDATA
->m_width
;
152 double yscale
= (double)height
/ (double)M_IMGDATA
->m_height
;
154 for (int j
= 0; j
< height
; j
++)
156 for (int i
= 0; i
< width
; i
++)
158 int new_pos
= 3*(j
*width
+ i
);
159 int old_pos
= 3*((long)(j
/yscale
)*M_IMGDATA
->m_width
+ (long)(i
/xscale
));
160 data
[ new_pos
] = M_IMGDATA
->m_data
[ old_pos
];
161 data
[ new_pos
+1 ] = M_IMGDATA
->m_data
[ old_pos
+1 ];
162 data
[ new_pos
+2 ] = M_IMGDATA
->m_data
[ old_pos
+2 ];
169 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
171 wxCHECK_RET( Ok(), "invalid image" );
173 int w
= M_IMGDATA
->m_width
;
174 int h
= M_IMGDATA
->m_height
;
176 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), "invalid image index" );
178 long pos
= (y
* w
+ x
) * 3;
180 M_IMGDATA
->m_data
[ pos
] = r
;
181 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
182 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
185 unsigned char wxImage::GetRed( int x
, int y
)
187 wxCHECK_MSG( Ok(), 0, "invalid image" );
189 int w
= M_IMGDATA
->m_width
;
190 int h
= M_IMGDATA
->m_height
;
192 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
194 long pos
= (y
* w
+ x
) * 3;
196 return M_IMGDATA
->m_data
[pos
];
199 unsigned char wxImage::GetGreen( int x
, int y
)
201 wxCHECK_MSG( Ok(), 0, "invalid image" );
203 int w
= M_IMGDATA
->m_width
;
204 int h
= M_IMGDATA
->m_height
;
206 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
208 long pos
= (y
* w
+ x
) * 3;
210 return M_IMGDATA
->m_data
[pos
+1];
213 unsigned char wxImage::GetBlue( int x
, int y
)
215 wxCHECK_MSG( Ok(), 0, "invalid image" );
217 int w
= M_IMGDATA
->m_width
;
218 int h
= M_IMGDATA
->m_height
;
220 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
222 long pos
= (y
* w
+ x
) * 3;
224 return M_IMGDATA
->m_data
[pos
+2];
227 bool wxImage::Ok() const
229 return (M_IMGDATA
&& M_IMGDATA
->m_ok
);
232 char unsigned *wxImage::GetData() const
234 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, "invalid image" );
236 return M_IMGDATA
->m_data
;
239 void wxImage::SetData( char unsigned *WXUNUSED(data
) )
241 wxCHECK_RET( Ok(), "invalid image" );
244 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
246 wxCHECK_RET( Ok(), "invalid image" );
248 M_IMGDATA
->m_maskRed
= r
;
249 M_IMGDATA
->m_maskGreen
= g
;
250 M_IMGDATA
->m_maskBlue
= b
;
251 M_IMGDATA
->m_hasMask
= TRUE
;
254 unsigned char wxImage::GetMaskRed() const
256 wxCHECK_MSG( Ok(), 0, "invalid image" );
258 return M_IMGDATA
->m_maskRed
;
261 unsigned char wxImage::GetMaskGreen() const
263 wxCHECK_MSG( Ok(), 0, "invalid image" );
265 return M_IMGDATA
->m_maskGreen
;
268 unsigned char wxImage::GetMaskBlue() const
270 wxCHECK_MSG( Ok(), 0, "invalid image" );
272 return M_IMGDATA
->m_maskBlue
;
275 void wxImage::SetMask( bool mask
)
277 wxCHECK_RET( Ok(), "invalid image" );
279 M_IMGDATA
->m_hasMask
= mask
;
282 bool wxImage::HasMask() const
284 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
286 return M_IMGDATA
->m_hasMask
;
289 int wxImage::GetWidth() const
291 wxCHECK_MSG( Ok(), 0, "invalid image" );
293 return M_IMGDATA
->m_width
;
296 int wxImage::GetHeight() const
298 wxCHECK_MSG( Ok(), 0, "invalid image" );
300 return M_IMGDATA
->m_height
;
303 bool wxImage::LoadFile( const wxString
& filename
, long type
)
305 if (wxFileExists(filename
))
307 wxFileInputStream
stream(filename
);
308 return LoadFile(stream
, type
);
312 wxLogWarning( "Image file does not exist." );
318 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
322 m_refData
= new wxImageRefData
;
324 wxImageHandler
*handler
= FindHandler(type
);
328 wxLogWarning( "No image handler for type %d defined.", type
);
333 return handler
->LoadFile( this, stream
);
336 bool wxImage::SaveFile( const wxString
& filename
, int type
)
338 wxFileOutputStream
stream(filename
);
340 if ( stream
.LastError() == wxStream_NOERROR
)
341 return SaveFile(stream
, type
);
346 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
348 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
350 wxImageHandler
*handler
= FindHandler(type
);
354 wxLogWarning( "No image handler for type %d defined.", type
);
359 return handler
->SaveFile( this, stream
);
362 void wxImage::AddHandler( wxImageHandler
*handler
)
364 sm_handlers
.Append( handler
);
367 void wxImage::InsertHandler( wxImageHandler
*handler
)
369 sm_handlers
.Insert( handler
);
372 bool wxImage::RemoveHandler( const wxString
& name
)
374 wxImageHandler
*handler
= FindHandler(name
);
377 sm_handlers
.DeleteObject(handler
);
384 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
386 wxNode
*node
= sm_handlers
.First();
389 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
390 if (handler
->GetName() == name
) return handler
;
393 return (wxImageHandler
*)NULL
;
396 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
398 wxNode
*node
= sm_handlers
.First();
401 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
402 if ( handler
->GetExtension() == extension
&&
403 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
407 return (wxImageHandler
*)NULL
;
410 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
412 wxNode
*node
= sm_handlers
.First();
415 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
416 if (handler
->GetType() == bitmapType
) return handler
;
422 void wxImage::InitStandardHandlers()
424 AddHandler( new wxBMPHandler
);
426 AddHandler( new wxPNGHandler
);
430 void wxImage::CleanUpHandlers()
432 wxNode
*node
= sm_handlers
.First();
435 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
436 wxNode
*next
= node
->Next();
443 //-----------------------------------------------------------------------------
445 //-----------------------------------------------------------------------------
447 #if !USE_SHARED_LIBRARIES
448 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
451 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
) )
456 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
) )
461 //-----------------------------------------------------------------------------
463 //-----------------------------------------------------------------------------
467 #if !USE_SHARED_LIBRARIES
468 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler
,wxImageHandler
)
472 static void _PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
474 ((wxInputStream
*) png_get_io_ptr( png_ptr
)) -> Read(data
, length
);
477 static void _PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
479 ((wxOutputStream
*) png_get_io_ptr( png_ptr
)) -> Write(data
, length
);
482 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
484 // png_structp png_ptr;
485 // png_infop info_ptr;
486 // unsigned char *ptr, **lines, *ptr2;
487 // int transp,bit_depth,color_type,interlace_type;
488 //png_uint_32 width, height;
493 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
494 (voidp
) NULL
, (png_error_ptr
) NULL
, (png_error_ptr
) NULL
);
495 if (!png_ptr
) return FALSE
;
497 png_infop info_ptr
= png_create_info_struct( png_ptr
);
500 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
504 if (setjmp(png_ptr
->jmpbuf
))
506 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
510 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
512 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
515 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
517 png_uint_32 width
,height
;
518 int bit_depth
,color_type
,interlace_type
;
520 png_read_info( png_ptr
, info_ptr
);
521 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
523 if (color_type
== PNG_COLOR_TYPE_PALETTE
) png_set_expand( png_ptr
);
525 png_set_strip_16( png_ptr
);
526 png_set_packing( png_ptr
);
527 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
)) png_set_expand( png_ptr
);
528 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
530 image
->Create( width
, height
);
534 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
538 unsigned char **lines
= (unsigned char **)malloc( height
* sizeof(unsigned char *) );
542 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
546 for (unsigned int i
= 0; i
< height
; i
++)
548 if ((lines
[i
] = (unsigned char *)malloc(width
* (sizeof(unsigned char) * 4))) == NULL
)
551 for (unsigned int n
= 0; n
< i
; n
++) free( lines
[n
] );
553 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
560 png_read_image( png_ptr
, lines
);
561 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
562 unsigned char *ptr
= image
->GetData();
563 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
564 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
566 for (unsigned int y
= 0; y
< height
; y
++)
568 unsigned char *ptr2
= lines
[y
];
569 for (unsigned int x
= 0; x
< width
; x
++)
571 unsigned char r
= *ptr2
++;
572 unsigned char a
= *ptr2
++;
591 for (unsigned int y
= 0; y
< height
; y
++)
593 unsigned char *ptr2
= lines
[y
];
594 for (unsigned int x
= 0; x
< width
; x
++)
596 unsigned char r
= *ptr2
++;
597 unsigned char g
= *ptr2
++;
598 unsigned char b
= *ptr2
++;
599 unsigned char a
= *ptr2
++;
609 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
618 for (unsigned int j
= 0; j
< height
; j
++) free( lines
[j
] );
623 image
->SetMaskColour( 255, 0, 255 );
627 image
->SetMask( FALSE
);
634 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
637 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
643 png_infop info_ptr
= png_create_info_struct(png_ptr
);
644 if (info_ptr
== NULL
)
646 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
650 if (setjmp(png_ptr
->jmpbuf
))
652 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
656 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
658 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
659 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
660 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
667 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
668 png_write_info( png_ptr
, info_ptr
);
669 png_set_shift( png_ptr
, &sig_bit
);
670 png_set_packing( png_ptr
);
672 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
675 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
679 for (int y
= 0; y
< image
->GetHeight(); y
++)
681 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
682 for (int x
= 0; x
< image
->GetWidth(); x
++)
684 data
[(x
<< 2) + 0] = *ptr
++;
685 data
[(x
<< 2) + 1] = *ptr
++;
686 data
[(x
<< 2) + 2] = *ptr
++;
687 if ((data
[(x
<< 2) + 0] == image
->GetMaskRed()) &&
688 (data
[(x
<< 2) + 1] == image
->GetMaskGreen()) &&
689 (data
[(x
<< 2) + 2] == image
->GetMaskBlue()))
691 data
[(x
<< 2) + 3] = 0;
695 data
[(x
<< 2) + 3] = 255;
698 png_bytep row_ptr
= data
;
699 png_write_rows( png_ptr
, &row_ptr
, 1 );
703 png_write_end( png_ptr
, info_ptr
);
704 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
713 //-----------------------------------------------------------------------------
715 //-----------------------------------------------------------------------------
717 #if !USE_SHARED_LIBRARIES
718 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
721 bool wxBMPHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
723 unsigned char *data
, *ptr
;
724 int done
, i
, bpp
, planes
, comp
, ncolors
, line
, column
,
725 linesize
, linepos
, rshift
= 0, gshift
= 0, bshift
= 0;
728 long int dbuf
[4], dword
, rmask
= 0, gmask
= 0, bmask
= 0, offset
,
730 off_t start_offset
= stream
.TellI();
734 unsigned char r
, g
, b
;
744 #define BI_BITFIELDS 3
751 * Reading the bmp header
754 stream
.Read(&bbuf
, 2);
756 stream
.Read(dbuf
, 4 * 4);
761 stream
.Read(dbuf
, 4 * 2);
762 int width
= (int)dbuf
[0];
763 int height
= (int)dbuf
[1];
766 wxLogError( "Image width > 32767 pixels for file\n" );
771 wxLogError( "Image height > 32767 pixels for file\n" );
774 stream
.Read(&word
, 2);
776 stream
.Read(&word
, 2);
778 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
&& 16 && bpp
!= 24 && bpp
!= 32)
780 wxLogError( "unknown bitdepth in file\n" );
783 stream
.Read(dbuf
, 4 * 4);
785 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
787 wxLogError( "unknown encoding in Windows BMP file\n" );
790 stream
.Read(dbuf
, 4 * 2);
791 ncolors
= (int)dbuf
[0];
794 /* some more sanity checks */
795 if (((comp
== BI_RLE4
) && (bpp
!= 4)) || ((comp
== BI_RLE8
) && (bpp
!= 8)) || ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
797 wxLogError( "encoding of BMP doesn't match bitdepth\n" );
802 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
806 wxLogError( "Cannot allocate RAM for color map in BMP file\n" );
813 image
->Create( width
, height
);
814 ptr
= image
->GetData();
817 wxLogError( "Cannot allocate RAM for RGB data in file\n" );
824 * Reading the palette, if it exists.
826 if (bpp
< 16 && ncolors
!= 0)
828 for (i
= 0; i
< ncolors
; i
++)
830 stream
.Read(bbuf
, 4);
836 else if (bpp
== 16 || bpp
== 32)
838 if (comp
== BI_BITFIELDS
)
842 stream
.Read(dbuf
, 4 * 3);
846 /* find shift amount.. ugly, but i can't think of a better way */
847 for (bit
= 0; bit
< bpp
; bit
++)
849 if (bmask
& (1 << bit
))
851 if (gmask
& (1 << bit
))
853 if (rmask
& (1 << bit
))
878 * Reading the image data
880 stream
.SeekI(start_offset
+ offset
);
883 /* set the whole image to the background color */
884 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
886 for (i
= 0; i
< width
* height
; i
++)
896 #define poffset (line * width * 3 + column * 3)
899 * BMPs are stored upside down... hmmmmmmmmmm....
902 linesize
= ((width
* bpp
+ 31) / 32) * 4;
903 for (line
= (height
- 1); line
>= 0; line
--)
906 for (column
= 0; column
< width
;)
913 aByte
= stream
.GetC();
918 for (bit
= 0; bit
< 8; bit
++)
920 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
921 ptr
[poffset
] = cmap
[index
].r
;
922 ptr
[poffset
+ 1] = cmap
[index
].g
;
923 ptr
[poffset
+ 2] = cmap
[index
].b
;
931 wxLogError( "can't deal with 4bit encoded yet.\n");
940 for (nibble
= 0; nibble
< 2; nibble
++)
942 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
945 ptr
[poffset
] = cmap
[index
].r
;
946 ptr
[poffset
+ 1] = cmap
[index
].g
;
947 ptr
[poffset
+ 2] = cmap
[index
].b
;
959 aByte
= stream
.GetC();
964 /* column = width; */
973 aByte
= stream
.GetC();
975 linepos
= column
* bpp
/ 8;
976 aByte
= stream
.GetC();
981 int absolute
= aByte
;
983 for (i
= 0; i
< absolute
; i
++)
986 aByte
= stream
.GetC();
987 ptr
[poffset
] = cmap
[aByte
].r
;
988 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
989 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
993 aByte
= stream
.GetC();
998 for (i
= 0; i
< first
; i
++)
1000 ptr
[poffset
] = cmap
[aByte
].r
;
1001 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1002 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1010 ptr
[poffset
] = cmap
[aByte
].r
;
1011 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1012 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1020 stream
.Read(&bbuf
, 3);
1022 ptr
[poffset
] = (unsigned char)bbuf
[2];
1023 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
1024 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
1031 stream
.Read(&word
, 2);
1033 temp
= (word
& rmask
) >> rshift
;
1034 ptr
[poffset
] = temp
;
1035 temp
= (word
& gmask
) >> gshift
;
1036 ptr
[poffset
+ 1] = temp
;
1037 temp
= (word
& bmask
) >> gshift
;
1038 ptr
[poffset
+ 2] = temp
;
1045 stream
.Read(&dword
, 4);
1047 temp
= (dword
& rmask
) >> rshift
;
1048 ptr
[poffset
] = temp
;
1049 temp
= (dword
& gmask
) >> gshift
;
1050 ptr
[poffset
+ 1] = temp
;
1051 temp
= (dword
& bmask
) >> bshift
;
1052 ptr
[poffset
+ 2] = temp
;
1056 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
1058 stream
.Read(&aByte
, 1);
1060 if (stream
.LastError() != wxStream_NOERROR
)
1064 if (cmap
) free(cmap
);
1066 image
->SetMask( FALSE
);
1073 wxBitmap
wxImage::ConvertToBitmap() const
1077 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1078 int width
= GetWidth();
1079 int height
= GetHeight();
1080 bitmap
.SetWidth( width
);
1081 bitmap
.SetHeight( height
);
1082 bitmap
.SetDepth( wxDisplayDepth() );
1084 int headersize
= sizeof(BITMAPINFOHEADER
);
1085 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1086 wxCHECK_MSG( lpDIBh
, bitmap
, "could not allocate memory for DIB header" );
1088 // Fill in the DIB header
1089 lpDIBh
->bmiHeader
.biSize
= headersize
;
1090 lpDIBh
->bmiHeader
.biWidth
= width
;
1091 lpDIBh
->bmiHeader
.biHeight
= -height
;
1092 lpDIBh
->bmiHeader
.biSizeImage
= width
* height
* 3;
1094 lpDIBh
->bmiHeader
.biPlanes
= 1;
1095 lpDIBh
->bmiHeader
.biBitCount
= 24;
1096 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1097 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1099 // These seem not needed for our purpose here.
1100 // lpDIBh->bmiHeader.biClrImportant = 0;
1101 // lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1102 // lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1104 unsigned char *lpBits
= (unsigned char *) malloc( width
*height
*3 );
1107 wxFAIL_MSG( "could not allocate memory for DIB" );
1112 unsigned char *data
= GetData();
1114 unsigned char *ptdata
= data
, *ptbits
= lpBits
;
1115 for( int i
=0; i
<width
*height
; i
++ )
1117 *(ptbits
++) = *(ptdata
+2);
1118 *(ptbits
++) = *(ptdata
+1);
1119 *(ptbits
++) = *(ptdata
);
1123 HDC hdc
= ::GetDC(NULL
);
1126 hbitmap
= CreateDIBitmap( hdc
, &(lpDIBh
->bmiHeader
), CBM_INIT
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1128 // The above line is equivalent to the following two lines.
1129 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1130 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1131 // or the following lines
1132 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1133 // HDC memdc = ::CreateCompatibleDC( hdc );
1134 // ::SelectObject( memdc, hbitmap);
1135 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
1136 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1137 // ::SelectObject( memdc, 0 );
1138 // ::DeleteDC( memdc );
1140 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
1144 unsigned char r
= GetMaskRed();
1145 unsigned char g
= GetMaskGreen();
1146 unsigned char b
= GetMaskBlue();
1147 unsigned char zero
= 0, one
= 255;
1150 for( int i
=0; i
<width
*height
; i
++ )
1152 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
1165 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)height
, 1, 1, NULL
);
1166 ::SetDIBits( hdc
, hbitmap
, 0, (WORD
)height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1167 wxMask
*mask
= new wxMask();
1168 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
1169 bitmap
.SetMask( mask
);
1171 /* The following can also be used but is slow to run
1172 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
1173 wxMask *mask = new wxMask( bitmap, colour );
1174 bitmap.SetMask( mask );
1178 ::ReleaseDC(NULL
, hdc
);
1182 if( bitmap
.GetHBITMAP() )
1183 bitmap
.SetOk( TRUE
);
1185 bitmap
.SetOk( FALSE
);
1191 wxImage::wxImage( const wxBitmap
&bitmap
)
1195 wxFAIL_MSG( "invalid bitmap" );
1199 int width
= bitmap
.GetWidth();
1200 int height
= bitmap
.GetHeight();
1201 Create( width
, height
);
1202 unsigned char *data
= GetData();
1205 wxFAIL_MSG( "could not allocate data for image" );
1209 int headersize
= sizeof(BITMAPINFOHEADER
);
1210 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1213 wxFAIL_MSG( "could not allocate data for DIB header" );
1218 // Fill in the DIB header
1219 lpDIBh
->bmiHeader
.biSize
= headersize
;
1220 lpDIBh
->bmiHeader
.biWidth
= width
;
1221 lpDIBh
->bmiHeader
.biHeight
= -height
;
1222 lpDIBh
->bmiHeader
.biSizeImage
= width
* height
* 3;
1224 lpDIBh
->bmiHeader
.biPlanes
= 1;
1225 lpDIBh
->bmiHeader
.biBitCount
= 24;
1226 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1227 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1229 // These seem not needed for our purpose here.
1230 // lpDIBh->bmiHeader.biClrImportant = 0;
1231 // lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1232 // lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1234 unsigned char *lpBits
= (unsigned char *) malloc( width
*height
*3 );
1237 wxFAIL_MSG( "could not allocate data for DIB" );
1244 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
1245 HDC hdc
= ::GetDC(NULL
);
1246 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1248 unsigned char *ptdata
= data
, *ptbits
= lpBits
;
1249 for( int i
=0; i
<width
*height
; i
++ )
1251 *(ptdata
++) = *(ptbits
+2);
1252 *(ptdata
++) = *(ptbits
+1);
1253 *(ptdata
++) = *(ptbits
);
1257 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
1259 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
1260 HDC memdc
= ::CreateCompatibleDC( hdc
);
1261 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1262 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1263 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1264 ::DeleteDC( memdc
);
1265 unsigned char r
=16, g
=16, b
=16; // background set to RGB(16,16,16)
1268 for( int i
=0; i
<width
*height
; i
++ )
1278 SetMaskColour( r
, g
, b
);
1281 ::ReleaseDC(NULL
, hdc
);
1290 #include "gtk/gtk.h"
1291 #include "gdk/gdk.h"
1292 #include "gdk/gdkx.h"
1294 wxBitmap
wxImage::ConvertToBitmap() const
1298 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1300 int width
= GetWidth();
1301 int height
= GetHeight();
1303 bitmap
.SetHeight( height
);
1304 bitmap
.SetWidth( width
);
1308 GdkImage
*data_image
=
1309 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1311 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1315 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1319 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1321 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1323 wxMask
*mask
= new wxMask();
1324 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1326 bitmap
.SetMask( mask
);
1331 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1332 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1333 int bpp
= visual
->depth
;
1335 bitmap
.SetDepth( bpp
);
1337 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1338 if (bpp
< 8) bpp
= 8;
1342 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1343 byte_order b_o
= RGB
;
1347 GdkVisual
*visual
= gdk_visual_get_system();
1348 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1349 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1350 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1351 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1352 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1353 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1356 int r_mask
= GetMaskRed();
1357 int g_mask
= GetMaskGreen();
1358 int b_mask
= GetMaskBlue();
1360 unsigned char* data
= GetData();
1363 for (int y
= 0; y
< height
; y
++)
1365 for (int x
= 0; x
< width
; x
++)
1367 int r
= data
[index
];
1369 int g
= data
[index
];
1371 int b
= data
[index
];
1376 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1377 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1379 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1386 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1387 GdkColor
*colors
= cmap
->colors
;
1388 int max
= 3 * (65536);
1391 for (int i
= 0; i
< cmap
->size
; i
++)
1393 int rdiff
= (r
<< 8) - colors
[i
].red
;
1394 int gdiff
= (g
<< 8) - colors
[i
].green
;
1395 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1396 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1397 if (sum
< max
) { index
= i
; max
= sum
; }
1400 gdk_image_put_pixel( data_image
, x
, y
, index
);
1406 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1407 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1412 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1413 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1422 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1423 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1424 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1425 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1426 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1427 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1429 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1438 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1440 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1442 gdk_image_destroy( data_image
);
1443 gdk_gc_unref( data_gc
);
1449 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1451 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1453 gdk_image_destroy( mask_image
);
1454 gdk_gc_unref( mask_gc
);
1460 wxImage::wxImage( const wxBitmap
&bitmap
)
1462 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
1464 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1466 bitmap
.GetWidth(), bitmap
.GetHeight() );
1468 wxCHECK_RET( gdk_image
, "couldn't create image" );
1470 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1471 char unsigned *data
= GetData();
1475 gdk_image_destroy( gdk_image
);
1476 wxFAIL_MSG( "couldn't create image" );
1480 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1481 if (bitmap
.GetMask())
1483 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1485 bitmap
.GetWidth(), bitmap
.GetHeight() );
1487 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1490 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1491 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1492 int bpp
= visual
->depth
;
1493 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1495 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1498 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1500 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1502 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1505 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1506 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1507 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1508 } else if (bpp
== 15)
1510 data
[pos
] = (pixel
>> 7) & 0xf8;
1511 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1512 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1513 } else if (bpp
== 16)
1515 data
[pos
] = (pixel
>> 8) & 0xf8;
1516 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1517 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1520 data
[pos
] = (pixel
>> 16) & 0xff;
1521 data
[pos
+1] = (pixel
>> 8) & 0xff;
1522 data
[pos
+2] = pixel
& 0xff;
1527 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1528 if (mask_pixel
== 0)
1540 gdk_image_destroy( gdk_image
);
1541 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1549 wxBitmap
wxImage::ConvertToBitmap() const
1551 wxFAIL_MSG("Sorry, wxImage::ConvertToBitmap isn't implemented for wxMotif yet.");
1552 return wxNullBitmap
;
1555 wxImage::wxImage( const wxBitmap
&bitmap
)
1557 wxFAIL_MSG("Sorry, wxImage::wxImage(const wxBitmap&) isn't implemented for wxMotif yet.");