blob: 2f780a771396ca67107dca079d0a5f8b2a67616d [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Kristian Høgsberg902865c2012-02-08 10:11:42 -05003 * Copyright © 2012 Intel Corporation
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
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øgsberg2f2cfae2008-11-08 22:46:30 -050024#include <stdint.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdio.h>
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050028#include <math.h>
29#include <cairo.h>
30#include "cairo-util.h"
31
Kristian Høgsberg27d38662011-10-20 13:11:12 -040032#include <jpeglib.h>
Kristian Høgsbergd6548762012-01-25 15:43:48 -050033#include <png.h>
Kristian Høgsberg27d38662011-10-20 13:11:12 -040034
Kristian Høgsberg87330262008-11-17 22:23:55 -050035#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
36
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050037void
Benjamin Franzke47eb8f42011-10-07 09:08:56 +020038surface_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
47void
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050048blur_surface(cairo_surface_t *surface, int margin)
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050049{
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050050 int32_t width, height, stride, x, y, z, w;
51 uint8_t *src, *dst;
52 uint32_t *s, *d, a, p;
Kristian Høgsberg87330262008-11-17 22:23:55 -050053 int i, j, k, size, half;
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040054 uint32_t kernel[49];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050055 double f;
56
Kristian Høgsberg87330262008-11-17 22:23:55 -050057 size = ARRAY_LENGTH(kernel);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050058 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øgsberg5fc96ff2009-09-12 15:58:48 -040063 dst = malloc(height * stride);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050064
65 half = size / 2;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050066 a = 0;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050067 for (i = 0; i < size; i++) {
68 f = (i - half);
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040069 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050070 a += kernel[i];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050071 }
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øgsberg87330262008-11-17 22:23:55 -050077 if (margin < j && j < width - margin) {
78 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050079 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -050080 }
81
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050082 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øgsberg87330262008-11-17 22:23:55 -0500104 if (margin <= i && i < height - margin) {
105 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -0500106 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -0500107 }
108
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500109 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øgsberg5fc96ff2009-09-12 15:58:48 -0400128 free(dst);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400129 cairo_surface_mark_dirty(surface);
130}
131
132void
133tile_mask(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400134 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400135{
136 cairo_pattern_t *pattern;
137 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400138 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400139
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øgsberg9a686242010-08-18 15:28:04 -0400152 if (fy)
153 vmargin = margin;
154 else
155 vmargin = top_margin;
156
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400157 cairo_reset_clip(cr);
158 cairo_rectangle(cr,
159 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400160 y + fy * (height - vmargin),
161 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400162 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
214void
215tile_source(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400216 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400217{
218 cairo_pattern_t *pattern;
219 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400220 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400221
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øgsberg9a686242010-08-18 15:28:04 -0400236 if (fy)
237 vmargin = margin;
238 else
239 vmargin = top_margin;
240
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400241 cairo_rectangle(cr,
242 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400243 y + fy * (height - vmargin),
244 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400245 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øgsberg9a686242010-08-18 15:28:04 -0400253 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400254 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øgsberg9a686242010-08-18 15:28:04 -0400265 cairo_matrix_scale(&matrix, 1, 64.0 / (height - margin - top_margin));
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400266 cairo_matrix_translate(&matrix, -x, -y - height / 2);
267 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400268 cairo_rectangle(cr, x, y + top_margin,
269 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400270 cairo_fill(cr);
271
272 /* Right strecth */
273 cairo_matrix_translate(&matrix, -width + 128, 0);
274 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400275 cairo_rectangle(cr, x + width - margin, y + top_margin,
276 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400277 cairo_fill(cr);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500278}
Kristian Høgsberg1e164b92011-09-13 14:47:46 -0400279
280void
281rounded_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øgsberg27d38662011-10-20 13:11:12 -0400293
Kristian Høgsberg02453dd2011-11-22 14:40:00 -0500294static void
295swizzle_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øgsbergffe3bba2012-01-25 14:54:26 -0500309static void
310error_exit(j_common_ptr cinfo)
311{
312 longjmp(cinfo->client_data, 1);
313}
314
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500315static cairo_surface_t *
316load_jpeg(FILE *fp)
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400317{
318 struct jpeg_decompress_struct cinfo;
319 struct jpeg_error_mgr jerr;
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
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400332 jpeg_stdio_src(&cinfo, fp);
333
334 jpeg_read_header(&cinfo, TRUE);
335
Kristian Høgsberg02453dd2011-11-22 14:40:00 -0500336 cinfo.out_color_space = JCS_RGB;
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400337 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øgsberg02453dd2011-11-22 14:40:00 -0500348 first = cinfo.output_scanline;
Kristian Høgsbergbf8bd5a2011-10-20 14:44:48 -0400349 for (i = 0; i < ARRAY_LENGTH(rows); i++)
Kristian Høgsberg02453dd2011-11-22 14:40:00 -0500350 rows[i] = data + (first + i) * stride;
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400351
352 jpeg_read_scanlines(&cinfo, rows, ARRAY_LENGTH(rows));
Kristian Høgsberg02453dd2011-11-22 14:40:00 -0500353 for (i = 0; first + i < cinfo.output_scanline; i++)
354 swizzle_row(rows[i], cinfo.output_width);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400355 }
356
357 jpeg_finish_decompress(&cinfo);
358
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400359 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øgsbergd6548762012-01-25 15:43:48 -0500367
368static inline int
369multiply_alpha(int alpha, int color)
370{
371 int temp = (alpha * color) + 0x80;
372
373 return ((temp + (temp >> 8)) >> 8);
374}
375
376static void
377premultiply_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
407static void
408read_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
416static void
417png_error_callback(png_structp png, png_const_charp error_msg)
418{
419 longjmp (png_jmpbuf (png), 1);
420}
421
422static cairo_surface_t *
423load_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
517cairo_surface_t *
518load_image(const char *filename)
519{
520 cairo_surface_t *surface;
521 static const unsigned char png_header[] = { 0x89, 'P', 'N', 'G' };
Kristian Høgsberg902865c2012-02-08 10:11:42 -0500522 static const unsigned char jpeg_header[] = { 0xff, 0xd8 };
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500523 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øgsberg902865c2012-02-08 10:11:42 -0500532 if (memcmp(header, png_header, sizeof png_header) == 0)
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500533 surface = load_png(fp);
Kristian Høgsberg902865c2012-02-08 10:11:42 -0500534 else if (memcmp(header, jpeg_header, sizeof jpeg_header) == 0)
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500535 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}