blob: 1ff9bbafa6906e7735a92ae2e5cb77dbe0ea58d8 [file] [log] [blame]
DRCf8e00552011-02-04 11:06:36 +00001/*
2 * Copyright (C)2011 D. R. Commander. All Rights Reserved.
3 *
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"
32#include <jni.h>
DRCc5a41992011-02-08 06:54:36 +000033#include "java/org_libjpegturbo_turbojpeg_TJCompressor.h"
34#include "java/org_libjpegturbo_turbojpeg_TJDecompressor.h"
35#include "java/org_libjpegturbo_turbojpeg_TJ.h"
DRCf8e00552011-02-04 11:06:36 +000036
37#define _throw(msg) { \
38 jclass _exccls=(*env)->FindClass(env, "java/lang/Exception"); \
39 if(!_exccls) goto bailout; \
40 (*env)->ThrowNew(env, _exccls, msg); \
41 goto bailout; \
42}
43
44#define bailif0(f) {if(!(f)) goto bailout;}
45
DRC3bad53f2011-02-23 02:20:49 +000046#define gethandle() \
DRCf8e00552011-02-04 11:06:36 +000047 jclass _cls=(*env)->GetObjectClass(env, obj); \
48 jfieldID _fid; \
49 if(!_cls) goto bailout; \
50 bailif0(_fid=(*env)->GetFieldID(env, _cls, "handle", "J")); \
DRC23da0c02011-03-04 15:28:16 +000051 handle=(tjhandle)(jlong)(*env)->GetLongField(env, obj, _fid); \
DRCf8e00552011-02-04 11:06:36 +000052
DRC3bad53f2011-02-23 02:20:49 +000053JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize
DRC9b49f0e2011-07-12 03:17:23 +000054 (JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp)
DRCf8e00552011-02-04 11:06:36 +000055{
DRC9b49f0e2011-07-12 03:17:23 +000056 jint retval=(jint)tjBufSize(width, height, jpegSubsamp);
DRC36336fc2011-02-22 10:27:31 +000057 if(retval==-1) _throw(tjGetErrorStr());
58
59 bailout:
60 return retval;
61}
62
DRC3bad53f2011-02-23 02:20:49 +000063JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV
DRC36336fc2011-02-22 10:27:31 +000064 (JNIEnv *env, jclass cls, jint width, jint height, jint subsamp)
65{
DRC9b49f0e2011-07-12 03:17:23 +000066 jint retval=(jint)tjBufSizeYUV(width, height, subsamp);
DRC36336fc2011-02-22 10:27:31 +000067 if(retval==-1) _throw(tjGetErrorStr());
68
69 bailout:
70 return retval;
DRCf8e00552011-02-04 11:06:36 +000071}
72
DRCc5a41992011-02-08 06:54:36 +000073JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_init
DRCf8e00552011-02-04 11:06:36 +000074 (JNIEnv *env, jobject obj)
75{
76 jclass cls;
77 jfieldID fid;
78 tjhandle handle;
79
DRC1a3dbe62011-02-28 10:51:55 +000080 if((handle=tjInitCompress())==NULL)
DRCf8e00552011-02-04 11:06:36 +000081 _throw(tjGetErrorStr());
82
83 bailif0(cls=(*env)->GetObjectClass(env, obj));
84 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J"));
DRC23da0c02011-03-04 15:28:16 +000085 (*env)->SetLongField(env, obj, fid, (jlong)handle);
DRCf8e00552011-02-04 11:06:36 +000086
87 bailout:
88 return;
89}
90
DRC84a1bcc2011-02-23 12:09:56 +000091JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIII_3BIII
DRCf8e00552011-02-04 11:06:36 +000092 (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch,
DRC9b28def2011-05-21 14:37:15 +000093 jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual,
DRC4f1580c2011-02-25 06:11:03 +000094 jint flags)
DRCf8e00552011-02-04 11:06:36 +000095{
DRC9b28def2011-05-21 14:37:15 +000096 tjhandle handle=0;
97 unsigned long jpegSize=0; jsize arraySize=0;
98 unsigned char *srcBuf=NULL, *jpegBuf=NULL;
DRCf8e00552011-02-04 11:06:36 +000099
100 gethandle();
101
DRC92549de2011-03-15 20:52:02 +0000102 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1
DRC6acf52b2011-03-02 01:09:20 +0000103 || pitch<0)
DRC4f1580c2011-02-25 06:11:03 +0000104 _throw("Invalid argument in compress()");
DRC9b28def2011-05-21 14:37:15 +0000105 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
106 _throw("Mismatch between Java and C API");
DRC4f1580c2011-02-25 06:11:03 +0000107
DRC9b28def2011-05-21 14:37:15 +0000108 arraySize=(pitch==0)? width*tjPixelSize[pf]*height:pitch*height;
109 if((*env)->GetArrayLength(env, src)<arraySize)
DRC6acf52b2011-03-02 01:09:20 +0000110 _throw("Source buffer is not large enough");
DRC9b49f0e2011-07-12 03:17:23 +0000111 jpegSize=tjBufSize(width, height, jpegSubsamp);
DRC9b28def2011-05-21 14:37:15 +0000112 if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize)
DRC6acf52b2011-03-02 01:09:20 +0000113 _throw("Destination buffer is not large enough");
114
DRC9b28def2011-05-21 14:37:15 +0000115 bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
116 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRCf8e00552011-02-04 11:06:36 +0000117
DRC9b28def2011-05-21 14:37:15 +0000118 if(tjCompress2(handle, srcBuf, width, pitch, height, pf, &jpegBuf,
DRC25b995a2011-05-21 15:34:54 +0000119 &jpegSize, jpegSubsamp, jpegQual, flags|TJFLAG_NOREALLOC)==-1)
DRCf8e00552011-02-04 11:06:36 +0000120 {
DRC9b28def2011-05-21 14:37:15 +0000121 (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
122 (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
123 jpegBuf=srcBuf=NULL;
DRCf8e00552011-02-04 11:06:36 +0000124 _throw(tjGetErrorStr());
125 }
126
127 bailout:
DRC9b28def2011-05-21 14:37:15 +0000128 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
129 if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
130 return (jint)jpegSize;
DRCf8e00552011-02-04 11:06:36 +0000131}
132
DRC4f1580c2011-02-25 06:11:03 +0000133JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIII_3BIII
DRC84a1bcc2011-02-23 12:09:56 +0000134 (JNIEnv *env, jobject obj, jintArray src, jint width, jint pitch,
DRC9b28def2011-05-21 14:37:15 +0000135 jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual,
DRC4f1580c2011-02-25 06:11:03 +0000136 jint flags)
DRC84a1bcc2011-02-23 12:09:56 +0000137{
138 tjhandle handle=0;
DRC9b28def2011-05-21 14:37:15 +0000139 unsigned long jpegSize=0; jsize arraySize=0;
140 unsigned char *srcBuf=NULL, *jpegBuf=NULL;
DRC84a1bcc2011-02-23 12:09:56 +0000141
142 gethandle();
143
DRC92549de2011-03-15 20:52:02 +0000144 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1
DRC6acf52b2011-03-02 01:09:20 +0000145 || pitch<0)
DRC4f1580c2011-02-25 06:11:03 +0000146 _throw("Invalid argument in compress()");
DRC9b28def2011-05-21 14:37:15 +0000147 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
148 _throw("Mismatch between Java and C API");
149 if(tjPixelSize[pf]!=sizeof(jint))
DRC4f1580c2011-02-25 06:11:03 +0000150 _throw("Pixel format must be 32-bit when compressing from an integer buffer.");
DRC4f1580c2011-02-25 06:11:03 +0000151
DRC9b28def2011-05-21 14:37:15 +0000152 arraySize=(pitch==0)? width*height:pitch*height;
153 if((*env)->GetArrayLength(env, src)<arraySize)
DRC6acf52b2011-03-02 01:09:20 +0000154 _throw("Source buffer is not large enough");
DRC9b49f0e2011-07-12 03:17:23 +0000155 jpegSize=tjBufSize(width, height, jpegSubsamp);
DRC9b28def2011-05-21 14:37:15 +0000156 if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize)
DRC6acf52b2011-03-02 01:09:20 +0000157 _throw("Destination buffer is not large enough");
158
DRC9b28def2011-05-21 14:37:15 +0000159 bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
160 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRC84a1bcc2011-02-23 12:09:56 +0000161
DRC9b28def2011-05-21 14:37:15 +0000162 if(tjCompress2(handle, srcBuf, width, pitch*sizeof(jint), height, pf,
DRC25b995a2011-05-21 15:34:54 +0000163 &jpegBuf, &jpegSize, jpegSubsamp, jpegQual, flags|TJFLAG_NOREALLOC)==-1)
DRC84a1bcc2011-02-23 12:09:56 +0000164 {
DRC9b28def2011-05-21 14:37:15 +0000165 (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
166 (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
167 jpegBuf=srcBuf=NULL;
DRC84a1bcc2011-02-23 12:09:56 +0000168 _throw(tjGetErrorStr());
169 }
170
171 bailout:
DRC9b28def2011-05-21 14:37:15 +0000172 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
173 if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
174 return (jint)jpegSize;
DRC84a1bcc2011-02-23 12:09:56 +0000175}
176
DRC4f1580c2011-02-25 06:11:03 +0000177JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIII_3BII
178 (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch,
DRC6acf52b2011-03-02 01:09:20 +0000179 jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
DRC4f1580c2011-02-25 06:11:03 +0000180{
DRC9b28def2011-05-21 14:37:15 +0000181 tjhandle handle=0;
182 jsize arraySize=0;
183 unsigned char *srcBuf=NULL, *dstBuf=NULL;
DRC4f1580c2011-02-25 06:11:03 +0000184
185 gethandle();
186
DRC92549de2011-03-15 20:52:02 +0000187 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1
DRC6acf52b2011-03-02 01:09:20 +0000188 || pitch<0)
DRC4f1580c2011-02-25 06:11:03 +0000189 _throw("Invalid argument in encodeYUV()");
DRC9b28def2011-05-21 14:37:15 +0000190 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
191 _throw("Mismatch between Java and C API");
DRC4f1580c2011-02-25 06:11:03 +0000192
DRC9b28def2011-05-21 14:37:15 +0000193 arraySize=(pitch==0)? width*tjPixelSize[pf]*height:pitch*height;
194 if((*env)->GetArrayLength(env, src)<arraySize)
DRC6acf52b2011-03-02 01:09:20 +0000195 _throw("Source buffer is not large enough");
DRCe6ab5392011-03-02 01:30:38 +0000196 if((*env)->GetArrayLength(env, dst)
DRC9b49f0e2011-07-12 03:17:23 +0000197 <(jsize)tjBufSizeYUV(width, height, subsamp))
DRC6acf52b2011-03-02 01:09:20 +0000198 _throw("Destination buffer is not large enough");
199
DRC9b28def2011-05-21 14:37:15 +0000200 bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
201 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRC4f1580c2011-02-25 06:11:03 +0000202
DRC9b28def2011-05-21 14:37:15 +0000203 if(tjEncodeYUV2(handle, srcBuf, width, pitch, height, pf, dstBuf, subsamp,
204 flags)==-1)
DRC4f1580c2011-02-25 06:11:03 +0000205 {
DRC9b28def2011-05-21 14:37:15 +0000206 (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
207 (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
208 dstBuf=srcBuf=NULL;
DRC4f1580c2011-02-25 06:11:03 +0000209 _throw(tjGetErrorStr());
210 }
211
212 bailout:
DRC9b28def2011-05-21 14:37:15 +0000213 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
214 if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
DRC4f1580c2011-02-25 06:11:03 +0000215 return;
216}
217
218JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIII_3BII
219 (JNIEnv *env, jobject obj, jintArray src, jint width, jint pitch,
DRC6acf52b2011-03-02 01:09:20 +0000220 jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
DRC4f1580c2011-02-25 06:11:03 +0000221{
222 tjhandle handle=0;
DRC9b28def2011-05-21 14:37:15 +0000223 jsize arraySize=0;
224 unsigned char *srcBuf=NULL, *dstBuf=NULL;
DRC4f1580c2011-02-25 06:11:03 +0000225
226 gethandle();
227
DRC92549de2011-03-15 20:52:02 +0000228 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1
DRC6acf52b2011-03-02 01:09:20 +0000229 || pitch<0)
DRC4f1580c2011-02-25 06:11:03 +0000230 _throw("Invalid argument in compress()");
DRC9b28def2011-05-21 14:37:15 +0000231 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
232 _throw("Mismatch between Java and C API");
233 if(tjPixelSize[pf]!=sizeof(jint))
DRC4f1580c2011-02-25 06:11:03 +0000234 _throw("Pixel format must be 32-bit when encoding from an integer buffer.");
DRC4f1580c2011-02-25 06:11:03 +0000235
DRC9b28def2011-05-21 14:37:15 +0000236 arraySize=(pitch==0)? width*height:pitch*height;
237 if((*env)->GetArrayLength(env, src)<arraySize)
DRC6acf52b2011-03-02 01:09:20 +0000238 _throw("Source buffer is not large enough");
DRCe6ab5392011-03-02 01:30:38 +0000239 if((*env)->GetArrayLength(env, dst)
DRC9b49f0e2011-07-12 03:17:23 +0000240 <(jsize)tjBufSizeYUV(width, height, subsamp))
DRC6acf52b2011-03-02 01:09:20 +0000241 _throw("Destination buffer is not large enough");
242
DRC9b28def2011-05-21 14:37:15 +0000243 bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
244 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRC4f1580c2011-02-25 06:11:03 +0000245
DRC9b28def2011-05-21 14:37:15 +0000246 if(tjEncodeYUV2(handle, srcBuf, width, pitch*sizeof(jint), height, pf,
247 dstBuf, subsamp, flags)==-1)
DRC4f1580c2011-02-25 06:11:03 +0000248 {
DRC9b28def2011-05-21 14:37:15 +0000249 (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
250 (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
251 dstBuf=srcBuf=NULL;
DRC4f1580c2011-02-25 06:11:03 +0000252 _throw(tjGetErrorStr());
253 }
254
255 bailout:
DRC9b28def2011-05-21 14:37:15 +0000256 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
257 if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
DRC4f1580c2011-02-25 06:11:03 +0000258 return;
259}
260
DRCc5a41992011-02-08 06:54:36 +0000261JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy
DRCf8e00552011-02-04 11:06:36 +0000262 (JNIEnv *env, jobject obj)
263{
264 tjhandle handle=0;
265
266 gethandle();
267
268 if(tjDestroy(handle)==-1) _throw(tjGetErrorStr());
DRC3bad53f2011-02-23 02:20:49 +0000269 (*env)->SetLongField(env, obj, _fid, 0);
DRCf8e00552011-02-04 11:06:36 +0000270
271 bailout:
272 return;
273}
274
DRCc5a41992011-02-08 06:54:36 +0000275JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_init
DRCf8e00552011-02-04 11:06:36 +0000276 (JNIEnv *env, jobject obj)
277{
278 jclass cls;
279 jfieldID fid;
280 tjhandle handle;
281
DRC1a3dbe62011-02-28 10:51:55 +0000282 if((handle=tjInitDecompress())==NULL) _throw(tjGetErrorStr());
DRCf8e00552011-02-04 11:06:36 +0000283
284 bailif0(cls=(*env)->GetObjectClass(env, obj));
285 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J"));
DRC23da0c02011-03-04 15:28:16 +0000286 (*env)->SetLongField(env, obj, fid, (jlong)handle);
DRCf8e00552011-02-04 11:06:36 +0000287
288 bailout:
289 return;
290}
291
DRC109a5782011-03-01 09:53:07 +0000292JNIEXPORT jobjectArray JNICALL Java_org_libjpegturbo_turbojpeg_TJ_getScalingFactors
293 (JNIEnv *env, jclass cls)
DRC36336fc2011-02-22 10:27:31 +0000294{
DRC109a5782011-03-01 09:53:07 +0000295 jclass sfcls=NULL; jfieldID fid=0;
296 tjscalingfactor *sf=NULL; int n=0, i;
297 jobject sfobj=NULL;
298 jobjectArray sfjava=NULL;
299
300 if((sf=tjGetScalingFactors(&n))==NULL || n==0)
DRC36336fc2011-02-22 10:27:31 +0000301 _throw(tjGetErrorStr());
302
DRCb2f94152011-04-02 02:09:03 +0000303 bailif0(sfcls=(*env)->FindClass(env, "org/libjpegturbo/turbojpeg/TJScalingFactor"));
DRC109a5782011-03-01 09:53:07 +0000304 bailif0(sfjava=(jobjectArray)(*env)->NewObjectArray(env, n, sfcls, 0));
DRC36336fc2011-02-22 10:27:31 +0000305
DRC109a5782011-03-01 09:53:07 +0000306 for(i=0; i<n; i++)
307 {
308 bailif0(sfobj=(*env)->AllocObject(env, sfcls));
309 bailif0(fid=(*env)->GetFieldID(env, sfcls, "num", "I"));
310 (*env)->SetIntField(env, sfobj, fid, sf[i].num);
311 bailif0(fid=(*env)->GetFieldID(env, sfcls, "denom", "I"));
312 (*env)->SetIntField(env, sfobj, fid, sf[i].denom);
313 (*env)->SetObjectArrayElement(env, sfjava, i, sfobj);
314 }
DRC36336fc2011-02-22 10:27:31 +0000315
316 bailout:
DRC109a5782011-03-01 09:53:07 +0000317 return sfjava;
DRC36336fc2011-02-22 10:27:31 +0000318}
319
DRC3bad53f2011-02-23 02:20:49 +0000320JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressHeader
DRC9b28def2011-05-21 14:37:15 +0000321 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize)
DRCf8e00552011-02-04 11:06:36 +0000322{
DRCf8e00552011-02-04 11:06:36 +0000323 tjhandle handle=0;
DRC9b28def2011-05-21 14:37:15 +0000324 unsigned char *jpegBuf=NULL;
325 int width=0, height=0, jpegSubsamp=-1;
DRCf8e00552011-02-04 11:06:36 +0000326
327 gethandle();
328
DRC9b28def2011-05-21 14:37:15 +0000329 if((*env)->GetArrayLength(env, src)<jpegSize)
DRC6acf52b2011-03-02 01:09:20 +0000330 _throw("Source buffer is not large enough");
331
DRC9b28def2011-05-21 14:37:15 +0000332 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
DRCf8e00552011-02-04 11:06:36 +0000333
DRC9b28def2011-05-21 14:37:15 +0000334 if(tjDecompressHeader2(handle, jpegBuf, (unsigned long)jpegSize,
335 &width, &height, &jpegSubsamp)==-1)
DRCf8e00552011-02-04 11:06:36 +0000336 {
DRC9b28def2011-05-21 14:37:15 +0000337 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
DRCf8e00552011-02-04 11:06:36 +0000338 _throw(tjGetErrorStr());
339 }
DRC9b28def2011-05-21 14:37:15 +0000340 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); jpegBuf=NULL;
DRCf8e00552011-02-04 11:06:36 +0000341
DRC3bad53f2011-02-23 02:20:49 +0000342 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
DRC9b28def2011-05-21 14:37:15 +0000343 (*env)->SetIntField(env, obj, _fid, jpegSubsamp);
DRC3bad53f2011-02-23 02:20:49 +0000344 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
345 (*env)->SetIntField(env, obj, _fid, width);
346 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
347 (*env)->SetIntField(env, obj, _fid, height);
DRCf8e00552011-02-04 11:06:36 +0000348
349 bailout:
DRC3bad53f2011-02-23 02:20:49 +0000350 return;
DRCf8e00552011-02-04 11:06:36 +0000351}
352
DRC84a1bcc2011-02-23 12:09:56 +0000353JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIII
DRC9b28def2011-05-21 14:37:15 +0000354 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
DRC4f1580c2011-02-25 06:11:03 +0000355 jint width, jint pitch, jint height, jint pf, jint flags)
DRCf8e00552011-02-04 11:06:36 +0000356{
DRC9b28def2011-05-21 14:37:15 +0000357 tjhandle handle=0;
358 jsize arraySize=0;
359 unsigned char *jpegBuf=NULL, *dstBuf=NULL;
DRCf8e00552011-02-04 11:06:36 +0000360
361 gethandle();
362
DRC92549de2011-03-15 20:52:02 +0000363 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
DRC4f1580c2011-02-25 06:11:03 +0000364 _throw("Invalid argument in decompress()");
DRC9b28def2011-05-21 14:37:15 +0000365 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
366 _throw("Mismatch between Java and C API");
DRC4f1580c2011-02-25 06:11:03 +0000367
DRC9b28def2011-05-21 14:37:15 +0000368 if((*env)->GetArrayLength(env, src)<jpegSize)
DRC6acf52b2011-03-02 01:09:20 +0000369 _throw("Source buffer is not large enough");
DRC9b28def2011-05-21 14:37:15 +0000370 arraySize=(pitch==0)? width*tjPixelSize[pf]*height:pitch*height;
371 if((*env)->GetArrayLength(env, dst)<arraySize)
DRC6acf52b2011-03-02 01:09:20 +0000372 _throw("Destination buffer is not large enough");
373
DRC9b28def2011-05-21 14:37:15 +0000374 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
375 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRCf8e00552011-02-04 11:06:36 +0000376
DRC9b28def2011-05-21 14:37:15 +0000377 if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize, dstBuf, width,
378 pitch, height, pf, flags)==-1)
DRCf8e00552011-02-04 11:06:36 +0000379 {
DRC9b28def2011-05-21 14:37:15 +0000380 (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
381 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
382 dstBuf=jpegBuf=NULL;
DRCf8e00552011-02-04 11:06:36 +0000383 _throw(tjGetErrorStr());
384 }
385
386 bailout:
DRC9b28def2011-05-21 14:37:15 +0000387 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
388 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
DRCf8e00552011-02-04 11:06:36 +0000389 return;
390}
391
DRC4f1580c2011-02-25 06:11:03 +0000392JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIII
DRC9b28def2011-05-21 14:37:15 +0000393 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst,
DRC4f1580c2011-02-25 06:11:03 +0000394 jint width, jint pitch, jint height, jint pf, jint flags)
DRC84a1bcc2011-02-23 12:09:56 +0000395{
DRC1a3dbe62011-02-28 10:51:55 +0000396 tjhandle handle=0;
DRC9b28def2011-05-21 14:37:15 +0000397 jsize arraySize=0;
398 unsigned char *jpegBuf=NULL, *dstBuf=NULL;
DRC84a1bcc2011-02-23 12:09:56 +0000399
400 gethandle();
401
DRC92549de2011-03-15 20:52:02 +0000402 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
DRC4f1580c2011-02-25 06:11:03 +0000403 _throw("Invalid argument in decompress()");
DRC9b28def2011-05-21 14:37:15 +0000404 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
405 _throw("Mismatch between Java and C API");
406 if(tjPixelSize[pf]!=sizeof(jint))
DRC4f1580c2011-02-25 06:11:03 +0000407 _throw("Pixel format must be 32-bit when decompressing to an integer buffer.");
DRC4f1580c2011-02-25 06:11:03 +0000408
DRC9b28def2011-05-21 14:37:15 +0000409 if((*env)->GetArrayLength(env, src)<jpegSize)
DRC6acf52b2011-03-02 01:09:20 +0000410 _throw("Source buffer is not large enough");
DRC9b28def2011-05-21 14:37:15 +0000411 arraySize=(pitch==0)? width*height:pitch*height;
412 if((*env)->GetArrayLength(env, dst)<arraySize)
DRC6acf52b2011-03-02 01:09:20 +0000413 _throw("Destination buffer is not large enough");
414
DRC9b28def2011-05-21 14:37:15 +0000415 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
416 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRC84a1bcc2011-02-23 12:09:56 +0000417
DRC9b28def2011-05-21 14:37:15 +0000418 if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize, dstBuf, width,
419 pitch*sizeof(jint), height, pf, flags)==-1)
DRC84a1bcc2011-02-23 12:09:56 +0000420 {
DRC9b28def2011-05-21 14:37:15 +0000421 (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
422 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
423 dstBuf=jpegBuf=NULL;
DRC84a1bcc2011-02-23 12:09:56 +0000424 _throw(tjGetErrorStr());
425 }
426
427 bailout:
DRC9b28def2011-05-21 14:37:15 +0000428 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
429 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
DRC84a1bcc2011-02-23 12:09:56 +0000430 return;
DRC4f1580c2011-02-25 06:11:03 +0000431}
DRC84a1bcc2011-02-23 12:09:56 +0000432
DRC4f1580c2011-02-25 06:11:03 +0000433JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV
DRC9b28def2011-05-21 14:37:15 +0000434 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
DRC4f1580c2011-02-25 06:11:03 +0000435 jint flags)
436{
437 tjhandle handle=0;
DRC9b28def2011-05-21 14:37:15 +0000438 unsigned char *jpegBuf=NULL, *dstBuf=NULL;
DRC6acf52b2011-03-02 01:09:20 +0000439 int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0;
DRC4f1580c2011-02-25 06:11:03 +0000440
441 gethandle();
442
DRC9b28def2011-05-21 14:37:15 +0000443 if((*env)->GetArrayLength(env, src)<jpegSize)
DRC6acf52b2011-03-02 01:09:20 +0000444 _throw("Source buffer is not large enough");
445 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
446 jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
447 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
448 jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
449 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
450 jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
451 if((*env)->GetArrayLength(env, dst)
DRC9b49f0e2011-07-12 03:17:23 +0000452 <(jsize)tjBufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp))
DRC6acf52b2011-03-02 01:09:20 +0000453 _throw("Destination buffer is not large enough");
454
DRC9b28def2011-05-21 14:37:15 +0000455 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
456 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRC4f1580c2011-02-25 06:11:03 +0000457
DRC9b28def2011-05-21 14:37:15 +0000458 if(tjDecompressToYUV(handle, jpegBuf, (unsigned long)jpegSize, dstBuf,
459 flags)==-1)
DRC4f1580c2011-02-25 06:11:03 +0000460 {
DRC9b28def2011-05-21 14:37:15 +0000461 (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
462 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
463 dstBuf=jpegBuf=NULL;
DRC4f1580c2011-02-25 06:11:03 +0000464 _throw(tjGetErrorStr());
465 }
466
467 bailout:
DRC9b28def2011-05-21 14:37:15 +0000468 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
469 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
DRC4f1580c2011-02-25 06:11:03 +0000470 return;
DRC84a1bcc2011-02-23 12:09:56 +0000471}
472
DRCe8573012011-03-04 10:13:59 +0000473JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_init
474 (JNIEnv *env, jobject obj)
475{
476 jclass cls;
477 jfieldID fid;
478 tjhandle handle;
479
480 if((handle=tjInitTransform())==NULL) _throw(tjGetErrorStr());
481
482 bailif0(cls=(*env)->GetObjectClass(env, obj));
483 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J"));
DRC23da0c02011-03-04 15:28:16 +0000484 (*env)->SetLongField(env, obj, fid, (jlong)handle);
DRCe8573012011-03-04 10:13:59 +0000485
486 bailout:
487 return;
488}
489
DRCf5467112011-09-20 05:02:19 +0000490typedef struct _JNICustomFilterParams
491{
492 JNIEnv *env;
493 jobject tobj;
494 jobject cfobj;
495} JNICustomFilterParams;
496
497static int JNICustomFilter(short *coeffs, tjregion arrayRegion,
498 tjregion planeRegion, int componentIndex, int transformIndex,
499 tjtransform *transform)
500{
501 JNICustomFilterParams *params=(JNICustomFilterParams *)transform->data;
502 JNIEnv *env=params->env;
503 jobject tobj=params->tobj, cfobj=params->cfobj;
504 jobject arrayRegionObj, planeRegionObj, bufobj, borobj;
505 jclass cls; jmethodID mid; jfieldID fid;
506
507 bailif0(bufobj=(*env)->NewDirectByteBuffer(env, coeffs,
508 sizeof(short)*arrayRegion.w*arrayRegion.h));
509 bailif0(cls=(*env)->FindClass(env, "java/nio/ByteOrder"));
510 bailif0(mid=(*env)->GetStaticMethodID(env, cls, "nativeOrder",
511 "()Ljava/nio/ByteOrder;"));
512 bailif0(borobj=(*env)->CallStaticObjectMethod(env, cls, mid));
513 bailif0(cls=(*env)->GetObjectClass(env, bufobj));
514 bailif0(mid=(*env)->GetMethodID(env, cls, "order",
515 "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;"));
516 (*env)->CallObjectMethod(env, bufobj, mid, borobj);
517 bailif0(mid=(*env)->GetMethodID(env, cls, "asShortBuffer",
518 "()Ljava/nio/ShortBuffer;"));
519 bailif0(bufobj=(*env)->CallObjectMethod(env, bufobj, mid));
520
521 bailif0(cls=(*env)->FindClass(env, "java/awt/Rectangle"));
522 bailif0(arrayRegionObj=(*env)->AllocObject(env, cls));
523 bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I"));
524 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.x);
525 bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I"));
526 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.y);
527 bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I"));
528 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.w);
529 bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I"));
530 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.h);
531
532 bailif0(planeRegionObj=(*env)->AllocObject(env, cls));
533 bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I"));
534 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.x);
535 bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I"));
536 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.y);
537 bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I"));
538 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.w);
539 bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I"));
540 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.h);
541
542 bailif0(cls=(*env)->GetObjectClass(env, cfobj));
543 bailif0(mid=(*env)->GetMethodID(env, cls, "customFilter",
544 "(Ljava/nio/ShortBuffer;Ljava/awt/Rectangle;Ljava/awt/Rectangle;IILorg/libjpegturbo/turbojpeg/TJTransform;)V"));
545 (*env)->CallVoidMethod(env, cfobj, mid, bufobj, arrayRegionObj,
546 planeRegionObj, componentIndex, transformIndex, tobj);
547
548 return 0;
549
550 bailout:
551 return -1;
552}
553
DRCe8573012011-03-04 10:13:59 +0000554JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transform
DRC9b28def2011-05-21 14:37:15 +0000555 (JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize,
DRCe8573012011-03-04 10:13:59 +0000556 jobjectArray dstobjs, jobjectArray tobjs, jint flags)
557{
558 tjhandle handle=0; int i;
DRC9b28def2011-05-21 14:37:15 +0000559 unsigned char *jpegBuf=NULL, **dstBufs=NULL; jsize n=0;
560 unsigned long *dstSizes=NULL; tjtransform *t=NULL;
561 jbyteArray *jdstBufs=NULL;
DRC9b49f0e2011-07-12 03:17:23 +0000562 int jpegWidth=0, jpegHeight=0, jpegSubsamp;
DRC9b28def2011-05-21 14:37:15 +0000563 jintArray jdstSizes=0; jint *dstSizesi=NULL;
DRCf5467112011-09-20 05:02:19 +0000564 JNICustomFilterParams *params=NULL;
DRCe8573012011-03-04 10:13:59 +0000565
566 gethandle();
567
DRC9b28def2011-05-21 14:37:15 +0000568 if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize)
DRCe8573012011-03-04 10:13:59 +0000569 _throw("Source buffer is not large enough");
570 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
571 jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
572 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
573 jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
DRC9b49f0e2011-07-12 03:17:23 +0000574 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
575 jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
DRCe8573012011-03-04 10:13:59 +0000576
577 n=(*env)->GetArrayLength(env, dstobjs);
578 if(n!=(*env)->GetArrayLength(env, tobjs))
579 _throw("Mismatch between size of transforms array and destination buffers array");
580
DRC9b28def2011-05-21 14:37:15 +0000581 if((dstBufs=(unsigned char **)malloc(sizeof(unsigned char *)*n))==NULL)
DRCe8573012011-03-04 10:13:59 +0000582 _throw("Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000583 if((jdstBufs=(jbyteArray *)malloc(sizeof(jbyteArray)*n))==NULL)
DRCe8573012011-03-04 10:13:59 +0000584 _throw("Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000585 if((dstSizes=(unsigned long *)malloc(sizeof(unsigned long)*n))==NULL)
DRCe8573012011-03-04 10:13:59 +0000586 _throw("Memory allocation failure");
587 if((t=(tjtransform *)malloc(sizeof(tjtransform)*n))==NULL)
588 _throw("Memory allocation failure");
DRCf5467112011-09-20 05:02:19 +0000589 if((params=(JNICustomFilterParams *)malloc(sizeof(JNICustomFilterParams)*n))
590 ==NULL)
591 _throw("Memory allocation failure");
DRCe8573012011-03-04 10:13:59 +0000592 for(i=0; i<n; i++)
593 {
DRC9b28def2011-05-21 14:37:15 +0000594 dstBufs[i]=NULL; jdstBufs[i]=NULL; dstSizes[i]=0;
DRCe8573012011-03-04 10:13:59 +0000595 memset(&t[i], 0, sizeof(tjtransform));
DRCf5467112011-09-20 05:02:19 +0000596 memset(&params[i], 0, sizeof(JNICustomFilterParams));
DRCe8573012011-03-04 10:13:59 +0000597 }
598
599 for(i=0; i<n; i++)
600 {
DRCf5467112011-09-20 05:02:19 +0000601 jobject tobj, cfobj;
DRCe8573012011-03-04 10:13:59 +0000602
603 bailif0(tobj=(*env)->GetObjectArrayElement(env, tobjs, i));
604 bailif0(_cls=(*env)->GetObjectClass(env, tobj));
605 bailif0(_fid=(*env)->GetFieldID(env, _cls, "op", "I"));
606 t[i].op=(*env)->GetIntField(env, tobj, _fid);
607 bailif0(_fid=(*env)->GetFieldID(env, _cls, "options", "I"));
608 t[i].options=(*env)->GetIntField(env, tobj, _fid);
609 bailif0(_fid=(*env)->GetFieldID(env, _cls, "x", "I"));
610 t[i].r.x=(*env)->GetIntField(env, tobj, _fid);
611 bailif0(_fid=(*env)->GetFieldID(env, _cls, "y", "I"));
612 t[i].r.y=(*env)->GetIntField(env, tobj, _fid);
613 bailif0(_fid=(*env)->GetFieldID(env, _cls, "width", "I"));
614 t[i].r.w=(*env)->GetIntField(env, tobj, _fid);
615 bailif0(_fid=(*env)->GetFieldID(env, _cls, "height", "I"));
616 t[i].r.h=(*env)->GetIntField(env, tobj, _fid);
DRCf5467112011-09-20 05:02:19 +0000617
DRCf5467112011-09-20 05:02:19 +0000618 bailif0(_fid=(*env)->GetFieldID(env, _cls, "cf",
619 "Lorg/libjpegturbo/turbojpeg/TJCustomFilter;"));
DRC06420c42011-09-26 18:46:09 +0000620 cfobj=(*env)->GetObjectField(env, tobj, _fid);
621 if(cfobj)
622 {
623 params[i].env=env;
624 params[i].tobj=tobj;
625 params[i].cfobj=cfobj;
626 t[i].customFilter=JNICustomFilter;
627 t[i].data=(void *)&params[i];
628 }
DRCe8573012011-03-04 10:13:59 +0000629 }
630
DRC9b28def2011-05-21 14:37:15 +0000631 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0));
DRCe8573012011-03-04 10:13:59 +0000632 for(i=0; i<n; i++)
633 {
634 int w=jpegWidth, h=jpegHeight;
635 if(t[i].r.w!=0) w=t[i].r.w;
636 if(t[i].r.h!=0) h=t[i].r.h;
DRC9b28def2011-05-21 14:37:15 +0000637 bailif0(jdstBufs[i]=(*env)->GetObjectArrayElement(env, dstobjs, i));
DRCefe28ce2012-01-17 11:48:38 +0000638 if((unsigned long)(*env)->GetArrayLength(env, jdstBufs[i])
639 <tjBufSize(w, h, jpegSubsamp))
DRCe8573012011-03-04 10:13:59 +0000640 _throw("Destination buffer is not large enough");
DRC9b28def2011-05-21 14:37:15 +0000641 bailif0(dstBufs[i]=(*env)->GetPrimitiveArrayCritical(env, jdstBufs[i], 0));
DRCe8573012011-03-04 10:13:59 +0000642 }
643
DRC9b28def2011-05-21 14:37:15 +0000644 if(tjTransform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t,
DRC4db92ad2011-05-25 04:52:25 +0000645 flags|TJFLAG_NOREALLOC)==-1)
DRCe8573012011-03-04 10:13:59 +0000646 {
DRC9b28def2011-05-21 14:37:15 +0000647 (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0);
648 jpegBuf=NULL;
DRCe8573012011-03-04 10:13:59 +0000649 for(i=0; i<n; i++)
650 {
DRC9b28def2011-05-21 14:37:15 +0000651 (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0);
652 dstBufs[i]=NULL;
DRCe8573012011-03-04 10:13:59 +0000653 }
654 _throw(tjGetErrorStr());
655 }
656
DRC9b28def2011-05-21 14:37:15 +0000657 jdstSizes=(*env)->NewIntArray(env, n);
658 bailif0(dstSizesi=(*env)->GetIntArrayElements(env, jdstSizes, 0));
659 for(i=0; i<n; i++) dstSizesi[i]=(int)dstSizes[i];
DRCe8573012011-03-04 10:13:59 +0000660
661 bailout:
DRC9b28def2011-05-21 14:37:15 +0000662 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0);
663 if(dstBufs)
DRCe8573012011-03-04 10:13:59 +0000664 {
665 for(i=0; i<n; i++)
666 {
DRC9b28def2011-05-21 14:37:15 +0000667 if(dstBufs[i] && jdstBufs && jdstBufs[i])
668 (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0);
DRCe8573012011-03-04 10:13:59 +0000669 }
DRC9b28def2011-05-21 14:37:15 +0000670 free(dstBufs);
DRCe8573012011-03-04 10:13:59 +0000671 }
DRC9b28def2011-05-21 14:37:15 +0000672 if(jdstBufs) free(jdstBufs);
673 if(dstSizes) free(dstSizes);
674 if(dstSizesi) (*env)->ReleaseIntArrayElements(env, jdstSizes, dstSizesi, 0);
DRCe8573012011-03-04 10:13:59 +0000675 if(t) free(t);
DRC9b28def2011-05-21 14:37:15 +0000676 return jdstSizes;
DRCe8573012011-03-04 10:13:59 +0000677}
678
DRCc5a41992011-02-08 06:54:36 +0000679JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_destroy
DRCf8e00552011-02-04 11:06:36 +0000680 (JNIEnv *env, jobject obj)
681{
DRCc5a41992011-02-08 06:54:36 +0000682 Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy(env, obj);
DRCf8e00552011-02-04 11:06:36 +0000683}