]> Pileus Git - ~andy/gtk/blob - gdk/gdkpixbuf-render.c
make the symlink. Does not work for srcdir != buildir != . Beats me why; I
[~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 <libart_lgpl/art_rect.h>
26 #include "gdk-pixbuf/gdk-pixbuf.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         ArtPixBuf *apb;
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         apb = pixbuf->art_pixbuf;
65
66         g_return_if_fail (apb->format == ART_PIX_RGB);
67         g_return_if_fail (apb->n_channels == 3 || apb->n_channels == 4);
68         g_return_if_fail (apb->bits_per_sample == 8);
69
70         g_return_if_fail (bitmap != NULL);
71         g_return_if_fail (src_x >= 0 && src_x + width <= apb->width);
72         g_return_if_fail (src_y >= 0 && src_y + height <= apb->height);
73
74         g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);
75
76         gc = gdk_gc_new (bitmap);
77
78         if (!apb->has_alpha) {
79                 color.pixel = (alpha_threshold == 255) ? 0 : 1;
80                 gdk_gc_set_foreground (gc, &color);
81                 gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
82                 gdk_gc_unref (gc);
83                 return;
84         }
85
86         color.pixel = 0;
87         gdk_gc_set_foreground (gc, &color);
88         gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
89
90         for (y = 0; y < height; y++) {
91                 p = (apb->pixels + (y + src_y) * apb->rowstride + src_x * apb->n_channels
92                      + apb->n_channels - 1);
93
94                 start = 0;
95                 start_status = *p < alpha_threshold;
96
97                 for (x = 0; x < width; x++) {
98                         status = *p < alpha_threshold;
99
100                         if (status != start_status) {
101                                 color.pixel = start_status ? 0 : 1;
102                                 gdk_gc_set_foreground (gc, &color);
103                                 gdk_draw_line (bitmap, gc,
104                                                start + dest_x, y + dest_y,
105                                                x - 1 + dest_x, y + dest_y);
106
107                                 start = x;
108                                 start_status = status;
109                         }
110
111                         p += apb->n_channels;
112                 }
113
114                 color.pixel = start_status ? 0 : 1;
115                 gdk_gc_set_foreground (gc, &color);
116                 gdk_draw_line (bitmap, gc,
117                                start + dest_x, y + dest_y,
118                                x - 1 + dest_x, y + dest_y);
119         }
120
121         gdk_gc_unref (gc);
122 }
123
124 \f
125
126 /* Creates a buffer by stripping the alpha channel of a pixbuf */
127 static guchar *
128 remove_alpha (ArtPixBuf *apb, int x, int y, int width, int height, int *rowstride)
129 {
130         guchar *buf;
131         int xx, yy;
132         guchar *src, *dest;
133
134         g_assert (apb->n_channels == 4);
135         g_assert (apb->has_alpha);
136         g_assert (x >= 0 && x + width <= apb->width);
137         g_assert (y >= 0 && y + height <= apb->height);
138
139         *rowstride = 4 * ((width * 3 + 3) / 4);
140
141         buf = g_new (guchar, *rowstride * height);
142
143         for (yy = 0; yy < height; yy++) {
144                 src = apb->pixels + apb->rowstride * (yy + y) + x * apb->n_channels;
145                 dest = buf + *rowstride * yy;
146
147                 for (xx = 0; xx < width; xx++) {
148                         *dest++ = *src++;
149                         *dest++ = *src++;
150                         *dest++ = *src++;
151                         src++;
152                 }
153         }
154
155         return buf;
156 }
157
158 /**
159  * gdk_pixbuf_render_to_drawable:
160  * @pixbuf: A pixbuf.
161  * @drawable: Destination drawable.
162  * @gc: GC used for rendering.
163  * @src_x: Source X coordinate within pixbuf.
164  * @src_y: Source Y coordinate within pixbuf.
165  * @dest_x: Destination X coordinate within drawable.
166  * @dest_y: Destination Y coordinate within drawable.
167  * @width: Width of region to render, in pixels.
168  * @height: Height of region to render, in pixels.
169  * @dither: Dithering mode for GdkRGB.
170  * @x_dither: X offset for dither.
171  * @y_dither: Y offset for dither.
172  * 
173  * Renders a rectangular portion of a pixbuf to a drawable while using the
174  * specified GC.  This is done using GdkRGB, so the specified drawable must have
175  * the GdkRGB visual and colormap.  Note that this function will ignore the
176  * opacity information for images with an alpha channel; the GC must already
177  * have the clipping mask set if you want transparent regions to show through.
178  *
179  * For an explanation of dither offsets, see the GdkRGB documentation.  In
180  * brief, the dither offset is important when re-rendering partial regions of an
181  * image to a rendered version of the full image, or for when the offsets to a
182  * base position change, as in scrolling.  The dither matrix has to be shifted
183  * for consistent visual results.  If you do not have any of these cases, the
184  * dither offsets can be both zero.
185  **/
186 void
187 gdk_pixbuf_render_to_drawable (GdkPixbuf *pixbuf,
188                                GdkDrawable *drawable, GdkGC *gc,
189                                int src_x, int src_y,
190                                int dest_x, int dest_y,
191                                int width, int height,
192                                GdkRgbDither dither,
193                                int x_dither, int y_dither)
194 {
195         ArtPixBuf *apb;
196         guchar *buf;
197         int rowstride;
198
199         g_return_if_fail (pixbuf != NULL);
200         apb = pixbuf->art_pixbuf;
201         
202         g_return_if_fail (apb->format == ART_PIX_RGB);
203         g_return_if_fail (apb->n_channels == 3 || apb->n_channels == 4);
204         g_return_if_fail (apb->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 (src_x >= 0 && src_x + width <= apb->width);
210         g_return_if_fail (src_y >= 0 && src_y + height <= apb->height);
211
212         /* This will have to be modified once libart supports other image types.
213          * Also, GdkRGB does not have gdk_draw_rgb_32_image_dithalign(), so we
214          * have to pack the buffer first.
215          */
216
217         if (apb->has_alpha)
218                 buf = remove_alpha (apb, src_x, src_y, width, height, &rowstride);
219         else {
220                 buf = apb->pixels + src_y * apb->rowstride + src_x * 3;
221                 rowstride = apb->rowstride;
222         }
223
224         gdk_draw_rgb_image_dithalign (drawable, gc,
225                                       dest_x, dest_y,
226                                       width, height,
227                                       dither,
228                                       buf, rowstride,
229                                       x_dither, y_dither);
230
231         if (apb->has_alpha)
232                 g_free (buf);
233 }
234
235 \f
236
237 /**
238  * gdk_pixbuf_render_to_drawable_alpha:
239  * @pixbuf: A pixbuf.
240  * @drawable: Destination drawable.
241  * @src_x: Source X coordinate within pixbuf.
242  * @src_y: Source Y coordinates within pixbuf.
243  * @dest_x: Destination X coordinate within drawable.
244  * @dest_y: Destination Y coordinate within drawable.
245  * @width: Width of region to render, in pixels.
246  * @height: Height of region to render, in pixels.
247  * @alpha_mode: If the image does not have opacity information, this is ignored.
248  * Otherwise, specifies how to handle transparency when rendering.
249  * @alpha_threshold: If the image does have opacity information and @alpha_mode
250  * is GDK_PIXBUF_ALPHA_BILEVEL, specifies the threshold value for opacity
251  * values.
252  * @dither: Dithering mode for GdkRGB.
253  * @x_dither: X offset for dither.
254  * @y_dither: Y offset for dither.
255  *
256  * Renders a rectangular portion of a pixbuf to a drawable.  This is done using
257  * GdkRGB, so the specified drawable must have the GdkRGB visual and colormap.
258  *
259  * When used with #GDK_PIXBUF_ALPHA_BILEVEL, this function has to create a bitmap
260  * out of the thresholded alpha channel of the image and, it has to set this
261  * bitmap as the clipping mask for the GC used for drawing.  This can be a
262  * significant performance penalty depending on the size and the complexity of
263  * the alpha channel of the image.  If performance is crucial, consider handling
264  * the alpha channel yourself (possibly by caching it in your application) and
265  * using gdk_pixbuf_render_to_drawable() or GdkRGB directly instead.
266  **/
267 void
268 gdk_pixbuf_render_to_drawable_alpha (GdkPixbuf *pixbuf, GdkDrawable *drawable,
269                                      int src_x, int src_y,
270                                      int dest_x, int dest_y,
271                                      int width, int height,
272                                      GdkPixbufAlphaMode alpha_mode,
273                                      int alpha_threshold,
274                                      GdkRgbDither dither,
275                                      int x_dither, int y_dither)
276 {
277         ArtPixBuf *apb;
278         GdkBitmap *bitmap;
279         GdkGC *gc;
280
281         g_return_if_fail (pixbuf != NULL);
282         apb = pixbuf->art_pixbuf;
283
284         g_return_if_fail (apb->format == ART_PIX_RGB);
285         g_return_if_fail (apb->n_channels == 3 || apb->n_channels == 4);
286         g_return_if_fail (apb->bits_per_sample == 8);
287
288         g_return_if_fail (drawable != NULL);
289         g_return_if_fail (src_x >= 0 && src_x + width <= apb->width);
290         g_return_if_fail (src_y >= 0 && src_y + height <= apb->height);
291
292         gc = gdk_gc_new (drawable);
293
294         if (apb->has_alpha) {
295                 /* Right now we only support GDK_PIXBUF_ALPHA_BILEVEL, so we
296                  * unconditionally create the clipping mask.
297                  */
298
299                 bitmap = gdk_pixmap_new (NULL, width, height, 1);
300                 gdk_pixbuf_render_threshold_alpha (pixbuf, bitmap,
301                                                    src_x, src_y,
302                                                    0, 0,
303                                                    width, height,
304                                                    alpha_threshold);
305
306                 gdk_gc_set_clip_mask (gc, bitmap);
307                 gdk_gc_set_clip_origin (gc, dest_x, dest_y);
308                 gdk_bitmap_unref (bitmap);
309         }
310
311         gdk_pixbuf_render_to_drawable (pixbuf, drawable, gc,
312                                        src_x, src_y,
313                                        dest_x, dest_y,
314                                        width, height,
315                                        dither,
316                                        x_dither, y_dither);
317
318         gdk_gc_unref (gc);
319 }