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"
32 #include "image-loader.h"
34 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
37 #include <webp/decode.h>
41 stride_for_width(int width)
47 swizzle_row(JSAMPLE *row, JDIMENSION width)
52 s = row + (width - 1) * 3;
53 d = (uint32_t *) (row + (width - 1) * 4);
55 *d = 0xff000000 | (s[0] << 16) | (s[1] << 8) | (s[2] << 0);
62 error_exit(j_common_ptr cinfo)
64 longjmp(cinfo->client_data, 1);
68 pixman_image_destroy_func(pixman_image_t *image, void *data)
73 static pixman_image_t *
76 struct jpeg_decompress_struct cinfo;
77 struct jpeg_error_mgr jerr;
78 pixman_image_t *pixman_image = NULL;
81 JSAMPLE *data, *rows[4];
84 cinfo.err = jpeg_std_error(&jerr);
85 jerr.error_exit = error_exit;
86 cinfo.client_data = env;
90 jpeg_create_decompress(&cinfo);
92 jpeg_stdio_src(&cinfo, fp);
94 jpeg_read_header(&cinfo, TRUE);
96 cinfo.out_color_space = JCS_RGB;
97 jpeg_start_decompress(&cinfo);
99 stride = cinfo.output_width * 4;
100 data = malloc(stride * cinfo.output_height);
102 fprintf(stderr, "couldn't allocate image data\n");
106 while (cinfo.output_scanline < cinfo.output_height) {
107 first = cinfo.output_scanline;
108 for (i = 0; i < ARRAY_LENGTH(rows); i++)
109 rows[i] = data + (first + i) * stride;
111 jpeg_read_scanlines(&cinfo, rows, ARRAY_LENGTH(rows));
112 for (i = 0; first + i < cinfo.output_scanline; i++)
113 swizzle_row(rows[i], cinfo.output_width);
116 jpeg_finish_decompress(&cinfo);
118 jpeg_destroy_decompress(&cinfo);
120 pixman_image = pixman_image_create_bits(PIXMAN_a8r8g8b8,
123 (uint32_t *) data, stride);
125 pixman_image_set_destroy_function(pixman_image,
126 pixman_image_destroy_func, data);
132 multiply_alpha(int alpha, int color)
134 int temp = (alpha * color) + 0x80;
136 return ((temp + (temp >> 8)) >> 8);
140 premultiply_data(png_structp png,
141 png_row_infop row_info,
147 for (i = 0, p = data; i < row_info->rowbytes; i += 4, p += 4) {
148 png_byte alpha = p[3];
155 png_byte green = p[1];
156 png_byte blue = p[2];
159 red = multiply_alpha(alpha, red);
160 green = multiply_alpha(alpha, green);
161 blue = multiply_alpha(alpha, blue);
163 w = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
166 * (uint32_t *) p = w;
171 read_func(png_structp png, png_bytep data, png_size_t size)
173 FILE *fp = png_get_io_ptr(png);
175 if (fread(data, 1, size, fp) != size)
176 png_error(png, NULL);
180 png_error_callback(png_structp png, png_const_charp error_msg)
182 longjmp (png_jmpbuf (png), 1);
185 static pixman_image_t *
190 png_byte *data = NULL;
191 png_byte **row_pointers = NULL;
192 png_uint_32 width, height;
193 int depth, color_type, interlace, stride;
195 pixman_image_t *pixman_image = NULL;
197 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
198 png_error_callback, NULL);
202 info = png_create_info_struct(png);
204 png_destroy_read_struct(&png, &info, NULL);
208 if (setjmp(png_jmpbuf(png))) {
213 png_destroy_read_struct(&png, &info, NULL);
217 png_set_read_fn(png, fp, read_func);
218 png_read_info(png, info);
219 png_get_IHDR(png, info,
220 &width, &height, &depth,
221 &color_type, &interlace, NULL, NULL);
223 if (color_type == PNG_COLOR_TYPE_PALETTE)
224 png_set_palette_to_rgb(png);
226 if (color_type == PNG_COLOR_TYPE_GRAY)
227 png_set_expand_gray_1_2_4_to_8(png);
229 if (png_get_valid(png, info, PNG_INFO_tRNS))
230 png_set_tRNS_to_alpha(png);
233 png_set_strip_16(png);
236 png_set_packing(png);
238 if (color_type == PNG_COLOR_TYPE_GRAY ||
239 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
240 png_set_gray_to_rgb(png);
242 if (interlace != PNG_INTERLACE_NONE)
243 png_set_interlace_handling(png);
245 png_set_filler(png, 0xff, PNG_FILLER_AFTER);
246 png_set_read_user_transform_fn(png, premultiply_data);
247 png_read_update_info(png, info);
248 png_get_IHDR(png, info,
249 &width, &height, &depth,
250 &color_type, &interlace, NULL, NULL);
253 stride = stride_for_width(width);
254 data = malloc(stride * height);
256 png_destroy_read_struct(&png, &info, NULL);
260 row_pointers = malloc(height * sizeof row_pointers[0]);
261 if (row_pointers == NULL) {
263 png_destroy_read_struct(&png, &info, NULL);
267 for (i = 0; i < height; i++)
268 row_pointers[i] = &data[i * stride];
270 png_read_image(png, row_pointers);
271 png_read_end(png, info);
274 png_destroy_read_struct(&png, &info, NULL);
276 pixman_image = pixman_image_create_bits(PIXMAN_a8r8g8b8,
277 width, height, (uint32_t *) data, stride);
279 pixman_image_set_destroy_function(pixman_image,
280 pixman_image_destroy_func, data);
287 static pixman_image_t *
290 WebPDecoderConfig config;
291 uint8_t buffer[16 * 1024];
293 VP8StatusCode status;
296 if (!WebPInitDecoderConfig(&config)) {
297 fprintf(stderr, "Library version mismatch!\n");
301 /* webp decoding api doesn't seem to specify a min size that's
302 usable for GetFeatures, but 256 works... */
303 len = fread(buffer, 1, 256, fp);
304 status = WebPGetFeatures(buffer, len, &config.input);
305 if (status != VP8_STATUS_OK) {
306 fprintf(stderr, "failed to parse webp header\n");
307 WebPFreeDecBuffer(&config.output);
311 config.output.colorspace = MODE_BGRA;
312 config.output.u.RGBA.stride = stride_for_width(config.input.width);
313 config.output.u.RGBA.size =
314 config.output.u.RGBA.stride * config.input.height;
315 config.output.u.RGBA.rgba =
316 malloc(config.output.u.RGBA.stride * config.input.height);
317 config.output.is_external_memory = 1;
318 if (!config.output.u.RGBA.rgba) {
319 WebPFreeDecBuffer(&config.output);
324 idec = WebPINewDecoder(&config.output);
326 WebPFreeDecBuffer(&config.output);
331 len = fread(buffer, 1, sizeof buffer, fp);
332 status = WebPIAppend(idec, buffer, len);
333 if (status != VP8_STATUS_OK) {
334 fprintf(stderr, "webp decode status %d\n", status);
336 WebPFreeDecBuffer(&config.output);
342 WebPFreeDecBuffer(&config.output);
344 return pixman_image_create_bits(PIXMAN_a8r8g8b8,
347 (uint32_t *) config.output.u.RGBA.rgba,
348 config.output.u.RGBA.stride);
354 struct image_loader {
355 unsigned char header[4];
357 pixman_image_t *(*load)(FILE *fp);
360 static const struct image_loader loaders[] = {
361 { { 0x89, 'P', 'N', 'G' }, 4, load_png },
362 { { 0xff, 0xd8 }, 2, load_jpeg },
364 { { 'R', 'I', 'F', 'F' }, 4, load_webp }
369 load_image(const char *filename)
371 pixman_image_t *image;
372 unsigned char header[4];
376 fp = fopen(filename, "rb");
380 if (fread(header, sizeof header, 1, fp) != 1) {
386 for (i = 0; i < ARRAY_LENGTH(loaders); i++) {
387 if (memcmp(header, loaders[i].header,
388 loaders[i].header_size) == 0) {
389 image = loaders[i].load(fp);
396 if (i == ARRAY_LENGTH(loaders)) {
397 fprintf(stderr, "unrecognized file header for %s: "
398 "0x%02x 0x%02x 0x%02x 0x%02x\n",
399 filename, header[0], header[1], header[2], header[3]);