DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1 | /* Copyright (C)2004 Landmark Graphics Corporation |
| 2 | * Copyright (C)2005 Sun Microsystems, Inc. |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 3 | * Copyright (C)2009-2010 D. R. Commander |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 4 | * |
| 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> |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 21 | #define JPEG_INTERNALS |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 22 | #include <jpeglib.h> |
| 23 | #include <jerror.h> |
| 24 | #include <setjmp.h> |
| 25 | #include "./turbojpeg.h" |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 26 | #ifdef sun |
| 27 | #include <malloc.h> |
| 28 | #endif |
| 29 | |
| 30 | void *__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))) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | // Error handling |
| 53 | |
| 54 | static char lasterror[JMSG_LENGTH_MAX]="No error"; |
| 55 | |
| 56 | typedef struct _error_mgr |
| 57 | { |
| 58 | struct jpeg_error_mgr pub; |
| 59 | jmp_buf jb; |
| 60 | } error_mgr; |
| 61 | |
| 62 | static 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 | |
| 69 | static 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 | |
| 77 | typedef 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 | |
| 87 | static const int hsampfactor[NUMSUBOPT]={1, 2, 2, 1}; |
| 88 | static 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 | |
| 98 | static boolean empty_output_buffer(struct jpeg_compress_struct *cinfo) |
| 99 | { |
| 100 | ERREXIT(cinfo, JERR_BUFFER_SIZE); |
| 101 | return TRUE; |
| 102 | } |
| 103 | |
| 104 | static void destination_noop(struct jpeg_compress_struct *cinfo) |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | DLLEXPORT 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; |
DRC | efa4ddc | 2010-10-13 19:22:50 +0000 | [diff] [blame] | 121 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 122 | |
| 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 | |
| 133 | DLLEXPORT 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 | |
| 139 | DLLEXPORT 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; |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 145 | JSAMPLE *_tmpbuf[MAX_COMPONENTS], *_tmpbuf2[MAX_COMPONENTS]; |
| 146 | JSAMPROW *tmpbuf[MAX_COMPONENTS], *tmpbuf2[MAX_COMPONENTS]; |
| 147 | JSAMPROW *outbuf[MAX_COMPONENTS]; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 148 | |
| 149 | checkhandle(h); |
| 150 | |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 151 | 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 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 157 | 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()"); |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 161 | 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"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 163 | 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 | |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 171 | if(ps==1) j->cinfo.in_color_space = JCS_GRAYSCALE; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 172 | #if JCS_EXTENSIONS==1 |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 173 | else j->cinfo.in_color_space = JCS_EXT_RGB; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 174 | 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 | |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 188 | 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 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 192 | if(setjmp(j->jerr.jb)) |
| 193 | { // this will execute if LIBJPEG has an error |
| 194 | if(row_pointer) free(row_pointer); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 195 | 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 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 203 | return -1; |
DRC | efa4ddc | 2010-10-13 19:22:50 +0000 | [diff] [blame] | 204 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 205 | |
| 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 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 225 | jpeg_start_compress(&j->cinfo, TRUE); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 226 | if(flags&TJ_YUV) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 227 | { |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 228 | 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 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 309 | } |
| 310 | jpeg_finish_compress(&j->cinfo); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 311 | if(!(flags&TJ_YUV)) |
| 312 | *size=TJBUFSIZE(j->cinfo.image_width, j->cinfo.image_height) |
| 313 | -(unsigned long)(j->jdms.free_in_buffer); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 314 | |
| 315 | if(row_pointer) free(row_pointer); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 316 | 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 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | // DEC |
| 329 | |
| 330 | static boolean fill_input_buffer (struct jpeg_decompress_struct *dinfo) |
| 331 | { |
| 332 | ERREXIT(dinfo, JERR_BUFFER_SIZE); |
| 333 | return TRUE; |
| 334 | } |
| 335 | |
| 336 | static 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 | |
| 342 | static void source_noop (struct jpeg_decompress_struct *dinfo) |
| 343 | { |
| 344 | } |
| 345 | |
| 346 | DLLEXPORT 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; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame^] | 359 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 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 | |
| 374 | DLLEXPORT 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 | |
| 403 | DLLEXPORT 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 | { |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame^] | 408 | int i, row; JSAMPROW *row_pointer=NULL, *outbuf[MAX_COMPONENTS]; |
| 409 | int cw[MAX_COMPONENTS], ch[MAX_COMPONENTS]; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 410 | |
| 411 | checkhandle(h); |
| 412 | |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame^] | 413 | for(i=0; i<MAX_COMPONENTS; i++) outbuf[i]=NULL; |
| 414 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 415 | if(srcbuf==NULL || size<=0 |
| 416 | || dstbuf==NULL || width<=0 || pitch<0 || height<=0) |
| 417 | _throw("Invalid argument in tjDecompress()"); |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 418 | if(ps!=3 && ps!=4 && ps!=1) |
| 419 | _throw("This decompressor can only handle 24-bit and 32-bit RGB or 8-bit grayscale output"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 420 | if(!j->initd) _throw("Instance has not been initialized for decompression"); |
| 421 | |
| 422 | if(pitch==0) pitch=width*ps; |
| 423 | |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 424 | if(flags&TJ_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 425 | else if(flags&TJ_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 426 | else if(flags&TJ_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
| 427 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 428 | if(setjmp(j->jerr.jb)) |
| 429 | { // this will execute if LIBJPEG has an error |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame^] | 430 | for(i=0; i<MAX_COMPONENTS; i++) |
| 431 | if(outbuf[i]!=NULL) free(outbuf[i]); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 432 | if(row_pointer) free(row_pointer); |
| 433 | return -1; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame^] | 434 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 435 | |
| 436 | j->jsms.bytes_in_buffer = size; |
| 437 | j->jsms.next_input_byte = srcbuf; |
| 438 | |
| 439 | jpeg_read_header(&j->dinfo, TRUE); |
| 440 | |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame^] | 441 | if(flags&TJ_YUV) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 442 | { |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame^] | 443 | j_decompress_ptr dinfo=&j->dinfo; |
| 444 | JSAMPLE *ptr=dstbuf; |
| 445 | |
| 446 | for(i=0; i<dinfo->num_components; i++) |
| 447 | { |
| 448 | jpeg_component_info *compptr=&dinfo->comp_info[i]; |
| 449 | cw[i]=compptr->width_in_blocks*DCTSIZE; |
| 450 | ch[i]=compptr->height_in_blocks*DCTSIZE; |
| 451 | if((outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]))==NULL) |
| 452 | _throw("Memory allocation failed in tjInitDecompress()"); |
| 453 | for(row=0; row<ch[i]; row++) |
| 454 | { |
| 455 | outbuf[i][row]=ptr; |
| 456 | ptr+=cw[i]; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | else |
| 461 | { |
| 462 | if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL) |
| 463 | _throw("Memory allocation failed in tjInitDecompress()"); |
| 464 | for(i=0; i<height; i++) |
| 465 | { |
| 466 | if(flags&TJ_BOTTOMUP) row_pointer[i]= &dstbuf[(height-i-1)*pitch]; |
| 467 | else row_pointer[i]= &dstbuf[i*pitch]; |
| 468 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 469 | } |
| 470 | |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 471 | if(ps==1) j->dinfo.out_color_space = JCS_GRAYSCALE; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 472 | #if JCS_EXTENSIONS==1 |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 473 | else j->dinfo.out_color_space = JCS_EXT_RGB; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 474 | if(ps==3 && (flags&TJ_BGR)) |
| 475 | j->dinfo.out_color_space = JCS_EXT_BGR; |
| 476 | else if(ps==4 && !(flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST)) |
| 477 | j->dinfo.out_color_space = JCS_EXT_RGBX; |
| 478 | else if(ps==4 && (flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST)) |
| 479 | j->dinfo.out_color_space = JCS_EXT_BGRX; |
| 480 | else if(ps==4 && (flags&TJ_BGR) && (flags&TJ_ALPHAFIRST)) |
| 481 | j->dinfo.out_color_space = JCS_EXT_XBGR; |
| 482 | else if(ps==4 && !(flags&TJ_BGR) && (flags&TJ_ALPHAFIRST)) |
| 483 | j->dinfo.out_color_space = JCS_EXT_XRGB; |
| 484 | #else |
| 485 | #error "TurboJPEG requires JPEG colorspace extensions" |
| 486 | #endif |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 487 | |
DRC | 61e51f9 | 2009-04-05 21:53:20 +0000 | [diff] [blame] | 488 | if(flags&TJ_FASTUPSAMPLE) j->dinfo.do_fancy_upsampling=FALSE; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame^] | 489 | if(flags&TJ_YUV) j->dinfo.raw_data_out=TRUE; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 490 | |
| 491 | jpeg_start_decompress(&j->dinfo); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame^] | 492 | if(flags&TJ_YUV) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 493 | { |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame^] | 494 | for(row=0; row<j->dinfo.output_height; |
| 495 | row+=j->dinfo.max_v_samp_factor*DCTSIZE) |
| 496 | { |
| 497 | JSAMPARRAY yuvptr[MAX_COMPONENTS]; |
| 498 | for(i=0; i<j->dinfo.num_components; i++) |
| 499 | { |
| 500 | jpeg_component_info *compptr=&j->dinfo.comp_info[i]; |
| 501 | yuvptr[i]=&outbuf[i][row*compptr->v_samp_factor/j->dinfo.max_v_samp_factor]; |
| 502 | } |
| 503 | jpeg_read_raw_data(&j->dinfo, yuvptr, j->dinfo.max_v_samp_factor*DCTSIZE); |
| 504 | } |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | while(j->dinfo.output_scanline<j->dinfo.output_height) |
| 509 | { |
| 510 | jpeg_read_scanlines(&j->dinfo, &row_pointer[j->dinfo.output_scanline], |
| 511 | j->dinfo.output_height-j->dinfo.output_scanline); |
| 512 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 513 | } |
| 514 | jpeg_finish_decompress(&j->dinfo); |
| 515 | |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame^] | 516 | for(i=0; i<MAX_COMPONENTS; i++) |
| 517 | if(outbuf[i]) free(outbuf[i]); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 518 | if(row_pointer) free(row_pointer); |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | |
| 523 | // General |
| 524 | |
| 525 | DLLEXPORT char* DLLCALL tjGetErrorStr(void) |
| 526 | { |
| 527 | return lasterror; |
| 528 | } |
| 529 | |
| 530 | DLLEXPORT int DLLCALL tjDestroy(tjhandle h) |
| 531 | { |
| 532 | checkhandle(h); |
| 533 | if(setjmp(j->jerr.jb)) return -1; |
| 534 | if(j->initc) jpeg_destroy_compress(&j->cinfo); |
| 535 | if(j->initd) jpeg_destroy_decompress(&j->dinfo); |
| 536 | free(j); |
| 537 | return 0; |
| 538 | } |