]>
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 wxMask
& mask
)
90 // create a copy of an existing mask
92 gdk_drawable_get_size(mask
.m_bitmap
, &w
, &h
);
93 m_bitmap
= gdk_pixmap_new(mask
.m_bitmap
, w
, h
, 1);
95 wxGtkObject
<GdkGC
> gc(gdk_gc_new(m_bitmap
));
96 gdk_draw_drawable(m_bitmap
, gc
, mask
.m_bitmap
, 0, 0, 0, 0, -1, -1);
99 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
102 InitFromColour(bitmap
, colour
);
106 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
109 Create( bitmap
, paletteIndex
);
111 #endif // wxUSE_PALETTE
113 wxMask::wxMask( const wxBitmap
& bitmap
)
116 InitFromMonoBitmap(bitmap
);
122 g_object_unref (m_bitmap
);
125 void wxMask::FreeData()
129 g_object_unref (m_bitmap
);
134 bool wxMask::InitFromColour(const wxBitmap
& bitmap
, const wxColour
& colour
)
136 const int w
= bitmap
.GetWidth();
137 const int h
= bitmap
.GetHeight();
139 // create mask as XBM format bitmap
141 // one bit per pixel, each row starts on a byte boundary
142 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
143 wxByte
* out
= new wxByte
[out_size
];
144 // set bits are unmasked
145 memset(out
, 0xff, out_size
);
146 unsigned bit_index
= 0;
147 if (bitmap
.HasPixbuf())
149 const wxByte r_mask
= colour
.Red();
150 const wxByte g_mask
= colour
.Green();
151 const wxByte b_mask
= colour
.Blue();
152 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
153 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
154 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
155 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
156 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
158 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
159 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
160 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
161 // move index to next byte boundary
162 bit_index
= (bit_index
+ 7) & ~7u;
167 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
168 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
170 if (colormap
== NULL
)
171 // mono bitmap, white is pixel value 0
172 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
176 c
.CalcPixel(colormap
);
177 mask_pixel
= c
.GetPixel();
179 for (int y
= 0; y
< h
; y
++)
181 for (int x
= 0; x
< w
; x
++, bit_index
++)
182 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
183 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
184 bit_index
= (bit_index
+ 7) & ~7u;
186 g_object_unref(image
);
188 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
193 bool wxMask::InitFromMonoBitmap(const wxBitmap
& bitmap
)
195 if (!bitmap
.IsOk()) return false;
197 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
199 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
201 if (!m_bitmap
) return false;
203 wxGtkObject
<GdkGC
> gc(gdk_gc_new( m_bitmap
));
204 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
205 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
210 GdkBitmap
*wxMask::GetBitmap() const
215 //-----------------------------------------------------------------------------
217 //-----------------------------------------------------------------------------
219 class wxBitmapRefData
: public wxGDIRefData
222 wxBitmapRefData(int width
, int height
, int depth
);
223 virtual ~wxBitmapRefData();
225 virtual bool IsOk() const;
233 bool m_alphaRequested
;
236 // We don't provide a copy ctor as copying m_pixmap and m_pixbuf properly
237 // is expensive and we don't want to do it implicitly (and possibly
238 // accidentally). wxBitmap::CloneGDIRefData() which does need to do it does
239 // it explicitly itself.
240 wxDECLARE_NO_COPY_CLASS(wxBitmapRefData
);
243 wxBitmapRefData::wxBitmapRefData(int width
, int height
, int depth
)
252 m_bpp
= gdk_drawable_get_depth(wxGetRootWindow()->window
);
253 m_alphaRequested
= depth
== 32;
256 wxBitmapRefData::~wxBitmapRefData()
259 g_object_unref (m_pixmap
);
261 g_object_unref (m_pixbuf
);
265 bool wxBitmapRefData::IsOk() const
270 //-----------------------------------------------------------------------------
272 //-----------------------------------------------------------------------------
274 #define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
276 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
278 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
280 LoadFile(filename
, type
);
283 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
285 wxASSERT(depth
== 1);
286 if (width
> 0 && height
> 0 && depth
== 1)
288 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
292 wxBitmap::wxBitmap(const char* const* bits
)
294 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
296 GdkBitmap
* mask
= NULL
;
297 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, const_cast<char**>(bits
)));
301 if (M_BMPDATA
->m_pixmap
!= NULL
&& mask
!= NULL
)
303 M_BMPDATA
->m_mask
= new wxMask
;
304 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
308 wxBitmap::~wxBitmap()
312 bool wxBitmap::Create( int width
, int height
, int depth
)
315 wxCHECK_MSG(width
>= 0 && height
>= 0, false, "invalid bitmap size");
316 m_refData
= new wxBitmapRefData(width
, height
, depth
);
322 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
326 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
327 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
329 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
332 // create pixbuf if image has alpha and requested depth is compatible
333 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
334 return CreateFromImageAsPixbuf(image
);
336 // otherwise create pixmap, if alpha is present it will be converted to mask
337 return CreateFromImageAsPixmap(image
, depth
);
340 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
342 const int w
= image
.GetWidth();
343 const int h
= image
.GetHeight();
346 // create XBM format bitmap
348 // one bit per pixel, each row starts on a byte boundary
349 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
350 wxByte
* out
= new wxByte
[out_size
];
351 // set bits are black
352 memset(out
, 0xff, out_size
);
353 const wxByte
* in
= image
.GetData();
354 unsigned bit_index
= 0;
355 for (int y
= 0; y
< h
; y
++)
357 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
358 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
359 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
360 // move index to next byte boundary
361 bit_index
= (bit_index
+ 7) & ~7u;
363 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
366 if (!M_BMPDATA
) // SetPixmap may have failed
371 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
375 wxGtkObject
<GdkGC
> gc(gdk_gc_new(M_BMPDATA
->m_pixmap
));
377 M_BMPDATA
->m_pixmap
, gc
,
379 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
382 const wxByte
* alpha
= image
.GetAlpha();
383 if (alpha
!= NULL
|| image
.HasMask())
385 // create mask as XBM format bitmap
387 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
388 wxByte
* out
= new wxByte
[out_size
];
389 memset(out
, 0xff, out_size
);
390 unsigned bit_index
= 0;
393 for (int y
= 0; y
< h
; y
++)
395 for (int x
= 0; x
< w
; x
++, bit_index
++)
396 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
397 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
398 bit_index
= (bit_index
+ 7) & ~7u;
403 const wxByte r_mask
= image
.GetMaskRed();
404 const wxByte g_mask
= image
.GetMaskGreen();
405 const wxByte b_mask
= image
.GetMaskBlue();
406 const wxByte
* in
= image
.GetData();
407 for (int y
= 0; y
< h
; y
++)
409 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
410 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
411 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
412 bit_index
= (bit_index
+ 7) & ~7u;
415 wxMask
* mask
= new wxMask
;
416 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
423 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
425 wxASSERT(image
.HasAlpha());
427 int width
= image
.GetWidth();
428 int height
= image
.GetHeight();
430 Create(width
, height
, 32);
431 GdkPixbuf
* pixbuf
= GetPixbuf();
436 const unsigned char* in
= image
.GetData();
437 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
438 unsigned char *alpha
= image
.GetAlpha();
440 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
442 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
444 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
456 wxImage
wxBitmap::ConvertToImage() const
458 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
460 const int w
= GetWidth();
461 const int h
= GetHeight();
462 wxImage
image(w
, h
, false);
463 unsigned char *data
= image
.GetData();
465 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
467 // prefer pixbuf if available, it will preserve alpha and should be quicker
470 GdkPixbuf
*pixbuf
= GetPixbuf();
471 unsigned char* alpha
= NULL
;
472 if (gdk_pixbuf_get_has_alpha(pixbuf
))
475 alpha
= image
.GetAlpha();
477 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
478 unsigned char *out
= data
;
479 const int inc
= 3 + int(alpha
!= NULL
);
480 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
482 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
484 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
496 GdkPixmap
* pixmap
= GetPixmap();
497 GdkPixmap
* pixmap_invert
= NULL
;
500 // mono bitmaps are inverted, i.e. 0 is white
501 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
502 wxGtkObject
<GdkGC
> gc(gdk_gc_new(pixmap_invert
));
503 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
504 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
505 pixmap
= pixmap_invert
;
507 // create a pixbuf which shares data with the wxImage
508 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
509 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
511 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
513 g_object_unref(pixbuf
);
514 if (pixmap_invert
!= NULL
)
515 g_object_unref(pixmap_invert
);
517 // convert mask, unless there is already alpha
518 if (GetMask() && !image
.HasAlpha())
520 // we hard code the mask colour for now but we could also make an
521 // effort (and waste time) to choose a colour not present in the
522 // image already to avoid having to fudge the pixels below --
523 // whether it's worth to do it is unclear however
524 const int MASK_RED
= 1;
525 const int MASK_GREEN
= 2;
526 const int MASK_BLUE
= 3;
527 const int MASK_BLUE_REPLACEMENT
= 2;
529 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
530 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
532 for (int y
= 0; y
< h
; y
++)
534 for (int x
= 0; x
< w
; x
++, data
+= 3)
536 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
539 data
[1] = MASK_GREEN
;
542 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
544 // we have to fudge the colour a bit to prevent
545 // this pixel from appearing transparent
546 data
[2] = MASK_BLUE_REPLACEMENT
;
550 g_object_unref(image_mask
);
556 #endif // wxUSE_IMAGE
558 int wxBitmap::GetHeight() const
560 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
562 return M_BMPDATA
->m_height
;
565 int wxBitmap::GetWidth() const
567 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
569 return M_BMPDATA
->m_width
;
572 int wxBitmap::GetDepth() const
574 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
576 return M_BMPDATA
->m_bpp
;
579 wxMask
*wxBitmap::GetMask() const
581 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
583 return M_BMPDATA
->m_mask
;
586 void wxBitmap::SetMask( wxMask
*mask
)
588 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
591 delete M_BMPDATA
->m_mask
;
592 M_BMPDATA
->m_mask
= mask
;
595 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
601 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
605 wxCHECK_MSG(IsOk(), ret
, wxT("invalid bitmap"));
607 const int w
= rect
.width
;
608 const int h
= rect
.height
;
609 const wxBitmapRefData
* bmpData
= M_BMPDATA
;
611 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
612 rect
.x
+ w
<= bmpData
->m_width
&&
613 rect
.y
+ h
<= bmpData
->m_height
,
614 ret
, wxT("invalid bitmap region"));
616 wxBitmapRefData
* const newRef
= new wxBitmapRefData(w
, h
, bmpData
->m_bpp
);
617 ret
.m_refData
= newRef
;
619 if (bmpData
->m_pixbuf
)
622 gdk_pixbuf_new_subpixbuf(bmpData
->m_pixbuf
, rect
.x
, rect
.y
, w
, h
);
623 newRef
->m_pixbuf
= gdk_pixbuf_copy(pixbuf
);
624 g_object_unref(pixbuf
);
626 if (bmpData
->m_pixmap
)
628 newRef
->m_pixmap
= gdk_pixmap_new(bmpData
->m_pixmap
, w
, h
, -1);
629 GdkGC
* gc
= gdk_gc_new(newRef
->m_pixmap
);
631 newRef
->m_pixmap
, gc
, bmpData
->m_pixmap
, rect
.x
, rect
.y
, 0, 0, w
, h
);
634 if (bmpData
->m_mask
&& bmpData
->m_mask
->m_bitmap
)
636 GdkPixmap
* sub_mask
= gdk_pixmap_new(bmpData
->m_mask
->m_bitmap
, w
, h
, 1);
637 newRef
->m_mask
= new wxMask
;
638 newRef
->m_mask
->m_bitmap
= sub_mask
;
639 GdkGC
* gc
= gdk_gc_new(sub_mask
);
641 sub_mask
, gc
, bmpData
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, w
, h
);
648 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
650 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
653 wxImage image
= ConvertToImage();
654 if (image
.IsOk() && image
.SaveFile(name
, type
))
657 const char* type_name
= NULL
;
660 case wxBITMAP_TYPE_BMP
: type_name
= "bmp"; break;
661 case wxBITMAP_TYPE_ICO
: type_name
= "ico"; break;
662 case wxBITMAP_TYPE_JPEG
: type_name
= "jpeg"; break;
663 case wxBITMAP_TYPE_PNG
: type_name
= "png"; break;
667 gdk_pixbuf_save(GetPixbuf(), name
.fn_str(), type_name
, NULL
, NULL
);
670 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
674 if (image
.LoadFile(name
, type
) && image
.IsOk())
675 *this = wxBitmap(image
);
679 wxUnusedVar(type
); // The type is detected automatically by GDK.
682 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_file(name
.fn_str(), NULL
);
691 wxPalette
*wxBitmap::GetPalette() const
696 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
700 #endif // wxUSE_PALETTE
702 void wxBitmap::SetHeight( int height
)
705 M_BMPDATA
->m_height
= height
;
708 void wxBitmap::SetWidth( int width
)
711 M_BMPDATA
->m_width
= width
;
714 void wxBitmap::SetDepth( int depth
)
717 M_BMPDATA
->m_bpp
= depth
;
720 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
728 gdk_drawable_get_size(pixmap
, &w
, &h
);
729 wxBitmapRefData
* bmpData
= new wxBitmapRefData(w
, h
, 0);
731 bmpData
->m_pixmap
= pixmap
;
732 bmpData
->m_bpp
= gdk_drawable_get_depth(pixmap
);
735 GdkPixmap
*wxBitmap::GetPixmap() const
737 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
739 wxBitmapRefData
* bmpData
= M_BMPDATA
;
740 if (bmpData
->m_pixmap
)
741 return bmpData
->m_pixmap
;
743 if (bmpData
->m_pixbuf
)
745 GdkPixmap
** mask_pixmap
= NULL
;
746 if (gdk_pixbuf_get_has_alpha(bmpData
->m_pixbuf
))
748 // make new mask from alpha
749 delete bmpData
->m_mask
;
750 bmpData
->m_mask
= new wxMask
;
751 mask_pixmap
= &bmpData
->m_mask
->m_bitmap
;
753 gdk_pixbuf_render_pixmap_and_mask(
754 bmpData
->m_pixbuf
, &bmpData
->m_pixmap
, mask_pixmap
, 128);
758 bmpData
->m_pixmap
= gdk_pixmap_new(wxGetRootWindow()->window
,
759 bmpData
->m_width
, bmpData
->m_height
, bmpData
->m_bpp
== 1 ? 1 : -1);
761 return bmpData
->m_pixmap
;
764 bool wxBitmap::HasPixmap() const
766 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
768 return M_BMPDATA
->m_pixmap
!= NULL
;
771 GdkPixbuf
*wxBitmap::GetPixbuf() const
773 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
775 wxBitmapRefData
* bmpData
= M_BMPDATA
;
776 if (bmpData
->m_pixbuf
)
777 return bmpData
->m_pixbuf
;
779 const int w
= bmpData
->m_width
;
780 const int h
= bmpData
->m_height
;
781 GdkPixmap
* mask
= NULL
;
783 mask
= bmpData
->m_mask
->m_bitmap
;
784 const bool useAlpha
= bmpData
->m_alphaRequested
|| mask
;
785 bmpData
->m_pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, useAlpha
, 8, w
, h
);
786 if (bmpData
->m_pixmap
)
787 PixmapToPixbuf(bmpData
->m_pixmap
, bmpData
->m_pixbuf
, w
, h
);
789 MaskToAlpha(mask
, bmpData
->m_pixbuf
, w
, h
);
790 return bmpData
->m_pixbuf
;
793 bool wxBitmap::HasPixbuf() const
795 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
797 return M_BMPDATA
->m_pixbuf
!= NULL
;
800 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
)
808 if (gdk_pixbuf_get_has_alpha(pixbuf
))
810 m_refData
= new wxBitmapRefData(
811 gdk_pixbuf_get_width(pixbuf
), gdk_pixbuf_get_height(pixbuf
), depth
);
813 M_BMPDATA
->m_pixbuf
= pixbuf
;
816 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
818 if (keep
== Pixmap
&& HasPixbuf())
820 g_object_unref (M_BMPDATA
->m_pixbuf
);
821 M_BMPDATA
->m_pixbuf
= NULL
;
823 if (keep
== Pixbuf
&& HasPixmap())
825 g_object_unref (M_BMPDATA
->m_pixmap
);
826 M_BMPDATA
->m_pixmap
= NULL
;
830 #ifdef wxHAS_RAW_BITMAP
831 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
834 GdkPixbuf
*pixbuf
= GetPixbuf();
835 const bool hasAlpha
= HasAlpha();
837 // allow access if bpp is valid and matches existence of alpha
838 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (bpp
== 32 && hasAlpha
)) )
840 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
841 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
842 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
843 bits
= gdk_pixbuf_get_pixels(pixbuf
);
848 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
851 #endif // wxHAS_RAW_BITMAP
853 bool wxBitmap::HasAlpha() const
855 const wxBitmapRefData
* bmpData
= M_BMPDATA
;
856 return bmpData
&& (bmpData
->m_alphaRequested
||
857 (bmpData
->m_pixbuf
&& gdk_pixbuf_get_has_alpha(bmpData
->m_pixbuf
)));
860 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
862 return new wxBitmapRefData(0, 0, 0);
865 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
867 const wxBitmapRefData
* oldRef
= static_cast<const wxBitmapRefData
*>(data
);
868 wxBitmapRefData
* const newRef
= new wxBitmapRefData(oldRef
->m_width
,
871 if (oldRef
->m_pixmap
!= NULL
)
873 newRef
->m_pixmap
= gdk_pixmap_new(
874 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
875 // use pixmap depth, m_bpp may not match
876 gdk_drawable_get_depth(oldRef
->m_pixmap
));
877 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_pixmap
));
879 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
881 if (oldRef
->m_pixbuf
!= NULL
)
883 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
885 if (oldRef
->m_mask
!= NULL
)
887 newRef
->m_mask
= new wxMask(*oldRef
->m_mask
);
893 /* static */ void wxBitmap::InitStandardHandlers()
895 // TODO: Insert handler based on GdkPixbufs handler later