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"
25 #include "../png/png.h"
26 #include "wx/filefn.h"
29 #include "gdk/gdkprivate.h"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 class wxImageRefData
: public wxObjectRefData
42 ~wxImageRefData(void);
46 unsigned char *m_data
;
48 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
52 wxImageRefData::wxImageRefData(void)
56 m_data
= (unsigned char*) NULL
;
64 wxImageRefData::~wxImageRefData(void)
66 if (m_data
) free( m_data
);
69 wxList
wxImage::sm_handlers
;
71 //-----------------------------------------------------------------------------
73 #define M_IMGDATA ((wxImageRefData *)m_refData)
75 #if !USE_SHARED_LIBRARIES
76 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
83 wxImage::wxImage( int width
, int height
)
85 Create( width
, height
);
88 wxImage::wxImage( const wxString
& name
, long type
)
90 LoadFile( name
, type
);
93 wxImage::wxImage( const wxImage
& image
)
98 wxImage::wxImage( const wxImage
* image
)
100 if (image
) Ref(*image
);
103 void wxImage::Create( int width
, int height
)
105 m_refData
= new wxImageRefData();
107 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
108 if (M_IMGDATA
->m_data
)
110 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
112 M_IMGDATA
->m_width
= width
;
113 M_IMGDATA
->m_height
= height
;
114 M_IMGDATA
->m_ok
= TRUE
;
122 void wxImage::Destroy()
127 wxImage
wxImage::Scale( int width
, int height
)
131 wxCHECK_MSG( Ok(), image
, "invlaid image" );
133 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, "invalid image size" );
135 image
.Create( width
, height
);
137 char unsigned *data
= image
.GetData();
139 wxCHECK_MSG( data
, image
, "unable to create image" );
141 if (M_IMGDATA
->m_hasMask
)
142 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
144 double xscale
= (double)width
/ (double)M_IMGDATA
->m_width
;
145 double yscale
= (double)height
/ (double)M_IMGDATA
->m_height
;
147 for (int j
= 0; j
< height
; j
++)
149 for (int i
= 0; i
< width
; i
++)
151 int new_pos
= 3*(j
*width
+ i
);
152 int old_pos
= 3*((long)(j
/yscale
)*M_IMGDATA
->m_width
+ (long)(i
/xscale
));
153 data
[ new_pos
] = M_IMGDATA
->m_data
[ old_pos
];
154 data
[ new_pos
+1 ] = M_IMGDATA
->m_data
[ old_pos
+1 ];
155 data
[ new_pos
+2 ] = M_IMGDATA
->m_data
[ old_pos
+2 ];
162 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
164 wxCHECK_RET( Ok(), "invalid image" );
166 int w
= M_IMGDATA
->m_width
;
167 int h
= M_IMGDATA
->m_height
;
169 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), "invalid image index" );
171 long pos
= (y
* w
+ x
) * 3;
173 M_IMGDATA
->m_data
[ pos
] = r
;
174 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
175 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
178 unsigned char wxImage::GetRed( int x
, int y
)
180 wxCHECK_MSG( Ok(), 0, "invalid image" );
182 int w
= M_IMGDATA
->m_width
;
183 int h
= M_IMGDATA
->m_height
;
185 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
187 long pos
= (y
* w
+ x
) * 3;
189 return M_IMGDATA
->m_data
[pos
];
192 unsigned char wxImage::GetGreen( int x
, int y
)
194 wxCHECK_MSG( Ok(), 0, "invalid image" );
196 int w
= M_IMGDATA
->m_width
;
197 int h
= M_IMGDATA
->m_height
;
199 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
201 long pos
= (y
* w
+ x
) * 3;
203 return M_IMGDATA
->m_data
[pos
+1];
206 unsigned char wxImage::GetBlue( int x
, int y
)
208 wxCHECK_MSG( Ok(), 0, "invalid image" );
210 int w
= M_IMGDATA
->m_width
;
211 int h
= M_IMGDATA
->m_height
;
213 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
215 long pos
= (y
* w
+ x
) * 3;
217 return M_IMGDATA
->m_data
[pos
+2];
220 bool wxImage::Ok() const
222 return (M_IMGDATA
&& M_IMGDATA
->m_ok
);
225 char unsigned *wxImage::GetData() const
227 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, "invalid image" );
229 return M_IMGDATA
->m_data
;
232 void wxImage::SetData( char unsigned *WXUNUSED(data
) )
234 wxCHECK_RET( Ok(), "invalid image" );
237 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
239 wxCHECK_RET( Ok(), "invalid image" );
241 M_IMGDATA
->m_maskRed
= r
;
242 M_IMGDATA
->m_maskGreen
= g
;
243 M_IMGDATA
->m_maskBlue
= b
;
244 M_IMGDATA
->m_hasMask
= TRUE
;
247 unsigned char wxImage::GetMaskRed() const
249 wxCHECK_MSG( Ok(), 0, "invalid image" );
251 return M_IMGDATA
->m_maskRed
;
254 unsigned char wxImage::GetMaskGreen() const
256 wxCHECK_MSG( Ok(), 0, "invalid image" );
258 return M_IMGDATA
->m_maskGreen
;
261 unsigned char wxImage::GetMaskBlue() const
263 wxCHECK_MSG( Ok(), 0, "invalid image" );
265 return M_IMGDATA
->m_maskBlue
;
268 void wxImage::SetMask( bool mask
)
270 wxCHECK_RET( Ok(), "invalid image" );
272 M_IMGDATA
->m_hasMask
= mask
;
275 bool wxImage::HasMask() const
277 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
279 return M_IMGDATA
->m_hasMask
;
282 int wxImage::GetWidth() const
284 wxCHECK_MSG( Ok(), 0, "invalid image" );
286 return M_IMGDATA
->m_width
;
289 int wxImage::GetHeight() const
291 wxCHECK_MSG( Ok(), 0, "invalid image" );
293 return M_IMGDATA
->m_height
;
296 bool wxImage::LoadFile( const wxString
& filename
, long type
)
300 if (!wxFileExists(filename
))
302 wxLogWarning( "Image file does not exist." );
307 m_refData
= new wxImageRefData
;
309 wxImageHandler
*handler
= FindHandler(type
);
313 wxLogWarning( "No image handler for type %d defined.", type
);
318 return handler
->LoadFile( this, filename
);
321 bool wxImage::SaveFile( const wxString
& filename
, int type
)
323 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
325 wxImageHandler
*handler
= FindHandler(type
);
329 wxLogWarning( "No image handler for type %d defined.", type
);
334 return handler
->SaveFile( this, filename
);
337 void wxImage::AddHandler( wxImageHandler
*handler
)
339 sm_handlers
.Append( handler
);
342 void wxImage::InsertHandler( wxImageHandler
*handler
)
344 sm_handlers
.Insert( handler
);
347 bool wxImage::RemoveHandler( const wxString
& name
)
349 wxImageHandler
*handler
= FindHandler(name
);
352 sm_handlers
.DeleteObject(handler
);
359 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
361 wxNode
*node
= sm_handlers
.First();
364 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
365 if (handler
->GetName() == name
) return handler
;
368 return (wxImageHandler
*)NULL
;
371 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
373 wxNode
*node
= sm_handlers
.First();
376 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
377 if ( handler
->GetExtension() == extension
&&
378 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
382 return (wxImageHandler
*)NULL
;
385 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
387 wxNode
*node
= sm_handlers
.First();
390 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
391 if (handler
->GetType() == bitmapType
) return handler
;
397 void wxImage::InitStandardHandlers()
399 AddHandler( new wxBMPHandler
);
400 AddHandler( new wxPNGHandler
);
403 void wxImage::CleanUpHandlers()
405 wxNode
*node
= sm_handlers
.First();
408 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
409 wxNode
*next
= node
->Next();
416 //-----------------------------------------------------------------------------
418 //-----------------------------------------------------------------------------
420 #if !USE_SHARED_LIBRARIES
421 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
424 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), const wxString
& WXUNUSED(name
) )
429 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), const wxString
& WXUNUSED(name
) )
434 //-----------------------------------------------------------------------------
436 //-----------------------------------------------------------------------------
438 #if !USE_SHARED_LIBRARIES
439 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler
,wxImageHandler
)
442 bool wxPNGHandler::LoadFile( wxImage
*image
, const wxString
& name
)
447 unsigned char *ptr
, **lines
, *ptr2
;
448 int transp
,bit_depth
,color_type
,interlace_type
;
449 png_uint_32 width
, height
;
455 png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
456 if (!png_ptr
) return FALSE
;
458 info_ptr
= png_create_info_struct( png_ptr
);
461 png_destroy_read_struct( &png_ptr
, NULL
, NULL
);
465 if (setjmp(png_ptr
->jmpbuf
))
467 png_destroy_read_struct( &png_ptr
, &info_ptr
, NULL
);
471 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
473 png_destroy_read_struct( &png_ptr
, &info_ptr
, NULL
);
477 f
= fopen( name
, "rb" );
478 png_init_io( png_ptr
, f
);
480 png_read_info( png_ptr
, info_ptr
);
481 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, NULL
, NULL
);
483 if (color_type
== PNG_COLOR_TYPE_PALETTE
) png_set_expand( png_ptr
);
485 png_set_strip_16( png_ptr
);
486 png_set_packing( png_ptr
);
487 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
)) png_set_expand( png_ptr
);
488 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
490 image
->Create( width
, height
);
494 png_destroy_read_struct( &png_ptr
, &info_ptr
, NULL
);
498 lines
= (unsigned char **)malloc( height
* sizeof(unsigned char *) );
502 png_destroy_read_struct( &png_ptr
, &info_ptr
, NULL
);
506 for (i
= 0; i
< height
; i
++)
508 if ((lines
[i
] = (unsigned char *)malloc(width
* (sizeof(unsigned char) * 4))) == NULL
)
511 for (unsigned int n
= 0; n
< i
; n
++) free( lines
[n
] );
513 png_destroy_read_struct( &png_ptr
, &info_ptr
, NULL
);
518 png_read_image( png_ptr
, lines
);
519 png_destroy_read_struct( &png_ptr
, &info_ptr
, NULL
);
520 ptr
= image
->GetData();
521 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
522 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
524 for (unsigned int y
= 0; y
< height
; y
++)
527 for (unsigned int x
= 0; x
< width
; x
++)
529 unsigned char r
= *ptr2
++;
530 unsigned char a
= *ptr2
++;
549 for (unsigned int y
= 0; y
< height
; y
++)
552 for (unsigned int x
= 0; x
< width
; x
++)
554 unsigned char r
= *ptr2
++;
555 unsigned char g
= *ptr2
++;
556 unsigned char b
= *ptr2
++;
557 unsigned char a
= *ptr2
++;
567 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
575 for (i
= 0; i
< height
; i
++) free( lines
[i
] );
578 image
->SetMaskColour( 255, 0, 255 );
580 image
->SetMask( FALSE
);
586 bool wxPNGHandler::SaveFile( wxImage
*image
, const wxString
& name
)
588 FILE *f
= fopen( name
, "wb" );
591 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
598 png_infop info_ptr
= png_create_info_struct(png_ptr
);
599 if (info_ptr
== NULL
)
602 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
606 if (setjmp(png_ptr
->jmpbuf
))
609 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
613 png_init_io( png_ptr
, f
);
614 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
615 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
616 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
623 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
624 png_write_info( png_ptr
, info_ptr
);
625 png_set_shift( png_ptr
, &sig_bit
);
626 png_set_packing( png_ptr
);
628 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
632 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
636 for (int y
= 0; y
< image
->GetHeight(); y
++)
638 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
639 for (int x
= 0; x
< image
->GetWidth(); x
++)
641 data
[(x
<< 2) + 0] = *ptr
++;
642 data
[(x
<< 2) + 1] = *ptr
++;
643 data
[(x
<< 2) + 2] = *ptr
++;
644 if ((data
[(x
<< 2) + 0] == image
->GetMaskRed()) &&
645 (data
[(x
<< 2) + 1] == image
->GetMaskGreen()) &&
646 (data
[(x
<< 2) + 2] == image
->GetMaskBlue()))
647 data
[(x
<< 2) + 3] = 0;
649 data
[(x
<< 2) + 3] = 255;
651 png_bytep row_ptr
= data
;
652 png_write_rows( png_ptr
, &row_ptr
, 1 );
655 png_write_end( png_ptr
, info_ptr
);
656 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
663 //-----------------------------------------------------------------------------
665 //-----------------------------------------------------------------------------
667 #if !USE_SHARED_LIBRARIES
668 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
671 bool wxBMPHandler::LoadFile( wxImage
*image
, const wxString
& name
)
674 unsigned char *data
, *ptr
;
675 int done
, i
, bpp
, planes
, comp
, ncolors
, line
, column
,
676 linesize
, linepos
, rshift
= 0, gshift
= 0, bshift
= 0;
679 long int dbuf
[4], dword
, rmask
= 0, gmask
= 0, bmask
= 0, offset
,
684 unsigned char r
, g
, b
;
694 #define BI_BITFIELDS 3
699 file
= fopen(name
, "r");
705 * Reading the bmp header
708 fread(&bbuf
, 1, 2, file
);
710 fread(dbuf
, 4, 4, file
);
715 fread(dbuf
, 4, 2, file
);
716 int width
= (int)dbuf
[0];
717 int height
= (int)dbuf
[1];
720 fprintf(stderr
, "IMLIB ERROR: Image width > 32767 pixels for file\n");
726 fprintf(stderr
, "IMLIB ERROR: Image height > 32767 pixels for file\n");
730 fread(&word
, 2, 1, file
);
732 fread(&word
, 2, 1, file
);
734 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
&& 16 && bpp
!= 24 && bpp
!= 32)
736 fprintf(stderr
, "IMLIB ERROR: unknown bitdepth in file\n");
740 fread(dbuf
, 4, 4, file
);
742 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
744 fprintf(stderr
, "IMLIB ERROR: unknown encoding in Windows BMP file\n");
748 fread(dbuf
, 4, 2, file
);
749 ncolors
= (int)dbuf
[0];
752 /* some more sanity checks */
753 if (((comp
== BI_RLE4
) && (bpp
!= 4)) || ((comp
== BI_RLE8
) && (bpp
!= 8)) || ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
755 fprintf(stderr
, "IMLIB ERROR: encoding of BMP doesn't match bitdepth\n");
761 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
765 fprintf(stderr
, "IMLIB ERROR: Cannot allocate RAM for color map in BMP file\n");
773 image
->Create( width
, height
);
774 ptr
= image
->GetData();
777 fprintf(stderr
, "IMLIB ERROR: Cannot allocate RAM for RGB data in file\n");
785 * Reading the palette, if it exists.
787 if (bpp
< 16 && ncolors
!= 0)
789 for (i
= 0; i
< ncolors
; i
++)
791 fread(bbuf
, 1, 4, file
);
797 else if (bpp
== 16 || bpp
== 32)
799 if (comp
== BI_BITFIELDS
)
803 fread(dbuf
, 4, 3, file
);
807 /* find shift amount.. ugly, but i can't think of a better way */
808 for (bit
= 0; bit
< bpp
; bit
++)
810 if (bmask
& (1 << bit
))
812 if (gmask
& (1 << bit
))
814 if (rmask
& (1 << bit
))
839 * REading the image data
841 fseek(file
, offset
, SEEK_SET
);
844 /* set the whole image to the background color */
845 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
847 for (i
= 0; i
< width
* height
; i
++)
857 #define poffset (line * width * 3 + column * 3)
860 * BMPs are stored upside down... hmmmmmmmmmm....
863 linesize
= ((width
* bpp
+ 31) / 32) * 4;
864 for (line
= (height
- 1); line
>= 0; line
--)
867 for (column
= 0; column
< width
;)
879 for (bit
= 0; bit
< 8; bit
++)
881 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
882 ptr
[poffset
] = cmap
[index
].r
;
883 ptr
[poffset
+ 1] = cmap
[index
].g
;
884 ptr
[poffset
+ 2] = cmap
[index
].b
;
892 fprintf(stderr
, "can't deal with 4bit encoded yet.\n");
901 for (nibble
= 0; nibble
< 2; nibble
++)
903 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
906 ptr
[poffset
] = cmap
[index
].r
;
907 ptr
[poffset
+ 1] = cmap
[index
].g
;
908 ptr
[poffset
+ 2] = cmap
[index
].b
;
925 /* column = width; */
936 linepos
= column
* bpp
/ 8;
942 int absolute
= aByte
;
944 for (i
= 0; i
< absolute
; i
++)
948 ptr
[poffset
] = cmap
[aByte
].r
;
949 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
950 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
959 for (i
= 0; i
< first
; i
++)
961 ptr
[poffset
] = cmap
[aByte
].r
;
962 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
963 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
971 ptr
[poffset
] = cmap
[aByte
].r
;
972 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
973 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
981 linepos
+= fread(&bbuf
, 1, 3, file
);
982 ptr
[poffset
] = (unsigned char)bbuf
[2];
983 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
984 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
991 linepos
+= fread(&word
, 2, 1, file
);
992 temp
= (word
& rmask
) >> rshift
;
994 temp
= (word
& gmask
) >> gshift
;
995 ptr
[poffset
+ 1] = temp
;
996 temp
= (word
& bmask
) >> gshift
;
997 ptr
[poffset
+ 2] = temp
;
1004 linepos
+= fread(&dword
, 4, 1, file
);
1005 temp
= (dword
& rmask
) >> rshift
;
1006 ptr
[poffset
] = temp
;
1007 temp
= (dword
& gmask
) >> gshift
;
1008 ptr
[poffset
+ 1] = temp
;
1009 temp
= (dword
& bmask
) >> bshift
;
1010 ptr
[poffset
+ 2] = temp
;
1014 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
1016 int temp
= fread(&aByte
, 1, 1, file
);
1023 if (cmap
) free(cmap
);
1025 image
->SetMask( FALSE
);
1033 wxBitmap
wxImage::ConvertToBitmap() const
1037 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1038 int width
= GetWidth();
1039 int height
= GetHeight();
1040 bitmap
.SetWidth( width
);
1041 bitmap
.SetHeight( height
);
1042 bitmap
.SetDepth( wxDisplayDepth() );
1044 int headersize
= sizeof(BITMAPINFOHEADER
);
1045 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1046 wxCHECK_MSG( lpDIBh
, bitmap
, "could not allocate memory for DIB header" );
1048 // Fill in the DIB header
1049 lpDIBh
->bmiHeader
.biSize
= headersize
;
1050 lpDIBh
->bmiHeader
.biWidth
= width
;
1051 lpDIBh
->bmiHeader
.biHeight
= -height
;
1052 lpDIBh
->bmiHeader
.biSizeImage
= width
* height
* 3;
1054 lpDIBh
->bmiHeader
.biPlanes
= 1;
1055 lpDIBh
->bmiHeader
.biBitCount
= 24;
1056 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1057 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1059 // These seem not needed for our purpose here.
1060 // lpDIBh->bmiHeader.biClrImportant = 0;
1061 // lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1062 // lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1064 unsigned char *lpBits
= (unsigned char *) malloc( width
*height
*3 );
1067 wxFAIL_MSG( "could not allocate memory for DIB" );
1072 unsigned char *data
= GetData();
1074 unsigned char *ptdata
= data
, *ptbits
= lpBits
;
1075 for( int i
=0; i
<width
*height
; i
++ )
1077 *(ptbits
++) = *(ptdata
+2);
1078 *(ptbits
++) = *(ptdata
+1);
1079 *(ptbits
++) = *(ptdata
);
1083 HDC hdc
= ::GetDC(NULL
);
1086 hbitmap
= CreateDIBitmap( hdc
, &(lpDIBh
->bmiHeader
), CBM_INIT
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1088 // The above line is equivalent to the following two lines.
1089 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1090 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1091 // or the following lines
1092 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1093 // HDC memdc = ::CreateCompatibleDC( hdc );
1094 // ::SelectObject( memdc, hbitmap);
1095 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
1096 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1097 // ::SelectObject( memdc, 0 );
1098 // ::DeleteDC( memdc );
1100 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
1104 unsigned char r
= GetMaskRed();
1105 unsigned char g
= GetMaskGreen();
1106 unsigned char b
= GetMaskBlue();
1107 unsigned char zero
= 0, one
= 255;
1110 for( int i
=0; i
<width
*height
; i
++ )
1112 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
1125 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)height
, 1, 1, NULL
);
1126 ::SetDIBits( hdc
, hbitmap
, 0, (WORD
)height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1127 wxMask
*mask
= new wxMask();
1128 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
1129 bitmap
.SetMask( mask
);
1131 /* The following can also be used but is slow to run
1132 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
1133 wxMask *mask = new wxMask( bitmap, colour );
1134 bitmap.SetMask( mask );
1138 ::ReleaseDC(NULL
, hdc
);
1142 if( bitmap
.GetHBITMAP() )
1143 bitmap
.SetOk( TRUE
);
1145 bitmap
.SetOk( FALSE
);
1151 wxImage::wxImage( const wxBitmap
&bitmap
)
1155 wxFAIL_MSG( "invalid bitmap" );
1159 int width
= bitmap
.GetWidth();
1160 int height
= bitmap
.GetHeight();
1161 Create( width
, height
);
1162 unsigned char *data
= GetData();
1165 wxFAIL_MSG( "could not allocate data for image" );
1169 int headersize
= sizeof(BITMAPINFOHEADER
);
1170 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1173 wxFAIL_MSG( "could not allocate data for DIB header" );
1178 // Fill in the DIB header
1179 lpDIBh
->bmiHeader
.biSize
= headersize
;
1180 lpDIBh
->bmiHeader
.biWidth
= width
;
1181 lpDIBh
->bmiHeader
.biHeight
= -height
;
1182 lpDIBh
->bmiHeader
.biSizeImage
= width
* height
* 3;
1184 lpDIBh
->bmiHeader
.biPlanes
= 1;
1185 lpDIBh
->bmiHeader
.biBitCount
= 24;
1186 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1187 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1189 // These seem not needed for our purpose here.
1190 // lpDIBh->bmiHeader.biClrImportant = 0;
1191 // lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1192 // lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1194 unsigned char *lpBits
= (unsigned char *) malloc( width
*height
*3 );
1197 wxFAIL_MSG( "could not allocate data for DIB" );
1204 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
1205 HDC hdc
= ::GetDC(NULL
);
1206 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1208 unsigned char *ptdata
= data
, *ptbits
= lpBits
;
1209 for( int i
=0; i
<width
*height
; i
++ )
1211 *(ptdata
++) = *(ptbits
+2);
1212 *(ptdata
++) = *(ptbits
+1);
1213 *(ptdata
++) = *(ptbits
);
1217 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
1219 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
1220 HDC memdc
= ::CreateCompatibleDC( hdc
);
1221 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1222 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1223 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1224 ::DeleteDC( memdc
);
1225 unsigned char r
=16, g
=16, b
=16; // background set to RGB(16,16,16)
1228 for( int i
=0; i
<width
*height
; i
++ )
1238 SetMaskColour( r
, g
, b
);
1241 ::ReleaseDC(NULL
, hdc
);
1250 wxBitmap
wxImage::ConvertToBitmap() const
1254 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1256 int width
= GetWidth();
1257 int height
= GetHeight();
1259 bitmap
.SetHeight( height
);
1260 bitmap
.SetWidth( width
);
1264 GdkImage
*data_image
=
1265 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1267 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1271 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1275 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1277 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1279 wxMask
*mask
= new wxMask();
1280 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1282 bitmap
.SetMask( mask
);
1287 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1288 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1289 int bpp
= visual
->depth
;
1290 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1291 if (bpp
< 8) bpp
= 8;
1295 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1296 byte_order b_o
= RGB
;
1300 GdkVisual
*visual
= gdk_visual_get_system();
1301 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1302 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1303 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1304 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1305 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1306 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1309 int r_mask
= GetMaskRed();
1310 int g_mask
= GetMaskGreen();
1311 int b_mask
= GetMaskBlue();
1313 unsigned char* data
= GetData();
1316 for (int y
= 0; y
< height
; y
++)
1318 for (int x
= 0; x
< width
; x
++)
1320 int r
= data
[index
];
1322 int g
= data
[index
];
1324 int b
= data
[index
];
1329 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1330 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1332 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1339 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1340 GdkColor
*colors
= cmap
->colors
;
1341 int max
= 3 * (65536);
1344 for (int i
= 0; i
< cmap
->size
; i
++)
1346 int rdiff
= (r
<< 8) - colors
[i
].red
;
1347 int gdiff
= (g
<< 8) - colors
[i
].green
;
1348 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1349 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1350 if (sum
< max
) { index
= i
; max
= sum
; }
1353 gdk_image_put_pixel( data_image
, x
, y
, index
);
1359 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1360 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1365 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1366 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1375 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1376 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1377 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1378 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1379 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1380 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1382 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1391 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1393 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1395 gdk_image_destroy( data_image
);
1396 gdk_gc_unref( data_gc
);
1402 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1404 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1406 gdk_image_destroy( mask_image
);
1407 gdk_gc_unref( mask_gc
);
1413 wxImage::wxImage( const wxBitmap
&bitmap
)
1415 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
1417 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1419 bitmap
.GetWidth(), bitmap
.GetHeight() );
1421 wxCHECK_RET( gdk_image
, "couldn't create image" );
1423 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1424 char unsigned *data
= GetData();
1428 gdk_image_destroy( gdk_image
);
1429 wxFAIL_MSG( "couldn't create image" );
1433 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1434 if (bitmap
.GetMask())
1436 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1438 bitmap
.GetWidth(), bitmap
.GetHeight() );
1440 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1443 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1444 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1445 int bpp
= visual
->depth
;
1446 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1448 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1451 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1453 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1455 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1458 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1459 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1460 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1461 } else if (bpp
== 15)
1463 data
[pos
] = (pixel
>> 7) & 0xf8;
1464 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1465 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1466 } else if (bpp
== 16)
1468 data
[pos
] = (pixel
>> 8) & 0xf8;
1469 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1470 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1473 data
[pos
] = (pixel
>> 16) & 0xff;
1474 data
[pos
+1] = (pixel
>> 8) & 0xff;
1475 data
[pos
+2] = pixel
& 0xff;
1480 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1481 if (mask_pixel
== 0)
1493 gdk_image_destroy( gdk_image
);
1494 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);