blob: 050f06770fe259da05783edb91e2a7ba0a9d609d [file] [log] [blame]
Kristian Høgsbergf02a6492012-03-12 01:05:25 -04001/*
2 * Copyright © 2008-2012 Kristian Høgsberg
3 * Copyright © 2012 Intel Corporation
4 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040012 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040028
Bill Spitzak28371f72014-08-19 18:13:10 -070029#include <errno.h>
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040030#include <stdlib.h>
31#include <stdio.h>
Armin Keb3c73f2013-04-03 21:29:03 +020032#include <string.h>
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040033#include <jpeglib.h>
34#include <png.h>
35#include <pixman.h>
36
Jon Cruz35b2eaa2015-06-15 15:37:08 -070037#include "shared/helpers.h"
Kristian Høgsberg3c2360f2013-01-28 16:01:22 -050038#include "image-loader.h"
Kristian Høgsbergee4b4cb2012-04-11 09:43:53 -040039
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040040#ifdef HAVE_WEBP
41#include <webp/decode.h>
42#endif
43
44static int
45stride_for_width(int width)
46{
47 return width * 4;
48}
49
50static void
51swizzle_row(JSAMPLE *row, JDIMENSION width)
52{
53 JSAMPLE *s;
54 uint32_t *d;
55
56 s = row + (width - 1) * 3;
57 d = (uint32_t *) (row + (width - 1) * 4);
58 while (s >= row) {
59 *d = 0xff000000 | (s[0] << 16) | (s[1] << 8) | (s[2] << 0);
60 s -= 3;
61 d--;
62 }
63}
64
65static void
66error_exit(j_common_ptr cinfo)
67{
68 longjmp(cinfo->client_data, 1);
69}
70
Rafal Mielniczuk9d4ddef2012-07-11 18:48:25 +020071static void
72pixman_image_destroy_func(pixman_image_t *image, void *data)
73{
74 free(data);
75}
76
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040077static pixman_image_t *
78load_jpeg(FILE *fp)
79{
80 struct jpeg_decompress_struct cinfo;
81 struct jpeg_error_mgr jerr;
Kristian Høgsbergc234fb82012-07-11 15:45:59 -040082 pixman_image_t *pixman_image = NULL;
Kristian Høgsbergee4b4cb2012-04-11 09:43:53 -040083 unsigned int i;
84 int stride, first;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040085 JSAMPLE *data, *rows[4];
86 jmp_buf env;
87
88 cinfo.err = jpeg_std_error(&jerr);
89 jerr.error_exit = error_exit;
90 cinfo.client_data = env;
91 if (setjmp(env))
92 return NULL;
93
94 jpeg_create_decompress(&cinfo);
95
96 jpeg_stdio_src(&cinfo, fp);
97
98 jpeg_read_header(&cinfo, TRUE);
99
100 cinfo.out_color_space = JCS_RGB;
101 jpeg_start_decompress(&cinfo);
102
103 stride = cinfo.output_width * 4;
104 data = malloc(stride * cinfo.output_height);
105 if (data == NULL) {
106 fprintf(stderr, "couldn't allocate image data\n");
107 return NULL;
108 }
109
110 while (cinfo.output_scanline < cinfo.output_height) {
111 first = cinfo.output_scanline;
112 for (i = 0; i < ARRAY_LENGTH(rows); i++)
113 rows[i] = data + (first + i) * stride;
114
115 jpeg_read_scanlines(&cinfo, rows, ARRAY_LENGTH(rows));
116 for (i = 0; first + i < cinfo.output_scanline; i++)
117 swizzle_row(rows[i], cinfo.output_width);
118 }
119
120 jpeg_finish_decompress(&cinfo);
121
122 jpeg_destroy_decompress(&cinfo);
123
Rafal Mielniczuk9d4ddef2012-07-11 18:48:25 +0200124 pixman_image = pixman_image_create_bits(PIXMAN_a8r8g8b8,
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400125 cinfo.output_width,
126 cinfo.output_height,
127 (uint32_t *) data, stride);
Rafal Mielniczuk9d4ddef2012-07-11 18:48:25 +0200128
129 pixman_image_set_destroy_function(pixman_image,
130 pixman_image_destroy_func, data);
131
132 return pixman_image;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400133}
134
135static inline int
136multiply_alpha(int alpha, int color)
137{
138 int temp = (alpha * color) + 0x80;
139
140 return ((temp + (temp >> 8)) >> 8);
141}
142
143static void
144premultiply_data(png_structp png,
145 png_row_infop row_info,
146 png_bytep data)
147{
148 unsigned int i;
149 png_bytep p;
150
151 for (i = 0, p = data; i < row_info->rowbytes; i += 4, p += 4) {
152 png_byte alpha = p[3];
153 uint32_t w;
154
155 if (alpha == 0) {
156 w = 0;
157 } else {
158 png_byte red = p[0];
159 png_byte green = p[1];
160 png_byte blue = p[2];
161
162 if (alpha != 0xff) {
163 red = multiply_alpha(alpha, red);
164 green = multiply_alpha(alpha, green);
165 blue = multiply_alpha(alpha, blue);
166 }
167 w = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
168 }
169
170 * (uint32_t *) p = w;
171 }
172}
173
174static void
175read_func(png_structp png, png_bytep data, png_size_t size)
176{
177 FILE *fp = png_get_io_ptr(png);
178
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200179 if (fread(data, 1, size, fp) != size)
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400180 png_error(png, NULL);
181}
182
183static void
184png_error_callback(png_structp png, png_const_charp error_msg)
185{
186 longjmp (png_jmpbuf (png), 1);
187}
188
189static pixman_image_t *
190load_png(FILE *fp)
191{
192 png_struct *png;
193 png_info *info;
194 png_byte *data = NULL;
195 png_byte **row_pointers = NULL;
196 png_uint_32 width, height;
197 int depth, color_type, interlace, stride;
198 unsigned int i;
Rafal Mielniczuk9d4ddef2012-07-11 18:48:25 +0200199 pixman_image_t *pixman_image = NULL;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400200
201 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
202 png_error_callback, NULL);
203 if (!png)
204 return NULL;
205
206 info = png_create_info_struct(png);
207 if (!info) {
208 png_destroy_read_struct(&png, &info, NULL);
209 return NULL;
210 }
211
212 if (setjmp(png_jmpbuf(png))) {
213 if (data)
214 free(data);
215 if (row_pointers)
216 free(row_pointers);
217 png_destroy_read_struct(&png, &info, NULL);
218 return NULL;
219 }
220
221 png_set_read_fn(png, fp, read_func);
222 png_read_info(png, info);
223 png_get_IHDR(png, info,
224 &width, &height, &depth,
225 &color_type, &interlace, NULL, NULL);
226
227 if (color_type == PNG_COLOR_TYPE_PALETTE)
228 png_set_palette_to_rgb(png);
229
230 if (color_type == PNG_COLOR_TYPE_GRAY)
231 png_set_expand_gray_1_2_4_to_8(png);
232
233 if (png_get_valid(png, info, PNG_INFO_tRNS))
234 png_set_tRNS_to_alpha(png);
235
236 if (depth == 16)
237 png_set_strip_16(png);
238
239 if (depth < 8)
240 png_set_packing(png);
241
242 if (color_type == PNG_COLOR_TYPE_GRAY ||
243 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
244 png_set_gray_to_rgb(png);
245
246 if (interlace != PNG_INTERLACE_NONE)
247 png_set_interlace_handling(png);
248
249 png_set_filler(png, 0xff, PNG_FILLER_AFTER);
250 png_set_read_user_transform_fn(png, premultiply_data);
251 png_read_update_info(png, info);
252 png_get_IHDR(png, info,
253 &width, &height, &depth,
254 &color_type, &interlace, NULL, NULL);
255
256
257 stride = stride_for_width(width);
258 data = malloc(stride * height);
259 if (!data) {
260 png_destroy_read_struct(&png, &info, NULL);
261 return NULL;
262 }
263
264 row_pointers = malloc(height * sizeof row_pointers[0]);
265 if (row_pointers == NULL) {
266 free(data);
267 png_destroy_read_struct(&png, &info, NULL);
268 return NULL;
269 }
270
271 for (i = 0; i < height; i++)
272 row_pointers[i] = &data[i * stride];
273
274 png_read_image(png, row_pointers);
275 png_read_end(png, info);
276
277 free(row_pointers);
278 png_destroy_read_struct(&png, &info, NULL);
279
Rafal Mielniczuk9d4ddef2012-07-11 18:48:25 +0200280 pixman_image = pixman_image_create_bits(PIXMAN_a8r8g8b8,
281 width, height, (uint32_t *) data, stride);
282
283 pixman_image_set_destroy_function(pixman_image,
284 pixman_image_destroy_func, data);
285
286 return pixman_image;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400287}
288
289#ifdef HAVE_WEBP
290
291static pixman_image_t *
292load_webp(FILE *fp)
293{
294 WebPDecoderConfig config;
295 uint8_t buffer[16 * 1024];
296 int len;
297 VP8StatusCode status;
298 WebPIDecoder *idec;
299
300 if (!WebPInitDecoderConfig(&config)) {
301 fprintf(stderr, "Library version mismatch!\n");
302 return NULL;
303 }
304
305 /* webp decoding api doesn't seem to specify a min size that's
306 usable for GetFeatures, but 256 works... */
307 len = fread(buffer, 1, 256, fp);
308 status = WebPGetFeatures(buffer, len, &config.input);
309 if (status != VP8_STATUS_OK) {
310 fprintf(stderr, "failed to parse webp header\n");
311 WebPFreeDecBuffer(&config.output);
312 return NULL;
313 }
314
315 config.output.colorspace = MODE_BGRA;
316 config.output.u.RGBA.stride = stride_for_width(config.input.width);
317 config.output.u.RGBA.size =
318 config.output.u.RGBA.stride * config.input.height;
319 config.output.u.RGBA.rgba =
320 malloc(config.output.u.RGBA.stride * config.input.height);
321 config.output.is_external_memory = 1;
322 if (!config.output.u.RGBA.rgba) {
323 WebPFreeDecBuffer(&config.output);
324 return NULL;
325 }
326
327 rewind(fp);
328 idec = WebPINewDecoder(&config.output);
329 if (!idec) {
330 WebPFreeDecBuffer(&config.output);
331 return NULL;
332 }
333
334 while (!feof(fp)) {
335 len = fread(buffer, 1, sizeof buffer, fp);
336 status = WebPIAppend(idec, buffer, len);
337 if (status != VP8_STATUS_OK) {
338 fprintf(stderr, "webp decode status %d\n", status);
339 WebPIDelete(idec);
340 WebPFreeDecBuffer(&config.output);
341 return NULL;
342 }
343 }
344
345 WebPIDelete(idec);
346 WebPFreeDecBuffer(&config.output);
347
348 return pixman_image_create_bits(PIXMAN_a8r8g8b8,
349 config.input.width,
350 config.input.height,
351 (uint32_t *) config.output.u.RGBA.rgba,
352 config.output.u.RGBA.stride);
353}
354
Emmanuel Gil Peyrot58b7a152016-02-16 01:57:51 +0000355#else
356
357static pixman_image_t *
358load_webp(FILE *fp)
359{
360 fprintf(stderr, "WebP support disabled at compile-time\n");
361 return NULL;
362}
363
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400364#endif
365
366
367struct image_loader {
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400368 unsigned char header[4];
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400369 int header_size;
370 pixman_image_t *(*load)(FILE *fp);
371};
372
373static const struct image_loader loaders[] = {
374 { { 0x89, 'P', 'N', 'G' }, 4, load_png },
375 { { 0xff, 0xd8 }, 2, load_jpeg },
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400376 { { 'R', 'I', 'F', 'F' }, 4, load_webp }
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400377};
378
379pixman_image_t *
380load_image(const char *filename)
381{
382 pixman_image_t *image;
383 unsigned char header[4];
384 FILE *fp;
Kristian Høgsbergee4b4cb2012-04-11 09:43:53 -0400385 unsigned int i;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400386
Bill Spitzak28371f72014-08-19 18:13:10 -0700387 if (!filename || !*filename)
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400388 return NULL;
389
Bill Spitzak28371f72014-08-19 18:13:10 -0700390 fp = fopen(filename, "rb");
391 if (!fp) {
392 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
393 return NULL;
394 }
395
Martin Olsson712f5f42012-07-08 03:03:41 +0200396 if (fread(header, sizeof header, 1, fp) != 1) {
397 fclose(fp);
Bill Spitzak28371f72014-08-19 18:13:10 -0700398 fprintf(stderr, "%s: unable to read file header\n", filename);
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200399 return NULL;
Martin Olsson712f5f42012-07-08 03:03:41 +0200400 }
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200401
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400402 rewind(fp);
403 for (i = 0; i < ARRAY_LENGTH(loaders); i++) {
404 if (memcmp(header, loaders[i].header,
405 loaders[i].header_size) == 0) {
406 image = loaders[i].load(fp);
407 break;
408 }
409 }
410
411 fclose(fp);
412
413 if (i == ARRAY_LENGTH(loaders)) {
Bill Spitzak28371f72014-08-19 18:13:10 -0700414 fprintf(stderr, "%s: unrecognized file header "
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400415 "0x%02x 0x%02x 0x%02x 0x%02x\n",
416 filename, header[0], header[1], header[2], header[3]);
417 image = NULL;
Bill Spitzak28371f72014-08-19 18:13:10 -0700418 } else if (!image) {
419 /* load probably printed something, but just in case */
420 fprintf(stderr, "%s: error reading image\n", filename);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400421 }
422
423 return image;
424}