1 /* LIBGIMP - The GIMP Library
2 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3 *
4 * gimpcairo.c
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 #include "config.h"
24
25 #include <cairo.h>
26 #include <gio/gio.h>
27 #include <gegl.h>
28
29 #include "libgimpbase/gimpbase.h"
30
31 #include "gimpcolortypes.h"
32
33 #include "gimpcairo.h"
34
35
36 /**
37 * SECTION: gimpcairo
38 * @title: GimpCairo
39 * @short_description: Color utility functions for cairo
40 *
41 * Utility functions that make cairo easier to use with GIMP color
42 * data types.
43 **/
44
45
46 /**
47 * gimp_cairo_set_source_rgb:
48 * @cr: Cairo context
49 * @color: GimpRGB color
50 *
51 * Sets the source pattern within @cr to the solid opaque color
52 * described by @color.
53 *
54 * This function calls cairo_set_source_rgb() for you.
55 *
56 * Since: 2.6
57 **/
58 void
gimp_cairo_set_source_rgb(cairo_t * cr,const GimpRGB * color)59 gimp_cairo_set_source_rgb (cairo_t *cr,
60 const GimpRGB *color)
61 {
62 cairo_set_source_rgb (cr, color->r, color->g, color->b);
63 }
64
65 /**
66 * gimp_cairo_set_source_rgba:
67 * @cr: Cairo context
68 * @color: GimpRGB color
69 *
70 * Sets the source pattern within @cr to the solid translucent color
71 * described by @color.
72 *
73 * This function calls cairo_set_source_rgba() for you.
74 *
75 * Since: 2.6
76 **/
77 void
gimp_cairo_set_source_rgba(cairo_t * cr,const GimpRGB * color)78 gimp_cairo_set_source_rgba (cairo_t *cr,
79 const GimpRGB *color)
80 {
81 cairo_set_source_rgba (cr, color->r, color->g, color->b, color->a);
82 }
83
84 /**
85 * gimp_cairo_checkerboard_create:
86 * @cr: Cairo context
87 * @size: check size
88 * @light: light check color or %NULL to use the default light gray
89 * @dark: dark check color or %NULL to use the default dark gray
90 *
91 * Create a repeating checkerboard pattern.
92 *
93 * Return value: a new Cairo pattern that can be used as a source on @cr.
94 *
95 * Since: 2.6
96 **/
97 cairo_pattern_t *
gimp_cairo_checkerboard_create(cairo_t * cr,gint size,const GimpRGB * light,const GimpRGB * dark)98 gimp_cairo_checkerboard_create (cairo_t *cr,
99 gint size,
100 const GimpRGB *light,
101 const GimpRGB *dark)
102 {
103 cairo_t *context;
104 cairo_surface_t *surface;
105 cairo_pattern_t *pattern;
106
107 g_return_val_if_fail (cr != NULL, NULL);
108 g_return_val_if_fail (size > 0, NULL);
109
110 surface = cairo_surface_create_similar (cairo_get_target (cr),
111 CAIRO_CONTENT_COLOR,
112 2 * size, 2 * size);
113 context = cairo_create (surface);
114
115 if (light)
116 gimp_cairo_set_source_rgb (context, light);
117 else
118 cairo_set_source_rgb (context,
119 GIMP_CHECK_LIGHT, GIMP_CHECK_LIGHT, GIMP_CHECK_LIGHT);
120
121 cairo_rectangle (context, 0, 0, size, size);
122 cairo_rectangle (context, size, size, size, size);
123 cairo_fill (context);
124
125 if (dark)
126 gimp_cairo_set_source_rgb (context, dark);
127 else
128 cairo_set_source_rgb (context,
129 GIMP_CHECK_DARK, GIMP_CHECK_DARK, GIMP_CHECK_DARK);
130
131 cairo_rectangle (context, 0, size, size, size);
132 cairo_rectangle (context, size, 0, size, size);
133 cairo_fill (context);
134
135 cairo_destroy (context);
136
137 pattern = cairo_pattern_create_for_surface (surface);
138 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
139
140 cairo_surface_destroy (surface);
141
142 return pattern;
143 }
144
145 /**
146 * gimp_cairo_surface_get_format:
147 * @surface: a Cairo surface
148 *
149 * This function returns a #Babl format that corresponds to @surface's
150 * pixel format.
151 *
152 * Return value: the #Babl format of @surface.
153 *
154 * Since: 2.10
155 **/
156 const Babl *
gimp_cairo_surface_get_format(cairo_surface_t * surface)157 gimp_cairo_surface_get_format (cairo_surface_t *surface)
158 {
159 g_return_val_if_fail (surface != NULL, NULL);
160 g_return_val_if_fail (cairo_surface_get_type (surface) ==
161 CAIRO_SURFACE_TYPE_IMAGE, NULL);
162
163 switch (cairo_image_surface_get_format (surface))
164 {
165 case CAIRO_FORMAT_RGB24: return babl_format ("cairo-RGB24");
166 case CAIRO_FORMAT_ARGB32: return babl_format ("cairo-ARGB32");
167 case CAIRO_FORMAT_A8: return babl_format ("cairo-A8");
168
169 default:
170 break;
171 }
172
173 g_return_val_if_reached (NULL);
174 }
175
176 /**
177 * gimp_cairo_surface_create_buffer:
178 * @surface: a Cairo surface
179 *
180 * This function returns a #GeglBuffer which wraps @surface's pixels.
181 * It must only be called on image surfaces, calling it on other surface
182 * types is an error.
183 *
184 * Return value: a #GeglBuffer
185 *
186 * Since: 2.10
187 **/
188 GeglBuffer *
gimp_cairo_surface_create_buffer(cairo_surface_t * surface)189 gimp_cairo_surface_create_buffer (cairo_surface_t *surface)
190 {
191 const Babl *format;
192 gint width;
193 gint height;
194
195 g_return_val_if_fail (surface != NULL, NULL);
196 g_return_val_if_fail (cairo_surface_get_type (surface) ==
197 CAIRO_SURFACE_TYPE_IMAGE, NULL);
198
199 format = gimp_cairo_surface_get_format (surface);
200 width = cairo_image_surface_get_width (surface);
201 height = cairo_image_surface_get_height (surface);
202
203 return
204 gegl_buffer_linear_new_from_data (cairo_image_surface_get_data (surface),
205 format,
206 GEGL_RECTANGLE (0, 0, width, height),
207 cairo_image_surface_get_stride (surface),
208 (GDestroyNotify) cairo_surface_destroy,
209 cairo_surface_reference (surface));
210 }
211