]> Pileus Git - ~andy/gtk/blob - gdk/gdkcairo.c
4c2d91ac0beee3ffaf97fd97c1e38c90491d335d
[~andy/gtk] / gdk / gdkcairo.c
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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "gdkcairo.h"
21 #include "gdkdrawable.h"
22 #include "gdkinternals.h"
23 #include "gdkregion-generic.h"
24 #include "gdkalias.h"
25
26 /**
27  * gdk_cairo_create:
28  * @drawable: a #GdkDrawable
29  * 
30  * Creates a Cairo context for drawing to @drawable.
31  *
32  * Return value: A newly created Cairo context. Free with
33  *  cairo_destroy() when you are done drawing.
34  * 
35  * Since: 2.8
36  **/
37 cairo_t *
38 gdk_cairo_create (GdkDrawable *drawable)
39 {
40   cairo_surface_t *surface;
41   cairo_t *cr;
42     
43   g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
44
45   surface = _gdk_drawable_ref_cairo_surface (drawable);
46   cr = cairo_create (surface);
47   cairo_surface_destroy (surface);
48
49   return cr;
50 }
51
52 /**
53  * gdk_cairo_set_source_color:
54  * @cr: a #cairo_t
55  * @color: a #GdkColor
56  * 
57  * Sets the specified #GdkColor as the source color of @cr.
58  *
59  * Since: 2.8
60  **/
61 void
62 gdk_cairo_set_source_color (cairo_t        *cr,
63                             const GdkColor *color)
64 {
65   g_return_if_fail (cr != NULL);
66   g_return_if_fail (color != NULL);
67     
68   cairo_set_source_rgb (cr,
69                         color->red / 65535.,
70                         color->green / 65535.,
71                         color->blue / 65535.);
72 }
73
74 /**
75  * gdk_cairo_rectangle:
76  * @cr: a #cairo_t
77  * @rectangle: a #GdkRectangle
78  * 
79  * Adds the given rectangle to the current path of @cr.
80  *
81  * Since: 2.8
82  **/
83 void
84 gdk_cairo_rectangle (cairo_t            *cr,
85                      const GdkRectangle *rectangle)
86 {
87   g_return_if_fail (cr != NULL);
88   g_return_if_fail (rectangle != NULL);
89
90   cairo_rectangle (cr,
91                    rectangle->x,     rectangle->y,
92                    rectangle->width, rectangle->height);
93 }
94
95 /**
96  * gdk_cairo_region:
97  * @cr: a #cairo_t
98  * @region: a #GdkRegion
99  * 
100  * Adds the given region to the current path of @cr.
101  *
102  * Since: 2.8
103  **/
104 void
105 gdk_cairo_region (cairo_t         *cr,
106                   const GdkRegion *region)
107 {
108   GdkRegionBox *boxes;
109   gint n_boxes, i;
110
111   g_return_if_fail (cr != NULL);
112   g_return_if_fail (region != NULL);
113
114   boxes = region->rects;
115   n_boxes = region->numRects;
116
117   for (i = 0; i < n_boxes; i++)
118     cairo_rectangle (cr,
119                      boxes[i].x1,
120                      boxes[i].y1,
121                      boxes[i].x2 - boxes[i].x1,
122                      boxes[i].y2 - boxes[i].y1);
123 }
124
125 /**
126  * gdk_cairo_set_source_pixbuf:
127  * @cr: a #Cairo context
128  * @pixbuf: a #GdkPixbuf
129  * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
130  * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
131  * 
132  * Sets the given pixbuf as the source pattern for the Cairo context.
133  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
134  * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y
135  *
136  * Since: 2.8
137  **/
138 void
139 gdk_cairo_set_source_pixbuf (cairo_t         *cr,
140                              const GdkPixbuf *pixbuf,
141                              double           pixbuf_x,
142                              double           pixbuf_y)
143 {
144   gint width = gdk_pixbuf_get_width (pixbuf);
145   gint height = gdk_pixbuf_get_height (pixbuf);
146   guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
147   int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
148   int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
149   int cairo_stride;
150   guchar *cairo_pixels;
151   cairo_format_t format;
152   cairo_surface_t *surface;
153   static const cairo_user_data_key_t key;
154   int j;
155
156   if (n_channels == 3)
157     format = CAIRO_FORMAT_RGB24;
158   else
159     format = CAIRO_FORMAT_ARGB32;
160
161   cairo_stride = cairo_format_stride_for_width (format, width);
162   cairo_pixels = g_malloc (height * cairo_stride);
163   surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
164                                                  format,
165                                                  width, height, cairo_stride);
166
167   cairo_surface_set_user_data (surface, &key,
168                                cairo_pixels, (cairo_destroy_func_t)g_free);
169
170   for (j = height; j; j--)
171     {
172       guchar *p = gdk_pixels;
173       guchar *q = cairo_pixels;
174
175       if (n_channels == 3)
176         {
177           guchar *end = p + 3 * width;
178           
179           while (p < end)
180             {
181 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
182               q[0] = p[2];
183               q[1] = p[1];
184               q[2] = p[0];
185 #else     
186               q[1] = p[0];
187               q[2] = p[1];
188               q[3] = p[2];
189 #endif
190               p += 3;
191               q += 4;
192             }
193         }
194       else
195         {
196           guchar *end = p + 4 * width;
197           guint t1,t2,t3;
198             
199 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
200
201           while (p < end)
202             {
203 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
204               MULT(q[0], p[2], p[3], t1);
205               MULT(q[1], p[1], p[3], t2);
206               MULT(q[2], p[0], p[3], t3);
207               q[3] = p[3];
208 #else     
209               q[0] = p[3];
210               MULT(q[1], p[0], p[3], t1);
211               MULT(q[2], p[1], p[3], t2);
212               MULT(q[3], p[2], p[3], t3);
213 #endif
214               
215               p += 4;
216               q += 4;
217             }
218           
219 #undef MULT
220         }
221
222       gdk_pixels += gdk_rowstride;
223       cairo_pixels += cairo_stride;
224     }
225
226   cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
227   cairo_surface_destroy (surface);
228 }
229
230 /**
231  * gdk_cairo_set_source_pixmap:
232  * @cr: a #Cairo context
233  * @pixmap: a #GdkPixmap
234  * @pixmap_x: X coordinate of location to place upper left corner of @pixmap
235  * @pixmap_y: Y coordinate of location to place upper left corner of @pixmap
236  * 
237  * Sets the given pixmap as the source pattern for the Cairo context.
238  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
239  * so that the origin of @pixmap is @pixmap_x, @pixmap_y
240  *
241  * Since: 2.10
242  **/
243 void
244 gdk_cairo_set_source_pixmap (cairo_t   *cr,
245                              GdkPixmap *pixmap,
246                              double     pixmap_x,
247                              double     pixmap_y)
248 {
249   cairo_surface_t *surface;
250   
251   surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (pixmap));
252   cairo_set_source_surface (cr, surface, pixmap_x, pixmap_y);
253   cairo_surface_destroy (surface);
254 }
255
256
257 #define __GDK_CAIRO_C__
258 #include "gdkaliasdef.c"