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
;
691 #define BI_BITFIELDS 3
696 file
= fopen(name
, "r");
702 * Reading the bmp header
705 fread(&bbuf
, 1, 2, file
);
707 fread(dbuf
, 4, 4, file
);
712 fread(dbuf
, 4, 2, file
);
713 int width
= (int)dbuf
[0];
714 int height
= (int)dbuf
[1];
717 fprintf(stderr
, "IMLIB ERROR: Image width > 32767 pixels for file\n");
723 fprintf(stderr
, "IMLIB ERROR: Image height > 32767 pixels for file\n");
727 fread(&word
, 2, 1, file
);
729 fread(&word
, 2, 1, file
);
731 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
&& 16 && bpp
!= 24 && bpp
!= 32)
733 fprintf(stderr
, "IMLIB ERROR: unknown bitdepth in file\n");
737 fread(dbuf
, 4, 4, file
);
739 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
741 fprintf(stderr
, "IMLIB ERROR: unknown encoding in Windows BMP file\n");
745 fread(dbuf
, 4, 2, file
);
746 ncolors
= (int)dbuf
[0];
749 /* some more sanity checks */
750 if (((comp
== BI_RLE4
) && (bpp
!= 4)) || ((comp
== BI_RLE8
) && (bpp
!= 8)) || ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
752 fprintf(stderr
, "IMLIB ERROR: encoding of BMP doesn't match bitdepth\n");
758 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
762 fprintf(stderr
, "IMLIB ERROR: Cannot allocate RAM for color map in BMP file\n");
770 image
->Create( width
, height
);
771 ptr
= image
->GetData();
774 fprintf(stderr
, "IMLIB ERROR: Cannot allocate RAM for RGB data in file\n");
782 * Reading the palette, if it exists.
784 if (bpp
< 16 && ncolors
!= 0)
786 for (i
= 0; i
< ncolors
; i
++)
788 fread(bbuf
, 1, 4, file
);
794 else if (bpp
== 16 || bpp
== 32)
796 if (comp
== BI_BITFIELDS
)
800 fread(dbuf
, 4, 3, file
);
804 /* find shift amount.. ugly, but i can't think of a better way */
805 for (bit
= 0; bit
< bpp
; bit
++)
807 if (bmask
& (1 << bit
))
809 if (gmask
& (1 << bit
))
811 if (rmask
& (1 << bit
))
836 * REading the image data
838 fseek(file
, offset
, SEEK_SET
);
841 /* set the whole image to the background color */
842 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
844 for (i
= 0; i
< width
* height
; i
++)
854 #define poffset (line * width * 3 + column * 3)
857 * BMPs are stored upside down... hmmmmmmmmmm....
860 linesize
= ((width
* bpp
+ 31) / 32) * 4;
861 for (line
= (height
- 1); line
>= 0; line
--)
864 for (column
= 0; column
< width
;)
876 for (bit
= 0; bit
< 8; bit
++)
878 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
879 ptr
[poffset
] = cmap
[index
].r
;
880 ptr
[poffset
+ 1] = cmap
[index
].g
;
881 ptr
[poffset
+ 2] = cmap
[index
].b
;
889 fprintf(stderr
, "can't deal with 4bit encoded yet.\n");
898 for (nibble
= 0; nibble
< 2; nibble
++)
900 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
903 ptr
[poffset
] = cmap
[index
].r
;
904 ptr
[poffset
+ 1] = cmap
[index
].g
;
905 ptr
[poffset
+ 2] = cmap
[index
].b
;
922 /* column = width; */
933 linepos
= column
* bpp
/ 8;
939 int absolute
= aByte
;
941 for (i
= 0; i
< absolute
; i
++)
945 ptr
[poffset
] = cmap
[aByte
].r
;
946 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
947 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
956 for (i
= 0; i
< first
; i
++)
958 ptr
[poffset
] = cmap
[aByte
].r
;
959 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
960 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
968 ptr
[poffset
] = cmap
[aByte
].r
;
969 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
970 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
978 linepos
+= fread(&bbuf
, 1, 3, file
);
979 ptr
[poffset
] = (unsigned char)bbuf
[2];
980 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
981 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
988 linepos
+= fread(&word
, 2, 1, file
);
989 temp
= (word
& rmask
) >> rshift
;
991 temp
= (word
& gmask
) >> gshift
;
992 ptr
[poffset
+ 1] = temp
;
993 temp
= (word
& bmask
) >> gshift
;
994 ptr
[poffset
+ 2] = temp
;
1001 linepos
+= fread(&dword
, 4, 1, file
);
1002 temp
= (dword
& rmask
) >> rshift
;
1003 ptr
[poffset
] = temp
;
1004 temp
= (dword
& gmask
) >> gshift
;
1005 ptr
[poffset
+ 1] = temp
;
1006 temp
= (dword
& bmask
) >> bshift
;
1007 ptr
[poffset
+ 2] = temp
;
1011 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
1013 int temp
= fread(&aByte
, 1, 1, file
);
1020 if (cmap
) free(cmap
);
1022 image
->SetMask( FALSE
);
1030 wxBitmap
wxImage::ConvertToBitmap() const
1034 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1035 int width
= GetWidth();
1036 int height
= GetHeight();
1037 bitmap
.SetWidth( width
);
1038 bitmap
.SetHeight( height
);
1039 bitmap
.SetDepth( wxDisplayDepth() );
1041 int headersize
= sizeof(BITMAPINFOHEADER
);
1042 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1043 wxCHECK_MSG( lpDIBh
, bitmap
, "could not allocate memory for DIB header" );
1045 // Fill in the DIB header
1046 lpDIBh
->bmiHeader
.biSize
= headersize
;
1047 lpDIBh
->bmiHeader
.biWidth
= width
;
1048 lpDIBh
->bmiHeader
.biHeight
= -height
;
1049 lpDIBh
->bmiHeader
.biSizeImage
= width
* height
* 3;
1051 lpDIBh
->bmiHeader
.biPlanes
= 1;
1052 lpDIBh
->bmiHeader
.biBitCount
= 24;
1053 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1054 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1056 // These seem not needed for our purpose here.
1057 // lpDIBh->bmiHeader.biClrImportant = 0;
1058 // lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1059 // lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1061 unsigned char *lpBits
= (unsigned char *) malloc( width
*height
*3 );
1064 wxFAIL_MSG( "could not allocate memory for DIB" );
1069 unsigned char *data
= GetData();
1071 unsigned char *ptdata
= data
, *ptbits
= lpBits
;
1072 for( int i
=0; i
<width
*height
; i
++ )
1074 *(ptbits
++) = *(ptdata
+2);
1075 *(ptbits
++) = *(ptdata
+1);
1076 *(ptbits
++) = *(ptdata
);
1080 HDC hdc
= ::GetDC(NULL
);
1083 hbitmap
= CreateDIBitmap( hdc
, &(lpDIBh
->bmiHeader
), CBM_INIT
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1085 // The above line is equivalent to the following two lines.
1086 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1087 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1088 // or the following lines
1089 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1090 // HDC memdc = ::CreateCompatibleDC( hdc );
1091 // ::SelectObject( memdc, hbitmap);
1092 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
1093 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1094 // ::SelectObject( memdc, 0 );
1095 // ::DeleteDC( memdc );
1097 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
1101 unsigned char r
= GetMaskRed();
1102 unsigned char g
= GetMaskGreen();
1103 unsigned char b
= GetMaskBlue();
1104 unsigned char zero
= 0, one
= 255;
1107 for( int i
=0; i
<width
*height
; i
++ )
1109 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
1122 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)height
, 1, 1, NULL
);
1123 ::SetDIBits( hdc
, hbitmap
, 0, (WORD
)height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1124 wxMask
*mask
= new wxMask();
1125 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
1126 bitmap
.SetMask( mask
);
1128 /* The following can also be used but is slow to run
1129 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
1130 wxMask *mask = new wxMask( bitmap, colour );
1131 bitmap.SetMask( mask );
1135 ::ReleaseDC(NULL
, hdc
);
1139 if( bitmap
.GetHBITMAP() )
1140 bitmap
.SetOk( TRUE
);
1142 bitmap
.SetOk( FALSE
);
1148 wxImage::wxImage( const wxBitmap
&bitmap
)
1152 wxFAIL_MSG( "invalid bitmap" );
1156 int width
= bitmap
.GetWidth();
1157 int height
= bitmap
.GetHeight();
1158 Create( width
, height
);
1159 unsigned char *data
= GetData();
1162 wxFAIL_MSG( "could not allocate data for image" );
1166 int headersize
= sizeof(BITMAPINFOHEADER
);
1167 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1170 wxFAIL_MSG( "could not allocate data for DIB header" );
1175 // Fill in the DIB header
1176 lpDIBh
->bmiHeader
.biSize
= headersize
;
1177 lpDIBh
->bmiHeader
.biWidth
= width
;
1178 lpDIBh
->bmiHeader
.biHeight
= -height
;
1179 lpDIBh
->bmiHeader
.biSizeImage
= width
* height
* 3;
1181 lpDIBh
->bmiHeader
.biPlanes
= 1;
1182 lpDIBh
->bmiHeader
.biBitCount
= 24;
1183 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1184 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1186 // These seem not needed for our purpose here.
1187 // lpDIBh->bmiHeader.biClrImportant = 0;
1188 // lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1189 // lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1191 unsigned char *lpBits
= (unsigned char *) malloc( width
*height
*3 );
1194 wxFAIL_MSG( "could not allocate data for DIB" );
1201 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
1202 HDC hdc
= ::GetDC(NULL
);
1203 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1205 unsigned char *ptdata
= data
, *ptbits
= lpBits
;
1206 for( int i
=0; i
<width
*height
; i
++ )
1208 *(ptdata
++) = *(ptbits
+2);
1209 *(ptdata
++) = *(ptbits
+1);
1210 *(ptdata
++) = *(ptbits
);
1214 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
1216 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
1217 HDC memdc
= ::CreateCompatibleDC( hdc
);
1218 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1219 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1220 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1221 ::DeleteDC( memdc
);
1222 unsigned char r
=16, g
=16, b
=16; // background set to RGB(16,16,16)
1225 for( int i
=0; i
<width
*height
; i
++ )
1235 SetMaskColour( r
, g
, b
);
1238 ::ReleaseDC(NULL
, hdc
);
1247 wxBitmap
wxImage::ConvertToBitmap() const
1251 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1253 int width
= GetWidth();
1254 int height
= GetHeight();
1256 bitmap
.SetHeight( height
);
1257 bitmap
.SetWidth( width
);
1261 GdkImage
*data_image
=
1262 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1264 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1268 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1272 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1274 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1276 wxMask
*mask
= new wxMask();
1277 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1279 bitmap
.SetMask( mask
);
1284 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1285 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1286 int bpp
= visual
->depth
;
1287 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1288 if (bpp
< 8) bpp
= 8;
1292 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1293 byte_order b_o
= RGB
;
1297 GdkVisual
*visual
= gdk_visual_get_system();
1298 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1299 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1300 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1301 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1302 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1303 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1306 int r_mask
= GetMaskRed();
1307 int g_mask
= GetMaskGreen();
1308 int b_mask
= GetMaskBlue();
1310 unsigned char* data
= GetData();
1313 for (int y
= 0; y
< height
; y
++)
1315 for (int x
= 0; x
< width
; x
++)
1317 int r
= data
[index
];
1319 int g
= data
[index
];
1321 int b
= data
[index
];
1326 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1327 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1329 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1336 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1337 GdkColor
*colors
= cmap
->colors
;
1338 int max
= 3 * (65536);
1341 for (int i
= 0; i
< cmap
->size
; i
++)
1343 int rdiff
= (r
<< 8) - colors
[i
].red
;
1344 int gdiff
= (g
<< 8) - colors
[i
].green
;
1345 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1346 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1347 if (sum
< max
) { index
= i
; max
= sum
; }
1350 gdk_image_put_pixel( data_image
, x
, y
, index
);
1356 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1357 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1362 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1363 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1372 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1373 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1374 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1375 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1376 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1377 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1379 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1388 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1390 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1392 gdk_image_destroy( data_image
);
1393 gdk_gc_unref( data_gc
);
1399 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1401 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1403 gdk_image_destroy( mask_image
);
1404 gdk_gc_unref( mask_gc
);
1410 wxImage::wxImage( const wxBitmap
&bitmap
)
1412 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
1414 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1416 bitmap
.GetWidth(), bitmap
.GetHeight() );
1418 wxCHECK_RET( gdk_image
, "couldn't create image" );
1420 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1421 char unsigned *data
= GetData();
1425 gdk_image_destroy( gdk_image
);
1426 wxFAIL_MSG( "couldn't create image" );
1430 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1431 if (bitmap
.GetMask())
1433 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1435 bitmap
.GetWidth(), bitmap
.GetHeight() );
1437 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1440 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1441 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1442 int bpp
= visual
->depth
;
1443 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1445 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1448 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1450 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1452 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1455 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1456 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1457 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1458 } else if (bpp
== 15)
1460 data
[pos
] = (pixel
>> 7) & 0xf8;
1461 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1462 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1463 } else if (bpp
== 16)
1465 data
[pos
] = (pixel
>> 8) & 0xf8;
1466 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1467 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1470 data
[pos
] = (pixel
>> 16) & 0xff;
1471 data
[pos
+1] = (pixel
>> 8) & 0xff;
1472 data
[pos
+2] = pixel
& 0xff;
1477 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1478 if (mask_pixel
== 0)
1490 gdk_image_destroy( gdk_image
);
1491 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);