1 /* LIBGIMP - The GIMP Library
2  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3  *
4  * gimpcairo.h
5  * Copyright (C) 2007      Sven Neumann <sven@gimp.org>
6  *               2010-2012 Michael Natterer <mitch@gimp.org>
7  *
8  * This library is free software: you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 3 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library.  If not, see
20  * <https://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef __GIMP_CAIRO_H__
24 #define __GIMP_CAIRO_H__
25 
26 
27 void              gimp_cairo_set_source_rgb        (cairo_t         *cr,
28                                                     const GimpRGB   *color);
29 void              gimp_cairo_set_source_rgba       (cairo_t         *cr,
30                                                     const GimpRGB   *color);
31 
32 cairo_pattern_t * gimp_cairo_checkerboard_create   (cairo_t         *cr,
33                                                     gint             size,
34                                                     const GimpRGB   *light,
35                                                     const GimpRGB   *dark);
36 
37 const Babl      * gimp_cairo_surface_get_format    (cairo_surface_t *surface);
38 GeglBuffer      * gimp_cairo_surface_create_buffer (cairo_surface_t *surface);
39 
40 
41 /*  some useful macros for writing directly to a Cairo surface  */
42 
43 /**
44  * GIMP_CAIRO_RGB24_SET_PIXEL:
45  * @d: pointer to the destination buffer
46  * @r: red component
47  * @g: green component
48  * @b: blue component
49  *
50  * Sets a single pixel in an Cairo image surface in %CAIRO_FORMAT_RGB24.
51  *
52  * Since: 2.6
53  **/
54 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
55 #define GIMP_CAIRO_RGB24_SET_PIXEL(d, r, g, b) \
56   G_STMT_START { d[0] = (b);  d[1] = (g);  d[2] = (r); } G_STMT_END
57 #else
58 #define GIMP_CAIRO_RGB24_SET_PIXEL(d, r, g, b) \
59   G_STMT_START { d[1] = (r);  d[2] = (g);  d[3] = (b); } G_STMT_END
60 #endif
61 
62 
63 /**
64  * GIMP_CAIRO_RGB24_GET_PIXEL:
65  * @s: pointer to the source buffer
66  * @r: red component
67  * @g: green component
68  * @b: blue component
69  *
70  * Gets a single pixel from a Cairo image surface in %CAIRO_FORMAT_RGB24.
71  *
72  * Since: 2.8
73  **/
74 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
75 #define GIMP_CAIRO_RGB24_GET_PIXEL(s, r, g, b) \
76   G_STMT_START { (b) = s[0]; (g) = s[1]; (r) = s[2]; } G_STMT_END
77 #else
78 #define GIMP_CAIRO_RGB24_GET_PIXEL(s, r, g, b) \
79   G_STMT_START { (r) = s[1]; (g) = s[2]; (b) = s[3]; } G_STMT_END
80 #endif
81 
82 
83 /**
84  * GIMP_CAIRO_ARGB32_SET_PIXEL:
85  * @d: pointer to the destination buffer
86  * @r: red component, not pre-multiplied
87  * @g: green component, not pre-multiplied
88  * @b: blue component, not pre-multiplied
89  * @a: alpha component
90  *
91  * Sets a single pixel in an Cairo image surface in %CAIRO_FORMAT_ARGB32.
92  *
93  * Since: 2.6
94  **/
95 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
96 #define GIMP_CAIRO_ARGB32_SET_PIXEL(d, r, g, b, a) \
97   G_STMT_START {                                   \
98     const guint tr = (a) * (r) + 0x80;             \
99     const guint tg = (a) * (g) + 0x80;             \
100     const guint tb = (a) * (b) + 0x80;             \
101     (d)[0] = (((tb) >> 8) + (tb)) >> 8;            \
102     (d)[1] = (((tg) >> 8) + (tg)) >> 8;            \
103     (d)[2] = (((tr) >> 8) + (tr)) >> 8;            \
104     (d)[3] = (a);                                  \
105   } G_STMT_END
106 #else
107 #define GIMP_CAIRO_ARGB32_SET_PIXEL(d, r, g, b, a) \
108   G_STMT_START {                                   \
109     const guint tr = (a) * (r) + 0x80;             \
110     const guint tg = (a) * (g) + 0x80;             \
111     const guint tb = (a) * (b) + 0x80;             \
112     (d)[0] = (a);                                  \
113     (d)[1] = (((tr) >> 8) + (tr)) >> 8;            \
114     (d)[2] = (((tg) >> 8) + (tg)) >> 8;            \
115     (d)[3] = (((tb) >> 8) + (tb)) >> 8;            \
116   } G_STMT_END
117 #endif
118 
119 
120 /**
121  * GIMP_CAIRO_ARGB32_GET_PIXEL:
122  * @s: pointer to the source buffer
123  * @r: red component, not pre-multiplied
124  * @g: green component, not pre-multiplied
125  * @b: blue component, not pre-multiplied
126  * @a: alpha component
127  *
128  * Gets a single pixel from a Cairo image surface in %CAIRO_FORMAT_ARGB32.
129  *
130  * Since: 2.8
131  **/
132 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
133 #define GIMP_CAIRO_ARGB32_GET_PIXEL(s, r, g, b, a) \
134   G_STMT_START {                                   \
135     const guint tb = (s)[0];                       \
136     const guint tg = (s)[1];                       \
137     const guint tr = (s)[2];                       \
138     const guint ta = (s)[3];                       \
139     (r) = (tr << 8) / (ta + 1);                    \
140     (g) = (tg << 8) / (ta + 1);                    \
141     (b) = (tb << 8) / (ta + 1);                    \
142     (a) = ta;                                      \
143   } G_STMT_END
144 #else
145 #define GIMP_CAIRO_ARGB32_GET_PIXEL(s, r, g, b, a) \
146   G_STMT_START {                                   \
147     const guint ta = (s)[0];                       \
148     const guint tr = (s)[1];                       \
149     const guint tg = (s)[2];                       \
150     const guint tb = (s)[3];                       \
151     (r) = (tr << 8) / (ta + 1);                    \
152     (g) = (tg << 8) / (ta + 1);                    \
153     (b) = (tb << 8) / (ta + 1);                    \
154     (a) = ta;                                      \
155   } G_STMT_END
156 #endif
157 
158 
159 #endif /* __GIMP_CAIRO_H__ */
160