]> Pileus Git - ~andy/gtk/blob - gdk/gdkpixbuf-render.c
Deprecate gdk_set/get_use_xshm, make gdk_set_use_xshm a noop. Remove
[~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
28 \f
29
30 /**
31  * gdk_pixbuf_render_threshold_alpha:
32  * @pixbuf: A pixbuf.
33  * @bitmap: Bitmap where the bilevel mask will be painted to.
34  * @src_x: Source X coordinate.
35  * @src_y: source Y coordinate.
36  * @dest_x: Destination X coordinate.
37  * @dest_y: Destination Y coordinate.
38  * @width: Width of region to threshold.
39  * @height: Height of region to threshold.
40  * @alpha_threshold: Opacity values below this will be painted as zero; all
41  * other values will be painted as one.
42  *
43  * Takes the opacity values in a rectangular portion of a pixbuf and thresholds
44  * them to produce a bi-level alpha mask that can be used as a clipping mask for
45  * a drawable.
46  *
47  **/
48 void
49 gdk_pixbuf_render_threshold_alpha (GdkPixbuf *pixbuf,
50                                    GdkBitmap *bitmap,
51                                    int src_x,  int src_y,
52                                    int dest_x, int dest_y,
53                                    int width,  int height,
54                                    int alpha_threshold)
55 {
56   GdkGC *gc;
57   GdkColor color;
58   int x, y;
59   guchar *p;
60   int start, start_status;
61   int status;
62
63   g_return_if_fail (pixbuf != NULL);
64   g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
65   g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
66   g_return_if_fail (pixbuf->bits_per_sample == 8);
67
68   g_return_if_fail (bitmap != NULL);
69   g_return_if_fail (width >= 0 && height >= 0);
70   g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
71   g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
72
73   g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);
74
75   if (width == 0 || height == 0)
76     return;
77
78   gc = gdk_gc_new (bitmap);
79
80   if (!pixbuf->has_alpha)
81     {
82       color.pixel = (alpha_threshold == 255) ? 0 : 1;
83       gdk_gc_set_foreground (gc, &color);
84       gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
85       gdk_gc_unref (gc);
86       return;
87     }
88
89   color.pixel = 0;
90   gdk_gc_set_foreground (gc, &color);
91   gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
92
93   color.pixel = 1;
94   gdk_gc_set_foreground (gc, &color);
95
96   for (y = 0; y < height; y++)
97     {
98       p = (pixbuf->pixels + (y + src_y) * pixbuf->rowstride + src_x * pixbuf->n_channels
99            + pixbuf->n_channels - 1);
100             
101       start = 0;
102       start_status = *p < alpha_threshold;
103             
104       for (x = 0; x < width; x++)
105         {
106           status = *p < alpha_threshold;
107           
108           if (status != start_status)
109             {
110               if (!start_status)
111                 gdk_draw_line (bitmap, gc,
112                                start + dest_x, y + dest_y,
113                                x - 1 + dest_x, y + dest_y);
114               
115               start = x;
116               start_status = status;
117             }
118           
119           p += pixbuf->n_channels;
120         }
121       
122       if (!start_status)
123         gdk_draw_line (bitmap, gc,
124                        start + dest_x, y + dest_y,
125                        x - 1 + dest_x, y + dest_y);
126     }
127         
128   gdk_gc_unref (gc);
129 }
130
131 \f
132
133 /**
134  * gdk_pixbuf_render_to_drawable:
135  * @pixbuf: A pixbuf.
136  * @drawable: Destination drawable.
137  * @gc: GC used for rendering.
138  * @src_x: Source X coordinate within pixbuf.
139  * @src_y: Source Y coordinate within pixbuf.
140  * @dest_x: Destination X coordinate within drawable.
141  * @dest_y: Destination Y coordinate within drawable.
142  * @width: Width of region to render, in pixels.
143  * @height: Height of region to render, in pixels.
144  * @dither: Dithering mode for GdkRGB.
145  * @x_dither: X offset for dither.
146  * @y_dither: Y offset for dither.
147  *
148  * Renders a rectangular portion of a pixbuf to a drawable while using the
149  * specified GC.  This is done using GdkRGB, so the specified drawable must have
150  * the GdkRGB visual and colormap.  Note that this function will ignore the
151  * opacity information for images with an alpha channel; the GC must already
152  * have the clipping mask set if you want transparent regions to show through.
153  *
154  * For an explanation of dither offsets, see the GdkRGB documentation.  In
155  * brief, the dither offset is important when re-rendering partial regions of an
156  * image to a rendered version of the full image, or for when the offsets to a
157  * base position change, as in scrolling.  The dither matrix has to be shifted
158  * for consistent visual results.  If you do not have any of these cases, the
159  * dither offsets can be both zero.
160  **/
161 void
162 gdk_pixbuf_render_to_drawable (GdkPixbuf   *pixbuf,
163                                GdkDrawable *drawable,
164                                GdkGC       *gc,
165                                int src_x,    int src_y,
166                                int dest_x,   int dest_y,
167                                int width,    int height,
168                                GdkRgbDither dither,
169                                int x_dither, int y_dither)
170 {
171   int rowstride;
172   guchar *buf;
173
174   g_return_if_fail (pixbuf != NULL);
175   g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
176   g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
177   g_return_if_fail (pixbuf->bits_per_sample == 8);
178
179   g_return_if_fail (drawable != NULL);
180   g_return_if_fail (gc != NULL);
181
182   g_return_if_fail (width >= 0 && height >= 0);
183   g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
184   g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
185
186   if (width == 0 || height == 0)
187     return;
188
189   /* This will have to be modified once we support other image types.
190    */
191
192   if (pixbuf->n_channels == 4)
193     {
194       buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4;
195       rowstride = pixbuf->rowstride;
196
197       gdk_draw_rgb_32_image_dithalign (drawable, gc,
198                                        dest_x, dest_y,
199                                        width, height,
200                                        dither,
201                                        buf, rowstride,
202                                        x_dither, y_dither);
203     }
204   else                          /* n_channels == 3 */
205     {
206       buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
207       rowstride = pixbuf->rowstride;
208
209       gdk_draw_rgb_image_dithalign (drawable, gc,
210                                     dest_x, dest_y,
211                                     width, height,
212                                     dither,
213                                     buf, rowstride,
214                                     x_dither, y_dither);
215     }
216 }
217
218 \f
219
220 /**
221  * gdk_pixbuf_render_to_drawable_alpha:
222  * @pixbuf: A pixbuf.
223  * @drawable: Destination drawable.
224  * @src_x: Source X coordinate within pixbuf.
225  * @src_y: Source Y coordinates within pixbuf.
226  * @dest_x: Destination X coordinate within drawable.
227  * @dest_y: Destination Y coordinate within drawable.
228  * @width: Width of region to render, in pixels.
229  * @height: Height of region to render, in pixels.
230  * @alpha_mode: If the image does not have opacity information, this is ignored.
231  * Otherwise, specifies how to handle transparency when rendering.
232  * @alpha_threshold: If the image does have opacity information and @alpha_mode
233  * is GDK_PIXBUF_ALPHA_BILEVEL, specifies the threshold value for opacity
234  * values.
235  * @dither: Dithering mode for GdkRGB.
236  * @x_dither: X offset for dither.
237  * @y_dither: Y offset for dither.
238  *
239  * Renders a rectangular portion of a pixbuf to a drawable.  This is done using
240  * GdkRGB, so the specified drawable must have the GdkRGB visual and colormap.
241  *
242  * When used with #GDK_PIXBUF_ALPHA_BILEVEL, this function has to create a bitmap
243  * out of the thresholded alpha channel of the image and, it has to set this
244  * bitmap as the clipping mask for the GC used for drawing.  This can be a
245  * significant performance penalty depending on the size and the complexity of
246  * the alpha channel of the image.  If performance is crucial, consider handling
247  * the alpha channel yourself (possibly by caching it in your application) and
248  * using gdk_pixbuf_render_to_drawable() or GdkRGB directly instead.
249  *
250  * The #GDK_PIXBUF_ALPHA_FULL mode involves round trips to the X
251  * server, and may also be somewhat slow in its current implementation
252  * (though in the future it could be made significantly faster, in
253  * principle).
254  **/
255 void
256 gdk_pixbuf_render_to_drawable_alpha (GdkPixbuf   *pixbuf,
257                                      GdkDrawable *drawable,
258                                      int src_x,    int src_y,
259                                      int dest_x,   int dest_y,
260                                      int width,    int height,
261                                      GdkPixbufAlphaMode alpha_mode,
262                                      int                alpha_threshold,
263                                      GdkRgbDither       dither,
264                                      int x_dither, int y_dither)
265 {
266   GdkBitmap *bitmap = NULL;
267   GdkGC *gc;
268   GdkPixbuf *composited = NULL;
269   gint dwidth, dheight;
270   GdkRegion *clip;
271   GdkRegion *drect;
272   GdkRectangle tmp_rect;
273   
274   g_return_if_fail (pixbuf != NULL);
275   g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
276   g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
277   g_return_if_fail (pixbuf->bits_per_sample == 8);
278
279   g_return_if_fail (drawable != NULL);
280   g_return_if_fail (width >= 0 && height >= 0);
281   g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
282   g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
283
284   /* Clip to the drawable; this is required for get_from_drawable() so
285    * can't be done implicitly
286    */
287   
288   if (dest_x < 0)
289     {
290       src_x -= dest_x;
291       width += dest_x;
292       dest_x = 0;
293     }
294
295   if (dest_y < 0)
296     {
297       src_y -= dest_y;
298       height += dest_y;
299       dest_y = 0;
300     }
301
302   gdk_drawable_get_size (drawable, &dwidth, &dheight);
303
304   if ((dest_x + width) > dwidth)
305     width = dwidth - dest_x;
306
307   if ((dest_y + height) > dheight)
308     height = dheight - dest_y;
309
310   if (width <= 0 || height <= 0)
311     return;
312
313   /* Clip to the clip region; this avoids getting more
314    * image data from the server than we need to.
315    */
316   
317   tmp_rect.x = dest_x;
318   tmp_rect.y = dest_y;
319   tmp_rect.width = width;
320   tmp_rect.height = height;
321
322   drect = gdk_region_rectangle (&tmp_rect);
323   clip = gdk_drawable_get_clip_region (drawable);
324
325   gdk_region_intersect (drect, clip);
326
327   gdk_region_get_clipbox (drect, &tmp_rect);
328   
329   gdk_region_destroy (drect);
330   gdk_region_destroy (clip);
331
332   if (tmp_rect.width == 0 ||
333       tmp_rect.height == 0)
334     return;
335   
336   /* Actually draw */
337   
338   gc = gdk_gc_new (drawable);
339
340   if (pixbuf->has_alpha)
341     {
342       if (alpha_mode == GDK_PIXBUF_ALPHA_FULL)
343         {
344           GdkPixbuf *sub = NULL;
345           
346           composited = gdk_pixbuf_get_from_drawable (NULL,
347                                                      drawable,
348                                                      NULL,
349                                                      dest_x, dest_y,
350                                                      0, 0,
351                                                      width, height);
352
353           if (composited)
354             {
355               if (src_x != 0 || src_y != 0)
356                 {
357                   sub = gdk_pixbuf_new_subpixbuf (pixbuf, src_x, src_y,
358                                                   width, height);
359                 }
360               
361               gdk_pixbuf_composite (sub ? sub : pixbuf,
362                                     composited,
363                                     0, 0,
364                                     width, height,
365                                     0, 0,
366                                     1.0, 1.0,
367                                     GDK_INTERP_BILINEAR,
368                                     255);
369               
370               if (sub)
371                 g_object_unref (G_OBJECT (sub));
372             }
373           else
374             alpha_mode = GDK_PIXBUF_ALPHA_BILEVEL; /* fall back */
375         }
376
377       if (alpha_mode == GDK_PIXBUF_ALPHA_BILEVEL)
378         {
379           bitmap = gdk_pixmap_new (NULL, width, height, 1);
380           gdk_pixbuf_render_threshold_alpha (pixbuf, bitmap,
381                                              src_x, src_y,
382                                              0, 0,
383                                              width, height,
384                                              alpha_threshold);
385           
386           gdk_gc_set_clip_mask (gc, bitmap);
387           gdk_gc_set_clip_origin (gc, dest_x, dest_y);
388         }
389     }
390
391   if (composited)
392     {
393       gdk_pixbuf_render_to_drawable (composited,
394                                      drawable, gc,
395                                      0, 0,
396                                      dest_x, dest_y,
397                                      width, height,
398                                      dither,
399                                      x_dither, y_dither);
400     }
401   else
402     {
403       gdk_pixbuf_render_to_drawable (pixbuf,
404                                      drawable, gc,
405                                      src_x, src_y,
406                                      dest_x, dest_y,
407                                      width, height,
408                                      dither,
409                                      x_dither, y_dither);
410     }
411
412   if (bitmap)
413     gdk_bitmap_unref (bitmap);
414
415   if (composited)
416     g_object_unref (G_OBJECT (composited));
417
418   gdk_gc_unref (gc);
419 }
420
421 /**
422  * gdk_pixbuf_render_pixmap_and_mask:
423  * @pixbuf: A pixbuf.
424  * @pixmap_return: Return value for the created pixmap.
425  * @mask_return: Return value for the created mask.
426  * @alpha_threshold: Threshold value for opacity values.
427  *
428  * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
429  * and @mask_return arguments, respectively, and renders a pixbuf and its
430  * corresponding thresholded alpha mask to them.  This is merely a convenience
431  * function; applications that need to render pixbufs with dither offsets or to
432  * given drawables should use gdk_pixbuf_render_to_drawable_alpha() or
433  * gdk_pixbuf_render_to_drawable(), and gdk_pixbuf_render_threshold_alpha().
434  *
435  * The pixmap that is created is created for the colormap returned
436  * by gdk_rgb_get_colormap(). You normally will want to instead use
437  * the actual colormap for a widget, and use
438  * gdk_pixbuf_render_pixmap_and_mask_for_colormap.
439  *
440  * If the pixbuf does not have an alpha channel, then *@mask_return will be set
441  * to NULL.
442  **/
443 void
444 gdk_pixbuf_render_pixmap_and_mask (GdkPixbuf  *pixbuf,
445                                    GdkPixmap **pixmap_return,
446                                    GdkBitmap **mask_return,
447                                    int         alpha_threshold)
448 {
449   gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf,
450                                                   gdk_rgb_get_colormap (),
451                                                   pixmap_return, mask_return,
452                                                   alpha_threshold);
453 }
454
455 /**
456  * gdk_pixbuf_render_pixmap_and_mask_for_colormap:
457  * @pixbuf: A pixbuf.
458  * @colormap: A #GdkColormap
459  * @pixmap_return: Return value for the created pixmap.
460  * @mask_return: Return value for the created mask.
461  * @alpha_threshold: Threshold value for opacity values.
462  *
463  * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
464  * and @mask_return arguments, respectively, and renders a pixbuf and its
465  * corresponding tresholded alpha mask to them.  This is merely a convenience
466  * function; applications that need to render pixbufs with dither offsets or to
467  * given drawables should use gdk_pixbuf_render_to_drawable_alpha() or
468  * gdk_pixbuf_render_to_drawable(), and gdk_pixbuf_render_threshold_alpha().
469  *
470  * The pixmap that is created uses the #GdkColormap specified by @colormap.
471  * This colormap must match the colormap of the window where the pixmap
472  * will eventually be used or an error will result.
473  *
474  * If the pixbuf does not have an alpha channel, then *@mask_return will be set
475  * to NULL.
476  **/
477 void
478 gdk_pixbuf_render_pixmap_and_mask_for_colormap (GdkPixbuf   *pixbuf,
479                                                 GdkColormap *colormap,
480                                                 GdkPixmap  **pixmap_return,
481                                                 GdkBitmap  **mask_return,
482                                                 int          alpha_threshold)
483 {
484   g_return_if_fail (pixbuf != NULL);
485   
486   if (pixmap_return)
487     {
488       GdkGC *gc;
489       
490       *pixmap_return = gdk_pixmap_new (NULL, gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
491                                        gdk_colormap_get_visual (colormap)->depth);
492       gdk_drawable_set_colormap (GDK_DRAWABLE (*pixmap_return),
493                                  colormap);
494       gc = gdk_gc_new (*pixmap_return);
495       gdk_pixbuf_render_to_drawable (pixbuf, *pixmap_return, gc,
496                                      0, 0, 0, 0,
497                                      gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
498                                      GDK_RGB_DITHER_NORMAL,
499                                      0, 0);
500       gdk_gc_unref (gc);
501     }
502   
503   if (mask_return)
504     {
505       if (gdk_pixbuf_get_has_alpha (pixbuf))
506         {
507           *mask_return = gdk_pixmap_new (NULL, gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf), 1);
508           
509           gdk_pixbuf_render_threshold_alpha (pixbuf, *mask_return,
510                                              0, 0, 0, 0,
511                                              gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
512                                              alpha_threshold);
513         }
514       else
515         *mask_return = NULL;
516     }
517 }
518
519