]>
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"
18 #include "wx/colour.h"
21 #include "wx/rawbmp.h"
23 #include "wx/gtk/private/object.h"
24 #include "wx/gtk/private.h"
28 extern GtkWidget
*wxGetRootWindow();
31 static void PixmapToPixbuf(GdkPixmap
* pixmap
, GdkPixbuf
* pixbuf
, int w
, int h
)
33 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
34 if (gdk_drawable_get_depth(pixmap
) == 1)
36 // invert to match XBM convention
37 guchar
* p
= gdk_pixbuf_get_pixels(pixbuf
);
38 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
39 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - w
* inc
;
40 for (int y
= h
; y
; y
--, p
+= rowpad
)
41 for (int x
= w
; x
; x
--, p
+= inc
)
43 // pixels are either (0,0,0) or (0xff,0xff,0xff)
51 static void MaskToAlpha(GdkPixmap
* mask
, GdkPixbuf
* pixbuf
, int w
, int h
)
53 GdkPixbuf
* mask_pixbuf
= gdk_pixbuf_get_from_drawable(
54 NULL
, mask
, NULL
, 0, 0, 0, 0, w
, h
);
55 guchar
* p
= gdk_pixbuf_get_pixels(pixbuf
) + 3;
56 const guchar
* mask_data
= gdk_pixbuf_get_pixels(mask_pixbuf
);
57 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - w
* 4;
58 const int mask_rowpad
= gdk_pixbuf_get_rowstride(mask_pixbuf
) - w
* 3;
59 for (int y
= h
; y
; y
--, p
+= rowpad
, mask_data
+= mask_rowpad
)
61 for (int x
= w
; x
; x
--, p
+= 4, mask_data
+= 3)
64 // no need to test all 3 components,
65 // pixels are either (0,0,0) or (0xff,0xff,0xff)
66 if (mask_data
[0] == 0)
70 g_object_unref(mask_pixbuf
);
74 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
78 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxMaskBase
)
85 wxMask::wxMask(const wxMask
& mask
)
91 const int w
= cairo_image_surface_get_width(mask
.m_bitmap
);
92 const int h
= cairo_image_surface_get_height(mask
.m_bitmap
);
93 m_bitmap
= cairo_image_surface_create(CAIRO_FORMAT_A8
, w
, h
);
94 const guchar
* src
= cairo_image_surface_get_data(mask
.m_bitmap
);
95 guchar
* dst
= cairo_image_surface_get_data(m_bitmap
);
96 const int stride
= cairo_image_surface_get_stride(m_bitmap
);
97 wxASSERT(stride
== cairo_image_surface_get_stride(mask
.m_bitmap
));
98 memcpy(dst
, src
, stride
* h
);
99 cairo_surface_mark_dirty(m_bitmap
);
102 if ( !mask
.m_bitmap
)
108 // create a copy of an existing mask
110 gdk_drawable_get_size(mask
.m_bitmap
, &w
, &h
);
111 m_bitmap
= gdk_pixmap_new(mask
.m_bitmap
, w
, h
, 1);
113 wxGtkObject
<GdkGC
> gc(gdk_gc_new(m_bitmap
));
114 gdk_draw_drawable(m_bitmap
, gc
, mask
.m_bitmap
, 0, 0, 0, 0, -1, -1);
118 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
121 InitFromColour(bitmap
, colour
);
125 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
128 Create( bitmap
, paletteIndex
);
130 #endif // wxUSE_PALETTE
132 wxMask::wxMask( const wxBitmap
& bitmap
)
135 InitFromMonoBitmap(bitmap
);
139 wxMask::wxMask(cairo_surface_t
* bitmap
)
141 wxMask::wxMask(GdkPixmap
* bitmap
)
152 cairo_surface_destroy(m_bitmap
);
154 g_object_unref (m_bitmap
);
159 void wxMask::FreeData()
164 cairo_surface_destroy(m_bitmap
);
166 g_object_unref (m_bitmap
);
172 bool wxMask::InitFromColour(const wxBitmap
& bitmap
, const wxColour
& colour
)
174 const int w
= bitmap
.GetWidth();
175 const int h
= bitmap
.GetHeight();
178 m_bitmap
= cairo_image_surface_create(CAIRO_FORMAT_A8
, w
, h
);
179 GdkPixbuf
* pixbuf
= bitmap
.GetPixbufNoMask();
180 const guchar
* src
= gdk_pixbuf_get_pixels(pixbuf
);
181 guchar
* dst
= cairo_image_surface_get_data(m_bitmap
);
182 const int stride_src
= gdk_pixbuf_get_rowstride(pixbuf
);
183 const int stride_dst
= cairo_image_surface_get_stride(m_bitmap
);
184 const int src_inc
= gdk_pixbuf_get_n_channels(pixbuf
);
185 const guchar r
= colour
.Red();
186 const guchar g
= colour
.Green();
187 const guchar b
= colour
.Blue();
188 for (int j
= 0; j
< h
; j
++, src
+= stride_src
, dst
+= stride_dst
)
190 const guchar
* s
= src
;
191 for (int i
= 0; i
< w
; i
++, s
+= src_inc
)
194 if (s
[0] == r
&& s
[1] == g
&& s
[2] == b
)
198 cairo_surface_mark_dirty(m_bitmap
);
200 // create mask as XBM format bitmap
202 // one bit per pixel, each row starts on a byte boundary
203 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
204 wxByte
* out
= new wxByte
[out_size
];
205 // set bits are unmasked
206 memset(out
, 0xff, out_size
);
207 unsigned bit_index
= 0;
208 if (bitmap
.HasPixbuf())
210 const wxByte r_mask
= colour
.Red();
211 const wxByte g_mask
= colour
.Green();
212 const wxByte b_mask
= colour
.Blue();
213 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
214 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
215 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
216 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
217 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
219 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
220 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
221 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
222 // move index to next byte boundary
223 bit_index
= (bit_index
+ 7) & ~7u;
228 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
229 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
231 if (colormap
== NULL
)
232 // mono bitmap, white is pixel value 0
233 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
237 c
.CalcPixel(colormap
);
238 mask_pixel
= c
.GetPixel();
240 for (int y
= 0; y
< h
; y
++)
242 for (int x
= 0; x
< w
; x
++, bit_index
++)
243 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
244 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
245 bit_index
= (bit_index
+ 7) & ~7u;
247 g_object_unref(image
);
249 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
255 bool wxMask::InitFromMonoBitmap(const wxBitmap
& bitmap
)
257 if (!bitmap
.IsOk()) return false;
259 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
262 InitFromColour(bitmap
, *wxBLACK
);
264 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
266 if (!m_bitmap
) return false;
268 wxGtkObject
<GdkGC
> gc(gdk_gc_new( m_bitmap
));
269 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
270 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
277 cairo_surface_t
* wxMask::GetBitmap() const
279 GdkPixmap
* wxMask::GetBitmap() const
285 //-----------------------------------------------------------------------------
287 //-----------------------------------------------------------------------------
289 class wxBitmapRefData
: public wxGDIRefData
292 wxBitmapRefData(int width
, int height
, int depth
);
293 virtual ~wxBitmapRefData();
295 virtual bool IsOk() const;
298 GdkPixbuf
* m_pixbufMask
;
299 GdkPixbuf
* m_pixbufNoMask
;
300 cairo_surface_t
* m_surface
;
310 bool m_alphaRequested
;
313 // We don't provide a copy ctor as copying m_pixmap and m_pixbuf properly
314 // is expensive and we don't want to do it implicitly (and possibly
315 // accidentally). wxBitmap::CloneGDIRefData() which does need to do it does
316 // it explicitly itself.
317 wxDECLARE_NO_COPY_CLASS(wxBitmapRefData
);
320 wxBitmapRefData::wxBitmapRefData(int width
, int height
, int depth
)
324 m_pixbufNoMask
= NULL
;
335 if (m_bpp
!= 1 && m_bpp
!= 32)
339 m_bpp
= gdk_drawable_get_depth(wxGetRootWindow()->window
);
340 m_alphaRequested
= depth
== 32;
344 wxBitmapRefData::~wxBitmapRefData()
348 g_object_unref(m_pixbufMask
);
350 g_object_unref(m_pixbufNoMask
);
352 cairo_surface_destroy(m_surface
);
355 g_object_unref (m_pixmap
);
357 g_object_unref (m_pixbuf
);
362 bool wxBitmapRefData::IsOk() const
367 //-----------------------------------------------------------------------------
369 //-----------------------------------------------------------------------------
371 #define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
373 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
375 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
377 LoadFile(filename
, type
);
380 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
382 wxASSERT(depth
== 1);
383 if (width
> 0 && height
> 0 && depth
== 1)
385 m_refData
= new wxBitmapRefData(width
, height
, 1);
387 GdkPixbuf
* pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8, width
, height
);
388 M_BMPDATA
->m_pixbufNoMask
= pixbuf
;
389 const char* src
= bits
;
390 guchar
* dst
= gdk_pixbuf_get_pixels(pixbuf
);
391 const int stride_src
= (width
+ 7) / 8;
392 const int rowinc_dst
= gdk_pixbuf_get_rowstride(pixbuf
) - 3 * width
;
393 for (int j
= 0; j
< width
; j
++, src
+= stride_src
, dst
+= rowinc_dst
)
395 for (int i
= 0; i
< height
; i
++)
398 if (src
[i
>> 3] & (1 << (i
& 7)))
406 M_BMPDATA
->m_pixmap
= gdk_bitmap_create_from_data(
407 wxGetRootWindow()->window
, bits
, width
, height
);
412 wxBitmap::wxBitmap(const char* const* bits
)
414 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
417 *this = wxBitmap(wxImage(bits
));
418 #elif defined __WXGTK3__
419 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_xpm_data(const_cast<const char**>(bits
));
422 m_refData
= new wxBitmapRefData(
423 gdk_pixbuf_get_width(pixbuf
), gdk_pixbuf_get_height(pixbuf
),
424 gdk_pixbuf_get_n_channels(pixbuf
) * 8);
425 M_BMPDATA
->m_pixbufNoMask
= pixbuf
;
426 wxASSERT(M_BMPDATA
->m_bpp
== 32 || !gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbufNoMask
));
429 GdkBitmap
* mask
= NULL
;
430 GdkPixmap
* pixmap
= gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, const_cast<char**>(bits
));
434 gdk_drawable_get_size(pixmap
, &width
, &height
);
435 m_refData
= new wxBitmapRefData(width
, height
, -1);
436 M_BMPDATA
->m_pixmap
= pixmap
;
439 M_BMPDATA
->m_mask
= new wxMask(mask
);
445 wxBitmap::wxBitmap(GdkPixbuf
* pixbuf
)
449 wxBitmapRefData
* bmpData
= new wxBitmapRefData(
450 gdk_pixbuf_get_width(pixbuf
), gdk_pixbuf_get_height(pixbuf
),
451 gdk_pixbuf_get_n_channels(pixbuf
) * 8);
454 bmpData
->m_pixbufNoMask
= pixbuf
;
456 bmpData
->m_pixbuf
= pixbuf
;
461 wxBitmap::~wxBitmap()
465 bool wxBitmap::Create( int width
, int height
, int depth
)
468 wxCHECK_MSG(width
>= 0 && height
>= 0, false, "invalid bitmap size");
469 m_refData
= new wxBitmapRefData(width
, height
, depth
);
474 static void CopyImageData(
475 guchar
* dst
, int dstChannels
, int dstStride
,
476 const guchar
* src
, int srcChannels
, int srcStride
,
479 if (dstChannels
== srcChannels
)
481 if (dstStride
== srcStride
)
482 memcpy(dst
, src
, size_t(dstStride
) * h
);
485 const int stride
= dstStride
< srcStride
? dstStride
: srcStride
;
486 for (int j
= 0; j
< h
; j
++, src
+= srcStride
, dst
+= dstStride
)
487 memcpy(dst
, src
, stride
);
492 for (int j
= 0; j
< h
; j
++, src
+= srcStride
, dst
+= dstStride
)
495 const guchar
* s
= src
;
496 if (dstChannels
== 4)
498 for (int i
= 0; i
< w
; i
++, d
+= 4, s
+= 3)
508 for (int i
= 0; i
< w
; i
++, d
+= 3, s
+= 4)
522 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
524 wxCHECK_RET(image
.IsOk(), "invalid image");
526 const int w
= image
.GetWidth();
527 const int h
= image
.GetHeight();
528 const guchar
* alpha
= image
.GetAlpha();
530 depth
= alpha
? 32 : 24;
531 else if (depth
!= 1 && depth
!= 32)
533 wxBitmapRefData
* bmpData
= new wxBitmapRefData(w
, h
, depth
);
535 GdkPixbuf
* pixbuf_dst
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, depth
== 32, 8, w
, h
);
536 bmpData
->m_pixbufNoMask
= pixbuf_dst
;
537 wxASSERT(bmpData
->m_bpp
== 32 || !gdk_pixbuf_get_has_alpha(bmpData
->m_pixbufNoMask
));
538 const guchar
* src
= image
.GetData();
540 guchar
* dst
= gdk_pixbuf_get_pixels(pixbuf_dst
);
541 const int dstStride
= gdk_pixbuf_get_rowstride(pixbuf_dst
);
542 CopyImageData(dst
, gdk_pixbuf_get_n_channels(pixbuf_dst
), dstStride
, src
, 3, 3 * w
, w
, h
);
544 if (depth
== 32 && alpha
)
546 for (int j
= 0; j
< h
; j
++, dst
+= dstStride
)
547 for (int i
= 0; i
< w
; i
++)
548 dst
[i
* 4 + 3] = *alpha
++;
552 const guchar r
= image
.GetMaskRed();
553 const guchar g
= image
.GetMaskGreen();
554 const guchar b
= image
.GetMaskBlue();
555 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_A8
, w
, h
);
556 const int stride
= cairo_image_surface_get_stride(surface
);
557 dst
= cairo_image_surface_get_data(surface
);
558 memset(dst
, 0xff, stride
* h
);
559 for (int j
= 0; j
< h
; j
++, dst
+= stride
)
560 for (int i
= 0; i
< w
; i
++, src
+= 3)
561 if (src
[0] == r
&& src
[1] == g
&& src
[2] == b
)
563 cairo_surface_mark_dirty(surface
);
564 bmpData
->m_mask
= new wxMask(surface
);
568 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
570 wxCHECK_RET(image
.IsOk(), "invalid image");
572 if (depth
== 32 || (depth
== -1 && image
.HasAlpha()))
573 CreateFromImageAsPixbuf(image
);
575 // otherwise create pixmap, if alpha is present it will be converted to mask
576 CreateFromImageAsPixmap(image
, depth
);
579 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
581 const int w
= image
.GetWidth();
582 const int h
= image
.GetHeight();
585 // create XBM format bitmap
587 // one bit per pixel, each row starts on a byte boundary
588 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
589 wxByte
* out
= new wxByte
[out_size
];
590 // set bits are black
591 memset(out
, 0xff, out_size
);
592 const wxByte
* in
= image
.GetData();
593 unsigned bit_index
= 0;
594 for (int y
= 0; y
< h
; y
++)
596 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
597 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
598 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
599 // move index to next byte boundary
600 bit_index
= (bit_index
+ 7) & ~7u;
602 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
605 if (!M_BMPDATA
) // SetPixmap may have failed
610 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
614 wxGtkObject
<GdkGC
> gc(gdk_gc_new(M_BMPDATA
->m_pixmap
));
616 M_BMPDATA
->m_pixmap
, gc
,
618 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
621 const wxByte
* alpha
= image
.GetAlpha();
622 if (alpha
!= NULL
|| image
.HasMask())
624 // create mask as XBM format bitmap
626 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
627 wxByte
* out
= new wxByte
[out_size
];
628 memset(out
, 0xff, out_size
);
629 unsigned bit_index
= 0;
632 for (int y
= 0; y
< h
; y
++)
634 for (int x
= 0; x
< w
; x
++, bit_index
++)
635 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
636 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
637 bit_index
= (bit_index
+ 7) & ~7u;
642 const wxByte r_mask
= image
.GetMaskRed();
643 const wxByte g_mask
= image
.GetMaskGreen();
644 const wxByte b_mask
= image
.GetMaskBlue();
645 const wxByte
* in
= image
.GetData();
646 for (int y
= 0; y
< h
; y
++)
648 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
649 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
650 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
651 bit_index
= (bit_index
+ 7) & ~7u;
654 SetMask(new wxMask(gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
)));
660 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
662 int width
= image
.GetWidth();
663 int height
= image
.GetHeight();
665 Create(width
, height
, 32);
666 GdkPixbuf
* pixbuf
= GetPixbuf();
671 const unsigned char* in
= image
.GetData();
672 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
673 unsigned char *alpha
= image
.GetAlpha();
675 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
677 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
679 for (int x
= 0; x
< width
; x
++, out
+= 4, in
+= 3)
693 wxImage
wxBitmap::ConvertToImage() const
697 wxCHECK_MSG(IsOk(), image
, "invalid bitmap");
698 wxBitmapRefData
* bmpData
= M_BMPDATA
;
699 const int w
= bmpData
->m_width
;
700 const int h
= bmpData
->m_height
;
701 image
.Create(w
, h
, false);
702 guchar
* dst
= image
.GetData();
703 GdkPixbuf
* pixbuf_src
= NULL
;
704 if (bmpData
->m_pixbufNoMask
)
705 pixbuf_src
= bmpData
->m_pixbufNoMask
;
706 else if (bmpData
->m_surface
)
708 pixbuf_src
= gdk_pixbuf_get_from_surface(bmpData
->m_surface
, 0, 0, w
, h
);
709 bmpData
->m_pixbufNoMask
= pixbuf_src
;
710 wxASSERT(bmpData
->m_bpp
== 32 || !gdk_pixbuf_get_has_alpha(bmpData
->m_pixbufNoMask
));
714 const guchar
* src
= gdk_pixbuf_get_pixels(pixbuf_src
);
715 const int srcStride
= gdk_pixbuf_get_rowstride(pixbuf_src
);
716 const int srcChannels
= gdk_pixbuf_get_n_channels(pixbuf_src
);
717 CopyImageData(dst
, 3, 3 * w
, src
, srcChannels
, srcStride
, w
, h
);
719 if (srcChannels
== 4)
722 guchar
* alpha
= image
.GetAlpha();
723 for (int j
= 0; j
< h
; j
++, src
+= srcStride
)
725 const guchar
* s
= src
;
726 for (int i
= 0; i
< w
; i
++, s
+= 4)
731 cairo_surface_t
* maskSurf
= NULL
;
733 maskSurf
= bmpData
->m_mask
->GetBitmap();
739 image
.SetMaskColour(r
, g
, b
);
740 wxASSERT(cairo_image_surface_get_format(maskSurf
) == CAIRO_FORMAT_A8
);
741 const int stride
= cairo_image_surface_get_stride(maskSurf
);
742 const guchar
* src
= cairo_image_surface_get_data(maskSurf
);
743 for (int j
= 0; j
< h
; j
++, src
+= stride
)
745 for (int i
= 0; i
< w
; i
++, dst
+= 3)
752 else if (dst
[0] == r
&& dst
[1] == g
&& dst
[2] == b
)
757 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
759 const int w
= GetWidth();
760 const int h
= GetHeight();
761 wxImage
image(w
, h
, false);
762 unsigned char *data
= image
.GetData();
764 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
766 // prefer pixbuf if available, it will preserve alpha and should be quicker
769 GdkPixbuf
*pixbuf
= GetPixbuf();
770 unsigned char* alpha
= NULL
;
771 if (gdk_pixbuf_get_has_alpha(pixbuf
))
774 alpha
= image
.GetAlpha();
776 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
777 unsigned char *out
= data
;
778 const int inc
= 3 + int(alpha
!= NULL
);
779 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
781 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
783 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
795 GdkPixmap
* pixmap
= GetPixmap();
796 GdkPixmap
* pixmap_invert
= NULL
;
799 // mono bitmaps are inverted, i.e. 0 is white
800 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
801 wxGtkObject
<GdkGC
> gc(gdk_gc_new(pixmap_invert
));
802 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
803 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
804 pixmap
= pixmap_invert
;
806 // create a pixbuf which shares data with the wxImage
807 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
808 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
810 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
812 g_object_unref(pixbuf
);
813 if (pixmap_invert
!= NULL
)
814 g_object_unref(pixmap_invert
);
816 // convert mask, unless there is already alpha
817 if (GetMask() && !image
.HasAlpha())
819 // we hard code the mask colour for now but we could also make an
820 // effort (and waste time) to choose a colour not present in the
821 // image already to avoid having to fudge the pixels below --
822 // whether it's worth to do it is unclear however
823 const int MASK_RED
= 1;
824 const int MASK_GREEN
= 2;
825 const int MASK_BLUE
= 3;
826 const int MASK_BLUE_REPLACEMENT
= 2;
828 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
829 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
831 for (int y
= 0; y
< h
; y
++)
833 for (int x
= 0; x
< w
; x
++, data
+= 3)
835 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
838 data
[1] = MASK_GREEN
;
841 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
843 // we have to fudge the colour a bit to prevent
844 // this pixel from appearing transparent
845 data
[2] = MASK_BLUE_REPLACEMENT
;
849 g_object_unref(image_mask
);
856 #endif // wxUSE_IMAGE
858 int wxBitmap::GetHeight() const
860 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
862 return M_BMPDATA
->m_height
;
865 int wxBitmap::GetWidth() const
867 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
869 return M_BMPDATA
->m_width
;
872 int wxBitmap::GetDepth() const
874 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
876 return M_BMPDATA
->m_bpp
;
879 wxMask
*wxBitmap::GetMask() const
881 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
883 return M_BMPDATA
->m_mask
;
886 void wxBitmap::SetMask( wxMask
*mask
)
888 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
891 delete M_BMPDATA
->m_mask
;
892 M_BMPDATA
->m_mask
= mask
;
895 wxBitmap
wxBitmap::GetMaskBitmap() const
898 wxBitmapRefData
* bmpData
= M_BMPDATA
;
900 cairo_surface_t
* mask
= NULL
;
901 if (bmpData
&& bmpData
->m_mask
)
902 mask
= bmpData
->m_mask
->GetBitmap();
905 const int w
= cairo_image_surface_get_width(mask
);
906 const int h
= cairo_image_surface_get_height(mask
);
907 GdkPixbuf
* pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8, w
, h
);
908 const guchar
* src
= cairo_image_surface_get_data(mask
);
909 guchar
* dst
= gdk_pixbuf_get_pixels(pixbuf
);
910 const int stride_src
= cairo_image_surface_get_stride(mask
);
911 const int stride_dst
= gdk_pixbuf_get_rowstride(pixbuf
);
912 for (int j
= 0; j
< h
; j
++, src
+= stride_src
, dst
+= stride_dst
)
915 for (int i
= 0; i
< w
; i
++, d
+= 3)
922 bitmap
= wxBitmap(pixbuf
);
925 GdkPixmap
* mask
= NULL
;
926 if (bmpData
&& bmpData
->m_mask
)
927 mask
= bmpData
->m_mask
->GetBitmap();
931 gdk_drawable_get_size(mask
, &w
, &h
);
932 GdkPixbuf
* pixbuf
= gdk_pixbuf_get_from_drawable(
933 NULL
, mask
, NULL
, 0, 0, 0, 0, w
, h
);
934 bitmap
= wxBitmap(pixbuf
);
940 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
947 static cairo_surface_t
* GetSubSurface(cairo_surface_t
* surface
, const wxRect
& rect
)
949 cairo_surface_flush(surface
);
950 const cairo_format_t format
= cairo_image_surface_get_format(surface
);
952 if (format
!= CAIRO_FORMAT_A8
)
954 cairo_surface_t
* subSurface
= cairo_image_surface_create(format
, rect
.width
, rect
.height
);
955 const int srcStride
= cairo_image_surface_get_stride(surface
);
956 const int dstStride
= cairo_image_surface_get_stride(subSurface
);
957 const guchar
* src
= cairo_image_surface_get_data(surface
) + rect
.y
* srcStride
+ x
;
958 guchar
* dst
= cairo_image_surface_get_data(subSurface
);
959 for (int j
= 0; j
< rect
.height
; j
++, src
+= srcStride
, dst
+= dstStride
)
960 memcpy(dst
, src
, dstStride
);
961 cairo_surface_mark_dirty(subSurface
);
966 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
970 wxCHECK_MSG(IsOk(), ret
, wxT("invalid bitmap"));
972 const int w
= rect
.width
;
973 const int h
= rect
.height
;
974 const wxBitmapRefData
* bmpData
= M_BMPDATA
;
976 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
977 rect
.x
+ w
<= bmpData
->m_width
&&
978 rect
.y
+ h
<= bmpData
->m_height
,
979 ret
, wxT("invalid bitmap region"));
981 wxBitmapRefData
* const newRef
= new wxBitmapRefData(w
, h
, bmpData
->m_bpp
);
982 ret
.m_refData
= newRef
;
985 if (bmpData
->m_pixbufNoMask
)
987 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_subpixbuf(bmpData
->m_pixbufNoMask
, rect
.x
, rect
.y
, w
, h
);
988 newRef
->m_pixbufNoMask
= gdk_pixbuf_copy(pixbuf
);
989 wxASSERT(newRef
->m_bpp
== 32 || !gdk_pixbuf_get_has_alpha(newRef
->m_pixbufNoMask
));
990 g_object_unref(pixbuf
);
992 else if (bmpData
->m_surface
)
993 newRef
->m_surface
= GetSubSurface(bmpData
->m_surface
, rect
);
995 cairo_surface_t
* maskSurf
= NULL
;
997 maskSurf
= bmpData
->m_mask
->GetBitmap();
1000 newRef
->m_mask
= new wxMask(GetSubSurface(maskSurf
, rect
));
1003 if (bmpData
->m_pixbuf
)
1006 gdk_pixbuf_new_subpixbuf(bmpData
->m_pixbuf
, rect
.x
, rect
.y
, w
, h
);
1007 newRef
->m_pixbuf
= gdk_pixbuf_copy(pixbuf
);
1008 g_object_unref(pixbuf
);
1010 if (bmpData
->m_pixmap
)
1012 newRef
->m_pixmap
= gdk_pixmap_new(bmpData
->m_pixmap
, w
, h
, -1);
1013 GdkGC
* gc
= gdk_gc_new(newRef
->m_pixmap
);
1015 newRef
->m_pixmap
, gc
, bmpData
->m_pixmap
, rect
.x
, rect
.y
, 0, 0, w
, h
);
1018 GdkPixmap
* mask
= NULL
;
1019 if (bmpData
->m_mask
)
1020 mask
= bmpData
->m_mask
->GetBitmap();
1023 GdkPixmap
* sub_mask
= gdk_pixmap_new(mask
, w
, h
, 1);
1024 newRef
->m_mask
= new wxMask(sub_mask
);
1025 GdkGC
* gc
= gdk_gc_new(sub_mask
);
1027 sub_mask
, gc
, mask
, rect
.x
, rect
.y
, 0, 0, w
, h
);
1035 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1037 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
1040 wxImage image
= ConvertToImage();
1041 if (image
.IsOk() && image
.SaveFile(name
, type
))
1044 const char* type_name
= NULL
;
1047 case wxBITMAP_TYPE_BMP
: type_name
= "bmp"; break;
1048 case wxBITMAP_TYPE_ICO
: type_name
= "ico"; break;
1049 case wxBITMAP_TYPE_JPEG
: type_name
= "jpeg"; break;
1050 case wxBITMAP_TYPE_PNG
: type_name
= "png"; break;
1054 gdk_pixbuf_save(GetPixbuf(), wxGTK_CONV_FN(name
), type_name
, NULL
, NULL
);
1057 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1061 if (image
.LoadFile(name
, type
) && image
.IsOk())
1062 *this = wxBitmap(image
);
1066 wxUnusedVar(type
); // The type is detected automatically by GDK.
1068 *this = wxBitmap(gdk_pixbuf_new_from_file(wxGTK_CONV_FN(name
), NULL
));
1075 wxPalette
*wxBitmap::GetPalette() const
1080 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1084 #endif // wxUSE_PALETTE
1086 void wxBitmap::SetHeight( int height
)
1089 M_BMPDATA
->m_height
= height
;
1092 void wxBitmap::SetWidth( int width
)
1095 M_BMPDATA
->m_width
= width
;
1098 void wxBitmap::SetDepth( int depth
)
1101 M_BMPDATA
->m_bpp
= depth
;
1105 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1113 gdk_drawable_get_size(pixmap
, &w
, &h
);
1114 wxBitmapRefData
* bmpData
= new wxBitmapRefData(w
, h
, 0);
1115 m_refData
= bmpData
;
1116 bmpData
->m_pixmap
= pixmap
;
1117 bmpData
->m_bpp
= gdk_drawable_get_depth(pixmap
);
1120 GdkPixmap
*wxBitmap::GetPixmap() const
1122 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1124 wxBitmapRefData
* bmpData
= M_BMPDATA
;
1125 if (bmpData
->m_pixmap
)
1126 return bmpData
->m_pixmap
;
1128 if (bmpData
->m_pixbuf
)
1130 GdkPixmap
* pixmap
= NULL
;
1131 GdkPixmap
** mask_pixmap
= NULL
;
1132 if (gdk_pixbuf_get_has_alpha(bmpData
->m_pixbuf
))
1134 // make new mask from alpha
1135 mask_pixmap
= &pixmap
;
1137 gdk_pixbuf_render_pixmap_and_mask(
1138 bmpData
->m_pixbuf
, &bmpData
->m_pixmap
, mask_pixmap
, 128);
1141 delete bmpData
->m_mask
;
1142 bmpData
->m_mask
= new wxMask(pixmap
);
1147 bmpData
->m_pixmap
= gdk_pixmap_new(wxGetRootWindow()->window
,
1148 bmpData
->m_width
, bmpData
->m_height
, bmpData
->m_bpp
== 1 ? 1 : -1);
1150 return bmpData
->m_pixmap
;
1153 bool wxBitmap::HasPixmap() const
1155 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
1157 return M_BMPDATA
->m_pixmap
!= NULL
;
1162 GdkPixbuf
* wxBitmap::GetPixbufNoMask() const
1164 wxCHECK_MSG(IsOk(), NULL
, "invalid bitmap");
1166 wxBitmapRefData
* bmpData
= M_BMPDATA
;
1167 GdkPixbuf
* pixbuf
= bmpData
->m_pixbufNoMask
;
1171 const int w
= bmpData
->m_width
;
1172 const int h
= bmpData
->m_height
;
1173 if (bmpData
->m_surface
)
1174 pixbuf
= gdk_pixbuf_get_from_surface(bmpData
->m_surface
, 0, 0, w
, h
);
1176 pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, bmpData
->m_bpp
== 32, 8, w
, h
);
1177 bmpData
->m_pixbufNoMask
= pixbuf
;
1178 wxASSERT(bmpData
->m_bpp
== 32 || !gdk_pixbuf_get_has_alpha(bmpData
->m_pixbufNoMask
));
1183 // helper to set up a simulated depth 1 surface
1184 static void SetSourceSurface1(const wxBitmapRefData
* bmpData
, cairo_t
* cr
, int x
, int y
, const wxColour
* fg
, const wxColour
* bg
)
1186 GdkPixbuf
* pixbuf
= gdk_pixbuf_copy(bmpData
->m_pixbufNoMask
);
1187 const int w
= bmpData
->m_width
;
1188 const int h
= bmpData
->m_height
;
1189 const int stride
= gdk_pixbuf_get_rowstride(pixbuf
);
1190 const int channels
= gdk_pixbuf_get_n_channels(pixbuf
);
1191 guchar
* dst
= gdk_pixbuf_get_pixels(pixbuf
);
1192 guchar fg_r
= 0, fg_g
= 0, fg_b
= 0;
1193 if (fg
&& fg
->IsOk())
1199 guchar bg_r
= 255, bg_g
= 255, bg_b
= 255;
1200 if (bg
&& bg
->IsOk())
1206 for (int j
= 0; j
< h
; j
++, dst
+= stride
)
1209 for (int i
= 0; i
< w
; i
++, d
+= channels
)
1223 gdk_cairo_set_source_pixbuf(cr
, pixbuf
, x
, y
);
1224 g_object_unref(pixbuf
);
1227 void wxBitmap::SetSourceSurface(cairo_t
* cr
, int x
, int y
, const wxColour
* fg
, const wxColour
* bg
) const
1229 wxBitmapRefData
* bmpData
= M_BMPDATA
;
1230 if (bmpData
->m_surface
)
1232 cairo_set_source_surface(cr
, bmpData
->m_surface
, x
, y
);
1235 wxCHECK_RET(bmpData
->m_pixbufNoMask
, "no bitmap data");
1236 if (bmpData
->m_bpp
== 1)
1237 SetSourceSurface1(bmpData
, cr
, x
, y
, fg
, bg
);
1240 gdk_cairo_set_source_pixbuf(cr
, bmpData
->m_pixbufNoMask
, x
, y
);
1241 cairo_pattern_get_surface(cairo_get_source(cr
), &bmpData
->m_surface
);
1242 cairo_surface_reference(bmpData
->m_surface
);
1246 cairo_t
* wxBitmap::CairoCreate() const
1248 wxCHECK_MSG(IsOk(), NULL
, "invalid bitmap");
1250 wxBitmapRefData
* bmpData
= M_BMPDATA
;
1252 if (bmpData
->m_surface
)
1253 cr
= cairo_create(bmpData
->m_surface
);
1256 GdkPixbuf
* pixbuf
= bmpData
->m_pixbufNoMask
;
1257 const bool useAlpha
= bmpData
->m_bpp
== 32 || (pixbuf
&& gdk_pixbuf_get_has_alpha(pixbuf
));
1258 bmpData
->m_surface
= cairo_image_surface_create(
1259 useAlpha
? CAIRO_FORMAT_ARGB32
: CAIRO_FORMAT_RGB24
,
1260 bmpData
->m_width
, bmpData
->m_height
);
1261 cr
= cairo_create(bmpData
->m_surface
);
1264 gdk_cairo_set_source_pixbuf(cr
, pixbuf
, 0, 0);
1266 cairo_set_source_rgb(cr
, 0, 0, 0);
1269 if (bmpData
->m_pixbufNoMask
)
1271 g_object_unref(bmpData
->m_pixbufNoMask
);
1272 bmpData
->m_pixbufNoMask
= NULL
;
1274 if (bmpData
->m_pixbufMask
)
1276 g_object_unref(bmpData
->m_pixbufMask
);
1277 bmpData
->m_pixbufMask
= NULL
;
1279 wxASSERT(cr
&& cairo_status(cr
) == 0);
1283 void wxBitmap::Draw(cairo_t
* cr
, int x
, int y
, bool useMask
, const wxColour
* fg
, const wxColour
* bg
) const
1285 wxCHECK_RET(IsOk(), "invalid bitmap");
1287 wxBitmapRefData
* bmpData
= M_BMPDATA
;
1288 SetSourceSurface(cr
, x
, y
, fg
, bg
);
1289 cairo_pattern_set_filter(cairo_get_source(cr
), CAIRO_FILTER_NEAREST
);
1290 cairo_surface_t
* mask
= NULL
;
1291 if (useMask
&& bmpData
->m_mask
)
1292 mask
= bmpData
->m_mask
->GetBitmap();
1294 cairo_mask_surface(cr
, mask
, x
, y
);
1300 GdkPixbuf
*wxBitmap::GetPixbuf() const
1302 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1304 wxBitmapRefData
* bmpData
= M_BMPDATA
;
1306 if (bmpData
->m_pixbufMask
)
1307 return bmpData
->m_pixbufMask
;
1309 if (bmpData
->m_pixbufNoMask
== NULL
)
1311 cairo_surface_t
* mask
= NULL
;
1312 if (bmpData
->m_mask
)
1313 mask
= bmpData
->m_mask
->GetBitmap();
1315 return bmpData
->m_pixbufNoMask
;
1317 const int w
= bmpData
->m_width
;
1318 const int h
= bmpData
->m_height
;
1319 bmpData
->m_pixbufMask
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, w
, h
);
1321 guchar
* dst
= gdk_pixbuf_get_pixels(bmpData
->m_pixbufMask
);
1322 const int dstStride
= gdk_pixbuf_get_rowstride(bmpData
->m_pixbufMask
);
1323 CopyImageData(dst
, 4, dstStride
,
1324 gdk_pixbuf_get_pixels(bmpData
->m_pixbufNoMask
),
1325 gdk_pixbuf_get_n_channels(bmpData
->m_pixbufNoMask
),
1326 gdk_pixbuf_get_rowstride(bmpData
->m_pixbufNoMask
),
1329 const guchar
* src
= cairo_image_surface_get_data(mask
);
1330 const int srcStride
= cairo_image_surface_get_stride(mask
);
1331 for (int j
= 0; j
< h
; j
++, src
+= srcStride
, dst
+= dstStride
)
1332 for (int i
= 0; i
< w
; i
++)
1336 return bmpData
->m_pixbufMask
;
1338 if (bmpData
->m_pixbuf
)
1339 return bmpData
->m_pixbuf
;
1341 const int w
= bmpData
->m_width
;
1342 const int h
= bmpData
->m_height
;
1343 GdkPixmap
* mask
= NULL
;
1344 if (bmpData
->m_mask
)
1345 mask
= bmpData
->m_mask
->GetBitmap();
1346 const bool useAlpha
= bmpData
->m_alphaRequested
|| mask
;
1347 bmpData
->m_pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, useAlpha
, 8, w
, h
);
1348 if (bmpData
->m_pixmap
)
1349 PixmapToPixbuf(bmpData
->m_pixmap
, bmpData
->m_pixbuf
, w
, h
);
1351 MaskToAlpha(mask
, bmpData
->m_pixbuf
, w
, h
);
1352 return bmpData
->m_pixbuf
;
1357 bool wxBitmap::HasPixbuf() const
1359 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
1361 return M_BMPDATA
->m_pixbuf
!= NULL
;
1364 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1366 if (keep
== Pixmap
&& HasPixbuf())
1368 g_object_unref (M_BMPDATA
->m_pixbuf
);
1369 M_BMPDATA
->m_pixbuf
= NULL
;
1371 if (keep
== Pixbuf
&& HasPixmap())
1373 g_object_unref (M_BMPDATA
->m_pixmap
);
1374 M_BMPDATA
->m_pixmap
= NULL
;
1379 #ifdef wxHAS_RAW_BITMAP
1380 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1384 GdkPixbuf
* pixbuf
= GetPixbufNoMask();
1385 if ((bpp
== 32) == (gdk_pixbuf_get_has_alpha(pixbuf
) != 0))
1387 bits
= gdk_pixbuf_get_pixels(pixbuf
);
1388 wxBitmapRefData
* bmpData
= M_BMPDATA
;
1389 data
.m_width
= bmpData
->m_width
;
1390 data
.m_height
= bmpData
->m_height
;
1391 data
.m_stride
= gdk_pixbuf_get_rowstride(pixbuf
);
1392 if (bmpData
->m_pixbufMask
)
1394 g_object_unref(bmpData
->m_pixbufMask
);
1395 bmpData
->m_pixbufMask
= NULL
;
1397 if (bmpData
->m_surface
)
1399 cairo_surface_destroy(bmpData
->m_surface
);
1400 bmpData
->m_surface
= NULL
;
1404 GdkPixbuf
*pixbuf
= GetPixbuf();
1405 const bool hasAlpha
= HasAlpha();
1407 // allow access if bpp is valid and matches existence of alpha
1408 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (bpp
== 32 && hasAlpha
)) )
1410 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1411 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1412 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1413 bits
= gdk_pixbuf_get_pixels(pixbuf
);
1419 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1422 #endif // wxHAS_RAW_BITMAP
1424 bool wxBitmap::HasAlpha() const
1426 const wxBitmapRefData
* bmpData
= M_BMPDATA
;
1428 return bmpData
&& bmpData
->m_bpp
== 32;
1430 return bmpData
&& (bmpData
->m_alphaRequested
||
1431 (bmpData
->m_pixbuf
&& gdk_pixbuf_get_has_alpha(bmpData
->m_pixbuf
)));
1435 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
1437 return new wxBitmapRefData(0, 0, 0);
1440 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
1442 const wxBitmapRefData
* oldRef
= static_cast<const wxBitmapRefData
*>(data
);
1443 wxBitmapRefData
* const newRef
= new wxBitmapRefData(oldRef
->m_width
,
1447 if (oldRef
->m_pixbufNoMask
)
1448 newRef
->m_pixbufNoMask
= gdk_pixbuf_copy(oldRef
->m_pixbufNoMask
);
1449 if (oldRef
->m_surface
)
1451 const int w
= oldRef
->m_width
;
1452 const int h
= oldRef
->m_height
;
1453 cairo_surface_t
* surface
= cairo_image_surface_create(
1454 cairo_image_surface_get_format(oldRef
->m_surface
), w
, h
);
1455 newRef
->m_surface
= surface
;
1456 cairo_surface_flush(oldRef
->m_surface
);
1457 const guchar
* src
= cairo_image_surface_get_data(oldRef
->m_surface
);
1458 guchar
* dst
= cairo_image_surface_get_data(surface
);
1459 const int stride
= cairo_image_surface_get_stride(surface
);
1460 wxASSERT(stride
== cairo_image_surface_get_stride(oldRef
->m_surface
));
1461 memcpy(dst
, src
, stride
* h
);
1462 cairo_surface_mark_dirty(surface
);
1465 if (oldRef
->m_pixmap
!= NULL
)
1467 newRef
->m_pixmap
= gdk_pixmap_new(
1468 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
1469 // use pixmap depth, m_bpp may not match
1470 gdk_drawable_get_depth(oldRef
->m_pixmap
));
1471 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_pixmap
));
1473 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
1475 if (oldRef
->m_pixbuf
!= NULL
)
1477 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
1480 if (oldRef
->m_mask
!= NULL
)
1482 newRef
->m_mask
= new wxMask(*oldRef
->m_mask
);
1488 /* static */ void wxBitmap::InitStandardHandlers()
1490 // TODO: Insert handler based on GdkPixbufs handler later