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.
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,
52 int dest_x, int dest_y,
53 int width, int height,
60 int start, start_status;
63 g_return_if_fail (pixbuf != NULL);
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);
68 g_return_if_fail (bitmap != NULL);
69 g_return_if_fail (width >= 0 && height >= 0);
70 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
71 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
73 g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);
75 if (width == 0 || height == 0)
78 gc = gdk_gc_new (bitmap);
80 if (!pixbuf->has_alpha)
82 color.pixel = (alpha_threshold == 255) ? 0 : 1;
83 gdk_gc_set_foreground (gc, &color);
84 gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
90 gdk_gc_set_foreground (gc, &color);
91 gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
94 gdk_gc_set_foreground (gc, &color);
96 for (y = 0; y < height; y++)
98 p = (pixbuf->pixels + (y + src_y) * pixbuf->rowstride + src_x * pixbuf->n_channels
99 + pixbuf->n_channels - 1);
102 start_status = *p < alpha_threshold;
104 for (x = 0; x < width; x++)
106 status = *p < alpha_threshold;
108 if (status != start_status)
111 gdk_draw_line (bitmap, gc,
112 start + dest_x, y + dest_y,
113 x - 1 + dest_x, y + dest_y);
116 start_status = status;
119 p += pixbuf->n_channels;
123 gdk_draw_line (bitmap, gc,
124 start + dest_x, y + dest_y,
125 x - 1 + dest_x, y + dest_y);
134 * gdk_pixbuf_render_to_drawable:
136 * @drawable: Destination drawable.
137 * @gc: GC used for rendering.
138 * @src_x: Source X coordinate within pixbuf.
139 * @src_y: Source Y coordinate within pixbuf.
140 * @dest_x: Destination X coordinate within drawable.
141 * @dest_y: Destination Y coordinate within drawable.
142 * @width: Width of region to render, in pixels.
143 * @height: Height of region to render, in pixels.
144 * @dither: Dithering mode for GdkRGB.
145 * @x_dither: X offset for dither.
146 * @y_dither: Y offset for dither.
148 * Renders a rectangular portion of a pixbuf to a drawable while using the
149 * specified GC. This is done using GdkRGB, so the specified drawable must have
150 * the GdkRGB visual and colormap. Note that this function will ignore the
151 * opacity information for images with an alpha channel; the GC must already
152 * have the clipping mask set if you want transparent regions to show through.
154 * For an explanation of dither offsets, see the GdkRGB documentation. In
155 * brief, the dither offset is important when re-rendering partial regions of an
156 * image to a rendered version of the full image, or for when the offsets to a
157 * base position change, as in scrolling. The dither matrix has to be shifted
158 * for consistent visual results. If you do not have any of these cases, the
159 * dither offsets can be both zero.
162 gdk_pixbuf_render_to_drawable (GdkPixbuf *pixbuf,
163 GdkDrawable *drawable,
165 int src_x, int src_y,
166 int dest_x, int dest_y,
167 int width, int height,
169 int x_dither, int y_dither)
174 g_return_if_fail (pixbuf != NULL);
175 g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
176 g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
177 g_return_if_fail (pixbuf->bits_per_sample == 8);
179 g_return_if_fail (drawable != NULL);
180 g_return_if_fail (gc != NULL);
182 g_return_if_fail (width >= 0 && height >= 0);
183 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
184 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
186 if (width == 0 || height == 0)
189 /* This will have to be modified once we support other image types.
192 if (pixbuf->n_channels == 4)
194 buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4;
195 rowstride = pixbuf->rowstride;
197 gdk_draw_rgb_32_image_dithalign (drawable, gc,
204 else /* n_channels == 3 */
206 buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
207 rowstride = pixbuf->rowstride;
209 gdk_draw_rgb_image_dithalign (drawable, gc,
221 * gdk_pixbuf_render_to_drawable_alpha:
223 * @drawable: Destination drawable.
224 * @src_x: Source X coordinate within pixbuf.
225 * @src_y: Source Y coordinates within pixbuf.
226 * @dest_x: Destination X coordinate within drawable.
227 * @dest_y: Destination Y coordinate within drawable.
228 * @width: Width of region to render, in pixels.
229 * @height: Height of region to render, in pixels.
230 * @alpha_mode: If the image does not have opacity information, this is ignored.
231 * Otherwise, specifies how to handle transparency when rendering.
232 * @alpha_threshold: If the image does have opacity information and @alpha_mode
233 * is GDK_PIXBUF_ALPHA_BILEVEL, specifies the threshold value for opacity
235 * @dither: Dithering mode for GdkRGB.
236 * @x_dither: X offset for dither.
237 * @y_dither: Y offset for dither.
239 * Renders a rectangular portion of a pixbuf to a drawable. This is done using
240 * GdkRGB, so the specified drawable must have the GdkRGB visual and colormap.
242 * When used with #GDK_PIXBUF_ALPHA_BILEVEL, this function has to create a bitmap
243 * out of the thresholded alpha channel of the image and, it has to set this
244 * bitmap as the clipping mask for the GC used for drawing. This can be a
245 * significant performance penalty depending on the size and the complexity of
246 * the alpha channel of the image. If performance is crucial, consider handling
247 * the alpha channel yourself (possibly by caching it in your application) and
248 * using gdk_pixbuf_render_to_drawable() or GdkRGB directly instead.
250 * The #GDK_PIXBUF_ALPHA_FULL mode involves round trips to the X
251 * server, and may also be somewhat slow in its current implementation
252 * (though in the future it could be made significantly faster, in
256 gdk_pixbuf_render_to_drawable_alpha (GdkPixbuf *pixbuf,
257 GdkDrawable *drawable,
258 int src_x, int src_y,
259 int dest_x, int dest_y,
260 int width, int height,
261 GdkPixbufAlphaMode alpha_mode,
264 int x_dither, int y_dither)
266 GdkBitmap *bitmap = NULL;
268 GdkPixbuf *composited = NULL;
269 gint dwidth, dheight;
272 GdkRectangle tmp_rect;
274 g_return_if_fail (pixbuf != NULL);
275 g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
276 g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
277 g_return_if_fail (pixbuf->bits_per_sample == 8);
279 g_return_if_fail (drawable != NULL);
280 g_return_if_fail (width >= 0 && height >= 0);
281 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
282 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
284 /* Clip to the drawable; this is required for get_from_drawable() so
285 * can't be done implicitly
302 gdk_drawable_get_size (drawable, &dwidth, &dheight);
304 if ((dest_x + width) > dwidth)
305 width = dwidth - dest_x;
307 if ((dest_y + height) > dheight)
308 height = dheight - dest_y;
310 if (width <= 0 || height <= 0)
313 /* Clip to the clip region; this avoids getting more
314 * image data from the server than we need to.
319 tmp_rect.width = width;
320 tmp_rect.height = height;
322 drect = gdk_region_rectangle (&tmp_rect);
323 clip = gdk_drawable_get_clip_region (drawable);
325 gdk_region_intersect (drect, clip);
327 gdk_region_get_clipbox (drect, &tmp_rect);
329 gdk_region_destroy (drect);
330 gdk_region_destroy (clip);
332 if (tmp_rect.width == 0 ||
333 tmp_rect.height == 0)
338 gc = gdk_gc_new (drawable);
340 if (pixbuf->has_alpha)
342 if (alpha_mode == GDK_PIXBUF_ALPHA_FULL)
344 GdkPixbuf *sub = NULL;
346 composited = gdk_pixbuf_get_from_drawable (NULL,
355 if (src_x != 0 || src_y != 0)
357 sub = gdk_pixbuf_new_subpixbuf (pixbuf, src_x, src_y,
361 gdk_pixbuf_composite (sub ? sub : pixbuf,
371 g_object_unref (G_OBJECT (sub));
374 alpha_mode = GDK_PIXBUF_ALPHA_BILEVEL; /* fall back */
377 if (alpha_mode == GDK_PIXBUF_ALPHA_BILEVEL)
379 bitmap = gdk_pixmap_new (NULL, width, height, 1);
380 gdk_pixbuf_render_threshold_alpha (pixbuf, bitmap,
386 gdk_gc_set_clip_mask (gc, bitmap);
387 gdk_gc_set_clip_origin (gc, dest_x, dest_y);
393 gdk_pixbuf_render_to_drawable (composited,
403 gdk_pixbuf_render_to_drawable (pixbuf,
413 gdk_bitmap_unref (bitmap);
416 g_object_unref (G_OBJECT (composited));
422 * gdk_pixbuf_render_pixmap_and_mask:
424 * @pixmap_return: Return value for the created pixmap.
425 * @mask_return: Return value for the created mask.
426 * @alpha_threshold: Threshold value for opacity values.
428 * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
429 * and @mask_return arguments, respectively, and renders a pixbuf and its
430 * corresponding thresholded alpha mask to them. This is merely a convenience
431 * function; applications that need to render pixbufs with dither offsets or to
432 * given drawables should use gdk_pixbuf_render_to_drawable_alpha() or
433 * gdk_pixbuf_render_to_drawable(), and gdk_pixbuf_render_threshold_alpha().
435 * The pixmap that is created is created for the colormap returned
436 * by gdk_rgb_get_colormap(). You normally will want to instead use
437 * the actual colormap for a widget, and use
438 * gdk_pixbuf_render_pixmap_and_mask_for_colormap.
440 * If the pixbuf does not have an alpha channel, then *@mask_return will be set
444 gdk_pixbuf_render_pixmap_and_mask (GdkPixbuf *pixbuf,
445 GdkPixmap **pixmap_return,
446 GdkBitmap **mask_return,
449 gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf,
450 gdk_rgb_get_colormap (),
451 pixmap_return, mask_return,
456 * gdk_pixbuf_render_pixmap_and_mask_for_colormap:
458 * @colormap: A #GdkColormap
459 * @pixmap_return: Return value for the created pixmap.
460 * @mask_return: Return value for the created mask.
461 * @alpha_threshold: Threshold value for opacity values.
463 * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
464 * and @mask_return arguments, respectively, and renders a pixbuf and its
465 * corresponding tresholded alpha mask to them. This is merely a convenience
466 * function; applications that need to render pixbufs with dither offsets or to
467 * given drawables should use gdk_pixbuf_render_to_drawable_alpha() or
468 * gdk_pixbuf_render_to_drawable(), and gdk_pixbuf_render_threshold_alpha().
470 * The pixmap that is created uses the #GdkColormap specified by @colormap.
471 * This colormap must match the colormap of the window where the pixmap
472 * will eventually be used or an error will result.
474 * If the pixbuf does not have an alpha channel, then *@mask_return will be set
478 gdk_pixbuf_render_pixmap_and_mask_for_colormap (GdkPixbuf *pixbuf,
479 GdkColormap *colormap,
480 GdkPixmap **pixmap_return,
481 GdkBitmap **mask_return,
484 g_return_if_fail (pixbuf != NULL);
490 *pixmap_return = gdk_pixmap_new (NULL, gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
491 gdk_colormap_get_visual (colormap)->depth);
492 gdk_drawable_set_colormap (GDK_DRAWABLE (*pixmap_return),
494 gc = gdk_gc_new (*pixmap_return);
495 gdk_pixbuf_render_to_drawable (pixbuf, *pixmap_return, gc,
497 gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
498 GDK_RGB_DITHER_NORMAL,
505 if (gdk_pixbuf_get_has_alpha (pixbuf))
507 *mask_return = gdk_pixmap_new (NULL, gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf), 1);
509 gdk_pixbuf_render_threshold_alpha (pixbuf, *mask_return,
511 gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),