blob: eeaedd17b024b2fee16744d6e831c98d8e25af64 [file] [log] [blame]
DRC2e7b76b2009-04-03 12:04:24 +00001/* Copyright (C)2004 Landmark Graphics Corporation
2 * Copyright (C)2005 Sun Microsystems, Inc.
DRC61e51f92009-04-05 21:53:20 +00003 * Copyright (C)2009 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;
96 }
97
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()");
127 if(ps!=3 && ps!=4) _throw("This compressor can only take 24-bit or 32-bit RGB input");
128 if(!j->initc) _throw("Instance has not been initialized for compression");
129
130 if(pitch==0) pitch=width*ps;
131
132 j->cinfo.image_width = width;
133 j->cinfo.image_height = height;
134 j->cinfo.input_components = ps;
135
136 #if JCS_EXTENSIONS==1
137 j->cinfo.in_color_space = JCS_EXT_RGB;
138 if(ps==3 && (flags&TJ_BGR))
139 j->cinfo.in_color_space = JCS_EXT_BGR;
140 else if(ps==4 && !(flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
141 j->cinfo.in_color_space = JCS_EXT_RGBX;
142 else if(ps==4 && (flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
143 j->cinfo.in_color_space = JCS_EXT_BGRX;
144 else if(ps==4 && (flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
145 j->cinfo.in_color_space = JCS_EXT_XBGR;
146 else if(ps==4 && !(flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
147 j->cinfo.in_color_space = JCS_EXT_XRGB;
148 #else
149 #error "TurboJPEG requires JPEG colorspace extensions"
150 #endif
151
DRC0c6a2712010-02-22 08:34:44 +0000152 if(flags&TJ_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
153 else if(flags&TJ_FORCESSE) putenv("JSIMD_FORCESSE=1");
154 else if(flags&TJ_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
155
DRC2e7b76b2009-04-03 12:04:24 +0000156 if(setjmp(j->jerr.jb))
157 { // this will execute if LIBJPEG has an error
158 if(row_pointer) free(row_pointer);
159 return -1;
160 }
161
162 jpeg_set_defaults(&j->cinfo);
163
164 jpeg_set_quality(&j->cinfo, qual, TRUE);
165 if(jpegsub==TJ_GRAYSCALE)
166 jpeg_set_colorspace(&j->cinfo, JCS_GRAYSCALE);
167 else
168 jpeg_set_colorspace(&j->cinfo, JCS_YCbCr);
DRCe1716b82011-02-18 03:19:43 +0000169 if(qual>=96) j->cinfo.dct_method=JDCT_ISLOW;
170 else j->cinfo.dct_method=JDCT_FASTEST;
DRC2e7b76b2009-04-03 12:04:24 +0000171
172 j->cinfo.comp_info[0].h_samp_factor=hsampfactor[jpegsub];
173 j->cinfo.comp_info[1].h_samp_factor=1;
174 j->cinfo.comp_info[2].h_samp_factor=1;
175 j->cinfo.comp_info[0].v_samp_factor=vsampfactor[jpegsub];
176 j->cinfo.comp_info[1].v_samp_factor=1;
177 j->cinfo.comp_info[2].v_samp_factor=1;
178
179 j->jdms.next_output_byte = dstbuf;
180 j->jdms.free_in_buffer = TJBUFSIZE(j->cinfo.image_width, j->cinfo.image_height);
181
182 if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
183 _throw("Memory allocation failed in tjInitCompress()");
184 for(i=0; i<height; i++)
185 {
186 if(flags&TJ_BOTTOMUP) row_pointer[i]= &srcbuf[(height-i-1)*pitch];
187 else row_pointer[i]= &srcbuf[i*pitch];
188 }
189 jpeg_start_compress(&j->cinfo, TRUE);
190 while(j->cinfo.next_scanline<j->cinfo.image_height)
191 {
192 jpeg_write_scanlines(&j->cinfo, &row_pointer[j->cinfo.next_scanline],
193 j->cinfo.image_height-j->cinfo.next_scanline);
194 }
195 jpeg_finish_compress(&j->cinfo);
DRC04899092010-02-26 23:01:19 +0000196 *size=TJBUFSIZE(j->cinfo.image_width, j->cinfo.image_height)
197 -(unsigned long)(j->jdms.free_in_buffer);
DRC2e7b76b2009-04-03 12:04:24 +0000198
199 if(row_pointer) free(row_pointer);
200 return 0;
201}
202
203
204// DEC
205
206static boolean fill_input_buffer (struct jpeg_decompress_struct *dinfo)
207{
208 ERREXIT(dinfo, JERR_BUFFER_SIZE);
209 return TRUE;
210}
211
212static void skip_input_data (struct jpeg_decompress_struct *dinfo, long num_bytes)
213{
214 dinfo->src->next_input_byte += (size_t) num_bytes;
215 dinfo->src->bytes_in_buffer -= (size_t) num_bytes;
216}
217
218static void source_noop (struct jpeg_decompress_struct *dinfo)
219{
220}
221
222DLLEXPORT tjhandle DLLCALL tjInitDecompress(void)
223{
224 jpgstruct *j;
225 if((j=(jpgstruct *)malloc(sizeof(jpgstruct)))==NULL)
226 {sprintf(lasterror, "Memory allocation failure"); return NULL;}
227 memset(j, 0, sizeof(jpgstruct));
228 j->dinfo.err=jpeg_std_error(&j->jerr.pub);
229 j->jerr.pub.error_exit=my_error_exit;
230 j->jerr.pub.output_message=my_output_message;
231
232 if(setjmp(j->jerr.jb))
233 { // this will execute if LIBJPEG has an error
234 free(j); return NULL;
235 }
236
237 jpeg_create_decompress(&j->dinfo);
238 j->dinfo.src=&j->jsms;
239 j->jsms.init_source=source_noop;
240 j->jsms.fill_input_buffer = fill_input_buffer;
241 j->jsms.skip_input_data = skip_input_data;
242 j->jsms.resync_to_restart = jpeg_resync_to_restart;
243 j->jsms.term_source = source_noop;
244
245 j->initd=1;
246 return (tjhandle)j;
247}
248
249
250DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle h,
251 unsigned char *srcbuf, unsigned long size,
252 int *width, int *height)
253{
254 checkhandle(h);
255
256 if(srcbuf==NULL || size<=0 || width==NULL || height==NULL)
257 _throw("Invalid argument in tjDecompressHeader()");
258 if(!j->initd) _throw("Instance has not been initialized for decompression");
259
260 if(setjmp(j->jerr.jb))
261 { // this will execute if LIBJPEG has an error
262 return -1;
263 }
264
265 j->jsms.bytes_in_buffer = size;
266 j->jsms.next_input_byte = srcbuf;
267
268 jpeg_read_header(&j->dinfo, TRUE);
269
270 *width=j->dinfo.image_width; *height=j->dinfo.image_height;
271
272 jpeg_abort_decompress(&j->dinfo);
273
274 if(*width<1 || *height<1) _throw("Invalid data returned in header");
275 return 0;
276}
277
278
279DLLEXPORT int DLLCALL tjDecompress(tjhandle h,
280 unsigned char *srcbuf, unsigned long size,
281 unsigned char *dstbuf, int width, int pitch, int height, int ps,
282 int flags)
283{
284 int i; JSAMPROW *row_pointer=NULL;
285
286 checkhandle(h);
287
288 if(srcbuf==NULL || size<=0
289 || dstbuf==NULL || width<=0 || pitch<0 || height<=0)
290 _throw("Invalid argument in tjDecompress()");
291 if(ps!=3 && ps!=4) _throw("This compressor can only take 24-bit or 32-bit RGB input");
292 if(!j->initd) _throw("Instance has not been initialized for decompression");
293
294 if(pitch==0) pitch=width*ps;
295
DRC0c6a2712010-02-22 08:34:44 +0000296 if(flags&TJ_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
297 else if(flags&TJ_FORCESSE) putenv("JSIMD_FORCESSE=1");
298 else if(flags&TJ_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
299
DRC2e7b76b2009-04-03 12:04:24 +0000300 if(setjmp(j->jerr.jb))
301 { // this will execute if LIBJPEG has an error
302 if(row_pointer) free(row_pointer);
303 return -1;
304 }
305
306 j->jsms.bytes_in_buffer = size;
307 j->jsms.next_input_byte = srcbuf;
308
309 jpeg_read_header(&j->dinfo, TRUE);
310
311 if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
312 _throw("Memory allocation failed in tjInitDecompress()");
313 for(i=0; i<height; i++)
314 {
315 if(flags&TJ_BOTTOMUP) row_pointer[i]= &dstbuf[(height-i-1)*pitch];
316 else row_pointer[i]= &dstbuf[i*pitch];
317 }
318
319 #if JCS_EXTENSIONS==1
320 j->dinfo.out_color_space = JCS_EXT_RGB;
321 if(ps==3 && (flags&TJ_BGR))
322 j->dinfo.out_color_space = JCS_EXT_BGR;
323 else if(ps==4 && !(flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
324 j->dinfo.out_color_space = JCS_EXT_RGBX;
325 else if(ps==4 && (flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
326 j->dinfo.out_color_space = JCS_EXT_BGRX;
327 else if(ps==4 && (flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
328 j->dinfo.out_color_space = JCS_EXT_XBGR;
329 else if(ps==4 && !(flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
330 j->dinfo.out_color_space = JCS_EXT_XRGB;
331 #else
332 #error "TurboJPEG requires JPEG colorspace extensions"
333 #endif
DRC61e51f92009-04-05 21:53:20 +0000334 if(flags&TJ_FASTUPSAMPLE) j->dinfo.do_fancy_upsampling=FALSE;
DRC2e7b76b2009-04-03 12:04:24 +0000335
336 jpeg_start_decompress(&j->dinfo);
337 while(j->dinfo.output_scanline<j->dinfo.output_height)
338 {
339 jpeg_read_scanlines(&j->dinfo, &row_pointer[j->dinfo.output_scanline],
340 j->dinfo.output_height-j->dinfo.output_scanline);
341 }
342 jpeg_finish_decompress(&j->dinfo);
343
344 if(row_pointer) free(row_pointer);
345 return 0;
346}
347
348
349// General
350
351DLLEXPORT char* DLLCALL tjGetErrorStr(void)
352{
353 return lasterror;
354}
355
356DLLEXPORT int DLLCALL tjDestroy(tjhandle h)
357{
358 checkhandle(h);
359 if(setjmp(j->jerr.jb)) return -1;
360 if(j->initc) jpeg_destroy_compress(&j->cinfo);
361 if(j->initd) jpeg_destroy_decompress(&j->dinfo);
362 free(j);
363 return 0;
364}