source: libpipi/trunk/pipi/codec/modular/jpeg.c @ 3083

Last change on this file since 3083 was 3083, checked in by Jean-Yves Lamoureux, 12 years ago
  • Added file extension detection to modular codec
File size: 4.8 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 "../modular.h"
21
22static int pipi_free_jpeg(pipi_image_t *img);
23
24
25static void format_msg(j_common_ptr cinfo, char *buf)
26{
27}
28static void emit_msg(j_common_ptr cinfo, int level)
29{
30}
31static void error_msg(j_common_ptr cinfo)
32{
33    cinfo->client_data = (void*)0x1;
34}
35static void output_msg(j_common_ptr cinfo)
36{
37}
38
39pipi_image_t *pipi_load_jpeg(const char *name)
40{
41    struct jpeg_decompress_struct cinfo;
42    struct jpeg_error_mgr jerr;
43    unsigned char *image = NULL, *scanline = NULL;
44    pipi_image_t *img = NULL;
45    unsigned int i, j, k = 0;
46
47    FILE *fp = fopen(name, "rb");
48    if(!fp) goto end;
49
50    /* Set callbacks */
51    cinfo.err = jpeg_std_error(&jerr);
52    jerr.error_exit = error_msg;
53    jerr.emit_message = emit_msg;
54    jerr.output_message = output_msg;
55    jerr.format_message = format_msg;
56
57    /* Initialize libjpeg */
58    jpeg_create_decompress(&cinfo);
59    cinfo.client_data = 0x0;
60    jpeg_stdio_src(&cinfo, fp);
61    if((int)cinfo.client_data == 0x1) {
62        goto end;
63    }
64    jpeg_read_header(&cinfo, TRUE);
65    if((int)cinfo.client_data == 0x1) {
66        goto end;
67    }
68    jpeg_start_decompress(&cinfo);
69    if((int)cinfo.client_data == 0x1) {
70        goto end;
71    }
72
73    /* One scanline */
74    image = malloc(cinfo.output_width * cinfo.output_height * 4);
75    if(!image) goto end;
76
77    scanline = malloc(cinfo.output_width * 3);
78
79
80    for(i=0; i < cinfo.output_height; i++)
81    {
82        jpeg_read_scanlines(&cinfo, &scanline, 1);
83        if((int)cinfo.client_data == 0x1) {
84            free(img);
85            img = NULL;
86            goto end;
87        }
88        for(j=0 ; j<cinfo.output_width*3; j+=3)
89        {
90            image[k+2] = scanline[j];
91            image[k+1] = scanline[j+1];
92            image[k]   = scanline[j+2];
93            image[k+3] = 255;
94            k+=4;
95        }
96    }
97
98    img = pipi_new(cinfo.output_width, cinfo.output_height);
99
100    img->p[PIPI_PIXELS_RGBA_C].pixels = image;
101    img->p[PIPI_PIXELS_RGBA_C].w = cinfo.output_width;
102    img->p[PIPI_PIXELS_RGBA_C].h = cinfo.output_height;
103    img->p[PIPI_PIXELS_RGBA_C].pitch = cinfo.output_width*4;
104    img->p[PIPI_PIXELS_RGBA_C].bpp = 24;
105    img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
106    img->last_modified = PIPI_PIXELS_RGBA_C;
107
108    img->codec_priv = (void *)&cinfo;
109    img->codec_format = PIPI_PIXELS_RGBA_C;
110    img->codec_free = pipi_free_jpeg;
111
112    img->wrap = 0;
113    img->u8 = 1;
114
115end:
116    if(fp) fclose(fp);
117    if(scanline) free(scanline);
118    jpeg_destroy_decompress(&cinfo);
119    return img;
120}
121
122int pipi_save_jpeg(pipi_image_t *img, const char *name)
123{
124    int quality = 75; /* FIXME */
125
126    struct jpeg_compress_struct cinfo;
127    struct jpeg_error_mgr jerr;
128    unsigned char *data = NULL;
129    unsigned char *line = NULL;
130
131    pipi_pixels_t *pixels = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
132
133    if (!pixels)
134        return 0;
135
136    data = pixels->pixels;
137
138    line = malloc(img->w * 3 * sizeof(unsigned char));
139    if (!line)
140        return 0;
141
142    FILE *fp = fopen(name, "wb");
143    if (!fp) {
144        free(line);
145        return 0;
146    }
147
148    jerr.error_exit = error_msg;
149    jerr.emit_message = emit_msg;
150    jerr.output_message = output_msg;
151
152    cinfo.err = jpeg_std_error(&(jerr));
153
154    jpeg_create_compress(&cinfo);
155    jpeg_stdio_dest(&cinfo, fp);
156    cinfo.image_width = img->w;
157    cinfo.image_height = img->h;
158    cinfo.input_components = 3;
159    cinfo.in_color_space = JCS_RGB;
160
161    jpeg_set_defaults(&cinfo);
162    jpeg_set_quality(&cinfo, quality, TRUE);
163    jpeg_start_compress(&cinfo, TRUE);
164
165    int j, i, y = 0;
166    uint32_t *ptr = (uint32_t*)data;
167    JSAMPROW           *jbuf;
168
169    while (cinfo.next_scanline < cinfo.image_height)
170    {
171        for (j = 0, i = 0; i < img->w; i++)
172        {
173            line[j++] = ((*ptr) >> 16) & 0xff;
174            line[j++] = ((*ptr) >> 8) & 0xff;
175            line[j++] = ((*ptr)) & 0xff;
176            ptr++;
177        }
178        jbuf = (JSAMPROW *) (&line);
179        jpeg_write_scanlines(&cinfo, jbuf, 1);
180        y++;
181    }
182
183    jpeg_finish_compress(&cinfo);
184    jpeg_destroy_compress(&cinfo);
185    free(line);
186    fclose(fp);
187
188    return 1;
189}
190
191static int pipi_free_jpeg(pipi_image_t *img)
192{
193    if(img->p[PIPI_PIXELS_RGBA_C].pixels)
194        free(img->p[PIPI_PIXELS_RGBA_C].pixels);
195    return 0;
196}
197
198
199
200
Note: See TracBrowser for help on using the repository browser.