1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2005 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include "gdkcairo.h"
21 
22 #include "gdkinternals.h"
23 
24 #include <math.h>
25 
26 /**
27  * gdk_cairo_set_source_rgba:
28  * @cr: a cairo context
29  * @rgba: a `GdkRGBA`
30  *
31  * Sets the specified `GdkRGBA` as the source color of @cr.
32  */
33 void
gdk_cairo_set_source_rgba(cairo_t * cr,const GdkRGBA * rgba)34 gdk_cairo_set_source_rgba (cairo_t       *cr,
35                            const GdkRGBA *rgba)
36 {
37   g_return_if_fail (cr != NULL);
38   g_return_if_fail (rgba != NULL);
39 
40   cairo_set_source_rgba (cr,
41                          rgba->red,
42                          rgba->green,
43                          rgba->blue,
44                          rgba->alpha);
45 }
46 
47 /**
48  * gdk_cairo_rectangle:
49  * @cr: a cairo context
50  * @rectangle: a `GdkRectangle`
51  *
52  * Adds the given rectangle to the current path of @cr.
53  */
54 void
gdk_cairo_rectangle(cairo_t * cr,const GdkRectangle * rectangle)55 gdk_cairo_rectangle (cairo_t            *cr,
56                      const GdkRectangle *rectangle)
57 {
58   g_return_if_fail (cr != NULL);
59   g_return_if_fail (rectangle != NULL);
60 
61   cairo_rectangle (cr,
62                    rectangle->x,     rectangle->y,
63                    rectangle->width, rectangle->height);
64 }
65 
66 /**
67  * gdk_cairo_region:
68  * @cr: a cairo context
69  * @region: a `cairo_region_t`
70  *
71  * Adds the given region to the current path of @cr.
72  */
73 void
gdk_cairo_region(cairo_t * cr,const cairo_region_t * region)74 gdk_cairo_region (cairo_t              *cr,
75                   const cairo_region_t *region)
76 {
77   cairo_rectangle_int_t box;
78   int n_boxes, i;
79 
80   g_return_if_fail (cr != NULL);
81   g_return_if_fail (region != NULL);
82 
83   n_boxes = cairo_region_num_rectangles (region);
84 
85   for (i = 0; i < n_boxes; i++)
86     {
87       cairo_region_get_rectangle (region, i, &box);
88       cairo_rectangle (cr, box.x, box.y, box.width, box.height);
89     }
90 }
91 
92 void
gdk_cairo_surface_paint_pixbuf(cairo_surface_t * surface,const GdkPixbuf * pixbuf)93 gdk_cairo_surface_paint_pixbuf (cairo_surface_t *surface,
94                                 const GdkPixbuf *pixbuf)
95 {
96   int width, height;
97   guchar *gdk_pixels, *cairo_pixels;
98   int gdk_rowstride, cairo_stride;
99   int n_channels;
100   int j;
101 
102   if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
103     return;
104 
105   /* This function can't just copy any pixbuf to any surface, be
106    * sure to read the invariants here before calling it */
107 
108   g_assert (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE);
109   g_assert (cairo_image_surface_get_format (surface) == CAIRO_FORMAT_RGB24 ||
110             cairo_image_surface_get_format (surface) == CAIRO_FORMAT_ARGB32);
111   g_assert (cairo_image_surface_get_width (surface) == gdk_pixbuf_get_width (pixbuf));
112   g_assert (cairo_image_surface_get_height (surface) == gdk_pixbuf_get_height (pixbuf));
113 
114   cairo_surface_flush (surface);
115 
116   width = gdk_pixbuf_get_width (pixbuf);
117   height = gdk_pixbuf_get_height (pixbuf);
118   gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
119   gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
120   n_channels = gdk_pixbuf_get_n_channels (pixbuf);
121   cairo_stride = cairo_image_surface_get_stride (surface);
122   cairo_pixels = cairo_image_surface_get_data (surface);
123 
124   for (j = height; j; j--)
125     {
126       guchar *p = gdk_pixels;
127       guchar *q = cairo_pixels;
128 
129       if (n_channels == 3)
130         {
131           guchar *end = p + 3 * width;
132 
133           while (p < end)
134             {
135 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
136               q[0] = p[2];
137               q[1] = p[1];
138               q[2] = p[0];
139               q[3] = 0xFF;
140 #else
141               q[0] = 0xFF;
142               q[1] = p[0];
143               q[2] = p[1];
144               q[3] = p[2];
145 #endif
146               p += 3;
147               q += 4;
148             }
149         }
150       else
151         {
152           guchar *end = p + 4 * width;
153           guint t1,t2,t3;
154 
155 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x80; d = ((t >> 8) + t) >> 8; } G_STMT_END
156 
157           while (p < end)
158             {
159 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
160               MULT(q[0], p[2], p[3], t1);
161               MULT(q[1], p[1], p[3], t2);
162               MULT(q[2], p[0], p[3], t3);
163               q[3] = p[3];
164 #else
165               q[0] = p[3];
166               MULT(q[1], p[0], p[3], t1);
167               MULT(q[2], p[1], p[3], t2);
168               MULT(q[3], p[2], p[3], t3);
169 #endif
170 
171               p += 4;
172               q += 4;
173             }
174 
175 #undef MULT
176         }
177 
178       gdk_pixels += gdk_rowstride;
179       cairo_pixels += cairo_stride;
180     }
181 
182   cairo_surface_mark_dirty (surface);
183 }
184 
185 /**
186  * gdk_cairo_set_source_pixbuf:
187  * @cr: a cairo context
188  * @pixbuf: a `GdkPixbuf`
189  * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
190  * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
191  *
192  * Sets the given pixbuf as the source pattern for @cr.
193  *
194  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
195  * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y.
196  */
197 void
gdk_cairo_set_source_pixbuf(cairo_t * cr,const GdkPixbuf * pixbuf,double pixbuf_x,double pixbuf_y)198 gdk_cairo_set_source_pixbuf (cairo_t         *cr,
199                              const GdkPixbuf *pixbuf,
200                              double           pixbuf_x,
201                              double           pixbuf_y)
202 {
203   cairo_format_t format;
204   cairo_surface_t *surface;
205 
206   if (gdk_pixbuf_get_n_channels (pixbuf) == 3)
207     format = CAIRO_FORMAT_RGB24;
208   else
209     format = CAIRO_FORMAT_ARGB32;
210 
211   surface = cairo_surface_create_similar_image (cairo_get_target (cr),
212                                                 format,
213                                                 gdk_pixbuf_get_width (pixbuf),
214                                                 gdk_pixbuf_get_height (pixbuf));
215 
216   gdk_cairo_surface_paint_pixbuf (surface, pixbuf);
217 
218   cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
219   cairo_surface_destroy (surface);
220 }
221 
222 /*
223  * _gdk_cairo_surface_extents:
224  * @surface: surface to measure
225  * @extents: (out): rectangle to put the extents
226  *
227  * Measures the area covered by @surface and puts it into @extents.
228  *
229  * Note that this function respects device offsets set on @surface.
230  * If @surface is unbounded, the resulting extents will be empty and
231  * not be a maximal sized rectangle. This is to avoid careless coding.
232  * You must explicitly check the return value of you want to handle
233  * that case.
234  *
235  * Returns: %TRUE if the extents fit in a `GdkRectangle`, %FALSE if not
236  */
237 gboolean
_gdk_cairo_surface_extents(cairo_surface_t * surface,GdkRectangle * extents)238 _gdk_cairo_surface_extents (cairo_surface_t *surface,
239                             GdkRectangle    *extents)
240 {
241   double x1, x2, y1, y2;
242   cairo_t *cr;
243 
244   g_return_val_if_fail (surface != NULL, FALSE);
245   g_return_val_if_fail (extents != NULL, FALSE);
246 
247   cr = cairo_create (surface);
248   cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
249   cairo_destroy (cr);
250 
251   x1 = floor (x1);
252   y1 = floor (y1);
253   x2 = ceil (x2);
254   y2 = ceil (y2);
255   x2 -= x1;
256   y2 -= y1;
257 
258   if (x1 < G_MININT || x1 > G_MAXINT ||
259       y1 < G_MININT || y1 > G_MAXINT ||
260       x2 > G_MAXINT || y2 > G_MAXINT)
261     {
262       extents->x = extents->y = extents->width = extents->height = 0;
263       return FALSE;
264     }
265 
266   extents->x = x1;
267   extents->y = y1;
268   extents->width = x2;
269   extents->height = y2;
270 
271   return TRUE;
272 }
273 
274 /* This function originally from Jean-Edouard Lachand-Robert, and
275  * available at www.codeguru.com. Simplified for our needs, not sure
276  * how much of the original code left any longer. Now handles just
277  * one-bit deep bitmaps (in Window parlance, ie those that GDK calls
278  * bitmaps (and not pixmaps), with zero pixels being transparent.
279  */
280 /**
281  * gdk_cairo_region_create_from_surface:
282  * @surface: a cairo surface
283  *
284  * Creates region that covers the area where the given
285  * @surface is more than 50% opaque.
286  *
287  * This function takes into account device offsets that might be
288  * set with cairo_surface_set_device_offset().
289  *
290  * Returns: (transfer full): A `cairo_region_t`
291  */
292 cairo_region_t *
gdk_cairo_region_create_from_surface(cairo_surface_t * surface)293 gdk_cairo_region_create_from_surface (cairo_surface_t *surface)
294 {
295   cairo_region_t *region;
296   GdkRectangle extents, rect;
297   cairo_surface_t *image;
298   cairo_t *cr;
299   int x, y, stride;
300   guchar *data;
301 
302   _gdk_cairo_surface_extents (surface, &extents);
303 
304   if (cairo_surface_get_content (surface) == CAIRO_CONTENT_COLOR)
305     return cairo_region_create_rectangle (&extents);
306 
307   if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_IMAGE ||
308       cairo_image_surface_get_format (surface) != CAIRO_FORMAT_A1)
309     {
310       /* coerce to an A1 image */
311       image = cairo_image_surface_create (CAIRO_FORMAT_A1,
312                                           extents.width, extents.height);
313       cr = cairo_create (image);
314       cairo_set_source_surface (cr, surface, -extents.x, -extents.y);
315       cairo_paint (cr);
316       cairo_destroy (cr);
317     }
318   else
319     image = cairo_surface_reference (surface);
320 
321   /* Flush the surface to make sure that the rendering is up to date. */
322   cairo_surface_flush (image);
323 
324   data = cairo_image_surface_get_data (image);
325   stride = cairo_image_surface_get_stride (image);
326 
327   region = cairo_region_create ();
328 
329   for (y = 0; y < extents.height; y++)
330     {
331       for (x = 0; x < extents.width; x++)
332         {
333           /* Search for a continuous range of "non transparent pixels"*/
334           int x0 = x;
335           while (x < extents.width)
336             {
337 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
338               if (((data[x / 8] >> (x%8)) & 1) == 0)
339 #else
340               if (((data[x / 8] >> (7-(x%8))) & 1) == 0)
341 #endif
342                 /* This pixel is "transparent"*/
343                 break;
344               x++;
345             }
346 
347           if (x > x0)
348             {
349               /* Add the pixels (x0, y) to (x, y+1) as a new rectangle
350                * in the region
351                */
352               rect.x = x0;
353               rect.width = x - x0;
354               rect.y = y;
355               rect.height = 1;
356 
357               cairo_region_union_rectangle (region, &rect);
358             }
359         }
360       data += stride;
361     }
362 
363   cairo_surface_destroy (image);
364 
365   cairo_region_translate (region, extents.x, extents.y);
366 
367   return region;
368 }
369 
370 cairo_region_t *
gdk_cairo_region_from_clip(cairo_t * cr)371 gdk_cairo_region_from_clip (cairo_t *cr)
372 {
373   cairo_rectangle_list_t *rectangles;
374   cairo_region_t *region;
375   int i;
376 
377   rectangles = cairo_copy_clip_rectangle_list (cr);
378 
379   if (rectangles->status != CAIRO_STATUS_SUCCESS)
380     return NULL;
381 
382   region = cairo_region_create ();
383   for (i = 0; i < rectangles->num_rectangles; i++)
384     {
385       cairo_rectangle_int_t clip_rect;
386       cairo_rectangle_t *rect;
387 
388       rect = &rectangles->rectangles[i];
389 
390       /* Here we assume clip rects are ints for direct targets, which
391          is true for cairo */
392       clip_rect.x = (int)rect->x;
393       clip_rect.y = (int)rect->y;
394       clip_rect.width = (int)rect->width;
395       clip_rect.height = (int)rect->height;
396 
397       cairo_region_union_rectangle (region, &clip_rect);
398     }
399 
400   cairo_rectangle_list_destroy (rectangles);
401 
402   return region;
403 }
404