]>
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 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
, 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);
288 // must initialize alpha, otherwise GetPixmap()
289 // will create a mask out of garbage
290 gdk_pixbuf_fill(M_BMPDATA
->m_pixbuf
, 0x000000ff);
294 if (visual
->depth
== depth
)
295 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
297 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8, width
, height
), 24);
304 depth
= visual
->depth
;
306 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
312 wxBitmap
wxBitmap::Rescale(int clipx
, int clipy
, int clipwidth
, int clipheight
,
313 int width
, int height
) const
317 wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap"));
319 width
= wxMax(width
, 1);
320 height
= wxMax(height
, 1);
322 const double scale_x
= double(width
) / clipwidth
;
323 const double scale_y
= double(height
) / clipheight
;
325 // Converting to pixbuf, scaling with gdk_pixbuf_scale, and converting
326 // back, is faster than scaling pixmap ourselves.
328 // use pixbuf if already available,
329 // otherwise create temporary pixbuf from pixmap
330 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
332 g_object_ref(pixbuf
);
334 pixbuf
= gdk_pixbuf_get_from_drawable(
335 NULL
, M_BMPDATA
->m_pixmap
, NULL
,
336 0, 0, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
338 // new pixbuf for scaled wxBitmap
339 GdkPixbuf
* pixbuf_scaled
= gdk_pixbuf_new(
340 GDK_COLORSPACE_RGB
, gdk_pixbuf_get_has_alpha(pixbuf
), 8, width
, height
);
342 // GDK_INTERP_NEAREST is the lowest-quality method for continuous-tone
343 // images, but the only one which preserves sharp edges
345 pixbuf
, pixbuf_scaled
,
346 0, 0, width
, height
, -clipx
*scale_x
, -clipy
*scale_y
, scale_x
, scale_y
,
349 g_object_unref(pixbuf
);
350 bmp
.SetPixbuf(pixbuf_scaled
, M_BMPDATA
->m_bpp
);
352 if (M_BMPDATA
->m_mask
)
354 pixbuf
= gdk_pixbuf_get_from_drawable(
355 NULL
, M_BMPDATA
->m_mask
->m_bitmap
, NULL
,
356 0, 0, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
358 pixbuf_scaled
= gdk_pixbuf_new(
359 GDK_COLORSPACE_RGB
, false, 8, width
, height
);
362 pixbuf
, pixbuf_scaled
,
363 0, 0, width
, height
, -clipx
, -clipy
, scale_x
, scale_y
,
366 g_object_unref(pixbuf
);
368 // use existing functionality to create mask from scaled pixbuf
370 maskbmp
.SetPixbuf(pixbuf_scaled
);
371 bmp
.SetMask(new wxMask(maskbmp
, *wxBLACK
));
378 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
382 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
383 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
385 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
388 // create pixbuf if image has alpha and requested depth is compatible
389 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
390 return CreateFromImageAsPixbuf(image
);
392 // otherwise create pixmap, if alpha is present it will be converted to mask
393 return CreateFromImageAsPixmap(image
, depth
);
396 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
398 const int w
= image
.GetWidth();
399 const int h
= image
.GetHeight();
402 // create XBM format bitmap
404 // one bit per pixel, each row starts on a byte boundary
405 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
406 wxByte
* out
= new wxByte
[out_size
];
407 // set bits are black
408 memset(out
, 0xff, out_size
);
409 const wxByte
* in
= image
.GetData();
410 unsigned bit_index
= 0;
411 for (int y
= 0; y
< h
; y
++)
413 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
414 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
415 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
416 // move index to next byte boundary
417 bit_index
= (bit_index
+ 7) & ~7u;
419 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
424 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
425 wxGtkObject
<GdkGC
> gc(gdk_gc_new(M_BMPDATA
->m_pixmap
));
427 M_BMPDATA
->m_pixmap
, gc
,
429 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
432 const wxByte
* alpha
= image
.GetAlpha();
433 if (alpha
!= NULL
|| image
.HasMask())
435 // create mask as XBM format bitmap
437 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
438 wxByte
* out
= new wxByte
[out_size
];
439 memset(out
, 0xff, out_size
);
440 unsigned bit_index
= 0;
443 for (int y
= 0; y
< h
; y
++)
445 for (int x
= 0; x
< w
; x
++, bit_index
++)
446 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
447 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
448 bit_index
= (bit_index
+ 7) & ~7u;
453 const wxByte r_mask
= image
.GetMaskRed();
454 const wxByte g_mask
= image
.GetMaskGreen();
455 const wxByte b_mask
= image
.GetMaskBlue();
456 const wxByte
* in
= image
.GetData();
457 for (int y
= 0; y
< h
; y
++)
459 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
460 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
461 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
462 bit_index
= (bit_index
+ 7) & ~7u;
465 wxMask
* mask
= new wxMask
;
466 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
473 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
475 wxASSERT(image
.HasAlpha());
477 int width
= image
.GetWidth();
478 int height
= image
.GetHeight();
480 Create(width
, height
, 32);
481 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
486 const unsigned char* in
= image
.GetData();
487 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
488 unsigned char *alpha
= image
.GetAlpha();
490 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
492 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
494 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
506 wxImage
wxBitmap::ConvertToImage() const
508 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
510 const int w
= GetWidth();
511 const int h
= GetHeight();
512 wxImage
image(w
, h
, false);
513 unsigned char *data
= image
.GetData();
515 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
517 // prefer pixbuf if available, it will preserve alpha and should be quicker
520 GdkPixbuf
*pixbuf
= GetPixbuf();
521 unsigned char* alpha
= NULL
;
522 if (gdk_pixbuf_get_has_alpha(pixbuf
))
525 alpha
= image
.GetAlpha();
527 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
528 unsigned char *out
= data
;
529 const int inc
= 3 + int(alpha
!= NULL
);
530 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
532 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
534 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
546 GdkPixmap
* pixmap
= GetPixmap();
547 GdkPixmap
* pixmap_invert
= NULL
;
550 // mono bitmaps are inverted, i.e. 0 is white
551 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
552 wxGtkObject
<GdkGC
> gc(gdk_gc_new(pixmap_invert
));
553 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
554 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
555 pixmap
= pixmap_invert
;
557 // create a pixbuf which shares data with the wxImage
558 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
559 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
561 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
563 g_object_unref(pixbuf
);
564 if (pixmap_invert
!= NULL
)
565 g_object_unref(pixmap_invert
);
567 // convert mask, unless there is already alpha
568 if (GetMask() && !image
.HasAlpha())
570 // we hard code the mask colour for now but we could also make an
571 // effort (and waste time) to choose a colour not present in the
572 // image already to avoid having to fudge the pixels below --
573 // whether it's worth to do it is unclear however
574 const int MASK_RED
= 1;
575 const int MASK_GREEN
= 2;
576 const int MASK_BLUE
= 3;
577 const int MASK_BLUE_REPLACEMENT
= 2;
579 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
580 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
582 for (int y
= 0; y
< h
; y
++)
584 for (int x
= 0; x
< w
; x
++, data
+= 3)
586 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
589 data
[1] = MASK_GREEN
;
592 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
594 // we have to fudge the colour a bit to prevent
595 // this pixel from appearing transparent
596 data
[2] = MASK_BLUE_REPLACEMENT
;
600 g_object_unref(image_mask
);
606 #endif // wxUSE_IMAGE
608 int wxBitmap::GetHeight() const
610 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
612 return M_BMPDATA
->m_height
;
615 int wxBitmap::GetWidth() const
617 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
619 return M_BMPDATA
->m_width
;
622 int wxBitmap::GetDepth() const
624 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
626 return M_BMPDATA
->m_bpp
;
629 wxMask
*wxBitmap::GetMask() const
631 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
633 return M_BMPDATA
->m_mask
;
636 void wxBitmap::SetMask( wxMask
*mask
)
638 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
641 delete M_BMPDATA
->m_mask
;
642 M_BMPDATA
->m_mask
= mask
;
645 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
651 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
655 wxCHECK_MSG(Ok(), ret
, wxT("invalid bitmap"));
656 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
657 rect
.x
+ rect
.width
<= M_BMPDATA
->m_width
&&
658 rect
.y
+ rect
.height
<= M_BMPDATA
->m_height
,
659 ret
, wxT("invalid bitmap region"));
661 if (HasPixbuf() || M_BMPDATA
->m_bpp
== 32)
663 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
664 gdk_pixbuf_get_has_alpha(GetPixbuf()),
665 8, rect
.width
, rect
.height
);
666 ret
.SetPixbuf(pixbuf
, M_BMPDATA
->m_bpp
);
667 gdk_pixbuf_copy_area(GetPixbuf(),
668 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
673 ret
.Create(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
674 wxGtkObject
<GdkGC
> gc(gdk_gc_new( ret
.GetPixmap() ));
675 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
677 // make mask, unless there is already alpha
678 if (GetMask() && !HasAlpha())
680 wxMask
*mask
= new wxMask
;
681 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
683 wxGtkObject
<GdkGC
> gc(gdk_gc_new( mask
->m_bitmap
));
684 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
692 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
694 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
697 // Try to save the bitmap via wxImage handlers:
698 wxImage image
= ConvertToImage();
699 return image
.Ok() && image
.SaveFile(name
, type
);
700 #else // !wxUSE_IMAGE
705 #endif // wxUSE_IMAGE
708 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
712 if (type
== wxBITMAP_TYPE_XPM
)
714 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
715 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
719 M_BMPDATA
->m_mask
= new wxMask
;
720 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
724 else // try if wxImage can load it
727 if (image
.LoadFile(name
, type
) && image
.Ok())
728 CreateFromImage(image
, -1);
730 #endif // wxUSE_IMAGE
736 wxPalette
*wxBitmap::GetPalette() const
738 wxCHECK_MSG(Ok(), NULL
, wxT("invalid bitmap"));
740 return M_BMPDATA
->m_palette
;
743 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
747 #endif // wxUSE_PALETTE
749 void wxBitmap::SetHeight( int height
)
752 M_BMPDATA
->m_height
= height
;
755 void wxBitmap::SetWidth( int width
)
758 M_BMPDATA
->m_width
= width
;
761 void wxBitmap::SetDepth( int depth
)
764 M_BMPDATA
->m_bpp
= depth
;
767 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
770 m_refData
= new wxBitmapRefData
;
772 // AllocExclusive should not be needed for this internal function
773 wxASSERT(m_refData
->GetRefCount() == 1);
774 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
775 M_BMPDATA
->m_pixmap
= pixmap
;
776 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
777 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
778 PurgeOtherRepresentations(Pixmap
);
781 GdkPixmap
*wxBitmap::GetPixmap() const
783 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
785 // create the pixmap on the fly if we use Pixbuf representation:
786 if (M_BMPDATA
->m_pixmap
== NULL
)
788 GdkPixmap
** pmask
= NULL
;
789 if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
))
791 // make new mask from alpha
792 delete M_BMPDATA
->m_mask
;
793 M_BMPDATA
->m_mask
= new wxMask
;
794 pmask
= &M_BMPDATA
->m_mask
->m_bitmap
;
796 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
797 &M_BMPDATA
->m_pixmap
,
799 0x80 /* alpha threshold */);
802 return M_BMPDATA
->m_pixmap
;
805 bool wxBitmap::HasPixmap() const
807 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
809 return M_BMPDATA
->m_pixmap
!= NULL
;
812 GdkPixbuf
*wxBitmap::GetPixbuf() const
814 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
816 if (M_BMPDATA
->m_pixbuf
== NULL
)
819 int width
= GetWidth();
820 int height
= GetHeight();
822 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
825 M_BMPDATA
->m_pixbuf
= pixbuf
;
826 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
827 0, 0, 0, 0, width
, height
);
828 // apply the mask to created pixbuf:
829 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
832 gdk_pixbuf_get_from_drawable(NULL
,
833 M_BMPDATA
->m_mask
->GetBitmap(),
835 0, 0, 0, 0, width
, height
);
838 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
839 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
840 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
841 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
843 for (int y
= 0; y
< height
;
844 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
846 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
848 if (mask
[0] == 0 /*black pixel*/)
853 g_object_unref (pmask
);
858 return M_BMPDATA
->m_pixbuf
;
861 bool wxBitmap::HasPixbuf() const
863 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
865 return M_BMPDATA
->m_pixbuf
!= NULL
;
868 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
, int depth
)
871 m_refData
= new wxBitmapRefData
;
873 // AllocExclusive should not be needed for this internal function
874 wxASSERT(m_refData
->GetRefCount() == 1);
875 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
876 M_BMPDATA
->m_pixbuf
= pixbuf
;
877 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
878 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
879 // if depth specified
881 M_BMPDATA
->m_bpp
= depth
;
882 else if (M_BMPDATA
->m_bpp
== 0)
883 // use something reasonable
884 M_BMPDATA
->m_bpp
= wxTheApp
->GetGdkVisual()->depth
;
885 PurgeOtherRepresentations(Pixbuf
);
888 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
890 if (keep
== Pixmap
&& HasPixbuf())
892 g_object_unref (M_BMPDATA
->m_pixbuf
);
893 M_BMPDATA
->m_pixbuf
= NULL
;
895 if (keep
== Pixbuf
&& HasPixmap())
897 g_object_unref (M_BMPDATA
->m_pixmap
);
898 M_BMPDATA
->m_pixmap
= NULL
;
902 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
905 GdkPixbuf
*pixbuf
= GetPixbuf();
906 const bool hasAlpha
= HasAlpha();
908 // allow access if bpp is valid and matches existence of alpha
909 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (bpp
== 32 && hasAlpha
)) )
911 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
912 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
913 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
914 bits
= gdk_pixbuf_get_pixels(pixbuf
);
919 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
923 bool wxBitmap::HasAlpha() const
925 return m_refData
!= NULL
&& M_BMPDATA
->m_pixbuf
!= NULL
&&
926 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
);
929 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
931 return new wxBitmapRefData
;
934 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
936 const wxBitmapRefData
* oldRef
= static_cast<const wxBitmapRefData
*>(data
);
937 wxBitmapRefData
* newRef
= new wxBitmapRefData
;
938 newRef
->m_width
= oldRef
->m_width
;
939 newRef
->m_height
= oldRef
->m_height
;
940 newRef
->m_bpp
= oldRef
->m_bpp
;
941 if (oldRef
->m_pixmap
!= NULL
)
943 newRef
->m_pixmap
= gdk_pixmap_new(
944 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
945 // use pixmap depth, m_bpp may not match
946 gdk_drawable_get_depth(oldRef
->m_pixmap
));
947 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_pixmap
));
949 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
951 if (oldRef
->m_pixbuf
!= NULL
)
953 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
955 if (oldRef
->m_mask
!= NULL
)
957 newRef
->m_mask
= new wxMask
;
958 newRef
->m_mask
->m_bitmap
= gdk_pixmap_new(
959 oldRef
->m_mask
->m_bitmap
, oldRef
->m_width
, oldRef
->m_height
, 1);
960 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_mask
->m_bitmap
));
961 gdk_draw_drawable(newRef
->m_mask
->m_bitmap
,
962 gc
, oldRef
->m_mask
->m_bitmap
, 0, 0, 0, 0, -1, -1);
965 // implement this if SetPalette is ever implemented
966 wxASSERT(oldRef
->m_palette
== NULL
);
972 /* static */ void wxBitmap::InitStandardHandlers()
974 // TODO: Insert handler based on GdkPixbufs handler later