1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/bitmap.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/bitmap.h"
17 #include "wx/palette.h"
21 #include "wx/colour.h"
24 #include "wx/rawbmp.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern GtkWidget
*wxGetRootWindow();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
42 m_bitmap
= (GdkBitmap
*) NULL
;
45 wxMask
::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
47 m_bitmap
= (GdkBitmap
*) NULL
;
48 Create( bitmap
, colour
);
52 wxMask
::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
54 m_bitmap
= (GdkBitmap
*) NULL
;
55 Create( bitmap
, paletteIndex
);
57 #endif // wxUSE_PALETTE
59 wxMask
::wxMask( const wxBitmap
& bitmap
)
61 m_bitmap
= (GdkBitmap
*) NULL
;
68 g_object_unref (m_bitmap
);
71 bool wxMask
::Create( const wxBitmap
& bitmap
,
72 const wxColour
& colour
)
76 g_object_unref (m_bitmap
);
77 m_bitmap
= (GdkBitmap
*) NULL
;
80 const int w
= bitmap
.GetWidth();
81 const int h
= bitmap
.GetHeight();
83 // create mask as XBM format bitmap
85 // one bit per pixel, each row starts on a byte boundary
86 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
87 wxByte
* out
= new wxByte
[out_size
];
88 // set bits are unmasked
89 memset(out
, 0xff, out_size
);
90 unsigned bit_index
= 0;
91 if (bitmap
.HasPixbuf())
93 const wxByte r_mask
= colour
.Red();
94 const wxByte g_mask
= colour
.Green();
95 const wxByte b_mask
= colour
.Blue();
96 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
97 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
98 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
99 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
100 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
102 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
103 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
104 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
105 // move index to next byte boundary
106 bit_index
= (bit_index
+ 7) & ~7u;
111 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
112 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
114 if (colormap
== NULL
)
115 // mono bitmap, white is pixel value 0
116 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
120 c
.CalcPixel(colormap
);
121 mask_pixel
= c
.GetPixel();
123 for (int y
= 0; y
< h
; y
++)
125 for (int x
= 0; x
< w
; x
++, bit_index
++)
126 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
127 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
128 bit_index
= (bit_index
+ 7) & ~7u;
130 g_object_unref(image
);
132 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
138 bool wxMask
::Create( const wxBitmap
& bitmap
, int paletteIndex
)
141 wxPalette
*pal
= bitmap
.GetPalette();
143 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
145 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
147 return Create(bitmap
, wxColour(r
, g
, b
));
149 #endif // wxUSE_PALETTE
151 bool wxMask
::Create( const wxBitmap
& bitmap
)
155 g_object_unref (m_bitmap
);
156 m_bitmap
= (GdkBitmap
*) NULL
;
159 if (!bitmap
.Ok()) return false;
161 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
163 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
165 if (!m_bitmap
) return false;
167 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
168 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
169 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
175 GdkBitmap
*wxMask
::GetBitmap() const
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
184 class wxBitmapRefData
: public wxObjectRefData
188 virtual ~wxBitmapRefData();
196 wxPalette
*m_palette
;
199 wxBitmapRefData
::wxBitmapRefData()
201 m_pixmap
= (GdkPixmap
*) NULL
;
202 m_pixbuf
= (GdkPixbuf
*) NULL
;
203 m_mask
= (wxMask
*) NULL
;
207 m_palette
= (wxPalette
*) NULL
;
210 wxBitmapRefData
::~wxBitmapRefData()
213 g_object_unref (m_pixmap
);
215 g_object_unref (m_pixbuf
);
219 #endif // wxUSE_PALETTE
222 //-----------------------------------------------------------------------------
224 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
226 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
232 wxBitmap
::wxBitmap( int width
, int height
, int depth
)
234 Create( width
, height
, depth
);
237 bool wxBitmap
::Create( int width
, int height
, int depth
)
241 if ( width
<= 0 || height
<= 0 )
248 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
));
249 M_BMPDATA
->m_bpp
= 32;
255 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
257 depth
= visual
->depth
;
259 wxCHECK_MSG(depth
== visual
->depth
, false, wxT("invalid bitmap depth"));
262 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
268 bool wxBitmap
::CreateFromXpm( const char **bits
)
272 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
274 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
275 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**)bits
));
277 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
281 M_BMPDATA
->m_mask
= new wxMask
;
282 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
288 wxBitmap wxBitmap
::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
292 wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap"));
294 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
297 int width
= wxMax(newx
, 1);
298 int height
= wxMax(newy
, 1);
299 width
= wxMin(width
, clipwidth
);
300 height
= wxMin(height
, clipheight
);
302 // scale pixbuf if available and it has alpha or there is no mask
303 if (M_BMPDATA
->m_pixbuf
!= NULL
&& (
304 M_BMPDATA
->m_mask
== NULL
|| gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
)))
306 bmp
.SetDepth(GetDepth());
307 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
308 gdk_pixbuf_get_has_alpha(GetPixbuf()),
310 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
313 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
314 GDK_INTERP_BILINEAR
);
318 GdkImage
* img
= gdk_drawable_get_image(GetPixmap(), 0, 0, GetWidth(), GetHeight());
320 wxCHECK_MSG(img
, bmp
, wxT("couldn't create image"));
323 GdkPixmap
*dstpix
= NULL
;
325 long dstbyteperline
= 0;
329 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
331 visual
= wxTheApp
->GetGdkVisual();
333 bmp
= wxBitmap(width
, height
, visual
->depth
);
334 dstpix
= bmp
.GetPixmap();
335 gc
= gdk_gc_new( dstpix
);
339 dstbyteperline
= (width
+ 7) / 8;
340 dst
= (char*) malloc(dstbyteperline
*height
);
343 // be careful to use the right scaling factor
344 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
345 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
346 // prepare accel-tables
347 int *tablex
= (int *)calloc(width
,sizeof(int));
348 int *tabley
= (int *)calloc(height
,sizeof(int));
350 // accel table filled with clipped values
351 for (int x
= 0; x
< width
; x
++)
352 tablex
[x
] = (int) (scx
* (x
+clipx
));
353 for (int y
= 0; y
< height
; y
++)
354 tabley
[y
] = (int) (scy
* (y
+clipy
));
356 // Main rescaling routine starts here
357 for (int h
= 0; h
< height
; h
++)
361 guint32 old_pixval
= 0;
363 for (int w
= 0; w
< width
; w
++)
371 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
381 char shift
= bit
<< (w
% 8);
387 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
395 gdk_gc_set_foreground( gc
, &col
);
396 gdk_draw_point( dstpix
, gc
, w
, h
);
400 // do not forget the last byte
401 if ( dst
&& (width
% 8 != 0) )
402 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
405 g_object_unref (img
);
406 if (gc
) g_object_unref (gc
);
410 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
416 dstbyteperline
= (width
+ 7) / 8;
417 dst
= (char*) malloc(dstbyteperline
*height
);
418 img
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight());
420 for (int h
= 0; h
< height
; h
++)
424 guint32 old_pixval
= 0;
426 for (int w
= 0; w
< width
; w
++)
434 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
442 char shift
= bit
<< (w
% 8);
448 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
453 // do not forget the last byte
455 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
457 wxMask
* mask
= new wxMask
;
458 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
462 g_object_unref (img
);
472 bool wxBitmap
::CreateFromImage(const wxImage
& image
, int depth
)
476 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
477 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
479 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
482 // create pixbuf if image has alpha and requested depth is compatible
483 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
484 return CreateFromImageAsPixbuf(image
);
486 // otherwise create pixmap, if alpha is present it will be converted to mask
487 return CreateFromImageAsPixmap(image
, depth
);
490 bool wxBitmap
::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
492 const int w
= image
.GetWidth();
493 const int h
= image
.GetHeight();
496 // create XBM format bitmap
498 // one bit per pixel, each row starts on a byte boundary
499 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
500 wxByte
* out
= new wxByte
[out_size
];
501 // set bits are black
502 memset(out
, 0xff, out_size
);
503 const wxByte
* in
= image
.GetData();
504 unsigned bit_index
= 0;
505 for (int y
= 0; y
< h
; y
++)
507 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
508 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
509 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
510 // move index to next byte boundary
511 bit_index
= (bit_index
+ 7) & ~7u;
513 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
518 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
519 GdkGC
* gc
= gdk_gc_new(M_BMPDATA
->m_pixmap
);
521 M_BMPDATA
->m_pixmap
, gc
,
523 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
527 const wxByte
* alpha
= image
.GetAlpha();
528 if (alpha
!= NULL
|| image
.HasMask())
530 // create mask as XBM format bitmap
532 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
533 wxByte
* out
= new wxByte
[out_size
];
534 memset(out
, 0xff, out_size
);
535 unsigned bit_index
= 0;
538 for (int y
= 0; y
< h
; y
++)
540 for (int x
= 0; x
< w
; x
++, bit_index
++)
541 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
542 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
543 bit_index
= (bit_index
+ 7) & ~7u;
548 const wxByte r_mask
= image
.GetMaskRed();
549 const wxByte g_mask
= image
.GetMaskGreen();
550 const wxByte b_mask
= image
.GetMaskBlue();
551 const wxByte
* in
= image
.GetData();
552 for (int y
= 0; y
< h
; y
++)
554 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
555 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
556 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
557 bit_index
= (bit_index
+ 7) & ~7u;
560 wxMask
* mask
= new wxMask
;
561 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
568 bool wxBitmap
::CreateFromImageAsPixbuf(const wxImage
& image
)
570 wxASSERT(image
.HasAlpha());
572 int width
= image
.GetWidth();
573 int height
= image
.GetHeight();
575 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
577 8 /* bits per sample */,
586 unsigned char *in
= image
.GetData();
587 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
588 unsigned char *alpha
= image
.GetAlpha();
590 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
592 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
594 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
606 wxImage wxBitmap
::ConvertToImage() const
610 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
612 const int w
= GetWidth();
613 const int h
= GetHeight();
615 unsigned char *data
= image
.GetData();
617 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
619 // prefer pixbuf if available, it will preserve alpha and should be quicker
622 GdkPixbuf
*pixbuf
= GetPixbuf();
623 unsigned char* alpha
= NULL
;
624 if (gdk_pixbuf_get_has_alpha(pixbuf
))
627 alpha
= image
.GetAlpha();
629 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
630 unsigned char *out
= data
;
631 const int inc
= 3 + int(alpha
!= NULL
);
632 const int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
634 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
636 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
648 GdkPixmap
* pixmap
= GetPixmap();
649 GdkPixmap
* pixmap_invert
= NULL
;
652 // mono bitmaps are inverted, i.e. 0 is white
653 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
654 GdkGC
* gc
= gdk_gc_new(pixmap_invert
);
655 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
656 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
658 pixmap
= pixmap_invert
;
660 // create a pixbuf which shares data with the wxImage
661 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
662 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
664 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
666 g_object_unref(pixbuf
);
667 if (pixmap_invert
!= NULL
)
668 g_object_unref(pixmap_invert
);
670 // convert mask, unless there is already alpha
671 if (GetMask() && !image
.HasAlpha())
673 // the colour used as transparent one in wxImage and the one it is
674 // replaced with when it really occurs in the bitmap
675 const int MASK_RED
= 1;
676 const int MASK_GREEN
= 2;
677 const int MASK_BLUE
= 3;
678 const int MASK_BLUE_REPLACEMENT
= 2;
680 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
681 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
683 for (int y
= 0; y
< h
; y
++)
685 for (int x
= 0; x
< w
; x
++, data
+= 3)
687 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
690 data
[1] = MASK_GREEN
;
693 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
695 data
[2] = MASK_BLUE_REPLACEMENT
;
699 g_object_unref(image_mask
);
705 wxBitmap
::wxBitmap( const wxString
&filename
, wxBitmapType type
)
707 LoadFile( filename
, type
);
710 wxBitmap
::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
712 if ( width
> 0 && height
> 0 )
714 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
716 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
720 wxBitmap
::~wxBitmap()
724 bool wxBitmap
::operator == ( const wxBitmap
& bmp
) const
726 return m_refData
== bmp
.m_refData
;
729 bool wxBitmap
::operator != ( const wxBitmap
& bmp
) const
731 return m_refData
!= bmp
.m_refData
;
734 bool wxBitmap
::Ok() const
736 return (m_refData
!= NULL
) &&
738 M_BMPDATA
->m_pixbuf
||
743 int wxBitmap
::GetHeight() const
745 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
747 return M_BMPDATA
->m_height
;
750 int wxBitmap
::GetWidth() const
752 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
754 return M_BMPDATA
->m_width
;
757 int wxBitmap
::GetDepth() const
759 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
761 return M_BMPDATA
->m_bpp
;
764 wxMask
*wxBitmap
::GetMask() const
766 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
768 return M_BMPDATA
->m_mask
;
771 void wxBitmap
::SetMask( wxMask
*mask
)
773 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
775 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
777 M_BMPDATA
->m_mask
= mask
;
780 bool wxBitmap
::CopyFromIcon(const wxIcon
& icon
)
786 wxBitmap wxBitmap
::GetSubBitmap( const wxRect
& rect
) const
791 (rect
.x
>= 0) && (rect
.y
>= 0) &&
792 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
793 ret
, wxT("invalid bitmap or bitmap region") );
797 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
798 gdk_pixbuf_get_has_alpha(GetPixbuf()),
799 8, rect
.width
, rect
.height
);
800 ret
.SetPixbuf(pixbuf
);
801 ret
.SetDepth(M_BMPDATA
->m_bpp
);
802 gdk_pixbuf_copy_area(GetPixbuf(),
803 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
808 ret
= wxBitmap(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
809 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
810 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
813 // make mask, unless there is already alpha
814 if (GetMask() && !HasAlpha())
816 wxMask
*mask
= new wxMask
;
817 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
819 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
820 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
829 bool wxBitmap
::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
831 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
833 // Try to save the bitmap via wxImage handlers:
834 wxImage image
= ConvertToImage();
835 return image
.Ok() && image
.SaveFile(name
, type
);
838 bool wxBitmap
::LoadFile( const wxString
&name
, wxBitmapType type
)
842 if (type
== wxBITMAP_TYPE_XPM
)
844 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
845 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
849 M_BMPDATA
->m_mask
= new wxMask
;
850 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
853 else // try if wxImage can load it
856 if (image
.LoadFile(name
, type
) && image
.Ok())
857 *this = wxBitmap(image
);
864 wxPalette
*wxBitmap
::GetPalette() const
867 return (wxPalette
*) NULL
;
869 return M_BMPDATA
->m_palette
;
872 void wxBitmap
::SetPalette(const wxPalette
& WXUNUSED(palette
))
876 #endif // wxUSE_PALETTE
878 void wxBitmap
::SetHeight( int height
)
881 m_refData
= new wxBitmapRefData
;
883 M_BMPDATA
->m_height
= height
;
886 void wxBitmap
::SetWidth( int width
)
889 m_refData
= new wxBitmapRefData
;
891 M_BMPDATA
->m_width
= width
;
894 void wxBitmap
::SetDepth( int depth
)
897 m_refData
= new wxBitmapRefData
;
899 M_BMPDATA
->m_bpp
= depth
;
902 void wxBitmap
::SetPixmap( GdkPixmap
*pixmap
)
905 m_refData
= new wxBitmapRefData
;
907 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
908 M_BMPDATA
->m_pixmap
= pixmap
;
909 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
910 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
911 PurgeOtherRepresentations(Pixmap
);
914 GdkPixmap
*wxBitmap
::GetPixmap() const
916 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
918 // create the pixmap on the fly if we use Pixbuf representation:
919 if (M_BMPDATA
->m_pixmap
== NULL
)
921 GdkPixmap
** pmask
= NULL
;
922 if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
))
924 // make new mask from alpha
925 delete M_BMPDATA
->m_mask
;
926 M_BMPDATA
->m_mask
= new wxMask
;
927 pmask
= &M_BMPDATA
->m_mask
->m_bitmap
;
929 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
930 &M_BMPDATA
->m_pixmap
,
935 return M_BMPDATA
->m_pixmap
;
938 bool wxBitmap
::HasPixmap() const
940 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
942 return M_BMPDATA
->m_pixmap
!= NULL
;
945 GdkPixbuf
*wxBitmap
::GetPixbuf() const
947 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
949 if (M_BMPDATA
->m_pixbuf
== NULL
)
951 int width
= GetWidth();
952 int height
= GetHeight();
954 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
957 M_BMPDATA
->m_pixbuf
=
958 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
959 0, 0, 0, 0, width
, height
);
961 // apply the mask to created pixbuf:
962 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
965 gdk_pixbuf_get_from_drawable(NULL
,
966 M_BMPDATA
->m_mask
->GetBitmap(),
968 0, 0, 0, 0, width
, height
);
971 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
972 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
973 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
974 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
976 for (int y
= 0; y
< height
;
977 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
979 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
981 if (mask
[0] == 0 /*black pixel*/)
986 g_object_unref (pmask
);
991 return M_BMPDATA
->m_pixbuf
;
994 bool wxBitmap
::HasPixbuf() const
996 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
998 return M_BMPDATA
->m_pixbuf
!= NULL
;
1001 void wxBitmap
::SetPixbuf( GdkPixbuf
*pixbuf
)
1004 m_refData
= new wxBitmapRefData
;
1006 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
1007 M_BMPDATA
->m_pixbuf
= pixbuf
;
1008 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
1009 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
1010 PurgeOtherRepresentations(Pixbuf
);
1013 void wxBitmap
::PurgeOtherRepresentations(wxBitmap
::Representation keep
)
1015 if (keep
== Pixmap
&& HasPixbuf())
1017 g_object_unref (M_BMPDATA
->m_pixbuf
);
1018 M_BMPDATA
->m_pixbuf
= NULL
;
1020 if (keep
== Pixbuf
&& HasPixmap())
1022 g_object_unref (M_BMPDATA
->m_pixmap
);
1023 M_BMPDATA
->m_pixmap
= NULL
;
1027 void *wxBitmap
::GetRawData(wxPixelDataBase
& data
, int bpp
)
1030 GdkPixbuf
*pixbuf
= GetPixbuf();
1031 const bool hasAlpha
= HasAlpha();
1032 // allow access if bpp is valid and matches existence of alpha
1033 if (pixbuf
!= NULL
&& (
1034 bpp
== 24 && !hasAlpha
||
1035 bpp
== 32 && hasAlpha
))
1037 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1038 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1039 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1040 bits
= gdk_pixbuf_get_pixels(pixbuf
);
1045 void wxBitmap
::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1049 bool wxBitmap
::HasAlpha() const
1051 return m_refData
!= NULL
&& M_BMPDATA
->m_pixbuf
!= NULL
&&
1052 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
);
1055 void wxBitmap
::UseAlpha()
1057 GdkPixbuf
* pixbuf
= GetPixbuf();
1058 // add alpha if necessary
1059 if (!gdk_pixbuf_get_has_alpha(pixbuf
))
1061 M_BMPDATA
->m_pixbuf
= NULL
;
1062 SetPixbuf(gdk_pixbuf_add_alpha(pixbuf
, false, 0, 0, 0));
1063 g_object_unref(pixbuf
);
1067 //-----------------------------------------------------------------------------
1069 //-----------------------------------------------------------------------------
1071 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1073 wxBitmapHandler
::~wxBitmapHandler()
1077 bool wxBitmapHandler
::Create(wxBitmap
* WXUNUSED(bitmap
),
1078 void * WXUNUSED(data
),
1079 long WXUNUSED(type
),
1080 int WXUNUSED(width
),
1081 int WXUNUSED(height
),
1082 int WXUNUSED(depth
))
1084 wxFAIL_MSG( _T("not implemented") );
1089 bool wxBitmapHandler
::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1090 const wxString
& WXUNUSED(name
),
1091 long WXUNUSED(flags
),
1092 int WXUNUSED(desiredWidth
),
1093 int WXUNUSED(desiredHeight
))
1095 wxFAIL_MSG( _T("not implemented") );
1100 bool wxBitmapHandler
::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1101 const wxString
& WXUNUSED(name
),
1103 const wxPalette
* WXUNUSED(palette
))
1105 wxFAIL_MSG( _T("not implemented") );
1110 /* static */ void wxBitmap
::InitStandardHandlers()
1112 // TODO: Insert handler based on GdkPixbufs handler later