]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/blob - gdk_pixbuf_get_from_surface.h
io-svg.c: improve documentation; drop the unused size_func field.
[libsvgtiny-pixbuf.git] / gdk_pixbuf_get_from_surface.h
1 /* This file contains gdk_pixbuf_get_from_surface() and its
2 * dependencies, copy & pasted from gdk/deprecated/gdkpixbuf.c.
3 * Having it inline means that we don't have to link against a
4 * specific version of GTK, risking that (say) a gtk4-linked
5 * pixbuf loader gets used on a gtk3 desktop environment.
6 *
7 * Note: the gdk_pixbuf_get_from_surface() function has been
8 * made static.
9 */
10 static cairo_format_t
11 gdk_cairo_format_for_content (cairo_content_t content)
12 {
13 switch (content)
14 {
15 case CAIRO_CONTENT_COLOR:
16 return CAIRO_FORMAT_RGB24;
17 case CAIRO_CONTENT_ALPHA:
18 return CAIRO_FORMAT_A8;
19 case CAIRO_CONTENT_COLOR_ALPHA:
20 default:
21 return CAIRO_FORMAT_ARGB32;
22 }
23 }
24
25
26 static cairo_surface_t *
27 gdk_cairo_surface_coerce_to_image (cairo_surface_t *surface,
28 cairo_content_t content,
29 int src_x,
30 int src_y,
31 int width,
32 int height)
33 {
34 cairo_surface_t *copy;
35 cairo_t *cr;
36
37 copy = cairo_image_surface_create (gdk_cairo_format_for_content (content),
38 width,
39 height);
40
41 cr = cairo_create (copy);
42 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
43 cairo_set_source_surface (cr, surface, -src_x, -src_y);
44 cairo_paint (cr);
45 cairo_destroy (cr);
46
47 return copy;
48 }
49
50
51 static void
52 convert_alpha (guchar *dest_data,
53 int dest_stride,
54 guchar *src_data,
55 int src_stride,
56 int src_x,
57 int src_y,
58 int width,
59 int height)
60 {
61 int x, y;
62
63 src_data += src_stride * src_y + src_x * 4;
64
65 for (y = 0; y < height; y++) {
66 guint32 *src = (guint32 *) src_data;
67
68 for (x = 0; x < width; x++) {
69 guint alpha = src[x] >> 24;
70
71 if (alpha == 0)
72 {
73 dest_data[x * 4 + 0] = 0;
74 dest_data[x * 4 + 1] = 0;
75 dest_data[x * 4 + 2] = 0;
76 }
77 else
78 {
79 dest_data[x * 4 + 0] = (((src[x] & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
80 dest_data[x * 4 + 1] = (((src[x] & 0x00ff00) >> 8) * 255 + alpha / 2) / alpha;
81 dest_data[x * 4 + 2] = (((src[x] & 0x0000ff) >> 0) * 255 + alpha / 2) / alpha;
82 }
83 dest_data[x * 4 + 3] = alpha;
84 }
85
86 src_data += src_stride;
87 dest_data += dest_stride;
88 }
89 }
90
91 static void
92 convert_no_alpha (guchar *dest_data,
93 int dest_stride,
94 guchar *src_data,
95 int src_stride,
96 int src_x,
97 int src_y,
98 int width,
99 int height)
100 {
101 int x, y;
102
103 src_data += src_stride * src_y + src_x * 4;
104
105 for (y = 0; y < height; y++) {
106 guint32 *src = (guint32 *) src_data;
107
108 for (x = 0; x < width; x++) {
109 dest_data[x * 3 + 0] = src[x] >> 16;
110 dest_data[x * 3 + 1] = src[x] >> 8;
111 dest_data[x * 3 + 2] = src[x];
112 }
113
114 src_data += src_stride;
115 dest_data += dest_stride;
116 }
117 }
118
119 /**
120 * gdk_pixbuf_get_from_surface:
121 * @surface: surface to copy from
122 * @src_x: Source X coordinate within @surface
123 * @src_y: Source Y coordinate within @surface
124 * @width: Width in pixels of region to get
125 * @height: Height in pixels of region to get
126 *
127 * Transfers image data from a `cairo_surface_t` and converts it
128 * to a `GdkPixbuf`.
129 *
130 * This allows you to efficiently read individual pixels from cairo surfaces.
131 *
132 * This function will create an RGB pixbuf with 8 bits per channel.
133 * The pixbuf will contain an alpha channel if the @surface contains one.
134 *
135 * Returns: (nullable) (transfer full): A newly-created pixbuf with a
136 * reference count of 1
137 *
138 * Deprecated: 4.12: Use [class@Gdk.Texture] and subclasses instead
139 * cairo surfaces and pixbufs
140 */
141 static GdkPixbuf *
142 gdk_pixbuf_get_from_surface (cairo_surface_t *surface,
143 int src_x,
144 int src_y,
145 int width,
146 int height)
147 {
148 cairo_content_t content;
149 GdkPixbuf *dest;
150
151 /* General sanity checks */
152 g_return_val_if_fail (surface != NULL, NULL);
153 g_return_val_if_fail (width > 0 && height > 0, NULL);
154
155 content = cairo_surface_get_content (surface) | CAIRO_CONTENT_COLOR;
156 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
157 !!(content & CAIRO_CONTENT_ALPHA),
158 8,
159 width, height);
160
161 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE &&
162 cairo_image_surface_get_format (surface) == gdk_cairo_format_for_content (content))
163 surface = cairo_surface_reference (surface);
164 else
165 {
166 surface = gdk_cairo_surface_coerce_to_image (surface, content,
167 src_x, src_y,
168 width, height);
169 src_x = 0;
170 src_y = 0;
171 }
172 cairo_surface_flush (surface);
173 if (cairo_surface_status (surface) || dest == NULL)
174 {
175 cairo_surface_destroy (surface);
176 g_clear_object (&dest);
177 return NULL;
178 }
179
180 if (gdk_pixbuf_get_has_alpha (dest))
181 convert_alpha (gdk_pixbuf_get_pixels (dest),
182 gdk_pixbuf_get_rowstride (dest),
183 cairo_image_surface_get_data (surface),
184 cairo_image_surface_get_stride (surface),
185 src_x, src_y,
186 width, height);
187 else
188 convert_no_alpha (gdk_pixbuf_get_pixels (dest),
189 gdk_pixbuf_get_rowstride (dest),
190 cairo_image_surface_get_data (surface),
191 cairo_image_surface_get_stride (surface),
192 src_x, src_y,
193 width, height);
194
195 cairo_surface_destroy (surface);
196 return dest;
197 }