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);
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);
95 start_status = *p < alpha_threshold;
97 for (x = 0; x < width; x++) {
98 status = *p < alpha_threshold;
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);
108 start_status = status;
111 p += apb->n_channels;
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);
126 /* Creates a buffer by stripping the alpha channel of a pixbuf */
128 remove_alpha (ArtPixBuf *apb, int x, int y, int width, int height, int *rowstride)
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);
139 *rowstride = 4 * ((width * 3 + 3) / 4);
141 buf = g_new (guchar, *rowstride * height);
143 for (yy = 0; yy < height; yy++) {
144 src = apb->pixels + apb->rowstride * (yy + y) + x * apb->n_channels;
145 dest = buf + *rowstride * yy;
147 for (xx = 0; xx < width; xx++) {
159 * gdk_pixbuf_render_to_drawable:
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.
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.
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.
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,
193 int x_dither, int y_dither)
199 g_return_if_fail (pixbuf != NULL);
200 apb = pixbuf->art_pixbuf;
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);
206 g_return_if_fail (drawable != NULL);
207 g_return_if_fail (gc != NULL);
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);
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.
218 buf = remove_alpha (apb, src_x, src_y, width, height, &rowstride);
220 buf = apb->pixels + src_y * apb->rowstride + src_x * 3;
221 rowstride = apb->rowstride;
224 gdk_draw_rgb_image_dithalign (drawable, gc,
238 * gdk_pixbuf_render_to_drawable_alpha:
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
252 * @dither: Dithering mode for GdkRGB.
253 * @x_dither: X offset for dither.
254 * @y_dither: Y offset for dither.
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.
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.
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,
275 int x_dither, int y_dither)
281 g_return_if_fail (pixbuf != NULL);
282 apb = pixbuf->art_pixbuf;
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);
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);
292 gc = gdk_gc_new (drawable);
294 if (apb->has_alpha) {
295 /* Right now we only support GDK_PIXBUF_ALPHA_BILEVEL, so we
296 * unconditionally create the clipping mask.
299 bitmap = gdk_pixmap_new (NULL, width, height, 1);
300 gdk_pixbuf_render_threshold_alpha (pixbuf, bitmap,
306 gdk_gc_set_clip_mask (gc, bitmap);
307 gdk_gc_set_clip_origin (gc, dest_x, dest_y);
308 gdk_bitmap_unref (bitmap);
311 gdk_pixbuf_render_to_drawable (pixbuf, drawable, gc,