2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /* -- JPEG module -- */
34 #define INPUT_BUF_SIZE 512
35 #define OUTPUT_BUF_SIZE 512
37 /* data source manager: adapted from jdatasrc.c */
39 struct jpeg_source_mgr pub;
42 unsigned char buffer[INPUT_BUF_SIZE];
46 /* data destination manager: adapted from jdatadst.c */
48 struct jpeg_destination_mgr pub;
51 unsigned char buffer[OUTPUT_BUF_SIZE];
54 static int check(struct img_io *io);
55 static int read(struct img_pixmap *img, struct img_io *io);
56 static int write(struct img_pixmap *img, struct img_io *io);
58 /* read source functions */
59 static void init_source(j_decompress_ptr jd);
60 static boolean fill_input_buffer(j_decompress_ptr jd);
61 static void skip_input_data(j_decompress_ptr jd, long num_bytes);
62 static void term_source(j_decompress_ptr jd);
64 /* write destination functions */
65 static void init_destination(j_compress_ptr jc);
66 static boolean empty_output_buffer(j_compress_ptr jc);
67 static void term_destination(j_compress_ptr jc);
69 int img_register_jpeg(void)
71 static struct ftype_module mod = {".jpg", check, read, write};
72 return img_register_module(&mod);
76 static int check(struct img_io *io)
78 unsigned char sig[10];
80 long pos = io->seek(0, SEEK_CUR, io->uptr);
82 if(io->read(sig, 10, io->uptr) < 10) {
83 io->seek(pos, SEEK_SET, io->uptr);
87 if(memcmp(sig, "\xff\xd8\xff\xe0", 4) != 0 && memcmp(sig, "\xff\xd8\xff\xe1", 4) != 0
88 && memcmp(sig, "\xff\xd8\xff\xdb", 4) != 0 && memcmp(sig + 6, "JFIF", 4) != 0) {
89 io->seek(pos, SEEK_SET, io->uptr);
92 io->seek(pos, SEEK_SET, io->uptr);
96 static int read(struct img_pixmap *img, struct img_io *io)
99 struct jpeg_decompress_struct cinfo;
100 struct jpeg_error_mgr jerr;
102 unsigned char **scanlines;
104 io->seek(0, SEEK_CUR, io->uptr);
106 cinfo.err = jpeg_std_error(&jerr); /* XXX change... */
107 jpeg_create_decompress(&cinfo);
109 src.pub.init_source = init_source;
110 src.pub.fill_input_buffer = fill_input_buffer;
111 src.pub.skip_input_data = skip_input_data;
112 src.pub.resync_to_restart = jpeg_resync_to_restart;
113 src.pub.term_source = term_source;
114 src.pub.next_input_byte = 0;
115 src.pub.bytes_in_buffer = 0;
117 cinfo.src = (struct jpeg_source_mgr*)&src;
119 jpeg_read_header(&cinfo, 1);
120 cinfo.out_color_space = JCS_RGB;
122 if(img_set_pixels(img, cinfo.image_width, cinfo.image_height, IMG_FMT_RGB24, 0) == -1) {
123 jpeg_destroy_decompress(&cinfo);
127 if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
128 jpeg_destroy_decompress(&cinfo);
131 scanlines[0] = img->pixels;
132 for(i=1; i<img->height; i++) {
133 scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
136 jpeg_start_decompress(&cinfo);
137 while(nlines < img->height) {
138 int res = jpeg_read_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
141 jpeg_finish_decompress(&cinfo);
142 jpeg_destroy_decompress(&cinfo);
148 static int write(struct img_pixmap *img, struct img_io *io)
151 struct jpeg_compress_struct cinfo;
152 struct jpeg_error_mgr jerr;
154 struct img_pixmap tmpimg;
155 unsigned char **scanlines;
159 if(img->fmt != IMG_FMT_RGB24) {
160 if(img_copy(&tmpimg, img) == -1) {
163 if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
164 img_destroy(&tmpimg);
170 if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
171 img_destroy(&tmpimg);
174 scanlines[0] = img->pixels;
175 for(i=1; i<img->height; i++) {
176 scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
179 cinfo.err = jpeg_std_error(&jerr); /* XXX */
180 jpeg_create_compress(&cinfo);
182 dest.pub.init_destination = init_destination;
183 dest.pub.empty_output_buffer = empty_output_buffer;
184 dest.pub.term_destination = term_destination;
186 cinfo.dest = (struct jpeg_destination_mgr*)&dest;
188 cinfo.image_width = img->width;
189 cinfo.image_height = img->height;
190 cinfo.input_components = 3;
191 cinfo.in_color_space = JCS_RGB;
193 jpeg_set_defaults(&cinfo);
195 jpeg_start_compress(&cinfo, 1);
196 while(nlines < img->height) {
197 int res = jpeg_write_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
200 jpeg_finish_compress(&cinfo);
201 jpeg_destroy_compress(&cinfo);
204 img_destroy(&tmpimg);
208 /* -- read source functions --
209 * the following functions are adapted from jdatasrc.c in jpeglib
211 static void init_source(j_decompress_ptr jd)
213 struct src_mgr *src = (struct src_mgr*)jd->src;
214 src->start_of_file = 1;
217 static boolean fill_input_buffer(j_decompress_ptr jd)
219 struct src_mgr *src = (struct src_mgr*)jd->src;
222 nbytes = src->io->read(src->buffer, INPUT_BUF_SIZE, src->io->uptr);
225 if(src->start_of_file) {
228 /* insert a fake EOI marker */
229 src->buffer[0] = 0xff;
230 src->buffer[1] = JPEG_EOI;
234 src->pub.next_input_byte = src->buffer;
235 src->pub.bytes_in_buffer = nbytes;
236 src->start_of_file = 0;
240 static void skip_input_data(j_decompress_ptr jd, long num_bytes)
242 struct src_mgr *src = (struct src_mgr*)jd->src;
245 while(num_bytes > (long)src->pub.bytes_in_buffer) {
246 num_bytes -= (long)src->pub.bytes_in_buffer;
247 fill_input_buffer(jd);
249 src->pub.next_input_byte += (size_t)num_bytes;
250 src->pub.bytes_in_buffer -= (size_t)num_bytes;
254 static void term_source(j_decompress_ptr jd)
256 /* nothing to see here, move along */
260 /* -- write destination functions --
261 * the following functions are adapted from jdatadst.c in jpeglib
263 static void init_destination(j_compress_ptr jc)
265 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
267 dest->pub.next_output_byte = dest->buffer;
268 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
271 static boolean empty_output_buffer(j_compress_ptr jc)
273 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
275 if(dest->io->write(dest->buffer, OUTPUT_BUF_SIZE, dest->io->uptr) != OUTPUT_BUF_SIZE) {
279 dest->pub.next_output_byte = dest->buffer;
280 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
284 static void term_destination(j_compress_ptr jc)
286 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
287 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
289 /* write any remaining data in the buffer */
291 dest->io->write(dest->buffer, datacount, dest->io->uptr);