blob: 3b9624a1ee957090b716f13317bd8dd522c9bd2b [file] [log] [blame]
DRCf8e00552011-02-04 11:06:36 +00001/*
DRCf659f432012-06-06 08:41:06 +00002 * Copyright (C)2011-2012 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"
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
DRCf659f432012-06-06 08:41:06 +0000353JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIIIII
DRC9b28def2011-05-21 14:37:15 +0000354 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
DRCf659f432012-06-06 08:41:06 +0000355 jint x, jint y, 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;
DRCf659f432012-06-06 08:41:06 +0000358 jsize arraySize=0, actualPitch;
DRC9b28def2011-05-21 14:37:15 +0000359 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");
DRCf659f432012-06-06 08:41:06 +0000370 actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
371 arraySize=(y+height)*actualPitch + x*tjPixelSize[pf];
DRC9b28def2011-05-21 14:37:15 +0000372 if((*env)->GetArrayLength(env, dst)<arraySize)
DRC6acf52b2011-03-02 01:09:20 +0000373 _throw("Destination buffer is not large enough");
374
DRC9b28def2011-05-21 14:37:15 +0000375 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
376 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRCf8e00552011-02-04 11:06:36 +0000377
DRCf659f432012-06-06 08:41:06 +0000378 if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize,
379 &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf,
380 flags)==-1)
381 {
382 (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
383 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
384 dstBuf=jpegBuf=NULL;
385 _throw(tjGetErrorStr());
386 }
387
388 bailout:
389 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
390 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
391 return;
392}
393
394JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIII
395 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
396 jint width, jint pitch, jint height, jint pf, jint flags)
397{
398 Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIIIII
399 (env, obj, src, jpegSize, dst, 0, 0, width, pitch, height, pf, flags);
400}
401
402JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIIIII
403 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst,
404 jint x, jint y, jint width, jint stride, jint height, jint pf, jint flags)
405{
406 tjhandle handle=0;
407 jsize arraySize=0, actualStride;
408 unsigned char *jpegBuf=NULL, *dstBuf=NULL;
409
410 gethandle();
411
412 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
413 _throw("Invalid argument in decompress()");
414 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
415 _throw("Mismatch between Java and C API");
416 if(tjPixelSize[pf]!=sizeof(jint))
417 _throw("Pixel format must be 32-bit when decompressing to an integer buffer.");
418
419 if((*env)->GetArrayLength(env, src)<jpegSize)
420 _throw("Source buffer is not large enough");
421 actualStride=(stride==0)? width:stride;
422 arraySize=(y+height)*actualStride + x;
423 if((*env)->GetArrayLength(env, dst)<arraySize)
424 _throw("Destination buffer is not large enough");
425
426 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
427 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
428
429 if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize,
430 &dstBuf[(y*actualStride + x)*sizeof(int)], width, stride*sizeof(jint),
431 height, pf, flags)==-1)
DRCf8e00552011-02-04 11:06:36 +0000432 {
DRC9b28def2011-05-21 14:37:15 +0000433 (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
434 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
435 dstBuf=jpegBuf=NULL;
DRCf8e00552011-02-04 11:06:36 +0000436 _throw(tjGetErrorStr());
437 }
438
439 bailout:
DRC9b28def2011-05-21 14:37:15 +0000440 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
441 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
DRCf8e00552011-02-04 11:06:36 +0000442 return;
443}
444
DRC4f1580c2011-02-25 06:11:03 +0000445JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIII
DRC9b28def2011-05-21 14:37:15 +0000446 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst,
DRCf659f432012-06-06 08:41:06 +0000447 jint width, jint stride, jint height, jint pf, jint flags)
DRC84a1bcc2011-02-23 12:09:56 +0000448{
DRCf659f432012-06-06 08:41:06 +0000449 Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIIIII
450 (env, obj, src, jpegSize, dst, 0, 0, width, stride, height, pf, flags);
451
DRC4f1580c2011-02-25 06:11:03 +0000452}
DRC84a1bcc2011-02-23 12:09:56 +0000453
DRC4f1580c2011-02-25 06:11:03 +0000454JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV
DRC9b28def2011-05-21 14:37:15 +0000455 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
DRC4f1580c2011-02-25 06:11:03 +0000456 jint flags)
457{
458 tjhandle handle=0;
DRC9b28def2011-05-21 14:37:15 +0000459 unsigned char *jpegBuf=NULL, *dstBuf=NULL;
DRC6acf52b2011-03-02 01:09:20 +0000460 int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0;
DRC4f1580c2011-02-25 06:11:03 +0000461
462 gethandle();
463
DRC9b28def2011-05-21 14:37:15 +0000464 if((*env)->GetArrayLength(env, src)<jpegSize)
DRC6acf52b2011-03-02 01:09:20 +0000465 _throw("Source buffer is not large enough");
466 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
467 jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
468 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
469 jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
470 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
471 jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
472 if((*env)->GetArrayLength(env, dst)
DRC9b49f0e2011-07-12 03:17:23 +0000473 <(jsize)tjBufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp))
DRC6acf52b2011-03-02 01:09:20 +0000474 _throw("Destination buffer is not large enough");
475
DRC9b28def2011-05-21 14:37:15 +0000476 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
477 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
DRC4f1580c2011-02-25 06:11:03 +0000478
DRC9b28def2011-05-21 14:37:15 +0000479 if(tjDecompressToYUV(handle, jpegBuf, (unsigned long)jpegSize, dstBuf,
480 flags)==-1)
DRC4f1580c2011-02-25 06:11:03 +0000481 {
DRC9b28def2011-05-21 14:37:15 +0000482 (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
483 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
484 dstBuf=jpegBuf=NULL;
DRC4f1580c2011-02-25 06:11:03 +0000485 _throw(tjGetErrorStr());
486 }
487
488 bailout:
DRC9b28def2011-05-21 14:37:15 +0000489 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
490 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
DRC4f1580c2011-02-25 06:11:03 +0000491 return;
DRC84a1bcc2011-02-23 12:09:56 +0000492}
493
DRCe8573012011-03-04 10:13:59 +0000494JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_init
495 (JNIEnv *env, jobject obj)
496{
497 jclass cls;
498 jfieldID fid;
499 tjhandle handle;
500
501 if((handle=tjInitTransform())==NULL) _throw(tjGetErrorStr());
502
503 bailif0(cls=(*env)->GetObjectClass(env, obj));
504 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J"));
DRC23da0c02011-03-04 15:28:16 +0000505 (*env)->SetLongField(env, obj, fid, (jlong)handle);
DRCe8573012011-03-04 10:13:59 +0000506
507 bailout:
508 return;
509}
510
DRCf5467112011-09-20 05:02:19 +0000511typedef struct _JNICustomFilterParams
512{
513 JNIEnv *env;
514 jobject tobj;
515 jobject cfobj;
516} JNICustomFilterParams;
517
518static int JNICustomFilter(short *coeffs, tjregion arrayRegion,
519 tjregion planeRegion, int componentIndex, int transformIndex,
520 tjtransform *transform)
521{
522 JNICustomFilterParams *params=(JNICustomFilterParams *)transform->data;
523 JNIEnv *env=params->env;
524 jobject tobj=params->tobj, cfobj=params->cfobj;
525 jobject arrayRegionObj, planeRegionObj, bufobj, borobj;
526 jclass cls; jmethodID mid; jfieldID fid;
527
528 bailif0(bufobj=(*env)->NewDirectByteBuffer(env, coeffs,
529 sizeof(short)*arrayRegion.w*arrayRegion.h));
530 bailif0(cls=(*env)->FindClass(env, "java/nio/ByteOrder"));
531 bailif0(mid=(*env)->GetStaticMethodID(env, cls, "nativeOrder",
532 "()Ljava/nio/ByteOrder;"));
533 bailif0(borobj=(*env)->CallStaticObjectMethod(env, cls, mid));
534 bailif0(cls=(*env)->GetObjectClass(env, bufobj));
535 bailif0(mid=(*env)->GetMethodID(env, cls, "order",
536 "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;"));
537 (*env)->CallObjectMethod(env, bufobj, mid, borobj);
538 bailif0(mid=(*env)->GetMethodID(env, cls, "asShortBuffer",
539 "()Ljava/nio/ShortBuffer;"));
540 bailif0(bufobj=(*env)->CallObjectMethod(env, bufobj, mid));
541
542 bailif0(cls=(*env)->FindClass(env, "java/awt/Rectangle"));
543 bailif0(arrayRegionObj=(*env)->AllocObject(env, cls));
544 bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I"));
545 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.x);
546 bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I"));
547 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.y);
548 bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I"));
549 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.w);
550 bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I"));
551 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.h);
552
553 bailif0(planeRegionObj=(*env)->AllocObject(env, cls));
554 bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I"));
555 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.x);
556 bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I"));
557 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.y);
558 bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I"));
559 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.w);
560 bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I"));
561 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.h);
562
563 bailif0(cls=(*env)->GetObjectClass(env, cfobj));
564 bailif0(mid=(*env)->GetMethodID(env, cls, "customFilter",
565 "(Ljava/nio/ShortBuffer;Ljava/awt/Rectangle;Ljava/awt/Rectangle;IILorg/libjpegturbo/turbojpeg/TJTransform;)V"));
566 (*env)->CallVoidMethod(env, cfobj, mid, bufobj, arrayRegionObj,
567 planeRegionObj, componentIndex, transformIndex, tobj);
568
569 return 0;
570
571 bailout:
572 return -1;
573}
574
DRCe8573012011-03-04 10:13:59 +0000575JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transform
DRC9b28def2011-05-21 14:37:15 +0000576 (JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize,
DRCe8573012011-03-04 10:13:59 +0000577 jobjectArray dstobjs, jobjectArray tobjs, jint flags)
578{
579 tjhandle handle=0; int i;
DRC9b28def2011-05-21 14:37:15 +0000580 unsigned char *jpegBuf=NULL, **dstBufs=NULL; jsize n=0;
581 unsigned long *dstSizes=NULL; tjtransform *t=NULL;
582 jbyteArray *jdstBufs=NULL;
DRC9b49f0e2011-07-12 03:17:23 +0000583 int jpegWidth=0, jpegHeight=0, jpegSubsamp;
DRC9b28def2011-05-21 14:37:15 +0000584 jintArray jdstSizes=0; jint *dstSizesi=NULL;
DRCf5467112011-09-20 05:02:19 +0000585 JNICustomFilterParams *params=NULL;
DRCe8573012011-03-04 10:13:59 +0000586
587 gethandle();
588
DRC9b28def2011-05-21 14:37:15 +0000589 if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize)
DRCe8573012011-03-04 10:13:59 +0000590 _throw("Source buffer is not large enough");
591 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
592 jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
593 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
594 jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
DRC9b49f0e2011-07-12 03:17:23 +0000595 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
596 jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
DRCe8573012011-03-04 10:13:59 +0000597
598 n=(*env)->GetArrayLength(env, dstobjs);
599 if(n!=(*env)->GetArrayLength(env, tobjs))
600 _throw("Mismatch between size of transforms array and destination buffers array");
601
DRC9b28def2011-05-21 14:37:15 +0000602 if((dstBufs=(unsigned char **)malloc(sizeof(unsigned char *)*n))==NULL)
DRCe8573012011-03-04 10:13:59 +0000603 _throw("Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000604 if((jdstBufs=(jbyteArray *)malloc(sizeof(jbyteArray)*n))==NULL)
DRCe8573012011-03-04 10:13:59 +0000605 _throw("Memory allocation failure");
DRC9b28def2011-05-21 14:37:15 +0000606 if((dstSizes=(unsigned long *)malloc(sizeof(unsigned long)*n))==NULL)
DRCe8573012011-03-04 10:13:59 +0000607 _throw("Memory allocation failure");
608 if((t=(tjtransform *)malloc(sizeof(tjtransform)*n))==NULL)
609 _throw("Memory allocation failure");
DRCf5467112011-09-20 05:02:19 +0000610 if((params=(JNICustomFilterParams *)malloc(sizeof(JNICustomFilterParams)*n))
611 ==NULL)
612 _throw("Memory allocation failure");
DRCe8573012011-03-04 10:13:59 +0000613 for(i=0; i<n; i++)
614 {
DRC9b28def2011-05-21 14:37:15 +0000615 dstBufs[i]=NULL; jdstBufs[i]=NULL; dstSizes[i]=0;
DRCe8573012011-03-04 10:13:59 +0000616 memset(&t[i], 0, sizeof(tjtransform));
DRCf5467112011-09-20 05:02:19 +0000617 memset(&params[i], 0, sizeof(JNICustomFilterParams));
DRCe8573012011-03-04 10:13:59 +0000618 }
619
620 for(i=0; i<n; i++)
621 {
DRCf5467112011-09-20 05:02:19 +0000622 jobject tobj, cfobj;
DRCe8573012011-03-04 10:13:59 +0000623
624 bailif0(tobj=(*env)->GetObjectArrayElement(env, tobjs, i));
625 bailif0(_cls=(*env)->GetObjectClass(env, tobj));
626 bailif0(_fid=(*env)->GetFieldID(env, _cls, "op", "I"));
627 t[i].op=(*env)->GetIntField(env, tobj, _fid);
628 bailif0(_fid=(*env)->GetFieldID(env, _cls, "options", "I"));
629 t[i].options=(*env)->GetIntField(env, tobj, _fid);
630 bailif0(_fid=(*env)->GetFieldID(env, _cls, "x", "I"));
631 t[i].r.x=(*env)->GetIntField(env, tobj, _fid);
632 bailif0(_fid=(*env)->GetFieldID(env, _cls, "y", "I"));
633 t[i].r.y=(*env)->GetIntField(env, tobj, _fid);
634 bailif0(_fid=(*env)->GetFieldID(env, _cls, "width", "I"));
635 t[i].r.w=(*env)->GetIntField(env, tobj, _fid);
636 bailif0(_fid=(*env)->GetFieldID(env, _cls, "height", "I"));
637 t[i].r.h=(*env)->GetIntField(env, tobj, _fid);
DRCf5467112011-09-20 05:02:19 +0000638
DRCf5467112011-09-20 05:02:19 +0000639 bailif0(_fid=(*env)->GetFieldID(env, _cls, "cf",
640 "Lorg/libjpegturbo/turbojpeg/TJCustomFilter;"));
DRC06420c42011-09-26 18:46:09 +0000641 cfobj=(*env)->GetObjectField(env, tobj, _fid);
642 if(cfobj)
643 {
644 params[i].env=env;
645 params[i].tobj=tobj;
646 params[i].cfobj=cfobj;
647 t[i].customFilter=JNICustomFilter;
648 t[i].data=(void *)&params[i];
649 }
DRCe8573012011-03-04 10:13:59 +0000650 }
651
DRC9b28def2011-05-21 14:37:15 +0000652 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0));
DRCe8573012011-03-04 10:13:59 +0000653 for(i=0; i<n; i++)
654 {
655 int w=jpegWidth, h=jpegHeight;
656 if(t[i].r.w!=0) w=t[i].r.w;
657 if(t[i].r.h!=0) h=t[i].r.h;
DRC9b28def2011-05-21 14:37:15 +0000658 bailif0(jdstBufs[i]=(*env)->GetObjectArrayElement(env, dstobjs, i));
DRCefe28ce2012-01-17 11:48:38 +0000659 if((unsigned long)(*env)->GetArrayLength(env, jdstBufs[i])
660 <tjBufSize(w, h, jpegSubsamp))
DRCe8573012011-03-04 10:13:59 +0000661 _throw("Destination buffer is not large enough");
DRC9b28def2011-05-21 14:37:15 +0000662 bailif0(dstBufs[i]=(*env)->GetPrimitiveArrayCritical(env, jdstBufs[i], 0));
DRCe8573012011-03-04 10:13:59 +0000663 }
664
DRC9b28def2011-05-21 14:37:15 +0000665 if(tjTransform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t,
DRC4db92ad2011-05-25 04:52:25 +0000666 flags|TJFLAG_NOREALLOC)==-1)
DRCe8573012011-03-04 10:13:59 +0000667 {
DRC9b28def2011-05-21 14:37:15 +0000668 (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0);
669 jpegBuf=NULL;
DRCe8573012011-03-04 10:13:59 +0000670 for(i=0; i<n; i++)
671 {
DRC9b28def2011-05-21 14:37:15 +0000672 (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0);
673 dstBufs[i]=NULL;
DRCe8573012011-03-04 10:13:59 +0000674 }
675 _throw(tjGetErrorStr());
676 }
677
DRC9b28def2011-05-21 14:37:15 +0000678 jdstSizes=(*env)->NewIntArray(env, n);
679 bailif0(dstSizesi=(*env)->GetIntArrayElements(env, jdstSizes, 0));
680 for(i=0; i<n; i++) dstSizesi[i]=(int)dstSizes[i];
DRCe8573012011-03-04 10:13:59 +0000681
682 bailout:
DRC9b28def2011-05-21 14:37:15 +0000683 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0);
684 if(dstBufs)
DRCe8573012011-03-04 10:13:59 +0000685 {
686 for(i=0; i<n; i++)
687 {
DRC9b28def2011-05-21 14:37:15 +0000688 if(dstBufs[i] && jdstBufs && jdstBufs[i])
689 (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0);
DRCe8573012011-03-04 10:13:59 +0000690 }
DRC9b28def2011-05-21 14:37:15 +0000691 free(dstBufs);
DRCe8573012011-03-04 10:13:59 +0000692 }
DRC9b28def2011-05-21 14:37:15 +0000693 if(jdstBufs) free(jdstBufs);
694 if(dstSizes) free(dstSizes);
695 if(dstSizesi) (*env)->ReleaseIntArrayElements(env, jdstSizes, dstSizesi, 0);
DRCe8573012011-03-04 10:13:59 +0000696 if(t) free(t);
DRC9b28def2011-05-21 14:37:15 +0000697 return jdstSizes;
DRCe8573012011-03-04 10:13:59 +0000698}
699
DRCc5a41992011-02-08 06:54:36 +0000700JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_destroy
DRCf8e00552011-02-04 11:06:36 +0000701 (JNIEnv *env, jobject obj)
702{
DRCc5a41992011-02-08 06:54:36 +0000703 Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy(env, obj);
DRCf8e00552011-02-04 11:06:36 +0000704}