Sunday, April 26, 2009

Create a GdkPixbuf from cairo surface

Hello,
here's the inverted code snippet of the previous post.

w, h = surface.get_width(), surface.get_height()

pixmap = gtk.gdk.Pixmap (None, w, h, 24)

cr = pixmap.cairo_create ()

cr.set_source_surface (surface, 0, 0)

cr.paint ()

pixbuf = gtk.gdk.Pixbuf (gtk.gdk.COLORSPACE_RGB, True, 8, w, h)

pixbuf = pixbuf.get_from_drawable (pixmap, gtk.gdk.colormap_get_system(), 0, 0, 0, 0, w, h)

Saturday, April 25, 2009

Create a cairo surface from a pixbuf

Hello,
sometimes in the code of a couple of projects I see some hard algorithms to transform a pixbuf in a cairo surface.
Maybe most of people don't know that GdkCairoContext, contrarily to cairo_t, is created against a cairo context not a cairo surface:

surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, pixbuf.get_width(), pixbuf.get_height())

cr = cairo.Context (surface)

gdkcr = gtk.gdk.CairoContext (cr)

gdkcr.set_source_pixbuf (pixbuf)

gdkcr.paint ()


Notice gtk.gdk.CairoContext (cr), which cr is not the surface. That's the key of the code snippet.

For instance this can be applied on a ClutterCairoTexture to render pixbufs in the canvas.