]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
ede32fc4430f8c8a8e4cef127580d3da1467a1db
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 )
283 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
287 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
), 32);
291 if (visual
->depth
== depth
)
292 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
294 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8, width
, height
), 24);
301 depth
= visual
->depth
;
303 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
309 wxBitmap
wxBitmap::Rescale(int clipx
, int clipy
, int clipwidth
, int clipheight
,
310 int width
, int height
) const
314 wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap"));
316 width
= wxMax(width
, 1);
317 height
= wxMax(height
, 1);
319 const double scale_x
= double(width
) / clipwidth
;
320 const double scale_y
= double(height
) / clipheight
;
322 // Converting to pixbuf, scaling with gdk_pixbuf_scale, and converting
323 // back, is faster than scaling pixmap ourselves.
325 // use pixbuf if already available,
326 // otherwise create temporary pixbuf from pixmap
327 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
329 g_object_ref(pixbuf
);
331 pixbuf
= gdk_pixbuf_get_from_drawable(
332 NULL
, M_BMPDATA
->m_pixmap
, NULL
,
333 0, 0, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
335 // new pixbuf for scaled wxBitmap
336 GdkPixbuf
* pixbuf_scaled
= gdk_pixbuf_new(
337 GDK_COLORSPACE_RGB
, gdk_pixbuf_get_has_alpha(pixbuf
), 8, width
, height
);
339 // GDK_INTERP_NEAREST is the lowest-quality method for continuous-tone
340 // images, but the only one which preserves sharp edges
342 pixbuf
, pixbuf_scaled
,
343 0, 0, width
, height
, -clipx
*scale_x
, -clipy
*scale_y
, scale_x
, scale_y
,
346 g_object_unref(pixbuf
);
347 bmp
.SetPixbuf(pixbuf_scaled
, M_BMPDATA
->m_bpp
);
349 if (M_BMPDATA
->m_mask
)
351 pixbuf
= gdk_pixbuf_get_from_drawable(
352 NULL
, M_BMPDATA
->m_mask
->m_bitmap
, NULL
,
353 0, 0, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
355 pixbuf_scaled
= gdk_pixbuf_new(
356 GDK_COLORSPACE_RGB
, false, 8, width
, height
);
359 pixbuf
, pixbuf_scaled
,
360 0, 0, width
, height
, -clipx
, -clipy
, scale_x
, scale_y
,
363 g_object_unref(pixbuf
);
365 // use existing functionality to create mask from scaled pixbuf
367 maskbmp
.SetPixbuf(pixbuf_scaled
);
368 bmp
.SetMask(new wxMask(maskbmp
, *wxBLACK
));
375 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
379 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
380 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
382 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
385 // create pixbuf if image has alpha and requested depth is compatible
386 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
387 return CreateFromImageAsPixbuf(image
);
389 // otherwise create pixmap, if alpha is present it will be converted to mask
390 return CreateFromImageAsPixmap(image
, depth
);
393 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
395 const int w
= image
.GetWidth();
396 const int h
= image
.GetHeight();
399 // create XBM format bitmap
401 // one bit per pixel, each row starts on a byte boundary
402 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
403 wxByte
* out
= new wxByte
[out_size
];
404 // set bits are black
405 memset(out
, 0xff, out_size
);
406 const wxByte
* in
= image
.GetData();
407 unsigned bit_index
= 0;
408 for (int y
= 0; y
< h
; y
++)
410 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
411 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
412 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
413 // move index to next byte boundary
414 bit_index
= (bit_index
+ 7) & ~7u;
416 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
421 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
422 wxGtkObject
<GdkGC
> gc(gdk_gc_new(M_BMPDATA
->m_pixmap
));
424 M_BMPDATA
->m_pixmap
, gc
,
426 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
429 const wxByte
* alpha
= image
.GetAlpha();
430 if (alpha
!= NULL
|| image
.HasMask())
432 // create mask as XBM format bitmap
434 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
435 wxByte
* out
= new wxByte
[out_size
];
436 memset(out
, 0xff, out_size
);
437 unsigned bit_index
= 0;
440 for (int y
= 0; y
< h
; y
++)
442 for (int x
= 0; x
< w
; x
++, bit_index
++)
443 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
444 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
445 bit_index
= (bit_index
+ 7) & ~7u;
450 const wxByte r_mask
= image
.GetMaskRed();
451 const wxByte g_mask
= image
.GetMaskGreen();
452 const wxByte b_mask
= image
.GetMaskBlue();
453 const wxByte
* in
= image
.GetData();
454 for (int y
= 0; y
< h
; y
++)
456 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
457 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
458 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
459 bit_index
= (bit_index
+ 7) & ~7u;
462 wxMask
* mask
= new wxMask
;
463 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
470 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
472 wxASSERT(image
.HasAlpha());
474 int width
= image
.GetWidth();
475 int height
= image
.GetHeight();
477 Create(width
, height
, 32);
478 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
483 const unsigned char* in
= image
.GetData();
484 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
485 unsigned char *alpha
= image
.GetAlpha();
487 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
489 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
491 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
503 wxImage
wxBitmap::ConvertToImage() const
505 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
507 const int w
= GetWidth();
508 const int h
= GetHeight();
509 wxImage
image(w
, h
, false);
510 unsigned char *data
= image
.GetData();
512 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
514 // prefer pixbuf if available, it will preserve alpha and should be quicker
517 GdkPixbuf
*pixbuf
= GetPixbuf();
518 unsigned char* alpha
= NULL
;
519 if (gdk_pixbuf_get_has_alpha(pixbuf
))
522 alpha
= image
.GetAlpha();
524 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
525 unsigned char *out
= data
;
526 const int inc
= 3 + int(alpha
!= NULL
);
527 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
529 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
531 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
543 GdkPixmap
* pixmap
= GetPixmap();
544 GdkPixmap
* pixmap_invert
= NULL
;
547 // mono bitmaps are inverted, i.e. 0 is white
548 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
549 wxGtkObject
<GdkGC
> gc(gdk_gc_new(pixmap_invert
));
550 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
551 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
552 pixmap
= pixmap_invert
;
554 // create a pixbuf which shares data with the wxImage
555 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
556 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
558 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
560 g_object_unref(pixbuf
);
561 if (pixmap_invert
!= NULL
)
562 g_object_unref(pixmap_invert
);
564 // convert mask, unless there is already alpha
565 if (GetMask() && !image
.HasAlpha())
567 // we hard code the mask colour for now but we could also make an
568 // effort (and waste time) to choose a colour not present in the
569 // image already to avoid having to fudge the pixels below --
570 // whether it's worth to do it is unclear however
571 const int MASK_RED
= 1;
572 const int MASK_GREEN
= 2;
573 const int MASK_BLUE
= 3;
574 const int MASK_BLUE_REPLACEMENT
= 2;
576 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
577 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
579 for (int y
= 0; y
< h
; y
++)
581 for (int x
= 0; x
< w
; x
++, data
+= 3)
583 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
586 data
[1] = MASK_GREEN
;
589 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
591 // we have to fudge the colour a bit to prevent
592 // this pixel from appearing transparent
593 data
[2] = MASK_BLUE_REPLACEMENT
;
597 g_object_unref(image_mask
);
603 #endif // wxUSE_IMAGE
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 wxGtkObject
<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
);
674 // make mask, unless there is already alpha
675 if (GetMask() && !HasAlpha())
677 wxMask
*mask
= new wxMask
;
678 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
680 wxGtkObject
<GdkGC
> gc(gdk_gc_new( mask
->m_bitmap
));
681 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
689 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
691 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
694 // Try to save the bitmap via wxImage handlers:
695 wxImage image
= ConvertToImage();
696 return image
.Ok() && image
.SaveFile(name
, type
);
697 #else // !wxUSE_IMAGE
702 #endif // wxUSE_IMAGE
705 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
709 if (type
== wxBITMAP_TYPE_XPM
)
711 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
712 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
716 M_BMPDATA
->m_mask
= new wxMask
;
717 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
721 else // try if wxImage can load it
724 if (image
.LoadFile(name
, type
) && image
.Ok())
725 CreateFromImage(image
, -1);
727 #endif // wxUSE_IMAGE
733 wxPalette
*wxBitmap::GetPalette() const
735 wxCHECK_MSG(Ok(), NULL
, wxT("invalid bitmap"));
737 return M_BMPDATA
->m_palette
;
740 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
744 #endif // wxUSE_PALETTE
746 void wxBitmap::SetHeight( int height
)
749 M_BMPDATA
->m_height
= height
;
752 void wxBitmap::SetWidth( int width
)
755 M_BMPDATA
->m_width
= width
;
758 void wxBitmap::SetDepth( int depth
)
761 M_BMPDATA
->m_bpp
= depth
;
764 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
767 m_refData
= new wxBitmapRefData
;
769 // AllocExclusive should not be needed for this internal function
770 wxASSERT(m_refData
->GetRefCount() == 1);
771 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
772 M_BMPDATA
->m_pixmap
= pixmap
;
773 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
774 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
775 PurgeOtherRepresentations(Pixmap
);
778 GdkPixmap
*wxBitmap::GetPixmap() const
780 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
782 // create the pixmap on the fly if we use Pixbuf representation:
783 if (M_BMPDATA
->m_pixmap
== NULL
)
785 GdkPixmap
** pmask
= NULL
;
786 if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
))
788 // make new mask from alpha
789 delete M_BMPDATA
->m_mask
;
790 M_BMPDATA
->m_mask
= new wxMask
;
791 pmask
= &M_BMPDATA
->m_mask
->m_bitmap
;
793 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
794 &M_BMPDATA
->m_pixmap
,
796 0x80 /* alpha threshold */);
799 return M_BMPDATA
->m_pixmap
;
802 bool wxBitmap::HasPixmap() const
804 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
806 return M_BMPDATA
->m_pixmap
!= NULL
;
809 GdkPixbuf
*wxBitmap::GetPixbuf() const
811 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
813 if (M_BMPDATA
->m_pixbuf
== NULL
)
816 int width
= GetWidth();
817 int height
= GetHeight();
819 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
822 M_BMPDATA
->m_pixbuf
= pixbuf
;
823 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
824 0, 0, 0, 0, width
, height
);
825 // apply the mask to created pixbuf:
826 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
829 gdk_pixbuf_get_from_drawable(NULL
,
830 M_BMPDATA
->m_mask
->GetBitmap(),
832 0, 0, 0, 0, width
, height
);
835 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
836 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
837 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
838 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
840 for (int y
= 0; y
< height
;
841 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
843 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
845 if (mask
[0] == 0 /*black pixel*/)
850 g_object_unref (pmask
);
855 return M_BMPDATA
->m_pixbuf
;
858 bool wxBitmap::HasPixbuf() const
860 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
862 return M_BMPDATA
->m_pixbuf
!= NULL
;
865 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
, int depth
)
868 m_refData
= new wxBitmapRefData
;
870 // AllocExclusive should not be needed for this internal function
871 wxASSERT(m_refData
->GetRefCount() == 1);
872 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
873 M_BMPDATA
->m_pixbuf
= pixbuf
;
874 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
875 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
876 // if depth specified
878 M_BMPDATA
->m_bpp
= depth
;
879 else if (M_BMPDATA
->m_bpp
== 0)
880 // use something reasonable
881 M_BMPDATA
->m_bpp
= wxTheApp
->GetGdkVisual()->depth
;
882 PurgeOtherRepresentations(Pixbuf
);
885 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
887 if (keep
== Pixmap
&& HasPixbuf())
889 g_object_unref (M_BMPDATA
->m_pixbuf
);
890 M_BMPDATA
->m_pixbuf
= NULL
;
892 if (keep
== Pixbuf
&& HasPixmap())
894 g_object_unref (M_BMPDATA
->m_pixmap
);
895 M_BMPDATA
->m_pixmap
= NULL
;
899 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
902 GdkPixbuf
*pixbuf
= GetPixbuf();
903 const bool hasAlpha
= HasAlpha();
905 // allow access if bpp is valid and matches existence of alpha
906 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (bpp
== 32 && hasAlpha
)) )
908 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
909 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
910 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
911 bits
= gdk_pixbuf_get_pixels(pixbuf
);
916 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
920 bool wxBitmap::HasAlpha() const
922 return m_refData
!= NULL
&& M_BMPDATA
->m_pixbuf
!= NULL
&&
923 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
);
926 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
928 return new wxBitmapRefData
;
931 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
933 const wxBitmapRefData
* oldRef
= wx_static_cast(const wxBitmapRefData
*, data
);
934 wxBitmapRefData
* newRef
= new wxBitmapRefData
;
935 newRef
->m_width
= oldRef
->m_width
;
936 newRef
->m_height
= oldRef
->m_height
;
937 newRef
->m_bpp
= oldRef
->m_bpp
;
938 if (oldRef
->m_pixmap
!= NULL
)
940 newRef
->m_pixmap
= gdk_pixmap_new(
941 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
942 // use pixmap depth, m_bpp may not match
943 gdk_drawable_get_depth(oldRef
->m_pixmap
));
944 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_pixmap
));
946 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
948 if (oldRef
->m_pixbuf
!= NULL
)
950 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
952 if (oldRef
->m_mask
!= NULL
)
954 newRef
->m_mask
= new wxMask
;
955 newRef
->m_mask
->m_bitmap
= gdk_pixmap_new(
956 oldRef
->m_mask
->m_bitmap
, oldRef
->m_width
, oldRef
->m_height
, 1);
957 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_mask
->m_bitmap
));
958 gdk_draw_drawable(newRef
->m_mask
->m_bitmap
,
959 gc
, oldRef
->m_mask
->m_bitmap
, 0, 0, 0, 0, -1, -1);
962 // implement this if SetPalette is ever implemented
963 wxASSERT(oldRef
->m_palette
== NULL
);
969 /* static */ void wxBitmap::InitStandardHandlers()
971 // TODO: Insert handler based on GdkPixbufs handler later