Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
Kristian Høgsberg | 902865c | 2012-02-08 10:11:42 -0500 | [diff] [blame^] | 3 | * Copyright © 2012 Intel Corporation |
Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 6 | * documentation for any purpose is hereby granted without fee, provided that |
| 7 | * the above copyright notice appear in all copies and that both that copyright |
| 8 | * notice and this permission notice appear in supporting documentation, and |
| 9 | * that the name of the copyright holders not be used in advertising or |
| 10 | * publicity pertaining to distribution of the software without specific, |
| 11 | * written prior permission. The copyright holders make no representations |
| 12 | * about the suitability of this software for any purpose. It is provided "as |
| 13 | * is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 21 | * OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 24 | #include <stdint.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <stdio.h> |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 28 | #include <math.h> |
| 29 | #include <cairo.h> |
| 30 | #include "cairo-util.h" |
| 31 | |
Kristian Høgsberg | 27d3866 | 2011-10-20 13:11:12 -0400 | [diff] [blame] | 32 | #include <jpeglib.h> |
Kristian Høgsberg | d654876 | 2012-01-25 15:43:48 -0500 | [diff] [blame] | 33 | #include <png.h> |
Kristian Høgsberg | 27d3866 | 2011-10-20 13:11:12 -0400 | [diff] [blame] | 34 | |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 35 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
| 36 | |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 37 | void |
Benjamin Franzke | 47eb8f4 | 2011-10-07 09:08:56 +0200 | [diff] [blame] | 38 | surface_flush_device(cairo_surface_t *surface) |
| 39 | { |
| 40 | cairo_device_t *device; |
| 41 | |
| 42 | device = cairo_surface_get_device(surface); |
| 43 | if (device) |
| 44 | cairo_device_flush(device); |
| 45 | } |
| 46 | |
| 47 | void |
Kristian Høgsberg | 10bdd29 | 2008-11-08 23:27:27 -0500 | [diff] [blame] | 48 | blur_surface(cairo_surface_t *surface, int margin) |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 49 | { |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 50 | int32_t width, height, stride, x, y, z, w; |
| 51 | uint8_t *src, *dst; |
| 52 | uint32_t *s, *d, a, p; |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 53 | int i, j, k, size, half; |
Kristian Høgsberg | 49e868c | 2010-06-15 16:18:58 -0400 | [diff] [blame] | 54 | uint32_t kernel[49]; |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 55 | double f; |
| 56 | |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 57 | size = ARRAY_LENGTH(kernel); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 58 | width = cairo_image_surface_get_width(surface); |
| 59 | height = cairo_image_surface_get_height(surface); |
| 60 | stride = cairo_image_surface_get_stride(surface); |
| 61 | src = cairo_image_surface_get_data(surface); |
| 62 | |
Kristian Høgsberg | 5fc96ff | 2009-09-12 15:58:48 -0400 | [diff] [blame] | 63 | dst = malloc(height * stride); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 64 | |
| 65 | half = size / 2; |
Kristian Høgsberg | 0cd8f6e | 2011-01-21 22:19:40 -0500 | [diff] [blame] | 66 | a = 0; |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 67 | for (i = 0; i < size; i++) { |
| 68 | f = (i - half); |
Kristian Høgsberg | 49e868c | 2010-06-15 16:18:58 -0400 | [diff] [blame] | 69 | kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000; |
Kristian Høgsberg | 0cd8f6e | 2011-01-21 22:19:40 -0500 | [diff] [blame] | 70 | a += kernel[i]; |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | for (i = 0; i < height; i++) { |
| 74 | s = (uint32_t *) (src + i * stride); |
| 75 | d = (uint32_t *) (dst + i * stride); |
| 76 | for (j = 0; j < width; j++) { |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 77 | if (margin < j && j < width - margin) { |
| 78 | d[j] = s[j]; |
Kristian Høgsberg | 10bdd29 | 2008-11-08 23:27:27 -0500 | [diff] [blame] | 79 | continue; |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 80 | } |
| 81 | |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 82 | x = 0; |
| 83 | y = 0; |
| 84 | z = 0; |
| 85 | w = 0; |
| 86 | for (k = 0; k < size; k++) { |
| 87 | if (j - half + k < 0 || j - half + k >= width) |
| 88 | continue; |
| 89 | p = s[j - half + k]; |
| 90 | |
| 91 | x += (p >> 24) * kernel[k]; |
| 92 | y += ((p >> 16) & 0xff) * kernel[k]; |
| 93 | z += ((p >> 8) & 0xff) * kernel[k]; |
| 94 | w += (p & 0xff) * kernel[k]; |
| 95 | } |
| 96 | d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | for (i = 0; i < height; i++) { |
| 101 | s = (uint32_t *) (dst + i * stride); |
| 102 | d = (uint32_t *) (src + i * stride); |
| 103 | for (j = 0; j < width; j++) { |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 104 | if (margin <= i && i < height - margin) { |
| 105 | d[j] = s[j]; |
Kristian Høgsberg | 10bdd29 | 2008-11-08 23:27:27 -0500 | [diff] [blame] | 106 | continue; |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 109 | x = 0; |
| 110 | y = 0; |
| 111 | z = 0; |
| 112 | w = 0; |
| 113 | for (k = 0; k < size; k++) { |
| 114 | if (i - half + k < 0 || i - half + k >= height) |
| 115 | continue; |
| 116 | s = (uint32_t *) (dst + (i - half + k) * stride); |
| 117 | p = s[j]; |
| 118 | |
| 119 | x += (p >> 24) * kernel[k]; |
| 120 | y += ((p >> 16) & 0xff) * kernel[k]; |
| 121 | z += ((p >> 8) & 0xff) * kernel[k]; |
| 122 | w += (p & 0xff) * kernel[k]; |
| 123 | } |
| 124 | d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a; |
| 125 | } |
| 126 | } |
| 127 | |
Kristian Høgsberg | 5fc96ff | 2009-09-12 15:58:48 -0400 | [diff] [blame] | 128 | free(dst); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 129 | cairo_surface_mark_dirty(surface); |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | tile_mask(cairo_t *cr, cairo_surface_t *surface, |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 134 | int x, int y, int width, int height, int margin, int top_margin) |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 135 | { |
| 136 | cairo_pattern_t *pattern; |
| 137 | cairo_matrix_t matrix; |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 138 | int i, fx, fy, vmargin; |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 139 | |
| 140 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 141 | pattern = cairo_pattern_create_for_surface (surface); |
| 142 | |
| 143 | for (i = 0; i < 4; i++) { |
| 144 | fx = i & 1; |
| 145 | fy = i >> 1; |
| 146 | |
| 147 | cairo_matrix_init_translate(&matrix, |
| 148 | -x + fx * (128 - width), |
| 149 | -y + fy * (128 - height)); |
| 150 | cairo_pattern_set_matrix(pattern, &matrix); |
| 151 | |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 152 | if (fy) |
| 153 | vmargin = margin; |
| 154 | else |
| 155 | vmargin = top_margin; |
| 156 | |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 157 | cairo_reset_clip(cr); |
| 158 | cairo_rectangle(cr, |
| 159 | x + fx * (width - margin), |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 160 | y + fy * (height - vmargin), |
| 161 | margin, vmargin); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 162 | cairo_clip (cr); |
| 163 | cairo_mask(cr, pattern); |
| 164 | } |
| 165 | |
| 166 | /* Top strecth */ |
| 167 | cairo_matrix_init_translate(&matrix, 64, 0); |
| 168 | cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1); |
| 169 | cairo_matrix_translate(&matrix, -x - width / 2, -y); |
| 170 | cairo_pattern_set_matrix(pattern, &matrix); |
| 171 | cairo_rectangle(cr, x + margin, y, width - 2 * margin, margin); |
| 172 | |
| 173 | cairo_reset_clip(cr); |
| 174 | cairo_rectangle(cr, |
| 175 | x + margin, |
| 176 | y, |
| 177 | width - 2 * margin, margin); |
| 178 | cairo_clip (cr); |
| 179 | cairo_mask(cr, pattern); |
| 180 | |
| 181 | /* Bottom strecth */ |
| 182 | cairo_matrix_translate(&matrix, 0, -height + 128); |
| 183 | cairo_pattern_set_matrix(pattern, &matrix); |
| 184 | |
| 185 | cairo_reset_clip(cr); |
| 186 | cairo_rectangle(cr, x + margin, y + height - margin, |
| 187 | width - 2 * margin, margin); |
| 188 | cairo_clip (cr); |
| 189 | cairo_mask(cr, pattern); |
| 190 | |
| 191 | /* Left strecth */ |
| 192 | cairo_matrix_init_translate(&matrix, 0, 64); |
| 193 | cairo_matrix_scale(&matrix, 1, 64.0 / (height - 2 * margin)); |
| 194 | cairo_matrix_translate(&matrix, -x, -y - height / 2); |
| 195 | cairo_pattern_set_matrix(pattern, &matrix); |
| 196 | cairo_reset_clip(cr); |
| 197 | cairo_rectangle(cr, x, y + margin, margin, height - 2 * margin); |
| 198 | cairo_clip (cr); |
| 199 | cairo_mask(cr, pattern); |
| 200 | |
| 201 | /* Right strecth */ |
| 202 | cairo_matrix_translate(&matrix, -width + 128, 0); |
| 203 | cairo_pattern_set_matrix(pattern, &matrix); |
| 204 | cairo_rectangle(cr, x + width - margin, y + margin, |
| 205 | margin, height - 2 * margin); |
| 206 | cairo_reset_clip(cr); |
| 207 | cairo_clip (cr); |
| 208 | cairo_mask(cr, pattern); |
| 209 | |
| 210 | cairo_pattern_destroy(pattern); |
| 211 | cairo_reset_clip(cr); |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | tile_source(cairo_t *cr, cairo_surface_t *surface, |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 216 | int x, int y, int width, int height, int margin, int top_margin) |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 217 | { |
| 218 | cairo_pattern_t *pattern; |
| 219 | cairo_matrix_t matrix; |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 220 | int i, fx, fy, vmargin; |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 221 | |
| 222 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 223 | pattern = cairo_pattern_create_for_surface (surface); |
| 224 | cairo_set_source(cr, pattern); |
| 225 | cairo_pattern_destroy(pattern); |
| 226 | |
| 227 | for (i = 0; i < 4; i++) { |
| 228 | fx = i & 1; |
| 229 | fy = i >> 1; |
| 230 | |
| 231 | cairo_matrix_init_translate(&matrix, |
| 232 | -x + fx * (128 - width), |
| 233 | -y + fy * (128 - height)); |
| 234 | cairo_pattern_set_matrix(pattern, &matrix); |
| 235 | |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 236 | if (fy) |
| 237 | vmargin = margin; |
| 238 | else |
| 239 | vmargin = top_margin; |
| 240 | |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 241 | cairo_rectangle(cr, |
| 242 | x + fx * (width - margin), |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 243 | y + fy * (height - vmargin), |
| 244 | margin, vmargin); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 245 | cairo_fill(cr); |
| 246 | } |
| 247 | |
| 248 | /* Top strecth */ |
| 249 | cairo_matrix_init_translate(&matrix, 64, 0); |
| 250 | cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1); |
| 251 | cairo_matrix_translate(&matrix, -x - width / 2, -y); |
| 252 | cairo_pattern_set_matrix(pattern, &matrix); |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 253 | cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 254 | cairo_fill(cr); |
| 255 | |
| 256 | /* Bottom strecth */ |
| 257 | cairo_matrix_translate(&matrix, 0, -height + 128); |
| 258 | cairo_pattern_set_matrix(pattern, &matrix); |
| 259 | cairo_rectangle(cr, x + margin, y + height - margin, |
| 260 | width - 2 * margin, margin); |
| 261 | cairo_fill(cr); |
| 262 | |
| 263 | /* Left strecth */ |
| 264 | cairo_matrix_init_translate(&matrix, 0, 64); |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 265 | cairo_matrix_scale(&matrix, 1, 64.0 / (height - margin - top_margin)); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 266 | cairo_matrix_translate(&matrix, -x, -y - height / 2); |
| 267 | cairo_pattern_set_matrix(pattern, &matrix); |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 268 | cairo_rectangle(cr, x, y + top_margin, |
| 269 | margin, height - margin - top_margin); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 270 | cairo_fill(cr); |
| 271 | |
| 272 | /* Right strecth */ |
| 273 | cairo_matrix_translate(&matrix, -width + 128, 0); |
| 274 | cairo_pattern_set_matrix(pattern, &matrix); |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 275 | cairo_rectangle(cr, x + width - margin, y + top_margin, |
| 276 | margin, height - margin - top_margin); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 277 | cairo_fill(cr); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 278 | } |
Kristian Høgsberg | 1e164b9 | 2011-09-13 14:47:46 -0400 | [diff] [blame] | 279 | |
| 280 | void |
| 281 | rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius) |
| 282 | { |
| 283 | cairo_move_to(cr, x0, y0 + radius); |
| 284 | cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2); |
| 285 | cairo_line_to(cr, x1 - radius, y0); |
| 286 | cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI); |
| 287 | cairo_line_to(cr, x1, y1 - radius); |
| 288 | cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2); |
| 289 | cairo_line_to(cr, x0 + radius, y1); |
| 290 | cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI); |
| 291 | cairo_close_path(cr); |
| 292 | } |
Kristian Høgsberg | 27d3866 | 2011-10-20 13:11:12 -0400 | [diff] [blame] | 293 | |
Kristian Høgsberg | 02453dd | 2011-11-22 14:40:00 -0500 | [diff] [blame] | 294 | static void |
| 295 | swizzle_row(JSAMPLE *row, JDIMENSION width) |
| 296 | { |
| 297 | JSAMPLE *s; |
| 298 | uint32_t *d; |
| 299 | |
| 300 | s = row + (width - 1) * 3; |
| 301 | d = (uint32_t *) (row + (width - 1) * 4); |
| 302 | while (s >= row) { |
| 303 | *d = 0xff000000 | (s[0] << 16) | (s[1] << 8) | (s[2] << 0); |
| 304 | s -= 3; |
| 305 | d--; |
| 306 | } |
| 307 | } |
| 308 | |
Kristian Høgsberg | ffe3bba | 2012-01-25 14:54:26 -0500 | [diff] [blame] | 309 | static void |
| 310 | error_exit(j_common_ptr cinfo) |
| 311 | { |
| 312 | longjmp(cinfo->client_data, 1); |
| 313 | } |
| 314 | |
Kristian Høgsberg | d654876 | 2012-01-25 15:43:48 -0500 | [diff] [blame] | 315 | static cairo_surface_t * |
| 316 | load_jpeg(FILE *fp) |
Kristian Høgsberg | 27d3866 | 2011-10-20 13:11:12 -0400 | [diff] [blame] | 317 | { |
| 318 | struct jpeg_decompress_struct cinfo; |
| 319 | struct jpeg_error_mgr jerr; |
Kristian Høgsberg | 02453dd | 2011-11-22 14:40:00 -0500 | [diff] [blame] | 320 | int stride, i, first; |
Kristian Høgsberg | 27d3866 | 2011-10-20 13:11:12 -0400 | [diff] [blame] | 321 | JSAMPLE *data, *rows[4]; |
Kristian Høgsberg | ffe3bba | 2012-01-25 14:54:26 -0500 | [diff] [blame] | 322 | jmp_buf env; |
Kristian Høgsberg | 27d3866 | 2011-10-20 13:11:12 -0400 | [diff] [blame] | 323 | |
| 324 | cinfo.err = jpeg_std_error(&jerr); |
Kristian Høgsberg | ffe3bba | 2012-01-25 14:54:26 -0500 | [diff] [blame] | 325 | jerr.error_exit = error_exit; |
| 326 | cinfo.client_data = env; |
| 327 | if (setjmp(env)) |
| 328 | return NULL; |
| 329 | |
Kristian Høgsberg | 27d3866 | 2011-10-20 13:11:12 -0400 | [diff] [blame] | 330 | jpeg_create_decompress(&cinfo); |
| 331 | |
Kristian Høgsberg | 27d3866 | 2011-10-20 13:11:12 -0400 | [diff] [blame] | 332 | jpeg_stdio_src(&cinfo, fp); |
| 333 | |
| 334 | jpeg_read_header(&cinfo, TRUE); |
| 335 | |
Kristian Høgsberg | 02453dd | 2011-11-22 14:40:00 -0500 | [diff] [blame] | 336 | cinfo.out_color_space = JCS_RGB; |
Kristian Høgsberg | 27d3866 | 2011-10-20 13:11:12 -0400 | [diff] [blame] | 337 | jpeg_start_decompress(&cinfo); |
| 338 | |
| 339 | stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, |
| 340 | cinfo.output_width); |
| 341 | data = malloc(stride * cinfo.output_height); |
| 342 | if (data == NULL) { |
| 343 | fprintf(stderr, "couldn't allocate image data\n"); |
| 344 | return NULL; |
| 345 | } |
| 346 | |
| 347 | while (cinfo.output_scanline < cinfo.output_height) { |
Kristian Høgsberg | 02453dd | 2011-11-22 14:40:00 -0500 | [diff] [blame] | 348 | first = cinfo.output_scanline; |
Kristian Høgsberg | bf8bd5a | 2011-10-20 14:44:48 -0400 | [diff] [blame] | 349 | for (i = 0; i < ARRAY_LENGTH(rows); i++) |
Kristian Høgsberg | 02453dd | 2011-11-22 14:40:00 -0500 | [diff] [blame] | 350 | rows[i] = data + (first + i) * stride; |
Kristian Høgsberg | 27d3866 | 2011-10-20 13:11:12 -0400 | [diff] [blame] | 351 | |
| 352 | jpeg_read_scanlines(&cinfo, rows, ARRAY_LENGTH(rows)); |
Kristian Høgsberg | 02453dd | 2011-11-22 14:40:00 -0500 | [diff] [blame] | 353 | for (i = 0; first + i < cinfo.output_scanline; i++) |
| 354 | swizzle_row(rows[i], cinfo.output_width); |
Kristian Høgsberg | 27d3866 | 2011-10-20 13:11:12 -0400 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | jpeg_finish_decompress(&cinfo); |
| 358 | |
Kristian Høgsberg | 27d3866 | 2011-10-20 13:11:12 -0400 | [diff] [blame] | 359 | jpeg_destroy_decompress(&cinfo); |
| 360 | |
| 361 | return cairo_image_surface_create_for_data (data, |
| 362 | CAIRO_FORMAT_RGB24, |
| 363 | cinfo.output_width, |
| 364 | cinfo.output_height, |
| 365 | stride); |
| 366 | } |
Kristian Høgsberg | d654876 | 2012-01-25 15:43:48 -0500 | [diff] [blame] | 367 | |
| 368 | static inline int |
| 369 | multiply_alpha(int alpha, int color) |
| 370 | { |
| 371 | int temp = (alpha * color) + 0x80; |
| 372 | |
| 373 | return ((temp + (temp >> 8)) >> 8); |
| 374 | } |
| 375 | |
| 376 | static void |
| 377 | premultiply_data(png_structp png, |
| 378 | png_row_infop row_info, |
| 379 | png_bytep data) |
| 380 | { |
| 381 | unsigned int i; |
| 382 | png_bytep p; |
| 383 | |
| 384 | for (i = 0, p = data; i < row_info->rowbytes; i += 4, p += 4) { |
| 385 | png_byte alpha = p[3]; |
| 386 | uint32_t w; |
| 387 | |
| 388 | if (alpha == 0) { |
| 389 | w = 0; |
| 390 | } else { |
| 391 | png_byte red = p[0]; |
| 392 | png_byte green = p[1]; |
| 393 | png_byte blue = p[2]; |
| 394 | |
| 395 | if (alpha != 0xff) { |
| 396 | red = multiply_alpha(alpha, red); |
| 397 | green = multiply_alpha(alpha, green); |
| 398 | blue = multiply_alpha(alpha, blue); |
| 399 | } |
| 400 | w = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0); |
| 401 | } |
| 402 | |
| 403 | * (uint32_t *) p = w; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | static void |
| 408 | read_func(png_structp png, png_bytep data, png_size_t size) |
| 409 | { |
| 410 | FILE *fp = png_get_io_ptr(png); |
| 411 | |
| 412 | if (fread(data, 1, size, fp) < 0) |
| 413 | png_error(png, NULL); |
| 414 | } |
| 415 | |
| 416 | static void |
| 417 | png_error_callback(png_structp png, png_const_charp error_msg) |
| 418 | { |
| 419 | longjmp (png_jmpbuf (png), 1); |
| 420 | } |
| 421 | |
| 422 | static cairo_surface_t * |
| 423 | load_png(FILE *fp) |
| 424 | { |
| 425 | png_struct *png; |
| 426 | png_info *info; |
| 427 | png_byte *data = NULL; |
| 428 | png_byte **row_pointers = NULL; |
| 429 | png_uint_32 width, height; |
| 430 | int depth, color_type, interlace, stride; |
| 431 | unsigned int i; |
| 432 | |
| 433 | png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, |
| 434 | png_error_callback, NULL); |
| 435 | if (!png) |
| 436 | return NULL; |
| 437 | |
| 438 | info = png_create_info_struct(png); |
| 439 | if (!info) { |
| 440 | png_destroy_read_struct(&png, &info, NULL); |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | if (setjmp(png_jmpbuf(png))) { |
| 445 | if (data) |
| 446 | free(data); |
| 447 | if (row_pointers) |
| 448 | free(row_pointers); |
| 449 | png_destroy_read_struct(&png, &info, NULL); |
| 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | png_set_read_fn(png, fp, read_func); |
| 454 | png_read_info(png, info); |
| 455 | png_get_IHDR(png, info, |
| 456 | &width, &height, &depth, |
| 457 | &color_type, &interlace, NULL, NULL); |
| 458 | |
| 459 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
| 460 | png_set_palette_to_rgb(png); |
| 461 | |
| 462 | if (color_type == PNG_COLOR_TYPE_GRAY) |
| 463 | png_set_expand_gray_1_2_4_to_8(png); |
| 464 | |
| 465 | if (png_get_valid(png, info, PNG_INFO_tRNS)) |
| 466 | png_set_tRNS_to_alpha(png); |
| 467 | |
| 468 | if (depth == 16) |
| 469 | png_set_strip_16(png); |
| 470 | |
| 471 | if (depth < 8) |
| 472 | png_set_packing(png); |
| 473 | |
| 474 | if (color_type == PNG_COLOR_TYPE_GRAY || |
| 475 | color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 476 | png_set_gray_to_rgb(png); |
| 477 | |
| 478 | if (interlace != PNG_INTERLACE_NONE) |
| 479 | png_set_interlace_handling(png); |
| 480 | |
| 481 | png_set_filler(png, 0xff, PNG_FILLER_AFTER); |
| 482 | png_set_read_user_transform_fn(png, premultiply_data); |
| 483 | png_read_update_info(png, info); |
| 484 | png_get_IHDR(png, info, |
| 485 | &width, &height, &depth, |
| 486 | &color_type, &interlace, NULL, NULL); |
| 487 | |
| 488 | |
| 489 | stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, width); |
| 490 | data = malloc(stride * height); |
| 491 | if (!data) { |
| 492 | png_destroy_read_struct(&png, &info, NULL); |
| 493 | return NULL; |
| 494 | } |
| 495 | |
| 496 | row_pointers = malloc(height * sizeof row_pointers[0]); |
| 497 | if (row_pointers == NULL) { |
| 498 | free(data); |
| 499 | png_destroy_read_struct(&png, &info, NULL); |
| 500 | return NULL; |
| 501 | } |
| 502 | |
| 503 | for (i = 0; i < height; i++) |
| 504 | row_pointers[i] = &data[i * stride]; |
| 505 | |
| 506 | png_read_image(png, row_pointers); |
| 507 | png_read_end(png, info); |
| 508 | |
| 509 | free(row_pointers); |
| 510 | png_destroy_read_struct(&png, &info, NULL); |
| 511 | |
| 512 | return cairo_image_surface_create_for_data (data, |
| 513 | CAIRO_FORMAT_RGB24, |
| 514 | width, height, stride); |
| 515 | } |
| 516 | |
| 517 | cairo_surface_t * |
| 518 | load_image(const char *filename) |
| 519 | { |
| 520 | cairo_surface_t *surface; |
| 521 | static const unsigned char png_header[] = { 0x89, 'P', 'N', 'G' }; |
Kristian Høgsberg | 902865c | 2012-02-08 10:11:42 -0500 | [diff] [blame^] | 522 | static const unsigned char jpeg_header[] = { 0xff, 0xd8 }; |
Kristian Høgsberg | d654876 | 2012-01-25 15:43:48 -0500 | [diff] [blame] | 523 | unsigned char header[4]; |
| 524 | FILE *fp; |
| 525 | |
| 526 | fp = fopen(filename, "rb"); |
| 527 | if (fp == NULL) |
| 528 | return NULL; |
| 529 | |
| 530 | fread(header, sizeof header, 1, fp); |
| 531 | rewind(fp); |
Kristian Høgsberg | 902865c | 2012-02-08 10:11:42 -0500 | [diff] [blame^] | 532 | if (memcmp(header, png_header, sizeof png_header) == 0) |
Kristian Høgsberg | d654876 | 2012-01-25 15:43:48 -0500 | [diff] [blame] | 533 | surface = load_png(fp); |
Kristian Høgsberg | 902865c | 2012-02-08 10:11:42 -0500 | [diff] [blame^] | 534 | else if (memcmp(header, jpeg_header, sizeof jpeg_header) == 0) |
Kristian Høgsberg | d654876 | 2012-01-25 15:43:48 -0500 | [diff] [blame] | 535 | surface = load_jpeg(fp); |
| 536 | else { |
| 537 | fprintf(stderr, "unrecognized file header for %s: " |
| 538 | "0x%02x 0x%02x 0x%02x 0x%02x\n", |
| 539 | filename, header[0], header[1], header[2], header[3]); |
| 540 | surface = NULL; |
| 541 | } |
| 542 | |
| 543 | fclose(fp); |
| 544 | |
| 545 | return surface; |
| 546 | } |