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 Lesser 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 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser 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 "gdk-pixbuf-private.h"
26 #include "gdkpixbuf.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, or -1 to use pixbuf width
39 * @height: Height of region to threshold, or -1 to use pixbuf height
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,
52 int dest_x, int dest_y,
53 int width, int height,
60 int start, start_status;
63 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
64 g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
65 g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
66 g_return_if_fail (pixbuf->bits_per_sample == 8);
69 width = pixbuf->width;
71 height = pixbuf->height;
73 g_return_if_fail (bitmap != NULL);
74 g_return_if_fail (width >= 0 && height >= 0);
75 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
76 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
78 g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);
80 if (width == 0 || height == 0)
83 gc = gdk_gc_new (bitmap);
85 if (!pixbuf->has_alpha)
87 color.pixel = (alpha_threshold == 255) ? 0 : 1;
88 gdk_gc_set_foreground (gc, &color);
89 gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
95 gdk_gc_set_foreground (gc, &color);
96 gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
99 gdk_gc_set_foreground (gc, &color);
101 for (y = 0; y < height; y++)
103 p = (pixbuf->pixels + (y + src_y) * pixbuf->rowstride + src_x * pixbuf->n_channels
104 + pixbuf->n_channels - 1);
107 start_status = *p < alpha_threshold;
109 for (x = 0; x < width; x++)
111 status = *p < alpha_threshold;
113 if (status != start_status)
116 gdk_draw_line (bitmap, gc,
117 start + dest_x, y + dest_y,
118 x - 1 + dest_x, y + dest_y);
121 start_status = status;
124 p += pixbuf->n_channels;
128 gdk_draw_line (bitmap, gc,
129 start + dest_x, y + dest_y,
130 x - 1 + dest_x, y + dest_y);
139 * gdk_pixbuf_render_to_drawable:
141 * @drawable: Destination drawable.
142 * @gc: GC used for rendering.
143 * @src_x: Source X coordinate within pixbuf.
144 * @src_y: Source Y coordinate within pixbuf.
145 * @dest_x: Destination X coordinate within drawable.
146 * @dest_y: Destination Y coordinate within drawable.
147 * @width: Width of region to render, in pixels, or -1 to use pixbuf width
148 * @height: Height of region to render, in pixels, or -1 to use pixbuf height
149 * @dither: Dithering mode for GdkRGB.
150 * @x_dither: X offset for dither.
151 * @y_dither: Y offset for dither.
153 * Renders a rectangular portion of a pixbuf to a drawable while using the
154 * specified GC. This is done using GdkRGB, so the specified drawable must have
155 * the GdkRGB visual and colormap. Note that this function will ignore the
156 * opacity information for images with an alpha channel; the GC must already
157 * have the clipping mask set if you want transparent regions to show through.
159 * For an explanation of dither offsets, see the GdkRGB documentation. In
160 * brief, the dither offset is important when re-rendering partial regions of an
161 * image to a rendered version of the full image, or for when the offsets to a
162 * base position change, as in scrolling. The dither matrix has to be shifted
163 * for consistent visual results. If you do not have any of these cases, the
164 * dither offsets can be both zero.
167 gdk_pixbuf_render_to_drawable (GdkPixbuf *pixbuf,
168 GdkDrawable *drawable,
170 int src_x, int src_y,
171 int dest_x, int dest_y,
172 int width, int height,
174 int x_dither, int y_dither)
179 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
180 g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
181 g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
182 g_return_if_fail (pixbuf->bits_per_sample == 8);
184 g_return_if_fail (drawable != NULL);
185 g_return_if_fail (gc != NULL);
188 width = pixbuf->width;
190 height = pixbuf->height;
192 g_return_if_fail (width >= 0 && height >= 0);
193 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
194 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
196 if (width == 0 || height == 0)
199 /* This will have to be modified once we support other image types.
202 if (pixbuf->n_channels == 4)
204 buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4;
205 rowstride = pixbuf->rowstride;
207 gdk_draw_rgb_32_image_dithalign (drawable, gc,
214 else /* n_channels == 3 */
216 buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
217 rowstride = pixbuf->rowstride;
219 gdk_draw_rgb_image_dithalign (drawable, gc,
231 * gdk_pixbuf_render_to_drawable_alpha:
233 * @drawable: Destination drawable.
234 * @src_x: Source X coordinate within pixbuf.
235 * @src_y: Source Y coordinates within pixbuf.
236 * @dest_x: Destination X coordinate within drawable.
237 * @dest_y: Destination Y coordinate within drawable.
238 * @width: Width of region to render, in pixels, or -1 to use pixbuf width.
239 * @height: Height of region to render, in pixels, or -1 to use pixbuf height.
240 * @alpha_mode: Ignored. Present for backwards compatibility.
241 * @alpha_threshold: Ignored. Present for backwards compatibility
242 * @dither: Dithering mode for GdkRGB.
243 * @x_dither: X offset for dither.
244 * @y_dither: Y offset for dither.
246 * Renders a rectangular portion of a pixbuf to a drawable. The destination
247 * drawable must have a colormap. All windows have a colormap, however, pixmaps
248 * only have colormap by default if they were created with a non-NULL window argument.
249 * Otherwise a colormap must be set on them with gdk_drawable_set_colormap.
251 * On older X servers, rendering pixbufs with an alpha channel involves round trips
252 * to the X server, and may be somewhat slow.
255 gdk_pixbuf_render_to_drawable_alpha (GdkPixbuf *pixbuf,
256 GdkDrawable *drawable,
257 int src_x, int src_y,
258 int dest_x, int dest_y,
259 int width, int height,
260 GdkPixbufAlphaMode alpha_mode,
263 int x_dither, int y_dither)
265 _gdk_draw_pixbuf (drawable, NULL, pixbuf,
266 src_x, src_y, dest_x, dest_y, width, height,
267 dither, x_dither, y_dither);
271 * gdk_pixbuf_render_pixmap_and_mask:
273 * @pixmap_return: Return value for the created pixmap.
274 * @mask_return: Return value for the created mask.
275 * @alpha_threshold: Threshold value for opacity values.
277 * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
278 * and @mask_return arguments, respectively, and renders a pixbuf and its
279 * corresponding thresholded alpha mask to them. This is merely a convenience
280 * function; applications that need to render pixbufs with dither offsets or to
281 * given drawables should use gdk_pixbuf_render_to_drawable_alpha() or
282 * gdk_pixbuf_render_to_drawable(), and gdk_pixbuf_render_threshold_alpha().
284 * The pixmap that is created is created for the colormap returned
285 * by gdk_rgb_get_colormap(). You normally will want to instead use
286 * the actual colormap for a widget, and use
287 * gdk_pixbuf_render_pixmap_and_mask_for_colormap.
289 * If the pixbuf does not have an alpha channel, then *@mask_return will be set
293 gdk_pixbuf_render_pixmap_and_mask (GdkPixbuf *pixbuf,
294 GdkPixmap **pixmap_return,
295 GdkBitmap **mask_return,
298 gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf,
299 gdk_rgb_get_colormap (),
300 pixmap_return, mask_return,
305 * gdk_pixbuf_render_pixmap_and_mask_for_colormap:
307 * @colormap: A #GdkColormap
308 * @pixmap_return: Return value for the created pixmap.
309 * @mask_return: Return value for the created mask.
310 * @alpha_threshold: Threshold value for opacity values.
312 * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
313 * and @mask_return arguments, respectively, and renders a pixbuf and its
314 * corresponding tresholded alpha mask to them. This is merely a convenience
315 * function; applications that need to render pixbufs with dither offsets or to
316 * given drawables should use gdk_pixbuf_render_to_drawable_alpha() or
317 * gdk_pixbuf_render_to_drawable(), and gdk_pixbuf_render_threshold_alpha().
319 * The pixmap that is created uses the #GdkColormap specified by @colormap.
320 * This colormap must match the colormap of the window where the pixmap
321 * will eventually be used or an error will result.
323 * If the pixbuf does not have an alpha channel, then *@mask_return will be set
327 gdk_pixbuf_render_pixmap_and_mask_for_colormap (GdkPixbuf *pixbuf,
328 GdkColormap *colormap,
329 GdkPixmap **pixmap_return,
330 GdkBitmap **mask_return,
333 g_return_if_fail (pixbuf != NULL);
339 *pixmap_return = gdk_pixmap_new (NULL, gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
340 gdk_colormap_get_visual (colormap)->depth);
341 gdk_drawable_set_colormap (GDK_DRAWABLE (*pixmap_return),
343 gc = gdk_gc_new (*pixmap_return);
344 gdk_pixbuf_render_to_drawable (pixbuf, *pixmap_return, gc,
346 gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
347 GDK_RGB_DITHER_NORMAL,
354 if (gdk_pixbuf_get_has_alpha (pixbuf))
356 *mask_return = gdk_pixmap_new (NULL, gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf), 1);
358 gdk_pixbuf_render_threshold_alpha (pixbuf, *mask_return,
360 gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),