]> Pileus Git - ~andy/gtk/blob - gdk/gdkpixbuf-drawable.c
67d890c9b659fe3e58d34f10bf2deabfb17a92f9
[~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
27 #include "gdkpixbuf.h"
28
29 #include "gdkcolor.h"
30 #include "gdkwindow.h"
31 #include "gdkinternals.h"
32
33 #include <gdk-pixbuf/gdk-pixbuf.h>
34
35 /**
36  * SECTION:pixbufs
37  * @Short_description: Functions for obtaining pixbufs
38  * @Title: Pixbufs
39  *
40  * Pixbufs are client-side images. For details on how to create
41  * and manipulate pixbufs, see the #GdkPixbuf API documentation.
42  *
43  * The functions described here allow to obtain pixbufs from
44  * #GdkWindows and cairo surfaces.
45  */
46
47
48 /**
49  * gdk_pixbuf_get_from_window:
50  * @window: Source window
51  * @src_x: Source X coordinate within @window
52  * @src_y: Source Y coordinate within @window
53  * @width: Width in pixels of region to get
54  * @height: Height in pixels of region to get
55  *
56  * Transfers image data from a #GdkWindow and converts it to an RGB(A)
57  * representation inside a #GdkPixbuf. In other words, copies
58  * image data from a server-side drawable to a client-side RGB(A) buffer.
59  * This allows you to efficiently read individual pixels on the client side.
60  *
61  * This function will create an RGB pixbuf with 8 bits per channel with
62  * the same size specified by the @width and @height arguments. The pixbuf
63  * will contain an alpha channel if the @window contains one.
64  *
65  * If the window is off the screen, then there is no image data in the
66  * obscured/offscreen regions to be placed in the pixbuf. The contents of
67  * portions of the pixbuf corresponding to the offscreen region are undefined.
68  *
69  * If the window you're obtaining data from is partially obscured by
70  * other windows, then the contents of the pixbuf areas corresponding
71  * to the obscured regions are undefined.
72  *
73  * If the window is not mapped (typically because it's iconified/minimized
74  * or not on the current workspace), then %NULL will be returned.
75  *
76  * If memory can't be allocated for the return value, %NULL will be returned
77  * instead.
78  *
79  * (In short, there are several ways this function can fail, and if it fails
80  *  it returns %NULL; so check the return value.)
81  *
82  * Return value: (transfer full): A newly-created pixbuf with a reference
83  *     count of 1, or %NULL on error
84  */
85 GdkPixbuf *
86 gdk_pixbuf_get_from_window (GdkWindow *src,
87                             gint       src_x,
88                             gint       src_y,
89                             gint       width,
90                             gint       height)
91 {
92   cairo_surface_t *surface;
93   GdkPixbuf *dest;
94
95   g_return_val_if_fail (GDK_IS_WINDOW (src), NULL);
96   g_return_val_if_fail (gdk_window_is_viewable (src), NULL);
97
98   surface = _gdk_window_ref_cairo_surface (src);
99   dest = gdk_pixbuf_get_from_surface (surface,
100                                       src_x, src_y,
101                                       width, height);
102   cairo_surface_destroy (surface);
103
104   return dest;
105 }
106
107 static cairo_format_t
108 gdk_cairo_format_for_content (cairo_content_t content)
109 {
110   switch (content)
111     {
112     case CAIRO_CONTENT_COLOR:
113       return CAIRO_FORMAT_RGB24;
114     case CAIRO_CONTENT_ALPHA:
115       return CAIRO_FORMAT_A8;
116     case CAIRO_CONTENT_COLOR_ALPHA:
117     default:
118       return CAIRO_FORMAT_ARGB32;
119     }
120 }
121
122 static cairo_surface_t *
123 gdk_cairo_surface_coerce_to_image (cairo_surface_t *surface,
124                                    cairo_content_t  content,
125                                    int              src_x,
126                                    int              src_y,
127                                    int              width,
128                                    int              height)
129 {
130   cairo_surface_t *copy;
131   cairo_t *cr;
132
133   copy = cairo_image_surface_create (gdk_cairo_format_for_content (content),
134                                      width,
135                                      height);
136
137   cr = cairo_create (copy);
138   cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
139   cairo_set_source_surface (cr, surface, -src_x, -src_y);
140   cairo_paint (cr);
141   cairo_destroy (cr);
142
143   return copy;
144 }
145
146 static void
147 convert_alpha (guchar *dest_data,
148                int     dest_stride,
149                guchar *src_data,
150                int     src_stride,
151                int     src_x,
152                int     src_y,
153                int     width,
154                int     height)
155 {
156   int x, y;
157
158   src_data += src_stride * src_y + src_x * 4;
159
160   for (y = 0; y < height; y++) {
161     guint32 *src = (guint32 *) src_data;
162
163     for (x = 0; x < width; x++) {
164       guint alpha = src[x] >> 24;
165
166       if (alpha == 0)
167         {
168           dest_data[x * 4 + 0] = 0;
169           dest_data[x * 4 + 1] = 0;
170           dest_data[x * 4 + 2] = 0;
171         }
172       else
173         {
174           dest_data[x * 4 + 0] = (((src[x] & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
175           dest_data[x * 4 + 1] = (((src[x] & 0x00ff00) >>  8) * 255 + alpha / 2) / alpha;
176           dest_data[x * 4 + 2] = (((src[x] & 0x0000ff) >>  0) * 255 + alpha / 2) / alpha;
177         }
178       dest_data[x * 4 + 3] = alpha;
179     }
180
181     src_data += src_stride;
182     dest_data += dest_stride;
183   }
184 }
185
186 static void
187 convert_no_alpha (guchar *dest_data,
188                   int     dest_stride,
189                   guchar *src_data,
190                   int     src_stride,
191                   int     src_x,
192                   int     src_y,
193                   int     width,
194                   int     height)
195 {
196   int x, y;
197
198   src_data += src_stride * src_y + src_x * 4;
199
200   for (y = 0; y < height; y++) {
201     guint32 *src = (guint32 *) src_data;
202
203     for (x = 0; x < width; x++) {
204       dest_data[x * 3 + 0] = src[x] >> 16;
205       dest_data[x * 3 + 1] = src[x] >>  8;
206       dest_data[x * 3 + 2] = src[x];
207     }
208
209     src_data += src_stride;
210     dest_data += dest_stride;
211   }
212 }
213
214 /**
215  * gdk_pixbuf_get_from_surface:
216  * @surface: surface to copy from
217  * @src_x: Source X coordinate within @surface
218  * @src_y: Source Y coordinate within @surface
219  * @width: Width in pixels of region to get
220  * @height: Height in pixels of region to get
221  *
222  * Transfers image data from a #cairo_surface_t and converts it to an RGB(A)
223  * representation inside a #GdkPixbuf. This allows you to efficiently read
224  * individual pixels from cairo surfaces. For #GdkWindows, use
225  * gdk_pixbuf_get_from_window() instead.
226  *
227  * This function will create an RGB pixbuf with 8 bits per channel.
228  * The pixbuf will contain an alpha channel if the @surface contains one.
229  *
230  * Return value: (transfer full): A newly-created pixbuf with a reference
231  *     count of 1, or %NULL on error
232  */
233 GdkPixbuf *
234 gdk_pixbuf_get_from_surface  (cairo_surface_t *surface,
235                               gint             src_x,
236                               gint             src_y,
237                               gint             width,
238                               gint             height)
239 {
240   cairo_content_t content;
241   GdkPixbuf *dest;
242
243   /* General sanity checks */
244   g_return_val_if_fail (surface != NULL, NULL);
245   g_return_val_if_fail (width > 0 && height > 0, NULL);
246
247   content = cairo_surface_get_content (surface) | CAIRO_CONTENT_COLOR;
248   dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
249                          !!(content & CAIRO_CONTENT_ALPHA),
250                          8,
251                          width, height);
252
253   surface = gdk_cairo_surface_coerce_to_image (surface, content,
254                                                src_x, src_y,
255                                                width, height);
256   cairo_surface_flush (surface);
257   if (cairo_surface_status (surface) || dest == NULL)
258     {
259       cairo_surface_destroy (surface);
260       return NULL;
261     }
262
263   if (gdk_pixbuf_get_has_alpha (dest))
264     convert_alpha (gdk_pixbuf_get_pixels (dest),
265                    gdk_pixbuf_get_rowstride (dest),
266                    cairo_image_surface_get_data (surface),
267                    cairo_image_surface_get_stride (surface),
268                    0, 0,
269                    width, height);
270   else
271     convert_no_alpha (gdk_pixbuf_get_pixels (dest),
272                       gdk_pixbuf_get_rowstride (dest),
273                       cairo_image_surface_get_data (surface),
274                       cairo_image_surface_get_stride (surface),
275                       0, 0,
276                       width, height);
277
278   cairo_surface_destroy (surface);
279   return dest;
280 }