[cairo] Rendering to PNG

Carl Worth cworth at cworth.org
Thu Dec 16 07:25:41 PST 2004


At Thu, 16 Dec 2004 11:01:12 +0800,
Steve Chaplin wrote:
>
> What would be the usual way to render to a png file?

Depends.

> cairo_set_target_png (cr, file, CAIRO_FORMAT_ARGB32, IMAGE_WIDTH,
> IMAGE_HEIGHT);
> /* drawing operations */
> cairo_show_page (cr);

That's using cairo's png backend, which is a reasonable and convenient
thing to do if you just want to render to the png file.

I'm still not a huge fan of the name "cairo_show_page" for flushing
operations to a non-paginated backend such as PNG, but it's what we
have now.

> cairo_set_target_image (cr, image, CAIRO_FORMAT_ARGB32, width, height,
> width * 4);
> /* drawing operations */

So that much is just using cairo's image backend. This gives you
direct access to the actual bits of the result in the image
buffer. The advantage is that you can do anything you want with that
buffer, (eg. save it in any other image format, muck with it using
other libraries, spit it out to some other window system, etc.).

> write_png_argb32 (image, png_file, width, height, width * 4);

The disadvantage being that you have to actually do something with
that buffer. Notice that the above write_png_argb32 is
application-specific code to write a png file --- it's not a cairo
function.

The only reason xsvg has custom code to make png files using the image
backend rather than directly using the png backend is that the xsvg
png code was written before cairo had a png backend.

-Carl



More information about the cairo mailing list