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"
27 #include "gdkscreen.h"
28 #include "gdkinternals.h"
34 * gdk_pixbuf_render_threshold_alpha:
36 * @bitmap: Bitmap where the bilevel mask will be painted to.
37 * @src_x: Source X coordinate.
38 * @src_y: source Y coordinate.
39 * @dest_x: Destination X coordinate.
40 * @dest_y: Destination Y coordinate.
41 * @width: Width of region to threshold, or -1 to use pixbuf width
42 * @height: Height of region to threshold, or -1 to use pixbuf height
43 * @alpha_threshold: Opacity values below this will be painted as zero; all
44 * other values will be painted as one.
46 * Takes the opacity values in a rectangular portion of a pixbuf and thresholds
47 * them to produce a bi-level alpha mask that can be used as a clipping mask for
52 gdk_pixbuf_render_threshold_alpha (GdkPixbuf *pixbuf,
55 int dest_x, int dest_y,
56 int width, int height,
63 int start, start_status;
66 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
67 g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
68 g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
69 g_return_if_fail (pixbuf->bits_per_sample == 8);
72 width = pixbuf->width;
74 height = pixbuf->height;
76 g_return_if_fail (bitmap != NULL);
77 g_return_if_fail (width >= 0 && height >= 0);
78 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
79 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
81 g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);
83 if (width == 0 || height == 0)
86 gc = _gdk_drawable_get_scratch_gc (bitmap, FALSE);
88 if (!pixbuf->has_alpha)
90 color.pixel = (alpha_threshold == 255) ? 0 : 1;
91 gdk_gc_set_foreground (gc, &color);
92 gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
97 gdk_gc_set_foreground (gc, &color);
98 gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
101 gdk_gc_set_foreground (gc, &color);
103 for (y = 0; y < height; y++)
105 p = (pixbuf->pixels + (y + src_y) * pixbuf->rowstride + src_x * pixbuf->n_channels
106 + pixbuf->n_channels - 1);
109 start_status = *p < alpha_threshold;
111 for (x = 0; x < width; x++)
113 status = *p < alpha_threshold;
115 if (status != start_status)
118 gdk_draw_line (bitmap, gc,
119 start + dest_x, y + dest_y,
120 x - 1 + dest_x, y + dest_y);
123 start_status = status;
126 p += pixbuf->n_channels;
130 gdk_draw_line (bitmap, gc,
131 start + dest_x, y + dest_y,
132 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.
166 * Deprecated: This function is obsolete. Use gdk_draw_pixbuf() instead.
169 gdk_pixbuf_render_to_drawable (GdkPixbuf *pixbuf,
170 GdkDrawable *drawable,
172 int src_x, int src_y,
173 int dest_x, int dest_y,
174 int width, int height,
176 int x_dither, int y_dither)
178 gdk_draw_pixbuf (drawable, gc, pixbuf,
179 src_x, src_y, dest_x, dest_y, width, height,
180 dither, x_dither, y_dither);
186 * gdk_pixbuf_render_to_drawable_alpha:
188 * @drawable: Destination drawable.
189 * @src_x: Source X coordinate within pixbuf.
190 * @src_y: Source Y coordinates within pixbuf.
191 * @dest_x: Destination X coordinate within drawable.
192 * @dest_y: Destination Y coordinate within drawable.
193 * @width: Width of region to render, in pixels, or -1 to use pixbuf width.
194 * @height: Height of region to render, in pixels, or -1 to use pixbuf height.
195 * @alpha_mode: Ignored. Present for backwards compatibility.
196 * @alpha_threshold: Ignored. Present for backwards compatibility
197 * @dither: Dithering mode for GdkRGB.
198 * @x_dither: X offset for dither.
199 * @y_dither: Y offset for dither.
201 * Renders a rectangular portion of a pixbuf to a drawable. The destination
202 * drawable must have a colormap. All windows have a colormap, however, pixmaps
203 * only have colormap by default if they were created with a non-NULL window argument.
204 * Otherwise a colormap must be set on them with gdk_drawable_set_colormap.
206 * On older X servers, rendering pixbufs with an alpha channel involves round trips
207 * to the X server, and may be somewhat slow.
209 * Deprecated: This function is obsolete. Use gdk_draw_pixbuf() instead.
212 gdk_pixbuf_render_to_drawable_alpha (GdkPixbuf *pixbuf,
213 GdkDrawable *drawable,
214 int src_x, int src_y,
215 int dest_x, int dest_y,
216 int width, int height,
217 GdkPixbufAlphaMode alpha_mode,
220 int x_dither, int y_dither)
222 gdk_draw_pixbuf (drawable, NULL, pixbuf,
223 src_x, src_y, dest_x, dest_y, width, height,
224 dither, x_dither, y_dither);
228 * gdk_pixbuf_render_pixmap_and_mask:
230 * @pixmap_return: Location to store a pointer to the created pixmap,
231 * or %NULL if the pixmap is not needed.
232 * @mask_return: Location to store a pointer to the created mask,
233 * or %NULL if the mask is not needed.
234 * @alpha_threshold: Threshold value for opacity values.
236 * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
237 * and @mask_return arguments, respectively, and renders a pixbuf and its
238 * corresponding thresholded alpha mask to them. This is merely a convenience
239 * function; applications that need to render pixbufs with dither offsets or to
240 * given drawables should use gdk_draw_pixbuf() and gdk_pixbuf_render_threshold_alpha().
242 * The pixmap that is created is created for the colormap returned
243 * by gdk_rgb_get_colormap(). You normally will want to instead use
244 * the actual colormap for a widget, and use
245 * gdk_pixbuf_render_pixmap_and_mask_for_colormap.
247 * If the pixbuf does not have an alpha channel, then *@mask_return will be set
251 gdk_pixbuf_render_pixmap_and_mask (GdkPixbuf *pixbuf,
252 GdkPixmap **pixmap_return,
253 GdkBitmap **mask_return,
256 gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf,
257 gdk_rgb_get_colormap (),
258 pixmap_return, mask_return,
263 * gdk_pixbuf_render_pixmap_and_mask_for_colormap:
265 * @colormap: A #GdkColormap
266 * @pixmap_return: Location to store a pointer to the created pixmap,
267 * or %NULL if the pixmap is not needed.
268 * @mask_return: Location to store a pointer to the created mask,
269 * or %NULL if the mask is not needed.
270 * @alpha_threshold: Threshold value for opacity values.
272 * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
273 * and @mask_return arguments, respectively, and renders a pixbuf and its
274 * corresponding tresholded alpha mask to them. This is merely a convenience
275 * function; applications that need to render pixbufs with dither offsets or to
276 * given drawables should use gdk_draw_pixbuf(), and gdk_pixbuf_render_threshold_alpha().
278 * The pixmap that is created uses the #GdkColormap specified by @colormap.
279 * This colormap must match the colormap of the window where the pixmap
280 * will eventually be used or an error will result.
282 * If the pixbuf does not have an alpha channel, then *@mask_return will be set
286 gdk_pixbuf_render_pixmap_and_mask_for_colormap (GdkPixbuf *pixbuf,
287 GdkColormap *colormap,
288 GdkPixmap **pixmap_return,
289 GdkBitmap **mask_return,
294 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
295 g_return_if_fail (GDK_IS_COLORMAP (colormap));
297 screen = gdk_colormap_get_screen (colormap);
302 *pixmap_return = gdk_pixmap_new (gdk_screen_get_root_window (screen),
303 gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
304 gdk_colormap_get_visual (colormap)->depth);
306 gdk_drawable_set_colormap (GDK_DRAWABLE (*pixmap_return), colormap);
307 gc = _gdk_drawable_get_scratch_gc (*pixmap_return, FALSE);
308 gdk_draw_pixbuf (*pixmap_return, gc, pixbuf,
310 gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
311 GDK_RGB_DITHER_NORMAL,
317 if (gdk_pixbuf_get_has_alpha (pixbuf))
319 *mask_return = gdk_pixmap_new (gdk_screen_get_root_window (screen),
320 gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf), 1);
322 gdk_pixbuf_render_threshold_alpha (pixbuf, *mask_return,
324 gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
333 * gdk_pixbuf_set_as_cairo_source:
334 * @pixbuf: a #GdkPixbuf
335 * @cr: a #Cairo context
336 * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
337 * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
339 * Sets the given pixbuf as the source pattern for the Cairo context.
340 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
341 * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y
344 gdk_pixbuf_set_as_cairo_source (GdkPixbuf *pixbuf,
349 gint width = gdk_pixbuf_get_width (pixbuf);
350 gint height = gdk_pixbuf_get_height (pixbuf);
351 guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
352 int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
353 int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
354 guchar *cairo_pixels;
355 cairo_format_t format;
356 cairo_surface_t *surface;
357 static const cairo_user_data_key_t key;
361 format = CAIRO_FORMAT_RGB24;
363 format = CAIRO_FORMAT_ARGB32;
365 cairo_pixels = g_malloc (4 * width * height);
366 surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
368 width, height, 4 * width);
369 cairo_surface_set_user_data (surface, &key,
370 cairo_pixels, (cairo_destroy_func_t)g_free);
372 for (j = height; j; j--)
374 guchar *p = gdk_pixels;
375 guchar *q = cairo_pixels;
379 guchar *end = p + 3 * width;
383 #if G_BYTE_ORDER == GDK_LSB_FIRST
398 guchar *end = p + 4 * width;
401 #define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
405 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
406 MULT(q[0], p[2], p[3], t1);
407 MULT(q[1], p[1], p[3], t2);
408 MULT(q[2], p[0], p[3], t3);
412 MULT(q[1], p[0], p[3], t1);
413 MULT(q[2], p[1], p[3], t2);
414 MULT(q[3], p[2], p[3], t3);
424 gdk_pixels += gdk_rowstride;
425 cairo_pixels += 4 * width;
428 cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
431 #define __GDK_PIXBUF_RENDER_C__
432 #include "gdkaliasdef.c"