]>
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"
26 #include "wx/gtk/private/object.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern GtkWidget
*wxGetRootWindow();
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
44 m_bitmap
= (GdkBitmap
*) NULL
;
47 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
49 m_bitmap
= (GdkBitmap
*) NULL
;
50 Create( bitmap
, colour
);
54 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
56 m_bitmap
= (GdkBitmap
*) NULL
;
57 Create( bitmap
, paletteIndex
);
59 #endif // wxUSE_PALETTE
61 wxMask::wxMask( const wxBitmap
& bitmap
)
63 m_bitmap
= (GdkBitmap
*) NULL
;
70 g_object_unref (m_bitmap
);
73 bool wxMask::Create( const wxBitmap
& bitmap
,
74 const wxColour
& colour
)
78 g_object_unref (m_bitmap
);
79 m_bitmap
= (GdkBitmap
*) NULL
;
82 const int w
= bitmap
.GetWidth();
83 const int h
= bitmap
.GetHeight();
85 // create mask as XBM format bitmap
87 // one bit per pixel, each row starts on a byte boundary
88 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
89 wxByte
* out
= new wxByte
[out_size
];
90 // set bits are unmasked
91 memset(out
, 0xff, out_size
);
92 unsigned bit_index
= 0;
93 if (bitmap
.HasPixbuf())
95 const wxByte r_mask
= colour
.Red();
96 const wxByte g_mask
= colour
.Green();
97 const wxByte b_mask
= colour
.Blue();
98 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
99 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
100 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
101 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
102 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
104 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
105 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
106 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
107 // move index to next byte boundary
108 bit_index
= (bit_index
+ 7) & ~7u;
113 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
114 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
116 if (colormap
== NULL
)
117 // mono bitmap, white is pixel value 0
118 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
122 c
.CalcPixel(colormap
);
123 mask_pixel
= c
.GetPixel();
125 for (int y
= 0; y
< h
; y
++)
127 for (int x
= 0; x
< w
; x
++, bit_index
++)
128 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
129 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
130 bit_index
= (bit_index
+ 7) & ~7u;
132 g_object_unref(image
);
134 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
140 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
143 wxPalette
*pal
= bitmap
.GetPalette();
145 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
147 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
149 return Create(bitmap
, wxColour(r
, g
, b
));
151 #endif // wxUSE_PALETTE
153 bool wxMask::Create( const wxBitmap
& bitmap
)
157 g_object_unref (m_bitmap
);
158 m_bitmap
= (GdkBitmap
*) NULL
;
161 if (!bitmap
.Ok()) return false;
163 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
165 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
167 if (!m_bitmap
) return false;
169 wxGtkObject
<GdkGC
> gc(gdk_gc_new( m_bitmap
));
170 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
171 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
176 GdkBitmap
*wxMask::GetBitmap() const
181 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
185 class wxBitmapRefData
: public wxGDIRefData
189 virtual ~wxBitmapRefData();
191 virtual bool IsOk() const { return m_pixmap
|| m_pixbuf
; }
200 wxPalette
*m_palette
;
201 #endif // wxUSE_PALETTE
204 wxBitmapRefData::wxBitmapRefData()
206 m_pixmap
= (GdkPixmap
*) NULL
;
207 m_pixbuf
= (GdkPixbuf
*) NULL
;
208 m_mask
= (wxMask
*) NULL
;
213 m_palette
= (wxPalette
*) NULL
;
214 #endif // wxUSE_PALETTE
217 wxBitmapRefData::~wxBitmapRefData()
220 g_object_unref (m_pixmap
);
222 g_object_unref (m_pixbuf
);
226 #endif // wxUSE_PALETTE
229 //-----------------------------------------------------------------------------
231 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
233 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
235 wxBitmap::wxBitmap(int width
, int height
, int depth
)
237 Create(width
, height
, depth
);
240 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
242 LoadFile(filename
, type
);
245 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
247 wxASSERT(depth
== 1);
248 if (width
> 0 && height
> 0 && depth
== 1)
250 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
252 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
256 wxBitmap::wxBitmap(const char* const* bits
)
258 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
260 GdkBitmap
* mask
= NULL
;
261 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, wx_const_cast(char**, bits
)));
263 if (M_BMPDATA
->m_pixmap
!= NULL
&& mask
!= NULL
)
265 M_BMPDATA
->m_mask
= new wxMask
;
266 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
270 wxBitmap::~wxBitmap()
274 bool wxBitmap::Create( int width
, int height
, int depth
)
278 if ( width
<= 0 || height
<= 0 )
285 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
), 32);
291 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
293 depth
= visual
->depth
;
295 wxCHECK_MSG(depth
== visual
->depth
, false, wxT("invalid bitmap depth"));
298 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
304 wxBitmap
wxBitmap::Rescale(int clipx
, int clipy
, int clipwidth
, int clipheight
,
305 int width
, int height
) const
309 wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap"));
311 width
= wxMax(width
, 1);
312 height
= wxMax(height
, 1);
314 const double scale_x
= double(width
) / clipwidth
;
315 const double scale_y
= double(height
) / clipheight
;
317 // Converting to pixbuf, scaling with gdk_pixbuf_scale, and converting
318 // back, is faster than scaling pixmap ourselves.
320 // use pixbuf if already available,
321 // otherwise create temporary pixbuf from pixmap
322 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
324 g_object_ref(pixbuf
);
326 pixbuf
= gdk_pixbuf_get_from_drawable(
327 NULL
, M_BMPDATA
->m_pixmap
, NULL
,
328 0, 0, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
330 // new pixbuf for scaled wxBitmap
331 GdkPixbuf
* pixbuf_scaled
= gdk_pixbuf_new(
332 GDK_COLORSPACE_RGB
, gdk_pixbuf_get_has_alpha(pixbuf
), 8, width
, height
);
334 // GDK_INTERP_NEAREST is the lowest-quality method for continuous-tone
335 // images, but the only one which preserves sharp edges
337 pixbuf
, pixbuf_scaled
,
338 0, 0, width
, height
, -clipx
*scale_x
, -clipy
*scale_y
, scale_x
, scale_y
,
341 g_object_unref(pixbuf
);
342 bmp
.SetPixbuf(pixbuf_scaled
, M_BMPDATA
->m_bpp
);
344 if (M_BMPDATA
->m_mask
)
346 pixbuf
= gdk_pixbuf_get_from_drawable(
347 NULL
, M_BMPDATA
->m_mask
->m_bitmap
, NULL
,
348 0, 0, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
350 pixbuf_scaled
= gdk_pixbuf_new(
351 GDK_COLORSPACE_RGB
, false, 8, width
, height
);
354 pixbuf
, pixbuf_scaled
,
355 0, 0, width
, height
, -clipx
, -clipy
, scale_x
, scale_y
,
358 g_object_unref(pixbuf
);
360 // use existing functionality to create mask from scaled pixbuf
362 maskbmp
.SetPixbuf(pixbuf_scaled
);
363 bmp
.SetMask(new wxMask(maskbmp
, *wxBLACK
));
370 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
374 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
375 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
377 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
380 // create pixbuf if image has alpha and requested depth is compatible
381 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
382 return CreateFromImageAsPixbuf(image
);
384 // otherwise create pixmap, if alpha is present it will be converted to mask
385 return CreateFromImageAsPixmap(image
, depth
);
388 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
390 const int w
= image
.GetWidth();
391 const int h
= image
.GetHeight();
394 // create XBM format bitmap
396 // one bit per pixel, each row starts on a byte boundary
397 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
398 wxByte
* out
= new wxByte
[out_size
];
399 // set bits are black
400 memset(out
, 0xff, out_size
);
401 const wxByte
* in
= image
.GetData();
402 unsigned bit_index
= 0;
403 for (int y
= 0; y
< h
; y
++)
405 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
406 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
407 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
408 // move index to next byte boundary
409 bit_index
= (bit_index
+ 7) & ~7u;
411 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
416 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
417 wxGtkObject
<GdkGC
> gc(gdk_gc_new(M_BMPDATA
->m_pixmap
));
419 M_BMPDATA
->m_pixmap
, gc
,
421 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 wxGtkObject
<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
);
547 pixmap
= pixmap_invert
;
549 // create a pixbuf which shares data with the wxImage
550 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
551 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
553 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
555 g_object_unref(pixbuf
);
556 if (pixmap_invert
!= NULL
)
557 g_object_unref(pixmap_invert
);
559 // convert mask, unless there is already alpha
560 if (GetMask() && !image
.HasAlpha())
562 // we hard code the mask colour for now but we could also make an
563 // effort (and waste time) to choose a colour not present in the
564 // image already to avoid having to fudge the pixels below --
565 // whether it's worth to do it is unclear however
566 const int MASK_RED
= 1;
567 const int MASK_GREEN
= 2;
568 const int MASK_BLUE
= 3;
569 const int MASK_BLUE_REPLACEMENT
= 2;
571 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
572 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
574 for (int y
= 0; y
< h
; y
++)
576 for (int x
= 0; x
< w
; x
++, data
+= 3)
578 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
581 data
[1] = MASK_GREEN
;
584 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
586 // we have to fudge the colour a bit to prevent
587 // this pixel from appearing transparent
588 data
[2] = MASK_BLUE_REPLACEMENT
;
592 g_object_unref(image_mask
);
598 #endif // wxUSE_IMAGE
600 int wxBitmap::GetHeight() const
602 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
604 return M_BMPDATA
->m_height
;
607 int wxBitmap::GetWidth() const
609 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
611 return M_BMPDATA
->m_width
;
614 int wxBitmap::GetDepth() const
616 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
618 return M_BMPDATA
->m_bpp
;
621 wxMask
*wxBitmap::GetMask() const
623 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
625 return M_BMPDATA
->m_mask
;
628 void wxBitmap::SetMask( wxMask
*mask
)
630 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
633 delete M_BMPDATA
->m_mask
;
634 M_BMPDATA
->m_mask
= mask
;
637 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
643 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
647 wxCHECK_MSG(Ok(), ret
, wxT("invalid bitmap"));
648 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
649 rect
.x
+ rect
.width
<= M_BMPDATA
->m_width
&&
650 rect
.y
+ rect
.height
<= M_BMPDATA
->m_height
,
651 ret
, wxT("invalid bitmap region"));
653 if (HasPixbuf() || M_BMPDATA
->m_bpp
== 32)
655 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
656 gdk_pixbuf_get_has_alpha(GetPixbuf()),
657 8, rect
.width
, rect
.height
);
658 ret
.SetPixbuf(pixbuf
, M_BMPDATA
->m_bpp
);
659 gdk_pixbuf_copy_area(GetPixbuf(),
660 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
665 ret
.Create(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
666 wxGtkObject
<GdkGC
> gc(gdk_gc_new( ret
.GetPixmap() ));
667 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
669 // make mask, unless there is already alpha
670 if (GetMask() && !HasAlpha())
672 wxMask
*mask
= new wxMask
;
673 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
675 wxGtkObject
<GdkGC
> gc(gdk_gc_new( mask
->m_bitmap
));
676 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
684 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
686 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
689 // Try to save the bitmap via wxImage handlers:
690 wxImage image
= ConvertToImage();
691 return image
.Ok() && image
.SaveFile(name
, type
);
692 #else // !wxUSE_IMAGE
697 #endif // wxUSE_IMAGE
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
;
716 else // try if wxImage can load it
719 if (image
.LoadFile(name
, type
) && image
.Ok())
720 CreateFromImage(image
, -1);
722 #endif // wxUSE_IMAGE
728 wxPalette
*wxBitmap::GetPalette() const
730 wxCHECK_MSG(Ok(), NULL
, wxT("invalid bitmap"));
732 return M_BMPDATA
->m_palette
;
735 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
739 #endif // wxUSE_PALETTE
741 void wxBitmap::SetHeight( int height
)
744 M_BMPDATA
->m_height
= height
;
747 void wxBitmap::SetWidth( int width
)
750 M_BMPDATA
->m_width
= width
;
753 void wxBitmap::SetDepth( int depth
)
756 M_BMPDATA
->m_bpp
= depth
;
759 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
762 m_refData
= new wxBitmapRefData
;
764 // AllocExclusive should not be needed for this internal function
765 wxASSERT(m_refData
->GetRefCount() == 1);
766 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
767 M_BMPDATA
->m_pixmap
= pixmap
;
768 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
769 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
770 PurgeOtherRepresentations(Pixmap
);
773 GdkPixmap
*wxBitmap::GetPixmap() const
775 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
777 // create the pixmap on the fly if we use Pixbuf representation:
778 if (M_BMPDATA
->m_pixmap
== NULL
)
780 GdkPixmap
** pmask
= NULL
;
781 if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
))
783 // make new mask from alpha
784 delete M_BMPDATA
->m_mask
;
785 M_BMPDATA
->m_mask
= new wxMask
;
786 pmask
= &M_BMPDATA
->m_mask
->m_bitmap
;
788 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
789 &M_BMPDATA
->m_pixmap
,
791 0x80 /* alpha threshold */);
794 return M_BMPDATA
->m_pixmap
;
797 bool wxBitmap::HasPixmap() const
799 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
801 return M_BMPDATA
->m_pixmap
!= NULL
;
804 GdkPixbuf
*wxBitmap::GetPixbuf() const
806 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
808 if (M_BMPDATA
->m_pixbuf
== NULL
)
810 int width
= GetWidth();
811 int height
= GetHeight();
813 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
816 M_BMPDATA
->m_pixbuf
= pixbuf
;
817 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
818 0, 0, 0, 0, width
, height
);
820 // apply the mask to created pixbuf:
821 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
824 gdk_pixbuf_get_from_drawable(NULL
,
825 M_BMPDATA
->m_mask
->GetBitmap(),
827 0, 0, 0, 0, width
, height
);
830 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
831 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
832 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
833 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
835 for (int y
= 0; y
< height
;
836 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
838 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
840 if (mask
[0] == 0 /*black pixel*/)
845 g_object_unref (pmask
);
850 return M_BMPDATA
->m_pixbuf
;
853 bool wxBitmap::HasPixbuf() const
855 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
857 return M_BMPDATA
->m_pixbuf
!= NULL
;
860 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
, int depth
)
863 m_refData
= new wxBitmapRefData
;
865 // AllocExclusive should not be needed for this internal function
866 wxASSERT(m_refData
->GetRefCount() == 1);
867 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
868 M_BMPDATA
->m_pixbuf
= pixbuf
;
869 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
870 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
871 // if depth specified
873 M_BMPDATA
->m_bpp
= depth
;
874 else if (M_BMPDATA
->m_bpp
== 0)
875 // use something reasonable
876 M_BMPDATA
->m_bpp
= wxTheApp
->GetGdkVisual()->depth
;
877 PurgeOtherRepresentations(Pixbuf
);
880 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
882 if (keep
== Pixmap
&& HasPixbuf())
884 g_object_unref (M_BMPDATA
->m_pixbuf
);
885 M_BMPDATA
->m_pixbuf
= NULL
;
887 if (keep
== Pixbuf
&& HasPixmap())
889 g_object_unref (M_BMPDATA
->m_pixmap
);
890 M_BMPDATA
->m_pixmap
= NULL
;
894 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
897 GdkPixbuf
*pixbuf
= GetPixbuf();
898 const bool hasAlpha
= HasAlpha();
899 // allow access if bpp is valid and matches existence of alpha
900 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (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 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
922 return new wxBitmapRefData
;
925 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
927 const wxBitmapRefData
* oldRef
= wx_static_cast(const wxBitmapRefData
*, data
);
928 wxBitmapRefData
* newRef
= new wxBitmapRefData
;
929 newRef
->m_width
= oldRef
->m_width
;
930 newRef
->m_height
= oldRef
->m_height
;
931 newRef
->m_bpp
= oldRef
->m_bpp
;
932 if (oldRef
->m_pixmap
!= NULL
)
934 newRef
->m_pixmap
= gdk_pixmap_new(
935 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
936 // use pixmap depth, m_bpp may not match
937 gdk_drawable_get_depth(oldRef
->m_pixmap
));
938 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_pixmap
));
940 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
942 if (oldRef
->m_pixbuf
!= NULL
)
944 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
946 if (oldRef
->m_mask
!= NULL
)
948 newRef
->m_mask
= new wxMask
;
949 newRef
->m_mask
->m_bitmap
= gdk_pixmap_new(
950 oldRef
->m_mask
->m_bitmap
, oldRef
->m_width
, oldRef
->m_height
, 1);
951 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_mask
->m_bitmap
));
952 gdk_draw_drawable(newRef
->m_mask
->m_bitmap
,
953 gc
, oldRef
->m_mask
->m_bitmap
, 0, 0, 0, 0, -1, -1);
956 // implement this if SetPalette is ever implemented
957 wxASSERT(oldRef
->m_palette
== NULL
);
963 /* static */ void wxBitmap::InitStandardHandlers()
965 // TODO: Insert handler based on GdkPixbufs handler later