]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
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"
18 #include "wx/colour.h"
21 #include "wx/rawbmp.h"
23 #include "wx/gtk/private/object.h"
27 extern GtkWidget
*wxGetRootWindow();
29 static void PixmapToPixbuf(GdkPixmap
* pixmap
, GdkPixbuf
* pixbuf
, int w
, int h
)
31 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
32 if (gdk_drawable_get_depth(pixmap
) == 1)
34 // invert to match XBM convention
35 guchar
* p
= gdk_pixbuf_get_pixels(pixbuf
);
36 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
37 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - w
* inc
;
38 for (int y
= h
; y
; y
--, p
+= rowpad
)
39 for (int x
= w
; x
; x
--, p
+= inc
)
41 // pixels are either (0,0,0) or (0xff,0xff,0xff)
49 static void MaskToAlpha(GdkPixmap
* mask
, GdkPixbuf
* pixbuf
, int w
, int h
)
51 GdkPixbuf
* mask_pixbuf
= gdk_pixbuf_get_from_drawable(
52 NULL
, mask
, NULL
, 0, 0, 0, 0, w
, h
);
53 guchar
* p
= gdk_pixbuf_get_pixels(pixbuf
) + 3;
54 const guchar
* mask_data
= gdk_pixbuf_get_pixels(mask_pixbuf
);
55 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - w
* 4;
56 const int mask_rowpad
= gdk_pixbuf_get_rowstride(mask_pixbuf
) - w
* 3;
57 for (int y
= h
; y
; y
--, p
+= rowpad
, mask_data
+= mask_rowpad
)
59 for (int x
= w
; x
; x
--, p
+= 4, mask_data
+= 3)
62 // no need to test all 3 components,
63 // pixels are either (0,0,0) or (0xff,0xff,0xff)
64 if (mask_data
[0] == 0)
68 g_object_unref(mask_pixbuf
);
71 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
75 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxMaskBase
)
82 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
85 InitFromColour(bitmap
, colour
);
89 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
92 Create( bitmap
, paletteIndex
);
94 #endif // wxUSE_PALETTE
96 wxMask::wxMask( const wxBitmap
& bitmap
)
99 InitFromMonoBitmap(bitmap
);
105 g_object_unref (m_bitmap
);
108 void wxMask::FreeData()
112 g_object_unref (m_bitmap
);
117 bool wxMask::InitFromColour(const wxBitmap
& bitmap
, const wxColour
& colour
)
119 const int w
= bitmap
.GetWidth();
120 const int h
= bitmap
.GetHeight();
122 // create mask as XBM format bitmap
124 // one bit per pixel, each row starts on a byte boundary
125 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
126 wxByte
* out
= new wxByte
[out_size
];
127 // set bits are unmasked
128 memset(out
, 0xff, out_size
);
129 unsigned bit_index
= 0;
130 if (bitmap
.HasPixbuf())
132 const wxByte r_mask
= colour
.Red();
133 const wxByte g_mask
= colour
.Green();
134 const wxByte b_mask
= colour
.Blue();
135 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
136 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
137 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
138 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
139 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
141 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
142 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
143 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
144 // move index to next byte boundary
145 bit_index
= (bit_index
+ 7) & ~7u;
150 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
151 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
153 if (colormap
== NULL
)
154 // mono bitmap, white is pixel value 0
155 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
159 c
.CalcPixel(colormap
);
160 mask_pixel
= c
.GetPixel();
162 for (int y
= 0; y
< h
; y
++)
164 for (int x
= 0; x
< w
; x
++, bit_index
++)
165 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
166 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
167 bit_index
= (bit_index
+ 7) & ~7u;
169 g_object_unref(image
);
171 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
176 bool wxMask::InitFromMonoBitmap(const wxBitmap
& bitmap
)
178 if (!bitmap
.IsOk()) return false;
180 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
182 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
184 if (!m_bitmap
) return false;
186 wxGtkObject
<GdkGC
> gc(gdk_gc_new( m_bitmap
));
187 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
188 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
193 GdkBitmap
*wxMask::GetBitmap() const
198 //-----------------------------------------------------------------------------
200 //-----------------------------------------------------------------------------
202 class wxBitmapRefData
: public wxGDIRefData
205 wxBitmapRefData(int width
, int height
, int depth
);
206 virtual ~wxBitmapRefData();
208 virtual bool IsOk() const;
216 bool m_alphaRequested
;
219 wxBitmapRefData::wxBitmapRefData(int width
, int height
, int depth
)
228 m_bpp
= gdk_drawable_get_depth(wxGetRootWindow()->window
);
229 m_alphaRequested
= depth
== 32;
232 wxBitmapRefData::~wxBitmapRefData()
235 g_object_unref (m_pixmap
);
237 g_object_unref (m_pixbuf
);
241 bool wxBitmapRefData::IsOk() const
246 //-----------------------------------------------------------------------------
248 //-----------------------------------------------------------------------------
250 #define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
252 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
254 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
256 LoadFile(filename
, type
);
259 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
261 wxASSERT(depth
== 1);
262 if (width
> 0 && height
> 0 && depth
== 1)
264 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
268 wxBitmap::wxBitmap(const char* const* bits
)
270 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
272 GdkBitmap
* mask
= NULL
;
273 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, const_cast<char**>(bits
)));
277 if (M_BMPDATA
->m_pixmap
!= NULL
&& mask
!= NULL
)
279 M_BMPDATA
->m_mask
= new wxMask
;
280 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
284 wxBitmap::~wxBitmap()
288 bool wxBitmap::Create( int width
, int height
, int depth
)
291 wxCHECK_MSG(width
>= 0 && height
>= 0, false, "invalid bitmap size");
292 m_refData
= new wxBitmapRefData(width
, height
, depth
);
298 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
302 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
303 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
305 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
308 // create pixbuf if image has alpha and requested depth is compatible
309 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
310 return CreateFromImageAsPixbuf(image
);
312 // otherwise create pixmap, if alpha is present it will be converted to mask
313 return CreateFromImageAsPixmap(image
, depth
);
316 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
318 const int w
= image
.GetWidth();
319 const int h
= image
.GetHeight();
322 // create XBM format bitmap
324 // one bit per pixel, each row starts on a byte boundary
325 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
326 wxByte
* out
= new wxByte
[out_size
];
327 // set bits are black
328 memset(out
, 0xff, out_size
);
329 const wxByte
* in
= image
.GetData();
330 unsigned bit_index
= 0;
331 for (int y
= 0; y
< h
; y
++)
333 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
334 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
335 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
336 // move index to next byte boundary
337 bit_index
= (bit_index
+ 7) & ~7u;
339 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
342 if (!M_BMPDATA
) // SetPixmap may have failed
347 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
351 wxGtkObject
<GdkGC
> gc(gdk_gc_new(M_BMPDATA
->m_pixmap
));
353 M_BMPDATA
->m_pixmap
, gc
,
355 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
358 const wxByte
* alpha
= image
.GetAlpha();
359 if (alpha
!= NULL
|| image
.HasMask())
361 // create mask as XBM format bitmap
363 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
364 wxByte
* out
= new wxByte
[out_size
];
365 memset(out
, 0xff, out_size
);
366 unsigned bit_index
= 0;
369 for (int y
= 0; y
< h
; y
++)
371 for (int x
= 0; x
< w
; x
++, bit_index
++)
372 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
373 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
374 bit_index
= (bit_index
+ 7) & ~7u;
379 const wxByte r_mask
= image
.GetMaskRed();
380 const wxByte g_mask
= image
.GetMaskGreen();
381 const wxByte b_mask
= image
.GetMaskBlue();
382 const wxByte
* in
= image
.GetData();
383 for (int y
= 0; y
< h
; y
++)
385 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
386 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
387 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
388 bit_index
= (bit_index
+ 7) & ~7u;
391 wxMask
* mask
= new wxMask
;
392 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
399 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
401 wxASSERT(image
.HasAlpha());
403 int width
= image
.GetWidth();
404 int height
= image
.GetHeight();
406 Create(width
, height
, 32);
407 GdkPixbuf
* pixbuf
= GetPixbuf();
412 const unsigned char* in
= image
.GetData();
413 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
414 unsigned char *alpha
= image
.GetAlpha();
416 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
418 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
420 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
432 wxImage
wxBitmap::ConvertToImage() const
434 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
436 const int w
= GetWidth();
437 const int h
= GetHeight();
438 wxImage
image(w
, h
, false);
439 unsigned char *data
= image
.GetData();
441 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
443 // prefer pixbuf if available, it will preserve alpha and should be quicker
446 GdkPixbuf
*pixbuf
= GetPixbuf();
447 unsigned char* alpha
= NULL
;
448 if (gdk_pixbuf_get_has_alpha(pixbuf
))
451 alpha
= image
.GetAlpha();
453 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
454 unsigned char *out
= data
;
455 const int inc
= 3 + int(alpha
!= NULL
);
456 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
458 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
460 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
472 GdkPixmap
* pixmap
= GetPixmap();
473 GdkPixmap
* pixmap_invert
= NULL
;
476 // mono bitmaps are inverted, i.e. 0 is white
477 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
478 wxGtkObject
<GdkGC
> gc(gdk_gc_new(pixmap_invert
));
479 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
480 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
481 pixmap
= pixmap_invert
;
483 // create a pixbuf which shares data with the wxImage
484 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
485 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
487 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
489 g_object_unref(pixbuf
);
490 if (pixmap_invert
!= NULL
)
491 g_object_unref(pixmap_invert
);
493 // convert mask, unless there is already alpha
494 if (GetMask() && !image
.HasAlpha())
496 // we hard code the mask colour for now but we could also make an
497 // effort (and waste time) to choose a colour not present in the
498 // image already to avoid having to fudge the pixels below --
499 // whether it's worth to do it is unclear however
500 const int MASK_RED
= 1;
501 const int MASK_GREEN
= 2;
502 const int MASK_BLUE
= 3;
503 const int MASK_BLUE_REPLACEMENT
= 2;
505 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
506 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
508 for (int y
= 0; y
< h
; y
++)
510 for (int x
= 0; x
< w
; x
++, data
+= 3)
512 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
515 data
[1] = MASK_GREEN
;
518 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
520 // we have to fudge the colour a bit to prevent
521 // this pixel from appearing transparent
522 data
[2] = MASK_BLUE_REPLACEMENT
;
526 g_object_unref(image_mask
);
532 #endif // wxUSE_IMAGE
534 int wxBitmap::GetHeight() const
536 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
538 return M_BMPDATA
->m_height
;
541 int wxBitmap::GetWidth() const
543 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
545 return M_BMPDATA
->m_width
;
548 int wxBitmap::GetDepth() const
550 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
552 return M_BMPDATA
->m_bpp
;
555 wxMask
*wxBitmap::GetMask() const
557 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
559 return M_BMPDATA
->m_mask
;
562 void wxBitmap::SetMask( wxMask
*mask
)
564 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
567 delete M_BMPDATA
->m_mask
;
568 M_BMPDATA
->m_mask
= mask
;
571 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
577 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
581 wxCHECK_MSG(IsOk(), ret
, wxT("invalid bitmap"));
583 const int w
= rect
.width
;
584 const int h
= rect
.height
;
585 const wxBitmapRefData
* bmpData
= M_BMPDATA
;
587 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
588 rect
.x
+ w
<= bmpData
->m_width
&&
589 rect
.y
+ h
<= bmpData
->m_height
,
590 ret
, wxT("invalid bitmap region"));
592 wxBitmapRefData
* newRef
= new wxBitmapRefData(*bmpData
);
593 ret
.m_refData
= newRef
;
595 newRef
->m_height
= h
;
597 if (bmpData
->m_pixbuf
)
600 gdk_pixbuf_new_subpixbuf(bmpData
->m_pixbuf
, rect
.x
, rect
.y
, w
, h
);
601 newRef
->m_pixbuf
= gdk_pixbuf_copy(pixbuf
);
602 g_object_unref(pixbuf
);
604 if (bmpData
->m_pixmap
)
606 newRef
->m_pixmap
= gdk_pixmap_new(bmpData
->m_pixmap
, w
, h
, -1);
607 GdkGC
* gc
= gdk_gc_new(newRef
->m_pixmap
);
609 newRef
->m_pixmap
, gc
, bmpData
->m_pixmap
, rect
.x
, rect
.y
, 0, 0, w
, h
);
612 newRef
->m_mask
= NULL
;
613 if (bmpData
->m_mask
&& bmpData
->m_mask
->m_bitmap
)
615 GdkPixmap
* sub_mask
= gdk_pixmap_new(bmpData
->m_mask
->m_bitmap
, w
, h
, 1);
616 newRef
->m_mask
= new wxMask
;
617 newRef
->m_mask
->m_bitmap
= sub_mask
;
618 GdkGC
* gc
= gdk_gc_new(sub_mask
);
620 sub_mask
, gc
, bmpData
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, w
, h
);
627 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
629 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
632 wxImage image
= ConvertToImage();
633 if (image
.IsOk() && image
.SaveFile(name
, type
))
636 const char* type_name
= NULL
;
639 case wxBITMAP_TYPE_BMP
: type_name
= "bmp"; break;
640 case wxBITMAP_TYPE_ICO
: type_name
= "ico"; break;
641 case wxBITMAP_TYPE_JPEG
: type_name
= "jpeg"; break;
642 case wxBITMAP_TYPE_PNG
: type_name
= "png"; break;
646 gdk_pixbuf_save(GetPixbuf(), name
.fn_str(), type_name
, NULL
, NULL
);
649 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
653 if (image
.LoadFile(name
, type
) && image
.IsOk())
654 *this = wxBitmap(image
);
659 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_file(name
.fn_str(), NULL
);
668 wxPalette
*wxBitmap::GetPalette() const
673 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
677 #endif // wxUSE_PALETTE
679 void wxBitmap::SetHeight( int height
)
682 M_BMPDATA
->m_height
= height
;
685 void wxBitmap::SetWidth( int width
)
688 M_BMPDATA
->m_width
= width
;
691 void wxBitmap::SetDepth( int depth
)
694 M_BMPDATA
->m_bpp
= depth
;
697 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
705 gdk_drawable_get_size(pixmap
, &w
, &h
);
706 wxBitmapRefData
* bmpData
= new wxBitmapRefData(w
, h
, 0);
708 bmpData
->m_pixmap
= pixmap
;
709 bmpData
->m_bpp
= gdk_drawable_get_depth(pixmap
);
712 GdkPixmap
*wxBitmap::GetPixmap() const
714 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
716 wxBitmapRefData
* bmpData
= M_BMPDATA
;
717 if (bmpData
->m_pixmap
)
718 return bmpData
->m_pixmap
;
720 if (bmpData
->m_pixbuf
)
722 GdkPixmap
** mask_pixmap
= NULL
;
723 if (gdk_pixbuf_get_has_alpha(bmpData
->m_pixbuf
))
725 // make new mask from alpha
726 delete bmpData
->m_mask
;
727 bmpData
->m_mask
= new wxMask
;
728 mask_pixmap
= &bmpData
->m_mask
->m_bitmap
;
730 gdk_pixbuf_render_pixmap_and_mask(
731 bmpData
->m_pixbuf
, &bmpData
->m_pixmap
, mask_pixmap
, 128);
735 bmpData
->m_pixmap
= gdk_pixmap_new(wxGetRootWindow()->window
,
736 bmpData
->m_width
, bmpData
->m_height
, bmpData
->m_bpp
== 1 ? 1 : -1);
738 return bmpData
->m_pixmap
;
741 bool wxBitmap::HasPixmap() const
743 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
745 return M_BMPDATA
->m_pixmap
!= NULL
;
748 GdkPixbuf
*wxBitmap::GetPixbuf() const
750 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
752 wxBitmapRefData
* bmpData
= M_BMPDATA
;
753 if (bmpData
->m_pixbuf
)
754 return bmpData
->m_pixbuf
;
756 const int w
= bmpData
->m_width
;
757 const int h
= bmpData
->m_height
;
758 GdkPixmap
* mask
= NULL
;
760 mask
= bmpData
->m_mask
->m_bitmap
;
761 const bool useAlpha
= bmpData
->m_alphaRequested
|| mask
;
762 bmpData
->m_pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, useAlpha
, 8, w
, h
);
763 if (bmpData
->m_pixmap
)
764 PixmapToPixbuf(bmpData
->m_pixmap
, bmpData
->m_pixbuf
, w
, h
);
766 MaskToAlpha(mask
, bmpData
->m_pixbuf
, w
, h
);
767 return bmpData
->m_pixbuf
;
770 bool wxBitmap::HasPixbuf() const
772 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
774 return M_BMPDATA
->m_pixbuf
!= NULL
;
777 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
)
785 if (gdk_pixbuf_get_has_alpha(pixbuf
))
787 m_refData
= new wxBitmapRefData(
788 gdk_pixbuf_get_width(pixbuf
), gdk_pixbuf_get_height(pixbuf
), depth
);
790 M_BMPDATA
->m_pixbuf
= pixbuf
;
793 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
795 if (keep
== Pixmap
&& HasPixbuf())
797 g_object_unref (M_BMPDATA
->m_pixbuf
);
798 M_BMPDATA
->m_pixbuf
= NULL
;
800 if (keep
== Pixbuf
&& HasPixmap())
802 g_object_unref (M_BMPDATA
->m_pixmap
);
803 M_BMPDATA
->m_pixmap
= NULL
;
807 #ifdef wxHAS_RAW_BITMAP
808 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
811 GdkPixbuf
*pixbuf
= GetPixbuf();
812 const bool hasAlpha
= HasAlpha();
814 // allow access if bpp is valid and matches existence of alpha
815 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (bpp
== 32 && hasAlpha
)) )
817 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
818 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
819 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
820 bits
= gdk_pixbuf_get_pixels(pixbuf
);
825 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
828 #endif // wxHAS_RAW_BITMAP
830 bool wxBitmap::HasAlpha() const
832 const wxBitmapRefData
* bmpData
= M_BMPDATA
;
833 return bmpData
&& (bmpData
->m_alphaRequested
||
834 (bmpData
->m_pixbuf
&& gdk_pixbuf_get_has_alpha(bmpData
->m_pixbuf
)));
837 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
839 return new wxBitmapRefData(0, 0, 0);
842 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
844 const wxBitmapRefData
* oldRef
= static_cast<const wxBitmapRefData
*>(data
);
845 wxBitmapRefData
* newRef
= new wxBitmapRefData(*oldRef
);
846 if (oldRef
->m_pixmap
!= NULL
)
848 newRef
->m_pixmap
= gdk_pixmap_new(
849 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
850 // use pixmap depth, m_bpp may not match
851 gdk_drawable_get_depth(oldRef
->m_pixmap
));
852 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_pixmap
));
854 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
856 if (oldRef
->m_pixbuf
!= NULL
)
858 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
860 if (oldRef
->m_mask
!= NULL
)
862 newRef
->m_mask
= new wxMask
;
863 newRef
->m_mask
->m_bitmap
= gdk_pixmap_new(
864 oldRef
->m_mask
->m_bitmap
, oldRef
->m_width
, oldRef
->m_height
, 1);
865 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_mask
->m_bitmap
));
866 gdk_draw_drawable(newRef
->m_mask
->m_bitmap
,
867 gc
, oldRef
->m_mask
->m_bitmap
, 0, 0, 0, 0, -1, -1);
873 /* static */ void wxBitmap::InitStandardHandlers()
875 // TODO: Insert handler based on GdkPixbufs handler later