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 | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 3 | * Copyright (C)2009-2011 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 | 2a2e451 | 2011-01-05 22:33:24 +0000 | [diff] [blame] | 26 | |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 27 | #ifndef min |
| 28 | #define min(a,b) ((a)<(b)?(a):(b)) |
| 29 | #endif |
| 30 | |
| 31 | #define PAD(v, p) ((v+(p)-1)&(~((p)-1))) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 32 | |
| 33 | |
| 34 | // Error handling |
| 35 | |
| 36 | static char lasterror[JMSG_LENGTH_MAX]="No error"; |
| 37 | |
| 38 | typedef struct _error_mgr |
| 39 | { |
| 40 | struct jpeg_error_mgr pub; |
| 41 | jmp_buf jb; |
| 42 | } error_mgr; |
| 43 | |
| 44 | static void my_error_exit(j_common_ptr cinfo) |
| 45 | { |
| 46 | error_mgr *myerr = (error_mgr *)cinfo->err; |
| 47 | (*cinfo->err->output_message)(cinfo); |
| 48 | longjmp(myerr->jb, 1); |
| 49 | } |
| 50 | |
| 51 | static void my_output_message(j_common_ptr cinfo) |
| 52 | { |
| 53 | (*cinfo->err->format_message)(cinfo, lasterror); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | // Global structures, macros, etc. |
| 58 | |
| 59 | typedef struct _jpgstruct |
| 60 | { |
| 61 | struct jpeg_compress_struct cinfo; |
| 62 | struct jpeg_decompress_struct dinfo; |
| 63 | struct jpeg_destination_mgr jdms; |
| 64 | struct jpeg_source_mgr jsms; |
| 65 | error_mgr jerr; |
| 66 | int initc, initd; |
| 67 | } jpgstruct; |
| 68 | |
| 69 | static const int hsampfactor[NUMSUBOPT]={1, 2, 2, 1}; |
| 70 | static const int vsampfactor[NUMSUBOPT]={1, 1, 2, 1}; |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 71 | static const int pixelsize[NUMSUBOPT]={3, 3, 3, 1}; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 72 | |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 73 | #define _throw(c) {sprintf(lasterror, "%s", c); retval=-1; goto bailout;} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 74 | #define checkhandle(h) jpgstruct *j=(jpgstruct *)h; \ |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 75 | if(!j) {sprintf(lasterror, "Invalid handle"); return -1;} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | // CO |
| 79 | |
| 80 | static boolean empty_output_buffer(struct jpeg_compress_struct *cinfo) |
| 81 | { |
| 82 | ERREXIT(cinfo, JERR_BUFFER_SIZE); |
| 83 | return TRUE; |
| 84 | } |
| 85 | |
| 86 | static void destination_noop(struct jpeg_compress_struct *cinfo) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | DLLEXPORT tjhandle DLLCALL tjInitCompress(void) |
| 91 | { |
| 92 | jpgstruct *j=NULL; |
| 93 | if((j=(jpgstruct *)malloc(sizeof(jpgstruct)))==NULL) |
| 94 | {sprintf(lasterror, "Memory allocation failure"); return NULL;} |
| 95 | memset(j, 0, sizeof(jpgstruct)); |
| 96 | j->cinfo.err=jpeg_std_error(&j->jerr.pub); |
| 97 | j->jerr.pub.error_exit=my_error_exit; |
| 98 | j->jerr.pub.output_message=my_output_message; |
| 99 | |
| 100 | if(setjmp(j->jerr.jb)) |
| 101 | { // this will execute if LIBJPEG has an error |
| 102 | if(j) free(j); return NULL; |
DRC | efa4ddc | 2010-10-13 19:22:50 +0000 | [diff] [blame] | 103 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 104 | |
| 105 | jpeg_create_compress(&j->cinfo); |
| 106 | j->cinfo.dest=&j->jdms; |
| 107 | j->jdms.init_destination=destination_noop; |
| 108 | j->jdms.empty_output_buffer=empty_output_buffer; |
| 109 | j->jdms.term_destination=destination_noop; |
| 110 | |
| 111 | j->initc=1; |
| 112 | return (tjhandle)j; |
| 113 | } |
| 114 | |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame^] | 115 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 116 | DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height) |
| 117 | { |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame^] | 118 | unsigned long retval=0; |
| 119 | if(width<1 || height<1) |
| 120 | _throw("Invalid argument in TJBUFSIZE()"); |
| 121 | |
| 122 | // This allows for rare corner cases in which a JPEG image can actually be |
| 123 | // larger than the uncompressed input (we wouldn't mention it if it hadn't |
| 124 | // happened before.) |
| 125 | retval=((width+15)&(~15)) * ((height+15)&(~15)) * 6 + 2048; |
| 126 | |
| 127 | bailout: |
| 128 | return retval; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 129 | } |
| 130 | |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame^] | 131 | |
| 132 | DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height, |
| 133 | int subsamp) |
| 134 | { |
| 135 | unsigned long retval=0; |
| 136 | int pw, ph, cw, ch; |
| 137 | if(width<1 || height<1 || subsamp<0 || subsamp>=NUMSUBOPT) |
| 138 | _throw("Invalid argument in TJBUFSIZEYUV()"); |
| 139 | pw=PAD(width, hsampfactor[subsamp]); |
| 140 | ph=PAD(height, vsampfactor[subsamp]); |
| 141 | cw=pw/hsampfactor[subsamp]; ch=ph/vsampfactor[subsamp]; |
| 142 | retval=PAD(pw, 4)*ph + (subsamp==TJ_GRAYSCALE? 0:PAD(cw, 4)*ch*2); |
| 143 | |
| 144 | bailout: |
| 145 | return retval; |
| 146 | } |
| 147 | |
| 148 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 149 | DLLEXPORT int DLLCALL tjCompress(tjhandle h, |
| 150 | unsigned char *srcbuf, int width, int pitch, int height, int ps, |
| 151 | unsigned char *dstbuf, unsigned long *size, |
| 152 | int jpegsub, int qual, int flags) |
| 153 | { |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 154 | int i, retval=0; JSAMPROW *row_pointer=NULL; |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 155 | JSAMPLE *_tmpbuf[MAX_COMPONENTS], *_tmpbuf2[MAX_COMPONENTS]; |
| 156 | JSAMPROW *tmpbuf[MAX_COMPONENTS], *tmpbuf2[MAX_COMPONENTS]; |
| 157 | JSAMPROW *outbuf[MAX_COMPONENTS]; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 158 | |
| 159 | checkhandle(h); |
| 160 | |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 161 | for(i=0; i<MAX_COMPONENTS; i++) |
| 162 | { |
| 163 | tmpbuf[i]=NULL; _tmpbuf[i]=NULL; |
| 164 | tmpbuf2[i]=NULL; _tmpbuf2[i]=NULL; outbuf[i]=NULL; |
| 165 | } |
| 166 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 167 | if(srcbuf==NULL || width<=0 || pitch<0 || height<=0 |
| 168 | || dstbuf==NULL || size==NULL |
| 169 | || jpegsub<0 || jpegsub>=NUMSUBOPT || qual<0 || qual>100) |
| 170 | _throw("Invalid argument in tjCompress()"); |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 171 | if(ps!=3 && ps!=4 && ps!=1) |
| 172 | _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] | 173 | if(!j->initc) _throw("Instance has not been initialized for compression"); |
| 174 | |
| 175 | if(pitch==0) pitch=width*ps; |
| 176 | |
| 177 | j->cinfo.image_width = width; |
| 178 | j->cinfo.image_height = height; |
| 179 | j->cinfo.input_components = ps; |
| 180 | |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 181 | if(ps==1) j->cinfo.in_color_space = JCS_GRAYSCALE; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 182 | #if JCS_EXTENSIONS==1 |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 183 | else j->cinfo.in_color_space = JCS_EXT_RGB; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 184 | if(ps==3 && (flags&TJ_BGR)) |
| 185 | j->cinfo.in_color_space = JCS_EXT_BGR; |
| 186 | else if(ps==4 && !(flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST)) |
| 187 | j->cinfo.in_color_space = JCS_EXT_RGBX; |
| 188 | else if(ps==4 && (flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST)) |
| 189 | j->cinfo.in_color_space = JCS_EXT_BGRX; |
| 190 | else if(ps==4 && (flags&TJ_BGR) && (flags&TJ_ALPHAFIRST)) |
| 191 | j->cinfo.in_color_space = JCS_EXT_XBGR; |
| 192 | else if(ps==4 && !(flags&TJ_BGR) && (flags&TJ_ALPHAFIRST)) |
| 193 | j->cinfo.in_color_space = JCS_EXT_XRGB; |
| 194 | #else |
| 195 | #error "TurboJPEG requires JPEG colorspace extensions" |
| 196 | #endif |
| 197 | |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 198 | if(flags&TJ_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 199 | else if(flags&TJ_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 200 | else if(flags&TJ_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
| 201 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 202 | if(setjmp(j->jerr.jb)) |
| 203 | { // this will execute if LIBJPEG has an error |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 204 | retval=-1; |
| 205 | goto bailout; |
DRC | efa4ddc | 2010-10-13 19:22:50 +0000 | [diff] [blame] | 206 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 207 | |
| 208 | jpeg_set_defaults(&j->cinfo); |
| 209 | |
| 210 | jpeg_set_quality(&j->cinfo, qual, TRUE); |
| 211 | if(jpegsub==TJ_GRAYSCALE) |
| 212 | jpeg_set_colorspace(&j->cinfo, JCS_GRAYSCALE); |
| 213 | else |
| 214 | jpeg_set_colorspace(&j->cinfo, JCS_YCbCr); |
DRC | e1716b8 | 2011-02-18 03:19:43 +0000 | [diff] [blame] | 215 | if(qual>=96) j->cinfo.dct_method=JDCT_ISLOW; |
| 216 | else j->cinfo.dct_method=JDCT_FASTEST; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 217 | |
| 218 | j->cinfo.comp_info[0].h_samp_factor=hsampfactor[jpegsub]; |
| 219 | j->cinfo.comp_info[1].h_samp_factor=1; |
| 220 | j->cinfo.comp_info[2].h_samp_factor=1; |
| 221 | j->cinfo.comp_info[0].v_samp_factor=vsampfactor[jpegsub]; |
| 222 | j->cinfo.comp_info[1].v_samp_factor=1; |
| 223 | j->cinfo.comp_info[2].v_samp_factor=1; |
| 224 | |
| 225 | j->jdms.next_output_byte = dstbuf; |
| 226 | j->jdms.free_in_buffer = TJBUFSIZE(j->cinfo.image_width, j->cinfo.image_height); |
| 227 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 228 | jpeg_start_compress(&j->cinfo, TRUE); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 229 | if(flags&TJ_YUV) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 230 | { |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 231 | j_compress_ptr cinfo=&j->cinfo; |
| 232 | int row; |
| 233 | int pw=PAD(width, cinfo->max_h_samp_factor); |
| 234 | int ph=PAD(height, cinfo->max_v_samp_factor); |
| 235 | int cw[MAX_COMPONENTS], ch[MAX_COMPONENTS]; |
| 236 | jpeg_component_info *compptr; |
| 237 | JSAMPLE *ptr=dstbuf; unsigned long yuvsize=0; |
| 238 | |
| 239 | if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph))==NULL) |
| 240 | _throw("Memory allocation failed in tjCompress()"); |
| 241 | for(i=0; i<height; i++) |
| 242 | { |
| 243 | if(flags&TJ_BOTTOMUP) row_pointer[i]= &srcbuf[(height-i-1)*pitch]; |
| 244 | else row_pointer[i]= &srcbuf[i*pitch]; |
| 245 | } |
| 246 | if(height<ph) |
| 247 | for(i=height; i<ph; i++) row_pointer[i]=row_pointer[height-1]; |
| 248 | |
| 249 | for(i=0; i<cinfo->num_components; i++) |
| 250 | { |
| 251 | compptr=&cinfo->comp_info[i]; |
DRC | 5742307 | 2011-01-05 23:35:53 +0000 | [diff] [blame] | 252 | _tmpbuf[i]=(JSAMPLE *)malloc( |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 253 | PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE) |
DRC | 5742307 | 2011-01-05 23:35:53 +0000 | [diff] [blame] | 254 | /compptr->h_samp_factor, 16) * cinfo->max_v_samp_factor + 16); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 255 | if(!_tmpbuf[i]) _throw("Memory allocation failure"); |
DRC | 2a2e451 | 2011-01-05 22:33:24 +0000 | [diff] [blame] | 256 | tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*cinfo->max_v_samp_factor); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 257 | if(!tmpbuf[i]) _throw("Memory allocation failure"); |
| 258 | for(row=0; row<cinfo->max_v_samp_factor; row++) |
DRC | 5742307 | 2011-01-05 23:35:53 +0000 | [diff] [blame] | 259 | { |
| 260 | unsigned char *_tmpbuf_aligned= |
| 261 | (unsigned char *)PAD((size_t)_tmpbuf[i], 16); |
| 262 | tmpbuf[i][row]=&_tmpbuf_aligned[ |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 263 | PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE) |
| 264 | /compptr->h_samp_factor, 16) * row]; |
DRC | 5742307 | 2011-01-05 23:35:53 +0000 | [diff] [blame] | 265 | } |
| 266 | _tmpbuf2[i]=(JSAMPLE *)malloc(PAD(compptr->width_in_blocks*DCTSIZE, 16) |
| 267 | * compptr->v_samp_factor + 16); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 268 | if(!_tmpbuf2[i]) _throw("Memory allocation failure"); |
DRC | 2a2e451 | 2011-01-05 22:33:24 +0000 | [diff] [blame] | 269 | tmpbuf2[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*compptr->v_samp_factor); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 270 | if(!tmpbuf2[i]) _throw("Memory allocation failure"); |
| 271 | for(row=0; row<compptr->v_samp_factor; row++) |
DRC | 5742307 | 2011-01-05 23:35:53 +0000 | [diff] [blame] | 272 | { |
| 273 | unsigned char *_tmpbuf2_aligned= |
| 274 | (unsigned char *)PAD((size_t)_tmpbuf2[i], 16); |
| 275 | tmpbuf2[i][row]=&_tmpbuf2_aligned[ |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 276 | PAD(compptr->width_in_blocks*DCTSIZE, 16) * row]; |
DRC | 5742307 | 2011-01-05 23:35:53 +0000 | [diff] [blame] | 277 | } |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 278 | cw[i]=pw*compptr->h_samp_factor/cinfo->max_h_samp_factor; |
| 279 | ch[i]=ph*compptr->v_samp_factor/cinfo->max_v_samp_factor; |
DRC | 2a2e451 | 2011-01-05 22:33:24 +0000 | [diff] [blame] | 280 | outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 281 | if(!outbuf[i]) _throw("Memory allocation failure"); |
| 282 | for(row=0; row<ch[i]; row++) |
| 283 | { |
| 284 | outbuf[i][row]=ptr; |
| 285 | ptr+=PAD(cw[i], 4); |
| 286 | } |
| 287 | } |
| 288 | yuvsize=(unsigned long)(ptr-dstbuf); |
| 289 | |
| 290 | for(row=0; row<ph; row+=cinfo->max_v_samp_factor) |
| 291 | { |
| 292 | (*cinfo->cconvert->color_convert)(cinfo, &row_pointer[row], tmpbuf, |
| 293 | 0, cinfo->max_v_samp_factor); |
| 294 | (cinfo->downsample->downsample)(cinfo, tmpbuf, 0, tmpbuf2, 0); |
| 295 | for(i=0, compptr=cinfo->comp_info; i<cinfo->num_components; |
| 296 | i++, compptr++) |
| 297 | jcopy_sample_rows(tmpbuf2[i], 0, outbuf[i], |
| 298 | row*compptr->v_samp_factor/cinfo->max_v_samp_factor, |
| 299 | compptr->v_samp_factor, cw[i]); |
| 300 | } |
| 301 | *size=yuvsize; |
| 302 | cinfo->next_scanline+=height; |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL) |
| 307 | _throw("Memory allocation failed in tjCompress()"); |
| 308 | for(i=0; i<height; i++) |
| 309 | { |
| 310 | if(flags&TJ_BOTTOMUP) row_pointer[i]= &srcbuf[(height-i-1)*pitch]; |
| 311 | else row_pointer[i]= &srcbuf[i*pitch]; |
| 312 | } |
| 313 | while(j->cinfo.next_scanline<j->cinfo.image_height) |
| 314 | { |
| 315 | jpeg_write_scanlines(&j->cinfo, &row_pointer[j->cinfo.next_scanline], |
| 316 | j->cinfo.image_height-j->cinfo.next_scanline); |
| 317 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 318 | } |
| 319 | jpeg_finish_compress(&j->cinfo); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 320 | if(!(flags&TJ_YUV)) |
| 321 | *size=TJBUFSIZE(j->cinfo.image_width, j->cinfo.image_height) |
| 322 | -(unsigned long)(j->jdms.free_in_buffer); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 323 | |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 324 | bailout: |
DRC | 582a46a | 2011-02-24 21:16:10 +0000 | [diff] [blame] | 325 | if(j->cinfo.global_state>CSTATE_START) jpeg_abort_compress(&j->cinfo); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 326 | if(row_pointer) free(row_pointer); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 327 | for(i=0; i<MAX_COMPONENTS; i++) |
| 328 | { |
| 329 | if(tmpbuf[i]!=NULL) free(tmpbuf[i]); |
DRC | 5742307 | 2011-01-05 23:35:53 +0000 | [diff] [blame] | 330 | if(_tmpbuf[i]!=NULL) free(_tmpbuf[i]); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 331 | if(tmpbuf2[i]!=NULL) free(tmpbuf2[i]); |
DRC | 5742307 | 2011-01-05 23:35:53 +0000 | [diff] [blame] | 332 | if(_tmpbuf2[i]!=NULL) free(_tmpbuf2[i]); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 333 | if(outbuf[i]!=NULL) free(outbuf[i]); |
| 334 | } |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 335 | return retval; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame^] | 339 | DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle h, |
| 340 | unsigned char *srcbuf, int width, int pitch, int height, int ps, |
| 341 | unsigned char *dstbuf, int subsamp, int flags) |
| 342 | { |
| 343 | unsigned long size; |
| 344 | return tjCompress(h, srcbuf, width, pitch, height, ps, dstbuf, &size, |
| 345 | subsamp, 0, flags|TJ_YUV); |
| 346 | } |
| 347 | |
| 348 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 349 | // DEC |
| 350 | |
| 351 | static boolean fill_input_buffer (struct jpeg_decompress_struct *dinfo) |
| 352 | { |
| 353 | ERREXIT(dinfo, JERR_BUFFER_SIZE); |
| 354 | return TRUE; |
| 355 | } |
| 356 | |
| 357 | static void skip_input_data (struct jpeg_decompress_struct *dinfo, long num_bytes) |
| 358 | { |
| 359 | dinfo->src->next_input_byte += (size_t) num_bytes; |
| 360 | dinfo->src->bytes_in_buffer -= (size_t) num_bytes; |
| 361 | } |
| 362 | |
| 363 | static void source_noop (struct jpeg_decompress_struct *dinfo) |
| 364 | { |
| 365 | } |
| 366 | |
| 367 | DLLEXPORT tjhandle DLLCALL tjInitDecompress(void) |
| 368 | { |
| 369 | jpgstruct *j; |
| 370 | if((j=(jpgstruct *)malloc(sizeof(jpgstruct)))==NULL) |
| 371 | {sprintf(lasterror, "Memory allocation failure"); return NULL;} |
| 372 | memset(j, 0, sizeof(jpgstruct)); |
| 373 | j->dinfo.err=jpeg_std_error(&j->jerr.pub); |
| 374 | j->jerr.pub.error_exit=my_error_exit; |
| 375 | j->jerr.pub.output_message=my_output_message; |
| 376 | |
| 377 | if(setjmp(j->jerr.jb)) |
| 378 | { // this will execute if LIBJPEG has an error |
| 379 | free(j); return NULL; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 380 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 381 | |
| 382 | jpeg_create_decompress(&j->dinfo); |
| 383 | j->dinfo.src=&j->jsms; |
| 384 | j->jsms.init_source=source_noop; |
| 385 | j->jsms.fill_input_buffer = fill_input_buffer; |
| 386 | j->jsms.skip_input_data = skip_input_data; |
| 387 | j->jsms.resync_to_restart = jpeg_resync_to_restart; |
| 388 | j->jsms.term_source = source_noop; |
| 389 | |
| 390 | j->initd=1; |
| 391 | return (tjhandle)j; |
| 392 | } |
| 393 | |
| 394 | |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 395 | DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle h, |
| 396 | unsigned char *srcbuf, unsigned long size, |
| 397 | int *width, int *height, int *jpegsub) |
| 398 | { |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 399 | int i, k, retval=0; |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 400 | |
| 401 | checkhandle(h); |
| 402 | |
| 403 | if(srcbuf==NULL || size<=0 || width==NULL || height==NULL || jpegsub==NULL) |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 404 | _throw("Invalid argument in tjDecompressHeader2()"); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 405 | if(!j->initd) _throw("Instance has not been initialized for decompression"); |
| 406 | |
| 407 | if(setjmp(j->jerr.jb)) |
| 408 | { // this will execute if LIBJPEG has an error |
| 409 | return -1; |
| 410 | } |
| 411 | |
| 412 | j->jsms.bytes_in_buffer = size; |
| 413 | j->jsms.next_input_byte = srcbuf; |
| 414 | |
| 415 | jpeg_read_header(&j->dinfo, TRUE); |
| 416 | |
| 417 | *width=j->dinfo.image_width; *height=j->dinfo.image_height; |
| 418 | *jpegsub=-1; |
| 419 | for(i=0; i<NUMSUBOPT; i++) |
| 420 | { |
| 421 | if(j->dinfo.num_components==pixelsize[i]) |
| 422 | { |
| 423 | if(j->dinfo.comp_info[0].h_samp_factor==hsampfactor[i] |
| 424 | && j->dinfo.comp_info[0].v_samp_factor==vsampfactor[i]) |
| 425 | { |
| 426 | int match=0; |
| 427 | for(k=1; k<j->dinfo.num_components; k++) |
| 428 | { |
| 429 | if(j->dinfo.comp_info[k].h_samp_factor==1 |
| 430 | && j->dinfo.comp_info[k].v_samp_factor==1) |
| 431 | match++; |
| 432 | } |
| 433 | if(match==j->dinfo.num_components-1) |
| 434 | { |
| 435 | *jpegsub=i; break; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | jpeg_abort_decompress(&j->dinfo); |
| 442 | |
| 443 | if(*jpegsub<0) _throw("Could not determine subsampling type for JPEG image"); |
| 444 | if(*width<1 || *height<1) _throw("Invalid data returned in header"); |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 445 | |
| 446 | bailout: |
| 447 | return retval; |
| 448 | } |
| 449 | |
| 450 | |
| 451 | DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle h, |
| 452 | unsigned char *srcbuf, unsigned long size, |
| 453 | int *width, int *height) |
| 454 | { |
| 455 | int jpegsub; |
| 456 | return tjDecompressHeader2(h, srcbuf, size, width, height, &jpegsub); |
DRC | 1fe80f8 | 2010-12-14 01:21:29 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 460 | DLLEXPORT int DLLCALL tjDecompress(tjhandle h, |
| 461 | unsigned char *srcbuf, unsigned long size, |
| 462 | unsigned char *dstbuf, int width, int pitch, int height, int ps, |
| 463 | int flags) |
| 464 | { |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 465 | int i, row, retval=0; JSAMPROW *row_pointer=NULL, *outbuf[MAX_COMPONENTS]; |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 466 | int cw[MAX_COMPONENTS], ch[MAX_COMPONENTS], iw[MAX_COMPONENTS], |
| 467 | tmpbufsize=0, usetmpbuf=0, th[MAX_COMPONENTS]; |
| 468 | JSAMPLE *_tmpbuf=NULL; JSAMPROW *tmpbuf[MAX_COMPONENTS]; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 469 | |
| 470 | checkhandle(h); |
| 471 | |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 472 | for(i=0; i<MAX_COMPONENTS; i++) |
| 473 | { |
| 474 | tmpbuf[i]=NULL; outbuf[i]=NULL; |
| 475 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 476 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 477 | if(srcbuf==NULL || size<=0 |
| 478 | || dstbuf==NULL || width<=0 || pitch<0 || height<=0) |
| 479 | _throw("Invalid argument in tjDecompress()"); |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 480 | if(ps!=3 && ps!=4 && ps!=1) |
| 481 | _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] | 482 | if(!j->initd) _throw("Instance has not been initialized for decompression"); |
| 483 | |
| 484 | if(pitch==0) pitch=width*ps; |
| 485 | |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 486 | if(flags&TJ_FORCEMMX) putenv("JSIMD_FORCEMMX=1"); |
| 487 | else if(flags&TJ_FORCESSE) putenv("JSIMD_FORCESSE=1"); |
| 488 | else if(flags&TJ_FORCESSE2) putenv("JSIMD_FORCESSE2=1"); |
| 489 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 490 | if(setjmp(j->jerr.jb)) |
| 491 | { // this will execute if LIBJPEG has an error |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 492 | retval=-1; |
| 493 | goto bailout; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 494 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 495 | |
| 496 | j->jsms.bytes_in_buffer = size; |
| 497 | j->jsms.next_input_byte = srcbuf; |
| 498 | |
| 499 | jpeg_read_header(&j->dinfo, TRUE); |
| 500 | |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 501 | if(flags&TJ_YUV) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 502 | { |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 503 | j_decompress_ptr dinfo=&j->dinfo; |
| 504 | JSAMPLE *ptr=dstbuf; |
| 505 | |
| 506 | for(i=0; i<dinfo->num_components; i++) |
| 507 | { |
| 508 | jpeg_component_info *compptr=&dinfo->comp_info[i]; |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 509 | int ih; |
| 510 | iw[i]=compptr->width_in_blocks*DCTSIZE; |
| 511 | ih=compptr->height_in_blocks*DCTSIZE; |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame^] | 512 | cw[i]=PAD(dinfo->image_width, dinfo->max_h_samp_factor) |
| 513 | *compptr->h_samp_factor/dinfo->max_h_samp_factor; |
| 514 | ch[i]=PAD(dinfo->image_height, dinfo->max_v_samp_factor) |
| 515 | *compptr->v_samp_factor/dinfo->max_v_samp_factor; |
DRC | a6f4fca | 2010-12-11 06:01:11 +0000 | [diff] [blame] | 516 | if(iw[i]!=cw[i] || ih!=ch[i]) usetmpbuf=1; |
| 517 | th[i]=compptr->v_samp_factor*DCTSIZE; |
| 518 | tmpbufsize+=iw[i]*th[i]; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 519 | if((outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]))==NULL) |
| 520 | _throw("Memory allocation failed in tjInitDecompress()"); |
| 521 | for(row=0; row<ch[i]; row++) |
| 522 | { |
| 523 | outbuf[i][row]=ptr; |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 524 | ptr+=PAD(cw[i], 4); |
| 525 | } |
| 526 | } |
| 527 | if(usetmpbuf) |
| 528 | { |
| 529 | if((_tmpbuf=(JSAMPLE *)malloc(sizeof(JSAMPLE)*tmpbufsize))==NULL) |
| 530 | _throw("Memory allocation failed in tjInitDecompress()"); |
| 531 | ptr=_tmpbuf; |
| 532 | for(i=0; i<dinfo->num_components; i++) |
| 533 | { |
| 534 | jpeg_component_info *compptr=&dinfo->comp_info[i]; |
| 535 | if((tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*th[i]))==NULL) |
| 536 | _throw("Memory allocation failed in tjInitDecompress()"); |
| 537 | for(row=0; row<th[i]; row++) |
| 538 | { |
| 539 | tmpbuf[i][row]=ptr; |
| 540 | ptr+=iw[i]; |
| 541 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | } |
| 545 | else |
| 546 | { |
| 547 | if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL) |
| 548 | _throw("Memory allocation failed in tjInitDecompress()"); |
| 549 | for(i=0; i<height; i++) |
| 550 | { |
| 551 | if(flags&TJ_BOTTOMUP) row_pointer[i]= &dstbuf[(height-i-1)*pitch]; |
| 552 | else row_pointer[i]= &dstbuf[i*pitch]; |
| 553 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 554 | } |
| 555 | |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 556 | if(ps==1) j->dinfo.out_color_space = JCS_GRAYSCALE; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 557 | #if JCS_EXTENSIONS==1 |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 558 | else j->dinfo.out_color_space = JCS_EXT_RGB; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 559 | if(ps==3 && (flags&TJ_BGR)) |
| 560 | j->dinfo.out_color_space = JCS_EXT_BGR; |
| 561 | else if(ps==4 && !(flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST)) |
| 562 | j->dinfo.out_color_space = JCS_EXT_RGBX; |
| 563 | else if(ps==4 && (flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST)) |
| 564 | j->dinfo.out_color_space = JCS_EXT_BGRX; |
| 565 | else if(ps==4 && (flags&TJ_BGR) && (flags&TJ_ALPHAFIRST)) |
| 566 | j->dinfo.out_color_space = JCS_EXT_XBGR; |
| 567 | else if(ps==4 && !(flags&TJ_BGR) && (flags&TJ_ALPHAFIRST)) |
| 568 | j->dinfo.out_color_space = JCS_EXT_XRGB; |
| 569 | #else |
| 570 | #error "TurboJPEG requires JPEG colorspace extensions" |
| 571 | #endif |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 572 | |
DRC | 61e51f9 | 2009-04-05 21:53:20 +0000 | [diff] [blame] | 573 | if(flags&TJ_FASTUPSAMPLE) j->dinfo.do_fancy_upsampling=FALSE; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 574 | if(flags&TJ_YUV) j->dinfo.raw_data_out=TRUE; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 575 | |
| 576 | jpeg_start_decompress(&j->dinfo); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 577 | if(flags&TJ_YUV) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 578 | { |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 579 | j_decompress_ptr dinfo=&j->dinfo; |
| 580 | for(row=0; row<dinfo->output_height; |
| 581 | row+=dinfo->max_v_samp_factor*DCTSIZE) |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 582 | { |
| 583 | JSAMPARRAY yuvptr[MAX_COMPONENTS]; |
DRC | a6f4fca | 2010-12-11 06:01:11 +0000 | [diff] [blame] | 584 | int crow[MAX_COMPONENTS]; |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 585 | for(i=0; i<dinfo->num_components; i++) |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 586 | { |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 587 | jpeg_component_info *compptr=&dinfo->comp_info[i]; |
DRC | a6f4fca | 2010-12-11 06:01:11 +0000 | [diff] [blame] | 588 | crow[i]=row*compptr->v_samp_factor/dinfo->max_v_samp_factor; |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 589 | if(usetmpbuf) yuvptr[i]=tmpbuf[i]; |
DRC | a6f4fca | 2010-12-11 06:01:11 +0000 | [diff] [blame] | 590 | else yuvptr[i]=&outbuf[i][crow[i]]; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 591 | } |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 592 | jpeg_read_raw_data(dinfo, yuvptr, dinfo->max_v_samp_factor*DCTSIZE); |
| 593 | if(usetmpbuf) |
| 594 | { |
| 595 | int j; |
| 596 | for(i=0; i<dinfo->num_components; i++) |
| 597 | { |
| 598 | jpeg_component_info *compptr=&dinfo->comp_info[i]; |
DRC | a6f4fca | 2010-12-11 06:01:11 +0000 | [diff] [blame] | 599 | for(j=0; j<min(th[i], ch[i]-crow[i]); j++) |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 600 | { |
DRC | a6f4fca | 2010-12-11 06:01:11 +0000 | [diff] [blame] | 601 | memcpy(outbuf[i][crow[i]+j], tmpbuf[i][j], cw[i]); |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | else |
| 608 | { |
| 609 | while(j->dinfo.output_scanline<j->dinfo.output_height) |
| 610 | { |
| 611 | jpeg_read_scanlines(&j->dinfo, &row_pointer[j->dinfo.output_scanline], |
| 612 | j->dinfo.output_height-j->dinfo.output_scanline); |
| 613 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 614 | } |
| 615 | jpeg_finish_decompress(&j->dinfo); |
| 616 | |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 617 | bailout: |
DRC | 582a46a | 2011-02-24 21:16:10 +0000 | [diff] [blame] | 618 | if(j->dinfo.global_state>DSTATE_START) jpeg_abort_decompress(&j->dinfo); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 619 | for(i=0; i<MAX_COMPONENTS; i++) |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 620 | { |
| 621 | if(tmpbuf[i]) free(tmpbuf[i]); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 622 | if(outbuf[i]) free(outbuf[i]); |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 623 | } |
| 624 | if(_tmpbuf) free(_tmpbuf); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 625 | if(row_pointer) free(row_pointer); |
DRC | 91e86ba | 2011-02-15 05:24:08 +0000 | [diff] [blame] | 626 | return retval; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame^] | 630 | DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle h, |
| 631 | unsigned char *srcbuf, unsigned long size, |
| 632 | unsigned char *dstbuf, int flags) |
| 633 | { |
| 634 | return tjDecompress(h, srcbuf, size, dstbuf, 1, 0, 1, 3, flags|TJ_YUV); |
| 635 | } |
| 636 | |
| 637 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 638 | // General |
| 639 | |
| 640 | DLLEXPORT char* DLLCALL tjGetErrorStr(void) |
| 641 | { |
| 642 | return lasterror; |
| 643 | } |
| 644 | |
| 645 | DLLEXPORT int DLLCALL tjDestroy(tjhandle h) |
| 646 | { |
| 647 | checkhandle(h); |
| 648 | if(setjmp(j->jerr.jb)) return -1; |
| 649 | if(j->initc) jpeg_destroy_compress(&j->cinfo); |
| 650 | if(j->initd) jpeg_destroy_decompress(&j->dinfo); |
| 651 | free(j); |
| 652 | return 0; |
| 653 | } |