blob: 61de82fc5e91491143797bd5402a983be04578c1 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050023#include <stdint.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdio.h>
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050027#include <math.h>
Kristian Høgsbergffe3bba2012-01-25 14:54:26 -050028#include <setjmp.h>
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050029#include <cairo.h>
30#include "cairo-util.h"
31
Kristian Høgsberg27d38662011-10-20 13:11:12 -040032#include <jpeglib.h>
33
Kristian Høgsberg87330262008-11-17 22:23:55 -050034#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
35
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050036void
Benjamin Franzke47eb8f42011-10-07 09:08:56 +020037surface_flush_device(cairo_surface_t *surface)
38{
39 cairo_device_t *device;
40
41 device = cairo_surface_get_device(surface);
42 if (device)
43 cairo_device_flush(device);
44}
45
46void
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050047blur_surface(cairo_surface_t *surface, int margin)
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050048{
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050049 int32_t width, height, stride, x, y, z, w;
50 uint8_t *src, *dst;
51 uint32_t *s, *d, a, p;
Kristian Høgsberg87330262008-11-17 22:23:55 -050052 int i, j, k, size, half;
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040053 uint32_t kernel[49];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050054 double f;
55
Kristian Høgsberg87330262008-11-17 22:23:55 -050056 size = ARRAY_LENGTH(kernel);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050057 width = cairo_image_surface_get_width(surface);
58 height = cairo_image_surface_get_height(surface);
59 stride = cairo_image_surface_get_stride(surface);
60 src = cairo_image_surface_get_data(surface);
61
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -040062 dst = malloc(height * stride);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050063
64 half = size / 2;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050065 a = 0;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050066 for (i = 0; i < size; i++) {
67 f = (i - half);
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040068 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050069 a += kernel[i];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050070 }
71
72 for (i = 0; i < height; i++) {
73 s = (uint32_t *) (src + i * stride);
74 d = (uint32_t *) (dst + i * stride);
75 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -050076 if (margin < j && j < width - margin) {
77 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050078 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -050079 }
80
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050081 x = 0;
82 y = 0;
83 z = 0;
84 w = 0;
85 for (k = 0; k < size; k++) {
86 if (j - half + k < 0 || j - half + k >= width)
87 continue;
88 p = s[j - half + k];
89
90 x += (p >> 24) * kernel[k];
91 y += ((p >> 16) & 0xff) * kernel[k];
92 z += ((p >> 8) & 0xff) * kernel[k];
93 w += (p & 0xff) * kernel[k];
94 }
95 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
96 }
97 }
98
99 for (i = 0; i < height; i++) {
100 s = (uint32_t *) (dst + i * stride);
101 d = (uint32_t *) (src + i * stride);
102 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -0500103 if (margin <= i && i < height - margin) {
104 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -0500105 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -0500106 }
107
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500108 x = 0;
109 y = 0;
110 z = 0;
111 w = 0;
112 for (k = 0; k < size; k++) {
113 if (i - half + k < 0 || i - half + k >= height)
114 continue;
115 s = (uint32_t *) (dst + (i - half + k) * stride);
116 p = s[j];
117
118 x += (p >> 24) * kernel[k];
119 y += ((p >> 16) & 0xff) * kernel[k];
120 z += ((p >> 8) & 0xff) * kernel[k];
121 w += (p & 0xff) * kernel[k];
122 }
123 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
124 }
125 }
126
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -0400127 free(dst);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400128 cairo_surface_mark_dirty(surface);
129}
130
131void
132tile_mask(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400133 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400134{
135 cairo_pattern_t *pattern;
136 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400137 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400138
139 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
140 pattern = cairo_pattern_create_for_surface (surface);
141
142 for (i = 0; i < 4; i++) {
143 fx = i & 1;
144 fy = i >> 1;
145
146 cairo_matrix_init_translate(&matrix,
147 -x + fx * (128 - width),
148 -y + fy * (128 - height));
149 cairo_pattern_set_matrix(pattern, &matrix);
150
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400151 if (fy)
152 vmargin = margin;
153 else
154 vmargin = top_margin;
155
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400156 cairo_reset_clip(cr);
157 cairo_rectangle(cr,
158 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400159 y + fy * (height - vmargin),
160 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400161 cairo_clip (cr);
162 cairo_mask(cr, pattern);
163 }
164
165 /* Top strecth */
166 cairo_matrix_init_translate(&matrix, 64, 0);
167 cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1);
168 cairo_matrix_translate(&matrix, -x - width / 2, -y);
169 cairo_pattern_set_matrix(pattern, &matrix);
170 cairo_rectangle(cr, x + margin, y, width - 2 * margin, margin);
171
172 cairo_reset_clip(cr);
173 cairo_rectangle(cr,
174 x + margin,
175 y,
176 width - 2 * margin, margin);
177 cairo_clip (cr);
178 cairo_mask(cr, pattern);
179
180 /* Bottom strecth */
181 cairo_matrix_translate(&matrix, 0, -height + 128);
182 cairo_pattern_set_matrix(pattern, &matrix);
183
184 cairo_reset_clip(cr);
185 cairo_rectangle(cr, x + margin, y + height - margin,
186 width - 2 * margin, margin);
187 cairo_clip (cr);
188 cairo_mask(cr, pattern);
189
190 /* Left strecth */
191 cairo_matrix_init_translate(&matrix, 0, 64);
192 cairo_matrix_scale(&matrix, 1, 64.0 / (height - 2 * margin));
193 cairo_matrix_translate(&matrix, -x, -y - height / 2);
194 cairo_pattern_set_matrix(pattern, &matrix);
195 cairo_reset_clip(cr);
196 cairo_rectangle(cr, x, y + margin, margin, height - 2 * margin);
197 cairo_clip (cr);
198 cairo_mask(cr, pattern);
199
200 /* Right strecth */
201 cairo_matrix_translate(&matrix, -width + 128, 0);
202 cairo_pattern_set_matrix(pattern, &matrix);
203 cairo_rectangle(cr, x + width - margin, y + margin,
204 margin, height - 2 * margin);
205 cairo_reset_clip(cr);
206 cairo_clip (cr);
207 cairo_mask(cr, pattern);
208
209 cairo_pattern_destroy(pattern);
210 cairo_reset_clip(cr);
211}
212
213void
214tile_source(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400215 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400216{
217 cairo_pattern_t *pattern;
218 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400219 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400220
221 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
222 pattern = cairo_pattern_create_for_surface (surface);
223 cairo_set_source(cr, pattern);
224 cairo_pattern_destroy(pattern);
225
226 for (i = 0; i < 4; i++) {
227 fx = i & 1;
228 fy = i >> 1;
229
230 cairo_matrix_init_translate(&matrix,
231 -x + fx * (128 - width),
232 -y + fy * (128 - height));
233 cairo_pattern_set_matrix(pattern, &matrix);
234
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400235 if (fy)
236 vmargin = margin;
237 else
238 vmargin = top_margin;
239
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400240 cairo_rectangle(cr,
241 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400242 y + fy * (height - vmargin),
243 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400244 cairo_fill(cr);
245 }
246
247 /* Top strecth */
248 cairo_matrix_init_translate(&matrix, 64, 0);
249 cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1);
250 cairo_matrix_translate(&matrix, -x - width / 2, -y);
251 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400252 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400253 cairo_fill(cr);
254
255 /* Bottom strecth */
256 cairo_matrix_translate(&matrix, 0, -height + 128);
257 cairo_pattern_set_matrix(pattern, &matrix);
258 cairo_rectangle(cr, x + margin, y + height - margin,
259 width - 2 * margin, margin);
260 cairo_fill(cr);
261
262 /* Left strecth */
263 cairo_matrix_init_translate(&matrix, 0, 64);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400264 cairo_matrix_scale(&matrix, 1, 64.0 / (height - margin - top_margin));
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400265 cairo_matrix_translate(&matrix, -x, -y - height / 2);
266 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400267 cairo_rectangle(cr, x, y + top_margin,
268 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400269 cairo_fill(cr);
270
271 /* Right strecth */
272 cairo_matrix_translate(&matrix, -width + 128, 0);
273 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400274 cairo_rectangle(cr, x + width - margin, y + top_margin,
275 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400276 cairo_fill(cr);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500277}
Kristian Høgsberg1e164b92011-09-13 14:47:46 -0400278
279void
280rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
281{
282 cairo_move_to(cr, x0, y0 + radius);
283 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
284 cairo_line_to(cr, x1 - radius, y0);
285 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
286 cairo_line_to(cr, x1, y1 - radius);
287 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
288 cairo_line_to(cr, x0 + radius, y1);
289 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
290 cairo_close_path(cr);
291}
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400292
Kristian Høgsberg02453dd2011-11-22 14:40:00 -0500293static void
294swizzle_row(JSAMPLE *row, JDIMENSION width)
295{
296 JSAMPLE *s;
297 uint32_t *d;
298
299 s = row + (width - 1) * 3;
300 d = (uint32_t *) (row + (width - 1) * 4);
301 while (s >= row) {
302 *d = 0xff000000 | (s[0] << 16) | (s[1] << 8) | (s[2] << 0);
303 s -= 3;
304 d--;
305 }
306}
307
Kristian Høgsbergffe3bba2012-01-25 14:54:26 -0500308static void
309error_exit(j_common_ptr cinfo)
310{
311 longjmp(cinfo->client_data, 1);
312}
313
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400314cairo_surface_t *
315load_jpeg(const char *filename)
316{
317 struct jpeg_decompress_struct cinfo;
318 struct jpeg_error_mgr jerr;
319 FILE *fp;
Kristian Høgsberg02453dd2011-11-22 14:40:00 -0500320 int stride, i, first;
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400321 JSAMPLE *data, *rows[4];
Kristian Høgsbergffe3bba2012-01-25 14:54:26 -0500322 jmp_buf env;
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400323
324 cinfo.err = jpeg_std_error(&jerr);
Kristian Høgsbergffe3bba2012-01-25 14:54:26 -0500325 jerr.error_exit = error_exit;
326 cinfo.client_data = env;
327 if (setjmp(env))
328 return NULL;
329
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400330 jpeg_create_decompress(&cinfo);
331
332 fp = fopen(filename, "rb");
333 if (fp == NULL) {
334 fprintf(stderr, "can't open %s\n", filename);
335 return NULL;
336 }
337 jpeg_stdio_src(&cinfo, fp);
338
339 jpeg_read_header(&cinfo, TRUE);
340
Kristian Høgsberg02453dd2011-11-22 14:40:00 -0500341 cinfo.out_color_space = JCS_RGB;
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400342 jpeg_start_decompress(&cinfo);
343
344 stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24,
345 cinfo.output_width);
346 data = malloc(stride * cinfo.output_height);
347 if (data == NULL) {
348 fprintf(stderr, "couldn't allocate image data\n");
349 return NULL;
350 }
351
352 while (cinfo.output_scanline < cinfo.output_height) {
Kristian Høgsberg02453dd2011-11-22 14:40:00 -0500353 first = cinfo.output_scanline;
Kristian Høgsbergbf8bd5a2011-10-20 14:44:48 -0400354 for (i = 0; i < ARRAY_LENGTH(rows); i++)
Kristian Høgsberg02453dd2011-11-22 14:40:00 -0500355 rows[i] = data + (first + i) * stride;
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400356
357 jpeg_read_scanlines(&cinfo, rows, ARRAY_LENGTH(rows));
Kristian Høgsberg02453dd2011-11-22 14:40:00 -0500358 for (i = 0; first + i < cinfo.output_scanline; i++)
359 swizzle_row(rows[i], cinfo.output_width);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400360 }
361
362 jpeg_finish_decompress(&cinfo);
363
364 fclose(fp);
365
366 jpeg_destroy_decompress(&cinfo);
367
368 return cairo_image_surface_create_for_data (data,
369 CAIRO_FORMAT_RGB24,
370 cinfo.output_width,
371 cinfo.output_height,
372 stride);
373}