+#ifdef __WXGTK3__
+ wxImage image;
+ wxCHECK_MSG(IsOk(), image, "invalid bitmap");
+ wxBitmapRefData* bmpData = M_BMPDATA;
+ const int w = bmpData->m_width;
+ const int h = bmpData->m_height;
+ image.Create(w, h, false);
+ guchar* dst = image.GetData();
+ GdkPixbuf* pixbuf_src = NULL;
+ if (bmpData->m_pixbufNoMask)
+ pixbuf_src = bmpData->m_pixbufNoMask;
+ else if (bmpData->m_surface)
+ {
+ pixbuf_src = gdk_pixbuf_get_from_surface(bmpData->m_surface, 0, 0, w, h);
+ bmpData->m_pixbufNoMask = pixbuf_src;
+ wxASSERT(bmpData->m_bpp == 32 || !gdk_pixbuf_get_has_alpha(bmpData->m_pixbufNoMask));
+ }
+ if (pixbuf_src)
+ {
+ const guchar* src = gdk_pixbuf_get_pixels(pixbuf_src);
+ const int srcStride = gdk_pixbuf_get_rowstride(pixbuf_src);
+ const int srcChannels = gdk_pixbuf_get_n_channels(pixbuf_src);
+ CopyImageData(dst, 3, 3 * w, src, srcChannels, srcStride, w, h);
+
+ if (srcChannels == 4)
+ {
+ image.SetAlpha();
+ guchar* alpha = image.GetAlpha();
+ for (int j = 0; j < h; j++, src += srcStride)
+ {
+ const guchar* s = src;
+ for (int i = 0; i < w; i++, s += 4)
+ *alpha++ = s[3];
+ }
+ }
+ }
+ cairo_surface_t* maskSurf = NULL;
+ if (bmpData->m_mask)
+ maskSurf = bmpData->m_mask->GetBitmap();
+ if (maskSurf)
+ {
+ const guchar r = 1;
+ const guchar g = 2;
+ const guchar b = 3;
+ image.SetMaskColour(r, g, b);
+ wxASSERT(cairo_image_surface_get_format(maskSurf) == CAIRO_FORMAT_A8);
+ const int stride = cairo_image_surface_get_stride(maskSurf);
+ const guchar* src = cairo_image_surface_get_data(maskSurf);
+ for (int j = 0; j < h; j++, src += stride)
+ {
+ for (int i = 0; i < w; i++, dst += 3)
+ if (src[i] == 0)
+ {
+ dst[0] = r;
+ dst[1] = g;
+ dst[2] = b;
+ }
+ else if (dst[0] == r && dst[1] == g && dst[2] == b)
+ dst[2]--;
+ }
+ }
+#else
+ wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") );
+
+ const int w = GetWidth();
+ const int h = GetHeight();
+ wxImage image(w, h, false);
+ unsigned char *data = image.GetData();
+
+ wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") );
+
+ // prefer pixbuf if available, it will preserve alpha and should be quicker
+ if (HasPixbuf())
+ {
+ GdkPixbuf *pixbuf = GetPixbuf();
+ unsigned char* alpha = NULL;
+ if (gdk_pixbuf_get_has_alpha(pixbuf))
+ {
+ image.SetAlpha();
+ alpha = image.GetAlpha();
+ }
+ const unsigned char* in = gdk_pixbuf_get_pixels(pixbuf);
+ unsigned char *out = data;
+ const int inc = 3 + int(alpha != NULL);
+ const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
+
+ for (int y = 0; y < h; y++, in += rowpad)
+ {
+ for (int x = 0; x < w; x++, in += inc, out += 3)
+ {
+ out[0] = in[0];
+ out[1] = in[1];
+ out[2] = in[2];
+ if (alpha != NULL)
+ *alpha++ = in[3];
+ }
+ }
+ }
+ else
+ {
+ GdkPixmap* pixmap = GetPixmap();
+ GdkPixmap* pixmap_invert = NULL;
+ if (GetDepth() == 1)
+ {
+ // mono bitmaps are inverted, i.e. 0 is white
+ pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1);
+ wxGtkObject<GdkGC> gc(gdk_gc_new(pixmap_invert));
+ gdk_gc_set_function(gc, GDK_COPY_INVERT);
+ gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h);
+ pixmap = pixmap_invert;
+ }
+ // create a pixbuf which shares data with the wxImage
+ GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(
+ data, GDK_COLORSPACE_RGB, false, 8, w, h, 3 * w, NULL, NULL);
+
+ gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);