]> Pileus Git - ~andy/gtk/blob - gdk/gdkpixbuf-render.c
Change prototype to match cairo_set_source_surface().
[~andy/gtk] / gdk / gdkpixbuf-render.c
1 /* GdkPixbuf library - Rendering functions
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
5  * Author: Federico Mena-Quintero <federico@gimp.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <config.h>
24 #include <gdk/gdk.h>
25 #include "gdk-pixbuf-private.h"
26 #include "gdkpixbuf.h"
27 #include "gdkscreen.h"
28 #include "gdkinternals.h"
29 #include "gdkalias.h"
30
31 \f
32
33 /**
34  * gdk_pixbuf_render_threshold_alpha:
35  * @pixbuf: A pixbuf.
36  * @bitmap: Bitmap where the bilevel mask will be painted to.
37  * @src_x: Source X coordinate.
38  * @src_y: source Y coordinate.
39  * @dest_x: Destination X coordinate.
40  * @dest_y: Destination Y coordinate.
41  * @width: Width of region to threshold, or -1 to use pixbuf width
42  * @height: Height of region to threshold, or -1 to use pixbuf height
43  * @alpha_threshold: Opacity values below this will be painted as zero; all
44  * other values will be painted as one.
45  *
46  * Takes the opacity values in a rectangular portion of a pixbuf and thresholds
47  * them to produce a bi-level alpha mask that can be used as a clipping mask for
48  * a drawable.
49  *
50  **/
51 void
52 gdk_pixbuf_render_threshold_alpha (GdkPixbuf *pixbuf,
53                                    GdkBitmap *bitmap,
54                                    int src_x,  int src_y,
55                                    int dest_x, int dest_y,
56                                    int width,  int height,
57                                    int alpha_threshold)
58 {
59   GdkGC *gc;
60   GdkColor color;
61   int x, y;
62   guchar *p;
63   int start, start_status;
64   int status;
65
66   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
67   g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
68   g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
69   g_return_if_fail (pixbuf->bits_per_sample == 8);
70
71   if (width == -1) 
72     width = pixbuf->width;
73   if (height == -1)
74     height = pixbuf->height;
75
76   g_return_if_fail (bitmap != NULL);
77   g_return_if_fail (width >= 0 && height >= 0);
78   g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
79   g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
80
81   g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);
82
83   if (width == 0 || height == 0)
84     return;
85
86   gc = _gdk_drawable_get_scratch_gc (bitmap, FALSE);
87
88   if (!pixbuf->has_alpha)
89     {
90       color.pixel = (alpha_threshold == 255) ? 0 : 1;
91       gdk_gc_set_foreground (gc, &color);
92       gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
93       return;
94     }
95
96   color.pixel = 0;
97   gdk_gc_set_foreground (gc, &color);
98   gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
99
100   color.pixel = 1;
101   gdk_gc_set_foreground (gc, &color);
102
103   for (y = 0; y < height; y++)
104     {
105       p = (pixbuf->pixels + (y + src_y) * pixbuf->rowstride + src_x * pixbuf->n_channels
106            + pixbuf->n_channels - 1);
107             
108       start = 0;
109       start_status = *p < alpha_threshold;
110             
111       for (x = 0; x < width; x++)
112         {
113           status = *p < alpha_threshold;
114           
115           if (status != start_status)
116             {
117               if (!start_status)
118                 gdk_draw_line (bitmap, gc,
119                                start + dest_x, y + dest_y,
120                                x - 1 + dest_x, y + dest_y);
121               
122               start = x;
123               start_status = status;
124             }
125           
126           p += pixbuf->n_channels;
127         }
128       
129       if (!start_status)
130         gdk_draw_line (bitmap, gc,
131                        start + dest_x, y + dest_y,
132                        x - 1 + dest_x, y + dest_y);
133     }
134 }
135
136 \f
137
138 /**
139  * gdk_pixbuf_render_to_drawable:
140  * @pixbuf: A pixbuf.
141  * @drawable: Destination drawable.
142  * @gc: GC used for rendering.
143  * @src_x: Source X coordinate within pixbuf.
144  * @src_y: Source Y coordinate within pixbuf.
145  * @dest_x: Destination X coordinate within drawable.
146  * @dest_y: Destination Y coordinate within drawable.
147  * @width: Width of region to render, in pixels, or -1 to use pixbuf width
148  * @height: Height of region to render, in pixels, or -1 to use pixbuf height
149  * @dither: Dithering mode for GdkRGB.
150  * @x_dither: X offset for dither.
151  * @y_dither: Y offset for dither.
152  *
153  * Renders a rectangular portion of a pixbuf to a drawable while using the
154  * specified GC.  This is done using GdkRGB, so the specified drawable must have
155  * the GdkRGB visual and colormap.  Note that this function will ignore the
156  * opacity information for images with an alpha channel; the GC must already
157  * have the clipping mask set if you want transparent regions to show through.
158  *
159  * For an explanation of dither offsets, see the GdkRGB documentation.  In
160  * brief, the dither offset is important when re-rendering partial regions of an
161  * image to a rendered version of the full image, or for when the offsets to a
162  * base position change, as in scrolling.  The dither matrix has to be shifted
163  * for consistent visual results.  If you do not have any of these cases, the
164  * dither offsets can be both zero.
165  *
166  * Deprecated: This function is obsolete. Use gdk_draw_pixbuf() instead.
167  **/
168 void
169 gdk_pixbuf_render_to_drawable (GdkPixbuf   *pixbuf,
170                                GdkDrawable *drawable,
171                                GdkGC       *gc,
172                                int src_x,    int src_y,
173                                int dest_x,   int dest_y,
174                                int width,    int height,
175                                GdkRgbDither dither,
176                                int x_dither, int y_dither)
177 {
178   gdk_draw_pixbuf (drawable, gc, pixbuf,
179                    src_x, src_y, dest_x, dest_y, width, height,
180                    dither, x_dither, y_dither);
181 }
182
183 \f
184
185 /**
186  * gdk_pixbuf_render_to_drawable_alpha:
187  * @pixbuf: A pixbuf.
188  * @drawable: Destination drawable.
189  * @src_x: Source X coordinate within pixbuf.
190  * @src_y: Source Y coordinates within pixbuf.
191  * @dest_x: Destination X coordinate within drawable.
192  * @dest_y: Destination Y coordinate within drawable.
193  * @width: Width of region to render, in pixels, or -1 to use pixbuf width.
194  * @height: Height of region to render, in pixels, or -1 to use pixbuf height.
195  * @alpha_mode: Ignored. Present for backwards compatibility.
196  * @alpha_threshold: Ignored. Present for backwards compatibility
197  * @dither: Dithering mode for GdkRGB.
198  * @x_dither: X offset for dither.
199  * @y_dither: Y offset for dither.
200  *
201  * Renders a rectangular portion of a pixbuf to a drawable.  The destination
202  * drawable must have a colormap. All windows have a colormap, however, pixmaps
203  * only have colormap by default if they were created with a non-NULL window argument.
204  * Otherwise a colormap must be set on them with gdk_drawable_set_colormap.
205  *
206  * On older X servers, rendering pixbufs with an alpha channel involves round trips
207  * to the X server, and may be somewhat slow.
208  *
209  * Deprecated: This function is obsolete. Use gdk_draw_pixbuf() instead.
210  **/
211 void
212 gdk_pixbuf_render_to_drawable_alpha (GdkPixbuf   *pixbuf,
213                                      GdkDrawable *drawable,
214                                      int src_x,    int src_y,
215                                      int dest_x,   int dest_y,
216                                      int width,    int height,
217                                      GdkPixbufAlphaMode alpha_mode,
218                                      int                alpha_threshold,
219                                      GdkRgbDither       dither,
220                                      int x_dither, int y_dither)
221 {
222   gdk_draw_pixbuf (drawable, NULL, pixbuf,
223                    src_x, src_y, dest_x, dest_y, width, height,
224                    dither, x_dither, y_dither);
225 }
226
227 /**
228  * gdk_pixbuf_render_pixmap_and_mask:
229  * @pixbuf: A pixbuf.
230  * @pixmap_return: Location to store a pointer to the created pixmap,
231  *   or %NULL if the pixmap is not needed.
232  * @mask_return: Location to store a pointer to the created mask,
233  *   or %NULL if the mask is not needed.
234  * @alpha_threshold: Threshold value for opacity values.
235  *
236  * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
237  * and @mask_return arguments, respectively, and renders a pixbuf and its
238  * corresponding thresholded alpha mask to them.  This is merely a convenience
239  * function; applications that need to render pixbufs with dither offsets or to
240  * given drawables should use gdk_draw_pixbuf() and gdk_pixbuf_render_threshold_alpha().
241  *
242  * The pixmap that is created is created for the colormap returned
243  * by gdk_rgb_get_colormap(). You normally will want to instead use
244  * the actual colormap for a widget, and use
245  * gdk_pixbuf_render_pixmap_and_mask_for_colormap.
246  *
247  * If the pixbuf does not have an alpha channel, then *@mask_return will be set
248  * to NULL.
249  **/
250 void
251 gdk_pixbuf_render_pixmap_and_mask (GdkPixbuf  *pixbuf,
252                                    GdkPixmap **pixmap_return,
253                                    GdkBitmap **mask_return,
254                                    int         alpha_threshold)
255 {
256   gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf,
257                                                   gdk_rgb_get_colormap (),
258                                                   pixmap_return, mask_return,
259                                                   alpha_threshold);
260 }
261
262 /**
263  * gdk_pixbuf_render_pixmap_and_mask_for_colormap:
264  * @pixbuf: A pixbuf.
265  * @colormap: A #GdkColormap
266  * @pixmap_return: Location to store a pointer to the created pixmap,
267  *   or %NULL if the pixmap is not needed.
268  * @mask_return: Location to store a pointer to the created mask,
269  *   or %NULL if the mask is not needed.
270  * @alpha_threshold: Threshold value for opacity values.
271  *
272  * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
273  * and @mask_return arguments, respectively, and renders a pixbuf and its
274  * corresponding tresholded alpha mask to them.  This is merely a convenience
275  * function; applications that need to render pixbufs with dither offsets or to
276  * given drawables should use gdk_draw_pixbuf(), and gdk_pixbuf_render_threshold_alpha().
277  *
278  * The pixmap that is created uses the #GdkColormap specified by @colormap.
279  * This colormap must match the colormap of the window where the pixmap
280  * will eventually be used or an error will result.
281  *
282  * If the pixbuf does not have an alpha channel, then *@mask_return will be set
283  * to NULL.
284  **/
285 void
286 gdk_pixbuf_render_pixmap_and_mask_for_colormap (GdkPixbuf   *pixbuf,
287                                                 GdkColormap *colormap,
288                                                 GdkPixmap  **pixmap_return,
289                                                 GdkBitmap  **mask_return,
290                                                 int          alpha_threshold)
291 {
292   GdkScreen *screen;
293
294   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
295   g_return_if_fail (GDK_IS_COLORMAP (colormap));
296
297   screen = gdk_colormap_get_screen (colormap);
298   
299   if (pixmap_return)
300     {
301       GdkGC *gc;
302       *pixmap_return = gdk_pixmap_new (gdk_screen_get_root_window (screen),
303                                        gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
304                                        gdk_colormap_get_visual (colormap)->depth);
305
306       gdk_drawable_set_colormap (GDK_DRAWABLE (*pixmap_return), colormap);
307       gc = _gdk_drawable_get_scratch_gc (*pixmap_return, FALSE);
308       gdk_draw_pixbuf (*pixmap_return, gc, pixbuf, 
309                        0, 0, 0, 0,
310                        gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
311                        GDK_RGB_DITHER_NORMAL,
312                        0, 0);
313     }
314   
315   if (mask_return)
316     {
317       if (gdk_pixbuf_get_has_alpha (pixbuf))
318         {
319           *mask_return = gdk_pixmap_new (gdk_screen_get_root_window (screen),
320                                          gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf), 1);
321
322           gdk_pixbuf_render_threshold_alpha (pixbuf, *mask_return,
323                                              0, 0, 0, 0,
324                                              gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
325                                              alpha_threshold);
326         }
327       else
328         *mask_return = NULL;
329     }
330 }
331
332 /**
333  * gdk_pixbuf_set_as_cairo_source:
334  * @pixbuf: a #GdkPixbuf
335  * @cr: a #Cairo context
336  * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
337  * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
338  * 
339  * Sets the given pixbuf as the source pattern for the Cairo context.
340  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
341  * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y
342  **/
343 void
344 gdk_pixbuf_set_as_cairo_source (GdkPixbuf *pixbuf,
345                                 cairo_t   *cr,
346                                 double     pixbuf_x,
347                                 double     pixbuf_y)
348 {
349   gint width = gdk_pixbuf_get_width (pixbuf);
350   gint height = gdk_pixbuf_get_height (pixbuf);
351   guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
352   int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
353   int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
354   guchar *cairo_pixels;
355   cairo_format_t format;
356   cairo_surface_t *surface;
357   static const cairo_user_data_key_t key;
358   int j;
359
360   if (n_channels == 3)
361     format = CAIRO_FORMAT_RGB24;
362   else
363     format = CAIRO_FORMAT_ARGB32;
364
365   cairo_pixels = g_malloc (4 * width * height);
366   surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
367                                                  format,
368                                                  width, height, 4 * width);
369   cairo_surface_set_user_data (surface, &key,
370                                cairo_pixels, (cairo_destroy_func_t)g_free);
371
372   for (j = height; j; j--)
373     {
374       guchar *p = gdk_pixels;
375       guchar *q = cairo_pixels;
376
377       if (n_channels == 3)
378         {
379           guchar *end = p + 3 * width;
380           
381           while (p < end)
382             {
383 #if G_BYTE_ORDER == GDK_LSB_FIRST
384               q[0] = p[2];
385               q[1] = p[1];
386               q[2] = p[2];
387 #else     
388               q[0] = p[0];
389               q[1] = p[1];
390               q[2] = p[2];
391 #endif
392               p += 3;
393               q += 4;
394             }
395         }
396       else
397         {
398           guchar *end = p + 4 * width;
399           guint t1,t2,t3;
400             
401 #define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
402
403           while (p < end)
404             {
405 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
406               MULT(q[0], p[2], p[3], t1);
407               MULT(q[1], p[1], p[3], t2);
408               MULT(q[2], p[0], p[3], t3);
409               q[3] = p[3];
410 #else     
411               q[0] = p[3];
412               MULT(q[1], p[0], p[3], t1);
413               MULT(q[2], p[1], p[3], t2);
414               MULT(q[3], p[2], p[3], t3);
415 #endif
416               
417               p += 4;
418               q += 4;
419             }
420           
421 #undef MULT
422         }
423
424       gdk_pixels += gdk_rowstride;
425       cairo_pixels += 4 * width;
426     }
427
428   cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
429 }
430
431 #define __GDK_PIXBUF_RENDER_C__
432 #include "gdkaliasdef.c"
433