blob: 634bedf605b37f85cfe3a1055053f0b1aaa3073e [file] [log] [blame]
DRCf8e00552011-02-04 11:06:36 +00001/*
DRC1d29c5f2013-04-27 20:54:44 +00002 * Copyright (C)2011-2013 D. R. Commander. All Rights Reserved.
DRCf8e00552011-02-04 11:06:36 +00003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * - 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.
27 */
28
DRCe8573012011-03-04 10:13:59 +000029#include <stdlib.h>
30#include <string.h>
DRCf8e00552011-02-04 11:06:36 +000031#include "turbojpeg.h"
DRCbc2e66c2012-10-02 06:47:37 +000032#ifdef WIN32
33#include "tjutil.h"
34#endif
DRCf8e00552011-02-04 11:06:36 +000035#include <jni.h>
DRCc5a41992011-02-08 06:54:36 +000036#include "java/org_libjpegturbo_turbojpeg_TJCompressor.h"
37#include "java/org_libjpegturbo_turbojpeg_TJDecompressor.h"
38#include "java/org_libjpegturbo_turbojpeg_TJ.h"
DRCf8e00552011-02-04 11:06:36 +000039
40#define _throw(msg) { \
41 jclass _exccls=(*env)->FindClass(env, "java/lang/Exception"); \
42 if(!_exccls) goto bailout; \
43 (*env)->ThrowNew(env, _exccls, msg); \
44 goto bailout; \
45}
46
DRCfac3bea2012-09-24 02:27:55 +000047#define bailif0(f) {if(!(f)) { \
48 char temps[80]; \
49 snprintf(temps, 80, "Unexpected NULL condition in line %d", __LINE__); \
50 _throw(temps); \
51}}
DRCf8e00552011-02-04 11:06:36 +000052
DRC3bad53f2011-02-23 02:20:49 +000053#define gethandle() \
DRCf8e00552011-02-04 11:06:36 +000054 jclass _cls=(*env)->GetObjectClass(env, obj); \
55 jfieldID _fid; \
56 if(!_cls) goto bailout; \
57 bailif0(_fid=(*env)->GetFieldID(env, _cls, "handle", "J")); \
DRC23da0c02011-03-04 15:28:16 +000058 handle=(tjhandle)(jlong)(*env)->GetLongField(env, obj, _fid); \
DRCf8e00552011-02-04 11:06:36 +000059
DRC3bad53f2011-02-23 02:20:49 +000060JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize
DRC9b49f0e2011-07-12 03:17:23 +000061 (JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp)
DRCf8e00552011-02-04 11:06:36 +000062{
DRC9b49f0e2011-07-12 03:17:23 +000063 jint retval=(jint)tjBufSize(width, height, jpegSubsamp);
DRC36336fc2011-02-22 10:27:31 +000064 if(retval==-1) _throw(tjGetErrorStr());
65
66 bailout:
67 return retval;
68}
69
DRC3bad53f2011-02-23 02:20:49 +000070JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV
DRC36336fc2011-02-22 10:27:31 +000071 (JNIEnv *env, jclass cls, jint width, jint height, jint subsamp)
72{
DRC9b49f0e2011-07-12 03:17:23 +000073 jint retval=(jint)tjBufSizeYUV(width, height, subsamp);
DRC36336fc2011-02-22 10:27:31 +000074 if(retval==-1) _throw(tjGetErrorStr());
75
76 bailout:
77 return retval;
DRCf8e00552011-02-04 11:06:36 +000078}
79
DRCc5a41992011-02-08 06:54:36 +000080JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_init
DRCf8e00552011-02-04 11:06:36 +000081 (JNIEnv *env, jobject obj)
82{
83 jclass cls;
84 jfieldID fid;
85 tjhandle handle;
86
DRC1a3dbe62011-02-28 10:51:55 +000087 if((handle=tjInitCompress())==NULL)
DRCf8e00552011-02-04 11:06:36 +000088 _throw(tjGetErrorStr());
89
90 bailif0(cls=(*env)->GetObjectClass(env, obj));
91 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J"));
DRC23da0c02011-03-04 15:28:16 +000092 (*env)->SetLongField(env, obj, fid, (jlong)handle);
DRCf8e00552011-02-04 11:06:36 +000093
94 bailout:
95 return;
96}
97
DRCfac3bea2012-09-24 02:27:55 +000098JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIIIII_3BIII
99 (JNIEnv *env, jobject obj, jbyteArray src, jint x, jint y, jint width,
100 jint pitch, jint height, jint pf, jbyteArray dst, jint jpegSubsamp,
101 jint jpegQual, jint flags)
DRCf8e00552011-02-04 11:06:36 +0000102{
DRC9b28def2011-05-21 14:37:15 +0000103 tjhandle handle=0;
DRCfac3bea2012-09-24 02:27:55 +0000104 unsigned long jpegSize=0;
105 jsize arraySize=0, actualPitch;
DRC9b28def2011-05-21 14:37:15 +0000106 unsigned char *srcBuf=NULL, *jpegBuf=NULL;
DRCf8e00552011-02-04 11:06:36 +0000107
108 gethandle();
109
DRC92549de2011-03-15 20:52:02 +0000110 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1
DRC6acf52b2011-03-02 01:09:20 +0000111 || pitch<0)
DRC4f1580c2011-02-25 06:11:03 +0000112 _throw("Invalid argument in compress()");
DRC9b28def2011-05-21 14:37:15 +0000113 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
114 _throw("Mismatch between Java and C API");
DRC4f1580c2011-02-25 06:11:03 +0000115
DRCfac3bea2012-09-24 02:27:55 +0000116 actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
117 arraySize=(y+height-1)*actualPitch + x+width;
DRC9b28def2011-05-21 14:37:15 +0000118 if((*env)->GetArrayLength(env, src)<arraySize)
DRC6acf52b2011-03-02 01:09:20 +0000119 _throw("Source buffer is not large enough");
DRC9b49f0e2011-07-12 03:17:23 +0000120 jpegSize=tjBufSize(width, height, jpegSubsamp);
DRC9b28def2011-05-21 14:37:15 +0000121 if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize)
DRC6acf52b2011-03-02 01:09:20 +0000122 _throw("Destination buffer is not large enough");
123
DRC9b28def2011-05-21 14:37:15 +0000124 bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
125 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRCf8e00552011-02-04 11:06:36 +0000126
DRCfac3bea2012-09-24 02:27:55 +0000127 if(tjCompress2(handle, &srcBuf[y*actualPitch + x*tjPixelSize[pf]], width,
128 pitch, height, pf, &jpegBuf, &jpegSize, jpegSubsamp, jpegQual,
129 flags|TJFLAG_NOREALLOC)==-1)
130 {
131 (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
132 (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
133 jpegBuf=srcBuf=NULL;
134 _throw(tjGetErrorStr());
135 }
136
137 bailout:
138 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
139 if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
140 return (jint)jpegSize;
141}
142
143JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIII_3BIII
144 (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch,
145 jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual,
146 jint flags)
147{
148 return Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIIIII_3BIII(
149 env, obj, src, 0, 0, width, pitch, height, pf, dst, jpegSubsamp, jpegQual,
150 flags);
151}
152
153JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIIIII_3BIII
154 (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width,
155 jint stride, jint height, jint pf, jbyteArray dst, jint jpegSubsamp,
156 jint jpegQual, jint flags)
157{
158 tjhandle handle=0;
159 unsigned long jpegSize=0;
160 jsize arraySize=0, actualStride;
161 unsigned char *srcBuf=NULL, *jpegBuf=NULL;
162
163 gethandle();
164
165 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1
166 || stride<0)
167 _throw("Invalid argument in compress()");
168 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
169 _throw("Mismatch between Java and C API");
170 if(tjPixelSize[pf]!=sizeof(jint))
171 _throw("Pixel format must be 32-bit when compressing from an integer buffer.");
172
173 actualStride=(stride==0)? width:stride;
174 arraySize=(y+height-1)*actualStride + x+width;
175 if((*env)->GetArrayLength(env, src)<arraySize)
176 _throw("Source buffer is not large enough");
177 jpegSize=tjBufSize(width, height, jpegSubsamp);
178 if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize)
179 _throw("Destination buffer is not large enough");
180
181 bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
182 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
183
184 if(tjCompress2(handle, &srcBuf[(y*actualStride + x)*sizeof(int)], width,
185 stride*sizeof(jint), height, pf, &jpegBuf, &jpegSize, jpegSubsamp,
186 jpegQual, flags|TJFLAG_NOREALLOC)==-1)
DRCf8e00552011-02-04 11:06:36 +0000187 {
DRC9b28def2011-05-21 14:37:15 +0000188 (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
189 (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
190 jpegBuf=srcBuf=NULL;
DRCf8e00552011-02-04 11:06:36 +0000191 _throw(tjGetErrorStr());
192 }
193
194 bailout:
DRC9b28def2011-05-21 14:37:15 +0000195 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
196 if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
197 return (jint)jpegSize;
DRCf8e00552011-02-04 11:06:36 +0000198}
199
DRC4f1580c2011-02-25 06:11:03 +0000200JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIII_3BIII
DRC84a1bcc2011-02-23 12:09:56 +0000201 (JNIEnv *env, jobject obj, jintArray src, jint width, jint pitch,
DRC9b28def2011-05-21 14:37:15 +0000202 jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual,
DRC4f1580c2011-02-25 06:11:03 +0000203 jint flags)
DRC84a1bcc2011-02-23 12:09:56 +0000204{
DRCfac3bea2012-09-24 02:27:55 +0000205 return Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIIIII_3BIII(
206 env, obj, src, 0, 0, width, pitch, height, pf, dst, jpegSubsamp, jpegQual,
207 flags);
DRC84a1bcc2011-02-23 12:09:56 +0000208}
209
DRC4f1580c2011-02-25 06:11:03 +0000210JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIII_3BII
211 (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch,
DRC6acf52b2011-03-02 01:09:20 +0000212 jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
DRC4f1580c2011-02-25 06:11:03 +0000213{
DRC9b28def2011-05-21 14:37:15 +0000214 tjhandle handle=0;
215 jsize arraySize=0;
216 unsigned char *srcBuf=NULL, *dstBuf=NULL;
DRC4f1580c2011-02-25 06:11:03 +0000217
218 gethandle();
219
DRC92549de2011-03-15 20:52:02 +0000220 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1
DRC6acf52b2011-03-02 01:09:20 +0000221 || pitch<0)
DRC4f1580c2011-02-25 06:11:03 +0000222 _throw("Invalid argument in encodeYUV()");
DRC9b28def2011-05-21 14:37:15 +0000223 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
224 _throw("Mismatch between Java and C API");
DRC4f1580c2011-02-25 06:11:03 +0000225
DRC9b28def2011-05-21 14:37:15 +0000226 arraySize=(pitch==0)? width*tjPixelSize[pf]*height:pitch*height;
227 if((*env)->GetArrayLength(env, src)<arraySize)
DRC6acf52b2011-03-02 01:09:20 +0000228 _throw("Source buffer is not large enough");
DRCe6ab5392011-03-02 01:30:38 +0000229 if((*env)->GetArrayLength(env, dst)
DRC9b49f0e2011-07-12 03:17:23 +0000230 <(jsize)tjBufSizeYUV(width, height, subsamp))
DRC6acf52b2011-03-02 01:09:20 +0000231 _throw("Destination buffer is not large enough");
232
DRC9b28def2011-05-21 14:37:15 +0000233 bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
234 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRC4f1580c2011-02-25 06:11:03 +0000235
DRC9b28def2011-05-21 14:37:15 +0000236 if(tjEncodeYUV2(handle, srcBuf, width, pitch, height, pf, dstBuf, subsamp,
237 flags)==-1)
DRC4f1580c2011-02-25 06:11:03 +0000238 {
DRC9b28def2011-05-21 14:37:15 +0000239 (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
240 (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
241 dstBuf=srcBuf=NULL;
DRC4f1580c2011-02-25 06:11:03 +0000242 _throw(tjGetErrorStr());
243 }
244
245 bailout:
DRC9b28def2011-05-21 14:37:15 +0000246 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
247 if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
DRC4f1580c2011-02-25 06:11:03 +0000248 return;
249}
250
251JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIII_3BII
DRC1d29c5f2013-04-27 20:54:44 +0000252 (JNIEnv *env, jobject obj, jintArray src, jint width, jint stride,
DRC6acf52b2011-03-02 01:09:20 +0000253 jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
DRC4f1580c2011-02-25 06:11:03 +0000254{
255 tjhandle handle=0;
DRC9b28def2011-05-21 14:37:15 +0000256 jsize arraySize=0;
257 unsigned char *srcBuf=NULL, *dstBuf=NULL;
DRC4f1580c2011-02-25 06:11:03 +0000258
259 gethandle();
260
DRC92549de2011-03-15 20:52:02 +0000261 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1
DRC1d29c5f2013-04-27 20:54:44 +0000262 || stride<0)
263 _throw("Invalid argument in encodeYUV()");
DRC9b28def2011-05-21 14:37:15 +0000264 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
265 _throw("Mismatch between Java and C API");
266 if(tjPixelSize[pf]!=sizeof(jint))
DRC4f1580c2011-02-25 06:11:03 +0000267 _throw("Pixel format must be 32-bit when encoding from an integer buffer.");
DRC4f1580c2011-02-25 06:11:03 +0000268
DRC1d29c5f2013-04-27 20:54:44 +0000269 arraySize=(stride==0)? width*height:stride*height;
DRC9b28def2011-05-21 14:37:15 +0000270 if((*env)->GetArrayLength(env, src)<arraySize)
DRC6acf52b2011-03-02 01:09:20 +0000271 _throw("Source buffer is not large enough");
DRCe6ab5392011-03-02 01:30:38 +0000272 if((*env)->GetArrayLength(env, dst)
DRC9b49f0e2011-07-12 03:17:23 +0000273 <(jsize)tjBufSizeYUV(width, height, subsamp))
DRC6acf52b2011-03-02 01:09:20 +0000274 _throw("Destination buffer is not large enough");
275
DRC9b28def2011-05-21 14:37:15 +0000276 bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
277 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRC4f1580c2011-02-25 06:11:03 +0000278
DRC1d29c5f2013-04-27 20:54:44 +0000279 if(tjEncodeYUV2(handle, srcBuf, width, stride*sizeof(jint), height, pf,
DRC9b28def2011-05-21 14:37:15 +0000280 dstBuf, subsamp, flags)==-1)
DRC4f1580c2011-02-25 06:11:03 +0000281 {
DRC9b28def2011-05-21 14:37:15 +0000282 (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
283 (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
284 dstBuf=srcBuf=NULL;
DRC4f1580c2011-02-25 06:11:03 +0000285 _throw(tjGetErrorStr());
286 }
287
288 bailout:
DRC9b28def2011-05-21 14:37:15 +0000289 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
290 if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
DRC4f1580c2011-02-25 06:11:03 +0000291 return;
292}
293
DRCc5a41992011-02-08 06:54:36 +0000294JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy
DRCf8e00552011-02-04 11:06:36 +0000295 (JNIEnv *env, jobject obj)
296{
297 tjhandle handle=0;
298
299 gethandle();
300
301 if(tjDestroy(handle)==-1) _throw(tjGetErrorStr());
DRC3bad53f2011-02-23 02:20:49 +0000302 (*env)->SetLongField(env, obj, _fid, 0);
DRCf8e00552011-02-04 11:06:36 +0000303
304 bailout:
305 return;
306}
307
DRCc5a41992011-02-08 06:54:36 +0000308JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_init
DRCf8e00552011-02-04 11:06:36 +0000309 (JNIEnv *env, jobject obj)
310{
311 jclass cls;
312 jfieldID fid;
313 tjhandle handle;
314
DRC1a3dbe62011-02-28 10:51:55 +0000315 if((handle=tjInitDecompress())==NULL) _throw(tjGetErrorStr());
DRCf8e00552011-02-04 11:06:36 +0000316
317 bailif0(cls=(*env)->GetObjectClass(env, obj));
318 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J"));
DRC23da0c02011-03-04 15:28:16 +0000319 (*env)->SetLongField(env, obj, fid, (jlong)handle);
DRCf8e00552011-02-04 11:06:36 +0000320
321 bailout:
322 return;
323}
324
DRC109a5782011-03-01 09:53:07 +0000325JNIEXPORT jobjectArray JNICALL Java_org_libjpegturbo_turbojpeg_TJ_getScalingFactors
326 (JNIEnv *env, jclass cls)
DRC36336fc2011-02-22 10:27:31 +0000327{
DRC109a5782011-03-01 09:53:07 +0000328 jclass sfcls=NULL; jfieldID fid=0;
329 tjscalingfactor *sf=NULL; int n=0, i;
330 jobject sfobj=NULL;
331 jobjectArray sfjava=NULL;
332
333 if((sf=tjGetScalingFactors(&n))==NULL || n==0)
DRC36336fc2011-02-22 10:27:31 +0000334 _throw(tjGetErrorStr());
335
DRCb2f94152011-04-02 02:09:03 +0000336 bailif0(sfcls=(*env)->FindClass(env, "org/libjpegturbo/turbojpeg/TJScalingFactor"));
DRC109a5782011-03-01 09:53:07 +0000337 bailif0(sfjava=(jobjectArray)(*env)->NewObjectArray(env, n, sfcls, 0));
DRC36336fc2011-02-22 10:27:31 +0000338
DRC109a5782011-03-01 09:53:07 +0000339 for(i=0; i<n; i++)
340 {
341 bailif0(sfobj=(*env)->AllocObject(env, sfcls));
342 bailif0(fid=(*env)->GetFieldID(env, sfcls, "num", "I"));
343 (*env)->SetIntField(env, sfobj, fid, sf[i].num);
344 bailif0(fid=(*env)->GetFieldID(env, sfcls, "denom", "I"));
345 (*env)->SetIntField(env, sfobj, fid, sf[i].denom);
346 (*env)->SetObjectArrayElement(env, sfjava, i, sfobj);
347 }
DRC36336fc2011-02-22 10:27:31 +0000348
349 bailout:
DRC109a5782011-03-01 09:53:07 +0000350 return sfjava;
DRC36336fc2011-02-22 10:27:31 +0000351}
352
DRC3bad53f2011-02-23 02:20:49 +0000353JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressHeader
DRC9b28def2011-05-21 14:37:15 +0000354 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize)
DRCf8e00552011-02-04 11:06:36 +0000355{
DRCf8e00552011-02-04 11:06:36 +0000356 tjhandle handle=0;
DRC9b28def2011-05-21 14:37:15 +0000357 unsigned char *jpegBuf=NULL;
358 int width=0, height=0, jpegSubsamp=-1;
DRCf8e00552011-02-04 11:06:36 +0000359
360 gethandle();
361
DRC9b28def2011-05-21 14:37:15 +0000362 if((*env)->GetArrayLength(env, src)<jpegSize)
DRC6acf52b2011-03-02 01:09:20 +0000363 _throw("Source buffer is not large enough");
364
DRC9b28def2011-05-21 14:37:15 +0000365 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
DRCf8e00552011-02-04 11:06:36 +0000366
DRC9b28def2011-05-21 14:37:15 +0000367 if(tjDecompressHeader2(handle, jpegBuf, (unsigned long)jpegSize,
368 &width, &height, &jpegSubsamp)==-1)
DRCf8e00552011-02-04 11:06:36 +0000369 {
DRC9b28def2011-05-21 14:37:15 +0000370 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
DRCf8e00552011-02-04 11:06:36 +0000371 _throw(tjGetErrorStr());
372 }
DRC9b28def2011-05-21 14:37:15 +0000373 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); jpegBuf=NULL;
DRCf8e00552011-02-04 11:06:36 +0000374
DRC3bad53f2011-02-23 02:20:49 +0000375 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
DRC9b28def2011-05-21 14:37:15 +0000376 (*env)->SetIntField(env, obj, _fid, jpegSubsamp);
DRC3bad53f2011-02-23 02:20:49 +0000377 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
378 (*env)->SetIntField(env, obj, _fid, width);
379 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
380 (*env)->SetIntField(env, obj, _fid, height);
DRCf8e00552011-02-04 11:06:36 +0000381
382 bailout:
DRC3bad53f2011-02-23 02:20:49 +0000383 return;
DRCf8e00552011-02-04 11:06:36 +0000384}
385
DRCf659f432012-06-06 08:41:06 +0000386JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIIIII
DRC9b28def2011-05-21 14:37:15 +0000387 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
DRCf659f432012-06-06 08:41:06 +0000388 jint x, jint y, jint width, jint pitch, jint height, jint pf, jint flags)
DRCf8e00552011-02-04 11:06:36 +0000389{
DRC9b28def2011-05-21 14:37:15 +0000390 tjhandle handle=0;
DRCf659f432012-06-06 08:41:06 +0000391 jsize arraySize=0, actualPitch;
DRC9b28def2011-05-21 14:37:15 +0000392 unsigned char *jpegBuf=NULL, *dstBuf=NULL;
DRCf8e00552011-02-04 11:06:36 +0000393
394 gethandle();
395
DRC92549de2011-03-15 20:52:02 +0000396 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
DRC4f1580c2011-02-25 06:11:03 +0000397 _throw("Invalid argument in decompress()");
DRC9b28def2011-05-21 14:37:15 +0000398 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
399 _throw("Mismatch between Java and C API");
DRC4f1580c2011-02-25 06:11:03 +0000400
DRC9b28def2011-05-21 14:37:15 +0000401 if((*env)->GetArrayLength(env, src)<jpegSize)
DRC6acf52b2011-03-02 01:09:20 +0000402 _throw("Source buffer is not large enough");
DRCf659f432012-06-06 08:41:06 +0000403 actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
DRCdc31f0b2012-06-07 09:38:57 +0000404 arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf];
DRC9b28def2011-05-21 14:37:15 +0000405 if((*env)->GetArrayLength(env, dst)<arraySize)
DRC6acf52b2011-03-02 01:09:20 +0000406 _throw("Destination buffer is not large enough");
407
DRC9b28def2011-05-21 14:37:15 +0000408 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
409 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRCf8e00552011-02-04 11:06:36 +0000410
DRCf659f432012-06-06 08:41:06 +0000411 if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize,
412 &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf,
413 flags)==-1)
414 {
415 (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
416 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
417 dstBuf=jpegBuf=NULL;
418 _throw(tjGetErrorStr());
419 }
420
421 bailout:
422 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
423 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
424 return;
425}
426
427JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIII
428 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
429 jint width, jint pitch, jint height, jint pf, jint flags)
430{
431 Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIIIII
432 (env, obj, src, jpegSize, dst, 0, 0, width, pitch, height, pf, flags);
433}
434
435JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIIIII
436 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst,
437 jint x, jint y, jint width, jint stride, jint height, jint pf, jint flags)
438{
439 tjhandle handle=0;
440 jsize arraySize=0, actualStride;
441 unsigned char *jpegBuf=NULL, *dstBuf=NULL;
442
443 gethandle();
444
445 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
446 _throw("Invalid argument in decompress()");
447 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
448 _throw("Mismatch between Java and C API");
449 if(tjPixelSize[pf]!=sizeof(jint))
450 _throw("Pixel format must be 32-bit when decompressing to an integer buffer.");
451
452 if((*env)->GetArrayLength(env, src)<jpegSize)
453 _throw("Source buffer is not large enough");
454 actualStride=(stride==0)? width:stride;
DRCdc31f0b2012-06-07 09:38:57 +0000455 arraySize=(y+height-1)*actualStride + x+width;
DRCf659f432012-06-06 08:41:06 +0000456 if((*env)->GetArrayLength(env, dst)<arraySize)
457 _throw("Destination buffer is not large enough");
458
459 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
460 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
461
462 if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize,
463 &dstBuf[(y*actualStride + x)*sizeof(int)], width, stride*sizeof(jint),
464 height, pf, flags)==-1)
DRCf8e00552011-02-04 11:06:36 +0000465 {
DRC9b28def2011-05-21 14:37:15 +0000466 (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
467 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
468 dstBuf=jpegBuf=NULL;
DRCf8e00552011-02-04 11:06:36 +0000469 _throw(tjGetErrorStr());
470 }
471
472 bailout:
DRC9b28def2011-05-21 14:37:15 +0000473 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
474 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
DRCf8e00552011-02-04 11:06:36 +0000475 return;
476}
477
DRC4f1580c2011-02-25 06:11:03 +0000478JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIII
DRC9b28def2011-05-21 14:37:15 +0000479 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst,
DRCf659f432012-06-06 08:41:06 +0000480 jint width, jint stride, jint height, jint pf, jint flags)
DRC84a1bcc2011-02-23 12:09:56 +0000481{
DRCf659f432012-06-06 08:41:06 +0000482 Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIIIII
483 (env, obj, src, jpegSize, dst, 0, 0, width, stride, height, pf, flags);
484
DRC4f1580c2011-02-25 06:11:03 +0000485}
DRC84a1bcc2011-02-23 12:09:56 +0000486
DRC4f1580c2011-02-25 06:11:03 +0000487JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV
DRC9b28def2011-05-21 14:37:15 +0000488 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
DRC4f1580c2011-02-25 06:11:03 +0000489 jint flags)
490{
491 tjhandle handle=0;
DRC9b28def2011-05-21 14:37:15 +0000492 unsigned char *jpegBuf=NULL, *dstBuf=NULL;
DRC6acf52b2011-03-02 01:09:20 +0000493 int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0;
DRC4f1580c2011-02-25 06:11:03 +0000494
495 gethandle();
496
DRC9b28def2011-05-21 14:37:15 +0000497 if((*env)->GetArrayLength(env, src)<jpegSize)
DRC6acf52b2011-03-02 01:09:20 +0000498 _throw("Source buffer is not large enough");
499 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
500 jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
501 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
502 jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
503 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
504 jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
505 if((*env)->GetArrayLength(env, dst)
DRC9b49f0e2011-07-12 03:17:23 +0000506 <(jsize)tjBufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp))
DRC6acf52b2011-03-02 01:09:20 +0000507 _throw("Destination buffer is not large enough");
508
DRC9b28def2011-05-21 14:37:15 +0000509 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
510 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRC4f1580c2011-02-25 06:11:03 +0000511
DRC9b28def2011-05-21 14:37:15 +0000512 if(tjDecompressToYUV(handle, jpegBuf, (unsigned long)jpegSize, dstBuf,
513 flags)==-1)
DRC4f1580c2011-02-25 06:11:03 +0000514 {
DRC9b28def2011-05-21 14:37:15 +0000515 (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
516 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
517 dstBuf=jpegBuf=NULL;
DRC4f1580c2011-02-25 06:11:03 +0000518 _throw(tjGetErrorStr());
519 }
520
521 bailout:
DRC9b28def2011-05-21 14:37:15 +0000522 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
523 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
DRC4f1580c2011-02-25 06:11:03 +0000524 return;
DRC84a1bcc2011-02-23 12:09:56 +0000525}
526
DRCe8573012011-03-04 10:13:59 +0000527JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_init
528 (JNIEnv *env, jobject obj)
529{
530 jclass cls;
531 jfieldID fid;
532 tjhandle handle;
533
534 if((handle=tjInitTransform())==NULL) _throw(tjGetErrorStr());
535
536 bailif0(cls=(*env)->GetObjectClass(env, obj));
537 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J"));
DRC23da0c02011-03-04 15:28:16 +0000538 (*env)->SetLongField(env, obj, fid, (jlong)handle);
DRCe8573012011-03-04 10:13:59 +0000539
540 bailout:
541 return;
542}
543
DRCf5467112011-09-20 05:02:19 +0000544typedef struct _JNICustomFilterParams
545{
546 JNIEnv *env;
547 jobject tobj;
548 jobject cfobj;
549} JNICustomFilterParams;
550
551static int JNICustomFilter(short *coeffs, tjregion arrayRegion,
552 tjregion planeRegion, int componentIndex, int transformIndex,
553 tjtransform *transform)
554{
555 JNICustomFilterParams *params=(JNICustomFilterParams *)transform->data;
556 JNIEnv *env=params->env;
557 jobject tobj=params->tobj, cfobj=params->cfobj;
558 jobject arrayRegionObj, planeRegionObj, bufobj, borobj;
559 jclass cls; jmethodID mid; jfieldID fid;
560
561 bailif0(bufobj=(*env)->NewDirectByteBuffer(env, coeffs,
562 sizeof(short)*arrayRegion.w*arrayRegion.h));
563 bailif0(cls=(*env)->FindClass(env, "java/nio/ByteOrder"));
564 bailif0(mid=(*env)->GetStaticMethodID(env, cls, "nativeOrder",
565 "()Ljava/nio/ByteOrder;"));
566 bailif0(borobj=(*env)->CallStaticObjectMethod(env, cls, mid));
567 bailif0(cls=(*env)->GetObjectClass(env, bufobj));
568 bailif0(mid=(*env)->GetMethodID(env, cls, "order",
569 "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;"));
570 (*env)->CallObjectMethod(env, bufobj, mid, borobj);
571 bailif0(mid=(*env)->GetMethodID(env, cls, "asShortBuffer",
572 "()Ljava/nio/ShortBuffer;"));
573 bailif0(bufobj=(*env)->CallObjectMethod(env, bufobj, mid));
574
575 bailif0(cls=(*env)->FindClass(env, "java/awt/Rectangle"));
576 bailif0(arrayRegionObj=(*env)->AllocObject(env, cls));
577 bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I"));
578 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.x);
579 bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I"));
580 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.y);
581 bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I"));
582 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.w);
583 bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I"));
584 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.h);
585
586 bailif0(planeRegionObj=(*env)->AllocObject(env, cls));
587 bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I"));
588 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.x);
589 bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I"));
590 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.y);
591 bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I"));
592 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.w);
593 bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I"));
594 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.h);
595
596 bailif0(cls=(*env)->GetObjectClass(env, cfobj));
597 bailif0(mid=(*env)->GetMethodID(env, cls, "customFilter",
598 "(Ljava/nio/ShortBuffer;Ljava/awt/Rectangle;Ljava/awt/Rectangle;IILorg/libjpegturbo/turbojpeg/TJTransform;)V"));
599 (*env)->CallVoidMethod(env, cfobj, mid, bufobj, arrayRegionObj,
600 planeRegionObj, componentIndex, transformIndex, tobj);
601
602 return 0;
603
604 bailout:
605 return -1;
606}
607
DRCe8573012011-03-04 10:13:59 +0000608JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transform
DRC9b28def2011-05-21 14:37:15 +0000609 (JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize,
DRCe8573012011-03-04 10:13:59 +0000610 jobjectArray dstobjs, jobjectArray tobjs, jint flags)
611{
612 tjhandle handle=0; int i;
DRC9b28def2011-05-21 14:37:15 +0000613 unsigned char *jpegBuf=NULL, **dstBufs=NULL; jsize n=0;
614 unsigned long *dstSizes=NULL; tjtransform *t=NULL;
615 jbyteArray *jdstBufs=NULL;
DRC9b49f0e2011-07-12 03:17:23 +0000616 int jpegWidth=0, jpegHeight=0, jpegSubsamp;
DRC9b28def2011-05-21 14:37:15 +0000617 jintArray jdstSizes=0; jint *dstSizesi=NULL;
DRCf5467112011-09-20 05:02:19 +0000618 JNICustomFilterParams *params=NULL;
DRCe8573012011-03-04 10:13:59 +0000619
620 gethandle();
621
DRC9b28def2011-05-21 14:37:15 +0000622 if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize)
DRCe8573012011-03-04 10:13:59 +0000623 _throw("Source buffer is not large enough");
624 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
625 jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
626 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
627 jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
DRC9b49f0e2011-07-12 03:17:23 +0000628 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
629 jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
DRCe8573012011-03-04 10:13:59 +0000630
631 n=(*env)->GetArrayLength(env, dstobjs);
632 if(n!=(*env)->GetArrayLength(env, tobjs))
633 _throw("Mismatch between size of transforms array and destination buffers array");
634
DRC9b28def2011-05-21 14:37:15 +0000635 if((dstBufs=(unsigned char **)malloc(sizeof(unsigned char *)*n))==NULL)
DRCe8573012011-03-04 10:13:59 +0000636 _throw("Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000637 if((jdstBufs=(jbyteArray *)malloc(sizeof(jbyteArray)*n))==NULL)
DRCe8573012011-03-04 10:13:59 +0000638 _throw("Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000639 if((dstSizes=(unsigned long *)malloc(sizeof(unsigned long)*n))==NULL)
DRCe8573012011-03-04 10:13:59 +0000640 _throw("Memory allocation failure");
641 if((t=(tjtransform *)malloc(sizeof(tjtransform)*n))==NULL)
642 _throw("Memory allocation failure");
DRCf5467112011-09-20 05:02:19 +0000643 if((params=(JNICustomFilterParams *)malloc(sizeof(JNICustomFilterParams)*n))
644 ==NULL)
645 _throw("Memory allocation failure");
DRCe8573012011-03-04 10:13:59 +0000646 for(i=0; i<n; i++)
647 {
DRC9b28def2011-05-21 14:37:15 +0000648 dstBufs[i]=NULL; jdstBufs[i]=NULL; dstSizes[i]=0;
DRCe8573012011-03-04 10:13:59 +0000649 memset(&t[i], 0, sizeof(tjtransform));
DRCf5467112011-09-20 05:02:19 +0000650 memset(&params[i], 0, sizeof(JNICustomFilterParams));
DRCe8573012011-03-04 10:13:59 +0000651 }
652
653 for(i=0; i<n; i++)
654 {
DRCf5467112011-09-20 05:02:19 +0000655 jobject tobj, cfobj;
DRCe8573012011-03-04 10:13:59 +0000656
657 bailif0(tobj=(*env)->GetObjectArrayElement(env, tobjs, i));
658 bailif0(_cls=(*env)->GetObjectClass(env, tobj));
659 bailif0(_fid=(*env)->GetFieldID(env, _cls, "op", "I"));
660 t[i].op=(*env)->GetIntField(env, tobj, _fid);
661 bailif0(_fid=(*env)->GetFieldID(env, _cls, "options", "I"));
662 t[i].options=(*env)->GetIntField(env, tobj, _fid);
663 bailif0(_fid=(*env)->GetFieldID(env, _cls, "x", "I"));
664 t[i].r.x=(*env)->GetIntField(env, tobj, _fid);
665 bailif0(_fid=(*env)->GetFieldID(env, _cls, "y", "I"));
666 t[i].r.y=(*env)->GetIntField(env, tobj, _fid);
667 bailif0(_fid=(*env)->GetFieldID(env, _cls, "width", "I"));
668 t[i].r.w=(*env)->GetIntField(env, tobj, _fid);
669 bailif0(_fid=(*env)->GetFieldID(env, _cls, "height", "I"));
670 t[i].r.h=(*env)->GetIntField(env, tobj, _fid);
DRCf5467112011-09-20 05:02:19 +0000671
DRCf5467112011-09-20 05:02:19 +0000672 bailif0(_fid=(*env)->GetFieldID(env, _cls, "cf",
673 "Lorg/libjpegturbo/turbojpeg/TJCustomFilter;"));
DRC06420c42011-09-26 18:46:09 +0000674 cfobj=(*env)->GetObjectField(env, tobj, _fid);
675 if(cfobj)
676 {
677 params[i].env=env;
678 params[i].tobj=tobj;
679 params[i].cfobj=cfobj;
680 t[i].customFilter=JNICustomFilter;
681 t[i].data=(void *)&params[i];
682 }
DRCe8573012011-03-04 10:13:59 +0000683 }
684
DRC9b28def2011-05-21 14:37:15 +0000685 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0));
DRCe8573012011-03-04 10:13:59 +0000686 for(i=0; i<n; i++)
687 {
688 int w=jpegWidth, h=jpegHeight;
689 if(t[i].r.w!=0) w=t[i].r.w;
690 if(t[i].r.h!=0) h=t[i].r.h;
DRC9b28def2011-05-21 14:37:15 +0000691 bailif0(jdstBufs[i]=(*env)->GetObjectArrayElement(env, dstobjs, i));
DRCefe28ce2012-01-17 11:48:38 +0000692 if((unsigned long)(*env)->GetArrayLength(env, jdstBufs[i])
693 <tjBufSize(w, h, jpegSubsamp))
DRCe8573012011-03-04 10:13:59 +0000694 _throw("Destination buffer is not large enough");
DRC9b28def2011-05-21 14:37:15 +0000695 bailif0(dstBufs[i]=(*env)->GetPrimitiveArrayCritical(env, jdstBufs[i], 0));
DRCe8573012011-03-04 10:13:59 +0000696 }
697
DRC9b28def2011-05-21 14:37:15 +0000698 if(tjTransform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t,
DRC4db92ad2011-05-25 04:52:25 +0000699 flags|TJFLAG_NOREALLOC)==-1)
DRCe8573012011-03-04 10:13:59 +0000700 {
DRC9b28def2011-05-21 14:37:15 +0000701 (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0);
702 jpegBuf=NULL;
DRCe8573012011-03-04 10:13:59 +0000703 for(i=0; i<n; i++)
704 {
DRC9b28def2011-05-21 14:37:15 +0000705 (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0);
706 dstBufs[i]=NULL;
DRCe8573012011-03-04 10:13:59 +0000707 }
708 _throw(tjGetErrorStr());
709 }
710
DRC9b28def2011-05-21 14:37:15 +0000711 jdstSizes=(*env)->NewIntArray(env, n);
712 bailif0(dstSizesi=(*env)->GetIntArrayElements(env, jdstSizes, 0));
713 for(i=0; i<n; i++) dstSizesi[i]=(int)dstSizes[i];
DRCe8573012011-03-04 10:13:59 +0000714
715 bailout:
DRC9b28def2011-05-21 14:37:15 +0000716 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0);
717 if(dstBufs)
DRCe8573012011-03-04 10:13:59 +0000718 {
719 for(i=0; i<n; i++)
720 {
DRC9b28def2011-05-21 14:37:15 +0000721 if(dstBufs[i] && jdstBufs && jdstBufs[i])
722 (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0);
DRCe8573012011-03-04 10:13:59 +0000723 }
DRC9b28def2011-05-21 14:37:15 +0000724 free(dstBufs);
DRCe8573012011-03-04 10:13:59 +0000725 }
DRC9b28def2011-05-21 14:37:15 +0000726 if(jdstBufs) free(jdstBufs);
727 if(dstSizes) free(dstSizes);
728 if(dstSizesi) (*env)->ReleaseIntArrayElements(env, jdstSizes, dstSizesi, 0);
DRCe8573012011-03-04 10:13:59 +0000729 if(t) free(t);
DRC9b28def2011-05-21 14:37:15 +0000730 return jdstSizes;
DRCe8573012011-03-04 10:13:59 +0000731}
732
DRCc5a41992011-02-08 06:54:36 +0000733JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_destroy
DRCf8e00552011-02-04 11:06:36 +0000734 (JNIEnv *env, jobject obj)
735{
DRCc5a41992011-02-08 06:54:36 +0000736 Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy(env, obj);
DRCf8e00552011-02-04 11:06:36 +0000737}