[cairo] Loading an image from a C source file, exported from e.g. GIMP

Luca Bacci luca.bacci982 at gmail.com
Thu Oct 13 13:13:20 UTC 2022


Hello Rafał,

I believe you can use cairo_image_surface_create_for_data(), however you
have to export the image data from GIMP as ARGB32 instead of RGBA32. If
that's not possible then you must swap the color channels using a loop.

Finally, for best results I'd make sure that the GIMP_IMAGE_pixel_data
array is properly aligned to at least 4 bytes. The syntax depend on the
compiler, see
https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Type-Attributes.html for GCC.

cairo_surface_t *surface;

cairo_format_t format = CAIRO_FORMAT_ARGB32;
int stride = GIMP_IMAGE_WIDTH * GIMP_IMAGE_BYTES_PER_PIXEL;

assert(cairo_format_stride_for_width (format, width) == stride);

surface = cairo_image_surface_create_for_data(GIMP_IMAGE_pixel_data,
format, GIMP_IMAGE_WIDTH, GIMP_IMAGE_HEIGHT,
stride);

https://www.cairographics.org/manual/cairo-Image-Surfaces.html#cairo-image-surface-create-for-data

Luca

Il gio 13 ott 2022, 11:44 Rafał Jopek <rafaljopek at hotmail.com> ha scritto:

> Hi,
>
> Below is a sample image code generated in GIMP
> What will the function in Cairo that allows drawing directly onto a
> surface generated source code in C.
>
> ```
> #define GIMP_IMAGE_WIDTH (4)
> #define GIMP_IMAGE_HEIGHT (4)
> #define GIMP_IMAGE_BYTES_PER_PIXEL (4) /* 2:RGB16, 3:RGB, 4:RGBA */
> #define GIMP_IMAGE_PIXEL_DATA ((unsigned char*) GIMP_IMAGE_pixel_data)
> static const unsigned char GIMP_IMAGE_pixel_data[4 * 4 * 4 + 1] =
> ("
> \000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\377\377\377\000\000\000\000\377\377\377\377\000
> "
>  "
> \000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\377\377\377\377\000\000\000\000\377
> "
>  "\377\377\377");
>
> ```
>
> Kind regards,
> -- Rafał
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.cairographics.org/archives/cairo/attachments/20221013/4240c32a/attachment.htm>


More information about the cairo mailing list