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