app: s/sprintf/g_snprintf/ in xcf_save_image()
[gimp.git] / libgimpcolor / gimpcairo.c
blobe1e57d9bbb62affa4c6fd18c0edb4dd4f86ecfd7
1 /* LIBGIMP - The GIMP Library
2 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
4 * gimpcairo.c
5 * Copyright (C) 2007 Sven Neumann <sven@gimp.org>
6 * 2010-2012 Michael Natterer <mitch@gimp.org>
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.
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.
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/>.
23 #include "config.h"
25 #include <cairo.h>
26 #include <gio/gio.h>
27 #include <gegl.h>
29 #include "libgimpbase/gimpbase.h"
31 #include "gimpcolortypes.h"
33 #include "gimpcairo.h"
36 /**
37 * SECTION: gimpcairo
38 * @title: GimpCairo
39 * @short_description: Color utility functions for cairo
41 * Utility functions that make cairo easier to use with GIMP color
42 * data types.
43 **/
46 /**
47 * gimp_cairo_set_source_rgb:
48 * @cr: Cairo context
49 * @color: GimpRGB color
51 * Sets the source pattern within @cr to the solid opaque color
52 * described by @color.
54 * This function calls cairo_set_source_rgb() for you.
56 * Since: 2.6
57 **/
58 void
59 gimp_cairo_set_source_rgb (cairo_t *cr,
60 const GimpRGB *color)
62 cairo_set_source_rgb (cr, color->r, color->g, color->b);
65 /**
66 * gimp_cairo_set_source_rgba:
67 * @cr: Cairo context
68 * @color: GimpRGB color
70 * Sets the source pattern within @cr to the solid translucent color
71 * described by @color.
73 * This function calls cairo_set_source_rgba() for you.
75 * Since: 2.6
76 **/
77 void
78 gimp_cairo_set_source_rgba (cairo_t *cr,
79 const GimpRGB *color)
81 cairo_set_source_rgba (cr, color->r, color->g, color->b, color->a);
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
91 * Create a repeating checkerboard pattern.
93 * Return value: a new Cairo pattern that can be used as a source on @cr.
95 * Since: 2.6
96 **/
97 cairo_pattern_t *
98 gimp_cairo_checkerboard_create (cairo_t *cr,
99 gint size,
100 const GimpRGB *light,
101 const GimpRGB *dark)
103 cairo_t *context;
104 cairo_surface_t *surface;
105 cairo_pattern_t *pattern;
107 g_return_val_if_fail (cr != NULL, NULL);
108 g_return_val_if_fail (size > 0, NULL);
110 surface = cairo_surface_create_similar (cairo_get_target (cr),
111 CAIRO_CONTENT_COLOR,
112 2 * size, 2 * size);
113 context = cairo_create (surface);
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);
121 cairo_rectangle (context, 0, 0, size, size);
122 cairo_rectangle (context, size, size, size, size);
123 cairo_fill (context);
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);
131 cairo_rectangle (context, 0, size, size, size);
132 cairo_rectangle (context, size, 0, size, size);
133 cairo_fill (context);
135 cairo_destroy (context);
137 pattern = cairo_pattern_create_for_surface (surface);
138 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
140 cairo_surface_destroy (surface);
142 return pattern;
146 * gimp_cairo_surface_get_format:
147 * @surface: a Cairo surface
149 * This function returns a #Babl format that corresponds to @surface's
150 * pixel format.
152 * Return value: the #Babl format of @surface.
154 * Since: 2.10
156 const Babl *
157 gimp_cairo_surface_get_format (cairo_surface_t *surface)
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);
163 switch (cairo_image_surface_get_format (surface))
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");
169 default:
170 break;
173 g_return_val_if_reached (NULL);
177 * gimp_cairo_surface_create_buffer:
178 * @surface: a Cairo surface
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.
184 * Return value: a #GeglBuffer
186 * Since: 2.10
188 GeglBuffer *
189 gimp_cairo_surface_create_buffer (cairo_surface_t *surface)
191 const Babl *format;
192 gint width;
193 gint height;
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);
199 format = gimp_cairo_surface_get_format (surface);
200 width = cairo_image_surface_get_width (surface);
201 height = cairo_image_surface_get_height (surface);
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));