]>
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"
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 wxObjectRefData
188 virtual ~wxBitmapRefData();
197 wxPalette
*m_palette
;
198 #endif // wxUSE_PALETTE
201 wxBitmapRefData::wxBitmapRefData()
203 m_pixmap
= (GdkPixmap
*) NULL
;
204 m_pixbuf
= (GdkPixbuf
*) NULL
;
205 m_mask
= (wxMask
*) NULL
;
210 m_palette
= (wxPalette
*) NULL
;
211 #endif // wxUSE_PALETTE
214 wxBitmapRefData::~wxBitmapRefData()
217 g_object_unref (m_pixmap
);
219 g_object_unref (m_pixbuf
);
223 #endif // wxUSE_PALETTE
226 //-----------------------------------------------------------------------------
228 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
230 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
232 wxBitmap::wxBitmap(int width
, int height
, int depth
)
234 Create(width
, height
, depth
);
237 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
239 LoadFile(filename
, type
);
242 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
244 wxASSERT(depth
== 1);
245 if (width
> 0 && height
> 0 && depth
== 1)
247 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
249 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
253 wxBitmap::wxBitmap(const char* const* bits
)
255 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
257 GdkBitmap
* mask
= NULL
;
258 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, wx_const_cast(char**, bits
)));
260 if (M_BMPDATA
->m_pixmap
!= NULL
&& mask
!= NULL
)
262 M_BMPDATA
->m_mask
= new wxMask
;
263 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
267 wxBitmap::~wxBitmap()
271 bool wxBitmap::Create( int width
, int height
, int depth
)
275 if ( width
<= 0 || height
<= 0 )
282 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
), 32);
288 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
290 depth
= visual
->depth
;
292 wxCHECK_MSG(depth
== visual
->depth
, false, wxT("invalid bitmap depth"));
295 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
301 wxBitmap
wxBitmap::Rescale(int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
) const
305 wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap"));
307 int width
= wxMax(newx
, 1);
308 int height
= wxMax(newy
, 1);
309 width
= wxMin(width
, clipwidth
);
310 height
= wxMin(height
, clipheight
);
312 const double scale_x
= double(newx
) / M_BMPDATA
->m_width
;
313 const double scale_y
= double(newy
) / M_BMPDATA
->m_height
;
315 // Converting to pixbuf, scaling with gdk_pixbuf_scale, and converting
316 // back, is faster than scaling pixmap ourselves.
318 // use pixbuf if already available,
319 // otherwise create temporary pixbuf from pixmap
320 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
322 g_object_ref(pixbuf
);
324 pixbuf
= gdk_pixbuf_get_from_drawable(
325 NULL
, M_BMPDATA
->m_pixmap
, NULL
,
326 0, 0, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
328 // new pixbuf for scaled wxBitmap
329 GdkPixbuf
* pixbuf_scaled
= gdk_pixbuf_new(
330 GDK_COLORSPACE_RGB
, gdk_pixbuf_get_has_alpha(pixbuf
), 8, width
, height
);
332 // GDK_INTERP_NEAREST is the lowest-quality method for continuous-tone
333 // images, but the only one which preserves sharp edges
335 pixbuf
, pixbuf_scaled
,
336 0, 0, width
, height
, -clipx
, -clipy
, scale_x
, scale_y
,
339 g_object_unref(pixbuf
);
340 bmp
.SetPixbuf(pixbuf_scaled
, M_BMPDATA
->m_bpp
);
342 if (M_BMPDATA
->m_mask
)
344 pixbuf
= gdk_pixbuf_get_from_drawable(
345 NULL
, M_BMPDATA
->m_mask
->m_bitmap
, NULL
,
346 0, 0, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
348 pixbuf_scaled
= gdk_pixbuf_new(
349 GDK_COLORSPACE_RGB
, false, 8, width
, height
);
352 pixbuf
, pixbuf_scaled
,
353 0, 0, width
, height
, -clipx
, -clipy
, scale_x
, scale_y
,
356 g_object_unref(pixbuf
);
358 // use existing functionality to create mask from scaled pixbuf
360 maskbmp
.SetPixbuf(pixbuf_scaled
);
361 bmp
.SetMask(new wxMask(maskbmp
, *wxBLACK
));
366 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
370 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
371 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
373 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
376 // create pixbuf if image has alpha and requested depth is compatible
377 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
378 return CreateFromImageAsPixbuf(image
);
380 // otherwise create pixmap, if alpha is present it will be converted to mask
381 return CreateFromImageAsPixmap(image
, depth
);
384 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
386 const int w
= image
.GetWidth();
387 const int h
= image
.GetHeight();
390 // create XBM format bitmap
392 // one bit per pixel, each row starts on a byte boundary
393 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
394 wxByte
* out
= new wxByte
[out_size
];
395 // set bits are black
396 memset(out
, 0xff, out_size
);
397 const wxByte
* in
= image
.GetData();
398 unsigned bit_index
= 0;
399 for (int y
= 0; y
< h
; y
++)
401 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
402 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
403 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
404 // move index to next byte boundary
405 bit_index
= (bit_index
+ 7) & ~7u;
407 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
412 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
413 GdkGC
* gc
= gdk_gc_new(M_BMPDATA
->m_pixmap
);
415 M_BMPDATA
->m_pixmap
, gc
,
417 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
421 const wxByte
* alpha
= image
.GetAlpha();
422 if (alpha
!= NULL
|| image
.HasMask())
424 // create mask as XBM format bitmap
426 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
427 wxByte
* out
= new wxByte
[out_size
];
428 memset(out
, 0xff, out_size
);
429 unsigned bit_index
= 0;
432 for (int y
= 0; y
< h
; y
++)
434 for (int x
= 0; x
< w
; x
++, bit_index
++)
435 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
436 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
437 bit_index
= (bit_index
+ 7) & ~7u;
442 const wxByte r_mask
= image
.GetMaskRed();
443 const wxByte g_mask
= image
.GetMaskGreen();
444 const wxByte b_mask
= image
.GetMaskBlue();
445 const wxByte
* in
= image
.GetData();
446 for (int y
= 0; y
< h
; y
++)
448 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
449 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
450 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
451 bit_index
= (bit_index
+ 7) & ~7u;
454 wxMask
* mask
= new wxMask
;
455 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
462 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
464 wxASSERT(image
.HasAlpha());
466 int width
= image
.GetWidth();
467 int height
= image
.GetHeight();
469 Create(width
, height
, 32);
470 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
475 const unsigned char* in
= image
.GetData();
476 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
477 unsigned char *alpha
= image
.GetAlpha();
479 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
481 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
483 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
495 wxImage
wxBitmap::ConvertToImage() const
497 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
499 const int w
= GetWidth();
500 const int h
= GetHeight();
501 wxImage
image(w
, h
, false);
502 unsigned char *data
= image
.GetData();
504 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
506 // prefer pixbuf if available, it will preserve alpha and should be quicker
509 GdkPixbuf
*pixbuf
= GetPixbuf();
510 unsigned char* alpha
= NULL
;
511 if (gdk_pixbuf_get_has_alpha(pixbuf
))
514 alpha
= image
.GetAlpha();
516 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
517 unsigned char *out
= data
;
518 const int inc
= 3 + int(alpha
!= NULL
);
519 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
521 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
523 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
535 GdkPixmap
* pixmap
= GetPixmap();
536 GdkPixmap
* pixmap_invert
= NULL
;
539 // mono bitmaps are inverted, i.e. 0 is white
540 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
541 GdkGC
* gc
= gdk_gc_new(pixmap_invert
);
542 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
543 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
545 pixmap
= pixmap_invert
;
547 // create a pixbuf which shares data with the wxImage
548 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
549 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
551 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
553 g_object_unref(pixbuf
);
554 if (pixmap_invert
!= NULL
)
555 g_object_unref(pixmap_invert
);
557 // convert mask, unless there is already alpha
558 if (GetMask() && !image
.HasAlpha())
560 // we hard code the mask colour for now but we could also make an
561 // effort (and waste time) to choose a colour not present in the
562 // image already to avoid having to fudge the pixels below --
563 // whether it's worth to do it is unclear however
564 const int MASK_RED
= 1;
565 const int MASK_GREEN
= 2;
566 const int MASK_BLUE
= 3;
567 const int MASK_BLUE_REPLACEMENT
= 2;
569 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
570 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
572 for (int y
= 0; y
< h
; y
++)
574 for (int x
= 0; x
< w
; x
++, data
+= 3)
576 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
579 data
[1] = MASK_GREEN
;
582 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
584 // we have to fudge the colour a bit to prevent
585 // this pixel from appearing transparent
586 data
[2] = MASK_BLUE_REPLACEMENT
;
590 g_object_unref(image_mask
);
596 bool wxBitmap::IsOk() const
598 return (m_refData
!= NULL
) &&
600 M_BMPDATA
->m_pixbuf
||
605 int wxBitmap::GetHeight() const
607 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
609 return M_BMPDATA
->m_height
;
612 int wxBitmap::GetWidth() const
614 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
616 return M_BMPDATA
->m_width
;
619 int wxBitmap::GetDepth() const
621 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
623 return M_BMPDATA
->m_bpp
;
626 wxMask
*wxBitmap::GetMask() const
628 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
630 return M_BMPDATA
->m_mask
;
633 void wxBitmap::SetMask( wxMask
*mask
)
635 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
638 delete M_BMPDATA
->m_mask
;
639 M_BMPDATA
->m_mask
= mask
;
642 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
648 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
652 wxCHECK_MSG(Ok(), ret
, wxT("invalid bitmap"));
653 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
654 rect
.x
+ rect
.width
<= M_BMPDATA
->m_width
&&
655 rect
.y
+ rect
.height
<= M_BMPDATA
->m_height
,
656 ret
, wxT("invalid bitmap region"));
658 if (HasPixbuf() || M_BMPDATA
->m_bpp
== 32)
660 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
661 gdk_pixbuf_get_has_alpha(GetPixbuf()),
662 8, rect
.width
, rect
.height
);
663 ret
.SetPixbuf(pixbuf
, M_BMPDATA
->m_bpp
);
664 gdk_pixbuf_copy_area(GetPixbuf(),
665 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
670 ret
.Create(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
671 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
672 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
675 // make mask, unless there is already alpha
676 if (GetMask() && !HasAlpha())
678 wxMask
*mask
= new wxMask
;
679 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
681 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
682 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
691 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
693 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
695 // Try to save the bitmap via wxImage handlers:
696 wxImage image
= ConvertToImage();
697 return image
.Ok() && image
.SaveFile(name
, type
);
700 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
704 if (type
== wxBITMAP_TYPE_XPM
)
706 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
707 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
711 M_BMPDATA
->m_mask
= new wxMask
;
712 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
715 else // try if wxImage can load it
718 if (image
.LoadFile(name
, type
) && image
.Ok())
719 CreateFromImage(image
, -1);
726 wxPalette
*wxBitmap::GetPalette() const
728 wxCHECK_MSG(Ok(), NULL
, wxT("invalid bitmap"));
730 return M_BMPDATA
->m_palette
;
733 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
737 #endif // wxUSE_PALETTE
739 void wxBitmap::SetHeight( int height
)
742 M_BMPDATA
->m_height
= height
;
745 void wxBitmap::SetWidth( int width
)
748 M_BMPDATA
->m_width
= width
;
751 void wxBitmap::SetDepth( int depth
)
754 M_BMPDATA
->m_bpp
= depth
;
757 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
760 m_refData
= new wxBitmapRefData
;
762 // AllocExclusive should not be needed for this internal function
763 wxASSERT(m_refData
->GetRefCount() == 1);
764 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
765 M_BMPDATA
->m_pixmap
= pixmap
;
766 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
767 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
768 PurgeOtherRepresentations(Pixmap
);
771 GdkPixmap
*wxBitmap::GetPixmap() const
773 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
775 // create the pixmap on the fly if we use Pixbuf representation:
776 if (M_BMPDATA
->m_pixmap
== NULL
)
778 GdkPixmap
** pmask
= NULL
;
779 if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
))
781 // make new mask from alpha
782 delete M_BMPDATA
->m_mask
;
783 M_BMPDATA
->m_mask
= new wxMask
;
784 pmask
= &M_BMPDATA
->m_mask
->m_bitmap
;
786 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
787 &M_BMPDATA
->m_pixmap
,
789 wxIMAGE_ALPHA_THRESHOLD
);
792 return M_BMPDATA
->m_pixmap
;
795 bool wxBitmap::HasPixmap() const
797 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
799 return M_BMPDATA
->m_pixmap
!= NULL
;
802 GdkPixbuf
*wxBitmap::GetPixbuf() const
804 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
806 if (M_BMPDATA
->m_pixbuf
== NULL
)
808 int width
= GetWidth();
809 int height
= GetHeight();
811 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
814 M_BMPDATA
->m_pixbuf
= pixbuf
;
815 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
816 0, 0, 0, 0, width
, height
);
818 // apply the mask to created pixbuf:
819 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
822 gdk_pixbuf_get_from_drawable(NULL
,
823 M_BMPDATA
->m_mask
->GetBitmap(),
825 0, 0, 0, 0, width
, height
);
828 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
829 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
830 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
831 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
833 for (int y
= 0; y
< height
;
834 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
836 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
838 if (mask
[0] == 0 /*black pixel*/)
843 g_object_unref (pmask
);
848 return M_BMPDATA
->m_pixbuf
;
851 bool wxBitmap::HasPixbuf() const
853 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
855 return M_BMPDATA
->m_pixbuf
!= NULL
;
858 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
, int depth
)
861 m_refData
= new wxBitmapRefData
;
863 // AllocExclusive should not be needed for this internal function
864 wxASSERT(m_refData
->GetRefCount() == 1);
865 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
866 M_BMPDATA
->m_pixbuf
= pixbuf
;
867 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
868 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
869 // if depth specified
871 M_BMPDATA
->m_bpp
= depth
;
872 else if (M_BMPDATA
->m_bpp
== 0)
873 // use something reasonable
874 M_BMPDATA
->m_bpp
= wxTheApp
->GetGdkVisual()->depth
;
875 PurgeOtherRepresentations(Pixbuf
);
878 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
880 if (keep
== Pixmap
&& HasPixbuf())
882 g_object_unref (M_BMPDATA
->m_pixbuf
);
883 M_BMPDATA
->m_pixbuf
= NULL
;
885 if (keep
== Pixbuf
&& HasPixmap())
887 g_object_unref (M_BMPDATA
->m_pixmap
);
888 M_BMPDATA
->m_pixmap
= NULL
;
892 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
895 GdkPixbuf
*pixbuf
= GetPixbuf();
896 const bool hasAlpha
= HasAlpha();
897 // allow access if bpp is valid and matches existence of alpha
898 if (pixbuf
!= NULL
&& (
899 bpp
== 24 && !hasAlpha
||
900 bpp
== 32 && hasAlpha
))
902 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
903 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
904 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
905 bits
= gdk_pixbuf_get_pixels(pixbuf
);
910 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
914 bool wxBitmap::HasAlpha() const
916 return m_refData
!= NULL
&& M_BMPDATA
->m_pixbuf
!= NULL
&&
917 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
);
920 void wxBitmap::UseAlpha()
922 GdkPixbuf
* pixbuf
= GetPixbuf();
923 // add alpha if necessary
924 if (!gdk_pixbuf_get_has_alpha(pixbuf
))
926 M_BMPDATA
->m_pixbuf
= NULL
;
928 M_BMPDATA
->m_pixbuf
= gdk_pixbuf_add_alpha(pixbuf
, false, 0, 0, 0);
929 g_object_unref(pixbuf
);
933 wxObjectRefData
* wxBitmap::CreateRefData() const
935 return new wxBitmapRefData
;
938 wxObjectRefData
* wxBitmap::CloneRefData(const wxObjectRefData
* data
) const
940 const wxBitmapRefData
* oldRef
= wx_static_cast(const wxBitmapRefData
*, data
);
941 wxBitmapRefData
* newRef
= new wxBitmapRefData
;
942 newRef
->m_width
= oldRef
->m_width
;
943 newRef
->m_height
= oldRef
->m_height
;
944 newRef
->m_bpp
= oldRef
->m_bpp
;
945 if (oldRef
->m_pixmap
!= NULL
)
947 newRef
->m_pixmap
= gdk_pixmap_new(
948 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
949 // use pixmap depth, m_bpp may not match
950 gdk_drawable_get_depth(oldRef
->m_pixmap
));
951 GdkGC
* gc
= gdk_gc_new(newRef
->m_pixmap
);
953 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
956 if (oldRef
->m_pixbuf
!= NULL
)
958 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
960 if (oldRef
->m_mask
!= NULL
)
962 newRef
->m_mask
= new wxMask
;
963 newRef
->m_mask
->m_bitmap
= gdk_pixmap_new(
964 oldRef
->m_mask
->m_bitmap
, oldRef
->m_width
, oldRef
->m_height
, 1);
965 GdkGC
* gc
= gdk_gc_new(newRef
->m_mask
->m_bitmap
);
966 gdk_draw_drawable(newRef
->m_mask
->m_bitmap
,
967 gc
, oldRef
->m_mask
->m_bitmap
, 0, 0, 0, 0, -1, -1);
971 // implement this if SetPalette is ever implemented
972 wxASSERT(oldRef
->m_palette
== NULL
);
978 //-----------------------------------------------------------------------------
980 //-----------------------------------------------------------------------------
982 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
984 /* static */ void wxBitmap::InitStandardHandlers()
986 // TODO: Insert handler based on GdkPixbufs handler later