2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010-2017 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 -- */
36 #define INPUT_BUF_SIZE 512
37 #define OUTPUT_BUF_SIZE 512
39 /* data source manager: adapted from jdatasrc.c */
41 struct jpeg_source_mgr pub;
44 unsigned char buffer[INPUT_BUF_SIZE];
48 /* data destination manager: adapted from jdatadst.c */
50 struct jpeg_destination_mgr pub;
53 unsigned char buffer[OUTPUT_BUF_SIZE];
56 static int check(struct img_io *io);
57 static int read(struct img_pixmap *img, struct img_io *io);
58 static int write(struct img_pixmap *img, struct img_io *io);
60 /* read source functions */
61 static void init_source(j_decompress_ptr jd);
62 static boolean fill_input_buffer(j_decompress_ptr jd);
63 static void skip_input_data(j_decompress_ptr jd, long num_bytes);
64 static void term_source(j_decompress_ptr jd);
66 /* write destination functions */
67 static void init_destination(j_compress_ptr jc);
68 static boolean empty_output_buffer(j_compress_ptr jc);
69 static void term_destination(j_compress_ptr jc);
71 int img_register_jpeg(void)
73 static struct ftype_module mod = {".jpg:.jpeg", check, read, write};
74 return img_register_module(&mod);
78 static int check(struct img_io *io)
80 unsigned char sig[10];
82 long pos = io->seek(0, SEEK_CUR, io->uptr);
84 if(io->read(sig, 10, io->uptr) < 10) {
85 io->seek(pos, SEEK_SET, io->uptr);
89 if(memcmp(sig, "\xff\xd8\xff\xe0", 4) != 0 && memcmp(sig, "\xff\xd8\xff\xe1", 4) != 0
90 && memcmp(sig, "\xff\xd8\xff\xdb", 4) != 0 && memcmp(sig + 6, "JFIF", 4) != 0) {
91 io->seek(pos, SEEK_SET, io->uptr);
94 io->seek(pos, SEEK_SET, io->uptr);
98 static int read(struct img_pixmap *img, struct img_io *io)
101 struct jpeg_decompress_struct cinfo;
102 struct jpeg_error_mgr jerr;
104 unsigned char **scanlines;
106 io->seek(0, SEEK_CUR, io->uptr);
108 cinfo.err = jpeg_std_error(&jerr); /* XXX change... */
109 jpeg_create_decompress(&cinfo);
111 src.pub.init_source = init_source;
112 src.pub.fill_input_buffer = fill_input_buffer;
113 src.pub.skip_input_data = skip_input_data;
114 src.pub.resync_to_restart = jpeg_resync_to_restart;
115 src.pub.term_source = term_source;
116 src.pub.next_input_byte = 0;
117 src.pub.bytes_in_buffer = 0;
119 cinfo.src = (struct jpeg_source_mgr*)&src;
121 jpeg_read_header(&cinfo, 1);
122 cinfo.out_color_space = JCS_RGB;
124 if(img_set_pixels(img, cinfo.image_width, cinfo.image_height, IMG_FMT_RGB24, 0) == -1) {
125 jpeg_destroy_decompress(&cinfo);
129 if(!(scanlines = chk_malloc(img->height * sizeof *scanlines))) {
130 jpeg_destroy_decompress(&cinfo);
133 scanlines[0] = img->pixels;
134 for(i=1; i<img->height; i++) {
135 scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
138 jpeg_start_decompress(&cinfo);
139 while(nlines < img->height) {
140 int res = jpeg_read_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
143 jpeg_finish_decompress(&cinfo);
144 jpeg_destroy_decompress(&cinfo);
150 static int write(struct img_pixmap *img, struct img_io *io)
153 struct jpeg_compress_struct cinfo;
154 struct jpeg_error_mgr jerr;
156 struct img_pixmap tmpimg;
157 unsigned char **scanlines;
161 if(img->fmt != IMG_FMT_RGB24) {
162 if(img_copy(&tmpimg, img) == -1) {
165 if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
166 img_destroy(&tmpimg);
172 if(!(scanlines = chk_malloc(img->height * sizeof *scanlines))) {
173 img_destroy(&tmpimg);
176 scanlines[0] = img->pixels;
177 for(i=1; i<img->height; i++) {
178 scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
181 cinfo.err = jpeg_std_error(&jerr); /* XXX */
182 jpeg_create_compress(&cinfo);
184 dest.pub.init_destination = init_destination;
185 dest.pub.empty_output_buffer = empty_output_buffer;
186 dest.pub.term_destination = term_destination;
188 cinfo.dest = (struct jpeg_destination_mgr*)&dest;
190 cinfo.image_width = img->width;
191 cinfo.image_height = img->height;
192 cinfo.input_components = 3;
193 cinfo.in_color_space = JCS_RGB;
195 jpeg_set_defaults(&cinfo);
196 jpeg_set_quality(&cinfo, 95, 0);
198 jpeg_start_compress(&cinfo, 1);
199 while(nlines < img->height) {
200 int res = jpeg_write_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
203 jpeg_finish_compress(&cinfo);
204 jpeg_destroy_compress(&cinfo);
207 img_destroy(&tmpimg);
211 /* -- read source functions --
212 * the following functions are adapted from jdatasrc.c in jpeglib
214 static void init_source(j_decompress_ptr jd)
216 struct src_mgr *src = (struct src_mgr*)jd->src;
217 src->start_of_file = 1;
220 static boolean fill_input_buffer(j_decompress_ptr jd)
222 struct src_mgr *src = (struct src_mgr*)jd->src;
225 nbytes = src->io->read(src->buffer, INPUT_BUF_SIZE, src->io->uptr);
228 if(src->start_of_file) {
231 /* insert a fake EOI marker */
232 src->buffer[0] = 0xff;
233 src->buffer[1] = JPEG_EOI;
237 src->pub.next_input_byte = src->buffer;
238 src->pub.bytes_in_buffer = nbytes;
239 src->start_of_file = 0;
243 static void skip_input_data(j_decompress_ptr jd, long num_bytes)
245 struct src_mgr *src = (struct src_mgr*)jd->src;
248 while(num_bytes > (long)src->pub.bytes_in_buffer) {
249 num_bytes -= (long)src->pub.bytes_in_buffer;
250 fill_input_buffer(jd);
252 src->pub.next_input_byte += (size_t)num_bytes;
253 src->pub.bytes_in_buffer -= (size_t)num_bytes;
257 static void term_source(j_decompress_ptr jd)
259 /* nothing to see here, move along */
263 /* -- write destination functions --
264 * the following functions are adapted from jdatadst.c in jpeglib
266 static void init_destination(j_compress_ptr jc)
268 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
270 dest->pub.next_output_byte = dest->buffer;
271 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
274 static boolean empty_output_buffer(j_compress_ptr jc)
276 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
278 if(dest->io->write(dest->buffer, OUTPUT_BUF_SIZE, dest->io->uptr) != OUTPUT_BUF_SIZE) {
282 dest->pub.next_output_byte = dest->buffer;
283 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
287 static void term_destination(j_compress_ptr jc)
289 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
290 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
292 /* write any remaining data in the buffer */
294 dest->io->write(dest->buffer, datacount, dest->io->uptr);
300 /* build without JPEG support */
301 int img_register_jpeg(void)