blob: 7ce222e75f6b1bdd50b92880cb5c638ffad47a0d [file] [log] [blame]
DRC9b28def2011-05-21 14:37:15 +00001/*
2 * Copyright (C)2009-2011 D. R. Commander. All Rights Reserved.
DRC2e7b76b2009-04-03 12:04:24 +00003 *
DRC9b28def2011-05-21 14:37:15 +00004 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
DRC2e7b76b2009-04-03 12:04:24 +00006 *
DRC9b28def2011-05-21 14:37:15 +00007 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the libjpeg-turbo Project nor the names of its
13 * contributors may be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
DRC2e7b76b2009-04-03 12:04:24 +000027 */
28
DRC9b28def2011-05-21 14:37:15 +000029/* TurboJPEG/OSS: this implements the TurboJPEG API using libjpeg-turbo */
DRC2e7b76b2009-04-03 12:04:24 +000030
31#include <stdio.h>
32#include <stdlib.h>
DRCfbb67472010-11-24 04:02:37 +000033#define JPEG_INTERNALS
DRC2e7b76b2009-04-03 12:04:24 +000034#include <jpeglib.h>
35#include <jerror.h>
36#include <setjmp.h>
DRC007a42c2011-05-22 13:55:56 +000037#include <jinclude.h>
DRC2e7b76b2009-04-03 12:04:24 +000038#include "./turbojpeg.h"
DRCda5220a2011-03-02 02:17:30 +000039#include "./rrutil.h"
DRC890f1e02011-02-26 22:02:37 +000040#include "transupp.h"
DRC2a2e4512011-01-05 22:33:24 +000041
DRC9b28def2011-05-21 14:37:15 +000042extern void jpeg_mem_dest_tj(j_compress_ptr, unsigned char **,
43 unsigned long *, boolean);
44extern void jpeg_mem_src_tj(j_decompress_ptr, unsigned char *, unsigned long);
45
DRCfbb67472010-11-24 04:02:37 +000046#ifndef min
47 #define min(a,b) ((a)<(b)?(a):(b))
48#endif
49
50#define PAD(v, p) ((v+(p)-1)&(~((p)-1)))
DRC2e7b76b2009-04-03 12:04:24 +000051
52
DRC9b28def2011-05-21 14:37:15 +000053/* Error handling (based on example in example.c) */
DRC2e7b76b2009-04-03 12:04:24 +000054
DRC9b28def2011-05-21 14:37:15 +000055static char errStr[JMSG_LENGTH_MAX]="No error";
DRC2e7b76b2009-04-03 12:04:24 +000056
DRC9b28def2011-05-21 14:37:15 +000057struct my_error_mgr
DRC2e7b76b2009-04-03 12:04:24 +000058{
59 struct jpeg_error_mgr pub;
DRC9b28def2011-05-21 14:37:15 +000060 jmp_buf setjmp_buffer;
61};
62typedef struct my_error_mgr *my_error_ptr;
DRC2e7b76b2009-04-03 12:04:24 +000063
64static void my_error_exit(j_common_ptr cinfo)
65{
DRC9b28def2011-05-21 14:37:15 +000066 my_error_ptr myerr=(my_error_ptr)cinfo->err;
DRC2e7b76b2009-04-03 12:04:24 +000067 (*cinfo->err->output_message)(cinfo);
DRC9b28def2011-05-21 14:37:15 +000068 longjmp(myerr->setjmp_buffer, 1);
DRC2e7b76b2009-04-03 12:04:24 +000069}
70
DRC9b28def2011-05-21 14:37:15 +000071/* Based on output_message() in jerror.c */
72
DRC2e7b76b2009-04-03 12:04:24 +000073static void my_output_message(j_common_ptr cinfo)
74{
DRC9b28def2011-05-21 14:37:15 +000075 (*cinfo->err->format_message)(cinfo, errStr);
DRC2e7b76b2009-04-03 12:04:24 +000076}
77
78
DRC9b28def2011-05-21 14:37:15 +000079/* Global structures, macros, etc. */
DRC2e7b76b2009-04-03 12:04:24 +000080
DRC9b28def2011-05-21 14:37:15 +000081enum {COMPRESS=1, DECOMPRESS=2};
82
83typedef struct _tjinstance
DRC2e7b76b2009-04-03 12:04:24 +000084{
85 struct jpeg_compress_struct cinfo;
86 struct jpeg_decompress_struct dinfo;
DRC9b28def2011-05-21 14:37:15 +000087 struct my_error_mgr jerr;
88 int init;
89} tjinstance;
DRC2e7b76b2009-04-03 12:04:24 +000090
DRC007a42c2011-05-22 13:55:56 +000091static const int pixelsize[TJ_NUMSAMP]={3, 3, 3, 1, 3};
DRC9b28def2011-05-21 14:37:15 +000092
DRC007a42c2011-05-22 13:55:56 +000093static const JXFORM_CODE xformtypes[TJ_NUMXOP]=
94{
DRC890f1e02011-02-26 22:02:37 +000095 JXFORM_NONE, JXFORM_FLIP_H, JXFORM_FLIP_V, JXFORM_TRANSPOSE,
96 JXFORM_TRANSVERSE, JXFORM_ROT_90, JXFORM_ROT_180, JXFORM_ROT_270
97};
DRC9b28def2011-05-21 14:37:15 +000098
DRC109a5782011-03-01 09:53:07 +000099#define NUMSF 4
100static const tjscalingfactor sf[NUMSF]={
101 {1, 1},
102 {1, 2},
103 {1, 4},
104 {1, 8}
105};
DRC2e7b76b2009-04-03 12:04:24 +0000106
DRC9b28def2011-05-21 14:37:15 +0000107#define _throw(c) {snprintf(errStr, JMSG_LENGTH_MAX, "%s", c); \
DRCda5220a2011-03-02 02:17:30 +0000108 retval=-1; goto bailout;}
DRC9b28def2011-05-21 14:37:15 +0000109#define getinstance(handle) tjinstance *this=(tjinstance *)handle; \
110 j_compress_ptr cinfo=NULL; j_decompress_ptr dinfo=NULL; \
111 if(!this) {snprintf(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \
112 return -1;} \
113 cinfo=&this->cinfo; dinfo=&this->dinfo;
DRC2e7b76b2009-04-03 12:04:24 +0000114
DRC9b28def2011-05-21 14:37:15 +0000115static int getPixelFormat(int pixelSize, int flags)
DRC2e7b76b2009-04-03 12:04:24 +0000116{
DRC25b995a2011-05-21 15:34:54 +0000117 if(pixelSize==1) return TJPF_GRAY;
DRC9b28def2011-05-21 14:37:15 +0000118 if(pixelSize==3)
119 {
DRC25b995a2011-05-21 15:34:54 +0000120 if(flags&TJ_BGR) return TJPF_BGR;
121 else return TJPF_RGB;
DRC9b28def2011-05-21 14:37:15 +0000122 }
123 if(pixelSize==4)
124 {
125 if(flags&TJ_ALPHAFIRST)
126 {
DRC25b995a2011-05-21 15:34:54 +0000127 if(flags&TJ_BGR) return TJPF_XBGR;
128 else return TJPF_XRGB;
DRC9b28def2011-05-21 14:37:15 +0000129 }
130 else
131 {
DRC25b995a2011-05-21 15:34:54 +0000132 if(flags&TJ_BGR) return TJPF_BGRX;
133 else return TJPF_RGBX;
DRC9b28def2011-05-21 14:37:15 +0000134 }
135 }
136 return -1;
DRC2e7b76b2009-04-03 12:04:24 +0000137}
138
DRC9b28def2011-05-21 14:37:15 +0000139static void setCompDefaults(struct jpeg_compress_struct *cinfo,
140 int pixelFormat, int subsamp, int jpegQual)
DRC2e7b76b2009-04-03 12:04:24 +0000141{
DRC9b28def2011-05-21 14:37:15 +0000142 switch(pixelFormat)
143 {
DRC25b995a2011-05-21 15:34:54 +0000144 case TJPF_GRAY:
DRC9b28def2011-05-21 14:37:15 +0000145 cinfo->in_color_space=JCS_GRAYSCALE; break;
146 #if JCS_EXTENSIONS==1
DRC25b995a2011-05-21 15:34:54 +0000147 case TJPF_RGB:
DRC9b28def2011-05-21 14:37:15 +0000148 cinfo->in_color_space=JCS_EXT_RGB; break;
DRC25b995a2011-05-21 15:34:54 +0000149 case TJPF_BGR:
DRC9b28def2011-05-21 14:37:15 +0000150 cinfo->in_color_space=JCS_EXT_BGR; break;
DRC25b995a2011-05-21 15:34:54 +0000151 case TJPF_RGBX:
DRC9b28def2011-05-21 14:37:15 +0000152 cinfo->in_color_space=JCS_EXT_RGBX; break;
DRC25b995a2011-05-21 15:34:54 +0000153 case TJPF_BGRX:
DRC9b28def2011-05-21 14:37:15 +0000154 cinfo->in_color_space=JCS_EXT_BGRX; break;
DRC25b995a2011-05-21 15:34:54 +0000155 case TJPF_XRGB:
DRC9b28def2011-05-21 14:37:15 +0000156 cinfo->in_color_space=JCS_EXT_XRGB; break;
DRC25b995a2011-05-21 15:34:54 +0000157 case TJPF_XBGR:
DRC9b28def2011-05-21 14:37:15 +0000158 cinfo->in_color_space=JCS_EXT_XBGR; break;
159 #else
DRC25b995a2011-05-21 15:34:54 +0000160 case TJPF_RGB:
DRC9b28def2011-05-21 14:37:15 +0000161 if(RGB_RED==0 && RGB_GREEN==1 && RGB_BLUE==2 && RGB_PIXELSIZE==3)
162 {
163 cinfo->in_color_space=JCS_RGB; break;
164 }
165 default:
166 _throw("Unsupported pixel format");
167 #endif
DRCefa4ddc2010-10-13 19:22:50 +0000168 }
DRC2e7b76b2009-04-03 12:04:24 +0000169
DRC9b28def2011-05-21 14:37:15 +0000170 cinfo->input_components=tjPixelSize[pixelFormat];
171 jpeg_set_defaults(cinfo);
172 if(jpegQual>=0)
173 {
174 jpeg_set_quality(cinfo, jpegQual, TRUE);
175 if(jpegQual>=96) cinfo->dct_method=JDCT_ISLOW;
176 else cinfo->dct_method=JDCT_FASTEST;
177 }
DRC25b995a2011-05-21 15:34:54 +0000178 if(subsamp==TJSAMP_GRAY)
DRC9b28def2011-05-21 14:37:15 +0000179 jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
180 else
181 jpeg_set_colorspace(cinfo, JCS_YCbCr);
DRC2e7b76b2009-04-03 12:04:24 +0000182
DRC9b28def2011-05-21 14:37:15 +0000183 cinfo->comp_info[0].h_samp_factor=tjMCUWidth[subsamp]/8;
184 cinfo->comp_info[1].h_samp_factor=1;
185 cinfo->comp_info[2].h_samp_factor=1;
186 cinfo->comp_info[0].v_samp_factor=tjMCUHeight[subsamp]/8;
187 cinfo->comp_info[1].v_samp_factor=1;
188 cinfo->comp_info[2].v_samp_factor=1;
189}
190
191static void setDecompDefaults(struct jpeg_decompress_struct *dinfo,
192 int pixelFormat)
193{
194 switch(pixelFormat)
195 {
DRC25b995a2011-05-21 15:34:54 +0000196 case TJPF_GRAY:
DRC9b28def2011-05-21 14:37:15 +0000197 dinfo->out_color_space=JCS_GRAYSCALE; break;
198 #if JCS_EXTENSIONS==1
DRC25b995a2011-05-21 15:34:54 +0000199 case TJPF_RGB:
DRC9b28def2011-05-21 14:37:15 +0000200 dinfo->out_color_space=JCS_EXT_RGB; break;
DRC25b995a2011-05-21 15:34:54 +0000201 case TJPF_BGR:
DRC9b28def2011-05-21 14:37:15 +0000202 dinfo->out_color_space=JCS_EXT_BGR; break;
DRC25b995a2011-05-21 15:34:54 +0000203 case TJPF_RGBX:
DRC9b28def2011-05-21 14:37:15 +0000204 dinfo->out_color_space=JCS_EXT_RGBX; break;
DRC25b995a2011-05-21 15:34:54 +0000205 case TJPF_BGRX:
DRC9b28def2011-05-21 14:37:15 +0000206 dinfo->out_color_space=JCS_EXT_BGRX; break;
DRC25b995a2011-05-21 15:34:54 +0000207 case TJPF_XRGB:
DRC9b28def2011-05-21 14:37:15 +0000208 dinfo->out_color_space=JCS_EXT_XRGB; break;
DRC25b995a2011-05-21 15:34:54 +0000209 case TJPF_XBGR:
DRC9b28def2011-05-21 14:37:15 +0000210 dinfo->out_color_space=JCS_EXT_XBGR; break;
211 #else
DRC25b995a2011-05-21 15:34:54 +0000212 case TJPF_RGB:
DRC9b28def2011-05-21 14:37:15 +0000213 if(RGB_RED==0 && RGB_GREEN==1 && RGB_BLUE==2 && RGB_PIXELSIZE==3)
214 {
215 dinfo->out_color_space=JCS_RGB; break;
216 }
217 default:
218 _throw("Unsupported pixel format");
219 #endif
220 }
221}
222
223
224/* General API functions */
225
226DLLEXPORT char* DLLCALL tjGetErrorStr(void)
227{
228 return errStr;
229}
230
231
232DLLEXPORT int DLLCALL tjDestroy(tjhandle handle)
233{
234 getinstance(handle);
235 if(setjmp(this->jerr.setjmp_buffer)) return -1;
236 if(this->init&COMPRESS) jpeg_destroy_compress(cinfo);
237 if(this->init&DECOMPRESS) jpeg_destroy_decompress(dinfo);
238 free(this);
239 return 0;
240}
241
242
243/* Compressor */
244
245static tjhandle _tjInitCompress(tjinstance *this)
246{
247 unsigned char buffer[1], *buf=buffer; unsigned long size=1;
248
249 /* This is also straight out of example.c */
250 this->cinfo.err=jpeg_std_error(&this->jerr.pub);
251 this->jerr.pub.error_exit=my_error_exit;
252 this->jerr.pub.output_message=my_output_message;
253
254 if(setjmp(this->jerr.setjmp_buffer))
255 {
256 /* If we get here, the JPEG code has signaled an error. */
257 if(this) free(this); return NULL;
258 }
259
260 jpeg_create_compress(&this->cinfo);
261 /* Make an initial call so it will create the destination manager */
262 jpeg_mem_dest_tj(&this->cinfo, &buf, &size, 0);
263
DRC007a42c2011-05-22 13:55:56 +0000264 this->init|=COMPRESS;
DRC9b28def2011-05-21 14:37:15 +0000265 return (tjhandle)this;
DRC2e7b76b2009-04-03 12:04:24 +0000266}
267
DRC890f1e02011-02-26 22:02:37 +0000268DLLEXPORT tjhandle DLLCALL tjInitCompress(void)
269{
DRC9b28def2011-05-21 14:37:15 +0000270 tjinstance *this=NULL;
271 if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL)
DRCda5220a2011-03-02 02:17:30 +0000272 {
DRC007a42c2011-05-22 13:55:56 +0000273 snprintf(errStr, JMSG_LENGTH_MAX,
274 "tjInitCompress(): Memory allocation failure");
DRCda5220a2011-03-02 02:17:30 +0000275 return NULL;
276 }
DRC007a42c2011-05-22 13:55:56 +0000277 MEMZERO(this, sizeof(tjinstance));
DRC9b28def2011-05-21 14:37:15 +0000278 return _tjInitCompress(this);
DRC890f1e02011-02-26 22:02:37 +0000279}
280
DRC84241602011-02-25 02:08:23 +0000281
DRC2e7b76b2009-04-03 12:04:24 +0000282DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height)
283{
DRCf3cf9732011-02-22 00:16:14 +0000284 unsigned long retval=0;
285 if(width<1 || height<1)
DRC007a42c2011-05-22 13:55:56 +0000286 _throw("TJBUFSIZE(): Invalid argument");
DRCf3cf9732011-02-22 00:16:14 +0000287
288 // This allows for rare corner cases in which a JPEG image can actually be
289 // larger than the uncompressed input (we wouldn't mention it if it hadn't
DRCb28fc572011-02-22 06:41:29 +0000290 // happened before.)
DRC007a42c2011-05-22 13:55:56 +0000291 retval=PAD(width, 16) * PAD(height, 16) * 6 + 2048;
DRCf3cf9732011-02-22 00:16:14 +0000292
293 bailout:
294 return retval;
295}
296
DRC84241602011-02-25 02:08:23 +0000297
DRCf3cf9732011-02-22 00:16:14 +0000298DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height,
299 int subsamp)
300{
301 unsigned long retval=0;
302 int pw, ph, cw, ch;
303 if(width<1 || height<1 || subsamp<0 || subsamp>=NUMSUBOPT)
DRC007a42c2011-05-22 13:55:56 +0000304 _throw("TJBUFSIZEYUV(): Invalid argument");
DRC9b28def2011-05-21 14:37:15 +0000305 pw=PAD(width, tjMCUWidth[subsamp]/8);
306 ph=PAD(height, tjMCUHeight[subsamp]/8);
307 cw=pw*8/tjMCUWidth[subsamp]; ch=ph*8/tjMCUHeight[subsamp];
DRC25b995a2011-05-21 15:34:54 +0000308 retval=PAD(pw, 4)*ph + (subsamp==TJSAMP_GRAY? 0:PAD(cw, 4)*ch*2);
DRCf3cf9732011-02-22 00:16:14 +0000309
310 bailout:
311 return retval;
DRC2e7b76b2009-04-03 12:04:24 +0000312}
313
DRC84241602011-02-25 02:08:23 +0000314
DRC9b28def2011-05-21 14:37:15 +0000315DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf,
316 int width, int pitch, int height, int pixelFormat, unsigned char **jpegBuf,
317 unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags)
318{
319 int i, retval=0; JSAMPROW *row_pointer=NULL;
320
321 getinstance(handle)
322 if((this->init&COMPRESS)==0)
DRC007a42c2011-05-22 13:55:56 +0000323 _throw("tjCompress2(): Instance has not been initialized for compression");
DRC9b28def2011-05-21 14:37:15 +0000324
325 if(srcBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0
326 || pixelFormat>=TJ_NUMPF || jpegBuf==NULL || jpegSize==NULL
327 || jpegSubsamp<0 || jpegSubsamp>=NUMSUBOPT || jpegQual<0 || jpegQual>100)
DRC007a42c2011-05-22 13:55:56 +0000328 _throw("tjCompress2(): Invalid argument");
DRC9b28def2011-05-21 14:37:15 +0000329
330 if(setjmp(this->jerr.setjmp_buffer))
331 {
332 /* If we get here, the JPEG code has signaled an error. */
333 retval=-1;
334 goto bailout;
335 }
336
337 if(pitch==0) pitch=width*tjPixelSize[pixelFormat];
338
339 cinfo->image_width=width;
340 cinfo->image_height=height;
341
DRC25b995a2011-05-21 15:34:54 +0000342 if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
343 else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
344 else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
DRC9b28def2011-05-21 14:37:15 +0000345
DRC25b995a2011-05-21 15:34:54 +0000346 jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, (flags&TJFLAG_NOREALLOC)==0);
DRC9b28def2011-05-21 14:37:15 +0000347 setCompDefaults(cinfo, pixelFormat, jpegSubsamp, jpegQual);
348
349 jpeg_start_compress(cinfo, TRUE);
350 if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
DRC007a42c2011-05-22 13:55:56 +0000351 _throw("tjCompress2(): Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000352 for(i=0; i<height; i++)
353 {
DRC25b995a2011-05-21 15:34:54 +0000354 if(flags&TJFLAG_BOTTOMUP) row_pointer[i]=&srcBuf[(height-i-1)*pitch];
DRC9b28def2011-05-21 14:37:15 +0000355 else row_pointer[i]=&srcBuf[i*pitch];
356 }
357 while(cinfo->next_scanline<cinfo->image_height)
358 {
359 jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
360 cinfo->image_height-cinfo->next_scanline);
361 }
362 jpeg_finish_compress(cinfo);
363
364 bailout:
365 if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
366 if(row_pointer) free(row_pointer);
367 return retval;
368}
369
370DLLEXPORT int DLLCALL tjCompress(tjhandle handle, unsigned char *srcBuf,
371 int width, int pitch, int height, int pixelSize, unsigned char *jpegBuf,
372 unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags)
373{
374 int retval=0; unsigned long size;
375 if(flags&TJ_YUV)
376 {
377 size=TJBUFSIZEYUV(width, height, jpegSubsamp);
378 retval=tjEncodeYUV2(handle, srcBuf, width, pitch, height,
379 getPixelFormat(pixelSize, flags), jpegBuf, jpegSubsamp, flags);
380 }
381 else
382 {
383 size=TJBUFSIZE(width, height);
384 retval=tjCompress2(handle, srcBuf, width, pitch, height,
385 getPixelFormat(pixelSize, flags), &jpegBuf, &size, jpegSubsamp, jpegQual,
DRC25b995a2011-05-21 15:34:54 +0000386 flags|TJFLAG_NOREALLOC);
DRC9b28def2011-05-21 14:37:15 +0000387 }
388 *jpegSize=size;
389 return retval;
390}
391
392
393DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf,
394 int width, int pitch, int height, int pixelFormat, unsigned char *dstBuf,
395 int subsamp, int flags)
DRC2e7b76b2009-04-03 12:04:24 +0000396{
DRC91e86ba2011-02-15 05:24:08 +0000397 int i, retval=0; JSAMPROW *row_pointer=NULL;
DRCfbb67472010-11-24 04:02:37 +0000398 JSAMPLE *_tmpbuf[MAX_COMPONENTS], *_tmpbuf2[MAX_COMPONENTS];
399 JSAMPROW *tmpbuf[MAX_COMPONENTS], *tmpbuf2[MAX_COMPONENTS];
400 JSAMPROW *outbuf[MAX_COMPONENTS];
DRC9b28def2011-05-21 14:37:15 +0000401 int row, pw, ph, cw[MAX_COMPONENTS], ch[MAX_COMPONENTS];
402 JSAMPLE *ptr=dstBuf;
403 unsigned long yuvsize=0;
404 jpeg_component_info *compptr;
DRC2e7b76b2009-04-03 12:04:24 +0000405
DRC9b28def2011-05-21 14:37:15 +0000406 getinstance(handle);
407 if((this->init&COMPRESS)==0)
DRC007a42c2011-05-22 13:55:56 +0000408 _throw("tjEncodeYUV2(): Instance has not been initialized for compression");
DRC2e7b76b2009-04-03 12:04:24 +0000409
DRCfbb67472010-11-24 04:02:37 +0000410 for(i=0; i<MAX_COMPONENTS; i++)
411 {
412 tmpbuf[i]=NULL; _tmpbuf[i]=NULL;
413 tmpbuf2[i]=NULL; _tmpbuf2[i]=NULL; outbuf[i]=NULL;
414 }
415
DRC9b28def2011-05-21 14:37:15 +0000416 if(srcBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0
417 || pixelFormat>=TJ_NUMPF || dstBuf==NULL || subsamp<0
418 || subsamp>=NUMSUBOPT)
419 _throw("tjEncodeYUV2(): Invalid argument");
DRC2e7b76b2009-04-03 12:04:24 +0000420
DRC9b28def2011-05-21 14:37:15 +0000421 if(setjmp(this->jerr.setjmp_buffer))
422 {
423 /* If we get here, the JPEG code has signaled an error. */
424 retval=-1;
425 goto bailout;
426 }
DRC2e7b76b2009-04-03 12:04:24 +0000427
DRC9b28def2011-05-21 14:37:15 +0000428 if(pitch==0) pitch=width*tjPixelSize[pixelFormat];
DRC2e7b76b2009-04-03 12:04:24 +0000429
DRC9b28def2011-05-21 14:37:15 +0000430 cinfo->image_width=width;
431 cinfo->image_height=height;
DRC2e7b76b2009-04-03 12:04:24 +0000432
DRC25b995a2011-05-21 15:34:54 +0000433 if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
434 else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
435 else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
DRC0c6a2712010-02-22 08:34:44 +0000436
DRC9b28def2011-05-21 14:37:15 +0000437 yuvsize=TJBUFSIZEYUV(width, height, subsamp);
438 jpeg_mem_dest_tj(cinfo, &dstBuf, &yuvsize, 0);
439 setCompDefaults(cinfo, pixelFormat, subsamp, -1);
DRC2e7b76b2009-04-03 12:04:24 +0000440
DRC9b28def2011-05-21 14:37:15 +0000441 jpeg_start_compress(cinfo, TRUE);
442 pw=PAD(width, cinfo->max_h_samp_factor);
443 ph=PAD(height, cinfo->max_v_samp_factor);
DRC2e7b76b2009-04-03 12:04:24 +0000444
DRC9b28def2011-05-21 14:37:15 +0000445 if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph))==NULL)
DRC007a42c2011-05-22 13:55:56 +0000446 _throw("tjEncodeYUV2(): Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000447 for(i=0; i<height; i++)
DRC2e7b76b2009-04-03 12:04:24 +0000448 {
DRC25b995a2011-05-21 15:34:54 +0000449 if(flags&TJFLAG_BOTTOMUP) row_pointer[i]=&srcBuf[(height-i-1)*pitch];
DRC9b28def2011-05-21 14:37:15 +0000450 else row_pointer[i]=&srcBuf[i*pitch];
451 }
452 if(height<ph)
453 for(i=height; i<ph; i++) row_pointer[i]=row_pointer[height-1];
DRCfbb67472010-11-24 04:02:37 +0000454
DRC9b28def2011-05-21 14:37:15 +0000455 for(i=0; i<cinfo->num_components; i++)
456 {
457 compptr=&cinfo->comp_info[i];
458 _tmpbuf[i]=(JSAMPLE *)malloc(
459 PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE)
460 /compptr->h_samp_factor, 16) * cinfo->max_v_samp_factor + 16);
DRC007a42c2011-05-22 13:55:56 +0000461 if(!_tmpbuf[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000462 tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*cinfo->max_v_samp_factor);
DRC007a42c2011-05-22 13:55:56 +0000463 if(!tmpbuf[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000464 for(row=0; row<cinfo->max_v_samp_factor; row++)
DRCfbb67472010-11-24 04:02:37 +0000465 {
DRC9b28def2011-05-21 14:37:15 +0000466 unsigned char *_tmpbuf_aligned=
467 (unsigned char *)PAD((size_t)_tmpbuf[i], 16);
468 tmpbuf[i][row]=&_tmpbuf_aligned[
DRCfbb67472010-11-24 04:02:37 +0000469 PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE)
DRC9b28def2011-05-21 14:37:15 +0000470 /compptr->h_samp_factor, 16) * row];
DRCfbb67472010-11-24 04:02:37 +0000471 }
DRC9b28def2011-05-21 14:37:15 +0000472 _tmpbuf2[i]=(JSAMPLE *)malloc(PAD(compptr->width_in_blocks*DCTSIZE, 16)
473 * compptr->v_samp_factor + 16);
DRC007a42c2011-05-22 13:55:56 +0000474 if(!_tmpbuf2[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000475 tmpbuf2[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*compptr->v_samp_factor);
DRC007a42c2011-05-22 13:55:56 +0000476 if(!tmpbuf2[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000477 for(row=0; row<compptr->v_samp_factor; row++)
478 {
479 unsigned char *_tmpbuf2_aligned=
480 (unsigned char *)PAD((size_t)_tmpbuf2[i], 16);
481 tmpbuf2[i][row]=&_tmpbuf2_aligned[
482 PAD(compptr->width_in_blocks*DCTSIZE, 16) * row];
483 }
484 cw[i]=pw*compptr->h_samp_factor/cinfo->max_h_samp_factor;
485 ch[i]=ph*compptr->v_samp_factor/cinfo->max_v_samp_factor;
486 outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]);
DRC007a42c2011-05-22 13:55:56 +0000487 if(!outbuf[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000488 for(row=0; row<ch[i]; row++)
489 {
490 outbuf[i][row]=ptr;
491 ptr+=PAD(cw[i], 4);
492 }
493 }
494 if(yuvsize!=(unsigned long)(ptr-dstBuf))
495 _throw("tjEncodeYUV2(): Generated image is not the correct size");
DRCfbb67472010-11-24 04:02:37 +0000496
DRC9b28def2011-05-21 14:37:15 +0000497 for(row=0; row<ph; row+=cinfo->max_v_samp_factor)
DRCfbb67472010-11-24 04:02:37 +0000498 {
DRC9b28def2011-05-21 14:37:15 +0000499 (*cinfo->cconvert->color_convert)(cinfo, &row_pointer[row], tmpbuf, 0,
500 cinfo->max_v_samp_factor);
501 (cinfo->downsample->downsample)(cinfo, tmpbuf, 0, tmpbuf2, 0);
502 for(i=0, compptr=cinfo->comp_info; i<cinfo->num_components; i++, compptr++)
503 jcopy_sample_rows(tmpbuf2[i], 0, outbuf[i],
504 row*compptr->v_samp_factor/cinfo->max_v_samp_factor,
505 compptr->v_samp_factor, cw[i]);
DRC6ee54592011-03-01 08:18:30 +0000506 }
DRC9b28def2011-05-21 14:37:15 +0000507 cinfo->next_scanline+=height;
508 jpeg_abort_compress(cinfo);
DRC2e7b76b2009-04-03 12:04:24 +0000509
DRC91e86ba2011-02-15 05:24:08 +0000510 bailout:
DRC9b28def2011-05-21 14:37:15 +0000511 if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
DRC2e7b76b2009-04-03 12:04:24 +0000512 if(row_pointer) free(row_pointer);
DRCfbb67472010-11-24 04:02:37 +0000513 for(i=0; i<MAX_COMPONENTS; i++)
514 {
515 if(tmpbuf[i]!=NULL) free(tmpbuf[i]);
DRC57423072011-01-05 23:35:53 +0000516 if(_tmpbuf[i]!=NULL) free(_tmpbuf[i]);
DRCfbb67472010-11-24 04:02:37 +0000517 if(tmpbuf2[i]!=NULL) free(tmpbuf2[i]);
DRC57423072011-01-05 23:35:53 +0000518 if(_tmpbuf2[i]!=NULL) free(_tmpbuf2[i]);
DRCfbb67472010-11-24 04:02:37 +0000519 if(outbuf[i]!=NULL) free(outbuf[i]);
520 }
DRC91e86ba2011-02-15 05:24:08 +0000521 return retval;
DRC2e7b76b2009-04-03 12:04:24 +0000522}
523
DRC9b28def2011-05-21 14:37:15 +0000524DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle handle, unsigned char *srcBuf,
525 int width, int pitch, int height, int pixelSize, unsigned char *dstBuf,
526 int subsamp, int flags)
DRC84241602011-02-25 02:08:23 +0000527{
DRC9b28def2011-05-21 14:37:15 +0000528 return tjEncodeYUV2(handle, srcBuf, width, pitch, height,
529 getPixelFormat(pixelSize, flags), dstBuf, subsamp, flags);
DRC84241602011-02-25 02:08:23 +0000530}
531
532
DRC9b28def2011-05-21 14:37:15 +0000533/* Decompressor */
DRC2e7b76b2009-04-03 12:04:24 +0000534
DRC9b28def2011-05-21 14:37:15 +0000535static tjhandle _tjInitDecompress(tjinstance *this)
DRC2e7b76b2009-04-03 12:04:24 +0000536{
DRC9b28def2011-05-21 14:37:15 +0000537 unsigned char buffer[1];
DRC2e7b76b2009-04-03 12:04:24 +0000538
DRC9b28def2011-05-21 14:37:15 +0000539 /* This is also straight out of example.c */
540 this->dinfo.err=jpeg_std_error(&this->jerr.pub);
541 this->jerr.pub.error_exit=my_error_exit;
542 this->jerr.pub.output_message=my_output_message;
DRC2e7b76b2009-04-03 12:04:24 +0000543
DRC9b28def2011-05-21 14:37:15 +0000544 if(setjmp(this->jerr.setjmp_buffer))
545 {
546 /* If we get here, the JPEG code has signaled an error. */
547 if(this) free(this); return NULL;
DRC9e17f7d2010-12-10 04:59:13 +0000548 }
DRC2e7b76b2009-04-03 12:04:24 +0000549
DRC9b28def2011-05-21 14:37:15 +0000550 jpeg_create_decompress(&this->dinfo);
551 /* Make an initial call so it will create the source manager */
552 jpeg_mem_src_tj(&this->dinfo, buffer, 1);
DRC2e7b76b2009-04-03 12:04:24 +0000553
DRC007a42c2011-05-22 13:55:56 +0000554 this->init|=DECOMPRESS;
DRC9b28def2011-05-21 14:37:15 +0000555 return (tjhandle)this;
DRC2e7b76b2009-04-03 12:04:24 +0000556}
557
DRC890f1e02011-02-26 22:02:37 +0000558DLLEXPORT tjhandle DLLCALL tjInitDecompress(void)
559{
DRC9b28def2011-05-21 14:37:15 +0000560 tjinstance *this;
561 if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL)
DRCda5220a2011-03-02 02:17:30 +0000562 {
DRC007a42c2011-05-22 13:55:56 +0000563 snprintf(errStr, JMSG_LENGTH_MAX,
564 "tjInitDecompress(): Memory allocation failure");
DRCda5220a2011-03-02 02:17:30 +0000565 return NULL;
566 }
DRC007a42c2011-05-22 13:55:56 +0000567 MEMZERO(this, sizeof(tjinstance));
DRC9b28def2011-05-21 14:37:15 +0000568 return _tjInitDecompress(this);
DRC890f1e02011-02-26 22:02:37 +0000569}
570
DRC2e7b76b2009-04-03 12:04:24 +0000571
DRC9b28def2011-05-21 14:37:15 +0000572DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle handle,
573 unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height,
574 int *jpegSubsamp)
DRC1fe80f82010-12-14 01:21:29 +0000575{
DRC91e86ba2011-02-15 05:24:08 +0000576 int i, k, retval=0;
DRC1fe80f82010-12-14 01:21:29 +0000577
DRC9b28def2011-05-21 14:37:15 +0000578 getinstance(handle);
579 if((this->init&DECOMPRESS)==0)
DRC007a42c2011-05-22 13:55:56 +0000580 _throw("tjDecompressHeader2(): Instance has not been initialized for decompression");
DRC1fe80f82010-12-14 01:21:29 +0000581
DRC9b28def2011-05-21 14:37:15 +0000582 if(jpegBuf==NULL || jpegSize<=0 || width==NULL || height==NULL
583 || jpegSubsamp==NULL)
584 _throw("tjDecompressHeader2(): Invalid argument");
DRC1fe80f82010-12-14 01:21:29 +0000585
DRC9b28def2011-05-21 14:37:15 +0000586 if(setjmp(this->jerr.setjmp_buffer))
587 {
588 /* If we get here, the JPEG code has signaled an error. */
DRC1fe80f82010-12-14 01:21:29 +0000589 return -1;
590 }
591
DRC9b28def2011-05-21 14:37:15 +0000592 jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
593 jpeg_read_header(dinfo, TRUE);
DRC1fe80f82010-12-14 01:21:29 +0000594
DRC9b28def2011-05-21 14:37:15 +0000595 *width=dinfo->image_width;
596 *height=dinfo->image_height;
597 *jpegSubsamp=-1;
DRC1fe80f82010-12-14 01:21:29 +0000598 for(i=0; i<NUMSUBOPT; i++)
599 {
DRC9b28def2011-05-21 14:37:15 +0000600 if(dinfo->num_components==pixelsize[i])
DRC1fe80f82010-12-14 01:21:29 +0000601 {
DRC9b28def2011-05-21 14:37:15 +0000602 if(dinfo->comp_info[0].h_samp_factor==tjMCUWidth[i]/8
603 && dinfo->comp_info[0].v_samp_factor==tjMCUHeight[i]/8)
DRC1fe80f82010-12-14 01:21:29 +0000604 {
605 int match=0;
DRC9b28def2011-05-21 14:37:15 +0000606 for(k=1; k<dinfo->num_components; k++)
DRC1fe80f82010-12-14 01:21:29 +0000607 {
DRC9b28def2011-05-21 14:37:15 +0000608 if(dinfo->comp_info[k].h_samp_factor==1
609 && dinfo->comp_info[k].v_samp_factor==1)
DRC1fe80f82010-12-14 01:21:29 +0000610 match++;
611 }
DRC9b28def2011-05-21 14:37:15 +0000612 if(match==dinfo->num_components-1)
DRC1fe80f82010-12-14 01:21:29 +0000613 {
DRC9b28def2011-05-21 14:37:15 +0000614 *jpegSubsamp=i; break;
DRC1fe80f82010-12-14 01:21:29 +0000615 }
616 }
617 }
618 }
619
DRC9b28def2011-05-21 14:37:15 +0000620 jpeg_abort_decompress(dinfo);
DRC1fe80f82010-12-14 01:21:29 +0000621
DRC9b28def2011-05-21 14:37:15 +0000622 if(*jpegSubsamp<0)
DRC007a42c2011-05-22 13:55:56 +0000623 _throw("tjDecompressHeader2(): Could not determine subsampling type for JPEG image");
624 if(*width<1 || *height<1)
625 _throw("tjDecompressHeader2(): Invalid data returned in header");
DRC91e86ba2011-02-15 05:24:08 +0000626
627 bailout:
628 return retval;
629}
630
DRC9b28def2011-05-21 14:37:15 +0000631DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle handle,
632 unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height)
DRC91e86ba2011-02-15 05:24:08 +0000633{
DRC9b28def2011-05-21 14:37:15 +0000634 int jpegSubsamp;
635 return tjDecompressHeader2(handle, jpegBuf, jpegSize, width, height,
636 &jpegSubsamp);
DRC1fe80f82010-12-14 01:21:29 +0000637}
638
639
DRC109a5782011-03-01 09:53:07 +0000640DLLEXPORT tjscalingfactor* DLLCALL tjGetScalingFactors(int *numscalingfactors)
DRCb28fc572011-02-22 06:41:29 +0000641{
DRC109a5782011-03-01 09:53:07 +0000642 if(numscalingfactors==NULL)
DRCb28fc572011-02-22 06:41:29 +0000643 {
DRC9b28def2011-05-21 14:37:15 +0000644 snprintf(errStr, JMSG_LENGTH_MAX,
DRC007a42c2011-05-22 13:55:56 +0000645 "tjGetScalingFactors(): Invalid argument");
DRC109a5782011-03-01 09:53:07 +0000646 return NULL;
DRCb28fc572011-02-22 06:41:29 +0000647 }
648
DRC109a5782011-03-01 09:53:07 +0000649 *numscalingfactors=NUMSF;
650 return (tjscalingfactor *)sf;
DRCb28fc572011-02-22 06:41:29 +0000651}
652
653
DRC9b28def2011-05-21 14:37:15 +0000654DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle, unsigned char *jpegBuf,
655 unsigned long jpegSize, unsigned char *dstBuf, int width, int pitch,
656 int height, int pixelFormat, int flags)
DRC2e7b76b2009-04-03 12:04:24 +0000657{
DRC9b28def2011-05-21 14:37:15 +0000658 int i, retval=0; JSAMPROW *row_pointer=NULL;
DRC109a5782011-03-01 09:53:07 +0000659 int jpegwidth, jpegheight, scaledw, scaledh;
DRC2e7b76b2009-04-03 12:04:24 +0000660
DRC9b28def2011-05-21 14:37:15 +0000661 getinstance(handle);
662 if((this->init&DECOMPRESS)==0)
DRC007a42c2011-05-22 13:55:56 +0000663 _throw("tjDecompress2(): Instance has not been initialized for decompression");
DRC9b28def2011-05-21 14:37:15 +0000664
665 if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL || width<0 || pitch<0
666 || height<0 || pixelFormat<0 || pixelFormat>=TJ_NUMPF)
667 _throw("tjDecompress2(): Invalid argument");
668
DRC25b995a2011-05-21 15:34:54 +0000669 if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
670 else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
671 else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
DRC9b28def2011-05-21 14:37:15 +0000672
673 if(setjmp(this->jerr.setjmp_buffer))
674 {
675 /* If we get here, the JPEG code has signaled an error. */
676 retval=-1;
677 goto bailout;
678 }
679
680 jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
681 jpeg_read_header(dinfo, TRUE);
682 setDecompDefaults(dinfo, pixelFormat);
683
DRC25b995a2011-05-21 15:34:54 +0000684 if(flags&TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling=FALSE;
DRC9b28def2011-05-21 14:37:15 +0000685
686 jpegwidth=dinfo->image_width; jpegheight=dinfo->image_height;
687 if(width==0) width=jpegwidth;
688 if(height==0) height=jpegheight;
689 for(i=0; i<NUMSF; i++)
690 {
691 scaledw=TJSCALED(jpegwidth, sf[i]);
692 scaledh=TJSCALED(jpegheight, sf[i]);
693 if(scaledw<=width && scaledh<=height)
694 break;
695 }
696 if(scaledw>width || scaledh>height)
DRC007a42c2011-05-22 13:55:56 +0000697 _throw("tjDecompress2(): Could not scale down to desired image dimensions");
DRC9b28def2011-05-21 14:37:15 +0000698 width=scaledw; height=scaledh;
699 dinfo->scale_num=sf[i].num;
700 dinfo->scale_denom=sf[i].denom;
701
702 jpeg_start_decompress(dinfo);
703 if(pitch==0) pitch=dinfo->output_width*tjPixelSize[pixelFormat];
704 if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)
705 *dinfo->output_height))==NULL)
DRC007a42c2011-05-22 13:55:56 +0000706 _throw("tjDecompress2(): Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000707 for(i=0; i<(int)dinfo->output_height; i++)
708 {
DRC25b995a2011-05-21 15:34:54 +0000709 if(flags&TJFLAG_BOTTOMUP)
DRC9b28def2011-05-21 14:37:15 +0000710 row_pointer[i]=&dstBuf[(dinfo->output_height-i-1)*pitch];
711 else row_pointer[i]=&dstBuf[i*pitch];
712 }
713 while(dinfo->output_scanline<dinfo->output_height)
714 {
715 jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
716 dinfo->output_height-dinfo->output_scanline);
717 }
718 jpeg_finish_decompress(dinfo);
719
720 bailout:
721 if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
722 if(row_pointer) free(row_pointer);
723 return retval;
724}
725
726DLLEXPORT int DLLCALL tjDecompress(tjhandle handle, unsigned char *jpegBuf,
727 unsigned long jpegSize, unsigned char *dstBuf, int width, int pitch,
728 int height, int pixelSize, int flags)
729{
730 if(flags&TJ_YUV)
731 return tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags);
732 else
733 return tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, width, pitch,
734 height, getPixelFormat(pixelSize, flags), flags);
735}
736
737
738DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle handle,
739 unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
740 int flags)
741{
742 int i, row, retval=0; JSAMPROW *outbuf[MAX_COMPONENTS];
743 int cw[MAX_COMPONENTS], ch[MAX_COMPONENTS], iw[MAX_COMPONENTS],
744 tmpbufsize=0, usetmpbuf=0, th[MAX_COMPONENTS];
745 JSAMPLE *_tmpbuf=NULL, *ptr=dstBuf; JSAMPROW *tmpbuf[MAX_COMPONENTS];
746
747 getinstance(handle);
748 if((this->init&DECOMPRESS)==0)
DRC007a42c2011-05-22 13:55:56 +0000749 _throw("tjDecompressToYUV(): Instance has not been initialized for decompression");
DRC2e7b76b2009-04-03 12:04:24 +0000750
DRCf9cf5c72010-12-10 10:58:49 +0000751 for(i=0; i<MAX_COMPONENTS; i++)
752 {
753 tmpbuf[i]=NULL; outbuf[i]=NULL;
754 }
DRC9e17f7d2010-12-10 04:59:13 +0000755
DRC9b28def2011-05-21 14:37:15 +0000756 if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL)
757 _throw("tjDecompressToYUV(): Invalid argument");
DRC2e7b76b2009-04-03 12:04:24 +0000758
DRC25b995a2011-05-21 15:34:54 +0000759 if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
760 else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
761 else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
DRC0c6a2712010-02-22 08:34:44 +0000762
DRC9b28def2011-05-21 14:37:15 +0000763 if(setjmp(this->jerr.setjmp_buffer))
764 {
765 /* If we get here, the JPEG code has signaled an error. */
DRC91e86ba2011-02-15 05:24:08 +0000766 retval=-1;
767 goto bailout;
DRC9e17f7d2010-12-10 04:59:13 +0000768 }
DRC2e7b76b2009-04-03 12:04:24 +0000769
DRC9b28def2011-05-21 14:37:15 +0000770 jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
771 jpeg_read_header(dinfo, TRUE);
DRC2e7b76b2009-04-03 12:04:24 +0000772
DRC9b28def2011-05-21 14:37:15 +0000773 for(i=0; i<dinfo->num_components; i++)
DRC2e7b76b2009-04-03 12:04:24 +0000774 {
DRC9b28def2011-05-21 14:37:15 +0000775 jpeg_component_info *compptr=&dinfo->comp_info[i];
776 int ih;
777 iw[i]=compptr->width_in_blocks*DCTSIZE;
778 ih=compptr->height_in_blocks*DCTSIZE;
779 cw[i]=PAD(dinfo->image_width, dinfo->max_h_samp_factor)
780 *compptr->h_samp_factor/dinfo->max_h_samp_factor;
781 ch[i]=PAD(dinfo->image_height, dinfo->max_v_samp_factor)
782 *compptr->v_samp_factor/dinfo->max_v_samp_factor;
783 if(iw[i]!=cw[i] || ih!=ch[i]) usetmpbuf=1;
784 th[i]=compptr->v_samp_factor*DCTSIZE;
785 tmpbufsize+=iw[i]*th[i];
786 if((outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]))==NULL)
DRC007a42c2011-05-22 13:55:56 +0000787 _throw("tjDecompressToYUV(): Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000788 for(row=0; row<ch[i]; row++)
789 {
790 outbuf[i][row]=ptr;
791 ptr+=PAD(cw[i], 4);
792 }
793 }
794 if(usetmpbuf)
795 {
796 if((_tmpbuf=(JSAMPLE *)malloc(sizeof(JSAMPLE)*tmpbufsize))==NULL)
DRC007a42c2011-05-22 13:55:56 +0000797 _throw("tjDecompressToYUV(): Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000798 ptr=_tmpbuf;
799 for(i=0; i<dinfo->num_components; i++)
800 {
801 if((tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*th[i]))==NULL)
DRC007a42c2011-05-22 13:55:56 +0000802 _throw("tjDecompressToYUV(): Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000803 for(row=0; row<th[i]; row++)
804 {
805 tmpbuf[i][row]=ptr;
806 ptr+=iw[i];
807 }
808 }
809 }
DRC9e17f7d2010-12-10 04:59:13 +0000810
DRC25b995a2011-05-21 15:34:54 +0000811 if(flags&TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling=FALSE;
DRC9b28def2011-05-21 14:37:15 +0000812 dinfo->raw_data_out=TRUE;
813
814 jpeg_start_decompress(dinfo);
815 for(row=0; row<(int)dinfo->output_height;
816 row+=dinfo->max_v_samp_factor*DCTSIZE)
817 {
818 JSAMPARRAY yuvptr[MAX_COMPONENTS];
819 int crow[MAX_COMPONENTS];
DRC9e17f7d2010-12-10 04:59:13 +0000820 for(i=0; i<dinfo->num_components; i++)
821 {
822 jpeg_component_info *compptr=&dinfo->comp_info[i];
DRC9b28def2011-05-21 14:37:15 +0000823 crow[i]=row*compptr->v_samp_factor/dinfo->max_v_samp_factor;
824 if(usetmpbuf) yuvptr[i]=tmpbuf[i];
825 else yuvptr[i]=&outbuf[i][crow[i]];
DRCf9cf5c72010-12-10 10:58:49 +0000826 }
DRC9b28def2011-05-21 14:37:15 +0000827 jpeg_read_raw_data(dinfo, yuvptr, dinfo->max_v_samp_factor*DCTSIZE);
DRCf9cf5c72010-12-10 10:58:49 +0000828 if(usetmpbuf)
829 {
DRC9b28def2011-05-21 14:37:15 +0000830 int j;
DRCf9cf5c72010-12-10 10:58:49 +0000831 for(i=0; i<dinfo->num_components; i++)
832 {
DRC9b28def2011-05-21 14:37:15 +0000833 for(j=0; j<min(th[i], ch[i]-crow[i]); j++)
DRCf9cf5c72010-12-10 10:58:49 +0000834 {
DRC9b28def2011-05-21 14:37:15 +0000835 memcpy(outbuf[i][crow[i]+j], tmpbuf[i][j], cw[i]);
DRCf9cf5c72010-12-10 10:58:49 +0000836 }
DRC9e17f7d2010-12-10 04:59:13 +0000837 }
838 }
839 }
DRC9b28def2011-05-21 14:37:15 +0000840 jpeg_finish_decompress(dinfo);
DRC2e7b76b2009-04-03 12:04:24 +0000841
DRC91e86ba2011-02-15 05:24:08 +0000842 bailout:
DRC9b28def2011-05-21 14:37:15 +0000843 if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
DRC9e17f7d2010-12-10 04:59:13 +0000844 for(i=0; i<MAX_COMPONENTS; i++)
DRCf9cf5c72010-12-10 10:58:49 +0000845 {
846 if(tmpbuf[i]) free(tmpbuf[i]);
DRC9e17f7d2010-12-10 04:59:13 +0000847 if(outbuf[i]) free(outbuf[i]);
DRCf9cf5c72010-12-10 10:58:49 +0000848 }
849 if(_tmpbuf) free(_tmpbuf);
DRC91e86ba2011-02-15 05:24:08 +0000850 return retval;
DRC2e7b76b2009-04-03 12:04:24 +0000851}
852
853
DRC9b28def2011-05-21 14:37:15 +0000854/* Transformer */
DRC890f1e02011-02-26 22:02:37 +0000855
856DLLEXPORT tjhandle DLLCALL tjInitTransform(void)
857{
DRC9b28def2011-05-21 14:37:15 +0000858 tjinstance *this=NULL; tjhandle handle=NULL;
859 if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL)
DRCda5220a2011-03-02 02:17:30 +0000860 {
DRC007a42c2011-05-22 13:55:56 +0000861 snprintf(errStr, JMSG_LENGTH_MAX,
862 "tjInitTransform(): Memory allocation failure");
DRCda5220a2011-03-02 02:17:30 +0000863 return NULL;
864 }
DRC007a42c2011-05-22 13:55:56 +0000865 MEMZERO(this, sizeof(tjinstance));
DRC9b28def2011-05-21 14:37:15 +0000866 handle=_tjInitCompress(this);
867 if(!handle) return NULL;
868 handle=_tjInitDecompress(this);
869 return handle;
DRC890f1e02011-02-26 22:02:37 +0000870}
871
872
DRC9b28def2011-05-21 14:37:15 +0000873DLLEXPORT int DLLCALL tjTransform(tjhandle handle, unsigned char *jpegBuf,
874 unsigned long jpegSize, int n, unsigned char **dstBufs,
875 unsigned long *dstSizes, tjtransform *t, int flags)
DRC890f1e02011-02-26 22:02:37 +0000876{
DRC0a325192011-03-02 09:22:41 +0000877 jpeg_transform_info *xinfo=NULL;
DRC890f1e02011-02-26 22:02:37 +0000878 jvirt_barray_ptr *srccoefs, *dstcoefs;
DRC0a325192011-03-02 09:22:41 +0000879 int retval=0, i;
DRC890f1e02011-02-26 22:02:37 +0000880
DRC9b28def2011-05-21 14:37:15 +0000881 getinstance(handle);
882 if((this->init&COMPRESS)==0 || (this->init&DECOMPRESS)==0)
DRC007a42c2011-05-22 13:55:56 +0000883 _throw("tjTransform(): Instance has not been initialized for transformation");
DRC890f1e02011-02-26 22:02:37 +0000884
DRC9b28def2011-05-21 14:37:15 +0000885 if(jpegBuf==NULL || jpegSize<=0 || n<1 || dstBufs==NULL || dstSizes==NULL
886 || t==NULL || flags<0)
887 _throw("tjTransform(): Invalid argument");
888
DRC25b995a2011-05-21 15:34:54 +0000889 if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
890 else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
891 else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
DRC890f1e02011-02-26 22:02:37 +0000892
DRC9b28def2011-05-21 14:37:15 +0000893 if(setjmp(this->jerr.setjmp_buffer))
894 {
895 /* If we get here, the JPEG code has signaled an error. */
DRC890f1e02011-02-26 22:02:37 +0000896 retval=-1;
897 goto bailout;
898 }
899
DRC9b28def2011-05-21 14:37:15 +0000900 jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
DRC890f1e02011-02-26 22:02:37 +0000901
DRC0a325192011-03-02 09:22:41 +0000902 if((xinfo=(jpeg_transform_info *)malloc(sizeof(jpeg_transform_info)*n))
903 ==NULL)
DRC007a42c2011-05-22 13:55:56 +0000904 _throw("tjTransform(): Memory allocation failure");
905 MEMZERO(xinfo, sizeof(jpeg_transform_info)*n);
DRC890f1e02011-02-26 22:02:37 +0000906
DRC0a325192011-03-02 09:22:41 +0000907 for(i=0; i<n; i++)
DRC890f1e02011-02-26 22:02:37 +0000908 {
DRC0a325192011-03-02 09:22:41 +0000909 xinfo[i].transform=xformtypes[t[i].op];
DRC25b995a2011-05-21 15:34:54 +0000910 xinfo[i].perfect=(t[i].options&TJXOPT_PERFECT)? 1:0;
911 xinfo[i].trim=(t[i].options&TJXOPT_TRIM)? 1:0;
912 xinfo[i].force_grayscale=(t[i].options&TJXOPT_GRAY)? 1:0;
913 xinfo[i].crop=(t[i].options&TJXOPT_CROP)? 1:0;
914 if(n!=1 && t[i].op==TJXOP_HFLIP) xinfo[i].slow_hflip=1;
DRCba5ea512011-03-04 03:20:34 +0000915 else xinfo[i].slow_hflip=0;
DRC0a325192011-03-02 09:22:41 +0000916
917 if(xinfo[i].crop)
DRC890f1e02011-02-26 22:02:37 +0000918 {
DRC0a325192011-03-02 09:22:41 +0000919 xinfo[i].crop_xoffset=t[i].r.x; xinfo[i].crop_xoffset_set=JCROP_POS;
920 xinfo[i].crop_yoffset=t[i].r.y; xinfo[i].crop_yoffset_set=JCROP_POS;
921 if(t[i].r.w!=0)
922 {
923 xinfo[i].crop_width=t[i].r.w; xinfo[i].crop_width_set=JCROP_POS;
924 }
DRCd932e582011-03-15 20:09:47 +0000925 else xinfo[i].crop_width=JCROP_UNSET;
DRC0a325192011-03-02 09:22:41 +0000926 if(t[i].r.h!=0)
927 {
928 xinfo[i].crop_height=t[i].r.h; xinfo[i].crop_height_set=JCROP_POS;
929 }
DRCd932e582011-03-15 20:09:47 +0000930 else xinfo[i].crop_height=JCROP_UNSET;
DRC890f1e02011-02-26 22:02:37 +0000931 }
932 }
933
DRC9b28def2011-05-21 14:37:15 +0000934 jcopy_markers_setup(dinfo, JCOPYOPT_ALL);
935 jpeg_read_header(dinfo, TRUE);
DRC890f1e02011-02-26 22:02:37 +0000936
DRC0a325192011-03-02 09:22:41 +0000937 for(i=0; i<n; i++)
DRC890f1e02011-02-26 22:02:37 +0000938 {
DRC9b28def2011-05-21 14:37:15 +0000939 if(!jtransform_request_workspace(dinfo, &xinfo[i]))
DRC007a42c2011-05-22 13:55:56 +0000940 _throw("tjTransform(): Transform is not perfect");
DRC890f1e02011-02-26 22:02:37 +0000941
DRC0a325192011-03-02 09:22:41 +0000942 if(xinfo[i].crop)
DRC890f1e02011-02-26 22:02:37 +0000943 {
DRC0a325192011-03-02 09:22:41 +0000944 if((t[i].r.x%xinfo[i].iMCU_sample_width)!=0
945 || (t[i].r.y%xinfo[i].iMCU_sample_height)!=0)
946 {
DRC9b28def2011-05-21 14:37:15 +0000947 snprintf(errStr, JMSG_LENGTH_MAX,
DRC0a325192011-03-02 09:22:41 +0000948 "To crop this JPEG image, x must be a multiple of %d\n"
949 "and y must be a multiple of %d.\n",
950 xinfo[i].iMCU_sample_width, xinfo[i].iMCU_sample_height);
951 retval=-1; goto bailout;
952 }
DRC890f1e02011-02-26 22:02:37 +0000953 }
954 }
955
DRC9b28def2011-05-21 14:37:15 +0000956 srccoefs=jpeg_read_coefficients(dinfo);
DRC890f1e02011-02-26 22:02:37 +0000957
DRC0a325192011-03-02 09:22:41 +0000958 for(i=0; i<n; i++)
959 {
960 int w, h;
DRC9b28def2011-05-21 14:37:15 +0000961 jpeg_mem_dest_tj(cinfo, &dstBufs[i], &dstSizes[i],
DRC25b995a2011-05-21 15:34:54 +0000962 (flags&TJFLAG_NOREALLOC)==0);
DRC0a325192011-03-02 09:22:41 +0000963 if(!xinfo[i].crop)
964 {
DRC9b28def2011-05-21 14:37:15 +0000965 w=dinfo->image_width; h=dinfo->image_height;
DRC0a325192011-03-02 09:22:41 +0000966 }
967 else
968 {
969 w=xinfo[i].crop_width; h=xinfo[i].crop_height;
970 }
DRC9b28def2011-05-21 14:37:15 +0000971 jpeg_copy_critical_parameters(dinfo, cinfo);
972 dstcoefs=jtransform_adjust_parameters(dinfo, cinfo, srccoefs,
DRC0a325192011-03-02 09:22:41 +0000973 &xinfo[i]);
DRC9b28def2011-05-21 14:37:15 +0000974 jpeg_write_coefficients(cinfo, dstcoefs);
975 jcopy_markers_execute(dinfo, cinfo, JCOPYOPT_ALL);
976 jtransform_execute_transformation(dinfo, cinfo, srccoefs,
DRC0a325192011-03-02 09:22:41 +0000977 &xinfo[i]);
DRC9b28def2011-05-21 14:37:15 +0000978 jpeg_finish_compress(cinfo);
DRC0a325192011-03-02 09:22:41 +0000979 }
980
DRC9b28def2011-05-21 14:37:15 +0000981 jpeg_finish_decompress(dinfo);
DRC890f1e02011-02-26 22:02:37 +0000982
DRC890f1e02011-02-26 22:02:37 +0000983 bailout:
DRC9b28def2011-05-21 14:37:15 +0000984 if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
985 if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
DRC0a325192011-03-02 09:22:41 +0000986 if(xinfo) free(xinfo);
DRC890f1e02011-02-26 22:02:37 +0000987 return retval;
988}