]>
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()
316 bool wxBitmap::Create( int width
, int height
, int depth
)
319 wxCHECK_MSG(width
>= 0 && height
>= 0, false, "invalid bitmap size");
320 m_refData
= new wxBitmapRefData(width
, height
, depth
);
326 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
330 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
332 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
335 if (depth
== 32 || (depth
== -1 && image
.HasAlpha()))
336 return CreateFromImageAsPixbuf(image
);
338 // otherwise create pixmap, if alpha is present it will be converted to mask
339 return CreateFromImageAsPixmap(image
, depth
);
342 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
344 const int w
= image
.GetWidth();
345 const int h
= image
.GetHeight();
348 // create XBM format bitmap
350 // one bit per pixel, each row starts on a byte boundary
351 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
352 wxByte
* out
= new wxByte
[out_size
];
353 // set bits are black
354 memset(out
, 0xff, out_size
);
355 const wxByte
* in
= image
.GetData();
356 unsigned bit_index
= 0;
357 for (int y
= 0; y
< h
; y
++)
359 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
360 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
361 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
362 // move index to next byte boundary
363 bit_index
= (bit_index
+ 7) & ~7u;
365 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
368 if (!M_BMPDATA
) // SetPixmap may have failed
373 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
377 wxGtkObject
<GdkGC
> gc(gdk_gc_new(M_BMPDATA
->m_pixmap
));
379 M_BMPDATA
->m_pixmap
, gc
,
381 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
384 const wxByte
* alpha
= image
.GetAlpha();
385 if (alpha
!= NULL
|| image
.HasMask())
387 // create mask as XBM format bitmap
389 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
390 wxByte
* out
= new wxByte
[out_size
];
391 memset(out
, 0xff, out_size
);
392 unsigned bit_index
= 0;
395 for (int y
= 0; y
< h
; y
++)
397 for (int x
= 0; x
< w
; x
++, bit_index
++)
398 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
399 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
400 bit_index
= (bit_index
+ 7) & ~7u;
405 const wxByte r_mask
= image
.GetMaskRed();
406 const wxByte g_mask
= image
.GetMaskGreen();
407 const wxByte b_mask
= image
.GetMaskBlue();
408 const wxByte
* in
= image
.GetData();
409 for (int y
= 0; y
< h
; y
++)
411 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
412 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
413 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
414 bit_index
= (bit_index
+ 7) & ~7u;
417 SetMask(new wxMask(gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
)));
423 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
425 int width
= image
.GetWidth();
426 int height
= image
.GetHeight();
428 Create(width
, height
, 32);
429 GdkPixbuf
* pixbuf
= GetPixbuf();
434 const unsigned char* in
= image
.GetData();
435 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
436 unsigned char *alpha
= image
.GetAlpha();
438 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
440 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
442 for (int x
= 0; x
< width
; x
++, out
+= 4, in
+= 3)
455 wxImage
wxBitmap::ConvertToImage() const
457 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
459 const int w
= GetWidth();
460 const int h
= GetHeight();
461 wxImage
image(w
, h
, false);
462 unsigned char *data
= image
.GetData();
464 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
466 // prefer pixbuf if available, it will preserve alpha and should be quicker
469 GdkPixbuf
*pixbuf
= GetPixbuf();
470 unsigned char* alpha
= NULL
;
471 if (gdk_pixbuf_get_has_alpha(pixbuf
))
474 alpha
= image
.GetAlpha();
476 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
477 unsigned char *out
= data
;
478 const int inc
= 3 + int(alpha
!= NULL
);
479 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
481 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
483 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
495 GdkPixmap
* pixmap
= GetPixmap();
496 GdkPixmap
* pixmap_invert
= NULL
;
499 // mono bitmaps are inverted, i.e. 0 is white
500 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
501 wxGtkObject
<GdkGC
> gc(gdk_gc_new(pixmap_invert
));
502 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
503 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
504 pixmap
= pixmap_invert
;
506 // create a pixbuf which shares data with the wxImage
507 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
508 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
510 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
512 g_object_unref(pixbuf
);
513 if (pixmap_invert
!= NULL
)
514 g_object_unref(pixmap_invert
);
516 // convert mask, unless there is already alpha
517 if (GetMask() && !image
.HasAlpha())
519 // we hard code the mask colour for now but we could also make an
520 // effort (and waste time) to choose a colour not present in the
521 // image already to avoid having to fudge the pixels below --
522 // whether it's worth to do it is unclear however
523 const int MASK_RED
= 1;
524 const int MASK_GREEN
= 2;
525 const int MASK_BLUE
= 3;
526 const int MASK_BLUE_REPLACEMENT
= 2;
528 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
529 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
531 for (int y
= 0; y
< h
; y
++)
533 for (int x
= 0; x
< w
; x
++, data
+= 3)
535 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
538 data
[1] = MASK_GREEN
;
541 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
543 // we have to fudge the colour a bit to prevent
544 // this pixel from appearing transparent
545 data
[2] = MASK_BLUE_REPLACEMENT
;
549 g_object_unref(image_mask
);
555 #endif // wxUSE_IMAGE
557 int wxBitmap::GetHeight() const
559 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
561 return M_BMPDATA
->m_height
;
564 int wxBitmap::GetWidth() const
566 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
568 return M_BMPDATA
->m_width
;
571 int wxBitmap::GetDepth() const
573 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
575 return M_BMPDATA
->m_bpp
;
578 wxMask
*wxBitmap::GetMask() const
580 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
582 return M_BMPDATA
->m_mask
;
585 void wxBitmap::SetMask( wxMask
*mask
)
587 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
590 delete M_BMPDATA
->m_mask
;
591 M_BMPDATA
->m_mask
= mask
;
594 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
600 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
604 wxCHECK_MSG(IsOk(), ret
, wxT("invalid bitmap"));
606 const int w
= rect
.width
;
607 const int h
= rect
.height
;
608 const wxBitmapRefData
* bmpData
= M_BMPDATA
;
610 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
611 rect
.x
+ w
<= bmpData
->m_width
&&
612 rect
.y
+ h
<= bmpData
->m_height
,
613 ret
, wxT("invalid bitmap region"));
615 wxBitmapRefData
* const newRef
= new wxBitmapRefData(w
, h
, bmpData
->m_bpp
);
616 ret
.m_refData
= newRef
;
618 if (bmpData
->m_pixbuf
)
621 gdk_pixbuf_new_subpixbuf(bmpData
->m_pixbuf
, rect
.x
, rect
.y
, w
, h
);
622 newRef
->m_pixbuf
= gdk_pixbuf_copy(pixbuf
);
623 g_object_unref(pixbuf
);
625 if (bmpData
->m_pixmap
)
627 newRef
->m_pixmap
= gdk_pixmap_new(bmpData
->m_pixmap
, w
, h
, -1);
628 GdkGC
* gc
= gdk_gc_new(newRef
->m_pixmap
);
630 newRef
->m_pixmap
, gc
, bmpData
->m_pixmap
, rect
.x
, rect
.y
, 0, 0, w
, h
);
633 GdkPixmap
* mask
= NULL
;
635 mask
= bmpData
->m_mask
->GetBitmap();
638 GdkPixmap
* sub_mask
= gdk_pixmap_new(mask
, w
, h
, 1);
639 newRef
->m_mask
= new wxMask(sub_mask
);
640 GdkGC
* gc
= gdk_gc_new(sub_mask
);
642 sub_mask
, gc
, mask
, rect
.x
, rect
.y
, 0, 0, w
, h
);
649 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
651 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
654 wxImage image
= ConvertToImage();
655 if (image
.IsOk() && image
.SaveFile(name
, type
))
658 const char* type_name
= NULL
;
661 case wxBITMAP_TYPE_BMP
: type_name
= "bmp"; break;
662 case wxBITMAP_TYPE_ICO
: type_name
= "ico"; break;
663 case wxBITMAP_TYPE_JPEG
: type_name
= "jpeg"; break;
664 case wxBITMAP_TYPE_PNG
: type_name
= "png"; break;
668 gdk_pixbuf_save(GetPixbuf(), name
.fn_str(), type_name
, NULL
, NULL
);
671 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
675 if (image
.LoadFile(name
, type
) && image
.IsOk())
676 *this = wxBitmap(image
);
680 wxUnusedVar(type
); // The type is detected automatically by GDK.
683 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_file(name
.fn_str(), NULL
);
692 wxPalette
*wxBitmap::GetPalette() const
697 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
701 #endif // wxUSE_PALETTE
703 void wxBitmap::SetHeight( int height
)
706 M_BMPDATA
->m_height
= height
;
709 void wxBitmap::SetWidth( int width
)
712 M_BMPDATA
->m_width
= width
;
715 void wxBitmap::SetDepth( int depth
)
718 M_BMPDATA
->m_bpp
= depth
;
721 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
729 gdk_drawable_get_size(pixmap
, &w
, &h
);
730 wxBitmapRefData
* bmpData
= new wxBitmapRefData(w
, h
, 0);
732 bmpData
->m_pixmap
= pixmap
;
733 bmpData
->m_bpp
= gdk_drawable_get_depth(pixmap
);
736 GdkPixmap
*wxBitmap::GetPixmap() const
738 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
740 wxBitmapRefData
* bmpData
= M_BMPDATA
;
741 if (bmpData
->m_pixmap
)
742 return bmpData
->m_pixmap
;
744 if (bmpData
->m_pixbuf
)
746 GdkPixmap
* pixmap
= NULL
;
747 GdkPixmap
** mask_pixmap
= NULL
;
748 if (gdk_pixbuf_get_has_alpha(bmpData
->m_pixbuf
))
750 // make new mask from alpha
751 mask_pixmap
= &pixmap
;
753 gdk_pixbuf_render_pixmap_and_mask(
754 bmpData
->m_pixbuf
, &bmpData
->m_pixmap
, mask_pixmap
, 128);
757 delete bmpData
->m_mask
;
758 bmpData
->m_mask
= new wxMask(pixmap
);
763 bmpData
->m_pixmap
= gdk_pixmap_new(wxGetRootWindow()->window
,
764 bmpData
->m_width
, bmpData
->m_height
, bmpData
->m_bpp
== 1 ? 1 : -1);
766 return bmpData
->m_pixmap
;
769 bool wxBitmap::HasPixmap() const
771 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
773 return M_BMPDATA
->m_pixmap
!= NULL
;
776 GdkPixbuf
*wxBitmap::GetPixbuf() const
778 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
780 wxBitmapRefData
* bmpData
= M_BMPDATA
;
781 if (bmpData
->m_pixbuf
)
782 return bmpData
->m_pixbuf
;
784 const int w
= bmpData
->m_width
;
785 const int h
= bmpData
->m_height
;
786 GdkPixmap
* mask
= NULL
;
788 mask
= bmpData
->m_mask
->GetBitmap();
789 const bool useAlpha
= bmpData
->m_alphaRequested
|| mask
;
790 bmpData
->m_pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, useAlpha
, 8, w
, h
);
791 if (bmpData
->m_pixmap
)
792 PixmapToPixbuf(bmpData
->m_pixmap
, bmpData
->m_pixbuf
, w
, h
);
794 MaskToAlpha(mask
, bmpData
->m_pixbuf
, w
, h
);
795 return bmpData
->m_pixbuf
;
798 bool wxBitmap::HasPixbuf() const
800 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
802 return M_BMPDATA
->m_pixbuf
!= NULL
;
805 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
)
813 if (gdk_pixbuf_get_has_alpha(pixbuf
))
815 m_refData
= new wxBitmapRefData(
816 gdk_pixbuf_get_width(pixbuf
), gdk_pixbuf_get_height(pixbuf
), depth
);
818 M_BMPDATA
->m_pixbuf
= pixbuf
;
821 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
823 if (keep
== Pixmap
&& HasPixbuf())
825 g_object_unref (M_BMPDATA
->m_pixbuf
);
826 M_BMPDATA
->m_pixbuf
= NULL
;
828 if (keep
== Pixbuf
&& HasPixmap())
830 g_object_unref (M_BMPDATA
->m_pixmap
);
831 M_BMPDATA
->m_pixmap
= NULL
;
835 #ifdef wxHAS_RAW_BITMAP
836 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
839 GdkPixbuf
*pixbuf
= GetPixbuf();
840 const bool hasAlpha
= HasAlpha();
842 // allow access if bpp is valid and matches existence of alpha
843 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (bpp
== 32 && hasAlpha
)) )
845 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
846 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
847 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
848 bits
= gdk_pixbuf_get_pixels(pixbuf
);
853 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
856 #endif // wxHAS_RAW_BITMAP
858 bool wxBitmap::HasAlpha() const
860 const wxBitmapRefData
* bmpData
= M_BMPDATA
;
861 return bmpData
&& (bmpData
->m_alphaRequested
||
862 (bmpData
->m_pixbuf
&& gdk_pixbuf_get_has_alpha(bmpData
->m_pixbuf
)));
865 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
867 return new wxBitmapRefData(0, 0, 0);
870 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
872 const wxBitmapRefData
* oldRef
= static_cast<const wxBitmapRefData
*>(data
);
873 wxBitmapRefData
* const newRef
= new wxBitmapRefData(oldRef
->m_width
,
876 if (oldRef
->m_pixmap
!= NULL
)
878 newRef
->m_pixmap
= gdk_pixmap_new(
879 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
880 // use pixmap depth, m_bpp may not match
881 gdk_drawable_get_depth(oldRef
->m_pixmap
));
882 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_pixmap
));
884 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
886 if (oldRef
->m_pixbuf
!= NULL
)
888 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
890 if (oldRef
->m_mask
!= NULL
)
892 newRef
->m_mask
= new wxMask(*oldRef
->m_mask
);
898 /* static */ void wxBitmap::InitStandardHandlers()
900 // TODO: Insert handler based on GdkPixbufs handler later