istereo2
view libs/imago2/file_jpeg.c @ 13:ea928c313344
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 28 Sep 2015 19:04:50 +0300 |
parents | |
children |
line source
1 /*
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/>.
17 */
19 /* -- JPEG module -- */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #ifdef WIN32
26 #include <windows.h>
27 #define HAVE_BOOLEAN
28 #endif
30 #include <jpeglib.h>
31 #include "imago2.h"
32 #include "ftype_module.h"
34 #define INPUT_BUF_SIZE 512
35 #define OUTPUT_BUF_SIZE 512
37 /* data source manager: adapted from jdatasrc.c */
38 struct src_mgr {
39 struct jpeg_source_mgr pub;
41 struct img_io *io;
42 unsigned char buffer[INPUT_BUF_SIZE];
43 int start_of_file;
44 };
46 /* data destination manager: adapted from jdatadst.c */
47 struct dst_mgr {
48 struct jpeg_destination_mgr pub;
50 struct img_io *io;
51 unsigned char buffer[OUTPUT_BUF_SIZE];
52 };
54 static int check(struct img_io *io);
55 static int readjpeg(struct img_pixmap *img, struct img_io *io);
56 static int writejpeg(struct img_pixmap *img, struct img_io *io);
58 /* read source functions */
59 static void init_source(j_decompress_ptr jd);
60 static int 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 int empty_output_buffer(j_compress_ptr jc);
67 static void term_destination(j_compress_ptr jc);
69 int img_register_jpeg(void)
70 {
71 static struct ftype_module mod = {".jpg", check, readjpeg, writejpeg};
72 return img_register_module(&mod);
73 }
76 static int check(struct img_io *io)
77 {
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);
84 return -1;
85 }
87 if(memcmp(sig, "\xff\xd8\xff\xe0", 4) != 0 || memcmp(sig + 6, "JFIF", 4) != 0) {
88 io->seek(pos, SEEK_SET, io->uptr);
89 return -1;
90 }
91 io->seek(pos, SEEK_SET, io->uptr);
92 return 0;
93 }
95 static int readjpeg(struct img_pixmap *img, struct img_io *io)
96 {
97 int i, nlines = 0;
98 struct jpeg_decompress_struct cinfo;
99 struct jpeg_error_mgr jerr;
100 struct src_mgr src;
101 unsigned char **scanlines;
103 cinfo.err = jpeg_std_error(&jerr); /* XXX change... */
104 jpeg_create_decompress(&cinfo);
106 src.pub.init_source = init_source;
107 src.pub.fill_input_buffer = fill_input_buffer;
108 src.pub.skip_input_data = skip_input_data;
109 src.pub.resync_to_restart = jpeg_resync_to_restart;
110 src.pub.term_source = term_source;
111 src.pub.next_input_byte = 0;
112 src.pub.bytes_in_buffer = 0;
113 src.io = io;
114 cinfo.src = (struct jpeg_source_mgr*)&src;
116 jpeg_read_header(&cinfo, 1);
117 cinfo.out_color_space = JCS_RGB;
119 if(img_set_pixels(img, cinfo.image_width, cinfo.image_height, IMG_FMT_RGB24, 0) == -1) {
120 jpeg_destroy_decompress(&cinfo);
121 return -1;
122 }
124 if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
125 jpeg_destroy_decompress(&cinfo);
126 return -1;
127 }
128 scanlines[0] = img->pixels;
129 for(i=1; i<img->height; i++) {
130 scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
131 }
133 jpeg_start_decompress(&cinfo);
134 while(nlines < img->height) {
135 int res = jpeg_read_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
136 nlines += res;
137 }
138 jpeg_finish_decompress(&cinfo);
139 jpeg_destroy_decompress(&cinfo);
141 free(scanlines);
142 return 0;
143 }
145 static int writejpeg(struct img_pixmap *img, struct img_io *io)
146 {
147 int i, nlines = 0;
148 struct jpeg_compress_struct cinfo;
149 struct jpeg_error_mgr jerr;
150 struct dst_mgr dest;
151 struct img_pixmap tmpimg;
152 unsigned char **scanlines;
154 img_init(&tmpimg);
156 if(img->fmt != IMG_FMT_RGB24) {
157 if(img_copy(&tmpimg, img) == -1) {
158 return -1;
159 }
160 if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
161 img_destroy(&tmpimg);
162 return -1;
163 }
164 img = &tmpimg;
165 }
167 if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
168 img_destroy(&tmpimg);
169 return -1;
170 }
171 scanlines[0] = img->pixels;
172 for(i=1; i<img->height; i++) {
173 scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
174 }
176 cinfo.err = jpeg_std_error(&jerr); /* XXX */
177 jpeg_create_compress(&cinfo);
179 dest.pub.init_destination = init_destination;
180 dest.pub.empty_output_buffer = empty_output_buffer;
181 dest.pub.term_destination = term_destination;
182 dest.io = io;
183 cinfo.dest = (struct jpeg_destination_mgr*)&dest;
185 cinfo.image_width = img->width;
186 cinfo.image_height = img->height;
187 cinfo.input_components = 3;
188 cinfo.in_color_space = JCS_RGB;
190 jpeg_set_defaults(&cinfo);
192 jpeg_start_compress(&cinfo, 1);
193 while(nlines < img->height) {
194 int res = jpeg_write_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
195 nlines += res;
196 }
197 jpeg_finish_compress(&cinfo);
198 jpeg_destroy_compress(&cinfo);
200 free(scanlines);
201 img_destroy(&tmpimg);
202 return 0;
203 }
205 /* -- read source functions --
206 * the following functions are adapted from jdatasrc.c in jpeglib
207 */
208 static void init_source(j_decompress_ptr jd)
209 {
210 struct src_mgr *src = (struct src_mgr*)jd->src;
211 src->start_of_file = 1;
212 }
214 static int fill_input_buffer(j_decompress_ptr jd)
215 {
216 struct src_mgr *src = (struct src_mgr*)jd->src;
217 size_t nbytes;
219 nbytes = src->io->read(src->buffer, INPUT_BUF_SIZE, src->io->uptr);
221 if(nbytes <= 0) {
222 if(src->start_of_file) {
223 return 0;
224 }
225 /* insert a fake EOI marker */
226 src->buffer[0] = 0xff;
227 src->buffer[1] = JPEG_EOI;
228 nbytes = 2;
229 }
231 src->pub.next_input_byte = src->buffer;
232 src->pub.bytes_in_buffer = nbytes;
233 src->start_of_file = 0;
234 return 1;
235 }
237 static void skip_input_data(j_decompress_ptr jd, long num_bytes)
238 {
239 struct src_mgr *src = (struct src_mgr*)jd->src;
241 if(num_bytes > 0) {
242 while(num_bytes > (long)src->pub.bytes_in_buffer) {
243 num_bytes -= (long)src->pub.bytes_in_buffer;
244 fill_input_buffer(jd);
245 }
246 src->pub.next_input_byte += (size_t)num_bytes;
247 src->pub.bytes_in_buffer -= (size_t)num_bytes;
248 }
249 }
251 static void term_source(j_decompress_ptr jd)
252 {
253 /* nothing to see here, move along */
254 }
257 /* -- write destination functions --
258 * the following functions are adapted from jdatadst.c in jpeglib
259 */
260 static void init_destination(j_compress_ptr jc)
261 {
262 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
264 dest->pub.next_output_byte = dest->buffer;
265 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
266 }
268 static int empty_output_buffer(j_compress_ptr jc)
269 {
270 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
272 if(dest->io->write(dest->buffer, OUTPUT_BUF_SIZE, dest->io->uptr) != OUTPUT_BUF_SIZE) {
273 return 0;
274 }
276 dest->pub.next_output_byte = dest->buffer;
277 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
278 return 1;
279 }
281 static void term_destination(j_compress_ptr jc)
282 {
283 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
284 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
286 /* write any remaining data in the buffer */
287 if(datacount > 0) {
288 dest->io->write(dest->buffer, datacount, dest->io->uptr);
289 }
290 /* XXX flush? ... */
291 }