Re: How to draw to a pixbuf?

On Tue, 2 Sep 2008 01:00:13 +0200
Carlo Wood <carlo alinoe com> wrote:
> I have a function that can draw to a cairo_t*.
> I need to create a Gtk::IconSet.
> Gtk::IconSet wants me to pass a Glib::RefPtr<Gdk::Pixbuf> const&,
> so ... I need to:
> - create a Gdk::Pixbuf object somehow
> - get a related cairo_t* for it.
> I tried long and hard and spend several hours trying
> to find examples or documentation about this, but
> everything I tried core dumped (if it compiled at all).
> Can someone please tell me how to do this?

I have never gone that way round, but I think you should be able to have
your destination surface for drawing as an image surface, created by
cairo_image_surface_create() with the CAIRO_FORMAT_ARGB32 format
(assuming it has an alpha channel, CAIRO_FORMAT_RGB24 if not), which
will each have 4 bytes per pixel.

After drawing, the data buffer obtainable with
cairo_image_surface_get_data() will have raw pixel data in standard
ARGB uncompressed format, with the alpha channel as the high byte and
native endian. You should be able then to construct a Gdk::Pixbuf
object from that with Gdk::Pixbuf::create_from_data().  Your rowstride
will be 4 * pixel width (cairo image surface data is aligned in 32 bit

However I believe Gdk::Pixbufs take their pixel data in RGBA format,
with the alpha channel as the high byte, so you will need to do some
byte reordering, but if so that will be trivial.  Possibly this will be
endian dependent - cairo surfaces are native endian.  You had best suck
that and see.

I say I have never gone that way round because I have always used cairo
image surfaces as pattern sources rather than surface destinations.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]