source: libpipi/trunk/pipi/codec/jpeg.c @ 3401

Last change on this file since 3401 was 3401, checked in by Sam Hocevar, 12 years ago

Fix a crash in the JPEG loader when meeting with ENOENT.

File size: 5.9 KB
Line 
1/*
2 *  libpipi       Pathetic image processing interface library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                2008 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  $Id$
8 *
9 *  This library is free software. It comes without any warranty, to
10 *  the extent permitted by applicable law. You can redistribute it
11 *  and/or modify it under the terms of the Do What The Fuck You Want
12 *  To Public License, Version 2, as published by Sam Hocevar. See
13 *  http://sam.zoy.org/wtfpl/COPYING for more details.
14 */
15
16/*
17 * jpeg.c: libjpeg I/O functions
18 */
19
20#include "config.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <ctype.h>
26#include <setjmp.h>
27
28#include <jpeglib.h>
29
30#include "pipi.h"
31#include "pipi_internals.h"
32
33static int pipi_free_jpeg(pipi_image_t *img);
34
35struct my_error_mgr
36{
37    struct jpeg_error_mgr pub;
38    jmp_buf setjmp_buffer;
39};
40
41typedef struct my_error_mgr * my_error_ptr;
42
43static void format_msg(j_common_ptr cinfo, char *buf)
44{
45}
46
47static void emit_msg(j_common_ptr cinfo, int level)
48{
49}
50
51static void error_msg(j_common_ptr cinfo)
52{
53    my_error_ptr myerr = (my_error_ptr) cinfo->err;
54    cinfo->client_data = (void*)0x1;
55    longjmp(myerr->setjmp_buffer, 1);
56}
57
58static void output_msg(j_common_ptr cinfo)
59{
60}
61
62pipi_image_t *pipi_load_jpeg(const char *name)
63{
64    struct jpeg_decompress_struct cinfo;
65    struct my_error_mgr jerr;
66    unsigned char *image = NULL, *scanline = NULL;
67    pipi_image_t *img = NULL;
68    unsigned int i, j, k = 0;
69    FILE *fp;
70
71    if(setjmp(jerr.setjmp_buffer))
72        return NULL;
73
74    fp = fopen(name, "rb");
75    if(!fp)
76        return NULL;
77
78    cinfo.err = jpeg_std_error(&jerr.pub);
79    jerr.pub.error_exit = error_msg;
80    jerr.pub.emit_message = emit_msg;
81    jerr.pub.output_message = output_msg;
82    jerr.pub.format_message = format_msg;
83
84    /* Initialize libjpeg */
85    jpeg_create_decompress(&cinfo);
86    cinfo.client_data = 0x0;
87    jpeg_stdio_src(&cinfo, fp);
88    if(cinfo.client_data == (void *)0x1)
89        goto end;
90    jpeg_read_header(&cinfo, TRUE);
91    if(cinfo.client_data == (void *)0x1)
92        goto end;
93    jpeg_start_decompress(&cinfo);
94    if(cinfo.client_data == (void *)0x1)
95        goto end;
96
97    /* One scanline */
98    image = malloc(cinfo.output_width * cinfo.output_height * 4);
99    if(!image)
100        goto end;
101
102    scanline = malloc(cinfo.output_width * 3);
103
104    for(i = 0; i < cinfo.output_height; i++)
105    {
106        jpeg_read_scanlines(&cinfo, &scanline, 1);
107        if(cinfo.client_data == (void *)0x1)
108        {
109            free(img);
110            img = NULL;
111            goto end;
112        }
113        for(j = 0; j < cinfo.output_width * 3; j += 3)
114        {
115            image[k + 2] = scanline[j];
116            image[k + 1] = scanline[j + 1];
117            image[k]     = scanline[j + 2];
118            image[k + 3] = 255;
119            k += 4;
120        }
121    }
122
123    img = pipi_new(cinfo.output_width, cinfo.output_height);
124
125    img->p[PIPI_PIXELS_RGBA_U8].pixels = image;
126    img->p[PIPI_PIXELS_RGBA_U8].w = cinfo.output_width;
127    img->p[PIPI_PIXELS_RGBA_U8].h = cinfo.output_height;
128    img->p[PIPI_PIXELS_RGBA_U8].pitch = cinfo.output_width * 4;
129    img->p[PIPI_PIXELS_RGBA_U8].bpp = 24;
130    img->p[PIPI_PIXELS_RGBA_U8].bytes = 4 * img->w * img->h;
131    img->last_modified = PIPI_PIXELS_RGBA_U8;
132
133    img->codec_priv = (void *)&cinfo;
134    img->codec_format = PIPI_PIXELS_RGBA_U8;
135    img->codec_free = pipi_free_jpeg;
136
137    img->wrap = 0;
138    img->u8 = 1;
139
140end:
141    if(fp)
142        fclose(fp);
143    if(scanline)
144        free(scanline);
145    jpeg_destroy_decompress(&cinfo);
146    return img;
147}
148
149int pipi_save_jpeg(pipi_image_t *img, const char *name)
150{
151    struct jpeg_compress_struct cinfo;
152    struct my_error_mgr jerr;
153    unsigned char *data;
154    unsigned char *line;
155    pipi_pixels_t *pixels;
156    JSAMPROW *jbuf;
157    uint32_t *ptr;
158    FILE *fp;
159    int i, j, y = 0;
160    size_t len;
161
162    int quality = 75; /* FIXME */
163
164    len = strlen(name);
165    if(len < 4 || name[len - 4] != '.'
166        || toupper(name[len - 3]) != 'J'
167        || toupper(name[len - 2]) != 'P'
168        || toupper(name[len - 1]) != 'G')
169    {
170        if(len < 5 || name[len - 5] != '.'
171            || toupper(name[len - 4]) != 'J'
172            || toupper(name[len - 3]) != 'P'
173            || toupper(name[len - 2]) != 'E'
174            || toupper(name[len - 1]) != 'G')
175            return -1;
176    }
177
178    pixels = pipi_get_pixels(img, PIPI_PIXELS_RGBA_U8);
179    if(!pixels)
180        return -1;
181
182    data = pixels->pixels;
183
184    line = malloc(img->w * 3 * sizeof(unsigned char));
185    if(!line)
186        return -1;
187
188    fp = fopen(name, "wb");
189    if(!fp)
190    {
191        free(line);
192        return -1;
193    }
194
195    if(setjmp(jerr.setjmp_buffer))
196        goto end;
197
198    jerr.pub.error_exit = error_msg;
199    jerr.pub.emit_message = emit_msg;
200    jerr.pub.output_message = output_msg;
201
202    cinfo.err = jpeg_std_error(&(jerr.pub));
203
204    jpeg_create_compress(&cinfo);
205    jpeg_stdio_dest(&cinfo, fp);
206    cinfo.image_width = img->w;
207    cinfo.image_height = img->h;
208    cinfo.input_components = 3;
209    cinfo.in_color_space = JCS_RGB;
210
211    jpeg_set_defaults(&cinfo);
212    jpeg_set_quality(&cinfo, quality, TRUE);
213    jpeg_start_compress(&cinfo, TRUE);
214
215    ptr = (uint32_t*)data;
216
217    while(cinfo.next_scanline < cinfo.image_height)
218    {
219        for (j = 0, i = 0; i < img->w; i++)
220        {
221            line[j++] = ((*ptr) >> 16) & 0xff;
222            line[j++] = ((*ptr) >> 8) & 0xff;
223            line[j++] = ((*ptr)) & 0xff;
224            ptr++;
225        }
226        jbuf = (JSAMPROW *) (&line);
227        jpeg_write_scanlines(&cinfo, jbuf, 1);
228        y++;
229    }
230
231    jpeg_finish_compress(&cinfo);
232
233end:
234    jpeg_destroy_compress(&cinfo);
235    free(line);
236    fclose(fp);
237
238    pipi_release_pixels(img, pixels);
239
240    return 0;
241}
242
243static int pipi_free_jpeg(pipi_image_t *img)
244{
245    if(img->p[PIPI_PIXELS_RGBA_U8].pixels)
246        free(img->p[PIPI_PIXELS_RGBA_U8].pixels);
247    return 0;
248}
249
Note: See TracBrowser for help on using the repository browser.