]>
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"
17 #include "wx/palette.h"
21 #include "wx/colour.h"
24 #include "wx/rawbmp.h"
26 #include "wx/gtk/private/object.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern GtkWidget
*wxGetRootWindow();
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxMaskBase
)
47 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
50 InitFromColour(bitmap
, colour
);
54 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
57 Create( bitmap
, paletteIndex
);
59 #endif // wxUSE_PALETTE
61 wxMask::wxMask( const wxBitmap
& bitmap
)
64 InitFromMonoBitmap(bitmap
);
70 g_object_unref (m_bitmap
);
73 void wxMask::FreeData()
77 g_object_unref (m_bitmap
);
82 bool wxMask::InitFromColour(const wxBitmap
& bitmap
, const wxColour
& colour
)
84 const int w
= bitmap
.GetWidth();
85 const int h
= bitmap
.GetHeight();
87 // create mask as XBM format bitmap
89 // one bit per pixel, each row starts on a byte boundary
90 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
91 wxByte
* out
= new wxByte
[out_size
];
92 // set bits are unmasked
93 memset(out
, 0xff, out_size
);
94 unsigned bit_index
= 0;
95 if (bitmap
.HasPixbuf())
97 const wxByte r_mask
= colour
.Red();
98 const wxByte g_mask
= colour
.Green();
99 const wxByte b_mask
= colour
.Blue();
100 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
101 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
102 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
103 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
104 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
106 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
107 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
108 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
109 // move index to next byte boundary
110 bit_index
= (bit_index
+ 7) & ~7u;
115 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
116 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
118 if (colormap
== NULL
)
119 // mono bitmap, white is pixel value 0
120 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
124 c
.CalcPixel(colormap
);
125 mask_pixel
= c
.GetPixel();
127 for (int y
= 0; y
< h
; y
++)
129 for (int x
= 0; x
< w
; x
++, bit_index
++)
130 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
131 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
132 bit_index
= (bit_index
+ 7) & ~7u;
134 g_object_unref(image
);
136 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
141 bool wxMask::InitFromMonoBitmap(const wxBitmap
& bitmap
)
143 if (!bitmap
.IsOk()) return false;
145 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
147 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
149 if (!m_bitmap
) return false;
151 wxGtkObject
<GdkGC
> gc(gdk_gc_new( m_bitmap
));
152 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
153 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
158 GdkBitmap
*wxMask::GetBitmap() const
163 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
167 class wxBitmapRefData
: public wxGDIRefData
171 virtual ~wxBitmapRefData();
173 virtual bool IsOk() const { return m_pixmap
|| m_pixbuf
; }
182 wxPalette
*m_palette
;
183 #endif // wxUSE_PALETTE
186 wxBitmapRefData::wxBitmapRefData()
196 #endif // wxUSE_PALETTE
199 wxBitmapRefData::~wxBitmapRefData()
202 g_object_unref (m_pixmap
);
204 g_object_unref (m_pixbuf
);
208 #endif // wxUSE_PALETTE
212 //-----------------------------------------------------------------------------
214 //-----------------------------------------------------------------------------
216 #define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
218 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
220 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
222 LoadFile(filename
, type
);
225 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
227 wxASSERT(depth
== 1);
228 if (width
> 0 && height
> 0 && depth
== 1)
230 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
234 wxBitmap::wxBitmap(const char* const* bits
)
236 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
238 GdkBitmap
* mask
= NULL
;
239 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, const_cast<char**>(bits
)));
243 if (M_BMPDATA
->m_pixmap
!= NULL
&& mask
!= NULL
)
245 M_BMPDATA
->m_mask
= new wxMask
;
246 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
250 wxBitmap::~wxBitmap()
254 bool wxBitmap::Create( int width
, int height
, int depth
)
258 if ( width
<= 0 || height
<= 0 )
263 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
267 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
), 32);
272 // must initialize alpha, otherwise GetPixmap()
273 // will create a mask out of garbage
274 gdk_pixbuf_fill(M_BMPDATA
->m_pixbuf
, 0x000000ff);
276 else if (depth
== 24)
278 if (visual
->depth
== depth
)
279 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
281 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8, width
, height
), 24);
288 depth
= visual
->depth
;
290 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, 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
= M_BMPDATA
->m_pixbuf
;
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"));
582 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
583 rect
.x
+ rect
.width
<= M_BMPDATA
->m_width
&&
584 rect
.y
+ rect
.height
<= M_BMPDATA
->m_height
,
585 ret
, wxT("invalid bitmap region"));
587 if (HasPixbuf() || M_BMPDATA
->m_bpp
== 32)
589 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
590 gdk_pixbuf_get_has_alpha(GetPixbuf()),
591 8, rect
.width
, rect
.height
);
595 ret
.SetPixbuf(pixbuf
, M_BMPDATA
->m_bpp
);
596 gdk_pixbuf_copy_area(GetPixbuf(),
597 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
602 ret
.Create(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
603 wxGtkObject
<GdkGC
> gc(gdk_gc_new( ret
.GetPixmap() ));
604 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
606 // make mask, unless there is already alpha
607 if (GetMask() && !HasAlpha())
609 wxMask
*mask
= new wxMask
;
610 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
612 wxGtkObject
<GdkGC
> gc(gdk_gc_new( mask
->m_bitmap
));
613 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
621 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
623 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
626 // Try to save the bitmap via wxImage handlers:
627 wxImage image
= ConvertToImage();
628 return image
.Ok() && image
.SaveFile(name
, type
);
629 #else // !wxUSE_IMAGE
634 #endif // wxUSE_IMAGE
637 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
641 if (type
== wxBITMAP_TYPE_XPM
)
643 GdkBitmap
*mask
= NULL
;
644 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
646 return false; // do not set the mask
650 M_BMPDATA
->m_mask
= new wxMask
;
651 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
655 else // try if wxImage can load it
658 if (image
.LoadFile(name
, type
) && image
.Ok())
659 CreateFromImage(image
, -1);
661 #endif // wxUSE_IMAGE
667 wxPalette
*wxBitmap::GetPalette() const
669 wxCHECK_MSG(IsOk(), NULL
, wxT("invalid bitmap"));
671 return M_BMPDATA
->m_palette
;
674 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
678 #endif // wxUSE_PALETTE
680 void wxBitmap::SetHeight( int height
)
683 M_BMPDATA
->m_height
= height
;
686 void wxBitmap::SetWidth( int width
)
689 M_BMPDATA
->m_width
= width
;
692 void wxBitmap::SetDepth( int depth
)
695 M_BMPDATA
->m_bpp
= depth
;
698 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
704 m_refData
= new wxBitmapRefData
;
706 // AllocExclusive should not be needed for this internal function
707 wxASSERT(m_refData
->GetRefCount() == 1);
708 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
709 M_BMPDATA
->m_pixmap
= pixmap
;
710 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
711 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
712 PurgeOtherRepresentations(Pixmap
);
715 GdkPixmap
*wxBitmap::GetPixmap() const
717 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
719 // create the pixmap on the fly if we use Pixbuf representation:
720 if (M_BMPDATA
->m_pixmap
== NULL
)
722 GdkPixmap
** pmask
= NULL
;
723 if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
))
725 // make new mask from alpha
726 delete M_BMPDATA
->m_mask
;
727 M_BMPDATA
->m_mask
= new wxMask
;
728 pmask
= &M_BMPDATA
->m_mask
->m_bitmap
;
730 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
731 &M_BMPDATA
->m_pixmap
,
733 0x80 /* alpha threshold */);
736 return M_BMPDATA
->m_pixmap
;
739 bool wxBitmap::HasPixmap() const
741 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
743 return M_BMPDATA
->m_pixmap
!= NULL
;
746 GdkPixbuf
*wxBitmap::GetPixbuf() const
748 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
750 if (M_BMPDATA
->m_pixbuf
== NULL
)
753 int width
= GetWidth();
754 int height
= GetHeight();
756 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
759 M_BMPDATA
->m_pixbuf
= pixbuf
;
760 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
761 0, 0, 0, 0, width
, height
);
762 // apply the mask to created pixbuf:
763 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
766 gdk_pixbuf_get_from_drawable(NULL
,
767 M_BMPDATA
->m_mask
->GetBitmap(),
769 0, 0, 0, 0, width
, height
);
772 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
773 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
774 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
775 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
777 for (int y
= 0; y
< height
;
778 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
780 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
782 if (mask
[0] == 0 /*black pixel*/)
787 g_object_unref (pmask
);
792 return M_BMPDATA
->m_pixbuf
;
795 bool wxBitmap::HasPixbuf() const
797 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
799 return M_BMPDATA
->m_pixbuf
!= NULL
;
802 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
, int depth
)
808 m_refData
= new wxBitmapRefData
;
810 // AllocExclusive should not be needed for this internal function
811 wxASSERT(m_refData
->GetRefCount() == 1);
812 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
813 M_BMPDATA
->m_pixbuf
= pixbuf
;
814 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
815 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
816 // if depth specified
818 M_BMPDATA
->m_bpp
= depth
;
819 else if (M_BMPDATA
->m_bpp
== 0)
820 // use something reasonable
821 M_BMPDATA
->m_bpp
= wxTheApp
->GetGdkVisual()->depth
;
822 PurgeOtherRepresentations(Pixbuf
);
825 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
827 if (keep
== Pixmap
&& HasPixbuf())
829 g_object_unref (M_BMPDATA
->m_pixbuf
);
830 M_BMPDATA
->m_pixbuf
= NULL
;
832 if (keep
== Pixbuf
&& HasPixmap())
834 g_object_unref (M_BMPDATA
->m_pixmap
);
835 M_BMPDATA
->m_pixmap
= NULL
;
839 #ifdef wxHAS_RAW_BITMAP
840 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
843 GdkPixbuf
*pixbuf
= GetPixbuf();
844 const bool hasAlpha
= HasAlpha();
846 // allow access if bpp is valid and matches existence of alpha
847 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (bpp
== 32 && hasAlpha
)) )
849 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
850 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
851 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
852 bits
= gdk_pixbuf_get_pixels(pixbuf
);
857 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
860 #endif // wxHAS_RAW_BITMAP
862 bool wxBitmap::HasAlpha() const
864 return m_refData
!= NULL
&& M_BMPDATA
->m_pixbuf
!= NULL
&&
865 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
);
868 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
870 return new wxBitmapRefData
;
873 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
875 const wxBitmapRefData
* oldRef
= static_cast<const wxBitmapRefData
*>(data
);
876 wxBitmapRefData
* newRef
= new wxBitmapRefData
;
877 newRef
->m_width
= oldRef
->m_width
;
878 newRef
->m_height
= oldRef
->m_height
;
879 newRef
->m_bpp
= oldRef
->m_bpp
;
880 if (oldRef
->m_pixmap
!= NULL
)
882 newRef
->m_pixmap
= gdk_pixmap_new(
883 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
884 // use pixmap depth, m_bpp may not match
885 gdk_drawable_get_depth(oldRef
->m_pixmap
));
886 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_pixmap
));
888 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
890 if (oldRef
->m_pixbuf
!= NULL
)
892 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
894 if (oldRef
->m_mask
!= NULL
)
896 newRef
->m_mask
= new wxMask
;
897 newRef
->m_mask
->m_bitmap
= gdk_pixmap_new(
898 oldRef
->m_mask
->m_bitmap
, oldRef
->m_width
, oldRef
->m_height
, 1);
899 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_mask
->m_bitmap
));
900 gdk_draw_drawable(newRef
->m_mask
->m_bitmap
,
901 gc
, oldRef
->m_mask
->m_bitmap
, 0, 0, 0, 0, -1, -1);
904 // implement this if SetPalette is ever implemented
905 wxASSERT(oldRef
->m_palette
== NULL
);
911 /* static */ void wxBitmap::InitStandardHandlers()
913 // TODO: Insert handler based on GdkPixbufs handler later