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.
23 /* Trivially ported to Xlib(RGB) by John Harper. */
26 #include "gdk-pixbuf-private.h"
27 #include "gdk-pixbuf-xlib-private.h"
32 * gdk_pixbuf_xlib_render_threshold_alpha:
34 * @bitmap: Bitmap where the bilevel mask will be painted to.
35 * @src_x: Source X coordinate.
36 * @src_y: source Y coordinate.
37 * @dest_x: Destination X coordinate.
38 * @dest_y: Destination Y coordinate.
39 * @width: Width of region to threshold.
40 * @height: Height of region to threshold.
41 * @alpha_threshold: Opacity values below this will be painted as zero; all
42 * other values will be painted as one.
44 * Takes the opacity values in a rectangular portion of a pixbuf and thresholds
45 * them to produce a bi-level alpha mask that can be used as a clipping mask for
50 gdk_pixbuf_xlib_render_threshold_alpha (GdkPixbuf *pixbuf, Pixmap bitmap,
52 int dest_x, int dest_y,
53 int width, int height,
60 int start, start_status;
64 g_return_if_fail (pixbuf != NULL);
65 g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
66 g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
67 g_return_if_fail (pixbuf->bits_per_sample == 8);
69 g_return_if_fail (bitmap != 0);
70 g_return_if_fail (width >= 0 && height >= 0);
71 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
72 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
74 g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);
76 if (width == 0 || height == 0)
79 gc = XCreateGC (gdk_pixbuf_dpy, bitmap, 0, &gcv);
81 if (!pixbuf->has_alpha) {
82 color.pixel = (alpha_threshold == 255) ? 0 : 1;
83 XSetForeground (gdk_pixbuf_dpy, gc, color.pixel);
84 XFillRectangle (gdk_pixbuf_dpy, bitmap, gc,
85 dest_x, dest_y, width, height);
86 XFreeGC (gdk_pixbuf_dpy, gc);
91 XSetForeground (gdk_pixbuf_dpy, gc, color.pixel);
92 XFillRectangle (gdk_pixbuf_dpy, bitmap, gc,
93 dest_x, dest_y, width, height);
96 XSetForeground (gdk_pixbuf_dpy, gc, color.pixel);
98 for (y = 0; y < height; y++) {
99 p = (pixbuf->pixels + (y + src_y) * pixbuf->rowstride + src_x * pixbuf->n_channels
100 + pixbuf->n_channels - 1);
103 start_status = *p < alpha_threshold;
105 for (x = 0; x < width; x++) {
106 status = *p < alpha_threshold;
108 if (status != start_status) {
110 XDrawLine (gdk_pixbuf_dpy, bitmap, gc,
111 start + dest_x, y + dest_y,
112 x - 1 + dest_x, y + dest_y);
115 start_status = status;
118 p += pixbuf->n_channels;
122 XDrawLine (gdk_pixbuf_dpy, bitmap, gc,
123 start + dest_x, y + dest_y,
124 x - 1 + dest_x, y + dest_y);
127 XFreeGC (gdk_pixbuf_dpy, gc);
132 /* Creates a buffer by stripping the alpha channel of a pixbuf */
134 remove_alpha (GdkPixbuf *pixbuf, int x, int y, int width, int height, int *rowstride)
140 g_assert (pixbuf->n_channels == 4);
141 g_assert (pixbuf->has_alpha);
142 g_assert (width > 0 && height > 0);
143 g_assert (x >= 0 && x + width <= pixbuf->width);
144 g_assert (y >= 0 && y + height <= pixbuf->height);
146 *rowstride = 4 * ((width * 3 + 3) / 4);
148 buf = g_new (guchar, *rowstride * height);
150 for (yy = 0; yy < height; yy++) {
151 src = pixbuf->pixels + pixbuf->rowstride * (yy + y) + x * pixbuf->n_channels;
152 dest = buf + *rowstride * yy;
154 for (xx = 0; xx < width; xx++) {
166 * gdk_pixbuf_xlib_render_to_drawable:
168 * @drawable: Destination drawable.
169 * @gc: GC used for rendering.
170 * @src_x: Source X coordinate within pixbuf.
171 * @src_y: Source Y coordinate within pixbuf.
172 * @dest_x: Destination X coordinate within drawable.
173 * @dest_y: Destination Y coordinate within drawable.
174 * @width: Width of region to render, in pixels.
175 * @height: Height of region to render, in pixels.
176 * @dither: Dithering mode for XlibRGB.
177 * @x_dither: X offset for dither.
178 * @y_dither: Y offset for dither.
180 * Renders a rectangular portion of a pixbuf to a drawable while using the
181 * specified GC. This is done using XlibRGB, so the specified drawable must
182 * have the XlibRGB visual and colormap. Note that this function will ignore
183 * the opacity information for images with an alpha channel; the GC must already
184 * have the clipping mask set if you want transparent regions to show through.
186 * For an explanation of dither offsets, see the XlibRGB documentation. In
187 * brief, the dither offset is important when re-rendering partial regions of an
188 * image to a rendered version of the full image, or for when the offsets to a
189 * base position change, as in scrolling. The dither matrix has to be shifted
190 * for consistent visual results. If you do not have any of these cases, the
191 * dither offsets can be both zero.
194 gdk_pixbuf_xlib_render_to_drawable (GdkPixbuf *pixbuf,
195 Drawable drawable, GC gc,
196 int src_x, int src_y,
197 int dest_x, int dest_y,
198 int width, int height,
199 XlibRgbDither dither,
200 int x_dither, int y_dither)
205 g_return_if_fail (pixbuf != NULL);
206 g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
207 g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
208 g_return_if_fail (pixbuf->bits_per_sample == 8);
210 g_return_if_fail (drawable != 0);
211 g_return_if_fail (gc != 0);
213 g_return_if_fail (width >= 0 && height >= 0);
214 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
215 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
217 if (width == 0 || height == 0)
220 /* This will have to be modified once we support other image types.
221 * Also, GdkRGB does not have gdk_draw_rgb_32_image_dithalign(), so we
222 * have to pack the buffer first. Sigh.
225 if (pixbuf->has_alpha)
226 buf = remove_alpha (pixbuf, src_x, src_y, width, height, &rowstride);
228 buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
229 rowstride = pixbuf->rowstride;
232 xlib_draw_rgb_image_dithalign (drawable, gc,
239 if (pixbuf->has_alpha)
246 * gdk_pixbuf_xlib_render_to_drawable_alpha:
248 * @drawable: Destination drawable.
249 * @src_x: Source X coordinate within pixbuf.
250 * @src_y: Source Y coordinates within pixbuf.
251 * @dest_x: Destination X coordinate within drawable.
252 * @dest_y: Destination Y coordinate within drawable.
253 * @width: Width of region to render, in pixels.
254 * @height: Height of region to render, in pixels.
255 * @alpha_mode: If the image does not have opacity information, this is ignored.
256 * Otherwise, specifies how to handle transparency when rendering.
257 * @alpha_threshold: If the image does have opacity information and @alpha_mode
258 * is GDK_PIXBUF_ALPHA_BILEVEL, specifies the threshold value for opacity
260 * @dither: Dithering mode for XlibRGB.
261 * @x_dither: X offset for dither.
262 * @y_dither: Y offset for dither.
264 * Renders a rectangular portion of a pixbuf to a drawable. This is done using
265 * XlibRGB, so the specified drawable must have the XlibRGB visual and colormap.
267 * When used with #GDK_PIXBUF_ALPHA_BILEVEL, this function has to create a bitmap
268 * out of the thresholded alpha channel of the image and, it has to set this
269 * bitmap as the clipping mask for the GC used for drawing. This can be a
270 * significant performance penalty depending on the size and the complexity of
271 * the alpha channel of the image. If performance is crucial, consider handling
272 * the alpha channel yourself (possibly by caching it in your application) and
273 * using gdk_pixbuf_xlib_render_to_drawable() or GdkRGB directly instead.
276 gdk_pixbuf_xlib_render_to_drawable_alpha (GdkPixbuf *pixbuf, Drawable drawable,
277 int src_x, int src_y,
278 int dest_x, int dest_y,
279 int width, int height,
280 GdkPixbufAlphaMode alpha_mode,
282 XlibRgbDither dither,
283 int x_dither, int y_dither)
289 g_return_if_fail (pixbuf != NULL);
290 g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
291 g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
292 g_return_if_fail (pixbuf->bits_per_sample == 8);
294 g_return_if_fail (drawable != 0);
295 g_return_if_fail (width >= 0 && height >= 0);
296 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
297 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
299 if (width == 0 || height == 0)
302 gc = XCreateGC (gdk_pixbuf_dpy, drawable, 0, &gcv);
304 if (pixbuf->has_alpha) {
305 /* Right now we only support GDK_PIXBUF_ALPHA_BILEVEL, so we
306 * unconditionally create the clipping mask.
309 bitmap = XCreatePixmap (gdk_pixbuf_dpy,
310 RootWindow (gdk_pixbuf_dpy,
313 gdk_pixbuf_xlib_render_threshold_alpha (pixbuf, bitmap,
319 XSetClipMask (gdk_pixbuf_dpy, gc, bitmap);
320 XSetClipOrigin (gdk_pixbuf_dpy, gc, dest_x, dest_y);
323 gdk_pixbuf_xlib_render_to_drawable (pixbuf, drawable, gc,
331 XFreePixmap (gdk_pixbuf_dpy, bitmap);
333 XFreeGC (gdk_pixbuf_dpy, gc);
337 * gdk_pixbuf_xlib_render_pixmap_and_mask:
339 * @pixmap_return: Return value for the created pixmap.
340 * @mask_return: Return value for the created mask.
341 * @alpha_threshold: Threshold value for opacity values.
343 * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
344 * and @mask_return arguments, respectively, and renders a pixbuf and its
345 * corresponding tresholded alpha mask to them. This is merely a convenience
346 * function; applications that need to render pixbufs with dither offsets or to
347 * given drawables should use gdk_pixbuf_xlib_render_to_drawable_alpha() or
348 * gdk_pixbuf_xlib_render_to_drawable(), and
349 * gdk_pixbuf_xlib_render_threshold_alpha().
351 * If the pixbuf does not have an alpha channel, then *@mask_return will be set
355 gdk_pixbuf_xlib_render_pixmap_and_mask (GdkPixbuf *pixbuf,
356 Pixmap *pixmap_return,
360 g_return_if_fail (pixbuf != NULL);
366 *pixmap_return = XCreatePixmap (gdk_pixbuf_dpy,
367 RootWindow (gdk_pixbuf_dpy,
371 xlib_rgb_get_depth ());
372 gc = XCreateGC (gdk_pixbuf_dpy, *pixmap_return, 0, &gcv);
373 gdk_pixbuf_xlib_render_to_drawable (pixbuf, *pixmap_return, gc,
377 XLIB_RGB_DITHER_NORMAL,
379 XFreeGC (gdk_pixbuf_dpy, gc);
383 if (pixbuf->has_alpha) {
384 *mask_return = XCreatePixmap (gdk_pixbuf_dpy,
385 RootWindow (gdk_pixbuf_dpy,
389 gdk_pixbuf_xlib_render_threshold_alpha (pixbuf,