]> Pileus Git - ~andy/gtk/blob - gdk/gdkpixbuf-render.c
Welcome aboard, gdk-pixbuf.
[~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 Library 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  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library 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, GdkBitmap *bitmap,
50                                    int src_x, int src_y,
51                                    int dest_x, int dest_y,
52                                    int width, int height,
53                                    int alpha_threshold)
54 {
55         GdkGC *gc;
56         GdkColor color;
57         int x, y;
58         guchar *p;
59         int start, start_status;
60         int status;
61
62         g_return_if_fail (pixbuf != NULL);
63         g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
64         g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
65         g_return_if_fail (pixbuf->bits_per_sample == 8);
66
67         g_return_if_fail (bitmap != NULL);
68         g_return_if_fail (width >= 0 && height >= 0);
69         g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
70         g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
71
72         g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);
73
74         if (width == 0 || height == 0)
75                 return;
76
77         gc = gdk_gc_new (bitmap);
78
79         if (!pixbuf->has_alpha) {
80                 color.pixel = (alpha_threshold == 255) ? 0 : 1;
81                 gdk_gc_set_foreground (gc, &color);
82                 gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
83                 gdk_gc_unref (gc);
84                 return;
85         }
86
87         color.pixel = 0;
88         gdk_gc_set_foreground (gc, &color);
89         gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
90
91         color.pixel = 1;
92         gdk_gc_set_foreground (gc, &color);
93
94         for (y = 0; y < height; y++) {
95                 p = (pixbuf->pixels + (y + src_y) * pixbuf->rowstride + src_x * pixbuf->n_channels
96                      + pixbuf->n_channels - 1);
97
98                 start = 0;
99                 start_status = *p < alpha_threshold;
100
101                 for (x = 0; x < width; x++) {
102                         status = *p < alpha_threshold;
103
104                         if (status != start_status) {
105                                 if (!start_status)
106                                         gdk_draw_line (bitmap, gc,
107                                                        start + dest_x, y + dest_y,
108                                                        x - 1 + dest_x, y + dest_y);
109
110                                 start = x;
111                                 start_status = status;
112                         }
113
114                         p += pixbuf->n_channels;
115                 }
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
123         gdk_gc_unref (gc);
124 }
125
126 \f
127
128 /* Creates a buffer by stripping the alpha channel of a pixbuf */
129 static guchar *
130 remove_alpha (GdkPixbuf *pixbuf, int x, int y, int width, int height, int *rowstride)
131 {
132         guchar *buf;
133         int xx, yy;
134         guchar *src, *dest;
135
136         g_assert (pixbuf->n_channels == 4);
137         g_assert (pixbuf->has_alpha);
138         g_assert (width > 0 && height > 0);
139         g_assert (x >= 0 && x + width <= pixbuf->width);
140         g_assert (y >= 0 && y + height <= pixbuf->height);
141
142         *rowstride = 4 * ((width * 3 + 3) / 4);
143
144         buf = g_new (guchar, *rowstride * height);
145
146         for (yy = 0; yy < height; yy++) {
147                 src = pixbuf->pixels + pixbuf->rowstride * (yy + y) + x * pixbuf->n_channels;
148                 dest = buf + *rowstride * yy;
149
150                 for (xx = 0; xx < width; xx++) {
151                         *dest++ = *src++;
152                         *dest++ = *src++;
153                         *dest++ = *src++;
154                         src++;
155                 }
156         }
157
158         return buf;
159 }
160
161 /**
162  * gdk_pixbuf_render_to_drawable:
163  * @pixbuf: A pixbuf.
164  * @drawable: Destination drawable.
165  * @gc: GC used for rendering.
166  * @src_x: Source X coordinate within pixbuf.
167  * @src_y: Source Y coordinate within pixbuf.
168  * @dest_x: Destination X coordinate within drawable.
169  * @dest_y: Destination Y coordinate within drawable.
170  * @width: Width of region to render, in pixels.
171  * @height: Height of region to render, in pixels.
172  * @dither: Dithering mode for GdkRGB.
173  * @x_dither: X offset for dither.
174  * @y_dither: Y offset for dither.
175  *
176  * Renders a rectangular portion of a pixbuf to a drawable while using the
177  * specified GC.  This is done using GdkRGB, so the specified drawable must have
178  * the GdkRGB visual and colormap.  Note that this function will ignore the
179  * opacity information for images with an alpha channel; the GC must already
180  * have the clipping mask set if you want transparent regions to show through.
181  *
182  * For an explanation of dither offsets, see the GdkRGB documentation.  In
183  * brief, the dither offset is important when re-rendering partial regions of an
184  * image to a rendered version of the full image, or for when the offsets to a
185  * base position change, as in scrolling.  The dither matrix has to be shifted
186  * for consistent visual results.  If you do not have any of these cases, the
187  * dither offsets can be both zero.
188  **/
189 void
190 gdk_pixbuf_render_to_drawable (GdkPixbuf *pixbuf,
191                                GdkDrawable *drawable, GdkGC *gc,
192                                int src_x, int src_y,
193                                int dest_x, int dest_y,
194                                int width, int height,
195                                GdkRgbDither dither,
196                                int x_dither, int y_dither)
197 {
198         guchar *buf;
199         int rowstride;
200
201         g_return_if_fail (pixbuf != NULL);
202         g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
203         g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
204         g_return_if_fail (pixbuf->bits_per_sample == 8);
205
206         g_return_if_fail (drawable != NULL);
207         g_return_if_fail (gc != NULL);
208
209         g_return_if_fail (width >= 0 && height >= 0);
210         g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
211         g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
212
213         if (width == 0 || height == 0)
214                 return;
215
216         /* This will have to be modified once we support other image types.
217          * Also, GdkRGB does not have gdk_draw_rgb_32_image_dithalign(), so we
218          * have to pack the buffer first.  Sigh.
219          */
220
221         if (pixbuf->has_alpha)
222                 buf = remove_alpha (pixbuf, src_x, src_y, width, height, &rowstride);
223         else {
224                 buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
225                 rowstride = pixbuf->rowstride;
226         }
227
228         gdk_draw_rgb_image_dithalign (drawable, gc,
229                                       dest_x, dest_y,
230                                       width, height,
231                                       dither,
232                                       buf, rowstride,
233                                       x_dither, y_dither);
234
235         if (pixbuf->has_alpha)
236                 g_free (buf);
237 }
238
239 \f
240
241 /**
242  * gdk_pixbuf_render_to_drawable_alpha:
243  * @pixbuf: A pixbuf.
244  * @drawable: Destination drawable.
245  * @src_x: Source X coordinate within pixbuf.
246  * @src_y: Source Y coordinates within pixbuf.
247  * @dest_x: Destination X coordinate within drawable.
248  * @dest_y: Destination Y coordinate within drawable.
249  * @width: Width of region to render, in pixels.
250  * @height: Height of region to render, in pixels.
251  * @alpha_mode: If the image does not have opacity information, this is ignored.
252  * Otherwise, specifies how to handle transparency when rendering.
253  * @alpha_threshold: If the image does have opacity information and @alpha_mode
254  * is GDK_PIXBUF_ALPHA_BILEVEL, specifies the threshold value for opacity
255  * values.
256  * @dither: Dithering mode for GdkRGB.
257  * @x_dither: X offset for dither.
258  * @y_dither: Y offset for dither.
259  *
260  * Renders a rectangular portion of a pixbuf to a drawable.  This is done using
261  * GdkRGB, so the specified drawable must have the GdkRGB visual and colormap.
262  *
263  * When used with #GDK_PIXBUF_ALPHA_BILEVEL, this function has to create a bitmap
264  * out of the thresholded alpha channel of the image and, it has to set this
265  * bitmap as the clipping mask for the GC used for drawing.  This can be a
266  * significant performance penalty depending on the size and the complexity of
267  * the alpha channel of the image.  If performance is crucial, consider handling
268  * the alpha channel yourself (possibly by caching it in your application) and
269  * using gdk_pixbuf_render_to_drawable() or GdkRGB directly instead.
270  **/
271 void
272 gdk_pixbuf_render_to_drawable_alpha (GdkPixbuf *pixbuf, GdkDrawable *drawable,
273                                      int src_x, int src_y,
274                                      int dest_x, int dest_y,
275                                      int width, int height,
276                                      GdkPixbufAlphaMode alpha_mode,
277                                      int alpha_threshold,
278                                      GdkRgbDither dither,
279                                      int x_dither, int y_dither)
280 {
281         GdkBitmap *bitmap = NULL;
282         GdkGC *gc;
283
284         g_return_if_fail (pixbuf != NULL);
285         g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
286         g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
287         g_return_if_fail (pixbuf->bits_per_sample == 8);
288
289         g_return_if_fail (drawable != NULL);
290         g_return_if_fail (width >= 0 && height >= 0);
291         g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
292         g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
293
294         if (width == 0 || height == 0)
295                 return;
296
297         gc = gdk_gc_new (drawable);
298
299         if (pixbuf->has_alpha) {
300                 /* Right now we only support GDK_PIXBUF_ALPHA_BILEVEL, so we
301                  * unconditionally create the clipping mask.
302                  */
303
304                 bitmap = gdk_pixmap_new (NULL, width, height, 1);
305                 gdk_pixbuf_render_threshold_alpha (pixbuf, bitmap,
306                                                    src_x, src_y,
307                                                    0, 0,
308                                                    width, height,
309                                                    alpha_threshold);
310
311                 gdk_gc_set_clip_mask (gc, bitmap);
312                 gdk_gc_set_clip_origin (gc, dest_x, dest_y);
313         }
314
315         gdk_pixbuf_render_to_drawable (pixbuf, drawable, gc,
316                                        src_x, src_y,
317                                        dest_x, dest_y,
318                                        width, height,
319                                        dither,
320                                        x_dither, y_dither);
321
322         if (bitmap)
323                 gdk_bitmap_unref (bitmap);
324
325         gdk_gc_unref (gc);
326 }
327
328 /**
329  * gdk_pixbuf_render_pixmap_and_mask:
330  * @pixbuf: A pixbuf.
331  * @pixmap_return: Return value for the created pixmap.
332  * @mask_return: Return value for the created mask.
333  * @alpha_threshold: Threshold value for opacity values.
334  *
335  * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
336  * and @mask_return arguments, respectively, and renders a pixbuf and its
337  * corresponding tresholded alpha mask to them.  This is merely a convenience
338  * function; applications that need to render pixbufs with dither offsets or to
339  * given drawables should use gdk_pixbuf_render_to_drawable_alpha() or
340  * gdk_pixbuf_render_to_drawable(), and gdk_pixbuf_render_threshold_alpha().
341  *
342  * If the pixbuf does not have an alpha channel, then *@mask_return will be set
343  * to NULL.
344  **/
345 void
346 gdk_pixbuf_render_pixmap_and_mask (GdkPixbuf *pixbuf,
347                                    GdkPixmap **pixmap_return, GdkBitmap **mask_return,
348                                    int alpha_threshold)
349 {
350         g_return_if_fail (pixbuf != NULL);
351
352         if (pixmap_return) {
353                 GdkGC *gc;
354
355                 *pixmap_return = gdk_pixmap_new (NULL, pixbuf->width, pixbuf->height,
356                                                  gdk_rgb_get_visual ()->depth);
357                 gc = gdk_gc_new (*pixmap_return);
358                 gdk_pixbuf_render_to_drawable (pixbuf, *pixmap_return, gc,
359                                                0, 0, 0, 0,
360                                                pixbuf->width, pixbuf->height,
361                                                GDK_RGB_DITHER_NORMAL,
362                                                0, 0);
363                 gdk_gc_unref (gc);
364         }
365
366         if (mask_return) {
367                 if (pixbuf->has_alpha) {
368                         *mask_return = gdk_pixmap_new (NULL, pixbuf->width, pixbuf->height, 1);
369                         gdk_pixbuf_render_threshold_alpha (pixbuf, *mask_return,
370                                                            0, 0, 0, 0,
371                                                            pixbuf->width, pixbuf->height,
372                                                            alpha_threshold);
373                 } else
374                         *mask_return = NULL;
375         }
376 }