]>
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"
20 #include "wx/colour.h"
23 #include "wx/rawbmp.h"
25 #include "wx/gtk/private/object.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern GtkWidget
*wxGetRootWindow();
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxMaskBase
)
46 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
49 InitFromColour(bitmap
, colour
);
53 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
56 Create( bitmap
, paletteIndex
);
58 #endif // wxUSE_PALETTE
60 wxMask::wxMask( const wxBitmap
& bitmap
)
63 InitFromMonoBitmap(bitmap
);
69 g_object_unref (m_bitmap
);
72 void wxMask::FreeData()
76 g_object_unref (m_bitmap
);
81 bool wxMask::InitFromColour(const wxBitmap
& bitmap
, const wxColour
& colour
)
83 const int w
= bitmap
.GetWidth();
84 const int h
= bitmap
.GetHeight();
86 // create mask as XBM format bitmap
88 // one bit per pixel, each row starts on a byte boundary
89 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
90 wxByte
* out
= new wxByte
[out_size
];
91 // set bits are unmasked
92 memset(out
, 0xff, out_size
);
93 unsigned bit_index
= 0;
94 if (bitmap
.HasPixbuf())
96 const wxByte r_mask
= colour
.Red();
97 const wxByte g_mask
= colour
.Green();
98 const wxByte b_mask
= colour
.Blue();
99 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
100 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
101 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
102 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
103 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
105 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
106 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
107 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
108 // move index to next byte boundary
109 bit_index
= (bit_index
+ 7) & ~7u;
114 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
115 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
117 if (colormap
== NULL
)
118 // mono bitmap, white is pixel value 0
119 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
123 c
.CalcPixel(colormap
);
124 mask_pixel
= c
.GetPixel();
126 for (int y
= 0; y
< h
; y
++)
128 for (int x
= 0; x
< w
; x
++, bit_index
++)
129 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
130 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
131 bit_index
= (bit_index
+ 7) & ~7u;
133 g_object_unref(image
);
135 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
140 bool wxMask::InitFromMonoBitmap(const wxBitmap
& bitmap
)
142 if (!bitmap
.IsOk()) return false;
144 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
146 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
148 if (!m_bitmap
) return false;
150 wxGtkObject
<GdkGC
> gc(gdk_gc_new( m_bitmap
));
151 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
152 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
157 GdkBitmap
*wxMask::GetBitmap() const
162 //-----------------------------------------------------------------------------
164 //-----------------------------------------------------------------------------
166 class wxBitmapRefData
: public wxGDIRefData
170 virtual ~wxBitmapRefData();
172 virtual bool IsOk() const { return m_pixmap
|| m_pixbuf
; }
182 wxBitmapRefData::wxBitmapRefData()
192 wxBitmapRefData::~wxBitmapRefData()
195 g_object_unref (m_pixmap
);
197 g_object_unref (m_pixbuf
);
202 //-----------------------------------------------------------------------------
204 //-----------------------------------------------------------------------------
206 #define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
208 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
210 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
212 LoadFile(filename
, type
);
215 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
217 wxASSERT(depth
== 1);
218 if (width
> 0 && height
> 0 && depth
== 1)
220 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
224 wxBitmap::wxBitmap(const char* const* bits
)
226 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
228 GdkBitmap
* mask
= NULL
;
229 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, const_cast<char**>(bits
)));
233 if (M_BMPDATA
->m_pixmap
!= NULL
&& mask
!= NULL
)
235 M_BMPDATA
->m_mask
= new wxMask
;
236 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
240 wxBitmap::~wxBitmap()
244 bool wxBitmap::Create( int width
, int height
, int depth
)
248 if ( width
<= 0 || height
<= 0 )
253 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
257 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
), 32);
262 // must initialize alpha, otherwise GetPixmap()
263 // will create a mask out of garbage
264 gdk_pixbuf_fill(M_BMPDATA
->m_pixbuf
, 0x000000ff);
266 else if (depth
== 24)
268 if (visual
->depth
== depth
)
269 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
271 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8, width
, height
), 24);
278 depth
= visual
->depth
;
280 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
288 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
292 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
293 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
295 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
298 // create pixbuf if image has alpha and requested depth is compatible
299 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
300 return CreateFromImageAsPixbuf(image
);
302 // otherwise create pixmap, if alpha is present it will be converted to mask
303 return CreateFromImageAsPixmap(image
, depth
);
306 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
308 const int w
= image
.GetWidth();
309 const int h
= image
.GetHeight();
312 // create XBM format bitmap
314 // one bit per pixel, each row starts on a byte boundary
315 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
316 wxByte
* out
= new wxByte
[out_size
];
317 // set bits are black
318 memset(out
, 0xff, out_size
);
319 const wxByte
* in
= image
.GetData();
320 unsigned bit_index
= 0;
321 for (int y
= 0; y
< h
; y
++)
323 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
324 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
325 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
326 // move index to next byte boundary
327 bit_index
= (bit_index
+ 7) & ~7u;
329 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
332 if (!M_BMPDATA
) // SetPixmap may have failed
337 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
341 wxGtkObject
<GdkGC
> gc(gdk_gc_new(M_BMPDATA
->m_pixmap
));
343 M_BMPDATA
->m_pixmap
, gc
,
345 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
348 const wxByte
* alpha
= image
.GetAlpha();
349 if (alpha
!= NULL
|| image
.HasMask())
351 // create mask as XBM format bitmap
353 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
354 wxByte
* out
= new wxByte
[out_size
];
355 memset(out
, 0xff, out_size
);
356 unsigned bit_index
= 0;
359 for (int y
= 0; y
< h
; y
++)
361 for (int x
= 0; x
< w
; x
++, bit_index
++)
362 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
363 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
364 bit_index
= (bit_index
+ 7) & ~7u;
369 const wxByte r_mask
= image
.GetMaskRed();
370 const wxByte g_mask
= image
.GetMaskGreen();
371 const wxByte b_mask
= image
.GetMaskBlue();
372 const wxByte
* in
= image
.GetData();
373 for (int y
= 0; y
< h
; y
++)
375 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
376 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
377 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
378 bit_index
= (bit_index
+ 7) & ~7u;
381 wxMask
* mask
= new wxMask
;
382 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
389 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
391 wxASSERT(image
.HasAlpha());
393 int width
= image
.GetWidth();
394 int height
= image
.GetHeight();
396 Create(width
, height
, 32);
397 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
402 const unsigned char* in
= image
.GetData();
403 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
404 unsigned char *alpha
= image
.GetAlpha();
406 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
408 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
410 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
422 wxImage
wxBitmap::ConvertToImage() const
424 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
426 const int w
= GetWidth();
427 const int h
= GetHeight();
428 wxImage
image(w
, h
, false);
429 unsigned char *data
= image
.GetData();
431 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
433 // prefer pixbuf if available, it will preserve alpha and should be quicker
436 GdkPixbuf
*pixbuf
= GetPixbuf();
437 unsigned char* alpha
= NULL
;
438 if (gdk_pixbuf_get_has_alpha(pixbuf
))
441 alpha
= image
.GetAlpha();
443 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
444 unsigned char *out
= data
;
445 const int inc
= 3 + int(alpha
!= NULL
);
446 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
448 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
450 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
462 GdkPixmap
* pixmap
= GetPixmap();
463 GdkPixmap
* pixmap_invert
= NULL
;
466 // mono bitmaps are inverted, i.e. 0 is white
467 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
468 wxGtkObject
<GdkGC
> gc(gdk_gc_new(pixmap_invert
));
469 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
470 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
471 pixmap
= pixmap_invert
;
473 // create a pixbuf which shares data with the wxImage
474 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
475 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
477 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
479 g_object_unref(pixbuf
);
480 if (pixmap_invert
!= NULL
)
481 g_object_unref(pixmap_invert
);
483 // convert mask, unless there is already alpha
484 if (GetMask() && !image
.HasAlpha())
486 // we hard code the mask colour for now but we could also make an
487 // effort (and waste time) to choose a colour not present in the
488 // image already to avoid having to fudge the pixels below --
489 // whether it's worth to do it is unclear however
490 const int MASK_RED
= 1;
491 const int MASK_GREEN
= 2;
492 const int MASK_BLUE
= 3;
493 const int MASK_BLUE_REPLACEMENT
= 2;
495 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
496 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
498 for (int y
= 0; y
< h
; y
++)
500 for (int x
= 0; x
< w
; x
++, data
+= 3)
502 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
505 data
[1] = MASK_GREEN
;
508 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
510 // we have to fudge the colour a bit to prevent
511 // this pixel from appearing transparent
512 data
[2] = MASK_BLUE_REPLACEMENT
;
516 g_object_unref(image_mask
);
522 #endif // wxUSE_IMAGE
524 int wxBitmap::GetHeight() const
526 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
528 return M_BMPDATA
->m_height
;
531 int wxBitmap::GetWidth() const
533 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
535 return M_BMPDATA
->m_width
;
538 int wxBitmap::GetDepth() const
540 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
542 return M_BMPDATA
->m_bpp
;
545 wxMask
*wxBitmap::GetMask() const
547 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
549 return M_BMPDATA
->m_mask
;
552 void wxBitmap::SetMask( wxMask
*mask
)
554 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
557 delete M_BMPDATA
->m_mask
;
558 M_BMPDATA
->m_mask
= mask
;
561 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
567 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
571 wxCHECK_MSG(IsOk(), ret
, wxT("invalid bitmap"));
572 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
573 rect
.x
+ rect
.width
<= M_BMPDATA
->m_width
&&
574 rect
.y
+ rect
.height
<= M_BMPDATA
->m_height
,
575 ret
, wxT("invalid bitmap region"));
577 if (HasPixbuf() || M_BMPDATA
->m_bpp
== 32)
579 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
580 gdk_pixbuf_get_has_alpha(GetPixbuf()),
581 8, rect
.width
, rect
.height
);
585 ret
.SetPixbuf(pixbuf
, M_BMPDATA
->m_bpp
);
586 gdk_pixbuf_copy_area(GetPixbuf(),
587 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
592 ret
.Create(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
593 wxGtkObject
<GdkGC
> gc(gdk_gc_new( ret
.GetPixmap() ));
594 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
596 // make mask, unless there is already alpha
597 if (GetMask() && !HasAlpha())
599 wxMask
*mask
= new wxMask
;
600 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
602 wxGtkObject
<GdkGC
> gc(gdk_gc_new( mask
->m_bitmap
));
603 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
611 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
613 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
616 // Try to save the bitmap via wxImage handlers:
617 wxImage image
= ConvertToImage();
618 return image
.Ok() && image
.SaveFile(name
, type
);
619 #else // !wxUSE_IMAGE
624 #endif // wxUSE_IMAGE
627 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
631 if (type
== wxBITMAP_TYPE_XPM
)
633 GdkBitmap
*mask
= NULL
;
634 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
636 return false; // do not set the mask
640 M_BMPDATA
->m_mask
= new wxMask
;
641 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
645 else // try if wxImage can load it
648 if (image
.LoadFile(name
, type
) && image
.Ok())
649 CreateFromImage(image
, -1);
651 #endif // wxUSE_IMAGE
657 wxPalette
*wxBitmap::GetPalette() const
662 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
666 #endif // wxUSE_PALETTE
668 void wxBitmap::SetHeight( int height
)
671 M_BMPDATA
->m_height
= height
;
674 void wxBitmap::SetWidth( int width
)
677 M_BMPDATA
->m_width
= width
;
680 void wxBitmap::SetDepth( int depth
)
683 M_BMPDATA
->m_bpp
= depth
;
686 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
692 m_refData
= new wxBitmapRefData
;
694 // AllocExclusive should not be needed for this internal function
695 wxASSERT(m_refData
->GetRefCount() == 1);
696 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
697 M_BMPDATA
->m_pixmap
= pixmap
;
698 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
699 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
700 PurgeOtherRepresentations(Pixmap
);
703 GdkPixmap
*wxBitmap::GetPixmap() const
705 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
707 // create the pixmap on the fly if we use Pixbuf representation:
708 if (M_BMPDATA
->m_pixmap
== NULL
)
710 GdkPixmap
** pmask
= NULL
;
711 if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
))
713 // make new mask from alpha
714 delete M_BMPDATA
->m_mask
;
715 M_BMPDATA
->m_mask
= new wxMask
;
716 pmask
= &M_BMPDATA
->m_mask
->m_bitmap
;
718 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
719 &M_BMPDATA
->m_pixmap
,
721 0x80 /* alpha threshold */);
724 return M_BMPDATA
->m_pixmap
;
727 bool wxBitmap::HasPixmap() const
729 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
731 return M_BMPDATA
->m_pixmap
!= NULL
;
734 GdkPixbuf
*wxBitmap::GetPixbuf() const
736 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
738 if (M_BMPDATA
->m_pixbuf
== NULL
)
741 int width
= GetWidth();
742 int height
= GetHeight();
744 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
747 M_BMPDATA
->m_pixbuf
= pixbuf
;
748 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
749 0, 0, 0, 0, width
, height
);
750 // apply the mask to created pixbuf:
751 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
754 gdk_pixbuf_get_from_drawable(NULL
,
755 M_BMPDATA
->m_mask
->GetBitmap(),
757 0, 0, 0, 0, width
, height
);
760 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
761 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
762 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
763 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
765 for (int y
= 0; y
< height
;
766 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
768 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
770 if (mask
[0] == 0 /*black pixel*/)
775 g_object_unref (pmask
);
780 return M_BMPDATA
->m_pixbuf
;
783 bool wxBitmap::HasPixbuf() const
785 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
787 return M_BMPDATA
->m_pixbuf
!= NULL
;
790 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
, int depth
)
796 m_refData
= new wxBitmapRefData
;
798 // AllocExclusive should not be needed for this internal function
799 wxASSERT(m_refData
->GetRefCount() == 1);
800 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
801 M_BMPDATA
->m_pixbuf
= pixbuf
;
802 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
803 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
804 // if depth specified
806 M_BMPDATA
->m_bpp
= depth
;
807 else if (M_BMPDATA
->m_bpp
== 0)
808 // use something reasonable
809 M_BMPDATA
->m_bpp
= wxTheApp
->GetGdkVisual()->depth
;
810 PurgeOtherRepresentations(Pixbuf
);
813 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
815 if (keep
== Pixmap
&& HasPixbuf())
817 g_object_unref (M_BMPDATA
->m_pixbuf
);
818 M_BMPDATA
->m_pixbuf
= NULL
;
820 if (keep
== Pixbuf
&& HasPixmap())
822 g_object_unref (M_BMPDATA
->m_pixmap
);
823 M_BMPDATA
->m_pixmap
= NULL
;
827 #ifdef wxHAS_RAW_BITMAP
828 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
831 GdkPixbuf
*pixbuf
= GetPixbuf();
832 const bool hasAlpha
= HasAlpha();
834 // allow access if bpp is valid and matches existence of alpha
835 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (bpp
== 32 && hasAlpha
)) )
837 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
838 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
839 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
840 bits
= gdk_pixbuf_get_pixels(pixbuf
);
845 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
848 #endif // wxHAS_RAW_BITMAP
850 bool wxBitmap::HasAlpha() const
852 return m_refData
!= NULL
&& M_BMPDATA
->m_pixbuf
!= NULL
&&
853 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
);
856 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
858 return new wxBitmapRefData
;
861 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
863 const wxBitmapRefData
* oldRef
= static_cast<const wxBitmapRefData
*>(data
);
864 wxBitmapRefData
* newRef
= new wxBitmapRefData
;
865 newRef
->m_width
= oldRef
->m_width
;
866 newRef
->m_height
= oldRef
->m_height
;
867 newRef
->m_bpp
= oldRef
->m_bpp
;
868 if (oldRef
->m_pixmap
!= NULL
)
870 newRef
->m_pixmap
= gdk_pixmap_new(
871 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
872 // use pixmap depth, m_bpp may not match
873 gdk_drawable_get_depth(oldRef
->m_pixmap
));
874 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_pixmap
));
876 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
878 if (oldRef
->m_pixbuf
!= NULL
)
880 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
882 if (oldRef
->m_mask
!= NULL
)
884 newRef
->m_mask
= new wxMask
;
885 newRef
->m_mask
->m_bitmap
= gdk_pixmap_new(
886 oldRef
->m_mask
->m_bitmap
, oldRef
->m_width
, oldRef
->m_height
, 1);
887 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_mask
->m_bitmap
));
888 gdk_draw_drawable(newRef
->m_mask
->m_bitmap
,
889 gc
, oldRef
->m_mask
->m_bitmap
, 0, 0, 0, 0, -1, -1);
895 /* static */ void wxBitmap::InitStandardHandlers()
897 // TODO: Insert handler based on GdkPixbufs handler later