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