blob: 1d719cb59ac41189f90e65a29f45709496297dff [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>
DRCfbb67472010-11-24 04:02:37 +000021#define JPEG_INTERNALS
DRC2e7b76b2009-04-03 12:04:24 +000022#include <jpeglib.h>
23#include <jerror.h>
24#include <setjmp.h>
25#include "./turbojpeg.h"
DRCfbb67472010-11-24 04:02:37 +000026#ifdef sun
27#include <malloc.h>
28#endif
29
30void *__memalign(size_t boundary, size_t size)
31{
32 #if defined(_WIN32) || defined(__APPLE__)
33 return malloc(size);
34 #else
35 #ifdef sun
36 return memalign(boundary, size);
37 #else
38 void *ptr=NULL;
39 posix_memalign(&ptr, boundary, size);
40 return ptr;
41 #endif
42 #endif
43}
44
45#ifndef min
46 #define min(a,b) ((a)<(b)?(a):(b))
47#endif
48
49#define PAD(v, p) ((v+(p)-1)&(~((p)-1)))
DRC2e7b76b2009-04-03 12:04:24 +000050
51
52// Error handling
53
54static char lasterror[JMSG_LENGTH_MAX]="No error";
55
56typedef struct _error_mgr
57{
58 struct jpeg_error_mgr pub;
59 jmp_buf jb;
60} error_mgr;
61
62static void my_error_exit(j_common_ptr cinfo)
63{
64 error_mgr *myerr = (error_mgr *)cinfo->err;
65 (*cinfo->err->output_message)(cinfo);
66 longjmp(myerr->jb, 1);
67}
68
69static void my_output_message(j_common_ptr cinfo)
70{
71 (*cinfo->err->format_message)(cinfo, lasterror);
72}
73
74
75// Global structures, macros, etc.
76
77typedef struct _jpgstruct
78{
79 struct jpeg_compress_struct cinfo;
80 struct jpeg_decompress_struct dinfo;
81 struct jpeg_destination_mgr jdms;
82 struct jpeg_source_mgr jsms;
83 error_mgr jerr;
84 int initc, initd;
85} jpgstruct;
86
87static const int hsampfactor[NUMSUBOPT]={1, 2, 2, 1};
88static const int vsampfactor[NUMSUBOPT]={1, 1, 2, 1};
89
90#define _throw(c) {sprintf(lasterror, "%s", c); return -1;}
91#define _catch(f) {if((f)==-1) return -1;}
92#define checkhandle(h) jpgstruct *j=(jpgstruct *)h; \
93 if(!j) _throw("Invalid handle");
94
95
96// CO
97
98static boolean empty_output_buffer(struct jpeg_compress_struct *cinfo)
99{
100 ERREXIT(cinfo, JERR_BUFFER_SIZE);
101 return TRUE;
102}
103
104static void destination_noop(struct jpeg_compress_struct *cinfo)
105{
106}
107
108DLLEXPORT tjhandle DLLCALL tjInitCompress(void)
109{
110 jpgstruct *j=NULL;
111 if((j=(jpgstruct *)malloc(sizeof(jpgstruct)))==NULL)
112 {sprintf(lasterror, "Memory allocation failure"); return NULL;}
113 memset(j, 0, sizeof(jpgstruct));
114 j->cinfo.err=jpeg_std_error(&j->jerr.pub);
115 j->jerr.pub.error_exit=my_error_exit;
116 j->jerr.pub.output_message=my_output_message;
117
118 if(setjmp(j->jerr.jb))
119 { // this will execute if LIBJPEG has an error
120 if(j) free(j); return NULL;
DRCefa4ddc2010-10-13 19:22:50 +0000121 }
DRC2e7b76b2009-04-03 12:04:24 +0000122
123 jpeg_create_compress(&j->cinfo);
124 j->cinfo.dest=&j->jdms;
125 j->jdms.init_destination=destination_noop;
126 j->jdms.empty_output_buffer=empty_output_buffer;
127 j->jdms.term_destination=destination_noop;
128
129 j->initc=1;
130 return (tjhandle)j;
131}
132
133DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height)
134{
135 // This allows enough room in case the image doesn't compress
136 return ((width+15)&(~15)) * ((height+15)&(~15)) * 6 + 2048;
137}
138
139DLLEXPORT int DLLCALL tjCompress(tjhandle h,
140 unsigned char *srcbuf, int width, int pitch, int height, int ps,
141 unsigned char *dstbuf, unsigned long *size,
142 int jpegsub, int qual, int flags)
143{
144 int i; JSAMPROW *row_pointer=NULL;
DRCfbb67472010-11-24 04:02:37 +0000145 JSAMPLE *_tmpbuf[MAX_COMPONENTS], *_tmpbuf2[MAX_COMPONENTS];
146 JSAMPROW *tmpbuf[MAX_COMPONENTS], *tmpbuf2[MAX_COMPONENTS];
147 JSAMPROW *outbuf[MAX_COMPONENTS];
DRC2e7b76b2009-04-03 12:04:24 +0000148
149 checkhandle(h);
150
DRCfbb67472010-11-24 04:02:37 +0000151 for(i=0; i<MAX_COMPONENTS; i++)
152 {
153 tmpbuf[i]=NULL; _tmpbuf[i]=NULL;
154 tmpbuf2[i]=NULL; _tmpbuf2[i]=NULL; outbuf[i]=NULL;
155 }
156
DRC2e7b76b2009-04-03 12:04:24 +0000157 if(srcbuf==NULL || width<=0 || pitch<0 || height<=0
158 || dstbuf==NULL || size==NULL
159 || jpegsub<0 || jpegsub>=NUMSUBOPT || qual<0 || qual>100)
160 _throw("Invalid argument in tjCompress()");
DRC09854f52010-11-04 22:39:59 +0000161 if(ps!=3 && ps!=4 && ps!=1)
162 _throw("This compressor can only handle 24-bit and 32-bit RGB or 8-bit grayscale input");
DRC2e7b76b2009-04-03 12:04:24 +0000163 if(!j->initc) _throw("Instance has not been initialized for compression");
164
165 if(pitch==0) pitch=width*ps;
166
167 j->cinfo.image_width = width;
168 j->cinfo.image_height = height;
169 j->cinfo.input_components = ps;
170
DRC09854f52010-11-04 22:39:59 +0000171 if(ps==1) j->cinfo.in_color_space = JCS_GRAYSCALE;
DRC2e7b76b2009-04-03 12:04:24 +0000172 #if JCS_EXTENSIONS==1
DRC09854f52010-11-04 22:39:59 +0000173 else j->cinfo.in_color_space = JCS_EXT_RGB;
DRC2e7b76b2009-04-03 12:04:24 +0000174 if(ps==3 && (flags&TJ_BGR))
175 j->cinfo.in_color_space = JCS_EXT_BGR;
176 else if(ps==4 && !(flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
177 j->cinfo.in_color_space = JCS_EXT_RGBX;
178 else if(ps==4 && (flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
179 j->cinfo.in_color_space = JCS_EXT_BGRX;
180 else if(ps==4 && (flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
181 j->cinfo.in_color_space = JCS_EXT_XBGR;
182 else if(ps==4 && !(flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
183 j->cinfo.in_color_space = JCS_EXT_XRGB;
184 #else
185 #error "TurboJPEG requires JPEG colorspace extensions"
186 #endif
187
DRC0c6a2712010-02-22 08:34:44 +0000188 if(flags&TJ_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
189 else if(flags&TJ_FORCESSE) putenv("JSIMD_FORCESSE=1");
190 else if(flags&TJ_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
191
DRC2e7b76b2009-04-03 12:04:24 +0000192 if(setjmp(j->jerr.jb))
193 { // this will execute if LIBJPEG has an error
194 if(row_pointer) free(row_pointer);
DRCfbb67472010-11-24 04:02:37 +0000195 for(i=0; i<MAX_COMPONENTS; i++)
196 {
197 if(tmpbuf[i]!=NULL) free(tmpbuf[i]);
198 if(_tmpbuf[i]!=NULL) free(_tmpbuf[i]);
199 if(tmpbuf2[i]!=NULL) free(tmpbuf2[i]);
200 if(_tmpbuf2[i]!=NULL) free(_tmpbuf2[i]);
201 if(outbuf[i]!=NULL) free(outbuf[i]);
202 }
DRC2e7b76b2009-04-03 12:04:24 +0000203 return -1;
DRCefa4ddc2010-10-13 19:22:50 +0000204 }
DRC2e7b76b2009-04-03 12:04:24 +0000205
206 jpeg_set_defaults(&j->cinfo);
207
208 jpeg_set_quality(&j->cinfo, qual, TRUE);
209 if(jpegsub==TJ_GRAYSCALE)
210 jpeg_set_colorspace(&j->cinfo, JCS_GRAYSCALE);
211 else
212 jpeg_set_colorspace(&j->cinfo, JCS_YCbCr);
213 j->cinfo.dct_method = JDCT_FASTEST;
214
215 j->cinfo.comp_info[0].h_samp_factor=hsampfactor[jpegsub];
216 j->cinfo.comp_info[1].h_samp_factor=1;
217 j->cinfo.comp_info[2].h_samp_factor=1;
218 j->cinfo.comp_info[0].v_samp_factor=vsampfactor[jpegsub];
219 j->cinfo.comp_info[1].v_samp_factor=1;
220 j->cinfo.comp_info[2].v_samp_factor=1;
221
222 j->jdms.next_output_byte = dstbuf;
223 j->jdms.free_in_buffer = TJBUFSIZE(j->cinfo.image_width, j->cinfo.image_height);
224
DRC2e7b76b2009-04-03 12:04:24 +0000225 jpeg_start_compress(&j->cinfo, TRUE);
DRCfbb67472010-11-24 04:02:37 +0000226 if(flags&TJ_YUV)
DRC2e7b76b2009-04-03 12:04:24 +0000227 {
DRCfbb67472010-11-24 04:02:37 +0000228 j_compress_ptr cinfo=&j->cinfo;
229 int row;
230 int pw=PAD(width, cinfo->max_h_samp_factor);
231 int ph=PAD(height, cinfo->max_v_samp_factor);
232 int cw[MAX_COMPONENTS], ch[MAX_COMPONENTS];
233 jpeg_component_info *compptr;
234 JSAMPLE *ptr=dstbuf; unsigned long yuvsize=0;
235
236 if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph))==NULL)
237 _throw("Memory allocation failed in tjCompress()");
238 for(i=0; i<height; i++)
239 {
240 if(flags&TJ_BOTTOMUP) row_pointer[i]= &srcbuf[(height-i-1)*pitch];
241 else row_pointer[i]= &srcbuf[i*pitch];
242 }
243 if(height<ph)
244 for(i=height; i<ph; i++) row_pointer[i]=row_pointer[height-1];
245
246 for(i=0; i<cinfo->num_components; i++)
247 {
248 compptr=&cinfo->comp_info[i];
249 _tmpbuf[i]=(JSAMPLE *)__memalign(16,
250 PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE)
251 /compptr->h_samp_factor, 16) * cinfo->max_v_samp_factor);
252 if(!_tmpbuf[i]) _throw("Memory allocation failure");
253 tmpbuf[i]=(JSAMPROW *)__memalign(16,
254 sizeof(JSAMPROW)*cinfo->max_v_samp_factor);
255 if(!tmpbuf[i]) _throw("Memory allocation failure");
256 for(row=0; row<cinfo->max_v_samp_factor; row++)
257 tmpbuf[i][row]=&_tmpbuf[i][
258 PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE)
259 /compptr->h_samp_factor, 16) * row];
260 _tmpbuf2[i]=(JSAMPLE *)__memalign(16,
261 PAD(compptr->width_in_blocks*DCTSIZE, 16) * compptr->v_samp_factor);
262 if(!_tmpbuf2[i]) _throw("Memory allocation failure");
263 tmpbuf2[i]=(JSAMPROW *)__memalign(16,
264 sizeof(JSAMPROW)*compptr->v_samp_factor);
265 if(!tmpbuf2[i]) _throw("Memory allocation failure");
266 for(row=0; row<compptr->v_samp_factor; row++)
267 tmpbuf2[i][row]=&_tmpbuf2[i][
268 PAD(compptr->width_in_blocks*DCTSIZE, 16) * row];
269 cw[i]=pw*compptr->h_samp_factor/cinfo->max_h_samp_factor;
270 ch[i]=ph*compptr->v_samp_factor/cinfo->max_v_samp_factor;
271 outbuf[i]=(JSAMPROW *)__memalign(16, sizeof(JSAMPROW)*ch[i]);
272 if(!outbuf[i]) _throw("Memory allocation failure");
273 for(row=0; row<ch[i]; row++)
274 {
275 outbuf[i][row]=ptr;
276 ptr+=PAD(cw[i], 4);
277 }
278 }
279 yuvsize=(unsigned long)(ptr-dstbuf);
280
281 for(row=0; row<ph; row+=cinfo->max_v_samp_factor)
282 {
283 (*cinfo->cconvert->color_convert)(cinfo, &row_pointer[row], tmpbuf,
284 0, cinfo->max_v_samp_factor);
285 (cinfo->downsample->downsample)(cinfo, tmpbuf, 0, tmpbuf2, 0);
286 for(i=0, compptr=cinfo->comp_info; i<cinfo->num_components;
287 i++, compptr++)
288 jcopy_sample_rows(tmpbuf2[i], 0, outbuf[i],
289 row*compptr->v_samp_factor/cinfo->max_v_samp_factor,
290 compptr->v_samp_factor, cw[i]);
291 }
292 *size=yuvsize;
293 cinfo->next_scanline+=height;
294 }
295 else
296 {
297 if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
298 _throw("Memory allocation failed in tjCompress()");
299 for(i=0; i<height; i++)
300 {
301 if(flags&TJ_BOTTOMUP) row_pointer[i]= &srcbuf[(height-i-1)*pitch];
302 else row_pointer[i]= &srcbuf[i*pitch];
303 }
304 while(j->cinfo.next_scanline<j->cinfo.image_height)
305 {
306 jpeg_write_scanlines(&j->cinfo, &row_pointer[j->cinfo.next_scanline],
307 j->cinfo.image_height-j->cinfo.next_scanline);
308 }
DRC2e7b76b2009-04-03 12:04:24 +0000309 }
310 jpeg_finish_compress(&j->cinfo);
DRCfbb67472010-11-24 04:02:37 +0000311 if(!(flags&TJ_YUV))
312 *size=TJBUFSIZE(j->cinfo.image_width, j->cinfo.image_height)
313 -(unsigned long)(j->jdms.free_in_buffer);
DRC2e7b76b2009-04-03 12:04:24 +0000314
315 if(row_pointer) free(row_pointer);
DRCfbb67472010-11-24 04:02:37 +0000316 for(i=0; i<MAX_COMPONENTS; i++)
317 {
318 if(tmpbuf[i]!=NULL) free(tmpbuf[i]);
319 if(_tmpbuf[i]!=NULL) free(_tmpbuf[i]);
320 if(tmpbuf2[i]!=NULL) free(tmpbuf2[i]);
321 if(_tmpbuf2[i]!=NULL) free(_tmpbuf2[i]);
322 if(outbuf[i]!=NULL) free(outbuf[i]);
323 }
DRC2e7b76b2009-04-03 12:04:24 +0000324 return 0;
325}
326
327
328// DEC
329
330static boolean fill_input_buffer (struct jpeg_decompress_struct *dinfo)
331{
332 ERREXIT(dinfo, JERR_BUFFER_SIZE);
333 return TRUE;
334}
335
336static void skip_input_data (struct jpeg_decompress_struct *dinfo, long num_bytes)
337{
338 dinfo->src->next_input_byte += (size_t) num_bytes;
339 dinfo->src->bytes_in_buffer -= (size_t) num_bytes;
340}
341
342static void source_noop (struct jpeg_decompress_struct *dinfo)
343{
344}
345
346DLLEXPORT tjhandle DLLCALL tjInitDecompress(void)
347{
348 jpgstruct *j;
349 if((j=(jpgstruct *)malloc(sizeof(jpgstruct)))==NULL)
350 {sprintf(lasterror, "Memory allocation failure"); return NULL;}
351 memset(j, 0, sizeof(jpgstruct));
352 j->dinfo.err=jpeg_std_error(&j->jerr.pub);
353 j->jerr.pub.error_exit=my_error_exit;
354 j->jerr.pub.output_message=my_output_message;
355
356 if(setjmp(j->jerr.jb))
357 { // this will execute if LIBJPEG has an error
358 free(j); return NULL;
359 }
360
361 jpeg_create_decompress(&j->dinfo);
362 j->dinfo.src=&j->jsms;
363 j->jsms.init_source=source_noop;
364 j->jsms.fill_input_buffer = fill_input_buffer;
365 j->jsms.skip_input_data = skip_input_data;
366 j->jsms.resync_to_restart = jpeg_resync_to_restart;
367 j->jsms.term_source = source_noop;
368
369 j->initd=1;
370 return (tjhandle)j;
371}
372
373
374DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle h,
375 unsigned char *srcbuf, unsigned long size,
376 int *width, int *height)
377{
378 checkhandle(h);
379
380 if(srcbuf==NULL || size<=0 || width==NULL || height==NULL)
381 _throw("Invalid argument in tjDecompressHeader()");
382 if(!j->initd) _throw("Instance has not been initialized for decompression");
383
384 if(setjmp(j->jerr.jb))
385 { // this will execute if LIBJPEG has an error
386 return -1;
387 }
388
389 j->jsms.bytes_in_buffer = size;
390 j->jsms.next_input_byte = srcbuf;
391
392 jpeg_read_header(&j->dinfo, TRUE);
393
394 *width=j->dinfo.image_width; *height=j->dinfo.image_height;
395
396 jpeg_abort_decompress(&j->dinfo);
397
398 if(*width<1 || *height<1) _throw("Invalid data returned in header");
399 return 0;
400}
401
402
403DLLEXPORT int DLLCALL tjDecompress(tjhandle h,
404 unsigned char *srcbuf, unsigned long size,
405 unsigned char *dstbuf, int width, int pitch, int height, int ps,
406 int flags)
407{
408 int i; JSAMPROW *row_pointer=NULL;
409
410 checkhandle(h);
411
412 if(srcbuf==NULL || size<=0
413 || dstbuf==NULL || width<=0 || pitch<0 || height<=0)
414 _throw("Invalid argument in tjDecompress()");
DRC09854f52010-11-04 22:39:59 +0000415 if(ps!=3 && ps!=4 && ps!=1)
416 _throw("This decompressor can only handle 24-bit and 32-bit RGB or 8-bit grayscale output");
DRC2e7b76b2009-04-03 12:04:24 +0000417 if(!j->initd) _throw("Instance has not been initialized for decompression");
418
419 if(pitch==0) pitch=width*ps;
420
DRC0c6a2712010-02-22 08:34:44 +0000421 if(flags&TJ_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
422 else if(flags&TJ_FORCESSE) putenv("JSIMD_FORCESSE=1");
423 else if(flags&TJ_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
424
DRC2e7b76b2009-04-03 12:04:24 +0000425 if(setjmp(j->jerr.jb))
426 { // this will execute if LIBJPEG has an error
427 if(row_pointer) free(row_pointer);
428 return -1;
429 }
430
431 j->jsms.bytes_in_buffer = size;
432 j->jsms.next_input_byte = srcbuf;
433
434 jpeg_read_header(&j->dinfo, TRUE);
435
436 if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
437 _throw("Memory allocation failed in tjInitDecompress()");
438 for(i=0; i<height; i++)
439 {
440 if(flags&TJ_BOTTOMUP) row_pointer[i]= &dstbuf[(height-i-1)*pitch];
441 else row_pointer[i]= &dstbuf[i*pitch];
442 }
443
DRC09854f52010-11-04 22:39:59 +0000444 if(ps==1) j->dinfo.out_color_space = JCS_GRAYSCALE;
DRC2e7b76b2009-04-03 12:04:24 +0000445 #if JCS_EXTENSIONS==1
DRC09854f52010-11-04 22:39:59 +0000446 else j->dinfo.out_color_space = JCS_EXT_RGB;
DRC2e7b76b2009-04-03 12:04:24 +0000447 if(ps==3 && (flags&TJ_BGR))
448 j->dinfo.out_color_space = JCS_EXT_BGR;
449 else if(ps==4 && !(flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
450 j->dinfo.out_color_space = JCS_EXT_RGBX;
451 else if(ps==4 && (flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
452 j->dinfo.out_color_space = JCS_EXT_BGRX;
453 else if(ps==4 && (flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
454 j->dinfo.out_color_space = JCS_EXT_XBGR;
455 else if(ps==4 && !(flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
456 j->dinfo.out_color_space = JCS_EXT_XRGB;
457 #else
458 #error "TurboJPEG requires JPEG colorspace extensions"
459 #endif
DRCfbb67472010-11-24 04:02:37 +0000460
DRC61e51f92009-04-05 21:53:20 +0000461 if(flags&TJ_FASTUPSAMPLE) j->dinfo.do_fancy_upsampling=FALSE;
DRC2e7b76b2009-04-03 12:04:24 +0000462
463 jpeg_start_decompress(&j->dinfo);
464 while(j->dinfo.output_scanline<j->dinfo.output_height)
465 {
466 jpeg_read_scanlines(&j->dinfo, &row_pointer[j->dinfo.output_scanline],
467 j->dinfo.output_height-j->dinfo.output_scanline);
468 }
469 jpeg_finish_decompress(&j->dinfo);
470
471 if(row_pointer) free(row_pointer);
472 return 0;
473}
474
475
476// General
477
478DLLEXPORT char* DLLCALL tjGetErrorStr(void)
479{
480 return lasterror;
481}
482
483DLLEXPORT int DLLCALL tjDestroy(tjhandle h)
484{
485 checkhandle(h);
486 if(setjmp(j->jerr.jb)) return -1;
487 if(j->initc) jpeg_destroy_compress(&j->cinfo);
488 if(j->initd) jpeg_destroy_decompress(&j->dinfo);
489 free(j);
490 return 0;
491}