]> Pileus Git - ~andy/gtk/blob - gdk/gdkpixbuf-render.c
daaf6b31bb2cef9bc0f9f5db47e67ad621f1dcdf
[~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, or -1 to use pixbuf width
39  * @height: Height of region to threshold, or -1 to use pixbuf height
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 (GDK_IS_PIXBUF (pixbuf));
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   if (width == -1) 
69     width = pixbuf->width;
70   if (height == -1)
71     height = pixbuf->height;
72
73   g_return_if_fail (bitmap != NULL);
74   g_return_if_fail (width >= 0 && height >= 0);
75   g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
76   g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
77
78   g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);
79
80   if (width == 0 || height == 0)
81     return;
82
83   gc = gdk_gc_new (bitmap);
84
85   if (!pixbuf->has_alpha)
86     {
87       color.pixel = (alpha_threshold == 255) ? 0 : 1;
88       gdk_gc_set_foreground (gc, &color);
89       gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
90       gdk_gc_unref (gc);
91       return;
92     }
93
94   color.pixel = 0;
95   gdk_gc_set_foreground (gc, &color);
96   gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
97
98   color.pixel = 1;
99   gdk_gc_set_foreground (gc, &color);
100
101   for (y = 0; y < height; y++)
102     {
103       p = (pixbuf->pixels + (y + src_y) * pixbuf->rowstride + src_x * pixbuf->n_channels
104            + pixbuf->n_channels - 1);
105             
106       start = 0;
107       start_status = *p < alpha_threshold;
108             
109       for (x = 0; x < width; x++)
110         {
111           status = *p < alpha_threshold;
112           
113           if (status != start_status)
114             {
115               if (!start_status)
116                 gdk_draw_line (bitmap, gc,
117                                start + dest_x, y + dest_y,
118                                x - 1 + dest_x, y + dest_y);
119               
120               start = x;
121               start_status = status;
122             }
123           
124           p += pixbuf->n_channels;
125         }
126       
127       if (!start_status)
128         gdk_draw_line (bitmap, gc,
129                        start + dest_x, y + dest_y,
130                        x - 1 + dest_x, y + dest_y);
131     }
132         
133   gdk_gc_unref (gc);
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 void
167 gdk_pixbuf_render_to_drawable (GdkPixbuf   *pixbuf,
168                                GdkDrawable *drawable,
169                                GdkGC       *gc,
170                                int src_x,    int src_y,
171                                int dest_x,   int dest_y,
172                                int width,    int height,
173                                GdkRgbDither dither,
174                                int x_dither, int y_dither)
175 {
176   int rowstride;
177   guchar *buf;
178
179   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
180   g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
181   g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
182   g_return_if_fail (pixbuf->bits_per_sample == 8);
183
184   g_return_if_fail (drawable != NULL);
185   g_return_if_fail (gc != NULL);
186
187   if (width == -1) 
188     width = pixbuf->width;
189   if (height == -1)
190     height = pixbuf->height;
191
192   g_return_if_fail (width >= 0 && height >= 0);
193   g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
194   g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
195
196   if (width == 0 || height == 0)
197     return;
198
199   /* This will have to be modified once we support other image types.
200    */
201
202   if (pixbuf->n_channels == 4)
203     {
204       buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4;
205       rowstride = pixbuf->rowstride;
206
207       gdk_draw_rgb_32_image_dithalign (drawable, gc,
208                                        dest_x, dest_y,
209                                        width, height,
210                                        dither,
211                                        buf, rowstride,
212                                        x_dither, y_dither);
213     }
214   else                          /* n_channels == 3 */
215     {
216       buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
217       rowstride = pixbuf->rowstride;
218
219       gdk_draw_rgb_image_dithalign (drawable, gc,
220                                     dest_x, dest_y,
221                                     width, height,
222                                     dither,
223                                     buf, rowstride,
224                                     x_dither, y_dither);
225     }
226 }
227
228 \f
229
230 /**
231  * gdk_pixbuf_render_to_drawable_alpha:
232  * @pixbuf: A pixbuf.
233  * @drawable: Destination drawable.
234  * @src_x: Source X coordinate within pixbuf.
235  * @src_y: Source Y coordinates within pixbuf.
236  * @dest_x: Destination X coordinate within drawable.
237  * @dest_y: Destination Y coordinate within drawable.
238  * @width: Width of region to render, in pixels, or -1 to use pixbuf width.
239  * @height: Height of region to render, in pixels, or -1 to use pixbuf height.
240  * @alpha_mode: Ignored. Present for backwards compatibility.
241  * @alpha_threshold: Ignored. Present for backwards compatibility
242  * @dither: Dithering mode for GdkRGB.
243  * @x_dither: X offset for dither.
244  * @y_dither: Y offset for dither.
245  *
246  * Renders a rectangular portion of a pixbuf to a drawable.  The destination
247  * drawable must have a colormap. All windows have a colormap, however, pixmaps
248  * only have colormap by default if they were created with a non-NULL window argument.
249  * Otherwise a colormap must be set on them with gdk_drawable_set_colormap.
250  *
251  * On older X servers, rendering pixbufs with an alpha channel involves round trips
252  * to the X server, and may be somewhat slow.
253  **/
254 void
255 gdk_pixbuf_render_to_drawable_alpha (GdkPixbuf   *pixbuf,
256                                      GdkDrawable *drawable,
257                                      int src_x,    int src_y,
258                                      int dest_x,   int dest_y,
259                                      int width,    int height,
260                                      GdkPixbufAlphaMode alpha_mode,
261                                      int                alpha_threshold,
262                                      GdkRgbDither       dither,
263                                      int x_dither, int y_dither)
264 {
265   _gdk_draw_pixbuf (drawable, NULL, pixbuf,
266                     src_x, src_y, dest_x, dest_y, width, height,
267                     dither, x_dither, y_dither);
268 }
269
270 /**
271  * gdk_pixbuf_render_pixmap_and_mask:
272  * @pixbuf: A pixbuf.
273  * @pixmap_return: Return value for the created pixmap.
274  * @mask_return: Return value for the created mask.
275  * @alpha_threshold: Threshold value for opacity values.
276  *
277  * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
278  * and @mask_return arguments, respectively, and renders a pixbuf and its
279  * corresponding thresholded alpha mask to them.  This is merely a convenience
280  * function; applications that need to render pixbufs with dither offsets or to
281  * given drawables should use gdk_pixbuf_render_to_drawable_alpha() or
282  * gdk_pixbuf_render_to_drawable(), and gdk_pixbuf_render_threshold_alpha().
283  *
284  * The pixmap that is created is created for the colormap returned
285  * by gdk_rgb_get_colormap(). You normally will want to instead use
286  * the actual colormap for a widget, and use
287  * gdk_pixbuf_render_pixmap_and_mask_for_colormap.
288  *
289  * If the pixbuf does not have an alpha channel, then *@mask_return will be set
290  * to NULL.
291  **/
292 void
293 gdk_pixbuf_render_pixmap_and_mask (GdkPixbuf  *pixbuf,
294                                    GdkPixmap **pixmap_return,
295                                    GdkBitmap **mask_return,
296                                    int         alpha_threshold)
297 {
298   gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf,
299                                                   gdk_rgb_get_colormap (),
300                                                   pixmap_return, mask_return,
301                                                   alpha_threshold);
302 }
303
304 /**
305  * gdk_pixbuf_render_pixmap_and_mask_for_colormap:
306  * @pixbuf: A pixbuf.
307  * @colormap: A #GdkColormap
308  * @pixmap_return: Return value for the created pixmap.
309  * @mask_return: Return value for the created mask.
310  * @alpha_threshold: Threshold value for opacity values.
311  *
312  * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
313  * and @mask_return arguments, respectively, and renders a pixbuf and its
314  * corresponding tresholded alpha mask to them.  This is merely a convenience
315  * function; applications that need to render pixbufs with dither offsets or to
316  * given drawables should use gdk_pixbuf_render_to_drawable_alpha() or
317  * gdk_pixbuf_render_to_drawable(), and gdk_pixbuf_render_threshold_alpha().
318  *
319  * The pixmap that is created uses the #GdkColormap specified by @colormap.
320  * This colormap must match the colormap of the window where the pixmap
321  * will eventually be used or an error will result.
322  *
323  * If the pixbuf does not have an alpha channel, then *@mask_return will be set
324  * to NULL.
325  **/
326 void
327 gdk_pixbuf_render_pixmap_and_mask_for_colormap (GdkPixbuf   *pixbuf,
328                                                 GdkColormap *colormap,
329                                                 GdkPixmap  **pixmap_return,
330                                                 GdkBitmap  **mask_return,
331                                                 int          alpha_threshold)
332 {
333   g_return_if_fail (pixbuf != NULL);
334   
335   if (pixmap_return)
336     {
337       GdkGC *gc;
338       
339       *pixmap_return = gdk_pixmap_new (NULL, gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
340                                        gdk_colormap_get_visual (colormap)->depth);
341       gdk_drawable_set_colormap (GDK_DRAWABLE (*pixmap_return),
342                                  colormap);
343       gc = gdk_gc_new (*pixmap_return);
344       gdk_pixbuf_render_to_drawable (pixbuf, *pixmap_return, gc,
345                                      0, 0, 0, 0,
346                                      gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
347                                      GDK_RGB_DITHER_NORMAL,
348                                      0, 0);
349       gdk_gc_unref (gc);
350     }
351   
352   if (mask_return)
353     {
354       if (gdk_pixbuf_get_has_alpha (pixbuf))
355         {
356           *mask_return = gdk_pixmap_new (NULL, gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf), 1);
357           
358           gdk_pixbuf_render_threshold_alpha (pixbuf, *mask_return,
359                                              0, 0, 0, 0,
360                                              gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
361                                              alpha_threshold);
362         }
363       else
364         *mask_return = NULL;
365     }
366 }