]> Pileus Git - ~andy/gtk/blob - contrib/gdk-pixbuf-xlib/gdk-pixbuf-xlib-render.c
[quartz] Delete the typedef of GdkDevicePrivate
[~andy/gtk] / contrib / gdk-pixbuf-xlib / gdk-pixbuf-xlib-render.c
1 /* GdkPixbuf library - Rendering functions
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
5  * Author: Federico Mena-Quintero <federico@gimp.org>
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /* Trivially ported to Xlib(RGB) by John Harper. */
24
25 #include "config.h"
26 #include "gdk-pixbuf-private.h"
27 #include "gdk-pixbuf-xlib-private.h"
28
29 \f
30
31 /**
32  * gdk_pixbuf_xlib_render_threshold_alpha:
33  * @pixbuf: A pixbuf.
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.
43  *
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
46  * a drawable.
47  *
48  **/
49 void
50 gdk_pixbuf_xlib_render_threshold_alpha (GdkPixbuf *pixbuf, Pixmap bitmap,
51                                         int src_x, int src_y,
52                                         int dest_x, int dest_y,
53                                         int width, int height,
54                                         int alpha_threshold)
55 {
56         GC gc;
57         XColor color;
58         int x, y;
59         guchar *p;
60         int start, start_status;
61         int status;
62         XGCValues gcv;
63
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);
68
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);
73
74         g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);
75
76         if (width == 0 || height == 0)
77                 return;
78
79         gc = XCreateGC (gdk_pixbuf_dpy, bitmap, 0, &gcv);
80
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);
87                 return;
88         }
89
90         color.pixel = 0;
91         XSetForeground (gdk_pixbuf_dpy, gc, color.pixel);
92         XFillRectangle (gdk_pixbuf_dpy, bitmap, gc,
93                         dest_x, dest_y, width, height);
94
95         color.pixel = 1;
96         XSetForeground (gdk_pixbuf_dpy, gc, color.pixel);
97
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);
101
102                 start = 0;
103                 start_status = *p < alpha_threshold;
104
105                 for (x = 0; x < width; x++) {
106                         status = *p < alpha_threshold;
107
108                         if (status != start_status) {
109                                 if (!start_status)
110                                         XDrawLine (gdk_pixbuf_dpy, bitmap, gc,
111                                                    start + dest_x, y + dest_y,
112                                                    x - 1 + dest_x, y + dest_y);
113
114                                 start = x;
115                                 start_status = status;
116                         }
117
118                         p += pixbuf->n_channels;
119                 }
120
121                 if (!start_status)
122                         XDrawLine (gdk_pixbuf_dpy, bitmap, gc,
123                                    start + dest_x, y + dest_y,
124                                    x - 1 + dest_x, y + dest_y);
125         }
126
127         XFreeGC (gdk_pixbuf_dpy, gc);
128 }
129
130 \f
131
132 /* Creates a buffer by stripping the alpha channel of a pixbuf */
133 static guchar *
134 remove_alpha (GdkPixbuf *pixbuf, int x, int y, int width, int height, int *rowstride)
135 {
136         guchar *buf;
137         int xx, yy;
138         guchar *src, *dest;
139
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);
145
146         *rowstride = 4 * ((width * 3 + 3) / 4);
147
148         buf = g_new (guchar, *rowstride * height);
149
150         for (yy = 0; yy < height; yy++) {
151                 src = pixbuf->pixels + pixbuf->rowstride * (yy + y) + x * pixbuf->n_channels;
152                 dest = buf + *rowstride * yy;
153
154                 for (xx = 0; xx < width; xx++) {
155                         *dest++ = *src++;
156                         *dest++ = *src++;
157                         *dest++ = *src++;
158                         src++;
159                 }
160         }
161
162         return buf;
163 }
164
165 /**
166  * gdk_pixbuf_xlib_render_to_drawable:
167  * @pixbuf: A pixbuf.
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.
179  *
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.
185  *
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.
192  **/
193 void
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)
201 {
202         guchar *buf;
203         int rowstride;
204
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);
209
210         g_return_if_fail (drawable != 0);
211         g_return_if_fail (gc != 0);
212
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);
216
217         if (width == 0 || height == 0)
218                 return;
219
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.
223          */
224
225         if (pixbuf->has_alpha)
226                 buf = remove_alpha (pixbuf, src_x, src_y, width, height, &rowstride);
227         else {
228                 buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
229                 rowstride = pixbuf->rowstride;
230         }
231
232         xlib_draw_rgb_image_dithalign (drawable, gc,
233                                        dest_x, dest_y,
234                                        width, height,
235                                        dither,
236                                        buf, rowstride,
237                                        x_dither, y_dither);
238
239         if (pixbuf->has_alpha)
240                 g_free (buf);
241 }
242
243 \f
244
245 /**
246  * gdk_pixbuf_xlib_render_to_drawable_alpha:
247  * @pixbuf: A pixbuf.
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
259  * values.
260  * @dither: Dithering mode for XlibRGB.
261  * @x_dither: X offset for dither.
262  * @y_dither: Y offset for dither.
263  *
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.
266  *
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.
274  **/
275 void
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,
281                                           int alpha_threshold,
282                                           XlibRgbDither dither,
283                                           int x_dither, int y_dither)
284 {
285         Pixmap bitmap = 0;
286         GC gc;
287         XGCValues gcv;
288
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);
293
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);
298
299         if (width == 0 || height == 0)
300                 return;
301
302         gc = XCreateGC (gdk_pixbuf_dpy, drawable, 0, &gcv);
303
304         if (pixbuf->has_alpha) {
305                 /* Right now we only support GDK_PIXBUF_ALPHA_BILEVEL, so we
306                  * unconditionally create the clipping mask.
307                  */
308
309                 bitmap = XCreatePixmap (gdk_pixbuf_dpy,
310                                         RootWindow (gdk_pixbuf_dpy,
311                                                     gdk_pixbuf_screen),
312                                         width, height, 1);
313                 gdk_pixbuf_xlib_render_threshold_alpha (pixbuf, bitmap,
314                                                         src_x, src_y,
315                                                         0, 0,
316                                                         width, height,
317                                                         alpha_threshold);
318
319                 XSetClipMask (gdk_pixbuf_dpy, gc, bitmap);
320                 XSetClipOrigin (gdk_pixbuf_dpy, gc, dest_x, dest_y);
321         }
322
323         gdk_pixbuf_xlib_render_to_drawable (pixbuf, drawable, gc,
324                                             src_x, src_y,
325                                             dest_x, dest_y,
326                                             width, height,
327                                             dither,
328                                             x_dither, y_dither);
329
330         if (bitmap)
331                 XFreePixmap (gdk_pixbuf_dpy, bitmap);
332
333         XFreeGC (gdk_pixbuf_dpy, gc);
334 }
335
336 /**
337  * gdk_pixbuf_xlib_render_pixmap_and_mask:
338  * @pixbuf: A pixbuf.
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.
342  *
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().
350  *
351  * If the pixbuf does not have an alpha channel, then *@mask_return will be set
352  * to None.
353  **/
354 void
355 gdk_pixbuf_xlib_render_pixmap_and_mask (GdkPixbuf *pixbuf,
356                                         Pixmap *pixmap_return,
357                                         Pixmap *mask_return,
358                                         int alpha_threshold)
359 {
360         g_return_if_fail (pixbuf != NULL);
361
362         if (pixmap_return) {
363                 GC gc;
364                 XGCValues gcv;
365
366                 *pixmap_return = XCreatePixmap (gdk_pixbuf_dpy,
367                                                 RootWindow (gdk_pixbuf_dpy,
368                                                             gdk_pixbuf_screen),
369                                                 pixbuf->width,
370                                                 pixbuf->height,
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,
374                                                     0, 0, 0, 0,
375                                                     pixbuf->width,
376                                                     pixbuf->height,
377                                                     XLIB_RGB_DITHER_NORMAL,
378                                                     0, 0);
379                 XFreeGC (gdk_pixbuf_dpy, gc);
380         }
381
382         if (mask_return) {
383                 if (pixbuf->has_alpha) {
384                     *mask_return = XCreatePixmap (gdk_pixbuf_dpy,
385                                                   RootWindow (gdk_pixbuf_dpy,
386                                                               gdk_pixbuf_screen),
387                                                   pixbuf->width,
388                                                   pixbuf->height, 1);
389                     gdk_pixbuf_xlib_render_threshold_alpha (pixbuf,
390                                                             *mask_return,
391                                                             0, 0, 0, 0,
392                                                             pixbuf->width,
393                                                             pixbuf->height,
394                                                             alpha_threshold);
395                 } else
396                         *mask_return = 0;
397         }
398 }