Gimp/libgimpcolor/gimpcairo.h
Jehan 926d5c6daa app, libgimp*, plug-ins: fix previous commit and add gimp_cairo_surface_get_buffer().
The previous commit was not taking into account usages of
gimp_cairo_surface_create_buffer(), other than the text layer rendering
usage, as it was focused on fixing #14970.

In particular, in other pieces of code, we were using a temporary
GeglBuffer as a way to edit the original Cairo surface but the syncing
back was not happening anymore. So previous commit was breaking all
thumbnails of layers or images, as well as PDF export for all raster
layers.

After some thought, I decided to implement a new function
gimp_cairo_surface_get_buffer() which would do what
gimp_cairo_surface_create_buffer() was doing, but adding a boolean flag
to declare whether or not we were expecting to sync back into the Cairo
surface's data. After all, when we don't need to sync back, better avoid
the useless work of copying possibly huge data.

I also added back the possibility of returning a linear-memory backed
buffer, after some discussion with pippin. Because for very small
surfaces, and if we are indeed looking to sync back into the source
Cairo surface, the advantages of using a linear buffer may still be
worth it, even though it's not true for the general case (where tiled
buffers will be much more efficient). I used 4096 (64^2 because our
current tile dimensions are 64 right now in GIMP), though this number is
kinda random and doesn't matter too much (it doesn't have to be the
square of the tile's width or height). I don't put any info about this
in the new function's documentation because developers should not have
any expectations on the type of buffer they get (only whether or not it
will sync back to the source).

As a consequence of all this, gimp_cairo_surface_create_buffer() has
been deprecated as of GIMP 3.2, though it will still work the same. The
new function will be recommended as a replacement.
2025-11-08 20:34:55 +01:00

159 lines
5.6 KiB
C

/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimpcairo.h
* Copyright (C) 2007 Sven Neumann <sven@gimp.org>
* 2010-2012 Michael Natterer <mitch@gimp.org>
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#ifndef __GIMP_CAIRO_H__
#define __GIMP_CAIRO_H__
cairo_pattern_t * gimp_cairo_checkerboard_create (cairo_t *cr,
gint size,
const GeglColor *light,
const GeglColor *dark);
const Babl * gimp_cairo_surface_get_format (cairo_surface_t *surface);
GIMP_DEPRECATED_FOR(gimp_cairo_surface_get_buffer)
GeglBuffer * gimp_cairo_surface_create_buffer (cairo_surface_t *surface,
const Babl *format);
GeglBuffer * gimp_cairo_surface_get_buffer (cairo_surface_t *surface,
const Babl *format,
gboolean sync_back);
/* some useful macros for writing directly to a Cairo surface */
/**
* GIMP_CAIRO_RGB24_SET_PIXEL:
* @d: pointer to the destination buffer
* @r: red component
* @g: green component
* @b: blue component
*
* Sets a single pixel in an Cairo image surface in %CAIRO_FORMAT_RGB24.
*
* Since: 2.6
**/
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#define GIMP_CAIRO_RGB24_SET_PIXEL(d, r, g, b) \
G_STMT_START { d[0] = (b); d[1] = (g); d[2] = (r); } G_STMT_END
#else
#define GIMP_CAIRO_RGB24_SET_PIXEL(d, r, g, b) \
G_STMT_START { d[1] = (r); d[2] = (g); d[3] = (b); } G_STMT_END
#endif
/**
* GIMP_CAIRO_RGB24_GET_PIXEL:
* @s: pointer to the source buffer
* @r: red component
* @g: green component
* @b: blue component
*
* Gets a single pixel from a Cairo image surface in %CAIRO_FORMAT_RGB24.
*
* Since: 2.8
**/
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#define GIMP_CAIRO_RGB24_GET_PIXEL(s, r, g, b) \
G_STMT_START { (b) = s[0]; (g) = s[1]; (r) = s[2]; } G_STMT_END
#else
#define GIMP_CAIRO_RGB24_GET_PIXEL(s, r, g, b) \
G_STMT_START { (r) = s[1]; (g) = s[2]; (b) = s[3]; } G_STMT_END
#endif
/**
* GIMP_CAIRO_ARGB32_SET_PIXEL:
* @d: pointer to the destination buffer
* @r: red component, not pre-multiplied
* @g: green component, not pre-multiplied
* @b: blue component, not pre-multiplied
* @a: alpha component
*
* Sets a single pixel in an Cairo image surface in %CAIRO_FORMAT_ARGB32.
*
* Since: 2.6
**/
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#define GIMP_CAIRO_ARGB32_SET_PIXEL(d, r, g, b, a) \
G_STMT_START { \
const guint tr = (a) * (r) + 0x80; \
const guint tg = (a) * (g) + 0x80; \
const guint tb = (a) * (b) + 0x80; \
(d)[0] = (((tb) >> 8) + (tb)) >> 8; \
(d)[1] = (((tg) >> 8) + (tg)) >> 8; \
(d)[2] = (((tr) >> 8) + (tr)) >> 8; \
(d)[3] = (a); \
} G_STMT_END
#else
#define GIMP_CAIRO_ARGB32_SET_PIXEL(d, r, g, b, a) \
G_STMT_START { \
const guint tr = (a) * (r) + 0x80; \
const guint tg = (a) * (g) + 0x80; \
const guint tb = (a) * (b) + 0x80; \
(d)[0] = (a); \
(d)[1] = (((tr) >> 8) + (tr)) >> 8; \
(d)[2] = (((tg) >> 8) + (tg)) >> 8; \
(d)[3] = (((tb) >> 8) + (tb)) >> 8; \
} G_STMT_END
#endif
/**
* GIMP_CAIRO_ARGB32_GET_PIXEL:
* @s: pointer to the source buffer
* @r: red component, not pre-multiplied
* @g: green component, not pre-multiplied
* @b: blue component, not pre-multiplied
* @a: alpha component
*
* Gets a single pixel from a Cairo image surface in %CAIRO_FORMAT_ARGB32.
*
* Since: 2.8
**/
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#define GIMP_CAIRO_ARGB32_GET_PIXEL(s, r, g, b, a) \
G_STMT_START { \
const guint tb = (s)[0]; \
const guint tg = (s)[1]; \
const guint tr = (s)[2]; \
const guint ta = (s)[3]; \
(r) = (tr << 8) / (ta + 1); \
(g) = (tg << 8) / (ta + 1); \
(b) = (tb << 8) / (ta + 1); \
(a) = ta; \
} G_STMT_END
#else
#define GIMP_CAIRO_ARGB32_GET_PIXEL(s, r, g, b, a) \
G_STMT_START { \
const guint ta = (s)[0]; \
const guint tr = (s)[1]; \
const guint tg = (s)[2]; \
const guint tb = (s)[3]; \
(r) = (tr << 8) / (ta + 1); \
(g) = (tg << 8) / (ta + 1); \
(b) = (tb << 8) / (ta + 1); \
(a) = ta; \
} G_STMT_END
#endif
#endif /* __GIMP_CAIRO_H__ */