35dadd3d80d0b302d68ead96ce270fd21e16c60c
[profile/ivi/weston-ivi-shell.git] / shared / image-loader.c
1 /*
2  * Copyright © 2008-2012 Kristian Høgsberg
3  * Copyright © 2012 Intel Corporation
4  *
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
24 #include "config.h"
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <jpeglib.h>
30 #include <png.h>
31 #include <pixman.h>
32
33 #include "image-loader.h"
34
35 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
36
37 #ifdef HAVE_WEBP
38 #include <webp/decode.h>
39 #endif
40
41 static int
42 stride_for_width(int width)
43 {
44         return width * 4;
45 }
46
47 static void
48 swizzle_row(JSAMPLE *row, JDIMENSION width)
49 {
50         JSAMPLE *s;
51         uint32_t *d;
52
53         s = row + (width - 1) * 3;
54         d = (uint32_t *) (row + (width - 1) * 4);
55         while (s >= row) {
56                 *d = 0xff000000 | (s[0] << 16) | (s[1] << 8) | (s[2] << 0);
57                 s -= 3;
58                 d--;
59         }
60 }
61
62 static void
63 error_exit(j_common_ptr cinfo)
64 {
65         longjmp(cinfo->client_data, 1);
66 }
67
68 static void
69 pixman_image_destroy_func(pixman_image_t *image, void *data)
70 {
71         free(data);
72 }
73
74 static pixman_image_t *
75 load_jpeg(FILE *fp)
76 {
77         struct jpeg_decompress_struct cinfo;
78         struct jpeg_error_mgr jerr;
79         pixman_image_t *pixman_image = NULL;
80         unsigned int i;
81         int stride, first;
82         JSAMPLE *data, *rows[4];
83         jmp_buf env;
84
85         cinfo.err = jpeg_std_error(&jerr);
86         jerr.error_exit = error_exit;
87         cinfo.client_data = env;
88         if (setjmp(env))
89                 return NULL;
90
91         jpeg_create_decompress(&cinfo);
92
93         jpeg_stdio_src(&cinfo, fp);
94
95         jpeg_read_header(&cinfo, TRUE);
96
97         cinfo.out_color_space = JCS_RGB;
98         jpeg_start_decompress(&cinfo);
99
100         stride = cinfo.output_width * 4;
101         data = malloc(stride * cinfo.output_height);
102         if (data == NULL) {
103                 fprintf(stderr, "couldn't allocate image data\n");
104                 return NULL;
105         }
106
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;
111
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);
115         }
116
117         jpeg_finish_decompress(&cinfo);
118
119         jpeg_destroy_decompress(&cinfo);
120
121         pixman_image = pixman_image_create_bits(PIXMAN_a8r8g8b8,
122                                         cinfo.output_width,
123                                         cinfo.output_height,
124                                         (uint32_t *) data, stride);
125
126         pixman_image_set_destroy_function(pixman_image,
127                                 pixman_image_destroy_func, data);
128
129         return pixman_image;
130 }
131
132 static inline int
133 multiply_alpha(int alpha, int color)
134 {
135     int temp = (alpha * color) + 0x80;
136
137     return ((temp + (temp >> 8)) >> 8);
138 }
139
140 static void
141 premultiply_data(png_structp   png,
142                  png_row_infop row_info,
143                  png_bytep     data)
144 {
145     unsigned int i;
146     png_bytep p;
147
148     for (i = 0, p = data; i < row_info->rowbytes; i += 4, p += 4) {
149         png_byte  alpha = p[3];
150         uint32_t w;
151
152         if (alpha == 0) {
153                 w = 0;
154         } else {
155                 png_byte red   = p[0];
156                 png_byte green = p[1];
157                 png_byte blue  = p[2];
158
159                 if (alpha != 0xff) {
160                         red   = multiply_alpha(alpha, red);
161                         green = multiply_alpha(alpha, green);
162                         blue  = multiply_alpha(alpha, blue);
163                 }
164                 w = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
165         }
166
167         * (uint32_t *) p = w;
168     }
169 }
170
171 static void
172 read_func(png_structp png, png_bytep data, png_size_t size)
173 {
174         FILE *fp = png_get_io_ptr(png);
175
176         if (fread(data, 1, size, fp) != size)
177                 png_error(png, NULL);
178 }
179
180 static void
181 png_error_callback(png_structp png, png_const_charp error_msg)
182 {
183     longjmp (png_jmpbuf (png), 1);
184 }
185
186 static pixman_image_t *
187 load_png(FILE *fp)
188 {
189         png_struct *png;
190         png_info *info;
191         png_byte *data = NULL;
192         png_byte **row_pointers = NULL;
193         png_uint_32 width, height;
194         int depth, color_type, interlace, stride;
195         unsigned int i;
196         pixman_image_t *pixman_image = NULL;
197
198         png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
199                                      png_error_callback, NULL);
200         if (!png)
201                 return NULL;
202
203         info = png_create_info_struct(png);
204         if (!info) {
205                 png_destroy_read_struct(&png, &info, NULL);
206                 return NULL;
207         }
208
209         if (setjmp(png_jmpbuf(png))) {
210                 if (data)
211                         free(data);
212                 if (row_pointers)
213                         free(row_pointers);
214                 png_destroy_read_struct(&png, &info, NULL);
215                 return NULL;
216         }
217
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);
223
224         if (color_type == PNG_COLOR_TYPE_PALETTE)
225                 png_set_palette_to_rgb(png);
226
227         if (color_type == PNG_COLOR_TYPE_GRAY)
228                 png_set_expand_gray_1_2_4_to_8(png);
229
230         if (png_get_valid(png, info, PNG_INFO_tRNS))
231                 png_set_tRNS_to_alpha(png);
232
233         if (depth == 16)
234                 png_set_strip_16(png);
235
236         if (depth < 8)
237                 png_set_packing(png);
238
239         if (color_type == PNG_COLOR_TYPE_GRAY ||
240             color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
241                 png_set_gray_to_rgb(png);
242
243         if (interlace != PNG_INTERLACE_NONE)
244                 png_set_interlace_handling(png);
245
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);
252
253
254         stride = stride_for_width(width);
255         data = malloc(stride * height);
256         if (!data) {
257                 png_destroy_read_struct(&png, &info, NULL);
258                 return NULL;
259         }
260
261         row_pointers = malloc(height * sizeof row_pointers[0]);
262         if (row_pointers == NULL) {
263                 free(data);
264                 png_destroy_read_struct(&png, &info, NULL);
265                 return NULL;
266         }
267
268         for (i = 0; i < height; i++)
269                 row_pointers[i] = &data[i * stride];
270
271         png_read_image(png, row_pointers);
272         png_read_end(png, info);
273
274         free(row_pointers);
275         png_destroy_read_struct(&png, &info, NULL);
276
277         pixman_image = pixman_image_create_bits(PIXMAN_a8r8g8b8,
278                                 width, height, (uint32_t *) data, stride);
279
280         pixman_image_set_destroy_function(pixman_image,
281                                 pixman_image_destroy_func, data);
282
283         return pixman_image;
284 }
285
286 #ifdef HAVE_WEBP
287
288 static pixman_image_t *
289 load_webp(FILE *fp)
290 {
291         WebPDecoderConfig config;
292         uint8_t buffer[16 * 1024];
293         int len;
294         VP8StatusCode status;
295         WebPIDecoder *idec;
296
297         if (!WebPInitDecoderConfig(&config)) {
298                 fprintf(stderr, "Library version mismatch!\n");
299                 return NULL;
300         }
301
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);
309                 return NULL;
310         }
311
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);
321                 return NULL;
322         }
323
324         rewind(fp);
325         idec = WebPINewDecoder(&config.output);
326         if (!idec) {
327                 WebPFreeDecBuffer(&config.output);
328                 return NULL;
329         }
330
331         while (!feof(fp)) {
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);
336                         WebPIDelete(idec);
337                         WebPFreeDecBuffer(&config.output);
338                         return NULL;
339                 }
340         }
341
342         WebPIDelete(idec);
343         WebPFreeDecBuffer(&config.output);
344
345         return pixman_image_create_bits(PIXMAN_a8r8g8b8,
346                                         config.input.width,
347                                         config.input.height,
348                                         (uint32_t *) config.output.u.RGBA.rgba,
349                                         config.output.u.RGBA.stride);
350 }
351
352 #endif
353
354
355 struct image_loader {
356         unsigned char header[4];
357         int header_size;
358         pixman_image_t *(*load)(FILE *fp);
359 };
360
361 static const struct image_loader loaders[] = {
362         { { 0x89, 'P', 'N', 'G' }, 4, load_png },
363         { { 0xff, 0xd8 }, 2, load_jpeg },
364 #ifdef HAVE_WEBP
365         { { 'R', 'I', 'F', 'F' }, 4, load_webp }
366 #endif
367 };
368
369 pixman_image_t *
370 load_image(const char *filename)
371 {
372         pixman_image_t *image;
373         unsigned char header[4];
374         FILE *fp;
375         unsigned int i;
376
377         fp = fopen(filename, "rb");
378         if (fp == NULL)
379                 return NULL;
380
381         if (fread(header, sizeof header, 1, fp) != 1) {
382                 fclose(fp);
383                 return NULL;
384         }
385
386         rewind(fp);
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);
391                         break;
392                 }
393         }
394
395         fclose(fp);
396
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]);
401                 image = NULL;
402         }
403
404         return image;
405 }