1 /* GdkPixbuf library - Rendering functions
3 * Copyright (C) 1999 The Free Software Foundation
5 * Author: Federico Mena-Quintero <federico@gimp.org>
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.
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.
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.
25 #include <libart_lgpl/art_rect.h>
26 #include "gdk-pixbuf/gdk-pixbuf.h"
31 * gdk_pixbuf_render_threshold_alpha:
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.
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
49 gdk_pixbuf_render_threshold_alpha (GdkPixbuf *pixbuf, GdkBitmap *bitmap,
51 int dest_x, int dest_y,
52 int width, int height,
60 int start, start_status;
63 g_return_if_fail (pixbuf != NULL);
64 apb = pixbuf->art_pixbuf;
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);
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);
74 g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);
76 gc = gdk_gc_new (bitmap);
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);
87 gdk_gc_set_foreground (gc, &color);
88 gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
91 gdk_gc_set_foreground (gc, &color);
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);
98 start_status = *p < alpha_threshold;
100 for (x = 0; x < width; x++) {
101 status = *p < alpha_threshold;
103 if (status != start_status) {
105 gdk_draw_line (bitmap, gc,
106 start + dest_x, y + dest_y,
107 x - 1 + dest_x, y + dest_y);
110 start_status = status;
113 p += apb->n_channels;
117 gdk_draw_line (bitmap, gc,
118 start + dest_x, y + dest_y,
119 x - 1 + dest_x, y + dest_y);
127 /* Creates a buffer by stripping the alpha channel of a pixbuf */
129 remove_alpha (ArtPixBuf *apb, int x, int y, int width, int height, int *rowstride)
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);
140 *rowstride = 4 * ((width * 3 + 3) / 4);
142 buf = g_new (guchar, *rowstride * height);
144 for (yy = 0; yy < height; yy++) {
145 src = apb->pixels + apb->rowstride * (yy + y) + x * apb->n_channels;
146 dest = buf + *rowstride * yy;
148 for (xx = 0; xx < width; xx++) {
160 * gdk_pixbuf_render_to_drawable:
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.
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.
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.
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,
194 int x_dither, int y_dither)
200 g_return_if_fail (pixbuf != NULL);
201 apb = pixbuf->art_pixbuf;
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);
207 g_return_if_fail (drawable != NULL);
208 g_return_if_fail (gc != NULL);
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);
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.
219 buf = remove_alpha (apb, src_x, src_y, width, height, &rowstride);
221 buf = apb->pixels + src_y * apb->rowstride + src_x * 3;
222 rowstride = apb->rowstride;
225 gdk_draw_rgb_image_dithalign (drawable, gc,
239 * gdk_pixbuf_render_to_drawable_alpha:
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
253 * @dither: Dithering mode for GdkRGB.
254 * @x_dither: X offset for dither.
255 * @y_dither: Y offset for dither.
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.
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.
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,
276 int x_dither, int y_dither)
279 GdkBitmap *bitmap = NULL;
282 g_return_if_fail (pixbuf != NULL);
283 apb = pixbuf->art_pixbuf;
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);
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);
293 gc = gdk_gc_new (drawable);
295 if (apb->has_alpha) {
296 /* Right now we only support GDK_PIXBUF_ALPHA_BILEVEL, so we
297 * unconditionally create the clipping mask.
300 bitmap = gdk_pixmap_new (NULL, width, height, 1);
301 gdk_pixbuf_render_threshold_alpha (pixbuf, bitmap,
307 gdk_gc_set_clip_mask (gc, bitmap);
308 gdk_gc_set_clip_origin (gc, dest_x, dest_y);
311 gdk_pixbuf_render_to_drawable (pixbuf, drawable, gc,
319 gdk_bitmap_unref (bitmap);