blob: f477dfd70269950573ade0bb47cdb111a0b3e433 [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 <png.h>
34#include <pixman.h>
35
Jon Cruz35b2eaa2015-06-15 15:37:08 -070036#include "shared/helpers.h"
Kristian Høgsberg3c2360f2013-01-28 16:01:22 -050037#include "image-loader.h"
Kristian Høgsbergee4b4cb2012-04-11 09:43:53 -040038
Emmanuel Gil Peyrot66e16142016-02-16 01:57:52 +000039#ifdef HAVE_JPEG
40#include <jpeglib.h>
41#endif
42
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040043#ifdef HAVE_WEBP
44#include <webp/decode.h>
45#endif
46
47static int
48stride_for_width(int width)
49{
50 return width * 4;
51}
52
53static void
Emmanuel Gil Peyrot66e16142016-02-16 01:57:52 +000054pixman_image_destroy_func(pixman_image_t *image, void *data)
55{
56 free(data);
57}
58
59#ifdef HAVE_JPEG
60
61static void
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040062swizzle_row(JSAMPLE *row, JDIMENSION width)
63{
64 JSAMPLE *s;
65 uint32_t *d;
66
67 s = row + (width - 1) * 3;
68 d = (uint32_t *) (row + (width - 1) * 4);
69 while (s >= row) {
70 *d = 0xff000000 | (s[0] << 16) | (s[1] << 8) | (s[2] << 0);
71 s -= 3;
72 d--;
73 }
74}
75
76static void
77error_exit(j_common_ptr cinfo)
78{
79 longjmp(cinfo->client_data, 1);
80}
81
82static pixman_image_t *
83load_jpeg(FILE *fp)
84{
85 struct jpeg_decompress_struct cinfo;
86 struct jpeg_error_mgr jerr;
Kristian Høgsbergc234fb82012-07-11 15:45:59 -040087 pixman_image_t *pixman_image = NULL;
Kristian Høgsbergee4b4cb2012-04-11 09:43:53 -040088 unsigned int i;
89 int stride, first;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040090 JSAMPLE *data, *rows[4];
91 jmp_buf env;
92
93 cinfo.err = jpeg_std_error(&jerr);
94 jerr.error_exit = error_exit;
95 cinfo.client_data = env;
96 if (setjmp(env))
97 return NULL;
98
99 jpeg_create_decompress(&cinfo);
100
101 jpeg_stdio_src(&cinfo, fp);
102
103 jpeg_read_header(&cinfo, TRUE);
104
105 cinfo.out_color_space = JCS_RGB;
106 jpeg_start_decompress(&cinfo);
107
108 stride = cinfo.output_width * 4;
109 data = malloc(stride * cinfo.output_height);
110 if (data == NULL) {
111 fprintf(stderr, "couldn't allocate image data\n");
112 return NULL;
113 }
114
115 while (cinfo.output_scanline < cinfo.output_height) {
116 first = cinfo.output_scanline;
117 for (i = 0; i < ARRAY_LENGTH(rows); i++)
118 rows[i] = data + (first + i) * stride;
119
120 jpeg_read_scanlines(&cinfo, rows, ARRAY_LENGTH(rows));
121 for (i = 0; first + i < cinfo.output_scanline; i++)
122 swizzle_row(rows[i], cinfo.output_width);
123 }
124
125 jpeg_finish_decompress(&cinfo);
126
127 jpeg_destroy_decompress(&cinfo);
128
Rafal Mielniczuk9d4ddef2012-07-11 18:48:25 +0200129 pixman_image = pixman_image_create_bits(PIXMAN_a8r8g8b8,
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400130 cinfo.output_width,
131 cinfo.output_height,
132 (uint32_t *) data, stride);
Rafal Mielniczuk9d4ddef2012-07-11 18:48:25 +0200133
134 pixman_image_set_destroy_function(pixman_image,
135 pixman_image_destroy_func, data);
136
137 return pixman_image;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400138}
139
Emmanuel Gil Peyrot66e16142016-02-16 01:57:52 +0000140#else
141
142static pixman_image_t *
143load_jpeg(FILE *fp)
144{
145 fprintf(stderr, "JPEG support disabled at compile-time\n");
146 return NULL;
147}
148
149#endif
150
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400151static inline int
152multiply_alpha(int alpha, int color)
153{
154 int temp = (alpha * color) + 0x80;
155
156 return ((temp + (temp >> 8)) >> 8);
157}
158
159static void
160premultiply_data(png_structp png,
161 png_row_infop row_info,
162 png_bytep data)
163{
164 unsigned int i;
165 png_bytep p;
166
167 for (i = 0, p = data; i < row_info->rowbytes; i += 4, p += 4) {
168 png_byte alpha = p[3];
169 uint32_t w;
170
171 if (alpha == 0) {
172 w = 0;
173 } else {
174 png_byte red = p[0];
175 png_byte green = p[1];
176 png_byte blue = p[2];
177
178 if (alpha != 0xff) {
179 red = multiply_alpha(alpha, red);
180 green = multiply_alpha(alpha, green);
181 blue = multiply_alpha(alpha, blue);
182 }
183 w = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
184 }
185
186 * (uint32_t *) p = w;
187 }
188}
189
190static void
191read_func(png_structp png, png_bytep data, png_size_t size)
192{
193 FILE *fp = png_get_io_ptr(png);
194
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200195 if (fread(data, 1, size, fp) != size)
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400196 png_error(png, NULL);
197}
198
199static void
200png_error_callback(png_structp png, png_const_charp error_msg)
201{
202 longjmp (png_jmpbuf (png), 1);
203}
204
205static pixman_image_t *
206load_png(FILE *fp)
207{
208 png_struct *png;
209 png_info *info;
210 png_byte *data = NULL;
211 png_byte **row_pointers = NULL;
212 png_uint_32 width, height;
213 int depth, color_type, interlace, stride;
214 unsigned int i;
Rafal Mielniczuk9d4ddef2012-07-11 18:48:25 +0200215 pixman_image_t *pixman_image = NULL;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400216
217 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
218 png_error_callback, NULL);
219 if (!png)
220 return NULL;
221
222 info = png_create_info_struct(png);
223 if (!info) {
224 png_destroy_read_struct(&png, &info, NULL);
225 return NULL;
226 }
227
228 if (setjmp(png_jmpbuf(png))) {
229 if (data)
230 free(data);
231 if (row_pointers)
232 free(row_pointers);
233 png_destroy_read_struct(&png, &info, NULL);
234 return NULL;
235 }
236
237 png_set_read_fn(png, fp, read_func);
238 png_read_info(png, info);
239 png_get_IHDR(png, info,
240 &width, &height, &depth,
241 &color_type, &interlace, NULL, NULL);
242
243 if (color_type == PNG_COLOR_TYPE_PALETTE)
244 png_set_palette_to_rgb(png);
245
246 if (color_type == PNG_COLOR_TYPE_GRAY)
247 png_set_expand_gray_1_2_4_to_8(png);
248
249 if (png_get_valid(png, info, PNG_INFO_tRNS))
250 png_set_tRNS_to_alpha(png);
251
252 if (depth == 16)
253 png_set_strip_16(png);
254
255 if (depth < 8)
256 png_set_packing(png);
257
258 if (color_type == PNG_COLOR_TYPE_GRAY ||
259 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
260 png_set_gray_to_rgb(png);
261
262 if (interlace != PNG_INTERLACE_NONE)
263 png_set_interlace_handling(png);
264
265 png_set_filler(png, 0xff, PNG_FILLER_AFTER);
266 png_set_read_user_transform_fn(png, premultiply_data);
267 png_read_update_info(png, info);
268 png_get_IHDR(png, info,
269 &width, &height, &depth,
270 &color_type, &interlace, NULL, NULL);
271
272
273 stride = stride_for_width(width);
274 data = malloc(stride * height);
275 if (!data) {
276 png_destroy_read_struct(&png, &info, NULL);
277 return NULL;
278 }
279
280 row_pointers = malloc(height * sizeof row_pointers[0]);
281 if (row_pointers == NULL) {
282 free(data);
283 png_destroy_read_struct(&png, &info, NULL);
284 return NULL;
285 }
286
287 for (i = 0; i < height; i++)
288 row_pointers[i] = &data[i * stride];
289
290 png_read_image(png, row_pointers);
291 png_read_end(png, info);
292
293 free(row_pointers);
294 png_destroy_read_struct(&png, &info, NULL);
295
Rafal Mielniczuk9d4ddef2012-07-11 18:48:25 +0200296 pixman_image = pixman_image_create_bits(PIXMAN_a8r8g8b8,
297 width, height, (uint32_t *) data, stride);
298
299 pixman_image_set_destroy_function(pixman_image,
300 pixman_image_destroy_func, data);
301
302 return pixman_image;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400303}
304
305#ifdef HAVE_WEBP
306
307static pixman_image_t *
308load_webp(FILE *fp)
309{
310 WebPDecoderConfig config;
311 uint8_t buffer[16 * 1024];
312 int len;
313 VP8StatusCode status;
314 WebPIDecoder *idec;
315
316 if (!WebPInitDecoderConfig(&config)) {
317 fprintf(stderr, "Library version mismatch!\n");
318 return NULL;
319 }
320
321 /* webp decoding api doesn't seem to specify a min size that's
322 usable for GetFeatures, but 256 works... */
323 len = fread(buffer, 1, 256, fp);
324 status = WebPGetFeatures(buffer, len, &config.input);
325 if (status != VP8_STATUS_OK) {
326 fprintf(stderr, "failed to parse webp header\n");
327 WebPFreeDecBuffer(&config.output);
328 return NULL;
329 }
330
331 config.output.colorspace = MODE_BGRA;
332 config.output.u.RGBA.stride = stride_for_width(config.input.width);
333 config.output.u.RGBA.size =
334 config.output.u.RGBA.stride * config.input.height;
335 config.output.u.RGBA.rgba =
336 malloc(config.output.u.RGBA.stride * config.input.height);
337 config.output.is_external_memory = 1;
338 if (!config.output.u.RGBA.rgba) {
339 WebPFreeDecBuffer(&config.output);
340 return NULL;
341 }
342
343 rewind(fp);
344 idec = WebPINewDecoder(&config.output);
345 if (!idec) {
346 WebPFreeDecBuffer(&config.output);
347 return NULL;
348 }
349
350 while (!feof(fp)) {
351 len = fread(buffer, 1, sizeof buffer, fp);
352 status = WebPIAppend(idec, buffer, len);
353 if (status != VP8_STATUS_OK) {
354 fprintf(stderr, "webp decode status %d\n", status);
355 WebPIDelete(idec);
356 WebPFreeDecBuffer(&config.output);
357 return NULL;
358 }
359 }
360
361 WebPIDelete(idec);
362 WebPFreeDecBuffer(&config.output);
363
364 return pixman_image_create_bits(PIXMAN_a8r8g8b8,
365 config.input.width,
366 config.input.height,
367 (uint32_t *) config.output.u.RGBA.rgba,
368 config.output.u.RGBA.stride);
369}
370
Emmanuel Gil Peyrot58b7a152016-02-16 01:57:51 +0000371#else
372
373static pixman_image_t *
374load_webp(FILE *fp)
375{
376 fprintf(stderr, "WebP support disabled at compile-time\n");
377 return NULL;
378}
379
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400380#endif
381
382
383struct image_loader {
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400384 unsigned char header[4];
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400385 int header_size;
386 pixman_image_t *(*load)(FILE *fp);
387};
388
389static const struct image_loader loaders[] = {
390 { { 0x89, 'P', 'N', 'G' }, 4, load_png },
391 { { 0xff, 0xd8 }, 2, load_jpeg },
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400392 { { 'R', 'I', 'F', 'F' }, 4, load_webp }
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400393};
394
395pixman_image_t *
396load_image(const char *filename)
397{
398 pixman_image_t *image;
399 unsigned char header[4];
400 FILE *fp;
Kristian Høgsbergee4b4cb2012-04-11 09:43:53 -0400401 unsigned int i;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400402
Bill Spitzak28371f72014-08-19 18:13:10 -0700403 if (!filename || !*filename)
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400404 return NULL;
405
Bill Spitzak28371f72014-08-19 18:13:10 -0700406 fp = fopen(filename, "rb");
407 if (!fp) {
408 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
409 return NULL;
410 }
411
Martin Olsson712f5f42012-07-08 03:03:41 +0200412 if (fread(header, sizeof header, 1, fp) != 1) {
413 fclose(fp);
Bill Spitzak28371f72014-08-19 18:13:10 -0700414 fprintf(stderr, "%s: unable to read file header\n", filename);
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200415 return NULL;
Martin Olsson712f5f42012-07-08 03:03:41 +0200416 }
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200417
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400418 rewind(fp);
419 for (i = 0; i < ARRAY_LENGTH(loaders); i++) {
420 if (memcmp(header, loaders[i].header,
421 loaders[i].header_size) == 0) {
422 image = loaders[i].load(fp);
423 break;
424 }
425 }
426
427 fclose(fp);
428
429 if (i == ARRAY_LENGTH(loaders)) {
Bill Spitzak28371f72014-08-19 18:13:10 -0700430 fprintf(stderr, "%s: unrecognized file header "
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400431 "0x%02x 0x%02x 0x%02x 0x%02x\n",
432 filename, header[0], header[1], header[2], header[3]);
433 image = NULL;
Bill Spitzak28371f72014-08-19 18:13:10 -0700434 } else if (!image) {
435 /* load probably printed something, but just in case */
436 fprintf(stderr, "%s: error reading image\n", filename);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400437 }
438
439 return image;
440}