+ wxCHECK_MSG(IsOk(), NULL, "invalid bitmap");
+
+ wxBitmapRefData* bmpData = M_BMPDATA;
+ GdkPixbuf* pixbuf = bmpData->m_pixbufNoMask;
+ if (pixbuf)
+ return pixbuf;
+
+ const int w = bmpData->m_width;
+ const int h = bmpData->m_height;
+ if (bmpData->m_surface)
+ pixbuf = gdk_pixbuf_get_from_surface(bmpData->m_surface, 0, 0, w, h);
+ else
+ pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, bmpData->m_bpp == 32, 8, w, h);
+ bmpData->m_pixbufNoMask = pixbuf;
+ wxASSERT(bmpData->m_bpp == 32 || !gdk_pixbuf_get_has_alpha(bmpData->m_pixbufNoMask));
+
+ return pixbuf;
+}
+
+// helper to set up a simulated depth 1 surface
+static void SetSourceSurface1(const wxBitmapRefData* bmpData, cairo_t* cr, int x, int y, const wxColour* fg, const wxColour* bg)
+{
+ GdkPixbuf* pixbuf = gdk_pixbuf_copy(bmpData->m_pixbufNoMask);
+ const int w = bmpData->m_width;
+ const int h = bmpData->m_height;
+ const int stride = gdk_pixbuf_get_rowstride(pixbuf);
+ const int channels = gdk_pixbuf_get_n_channels(pixbuf);
+ guchar* dst = gdk_pixbuf_get_pixels(pixbuf);
+ guchar fg_r = 0, fg_g = 0, fg_b = 0;
+ if (fg && fg->IsOk())
+ {
+ fg_r = fg->Red();
+ fg_g = fg->Green();
+ fg_b = fg->Blue();
+ }
+ guchar bg_r = 255, bg_g = 255, bg_b = 255;
+ if (bg && bg->IsOk())
+ {
+ bg_r = bg->Red();
+ bg_g = bg->Green();
+ bg_b = bg->Blue();
+ }
+ for (int j = 0; j < h; j++, dst += stride)
+ {
+ guchar* d = dst;
+ for (int i = 0; i < w; i++, d += channels)
+ if (d[0])
+ {
+ d[0] = bg_r;
+ d[1] = bg_g;
+ d[2] = bg_b;
+ }
+ else
+ {
+ d[0] = fg_r;
+ d[1] = fg_g;
+ d[2] = fg_b;
+ }
+ }
+ gdk_cairo_set_source_pixbuf(cr, pixbuf, x, y);
+ g_object_unref(pixbuf);
+}
+
+void wxBitmap::SetSourceSurface(cairo_t* cr, int x, int y, const wxColour* fg, const wxColour* bg) const
+{
+ wxBitmapRefData* bmpData = M_BMPDATA;
+ if (bmpData->m_surface)
+ {
+ cairo_set_source_surface(cr, bmpData->m_surface, x, y);
+ return;
+ }
+ wxCHECK_RET(bmpData->m_pixbufNoMask, "no bitmap data");
+ if (bmpData->m_bpp == 1)
+ SetSourceSurface1(bmpData, cr, x, y, fg, bg);
+ else
+ {
+ gdk_cairo_set_source_pixbuf(cr, bmpData->m_pixbufNoMask, x, y);
+ cairo_pattern_get_surface(cairo_get_source(cr), &bmpData->m_surface);
+ cairo_surface_reference(bmpData->m_surface);
+ }
+}
+
+cairo_t* wxBitmap::CairoCreate() const
+{
+ wxCHECK_MSG(IsOk(), NULL, "invalid bitmap");
+
+ wxBitmapRefData* bmpData = M_BMPDATA;
+ cairo_t* cr;
+ if (bmpData->m_surface)
+ cr = cairo_create(bmpData->m_surface);
+ else
+ {
+ GdkPixbuf* pixbuf = bmpData->m_pixbufNoMask;
+ const bool useAlpha = bmpData->m_bpp == 32 || (pixbuf && gdk_pixbuf_get_has_alpha(pixbuf));
+ bmpData->m_surface = cairo_image_surface_create(
+ useAlpha ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
+ bmpData->m_width, bmpData->m_height);
+ cr = cairo_create(bmpData->m_surface);
+ if (pixbuf)
+ {
+ gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
+ cairo_paint(cr);
+ cairo_set_source_rgb(cr, 0, 0, 0);
+ }
+ }
+ if (bmpData->m_pixbufNoMask)
+ {
+ g_object_unref(bmpData->m_pixbufNoMask);
+ bmpData->m_pixbufNoMask = NULL;
+ }
+ if (bmpData->m_pixbufMask)
+ {
+ g_object_unref(bmpData->m_pixbufMask);
+ bmpData->m_pixbufMask = NULL;
+ }
+ wxASSERT(cr && cairo_status(cr) == 0);
+ return cr;
+}
+
+void wxBitmap::Draw(cairo_t* cr, int x, int y, bool useMask, const wxColour* fg, const wxColour* bg) const
+{
+ wxCHECK_RET(IsOk(), "invalid bitmap");
+
+ wxBitmapRefData* bmpData = M_BMPDATA;
+ SetSourceSurface(cr, x, y, fg, bg);
+ cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_NEAREST);
+ cairo_surface_t* mask = NULL;
+ if (useMask && bmpData->m_mask)
+ mask = bmpData->m_mask->GetBitmap();
+ if (mask)
+ cairo_mask_surface(cr, mask, x, y);
+ else
+ cairo_paint(cr);
+}
+#endif
+
+GdkPixbuf *wxBitmap::GetPixbuf() const
+{
+ wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
+
+ wxBitmapRefData* bmpData = M_BMPDATA;
+#ifdef __WXGTK3__
+ if (bmpData->m_pixbufMask)
+ return bmpData->m_pixbufMask;
+
+ if (bmpData->m_pixbufNoMask == NULL)
+ GetPixbufNoMask();
+ cairo_surface_t* mask = NULL;
+ if (bmpData->m_mask)
+ mask = bmpData->m_mask->GetBitmap();
+ if (mask == NULL)
+ return bmpData->m_pixbufNoMask;
+
+ const int w = bmpData->m_width;
+ const int h = bmpData->m_height;
+ bmpData->m_pixbufMask = gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, w, h);
+
+ guchar* dst = gdk_pixbuf_get_pixels(bmpData->m_pixbufMask);
+ const int dstStride = gdk_pixbuf_get_rowstride(bmpData->m_pixbufMask);
+ CopyImageData(dst, 4, dstStride,
+ gdk_pixbuf_get_pixels(bmpData->m_pixbufNoMask),
+ gdk_pixbuf_get_n_channels(bmpData->m_pixbufNoMask),
+ gdk_pixbuf_get_rowstride(bmpData->m_pixbufNoMask),
+ w, h);
+
+ const guchar* src = cairo_image_surface_get_data(mask);
+ const int srcStride = cairo_image_surface_get_stride(mask);
+ for (int j = 0; j < h; j++, src += srcStride, dst += dstStride)
+ for (int i = 0; i < w; i++)
+ if (src[i] == 0)
+ dst[i * 4 + 3] = 0;
+
+ return bmpData->m_pixbufMask;
+#else
+ if (bmpData->m_pixbuf)
+ return bmpData->m_pixbuf;
+
+ const int w = bmpData->m_width;
+ const int h = bmpData->m_height;
+ GdkPixmap* mask = NULL;
+ if (bmpData->m_mask)
+ mask = bmpData->m_mask->GetBitmap();
+ const bool useAlpha = bmpData->m_alphaRequested || mask;
+ bmpData->m_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, useAlpha, 8, w, h);
+ if (bmpData->m_pixmap)
+ PixmapToPixbuf(bmpData->m_pixmap, bmpData->m_pixbuf, w, h);
+ if (mask)
+ MaskToAlpha(mask, bmpData->m_pixbuf, w, h);
+ return bmpData->m_pixbuf;
+#endif
+}
+
+#ifndef __WXGTK3__
+bool wxBitmap::HasPixbuf() const
+{
+ wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
+
+ return M_BMPDATA->m_pixbuf != NULL;
+}
+
+void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
+{
+ if (keep == Pixmap && HasPixbuf())
+ {
+ g_object_unref (M_BMPDATA->m_pixbuf);
+ M_BMPDATA->m_pixbuf = NULL;
+ }
+ if (keep == Pixbuf && HasPixmap())
+ {
+ g_object_unref (M_BMPDATA->m_pixmap);
+ M_BMPDATA->m_pixmap = NULL;
+ }
+}
+#endif
+
+#ifdef wxHAS_RAW_BITMAP
+void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
+{
+ void* bits = NULL;
+#ifdef __WXGTK3__
+ GdkPixbuf* pixbuf = GetPixbufNoMask();
+ if ((bpp == 32) == (gdk_pixbuf_get_has_alpha(pixbuf) != 0))
+ {
+ bits = gdk_pixbuf_get_pixels(pixbuf);
+ wxBitmapRefData* bmpData = M_BMPDATA;
+ data.m_width = bmpData->m_width;
+ data.m_height = bmpData->m_height;
+ data.m_stride = gdk_pixbuf_get_rowstride(pixbuf);
+ if (bmpData->m_pixbufMask)
+ {
+ g_object_unref(bmpData->m_pixbufMask);
+ bmpData->m_pixbufMask = NULL;
+ }
+ if (bmpData->m_surface)
+ {
+ cairo_surface_destroy(bmpData->m_surface);
+ bmpData->m_surface = NULL;
+ }
+ }
+#else
+ GdkPixbuf *pixbuf = GetPixbuf();
+ const bool hasAlpha = HasAlpha();
+
+ // allow access if bpp is valid and matches existence of alpha
+ if ( pixbuf && ((bpp == 24 && !hasAlpha) || (bpp == 32 && hasAlpha)) )
+ {
+ data.m_height = gdk_pixbuf_get_height( pixbuf );
+ data.m_width = gdk_pixbuf_get_width( pixbuf );
+ data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
+ bits = gdk_pixbuf_get_pixels(pixbuf);
+ }
+#endif
+ return bits;
+}
+
+void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
+{
+}
+#endif // wxHAS_RAW_BITMAP
+
+bool wxBitmap::HasAlpha() const
+{
+ const wxBitmapRefData* bmpData = M_BMPDATA;
+#ifdef __WXGTK3__
+ return bmpData && bmpData->m_bpp == 32;
+#else
+ return bmpData && (bmpData->m_alphaRequested ||
+ (bmpData->m_pixbuf && gdk_pixbuf_get_has_alpha(bmpData->m_pixbuf)));
+#endif
+}
+
+wxGDIRefData* wxBitmap::CreateGDIRefData() const
+{
+ return new wxBitmapRefData(0, 0, 0);
+}
+
+wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const
+{
+ const wxBitmapRefData* oldRef = static_cast<const wxBitmapRefData*>(data);
+ wxBitmapRefData * const newRef = new wxBitmapRefData(oldRef->m_width,
+ oldRef->m_height,
+ oldRef->m_bpp);
+#ifdef __WXGTK3__
+ if (oldRef->m_pixbufNoMask)
+ newRef->m_pixbufNoMask = gdk_pixbuf_copy(oldRef->m_pixbufNoMask);
+ if (oldRef->m_surface)
+ {
+ const int w = oldRef->m_width;
+ const int h = oldRef->m_height;
+ cairo_surface_t* surface = cairo_image_surface_create(
+ cairo_image_surface_get_format(oldRef->m_surface), w, h);
+ newRef->m_surface = surface;
+ cairo_surface_flush(oldRef->m_surface);
+ const guchar* src = cairo_image_surface_get_data(oldRef->m_surface);
+ guchar* dst = cairo_image_surface_get_data(surface);
+ const int stride = cairo_image_surface_get_stride(surface);
+ wxASSERT(stride == cairo_image_surface_get_stride(oldRef->m_surface));
+ memcpy(dst, src, stride * h);
+ cairo_surface_mark_dirty(surface);
+ }
+#else
+ if (oldRef->m_pixmap != NULL)
+ {
+ newRef->m_pixmap = gdk_pixmap_new(
+ oldRef->m_pixmap, oldRef->m_width, oldRef->m_height,
+ // use pixmap depth, m_bpp may not match
+ gdk_drawable_get_depth(oldRef->m_pixmap));
+ wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_pixmap));
+ gdk_draw_drawable(
+ newRef->m_pixmap, gc, oldRef->m_pixmap, 0, 0, 0, 0, -1, -1);
+ }
+ if (oldRef->m_pixbuf != NULL)
+ {
+ newRef->m_pixbuf = gdk_pixbuf_copy(oldRef->m_pixbuf);
+ }
+#endif
+ if (oldRef->m_mask != NULL)
+ {
+ newRef->m_mask = new wxMask(*oldRef->m_mask);
+ }
+
+ return newRef;
+}
+
+/* static */ void wxBitmap::InitStandardHandlers()
+{
+ // TODO: Insert handler based on GdkPixbufs handler later
+}