Re: How to draw to a pixbuf?

On Tue, 2 Sep 2008 10:36:40 +0100
Chris Vine <chris cvine freeserve co uk> wrote:
> 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
> units). 
> 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.

It has also occurred to me that another and probably easier way to the
same result is to create a drawable (such as a Gdk::Pixmap object), get
a cairo_t surface for it with gdk_cairo_create(), and then after
drawing create a GdkPixbuf object from it with
gdk_pixbuf_get_from_drawable() which you can wrap as a Gdk::Pixbuf

That would save you having to do your own byte reordering on the raw
pixel data from a cairo image surface - it would be done for you,
possibly at the expense of some efficiency because drawables are server
side entities.


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