check alloc
[dos_imgv] / imago / src / filejpeg.c
1 /*
2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010-2017 John Tsiombikas <nuclear@member.fsf.org>
4
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.
9
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.
14
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 */
18
19 /* -- JPEG module -- */
20 #ifndef NO_JPEG
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #ifdef WIN32
27 #include <windows.h>
28 #define HAVE_BOOLEAN
29 #endif
30
31 #include <jpeglib.h>
32 #include "imago2.h"
33 #include "ftmodule.h"
34 #include "chkalloc.h"
35
36 #define INPUT_BUF_SIZE  512
37 #define OUTPUT_BUF_SIZE 512
38
39 /* data source manager: adapted from jdatasrc.c */
40 struct src_mgr {
41         struct jpeg_source_mgr pub;
42
43         struct img_io *io;
44         unsigned char buffer[INPUT_BUF_SIZE];
45         int start_of_file;
46 };
47
48 /* data destination manager: adapted from jdatadst.c */
49 struct dst_mgr {
50         struct jpeg_destination_mgr pub;
51
52         struct img_io *io;
53         unsigned char buffer[OUTPUT_BUF_SIZE];
54 };
55
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);
59
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);
65
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);
70
71 int img_register_jpeg(void)
72 {
73         static struct ftype_module mod = {".jpg:.jpeg", check, read, write};
74         return img_register_module(&mod);
75 }
76
77
78 static int check(struct img_io *io)
79 {
80         unsigned char sig[10];
81
82         long pos = io->seek(0, SEEK_CUR, io->uptr);
83
84         if(io->read(sig, 10, io->uptr) < 10) {
85                 io->seek(pos, SEEK_SET, io->uptr);
86                 return -1;
87         }
88
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);
92                 return -1;
93         }
94         io->seek(pos, SEEK_SET, io->uptr);
95         return 0;
96 }
97
98 static int read(struct img_pixmap *img, struct img_io *io)
99 {
100         int i, nlines = 0;
101         struct jpeg_decompress_struct cinfo;
102         struct jpeg_error_mgr jerr;
103         struct src_mgr src;
104         unsigned char **scanlines;
105
106         io->seek(0, SEEK_CUR, io->uptr);
107
108         cinfo.err = jpeg_std_error(&jerr);      /* XXX change... */
109         jpeg_create_decompress(&cinfo);
110
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;
118         src.io = io;
119         cinfo.src = (struct jpeg_source_mgr*)&src;
120
121         jpeg_read_header(&cinfo, 1);
122         cinfo.out_color_space = JCS_RGB;
123
124         if(img_set_pixels(img, cinfo.image_width, cinfo.image_height, IMG_FMT_RGB24, 0) == -1) {
125                 jpeg_destroy_decompress(&cinfo);
126                 return -1;
127         }
128
129         if(!(scanlines = chk_malloc(img->height * sizeof *scanlines))) {
130                 jpeg_destroy_decompress(&cinfo);
131                 return -1;
132         }
133         scanlines[0] = img->pixels;
134         for(i=1; i<img->height; i++) {
135                 scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
136         }
137
138         jpeg_start_decompress(&cinfo);
139         while(nlines < img->height) {
140                 int res = jpeg_read_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
141                 nlines += res;
142         }
143         jpeg_finish_decompress(&cinfo);
144         jpeg_destroy_decompress(&cinfo);
145
146         chk_free(scanlines);
147         return 0;
148 }
149
150 static int write(struct img_pixmap *img, struct img_io *io)
151 {
152         int i, nlines = 0;
153         struct jpeg_compress_struct cinfo;
154         struct jpeg_error_mgr jerr;
155         struct dst_mgr dest;
156         struct img_pixmap tmpimg;
157         unsigned char **scanlines;
158
159         img_init(&tmpimg);
160
161         if(img->fmt != IMG_FMT_RGB24) {
162                 if(img_copy(&tmpimg, img) == -1) {
163                         return -1;
164                 }
165                 if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
166                         img_destroy(&tmpimg);
167                         return -1;
168                 }
169                 img = &tmpimg;
170         }
171
172         if(!(scanlines = chk_malloc(img->height * sizeof *scanlines))) {
173                 img_destroy(&tmpimg);
174                 return -1;
175         }
176         scanlines[0] = img->pixels;
177         for(i=1; i<img->height; i++) {
178                 scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
179         }
180
181         cinfo.err = jpeg_std_error(&jerr);      /* XXX */
182         jpeg_create_compress(&cinfo);
183
184         dest.pub.init_destination = init_destination;
185         dest.pub.empty_output_buffer = empty_output_buffer;
186         dest.pub.term_destination = term_destination;
187         dest.io = io;
188         cinfo.dest = (struct jpeg_destination_mgr*)&dest;
189
190         cinfo.image_width = img->width;
191         cinfo.image_height = img->height;
192         cinfo.input_components = 3;
193         cinfo.in_color_space = JCS_RGB;
194
195         jpeg_set_defaults(&cinfo);
196         jpeg_set_quality(&cinfo, 95, 0);
197
198         jpeg_start_compress(&cinfo, 1);
199         while(nlines < img->height) {
200                 int res = jpeg_write_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
201                 nlines += res;
202         }
203         jpeg_finish_compress(&cinfo);
204         jpeg_destroy_compress(&cinfo);
205
206         chk_free(scanlines);
207         img_destroy(&tmpimg);
208         return 0;
209 }
210
211 /* -- read source functions --
212  * the following functions are adapted from jdatasrc.c in jpeglib
213  */
214 static void init_source(j_decompress_ptr jd)
215 {
216         struct src_mgr *src = (struct src_mgr*)jd->src;
217         src->start_of_file = 1;
218 }
219
220 static boolean fill_input_buffer(j_decompress_ptr jd)
221 {
222         struct src_mgr *src = (struct src_mgr*)jd->src;
223         size_t nbytes;
224
225         nbytes = src->io->read(src->buffer, INPUT_BUF_SIZE, src->io->uptr);
226
227         if(nbytes <= 0) {
228                 if(src->start_of_file) {
229                         return 0;
230                 }
231                 /* insert a fake EOI marker */
232                 src->buffer[0] = 0xff;
233                 src->buffer[1] = JPEG_EOI;
234                 nbytes = 2;
235         }
236
237         src->pub.next_input_byte = src->buffer;
238         src->pub.bytes_in_buffer = nbytes;
239         src->start_of_file = 0;
240         return 1;
241 }
242
243 static void skip_input_data(j_decompress_ptr jd, long num_bytes)
244 {
245         struct src_mgr *src = (struct src_mgr*)jd->src;
246
247         if(num_bytes > 0) {
248                 while(num_bytes > (long)src->pub.bytes_in_buffer) {
249                         num_bytes -= (long)src->pub.bytes_in_buffer;
250                         fill_input_buffer(jd);
251                 }
252                 src->pub.next_input_byte += (size_t)num_bytes;
253                 src->pub.bytes_in_buffer -= (size_t)num_bytes;
254         }
255 }
256
257 static void term_source(j_decompress_ptr jd)
258 {
259         /* nothing to see here, move along */
260 }
261
262
263 /* -- write destination functions --
264  * the following functions are adapted from jdatadst.c in jpeglib
265  */
266 static void init_destination(j_compress_ptr jc)
267 {
268         struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
269
270         dest->pub.next_output_byte = dest->buffer;
271         dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
272 }
273
274 static boolean empty_output_buffer(j_compress_ptr jc)
275 {
276         struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
277
278         if(dest->io->write(dest->buffer, OUTPUT_BUF_SIZE, dest->io->uptr) != OUTPUT_BUF_SIZE) {
279                 return 0;
280         }
281
282         dest->pub.next_output_byte = dest->buffer;
283         dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
284         return 1;
285 }
286
287 static void term_destination(j_compress_ptr jc)
288 {
289         struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
290         size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
291
292         /* write any remaining data in the buffer */
293         if(datacount > 0) {
294                 dest->io->write(dest->buffer, datacount, dest->io->uptr);
295         }
296         /* XXX flush? ... */
297 }
298
299 #else
300 /* build without JPEG support */
301 int img_register_jpeg(void)
302 {
303         return -1;
304 }
305 #endif