1 /* GdkPixbuf library - convert X drawable information to RGB
3 * Copyright (C) 1999 Michael Zucchi
5 * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
6 * Cody Russell <bratsche@dfw.net>
7 * Federico Mena-Quintero <federico@gimp.org>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include "gdkwindow.h"
30 #include "gdkpixbuf.h"
31 #include "gdkinternals.h"
34 /* Exported functions */
37 * gdk_pixbuf_get_from_drawable:
38 * @dest: (allow-none): Destination pixbuf, or %NULL if a new pixbuf should be created.
39 * @src: Source drawable.
40 * @cmap: A colormap if @src doesn't have one set.
41 * @src_x: Source X coordinate within drawable.
42 * @src_y: Source Y coordinate within drawable.
43 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
44 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
45 * @width: Width in pixels of region to get.
46 * @height: Height in pixels of region to get.
48 * Transfers image data from a #GdkDrawable and converts it to an RGB(A)
49 * representation inside a #GdkPixbuf. In other words, copies
50 * image data from a server-side drawable to a client-side RGB(A) buffer.
51 * This allows you to efficiently read individual pixels on the client side.
53 * If the drawable @src has no colormap (gdk_drawable_get_colormap()
54 * returns %NULL), then a suitable colormap must be specified.
55 * If the drawable has a colormap, the @cmap argument will be
58 * If the specified destination pixbuf @dest is %NULL, then this
59 * function will create an RGB pixbuf with 8 bits per channel and no
60 * alpha, with the same size specified by the @width and @height
61 * arguments. In this case, the @dest_x and @dest_y arguments must be
62 * specified as 0. If the specified destination pixbuf is not %NULL
63 * and it contains alpha information, then the filled pixels will be
64 * set to full opacity (alpha = 255).
66 * If the specified drawable is a window, and the window is off the
67 * screen, then there is no image data in the obscured/offscreen
68 * regions to be placed in the pixbuf. The contents of portions of the
69 * pixbuf corresponding to the offscreen region are undefined.
71 * If the window you're obtaining data from is partially obscured by
72 * other windows, then the contents of the pixbuf areas corresponding
73 * to the obscured regions are undefined.
75 * If the target drawable is not mapped (typically because it's
76 * iconified/minimized or not on the current workspace), then %NULL
79 * If memory can't be allocated for the return value, %NULL will be returned
82 * (In short, there are several ways this function can fail, and if it fails
83 * it returns %NULL; so check the return value.)
85 * Return value: The same pixbuf as @dest if it was non-%NULL, or a newly-created
86 * pixbuf with a reference count of 1 if no destination pixbuf was specified, or %NULL on error
89 gdk_pixbuf_get_from_drawable (GdkPixbuf *dest,
93 int dest_x, int dest_y,
94 int width, int height)
96 cairo_surface_t *surface;
99 /* General sanity checks */
101 g_return_val_if_fail (src != NULL, NULL);
103 if (GDK_IS_WINDOW (src))
104 /* FIXME: this is not perfect, since is_viewable() only tests
105 * recursively up the Gdk parent window tree, but stops at
106 * foreign windows or Gdk toplevels. I.e. if a window manager
107 * unmapped one of its own windows, this won't work.
109 g_return_val_if_fail (gdk_window_is_viewable (src), NULL);
112 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
115 g_return_val_if_fail (gdk_pixbuf_get_colorspace (dest) == GDK_COLORSPACE_RGB, NULL);
116 g_return_val_if_fail (gdk_pixbuf_get_n_channels (dest) == 3 ||
117 gdk_pixbuf_get_n_channels (dest) == 4, NULL);
118 g_return_val_if_fail (gdk_pixbuf_get_bits_per_sample (dest) == 8, NULL);
122 cmap = gdk_drawable_get_colormap (src);
124 depth = gdk_drawable_get_depth (src);
126 if (depth != 1 && cmap == NULL)
128 g_warning ("%s: Source drawable has no colormap; either pass "
129 "in a colormap, or set the colormap on the drawable "
130 "with gdk_drawable_set_colormap()", G_STRLOC);
134 if (cmap != NULL && depth != cmap->visual->depth)
136 g_warning ("%s: Depth of the source drawable is %d where as "
137 "the visual depth of the colormap passed is %d",
138 G_STRLOC, depth, cmap->visual->depth);
142 /* Coordinate sanity checks */
144 surface = _gdk_drawable_ref_cairo_surface (src);
145 dest = gdk_pixbuf_get_from_surface (dest,
150 cairo_surface_destroy (surface);
155 static cairo_format_t
156 gdk_cairo_format_for_content (cairo_content_t content)
160 case CAIRO_CONTENT_COLOR:
161 return CAIRO_FORMAT_RGB24;
162 case CAIRO_CONTENT_ALPHA:
163 return CAIRO_FORMAT_A8;
164 case CAIRO_CONTENT_COLOR_ALPHA:
166 return CAIRO_FORMAT_ARGB32;
170 static cairo_surface_t *
171 gdk_cairo_surface_coerce_to_image (cairo_surface_t *surface,
172 cairo_content_t content,
176 cairo_surface_t *copy;
179 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE &&
180 cairo_surface_get_content (surface) == content &&
181 cairo_image_surface_get_width (surface) >= width &&
182 cairo_image_surface_get_height (surface) >= height)
183 return cairo_surface_reference (surface);
185 copy = cairo_image_surface_create (gdk_cairo_format_for_content (content),
189 cr = cairo_create (copy);
190 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
191 cairo_set_source_surface (cr, surface, 0, 0);
199 convert_alpha (guchar *dest_data,
212 dest_data += dest_stride * dest_y + dest_x * 4;
213 src_data += src_stride * src_y + src_x * 4;
215 for (y = 0; y < height; y++) {
216 guint32 *src = (guint32 *) src_data;
218 for (x = 0; x < width; x++) {
219 guint alpha = src[x] >> 24;
223 dest_data[x * 4 + 0] = 0;
224 dest_data[x * 4 + 1] = 0;
225 dest_data[x * 4 + 2] = 0;
229 dest_data[x * 4 + 0] = (((src[x] & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
230 dest_data[x * 4 + 1] = (((src[x] & 0x00ff00) >> 8) * 255 + alpha / 2) / alpha;
231 dest_data[x * 4 + 2] = (((src[x] & 0x0000ff) >> 0) * 255 + alpha / 2) / alpha;
233 dest_data[x * 4 + 3] = alpha;
236 src_data += src_stride;
237 dest_data += dest_stride;
242 convert_no_alpha (guchar *dest_data,
255 dest_data += dest_stride * dest_y + dest_x * 3;
256 src_data += src_stride * src_y + src_x * 4;
258 for (y = 0; y < height; y++) {
259 guint32 *src = (guint32 *) src_data;
261 for (x = 0; x < width; x++) {
262 dest_data[x * 3 + 0] = src[x] >> 16;
263 dest_data[x * 3 + 1] = src[x] >> 8;
264 dest_data[x * 3 + 2] = src[x];
267 src_data += src_stride;
268 dest_data += dest_stride;
273 * gdk_pixbuf_get_from_surface:
274 * @dest: (allow-none): Destination pixbuf, or %NULL if a new pixbuf should be created.
275 * @surface: surface to copy from
276 * @src_x: Source X coordinate within drawable.
277 * @src_y: Source Y coordinate within drawable.
278 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
279 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
280 * @width: Width in pixels of region to get.
281 * @height: Height in pixels of region to get.
283 * Transfers image data from a #cairo_surface_t and converts it to an RGB(A)
284 * representation inside a #GdkPixbuf. This allows you to efficiently read individual
285 * pixels from Cairo surfaces. For #GdkWindows, use gdk_pixbuf_get_from_drawable()
288 * If the specified destination pixbuf @dest is %NULL, then this
289 * function will create an RGB pixbuf with 8 bits per channel. The pixbuf will
290 * contain an alpha channel if the @surface contains one. In this case, the @dest_x
291 * and @dest_y arguments must be specified as 0.
293 * If the specified drawable is a window, and the window is off the
294 * screen, then there is no image data in the obscured/offscreen
295 * regions to be placed in the pixbuf. The contents of portions of the
296 * pixbuf corresponding to the offscreen region are undefined.
298 * If the window you're obtaining data from is partially obscured by
299 * other windows, then the contents of the pixbuf areas corresponding
300 * to the obscured regions are undefined.
302 * If memory can't be allocated for the return value, %NULL will be returned
305 * (In short, there are several ways this function can fail, and if it fails
306 * it returns %NULL; so check the return value.)
308 * Return value: The same pixbuf as @dest if it was non-%NULL, or a newly-created
309 * pixbuf with a reference count of 1 if no destination pixbuf was specified, or %NULL on error
312 gdk_pixbuf_get_from_surface (GdkPixbuf *dest,
313 cairo_surface_t *surface,
321 cairo_content_t content;
323 /* General sanity checks */
324 g_return_val_if_fail (surface != NULL, NULL);
325 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
326 g_return_val_if_fail (width > 0 && height > 0, NULL);
330 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
332 content = cairo_surface_get_content (surface) | CAIRO_CONTENT_COLOR;
333 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
334 !!(content & CAIRO_CONTENT_ALPHA),
340 g_return_val_if_fail (gdk_pixbuf_get_colorspace (dest) == GDK_COLORSPACE_RGB, NULL);
341 g_return_val_if_fail (gdk_pixbuf_get_n_channels (dest) == 3 ||
342 gdk_pixbuf_get_n_channels (dest) == 4, NULL);
343 g_return_val_if_fail (gdk_pixbuf_get_bits_per_sample (dest) == 8, NULL);
344 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
345 g_return_val_if_fail (dest_x + width <= gdk_pixbuf_get_width (dest), NULL);
346 g_return_val_if_fail (dest_y + height <= gdk_pixbuf_get_height (dest), NULL);
348 content = gdk_pixbuf_get_has_alpha (dest) ? CAIRO_CONTENT_COLOR_ALPHA : CAIRO_CONTENT_COLOR;
351 surface = gdk_cairo_surface_coerce_to_image (surface, content, src_x + width, src_y + height);
352 cairo_surface_flush (surface);
353 if (cairo_surface_status (surface) || dest == NULL)
355 cairo_surface_destroy (surface);
359 if (gdk_pixbuf_get_has_alpha (dest))
360 convert_alpha (gdk_pixbuf_get_pixels (dest),
361 gdk_pixbuf_get_rowstride (dest),
362 cairo_image_surface_get_data (surface),
363 cairo_image_surface_get_stride (surface),
368 convert_no_alpha (gdk_pixbuf_get_pixels (dest),
369 gdk_pixbuf_get_rowstride (dest),
370 cairo_image_surface_get_data (surface),
371 cairo_image_surface_get_stride (surface),
376 cairo_surface_destroy (surface);