2 * Copyright © 2008-2012 Kristian Høgsberg
3 * Copyright © 2012 Intel Corporation
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.
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
24 #include "../config.h"
33 #include "image-loader.h"
35 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
38 #include <webp/decode.h>
42 stride_for_width(int width)
48 swizzle_row(JSAMPLE *row, JDIMENSION width)
53 s = row + (width - 1) * 3;
54 d = (uint32_t *) (row + (width - 1) * 4);
56 *d = 0xff000000 | (s[0] << 16) | (s[1] << 8) | (s[2] << 0);
63 error_exit(j_common_ptr cinfo)
65 longjmp(cinfo->client_data, 1);
69 pixman_image_destroy_func(pixman_image_t *image, void *data)
74 static pixman_image_t *
77 struct jpeg_decompress_struct cinfo;
78 struct jpeg_error_mgr jerr;
79 pixman_image_t *pixman_image = NULL;
82 JSAMPLE *data, *rows[4];
85 cinfo.err = jpeg_std_error(&jerr);
86 jerr.error_exit = error_exit;
87 cinfo.client_data = env;
91 jpeg_create_decompress(&cinfo);
93 jpeg_stdio_src(&cinfo, fp);
95 jpeg_read_header(&cinfo, TRUE);
97 cinfo.out_color_space = JCS_RGB;
98 jpeg_start_decompress(&cinfo);
100 stride = cinfo.output_width * 4;
101 data = malloc(stride * cinfo.output_height);
103 fprintf(stderr, "couldn't allocate image data\n");
107 while (cinfo.output_scanline < cinfo.output_height) {
108 first = cinfo.output_scanline;
109 for (i = 0; i < ARRAY_LENGTH(rows); i++)
110 rows[i] = data + (first + i) * stride;
112 jpeg_read_scanlines(&cinfo, rows, ARRAY_LENGTH(rows));
113 for (i = 0; first + i < cinfo.output_scanline; i++)
114 swizzle_row(rows[i], cinfo.output_width);
117 jpeg_finish_decompress(&cinfo);
119 jpeg_destroy_decompress(&cinfo);
121 pixman_image = pixman_image_create_bits(PIXMAN_a8r8g8b8,
124 (uint32_t *) data, stride);
126 pixman_image_set_destroy_function(pixman_image,
127 pixman_image_destroy_func, data);
133 multiply_alpha(int alpha, int color)
135 int temp = (alpha * color) + 0x80;
137 return ((temp + (temp >> 8)) >> 8);
141 premultiply_data(png_structp png,
142 png_row_infop row_info,
148 for (i = 0, p = data; i < row_info->rowbytes; i += 4, p += 4) {
149 png_byte alpha = p[3];
156 png_byte green = p[1];
157 png_byte blue = p[2];
160 red = multiply_alpha(alpha, red);
161 green = multiply_alpha(alpha, green);
162 blue = multiply_alpha(alpha, blue);
164 w = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
167 * (uint32_t *) p = w;
172 read_func(png_structp png, png_bytep data, png_size_t size)
174 FILE *fp = png_get_io_ptr(png);
176 if (fread(data, 1, size, fp) != size)
177 png_error(png, NULL);
181 png_error_callback(png_structp png, png_const_charp error_msg)
183 longjmp (png_jmpbuf (png), 1);
186 static pixman_image_t *
191 png_byte *data = NULL;
192 png_byte **row_pointers = NULL;
193 png_uint_32 width, height;
194 int depth, color_type, interlace, stride;
196 pixman_image_t *pixman_image = NULL;
198 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
199 png_error_callback, NULL);
203 info = png_create_info_struct(png);
205 png_destroy_read_struct(&png, &info, NULL);
209 if (setjmp(png_jmpbuf(png))) {
214 png_destroy_read_struct(&png, &info, NULL);
218 png_set_read_fn(png, fp, read_func);
219 png_read_info(png, info);
220 png_get_IHDR(png, info,
221 &width, &height, &depth,
222 &color_type, &interlace, NULL, NULL);
224 if (color_type == PNG_COLOR_TYPE_PALETTE)
225 png_set_palette_to_rgb(png);
227 if (color_type == PNG_COLOR_TYPE_GRAY)
228 png_set_expand_gray_1_2_4_to_8(png);
230 if (png_get_valid(png, info, PNG_INFO_tRNS))
231 png_set_tRNS_to_alpha(png);
234 png_set_strip_16(png);
237 png_set_packing(png);
239 if (color_type == PNG_COLOR_TYPE_GRAY ||
240 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
241 png_set_gray_to_rgb(png);
243 if (interlace != PNG_INTERLACE_NONE)
244 png_set_interlace_handling(png);
246 png_set_filler(png, 0xff, PNG_FILLER_AFTER);
247 png_set_read_user_transform_fn(png, premultiply_data);
248 png_read_update_info(png, info);
249 png_get_IHDR(png, info,
250 &width, &height, &depth,
251 &color_type, &interlace, NULL, NULL);
254 stride = stride_for_width(width);
255 data = malloc(stride * height);
257 png_destroy_read_struct(&png, &info, NULL);
261 row_pointers = malloc(height * sizeof row_pointers[0]);
262 if (row_pointers == NULL) {
264 png_destroy_read_struct(&png, &info, NULL);
268 for (i = 0; i < height; i++)
269 row_pointers[i] = &data[i * stride];
271 png_read_image(png, row_pointers);
272 png_read_end(png, info);
275 png_destroy_read_struct(&png, &info, NULL);
277 pixman_image = pixman_image_create_bits(PIXMAN_a8r8g8b8,
278 width, height, (uint32_t *) data, stride);
280 pixman_image_set_destroy_function(pixman_image,
281 pixman_image_destroy_func, data);
288 static pixman_image_t *
291 WebPDecoderConfig config;
292 uint8_t buffer[16 * 1024];
294 VP8StatusCode status;
297 if (!WebPInitDecoderConfig(&config)) {
298 fprintf(stderr, "Library version mismatch!\n");
302 /* webp decoding api doesn't seem to specify a min size that's
303 usable for GetFeatures, but 256 works... */
304 len = fread(buffer, 1, 256, fp);
305 status = WebPGetFeatures(buffer, len, &config.input);
306 if (status != VP8_STATUS_OK) {
307 fprintf(stderr, "failed to parse webp header\n");
308 WebPFreeDecBuffer(&config.output);
312 config.output.colorspace = MODE_BGRA;
313 config.output.u.RGBA.stride = stride_for_width(config.input.width);
314 config.output.u.RGBA.size =
315 config.output.u.RGBA.stride * config.input.height;
316 config.output.u.RGBA.rgba =
317 malloc(config.output.u.RGBA.stride * config.input.height);
318 config.output.is_external_memory = 1;
319 if (!config.output.u.RGBA.rgba) {
320 WebPFreeDecBuffer(&config.output);
325 idec = WebPINewDecoder(&config.output);
327 WebPFreeDecBuffer(&config.output);
332 len = fread(buffer, 1, sizeof buffer, fp);
333 status = WebPIAppend(idec, buffer, len);
334 if (status != VP8_STATUS_OK) {
335 fprintf(stderr, "webp decode status %d\n", status);
337 WebPFreeDecBuffer(&config.output);
343 WebPFreeDecBuffer(&config.output);
345 return pixman_image_create_bits(PIXMAN_a8r8g8b8,
348 (uint32_t *) config.output.u.RGBA.rgba,
349 config.output.u.RGBA.stride);
355 struct image_loader {
356 unsigned char header[4];
358 pixman_image_t *(*load)(FILE *fp);
361 static const struct image_loader loaders[] = {
362 { { 0x89, 'P', 'N', 'G' }, 4, load_png },
363 { { 0xff, 0xd8 }, 2, load_jpeg },
365 { { 'R', 'I', 'F', 'F' }, 4, load_webp }
370 load_image(const char *filename)
372 pixman_image_t *image;
373 unsigned char header[4];
377 fp = fopen(filename, "rb");
381 if (fread(header, sizeof header, 1, fp) != 1) {
387 for (i = 0; i < ARRAY_LENGTH(loaders); i++) {
388 if (memcmp(header, loaders[i].header,
389 loaders[i].header_size) == 0) {
390 image = loaders[i].load(fp);
397 if (i == ARRAY_LENGTH(loaders)) {
398 fprintf(stderr, "unrecognized file header for %s: "
399 "0x%02x 0x%02x 0x%02x 0x%02x\n",
400 filename, header[0], header[1], header[2], header[3]);