]>
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
);
119 wxMask::wxMask(GdkPixmap
* bitmap
)
127 g_object_unref (m_bitmap
);
130 void wxMask::FreeData()
134 g_object_unref (m_bitmap
);
139 bool wxMask::InitFromColour(const wxBitmap
& bitmap
, const wxColour
& colour
)
141 const int w
= bitmap
.GetWidth();
142 const int h
= bitmap
.GetHeight();
144 // create mask as XBM format bitmap
146 // one bit per pixel, each row starts on a byte boundary
147 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
148 wxByte
* out
= new wxByte
[out_size
];
149 // set bits are unmasked
150 memset(out
, 0xff, out_size
);
151 unsigned bit_index
= 0;
152 if (bitmap
.HasPixbuf())
154 const wxByte r_mask
= colour
.Red();
155 const wxByte g_mask
= colour
.Green();
156 const wxByte b_mask
= colour
.Blue();
157 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
158 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
159 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
160 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
161 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
163 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
164 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
165 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
166 // move index to next byte boundary
167 bit_index
= (bit_index
+ 7) & ~7u;
172 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
173 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
175 if (colormap
== NULL
)
176 // mono bitmap, white is pixel value 0
177 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
181 c
.CalcPixel(colormap
);
182 mask_pixel
= c
.GetPixel();
184 for (int y
= 0; y
< h
; y
++)
186 for (int x
= 0; x
< w
; x
++, bit_index
++)
187 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
188 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
189 bit_index
= (bit_index
+ 7) & ~7u;
191 g_object_unref(image
);
193 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
198 bool wxMask::InitFromMonoBitmap(const wxBitmap
& bitmap
)
200 if (!bitmap
.IsOk()) return false;
202 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
204 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
206 if (!m_bitmap
) return false;
208 wxGtkObject
<GdkGC
> gc(gdk_gc_new( m_bitmap
));
209 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
210 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
215 GdkPixmap
* wxMask::GetBitmap() const
220 //-----------------------------------------------------------------------------
222 //-----------------------------------------------------------------------------
224 class wxBitmapRefData
: public wxGDIRefData
227 wxBitmapRefData(int width
, int height
, int depth
);
228 virtual ~wxBitmapRefData();
230 virtual bool IsOk() const;
238 bool m_alphaRequested
;
241 // We don't provide a copy ctor as copying m_pixmap and m_pixbuf properly
242 // is expensive and we don't want to do it implicitly (and possibly
243 // accidentally). wxBitmap::CloneGDIRefData() which does need to do it does
244 // it explicitly itself.
245 wxDECLARE_NO_COPY_CLASS(wxBitmapRefData
);
248 wxBitmapRefData::wxBitmapRefData(int width
, int height
, int depth
)
257 m_bpp
= gdk_drawable_get_depth(wxGetRootWindow()->window
);
258 m_alphaRequested
= depth
== 32;
261 wxBitmapRefData::~wxBitmapRefData()
264 g_object_unref (m_pixmap
);
266 g_object_unref (m_pixbuf
);
270 bool wxBitmapRefData::IsOk() const
275 //-----------------------------------------------------------------------------
277 //-----------------------------------------------------------------------------
279 #define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
281 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
283 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
285 LoadFile(filename
, type
);
288 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
290 wxASSERT(depth
== 1);
291 if (width
> 0 && height
> 0 && depth
== 1)
293 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
297 wxBitmap::wxBitmap(const char* const* bits
)
299 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
301 GdkBitmap
* mask
= NULL
;
302 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, const_cast<char**>(bits
)));
306 if (M_BMPDATA
->m_pixmap
!= NULL
&& mask
!= NULL
)
308 M_BMPDATA
->m_mask
= new wxMask(mask
);
312 wxBitmap::wxBitmap(GdkPixbuf
* pixbuf
)
316 wxBitmapRefData
* bmpData
= new wxBitmapRefData(
317 gdk_pixbuf_get_width(pixbuf
), gdk_pixbuf_get_height(pixbuf
),
318 gdk_pixbuf_get_n_channels(pixbuf
) * 8);
320 bmpData
->m_pixbuf
= pixbuf
;
324 wxBitmap::~wxBitmap()
328 bool wxBitmap::Create( int width
, int height
, int depth
)
331 wxCHECK_MSG(width
>= 0 && height
>= 0, false, "invalid bitmap size");
332 m_refData
= new wxBitmapRefData(width
, height
, depth
);
338 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
342 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
344 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
347 if (depth
== 32 || (depth
== -1 && image
.HasAlpha()))
348 return CreateFromImageAsPixbuf(image
);
350 // otherwise create pixmap, if alpha is present it will be converted to mask
351 return CreateFromImageAsPixmap(image
, depth
);
354 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
356 const int w
= image
.GetWidth();
357 const int h
= image
.GetHeight();
360 // create XBM format bitmap
362 // one bit per pixel, each row starts on a byte boundary
363 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
364 wxByte
* out
= new wxByte
[out_size
];
365 // set bits are black
366 memset(out
, 0xff, out_size
);
367 const wxByte
* in
= image
.GetData();
368 unsigned bit_index
= 0;
369 for (int y
= 0; y
< h
; y
++)
371 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
372 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
373 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
374 // move index to next byte boundary
375 bit_index
= (bit_index
+ 7) & ~7u;
377 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
380 if (!M_BMPDATA
) // SetPixmap may have failed
385 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
389 wxGtkObject
<GdkGC
> gc(gdk_gc_new(M_BMPDATA
->m_pixmap
));
391 M_BMPDATA
->m_pixmap
, gc
,
393 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
396 const wxByte
* alpha
= image
.GetAlpha();
397 if (alpha
!= NULL
|| image
.HasMask())
399 // create mask as XBM format bitmap
401 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
402 wxByte
* out
= new wxByte
[out_size
];
403 memset(out
, 0xff, out_size
);
404 unsigned bit_index
= 0;
407 for (int y
= 0; y
< h
; y
++)
409 for (int x
= 0; x
< w
; x
++, bit_index
++)
410 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
411 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
412 bit_index
= (bit_index
+ 7) & ~7u;
417 const wxByte r_mask
= image
.GetMaskRed();
418 const wxByte g_mask
= image
.GetMaskGreen();
419 const wxByte b_mask
= image
.GetMaskBlue();
420 const wxByte
* in
= image
.GetData();
421 for (int y
= 0; y
< h
; y
++)
423 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
424 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
425 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
426 bit_index
= (bit_index
+ 7) & ~7u;
429 SetMask(new wxMask(gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
)));
435 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
437 int width
= image
.GetWidth();
438 int height
= image
.GetHeight();
440 Create(width
, height
, 32);
441 GdkPixbuf
* pixbuf
= GetPixbuf();
446 const unsigned char* in
= image
.GetData();
447 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
448 unsigned char *alpha
= image
.GetAlpha();
450 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
452 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
454 for (int x
= 0; x
< width
; x
++, out
+= 4, in
+= 3)
467 wxImage
wxBitmap::ConvertToImage() const
469 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
471 const int w
= GetWidth();
472 const int h
= GetHeight();
473 wxImage
image(w
, h
, false);
474 unsigned char *data
= image
.GetData();
476 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
478 // prefer pixbuf if available, it will preserve alpha and should be quicker
481 GdkPixbuf
*pixbuf
= GetPixbuf();
482 unsigned char* alpha
= NULL
;
483 if (gdk_pixbuf_get_has_alpha(pixbuf
))
486 alpha
= image
.GetAlpha();
488 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
489 unsigned char *out
= data
;
490 const int inc
= 3 + int(alpha
!= NULL
);
491 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
493 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
495 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
507 GdkPixmap
* pixmap
= GetPixmap();
508 GdkPixmap
* pixmap_invert
= NULL
;
511 // mono bitmaps are inverted, i.e. 0 is white
512 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
513 wxGtkObject
<GdkGC
> gc(gdk_gc_new(pixmap_invert
));
514 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
515 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
516 pixmap
= pixmap_invert
;
518 // create a pixbuf which shares data with the wxImage
519 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
520 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
522 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
524 g_object_unref(pixbuf
);
525 if (pixmap_invert
!= NULL
)
526 g_object_unref(pixmap_invert
);
528 // convert mask, unless there is already alpha
529 if (GetMask() && !image
.HasAlpha())
531 // we hard code the mask colour for now but we could also make an
532 // effort (and waste time) to choose a colour not present in the
533 // image already to avoid having to fudge the pixels below --
534 // whether it's worth to do it is unclear however
535 const int MASK_RED
= 1;
536 const int MASK_GREEN
= 2;
537 const int MASK_BLUE
= 3;
538 const int MASK_BLUE_REPLACEMENT
= 2;
540 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
541 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
543 for (int y
= 0; y
< h
; y
++)
545 for (int x
= 0; x
< w
; x
++, data
+= 3)
547 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
550 data
[1] = MASK_GREEN
;
553 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
555 // we have to fudge the colour a bit to prevent
556 // this pixel from appearing transparent
557 data
[2] = MASK_BLUE_REPLACEMENT
;
561 g_object_unref(image_mask
);
567 #endif // wxUSE_IMAGE
569 int wxBitmap::GetHeight() const
571 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
573 return M_BMPDATA
->m_height
;
576 int wxBitmap::GetWidth() const
578 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
580 return M_BMPDATA
->m_width
;
583 int wxBitmap::GetDepth() const
585 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
587 return M_BMPDATA
->m_bpp
;
590 wxMask
*wxBitmap::GetMask() const
592 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
594 return M_BMPDATA
->m_mask
;
597 void wxBitmap::SetMask( wxMask
*mask
)
599 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
602 delete M_BMPDATA
->m_mask
;
603 M_BMPDATA
->m_mask
= mask
;
606 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
612 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
616 wxCHECK_MSG(IsOk(), ret
, wxT("invalid bitmap"));
618 const int w
= rect
.width
;
619 const int h
= rect
.height
;
620 const wxBitmapRefData
* bmpData
= M_BMPDATA
;
622 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
623 rect
.x
+ w
<= bmpData
->m_width
&&
624 rect
.y
+ h
<= bmpData
->m_height
,
625 ret
, wxT("invalid bitmap region"));
627 wxBitmapRefData
* const newRef
= new wxBitmapRefData(w
, h
, bmpData
->m_bpp
);
628 ret
.m_refData
= newRef
;
630 if (bmpData
->m_pixbuf
)
633 gdk_pixbuf_new_subpixbuf(bmpData
->m_pixbuf
, rect
.x
, rect
.y
, w
, h
);
634 newRef
->m_pixbuf
= gdk_pixbuf_copy(pixbuf
);
635 g_object_unref(pixbuf
);
637 if (bmpData
->m_pixmap
)
639 newRef
->m_pixmap
= gdk_pixmap_new(bmpData
->m_pixmap
, w
, h
, -1);
640 GdkGC
* gc
= gdk_gc_new(newRef
->m_pixmap
);
642 newRef
->m_pixmap
, gc
, bmpData
->m_pixmap
, rect
.x
, rect
.y
, 0, 0, w
, h
);
645 GdkPixmap
* mask
= NULL
;
647 mask
= bmpData
->m_mask
->GetBitmap();
650 GdkPixmap
* sub_mask
= gdk_pixmap_new(mask
, w
, h
, 1);
651 newRef
->m_mask
= new wxMask(sub_mask
);
652 GdkGC
* gc
= gdk_gc_new(sub_mask
);
654 sub_mask
, gc
, mask
, rect
.x
, rect
.y
, 0, 0, w
, h
);
661 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
663 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
666 wxImage image
= ConvertToImage();
667 if (image
.IsOk() && image
.SaveFile(name
, type
))
670 const char* type_name
= NULL
;
673 case wxBITMAP_TYPE_BMP
: type_name
= "bmp"; break;
674 case wxBITMAP_TYPE_ICO
: type_name
= "ico"; break;
675 case wxBITMAP_TYPE_JPEG
: type_name
= "jpeg"; break;
676 case wxBITMAP_TYPE_PNG
: type_name
= "png"; break;
680 gdk_pixbuf_save(GetPixbuf(), name
.fn_str(), type_name
, NULL
, NULL
);
683 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
687 if (image
.LoadFile(name
, type
) && image
.IsOk())
688 *this = wxBitmap(image
);
692 wxUnusedVar(type
); // The type is detected automatically by GDK.
694 *this = wxBitmap(gdk_pixbuf_new_from_file(name
.fn_str(), NULL
));
701 wxPalette
*wxBitmap::GetPalette() const
706 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
710 #endif // wxUSE_PALETTE
712 void wxBitmap::SetHeight( int height
)
715 M_BMPDATA
->m_height
= height
;
718 void wxBitmap::SetWidth( int width
)
721 M_BMPDATA
->m_width
= width
;
724 void wxBitmap::SetDepth( int depth
)
727 M_BMPDATA
->m_bpp
= depth
;
730 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
738 gdk_drawable_get_size(pixmap
, &w
, &h
);
739 wxBitmapRefData
* bmpData
= new wxBitmapRefData(w
, h
, 0);
741 bmpData
->m_pixmap
= pixmap
;
742 bmpData
->m_bpp
= gdk_drawable_get_depth(pixmap
);
745 GdkPixmap
*wxBitmap::GetPixmap() const
747 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
749 wxBitmapRefData
* bmpData
= M_BMPDATA
;
750 if (bmpData
->m_pixmap
)
751 return bmpData
->m_pixmap
;
753 if (bmpData
->m_pixbuf
)
755 GdkPixmap
* pixmap
= NULL
;
756 GdkPixmap
** mask_pixmap
= NULL
;
757 if (gdk_pixbuf_get_has_alpha(bmpData
->m_pixbuf
))
759 // make new mask from alpha
760 mask_pixmap
= &pixmap
;
762 gdk_pixbuf_render_pixmap_and_mask(
763 bmpData
->m_pixbuf
, &bmpData
->m_pixmap
, mask_pixmap
, 128);
766 delete bmpData
->m_mask
;
767 bmpData
->m_mask
= new wxMask(pixmap
);
772 bmpData
->m_pixmap
= gdk_pixmap_new(wxGetRootWindow()->window
,
773 bmpData
->m_width
, bmpData
->m_height
, bmpData
->m_bpp
== 1 ? 1 : -1);
775 return bmpData
->m_pixmap
;
778 bool wxBitmap::HasPixmap() const
780 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
782 return M_BMPDATA
->m_pixmap
!= NULL
;
785 GdkPixbuf
*wxBitmap::GetPixbuf() const
787 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
789 wxBitmapRefData
* bmpData
= M_BMPDATA
;
790 if (bmpData
->m_pixbuf
)
791 return bmpData
->m_pixbuf
;
793 const int w
= bmpData
->m_width
;
794 const int h
= bmpData
->m_height
;
795 GdkPixmap
* mask
= NULL
;
797 mask
= bmpData
->m_mask
->GetBitmap();
798 const bool useAlpha
= bmpData
->m_alphaRequested
|| mask
;
799 bmpData
->m_pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, useAlpha
, 8, w
, h
);
800 if (bmpData
->m_pixmap
)
801 PixmapToPixbuf(bmpData
->m_pixmap
, bmpData
->m_pixbuf
, w
, h
);
803 MaskToAlpha(mask
, bmpData
->m_pixbuf
, w
, h
);
804 return bmpData
->m_pixbuf
;
807 bool wxBitmap::HasPixbuf() const
809 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
811 return M_BMPDATA
->m_pixbuf
!= NULL
;
814 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
816 if (keep
== Pixmap
&& HasPixbuf())
818 g_object_unref (M_BMPDATA
->m_pixbuf
);
819 M_BMPDATA
->m_pixbuf
= NULL
;
821 if (keep
== Pixbuf
&& HasPixmap())
823 g_object_unref (M_BMPDATA
->m_pixmap
);
824 M_BMPDATA
->m_pixmap
= NULL
;
828 #ifdef wxHAS_RAW_BITMAP
829 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
832 GdkPixbuf
*pixbuf
= GetPixbuf();
833 const bool hasAlpha
= HasAlpha();
835 // allow access if bpp is valid and matches existence of alpha
836 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (bpp
== 32 && hasAlpha
)) )
838 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
839 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
840 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
841 bits
= gdk_pixbuf_get_pixels(pixbuf
);
846 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
849 #endif // wxHAS_RAW_BITMAP
851 bool wxBitmap::HasAlpha() const
853 const wxBitmapRefData
* bmpData
= M_BMPDATA
;
854 return bmpData
&& (bmpData
->m_alphaRequested
||
855 (bmpData
->m_pixbuf
&& gdk_pixbuf_get_has_alpha(bmpData
->m_pixbuf
)));
858 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
860 return new wxBitmapRefData(0, 0, 0);
863 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
865 const wxBitmapRefData
* oldRef
= static_cast<const wxBitmapRefData
*>(data
);
866 wxBitmapRefData
* const newRef
= new wxBitmapRefData(oldRef
->m_width
,
869 if (oldRef
->m_pixmap
!= NULL
)
871 newRef
->m_pixmap
= gdk_pixmap_new(
872 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
873 // use pixmap depth, m_bpp may not match
874 gdk_drawable_get_depth(oldRef
->m_pixmap
));
875 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_pixmap
));
877 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
879 if (oldRef
->m_pixbuf
!= NULL
)
881 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
883 if (oldRef
->m_mask
!= NULL
)
885 newRef
->m_mask
= new wxMask(*oldRef
->m_mask
);
891 /* static */ void wxBitmap::InitStandardHandlers()
893 // TODO: Insert handler based on GdkPixbufs handler later