]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
cd556674cf9b822e2468c5a8e82ac512a69f4946
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"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern GtkWidget
*wxGetRootWindow();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
42 m_bitmap
= (GdkBitmap
*) NULL
;
45 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
47 m_bitmap
= (GdkBitmap
*) NULL
;
48 Create( bitmap
, colour
);
52 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
54 m_bitmap
= (GdkBitmap
*) NULL
;
55 Create( bitmap
, paletteIndex
);
57 #endif // wxUSE_PALETTE
59 wxMask::wxMask( const wxBitmap
& bitmap
)
61 m_bitmap
= (GdkBitmap
*) NULL
;
68 g_object_unref (m_bitmap
);
71 bool wxMask::Create( const wxBitmap
& bitmap
,
72 const wxColour
& colour
)
76 g_object_unref (m_bitmap
);
77 m_bitmap
= (GdkBitmap
*) NULL
;
80 const int w
= bitmap
.GetWidth();
81 const int h
= bitmap
.GetHeight();
83 // create mask as XBM format bitmap
85 // one bit per pixel, each row starts on a byte boundary
86 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
87 wxByte
* out
= new wxByte
[out_size
];
88 // set bits are unmasked
89 memset(out
, 0xff, out_size
);
90 unsigned bit_index
= 0;
91 if (bitmap
.HasPixbuf())
93 const wxByte r_mask
= colour
.Red();
94 const wxByte g_mask
= colour
.Green();
95 const wxByte b_mask
= colour
.Blue();
96 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
97 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
98 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
99 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
100 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
102 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
103 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
104 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
105 // move index to next byte boundary
106 bit_index
= (bit_index
+ 7) & ~7u;
111 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
112 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
114 if (colormap
== NULL
)
115 // mono bitmap, white is pixel value 0
116 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
120 c
.CalcPixel(colormap
);
121 mask_pixel
= c
.GetPixel();
123 for (int y
= 0; y
< h
; y
++)
125 for (int x
= 0; x
< w
; x
++, bit_index
++)
126 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
127 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
128 bit_index
= (bit_index
+ 7) & ~7u;
130 g_object_unref(image
);
132 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
138 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
141 wxPalette
*pal
= bitmap
.GetPalette();
143 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
145 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
147 return Create(bitmap
, wxColour(r
, g
, b
));
149 #endif // wxUSE_PALETTE
151 bool wxMask::Create( const wxBitmap
& bitmap
)
155 g_object_unref (m_bitmap
);
156 m_bitmap
= (GdkBitmap
*) NULL
;
159 if (!bitmap
.Ok()) return false;
161 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
163 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
165 if (!m_bitmap
) return false;
167 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
168 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
169 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
175 GdkBitmap
*wxMask::GetBitmap() const
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
184 class wxBitmapRefData
: public wxGDIRefData
188 virtual ~wxBitmapRefData();
190 virtual bool IsOk() const { return m_pixmap
|| m_pixbuf
; }
199 wxPalette
*m_palette
;
200 #endif // wxUSE_PALETTE
203 wxBitmapRefData::wxBitmapRefData()
205 m_pixmap
= (GdkPixmap
*) NULL
;
206 m_pixbuf
= (GdkPixbuf
*) NULL
;
207 m_mask
= (wxMask
*) NULL
;
212 m_palette
= (wxPalette
*) NULL
;
213 #endif // wxUSE_PALETTE
216 wxBitmapRefData::~wxBitmapRefData()
219 g_object_unref (m_pixmap
);
221 g_object_unref (m_pixbuf
);
225 #endif // wxUSE_PALETTE
228 //-----------------------------------------------------------------------------
230 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
232 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
234 wxBitmap::wxBitmap(int width
, int height
, int depth
)
236 Create(width
, height
, depth
);
239 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
241 LoadFile(filename
, type
);
244 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
246 wxASSERT(depth
== 1);
247 if (width
> 0 && height
> 0 && depth
== 1)
249 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
251 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
255 wxBitmap::wxBitmap(const char* const* bits
)
257 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
259 GdkBitmap
* mask
= NULL
;
260 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, wx_const_cast(char**, bits
)));
262 if (M_BMPDATA
->m_pixmap
!= NULL
&& mask
!= NULL
)
264 M_BMPDATA
->m_mask
= new wxMask
;
265 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
269 wxBitmap::~wxBitmap()
273 bool wxBitmap::Create( int width
, int height
, int depth
)
277 if ( width
<= 0 || height
<= 0 )
284 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
), 32);
290 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
292 depth
= visual
->depth
;
294 wxCHECK_MSG(depth
== visual
->depth
, false, wxT("invalid bitmap depth"));
297 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
303 wxBitmap
wxBitmap::Rescale(int clipx
, int clipy
, int clipwidth
, int clipheight
,
304 int width
, int height
) const
308 wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap"));
310 width
= wxMax(width
, 1);
311 height
= wxMax(height
, 1);
313 const double scale_x
= double(width
) / clipwidth
;
314 const double scale_y
= double(height
) / clipheight
;
316 // Converting to pixbuf, scaling with gdk_pixbuf_scale, and converting
317 // back, is faster than scaling pixmap ourselves.
319 // use pixbuf if already available,
320 // otherwise create temporary pixbuf from pixmap
321 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
323 g_object_ref(pixbuf
);
325 pixbuf
= gdk_pixbuf_get_from_drawable(
326 NULL
, M_BMPDATA
->m_pixmap
, NULL
,
327 0, 0, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
329 // new pixbuf for scaled wxBitmap
330 GdkPixbuf
* pixbuf_scaled
= gdk_pixbuf_new(
331 GDK_COLORSPACE_RGB
, gdk_pixbuf_get_has_alpha(pixbuf
), 8, width
, height
);
333 // GDK_INTERP_NEAREST is the lowest-quality method for continuous-tone
334 // images, but the only one which preserves sharp edges
336 pixbuf
, pixbuf_scaled
,
337 0, 0, width
, height
, -clipx
*scale_x
, -clipy
*scale_y
, scale_x
, scale_y
,
340 g_object_unref(pixbuf
);
341 bmp
.SetPixbuf(pixbuf_scaled
, M_BMPDATA
->m_bpp
);
343 if (M_BMPDATA
->m_mask
)
345 pixbuf
= gdk_pixbuf_get_from_drawable(
346 NULL
, M_BMPDATA
->m_mask
->m_bitmap
, NULL
,
347 0, 0, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
349 pixbuf_scaled
= gdk_pixbuf_new(
350 GDK_COLORSPACE_RGB
, false, 8, width
, height
);
353 pixbuf
, pixbuf_scaled
,
354 0, 0, width
, height
, -clipx
, -clipy
, scale_x
, scale_y
,
357 g_object_unref(pixbuf
);
359 // use existing functionality to create mask from scaled pixbuf
361 maskbmp
.SetPixbuf(pixbuf_scaled
);
362 bmp
.SetMask(new wxMask(maskbmp
, *wxBLACK
));
369 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
373 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
374 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
376 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
379 // create pixbuf if image has alpha and requested depth is compatible
380 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
381 return CreateFromImageAsPixbuf(image
);
383 // otherwise create pixmap, if alpha is present it will be converted to mask
384 return CreateFromImageAsPixmap(image
, depth
);
387 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
389 const int w
= image
.GetWidth();
390 const int h
= image
.GetHeight();
393 // create XBM format bitmap
395 // one bit per pixel, each row starts on a byte boundary
396 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
397 wxByte
* out
= new wxByte
[out_size
];
398 // set bits are black
399 memset(out
, 0xff, out_size
);
400 const wxByte
* in
= image
.GetData();
401 unsigned bit_index
= 0;
402 for (int y
= 0; y
< h
; y
++)
404 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
405 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
406 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
407 // move index to next byte boundary
408 bit_index
= (bit_index
+ 7) & ~7u;
410 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
415 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
416 GdkGC
* gc
= gdk_gc_new(M_BMPDATA
->m_pixmap
);
418 M_BMPDATA
->m_pixmap
, gc
,
420 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
424 const wxByte
* alpha
= image
.GetAlpha();
425 if (alpha
!= NULL
|| image
.HasMask())
427 // create mask as XBM format bitmap
429 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
430 wxByte
* out
= new wxByte
[out_size
];
431 memset(out
, 0xff, out_size
);
432 unsigned bit_index
= 0;
435 for (int y
= 0; y
< h
; y
++)
437 for (int x
= 0; x
< w
; x
++, bit_index
++)
438 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
439 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
440 bit_index
= (bit_index
+ 7) & ~7u;
445 const wxByte r_mask
= image
.GetMaskRed();
446 const wxByte g_mask
= image
.GetMaskGreen();
447 const wxByte b_mask
= image
.GetMaskBlue();
448 const wxByte
* in
= image
.GetData();
449 for (int y
= 0; y
< h
; y
++)
451 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
452 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
453 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
454 bit_index
= (bit_index
+ 7) & ~7u;
457 wxMask
* mask
= new wxMask
;
458 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
465 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
467 wxASSERT(image
.HasAlpha());
469 int width
= image
.GetWidth();
470 int height
= image
.GetHeight();
472 Create(width
, height
, 32);
473 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
478 const unsigned char* in
= image
.GetData();
479 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
480 unsigned char *alpha
= image
.GetAlpha();
482 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
484 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
486 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
498 wxImage
wxBitmap::ConvertToImage() const
500 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
502 const int w
= GetWidth();
503 const int h
= GetHeight();
504 wxImage
image(w
, h
, false);
505 unsigned char *data
= image
.GetData();
507 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
509 // prefer pixbuf if available, it will preserve alpha and should be quicker
512 GdkPixbuf
*pixbuf
= GetPixbuf();
513 unsigned char* alpha
= NULL
;
514 if (gdk_pixbuf_get_has_alpha(pixbuf
))
517 alpha
= image
.GetAlpha();
519 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
520 unsigned char *out
= data
;
521 const int inc
= 3 + int(alpha
!= NULL
);
522 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
524 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
526 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
538 GdkPixmap
* pixmap
= GetPixmap();
539 GdkPixmap
* pixmap_invert
= NULL
;
542 // mono bitmaps are inverted, i.e. 0 is white
543 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
544 GdkGC
* gc
= gdk_gc_new(pixmap_invert
);
545 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
546 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
548 pixmap
= pixmap_invert
;
550 // create a pixbuf which shares data with the wxImage
551 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
552 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
554 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
556 g_object_unref(pixbuf
);
557 if (pixmap_invert
!= NULL
)
558 g_object_unref(pixmap_invert
);
560 // convert mask, unless there is already alpha
561 if (GetMask() && !image
.HasAlpha())
563 // we hard code the mask colour for now but we could also make an
564 // effort (and waste time) to choose a colour not present in the
565 // image already to avoid having to fudge the pixels below --
566 // whether it's worth to do it is unclear however
567 const int MASK_RED
= 1;
568 const int MASK_GREEN
= 2;
569 const int MASK_BLUE
= 3;
570 const int MASK_BLUE_REPLACEMENT
= 2;
572 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
573 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
575 for (int y
= 0; y
< h
; y
++)
577 for (int x
= 0; x
< w
; x
++, data
+= 3)
579 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
582 data
[1] = MASK_GREEN
;
585 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
587 // we have to fudge the colour a bit to prevent
588 // this pixel from appearing transparent
589 data
[2] = MASK_BLUE_REPLACEMENT
;
593 g_object_unref(image_mask
);
599 #endif // wxUSE_IMAGE
601 int wxBitmap::GetHeight() const
603 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
605 return M_BMPDATA
->m_height
;
608 int wxBitmap::GetWidth() const
610 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
612 return M_BMPDATA
->m_width
;
615 int wxBitmap::GetDepth() const
617 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
619 return M_BMPDATA
->m_bpp
;
622 wxMask
*wxBitmap::GetMask() const
624 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
626 return M_BMPDATA
->m_mask
;
629 void wxBitmap::SetMask( wxMask
*mask
)
631 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
634 delete M_BMPDATA
->m_mask
;
635 M_BMPDATA
->m_mask
= mask
;
638 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
644 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
648 wxCHECK_MSG(Ok(), ret
, wxT("invalid bitmap"));
649 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
650 rect
.x
+ rect
.width
<= M_BMPDATA
->m_width
&&
651 rect
.y
+ rect
.height
<= M_BMPDATA
->m_height
,
652 ret
, wxT("invalid bitmap region"));
654 if (HasPixbuf() || M_BMPDATA
->m_bpp
== 32)
656 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
657 gdk_pixbuf_get_has_alpha(GetPixbuf()),
658 8, rect
.width
, rect
.height
);
659 ret
.SetPixbuf(pixbuf
, M_BMPDATA
->m_bpp
);
660 gdk_pixbuf_copy_area(GetPixbuf(),
661 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
666 ret
.Create(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
667 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
668 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
671 // make mask, unless there is already alpha
672 if (GetMask() && !HasAlpha())
674 wxMask
*mask
= new wxMask
;
675 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
677 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
678 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
687 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
689 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
692 // Try to save the bitmap via wxImage handlers:
693 wxImage image
= ConvertToImage();
694 return image
.Ok() && image
.SaveFile(name
, type
);
695 #else // !wxUSE_IMAGE
700 #endif // wxUSE_IMAGE
703 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
707 if (type
== wxBITMAP_TYPE_XPM
)
709 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
710 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
714 M_BMPDATA
->m_mask
= new wxMask
;
715 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
719 else // try if wxImage can load it
722 if (image
.LoadFile(name
, type
) && image
.Ok())
723 CreateFromImage(image
, -1);
725 #endif // wxUSE_IMAGE
731 wxPalette
*wxBitmap::GetPalette() const
733 wxCHECK_MSG(Ok(), NULL
, wxT("invalid bitmap"));
735 return M_BMPDATA
->m_palette
;
738 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
742 #endif // wxUSE_PALETTE
744 void wxBitmap::SetHeight( int height
)
747 M_BMPDATA
->m_height
= height
;
750 void wxBitmap::SetWidth( int width
)
753 M_BMPDATA
->m_width
= width
;
756 void wxBitmap::SetDepth( int depth
)
759 M_BMPDATA
->m_bpp
= depth
;
762 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
765 m_refData
= new wxBitmapRefData
;
767 // AllocExclusive should not be needed for this internal function
768 wxASSERT(m_refData
->GetRefCount() == 1);
769 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
770 M_BMPDATA
->m_pixmap
= pixmap
;
771 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
772 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
773 PurgeOtherRepresentations(Pixmap
);
776 GdkPixmap
*wxBitmap::GetPixmap() const
778 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
780 // create the pixmap on the fly if we use Pixbuf representation:
781 if (M_BMPDATA
->m_pixmap
== NULL
)
783 GdkPixmap
** pmask
= NULL
;
784 if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
))
786 // make new mask from alpha
787 delete M_BMPDATA
->m_mask
;
788 M_BMPDATA
->m_mask
= new wxMask
;
789 pmask
= &M_BMPDATA
->m_mask
->m_bitmap
;
791 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
792 &M_BMPDATA
->m_pixmap
,
794 0x80 /* alpha threshold */);
797 return M_BMPDATA
->m_pixmap
;
800 bool wxBitmap::HasPixmap() const
802 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
804 return M_BMPDATA
->m_pixmap
!= NULL
;
807 GdkPixbuf
*wxBitmap::GetPixbuf() const
809 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
811 if (M_BMPDATA
->m_pixbuf
== NULL
)
813 int width
= GetWidth();
814 int height
= GetHeight();
816 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
819 M_BMPDATA
->m_pixbuf
= pixbuf
;
820 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
821 0, 0, 0, 0, width
, height
);
823 // apply the mask to created pixbuf:
824 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
827 gdk_pixbuf_get_from_drawable(NULL
,
828 M_BMPDATA
->m_mask
->GetBitmap(),
830 0, 0, 0, 0, width
, height
);
833 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
834 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
835 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
836 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
838 for (int y
= 0; y
< height
;
839 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
841 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
843 if (mask
[0] == 0 /*black pixel*/)
848 g_object_unref (pmask
);
853 return M_BMPDATA
->m_pixbuf
;
856 bool wxBitmap::HasPixbuf() const
858 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
860 return M_BMPDATA
->m_pixbuf
!= NULL
;
863 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
, int depth
)
866 m_refData
= new wxBitmapRefData
;
868 // AllocExclusive should not be needed for this internal function
869 wxASSERT(m_refData
->GetRefCount() == 1);
870 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
871 M_BMPDATA
->m_pixbuf
= pixbuf
;
872 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
873 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
874 // if depth specified
876 M_BMPDATA
->m_bpp
= depth
;
877 else if (M_BMPDATA
->m_bpp
== 0)
878 // use something reasonable
879 M_BMPDATA
->m_bpp
= wxTheApp
->GetGdkVisual()->depth
;
880 PurgeOtherRepresentations(Pixbuf
);
883 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
885 if (keep
== Pixmap
&& HasPixbuf())
887 g_object_unref (M_BMPDATA
->m_pixbuf
);
888 M_BMPDATA
->m_pixbuf
= NULL
;
890 if (keep
== Pixbuf
&& HasPixmap())
892 g_object_unref (M_BMPDATA
->m_pixmap
);
893 M_BMPDATA
->m_pixmap
= NULL
;
897 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
900 GdkPixbuf
*pixbuf
= GetPixbuf();
901 const bool hasAlpha
= HasAlpha();
902 // allow access if bpp is valid and matches existence of alpha
903 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (bpp
== 32 && hasAlpha
)) )
905 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
906 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
907 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
908 bits
= gdk_pixbuf_get_pixels(pixbuf
);
913 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
917 bool wxBitmap::HasAlpha() const
919 return m_refData
!= NULL
&& M_BMPDATA
->m_pixbuf
!= NULL
&&
920 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
);
923 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
925 return new wxBitmapRefData
;
928 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
930 const wxBitmapRefData
* oldRef
= wx_static_cast(const wxBitmapRefData
*, data
);
931 wxBitmapRefData
* newRef
= new wxBitmapRefData
;
932 newRef
->m_width
= oldRef
->m_width
;
933 newRef
->m_height
= oldRef
->m_height
;
934 newRef
->m_bpp
= oldRef
->m_bpp
;
935 if (oldRef
->m_pixmap
!= NULL
)
937 newRef
->m_pixmap
= gdk_pixmap_new(
938 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
939 // use pixmap depth, m_bpp may not match
940 gdk_drawable_get_depth(oldRef
->m_pixmap
));
941 GdkGC
* gc
= gdk_gc_new(newRef
->m_pixmap
);
943 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
946 if (oldRef
->m_pixbuf
!= NULL
)
948 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
950 if (oldRef
->m_mask
!= NULL
)
952 newRef
->m_mask
= new wxMask
;
953 newRef
->m_mask
->m_bitmap
= gdk_pixmap_new(
954 oldRef
->m_mask
->m_bitmap
, oldRef
->m_width
, oldRef
->m_height
, 1);
955 GdkGC
* gc
= gdk_gc_new(newRef
->m_mask
->m_bitmap
);
956 gdk_draw_drawable(newRef
->m_mask
->m_bitmap
,
957 gc
, oldRef
->m_mask
->m_bitmap
, 0, 0, 0, 0, -1, -1);
961 // implement this if SetPalette is ever implemented
962 wxASSERT(oldRef
->m_palette
== NULL
);
968 /* static */ void wxBitmap::InitStandardHandlers()
970 // TODO: Insert handler based on GdkPixbufs handler later