]> Pileus Git - ~andy/gtk/blob - gdk/gdkpixbuf-drawable.c
gdk: Remove GdkPixmap
[~andy/gtk] / gdk / gdkpixbuf-drawable.c
1 /* GdkPixbuf library - convert X drawable information to RGB
2  *
3  * Copyright (C) 1999 Michael Zucchi
4  *
5  * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
6  *          Cody Russell <bratsche@dfw.net>
7  *          Federico Mena-Quintero <federico@gimp.org>
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #include "config.h"
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27
28 #include "gdkcolor.h"
29 #include "gdkwindow.h"
30 #include "gdkpixbuf.h"
31 #include "gdkinternals.h"
32
33
34 /* Exported functions */
35
36 /**
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.
47  *
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.
52  * 
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
56  * ignored.
57  *
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).
65  *
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.
70  *
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.
74  * 
75  * If the target drawable is not mapped (typically because it's
76  * iconified/minimized or not on the current workspace), then %NULL
77  * will be returned.
78  *
79  * If memory can't be allocated for the return value, %NULL will be returned
80  * instead.
81  *
82  * (In short, there are several ways this function can fail, and if it fails
83  *  it returns %NULL; so check the return value.)
84  *
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
87  **/
88 GdkPixbuf *
89 gdk_pixbuf_get_from_drawable (GdkPixbuf   *dest,
90                               GdkDrawable *src,
91                               GdkColormap *cmap,
92                               int src_x,  int src_y,
93                               int dest_x, int dest_y,
94                               int width,  int height)
95 {
96   cairo_surface_t *surface;
97   int depth;
98   
99   /* General sanity checks */
100
101   g_return_val_if_fail (src != NULL, NULL);
102
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.
108      */
109     g_return_val_if_fail (gdk_window_is_viewable (src), NULL);
110
111   if (!dest)
112     g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
113   else
114     {
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);
119     }
120
121   if (cmap == NULL)
122     cmap = gdk_drawable_get_colormap (src);
123
124   depth = gdk_drawable_get_depth (src);
125   
126   if (depth != 1 && cmap == NULL)
127     {
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);
131       return NULL;
132     }
133   
134   if (cmap != NULL && depth != cmap->visual->depth)
135     {
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);
139       return NULL;
140     } 
141  
142   /* Coordinate sanity checks */
143   
144   surface = _gdk_drawable_ref_cairo_surface (src);
145   dest = gdk_pixbuf_get_from_surface (dest,
146                                       surface,
147                                       src_x, src_y,
148                                       dest_x, dest_y,
149                                       width, height);
150   cairo_surface_destroy (surface);
151
152   return dest;
153 }
154         
155 static cairo_format_t
156 gdk_cairo_format_for_content (cairo_content_t content)
157 {
158   switch (content)
159     {
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:
165     default:
166       return CAIRO_FORMAT_ARGB32;
167     }
168 }
169
170 static cairo_surface_t *
171 gdk_cairo_surface_coerce_to_image (cairo_surface_t *surface,
172                                    cairo_content_t content,
173                                    int width,
174                                    int height)
175 {
176   cairo_surface_t *copy;
177   cairo_t *cr;
178
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);
184
185   copy = cairo_image_surface_create (gdk_cairo_format_for_content (content),
186                                      width,
187                                      height);
188
189   cr = cairo_create (copy);
190   cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
191   cairo_set_source_surface (cr, surface, 0, 0);
192   cairo_paint (cr);
193   cairo_destroy (cr);
194
195   return copy;
196 }
197
198 static void
199 convert_alpha (guchar  *dest_data,
200                int      dest_stride,
201                guchar  *src_data,
202                int      src_stride,
203                int      src_x,
204                int      src_y,
205                int      dest_x,
206                int      dest_y,
207                int      width,
208                int      height)
209 {
210   int x, y;
211
212   dest_data += dest_stride * dest_y + dest_x * 4;
213   src_data += src_stride * src_y + src_x * 4;
214
215   for (y = 0; y < height; y++) {
216     guint32 *src = (guint32 *) src_data;
217
218     for (x = 0; x < width; x++) {
219       guint alpha = src[x] >> 24;
220
221       if (alpha == 0)
222         {
223           dest_data[x * 4 + 0] = 0;
224           dest_data[x * 4 + 1] = 0;
225           dest_data[x * 4 + 2] = 0;
226         }
227       else
228         {
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;
232         }
233       dest_data[x * 4 + 3] = alpha;
234     }
235
236     src_data += src_stride;
237     dest_data += dest_stride;
238   }
239 }
240
241 static void
242 convert_no_alpha (guchar  *dest_data,
243                   int      dest_stride,
244                   guchar  *src_data,
245                   int      src_stride,
246                   int      src_x,
247                   int      src_y,
248                   int      dest_x,
249                   int      dest_y,
250                   int      width,
251                   int      height)
252 {
253   int x, y;
254
255   dest_data += dest_stride * dest_y + dest_x * 3;
256   src_data += src_stride * src_y + src_x * 4;
257
258   for (y = 0; y < height; y++) {
259     guint32 *src = (guint32 *) src_data;
260
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];
265     }
266
267     src_data += src_stride;
268     dest_data += dest_stride;
269   }
270 }
271
272 /**
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.
282  *
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()
286  * instead.
287  * 
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.
292  *
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.
297  *
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.
301  * 
302  * If memory can't be allocated for the return value, %NULL will be returned
303  * instead.
304  *
305  * (In short, there are several ways this function can fail, and if it fails
306  *  it returns %NULL; so check the return value.)
307  *
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
310  **/
311 GdkPixbuf *
312 gdk_pixbuf_get_from_surface  (GdkPixbuf       *dest,
313                               cairo_surface_t *surface,
314                               int              src_x,
315                               int              src_y,
316                               int              dest_x,
317                               int              dest_y,
318                               int              width,
319                               int              height)
320 {
321   cairo_content_t content;
322   
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);
327
328   if (!dest)
329     {
330       g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
331       
332       content = cairo_surface_get_content (surface) | CAIRO_CONTENT_COLOR;
333       dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 
334                              !!(content & CAIRO_CONTENT_ALPHA),
335                              8,
336                              width, height);
337     }
338   else
339     {
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);
347
348       content = gdk_pixbuf_get_has_alpha (dest) ? CAIRO_CONTENT_COLOR_ALPHA : CAIRO_CONTENT_COLOR;
349     }
350
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)
354     {
355       cairo_surface_destroy (surface);
356       return NULL;
357     }
358
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),
364                    src_x, src_y,
365                    dest_x, dest_y,
366                    width, height);
367   else
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),
372                       src_x, src_y,
373                       dest_x, dest_y,
374                       width, height);
375
376   cairo_surface_destroy (surface);
377   return dest;
378 }
379