dungeon_crawler

view prototype/imago2/file_jpeg.c @ 67:2560a7ab0243

internalized libanim, libimago2, and libpsys
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Oct 2012 02:04:00 +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 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 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, read, write};
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 read(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 io->seek(0, SEEK_CUR, io->uptr);
105 cinfo.err = jpeg_std_error(&jerr); /* XXX change... */
106 jpeg_create_decompress(&cinfo);
108 src.pub.init_source = init_source;
109 src.pub.fill_input_buffer = fill_input_buffer;
110 src.pub.skip_input_data = skip_input_data;
111 src.pub.resync_to_restart = jpeg_resync_to_restart;
112 src.pub.term_source = term_source;
113 src.pub.next_input_byte = 0;
114 src.pub.bytes_in_buffer = 0;
115 src.io = io;
116 cinfo.src = (struct jpeg_source_mgr*)&src;
118 jpeg_read_header(&cinfo, 1);
119 cinfo.out_color_space = JCS_RGB;
121 if(img_set_pixels(img, cinfo.image_width, cinfo.image_height, IMG_FMT_RGB24, 0) == -1) {
122 jpeg_destroy_decompress(&cinfo);
123 return -1;
124 }
126 if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
127 jpeg_destroy_decompress(&cinfo);
128 return -1;
129 }
130 scanlines[0] = img->pixels;
131 for(i=1; i<img->height; i++) {
132 scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
133 }
135 jpeg_start_decompress(&cinfo);
136 while(nlines < img->height) {
137 int res = jpeg_read_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
138 nlines += res;
139 }
140 jpeg_finish_decompress(&cinfo);
141 jpeg_destroy_decompress(&cinfo);
143 free(scanlines);
144 return 0;
145 }
147 static int write(struct img_pixmap *img, struct img_io *io)
148 {
149 int i, nlines = 0;
150 struct jpeg_compress_struct cinfo;
151 struct jpeg_error_mgr jerr;
152 struct dst_mgr dest;
153 struct img_pixmap tmpimg;
154 unsigned char **scanlines;
156 img_init(&tmpimg);
158 if(img->fmt != IMG_FMT_RGB24) {
159 if(img_copy(&tmpimg, img) == -1) {
160 return -1;
161 }
162 if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
163 img_destroy(&tmpimg);
164 return -1;
165 }
166 img = &tmpimg;
167 }
169 if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
170 img_destroy(&tmpimg);
171 return -1;
172 }
173 scanlines[0] = img->pixels;
174 for(i=1; i<img->height; i++) {
175 scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
176 }
178 cinfo.err = jpeg_std_error(&jerr); /* XXX */
179 jpeg_create_compress(&cinfo);
181 dest.pub.init_destination = init_destination;
182 dest.pub.empty_output_buffer = empty_output_buffer;
183 dest.pub.term_destination = term_destination;
184 dest.io = io;
185 cinfo.dest = (struct jpeg_destination_mgr*)&dest;
187 cinfo.image_width = img->width;
188 cinfo.image_height = img->height;
189 cinfo.input_components = 3;
190 cinfo.in_color_space = JCS_RGB;
192 jpeg_set_defaults(&cinfo);
194 jpeg_start_compress(&cinfo, 1);
195 while(nlines < img->height) {
196 int res = jpeg_write_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
197 nlines += res;
198 }
199 jpeg_finish_compress(&cinfo);
200 jpeg_destroy_compress(&cinfo);
202 free(scanlines);
203 img_destroy(&tmpimg);
204 return 0;
205 }
207 /* -- read source functions --
208 * the following functions are adapted from jdatasrc.c in jpeglib
209 */
210 static void init_source(j_decompress_ptr jd)
211 {
212 struct src_mgr *src = (struct src_mgr*)jd->src;
213 src->start_of_file = 1;
214 }
216 static int fill_input_buffer(j_decompress_ptr jd)
217 {
218 struct src_mgr *src = (struct src_mgr*)jd->src;
219 size_t nbytes;
221 nbytes = src->io->read(src->buffer, INPUT_BUF_SIZE, src->io->uptr);
223 if(nbytes <= 0) {
224 if(src->start_of_file) {
225 return 0;
226 }
227 /* insert a fake EOI marker */
228 src->buffer[0] = 0xff;
229 src->buffer[1] = JPEG_EOI;
230 nbytes = 2;
231 }
233 src->pub.next_input_byte = src->buffer;
234 src->pub.bytes_in_buffer = nbytes;
235 src->start_of_file = 0;
236 return 1;
237 }
239 static void skip_input_data(j_decompress_ptr jd, long num_bytes)
240 {
241 struct src_mgr *src = (struct src_mgr*)jd->src;
243 if(num_bytes > 0) {
244 while(num_bytes > (long)src->pub.bytes_in_buffer) {
245 num_bytes -= (long)src->pub.bytes_in_buffer;
246 fill_input_buffer(jd);
247 }
248 src->pub.next_input_byte += (size_t)num_bytes;
249 src->pub.bytes_in_buffer -= (size_t)num_bytes;
250 }
251 }
253 static void term_source(j_decompress_ptr jd)
254 {
255 /* nothing to see here, move along */
256 }
259 /* -- write destination functions --
260 * the following functions are adapted from jdatadst.c in jpeglib
261 */
262 static void init_destination(j_compress_ptr jc)
263 {
264 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
266 dest->pub.next_output_byte = dest->buffer;
267 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
268 }
270 static int empty_output_buffer(j_compress_ptr jc)
271 {
272 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
274 if(dest->io->write(dest->buffer, OUTPUT_BUF_SIZE, dest->io->uptr) != OUTPUT_BUF_SIZE) {
275 return 0;
276 }
278 dest->pub.next_output_byte = dest->buffer;
279 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
280 return 1;
281 }
283 static void term_destination(j_compress_ptr jc)
284 {
285 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
286 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
288 /* write any remaining data in the buffer */
289 if(datacount > 0) {
290 dest->io->write(dest->buffer, datacount, dest->io->uptr);
291 }
292 /* XXX flush? ... */
293 }