[cairo] Images with stride == width?

Chris Wilson chris at chris-wilson.co.uk
Tue Jan 20 07:32:17 PST 2009


On Tue, 2009-01-20 at 10:44 -0400, Ian Britten wrote:
> Having now tried this, I can report that you'll simply get a
> CAIRO_STATUS_INVALID_STRIDE error if you try this.
> 
> As such, it seems that you cannot make an image surface from an
> existing block of image data, and will always have to create a
> new buffer (Using the calculated stride), and copy your source
> data into the temporary buffer (And then create the image surface
> using that temporary buffer).

In general this is true. Note this problem is even more complicated if
you need to create a surface for h/w acceleration as each piece of h/w
has it own limitations for valid strides, widths and heights. For the
image backend, Cairo deliberately only supports a limited number of
formats in order to avoid the exponential explosion of possible pathways
and so that the image backend matches the requirements of xlib and we do
not suffer conversion costs unnecessarily.

Aside from stride, you also need to be very careful about pixel packing
and endianess. In particular, if you have an RGB packed-pixel (3 bytes)
the CAIRO_FORMAT_RGB24 representation is actually xRGB (4 bytes).

Often you find image loaders are far more accommodating when it comes to
filling buffers (because of the multitude of different formats, as
above) - see libpng (cairo/src/cairo-png.c) for an example. If you do
have to convert from a raw image buffer, then I'd recommend using:
  image = cairo_image_surface_create (CAIRO_FORMAT_XXX, width, height);
  data = cairo_image_surface_get_data (image);
  ... copy from buffer to data ...
  cairo_surface_mark_dirty (image);
so that the temporary buffer is automatically managed along with the
image surface referencing it.

Hope this helps and you continue you have fun using Cairo.
-ickle



More information about the cairo mailing list