blob: 03833c504d04dd744775f0bddb1ae327a5353d09 [file] [log] [blame]
DRC2e7b76b2009-04-03 12:04:24 +00001/* Copyright (C)2004 Landmark Graphics Corporation
2 * Copyright (C)2005 Sun Microsystems, Inc.
DRC09854f52010-11-04 22:39:59 +00003 * Copyright (C)2009-2010 D. R. Commander
DRC2e7b76b2009-04-03 12:04:24 +00004 *
5 * This library is free software and may be redistributed and/or modified under
6 * the terms of the wxWindows Library License, Version 3.1 or (at your option)
7 * any later version. The full license is in the LICENSE.txt file included
8 * with this distribution.
9 *
10 * This library 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 * wxWindows Library License for more details.
14 */
15
16// This implements a JPEG compressor/decompressor using the libjpeg API
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <jpeglib.h>
22#include <jerror.h>
23#include <setjmp.h>
24#include "./turbojpeg.h"
25
26
27// Error handling
28
29static char lasterror[JMSG_LENGTH_MAX]="No error";
30
31typedef struct _error_mgr
32{
33 struct jpeg_error_mgr pub;
34 jmp_buf jb;
35} error_mgr;
36
37static void my_error_exit(j_common_ptr cinfo)
38{
39 error_mgr *myerr = (error_mgr *)cinfo->err;
40 (*cinfo->err->output_message)(cinfo);
41 longjmp(myerr->jb, 1);
42}
43
44static void my_output_message(j_common_ptr cinfo)
45{
46 (*cinfo->err->format_message)(cinfo, lasterror);
47}
48
49
50// Global structures, macros, etc.
51
52typedef struct _jpgstruct
53{
54 struct jpeg_compress_struct cinfo;
55 struct jpeg_decompress_struct dinfo;
56 struct jpeg_destination_mgr jdms;
57 struct jpeg_source_mgr jsms;
58 error_mgr jerr;
59 int initc, initd;
60} jpgstruct;
61
62static const int hsampfactor[NUMSUBOPT]={1, 2, 2, 1};
63static const int vsampfactor[NUMSUBOPT]={1, 1, 2, 1};
64
65#define _throw(c) {sprintf(lasterror, "%s", c); return -1;}
66#define _catch(f) {if((f)==-1) return -1;}
67#define checkhandle(h) jpgstruct *j=(jpgstruct *)h; \
68 if(!j) _throw("Invalid handle");
69
70
71// CO
72
73static boolean empty_output_buffer(struct jpeg_compress_struct *cinfo)
74{
75 ERREXIT(cinfo, JERR_BUFFER_SIZE);
76 return TRUE;
77}
78
79static void destination_noop(struct jpeg_compress_struct *cinfo)
80{
81}
82
83DLLEXPORT tjhandle DLLCALL tjInitCompress(void)
84{
85 jpgstruct *j=NULL;
86 if((j=(jpgstruct *)malloc(sizeof(jpgstruct)))==NULL)
87 {sprintf(lasterror, "Memory allocation failure"); return NULL;}
88 memset(j, 0, sizeof(jpgstruct));
89 j->cinfo.err=jpeg_std_error(&j->jerr.pub);
90 j->jerr.pub.error_exit=my_error_exit;
91 j->jerr.pub.output_message=my_output_message;
92
93 if(setjmp(j->jerr.jb))
94 { // this will execute if LIBJPEG has an error
95 if(j) free(j); return NULL;
DRCefa4ddc2010-10-13 19:22:50 +000096 }
DRC2e7b76b2009-04-03 12:04:24 +000097
98 jpeg_create_compress(&j->cinfo);
99 j->cinfo.dest=&j->jdms;
100 j->jdms.init_destination=destination_noop;
101 j->jdms.empty_output_buffer=empty_output_buffer;
102 j->jdms.term_destination=destination_noop;
103
104 j->initc=1;
105 return (tjhandle)j;
106}
107
108DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height)
109{
110 // This allows enough room in case the image doesn't compress
111 return ((width+15)&(~15)) * ((height+15)&(~15)) * 6 + 2048;
112}
113
114DLLEXPORT int DLLCALL tjCompress(tjhandle h,
115 unsigned char *srcbuf, int width, int pitch, int height, int ps,
116 unsigned char *dstbuf, unsigned long *size,
117 int jpegsub, int qual, int flags)
118{
119 int i; JSAMPROW *row_pointer=NULL;
120
121 checkhandle(h);
122
123 if(srcbuf==NULL || width<=0 || pitch<0 || height<=0
124 || dstbuf==NULL || size==NULL
125 || jpegsub<0 || jpegsub>=NUMSUBOPT || qual<0 || qual>100)
126 _throw("Invalid argument in tjCompress()");
DRC09854f52010-11-04 22:39:59 +0000127 if(ps!=3 && ps!=4 && ps!=1)
128 _throw("This compressor can only handle 24-bit and 32-bit RGB or 8-bit grayscale input");
DRC2e7b76b2009-04-03 12:04:24 +0000129 if(!j->initc) _throw("Instance has not been initialized for compression");
130
131 if(pitch==0) pitch=width*ps;
132
133 j->cinfo.image_width = width;
134 j->cinfo.image_height = height;
135 j->cinfo.input_components = ps;
136
DRC09854f52010-11-04 22:39:59 +0000137 if(ps==1) j->cinfo.in_color_space = JCS_GRAYSCALE;
DRC2e7b76b2009-04-03 12:04:24 +0000138 #if JCS_EXTENSIONS==1
DRC09854f52010-11-04 22:39:59 +0000139 else j->cinfo.in_color_space = JCS_EXT_RGB;
DRC2e7b76b2009-04-03 12:04:24 +0000140 if(ps==3 && (flags&TJ_BGR))
141 j->cinfo.in_color_space = JCS_EXT_BGR;
142 else if(ps==4 && !(flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
143 j->cinfo.in_color_space = JCS_EXT_RGBX;
144 else if(ps==4 && (flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
145 j->cinfo.in_color_space = JCS_EXT_BGRX;
146 else if(ps==4 && (flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
147 j->cinfo.in_color_space = JCS_EXT_XBGR;
148 else if(ps==4 && !(flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
149 j->cinfo.in_color_space = JCS_EXT_XRGB;
150 #else
151 #error "TurboJPEG requires JPEG colorspace extensions"
152 #endif
153
DRC0c6a2712010-02-22 08:34:44 +0000154 if(flags&TJ_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
155 else if(flags&TJ_FORCESSE) putenv("JSIMD_FORCESSE=1");
156 else if(flags&TJ_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
157
DRC2e7b76b2009-04-03 12:04:24 +0000158 if(setjmp(j->jerr.jb))
159 { // this will execute if LIBJPEG has an error
160 if(row_pointer) free(row_pointer);
161 return -1;
DRCefa4ddc2010-10-13 19:22:50 +0000162 }
DRC2e7b76b2009-04-03 12:04:24 +0000163
164 jpeg_set_defaults(&j->cinfo);
165
166 jpeg_set_quality(&j->cinfo, qual, TRUE);
167 if(jpegsub==TJ_GRAYSCALE)
168 jpeg_set_colorspace(&j->cinfo, JCS_GRAYSCALE);
169 else
170 jpeg_set_colorspace(&j->cinfo, JCS_YCbCr);
171 j->cinfo.dct_method = JDCT_FASTEST;
172
173 j->cinfo.comp_info[0].h_samp_factor=hsampfactor[jpegsub];
174 j->cinfo.comp_info[1].h_samp_factor=1;
175 j->cinfo.comp_info[2].h_samp_factor=1;
176 j->cinfo.comp_info[0].v_samp_factor=vsampfactor[jpegsub];
177 j->cinfo.comp_info[1].v_samp_factor=1;
178 j->cinfo.comp_info[2].v_samp_factor=1;
179
180 j->jdms.next_output_byte = dstbuf;
181 j->jdms.free_in_buffer = TJBUFSIZE(j->cinfo.image_width, j->cinfo.image_height);
182
183 if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
184 _throw("Memory allocation failed in tjInitCompress()");
185 for(i=0; i<height; i++)
186 {
187 if(flags&TJ_BOTTOMUP) row_pointer[i]= &srcbuf[(height-i-1)*pitch];
188 else row_pointer[i]= &srcbuf[i*pitch];
189 }
190 jpeg_start_compress(&j->cinfo, TRUE);
191 while(j->cinfo.next_scanline<j->cinfo.image_height)
192 {
193 jpeg_write_scanlines(&j->cinfo, &row_pointer[j->cinfo.next_scanline],
194 j->cinfo.image_height-j->cinfo.next_scanline);
195 }
196 jpeg_finish_compress(&j->cinfo);
DRC04899092010-02-26 23:01:19 +0000197 *size=TJBUFSIZE(j->cinfo.image_width, j->cinfo.image_height)
198 -(unsigned long)(j->jdms.free_in_buffer);
DRC2e7b76b2009-04-03 12:04:24 +0000199
200 if(row_pointer) free(row_pointer);
201 return 0;
202}
203
204
205// DEC
206
207static boolean fill_input_buffer (struct jpeg_decompress_struct *dinfo)
208{
209 ERREXIT(dinfo, JERR_BUFFER_SIZE);
210 return TRUE;
211}
212
213static void skip_input_data (struct jpeg_decompress_struct *dinfo, long num_bytes)
214{
215 dinfo->src->next_input_byte += (size_t) num_bytes;
216 dinfo->src->bytes_in_buffer -= (size_t) num_bytes;
217}
218
219static void source_noop (struct jpeg_decompress_struct *dinfo)
220{
221}
222
223DLLEXPORT tjhandle DLLCALL tjInitDecompress(void)
224{
225 jpgstruct *j;
226 if((j=(jpgstruct *)malloc(sizeof(jpgstruct)))==NULL)
227 {sprintf(lasterror, "Memory allocation failure"); return NULL;}
228 memset(j, 0, sizeof(jpgstruct));
229 j->dinfo.err=jpeg_std_error(&j->jerr.pub);
230 j->jerr.pub.error_exit=my_error_exit;
231 j->jerr.pub.output_message=my_output_message;
232
233 if(setjmp(j->jerr.jb))
234 { // this will execute if LIBJPEG has an error
235 free(j); return NULL;
236 }
237
238 jpeg_create_decompress(&j->dinfo);
239 j->dinfo.src=&j->jsms;
240 j->jsms.init_source=source_noop;
241 j->jsms.fill_input_buffer = fill_input_buffer;
242 j->jsms.skip_input_data = skip_input_data;
243 j->jsms.resync_to_restart = jpeg_resync_to_restart;
244 j->jsms.term_source = source_noop;
245
246 j->initd=1;
247 return (tjhandle)j;
248}
249
250
251DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle h,
252 unsigned char *srcbuf, unsigned long size,
253 int *width, int *height)
254{
255 checkhandle(h);
256
257 if(srcbuf==NULL || size<=0 || width==NULL || height==NULL)
258 _throw("Invalid argument in tjDecompressHeader()");
259 if(!j->initd) _throw("Instance has not been initialized for decompression");
260
261 if(setjmp(j->jerr.jb))
262 { // this will execute if LIBJPEG has an error
263 return -1;
264 }
265
266 j->jsms.bytes_in_buffer = size;
267 j->jsms.next_input_byte = srcbuf;
268
269 jpeg_read_header(&j->dinfo, TRUE);
270
271 *width=j->dinfo.image_width; *height=j->dinfo.image_height;
272
273 jpeg_abort_decompress(&j->dinfo);
274
275 if(*width<1 || *height<1) _throw("Invalid data returned in header");
276 return 0;
277}
278
279
280DLLEXPORT int DLLCALL tjDecompress(tjhandle h,
281 unsigned char *srcbuf, unsigned long size,
282 unsigned char *dstbuf, int width, int pitch, int height, int ps,
283 int flags)
284{
285 int i; JSAMPROW *row_pointer=NULL;
286
287 checkhandle(h);
288
289 if(srcbuf==NULL || size<=0
290 || dstbuf==NULL || width<=0 || pitch<0 || height<=0)
291 _throw("Invalid argument in tjDecompress()");
DRC09854f52010-11-04 22:39:59 +0000292 if(ps!=3 && ps!=4 && ps!=1)
293 _throw("This decompressor can only handle 24-bit and 32-bit RGB or 8-bit grayscale output");
DRC2e7b76b2009-04-03 12:04:24 +0000294 if(!j->initd) _throw("Instance has not been initialized for decompression");
295
296 if(pitch==0) pitch=width*ps;
297
DRC0c6a2712010-02-22 08:34:44 +0000298 if(flags&TJ_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
299 else if(flags&TJ_FORCESSE) putenv("JSIMD_FORCESSE=1");
300 else if(flags&TJ_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
301
DRC2e7b76b2009-04-03 12:04:24 +0000302 if(setjmp(j->jerr.jb))
303 { // this will execute if LIBJPEG has an error
304 if(row_pointer) free(row_pointer);
305 return -1;
306 }
307
308 j->jsms.bytes_in_buffer = size;
309 j->jsms.next_input_byte = srcbuf;
310
311 jpeg_read_header(&j->dinfo, TRUE);
312
313 if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
314 _throw("Memory allocation failed in tjInitDecompress()");
315 for(i=0; i<height; i++)
316 {
317 if(flags&TJ_BOTTOMUP) row_pointer[i]= &dstbuf[(height-i-1)*pitch];
318 else row_pointer[i]= &dstbuf[i*pitch];
319 }
320
DRC09854f52010-11-04 22:39:59 +0000321 if(ps==1) j->dinfo.out_color_space = JCS_GRAYSCALE;
DRC2e7b76b2009-04-03 12:04:24 +0000322 #if JCS_EXTENSIONS==1
DRC09854f52010-11-04 22:39:59 +0000323 else j->dinfo.out_color_space = JCS_EXT_RGB;
DRC2e7b76b2009-04-03 12:04:24 +0000324 if(ps==3 && (flags&TJ_BGR))
325 j->dinfo.out_color_space = JCS_EXT_BGR;
326 else if(ps==4 && !(flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
327 j->dinfo.out_color_space = JCS_EXT_RGBX;
328 else if(ps==4 && (flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
329 j->dinfo.out_color_space = JCS_EXT_BGRX;
330 else if(ps==4 && (flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
331 j->dinfo.out_color_space = JCS_EXT_XBGR;
332 else if(ps==4 && !(flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
333 j->dinfo.out_color_space = JCS_EXT_XRGB;
334 #else
335 #error "TurboJPEG requires JPEG colorspace extensions"
336 #endif
DRC61e51f92009-04-05 21:53:20 +0000337 if(flags&TJ_FASTUPSAMPLE) j->dinfo.do_fancy_upsampling=FALSE;
DRC2e7b76b2009-04-03 12:04:24 +0000338
339 jpeg_start_decompress(&j->dinfo);
340 while(j->dinfo.output_scanline<j->dinfo.output_height)
341 {
342 jpeg_read_scanlines(&j->dinfo, &row_pointer[j->dinfo.output_scanline],
343 j->dinfo.output_height-j->dinfo.output_scanline);
344 }
345 jpeg_finish_decompress(&j->dinfo);
346
347 if(row_pointer) free(row_pointer);
348 return 0;
349}
350
351
352// General
353
354DLLEXPORT char* DLLCALL tjGetErrorStr(void)
355{
356 return lasterror;
357}
358
359DLLEXPORT int DLLCALL tjDestroy(tjhandle h)
360{
361 checkhandle(h);
362 if(setjmp(j->jerr.jb)) return -1;
363 if(j->initc) jpeg_destroy_compress(&j->cinfo);
364 if(j->initd) jpeg_destroy_decompress(&j->dinfo);
365 free(j);
366 return 0;
367}