DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 1 | /* |
DRC | 1d29c5f | 2013-04-27 20:54:44 +0000 | [diff] [blame] | 2 | * Copyright (C)2011-2013 D. R. Commander. All Rights Reserved. |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 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 | |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 31 | #include "turbojpeg.h" |
DRC | bc2e66c | 2012-10-02 06:47:37 +0000 | [diff] [blame] | 32 | #ifdef WIN32 |
| 33 | #include "tjutil.h" |
| 34 | #endif |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 35 | #include <jni.h> |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 36 | #include "java/org_libjpegturbo_turbojpeg_TJCompressor.h" |
| 37 | #include "java/org_libjpegturbo_turbojpeg_TJDecompressor.h" |
| 38 | #include "java/org_libjpegturbo_turbojpeg_TJ.h" |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 39 | |
| 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 | |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 47 | #define bailif0(f) {if(!(f)) { \ |
| 48 | char temps[80]; \ |
| 49 | snprintf(temps, 80, "Unexpected NULL condition in line %d", __LINE__); \ |
| 50 | _throw(temps); \ |
| 51 | }} |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 52 | |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 53 | #define gethandle() \ |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 54 | jclass _cls=(*env)->GetObjectClass(env, obj); \ |
| 55 | jfieldID _fid; \ |
| 56 | if(!_cls) goto bailout; \ |
| 57 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "handle", "J")); \ |
DRC | 23da0c0 | 2011-03-04 15:28:16 +0000 | [diff] [blame] | 58 | handle=(tjhandle)(jlong)(*env)->GetLongField(env, obj, _fid); \ |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 59 | |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 60 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 61 | (JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 62 | { |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 63 | jint retval=(jint)tjBufSize(width, height, jpegSubsamp); |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 64 | if(retval==-1) _throw(tjGetErrorStr()); |
| 65 | |
| 66 | bailout: |
| 67 | return retval; |
| 68 | } |
| 69 | |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 70 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII |
| 71 | (JNIEnv *env, jclass cls, jint width, jint pad, jint height, jint subsamp) |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 72 | { |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 73 | jint retval=(jint)tjBufSizeYUV2(width, pad, height, subsamp); |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 74 | if(retval==-1) _throw(tjGetErrorStr()); |
| 75 | |
| 76 | bailout: |
| 77 | return retval; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 78 | } |
| 79 | |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 80 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__III |
| 81 | (JNIEnv *env, jclass cls, jint width, jint height, jint subsamp) |
| 82 | { |
| 83 | return Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII(env, cls, width, |
| 84 | 4, height, subsamp); |
| 85 | } |
| 86 | |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 87 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_init |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 88 | (JNIEnv *env, jobject obj) |
| 89 | { |
| 90 | jclass cls; |
| 91 | jfieldID fid; |
| 92 | tjhandle handle; |
| 93 | |
DRC | 1a3dbe6 | 2011-02-28 10:51:55 +0000 | [diff] [blame] | 94 | if((handle=tjInitCompress())==NULL) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 95 | _throw(tjGetErrorStr()); |
| 96 | |
| 97 | bailif0(cls=(*env)->GetObjectClass(env, obj)); |
| 98 | bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); |
DRC | 23da0c0 | 2011-03-04 15:28:16 +0000 | [diff] [blame] | 99 | (*env)->SetLongField(env, obj, fid, (jlong)handle); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 100 | |
| 101 | bailout: |
| 102 | return; |
| 103 | } |
| 104 | |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 105 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIIIII_3BIII |
| 106 | (JNIEnv *env, jobject obj, jbyteArray src, jint x, jint y, jint width, |
| 107 | jint pitch, jint height, jint pf, jbyteArray dst, jint jpegSubsamp, |
| 108 | jint jpegQual, jint flags) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 109 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 110 | tjhandle handle=0; |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 111 | unsigned long jpegSize=0; |
| 112 | jsize arraySize=0, actualPitch; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 113 | unsigned char *srcBuf=NULL, *jpegBuf=NULL; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 114 | |
| 115 | gethandle(); |
| 116 | |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 117 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 118 | || pitch<0) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 119 | _throw("Invalid argument in compress()"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 120 | if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) |
| 121 | _throw("Mismatch between Java and C API"); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 122 | |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 123 | actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch; |
| 124 | arraySize=(y+height-1)*actualPitch + x+width; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 125 | if((*env)->GetArrayLength(env, src)<arraySize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 126 | _throw("Source buffer is not large enough"); |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 127 | jpegSize=tjBufSize(width, height, jpegSubsamp); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 128 | if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 129 | _throw("Destination buffer is not large enough"); |
| 130 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 131 | bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 132 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 133 | |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 134 | if(tjCompress2(handle, &srcBuf[y*actualPitch + x*tjPixelSize[pf]], width, |
| 135 | pitch, height, pf, &jpegBuf, &jpegSize, jpegSubsamp, jpegQual, |
| 136 | flags|TJFLAG_NOREALLOC)==-1) |
| 137 | { |
| 138 | (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); |
| 139 | (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); |
| 140 | jpegBuf=srcBuf=NULL; |
| 141 | _throw(tjGetErrorStr()); |
| 142 | } |
| 143 | |
| 144 | bailout: |
| 145 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); |
| 146 | if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); |
| 147 | return (jint)jpegSize; |
| 148 | } |
| 149 | |
| 150 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIII_3BIII |
| 151 | (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch, |
| 152 | jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual, |
| 153 | jint flags) |
| 154 | { |
| 155 | return Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIIIII_3BIII( |
| 156 | env, obj, src, 0, 0, width, pitch, height, pf, dst, jpegSubsamp, jpegQual, |
| 157 | flags); |
| 158 | } |
| 159 | |
| 160 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIIIII_3BIII |
| 161 | (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width, |
| 162 | jint stride, jint height, jint pf, jbyteArray dst, jint jpegSubsamp, |
| 163 | jint jpegQual, jint flags) |
| 164 | { |
| 165 | tjhandle handle=0; |
| 166 | unsigned long jpegSize=0; |
| 167 | jsize arraySize=0, actualStride; |
| 168 | unsigned char *srcBuf=NULL, *jpegBuf=NULL; |
| 169 | |
| 170 | gethandle(); |
| 171 | |
| 172 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 |
| 173 | || stride<0) |
| 174 | _throw("Invalid argument in compress()"); |
| 175 | if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) |
| 176 | _throw("Mismatch between Java and C API"); |
| 177 | if(tjPixelSize[pf]!=sizeof(jint)) |
| 178 | _throw("Pixel format must be 32-bit when compressing from an integer buffer."); |
| 179 | |
| 180 | actualStride=(stride==0)? width:stride; |
| 181 | arraySize=(y+height-1)*actualStride + x+width; |
| 182 | if((*env)->GetArrayLength(env, src)<arraySize) |
| 183 | _throw("Source buffer is not large enough"); |
| 184 | jpegSize=tjBufSize(width, height, jpegSubsamp); |
| 185 | if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize) |
| 186 | _throw("Destination buffer is not large enough"); |
| 187 | |
| 188 | bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 189 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
| 190 | |
| 191 | if(tjCompress2(handle, &srcBuf[(y*actualStride + x)*sizeof(int)], width, |
| 192 | stride*sizeof(jint), height, pf, &jpegBuf, &jpegSize, jpegSubsamp, |
| 193 | jpegQual, flags|TJFLAG_NOREALLOC)==-1) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 194 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 195 | (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); |
| 196 | (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); |
| 197 | jpegBuf=srcBuf=NULL; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 198 | _throw(tjGetErrorStr()); |
| 199 | } |
| 200 | |
| 201 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 202 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); |
| 203 | if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); |
| 204 | return (jint)jpegSize; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 205 | } |
| 206 | |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 207 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIII_3BIII |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 208 | (JNIEnv *env, jobject obj, jintArray src, jint width, jint pitch, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 209 | jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual, |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 210 | jint flags) |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 211 | { |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 212 | return Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIIIII_3BIII( |
| 213 | env, obj, src, 0, 0, width, pitch, height, pf, dst, jpegSubsamp, jpegQual, |
| 214 | flags); |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 215 | } |
| 216 | |
DRC | 1e67274 | 2013-10-31 05:04:51 +0000 | [diff] [blame] | 217 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compressFromYUV___3BIIII_3BII |
| 218 | (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pad, jint height, |
| 219 | jint subsamp, jbyteArray dst, jint jpegQual, jint flags) |
| 220 | { |
| 221 | tjhandle handle=0; |
| 222 | unsigned long jpegSize=0; |
| 223 | jsize arraySize=0; |
| 224 | unsigned char *srcBuf=NULL, *jpegBuf=NULL; |
| 225 | |
| 226 | gethandle(); |
| 227 | |
| 228 | arraySize=tjBufSizeYUV2(width, pad, height, subsamp); |
| 229 | if((*env)->GetArrayLength(env, src)<arraySize) |
| 230 | _throw("Source buffer is not large enough"); |
| 231 | jpegSize=tjBufSize(width, height, subsamp); |
| 232 | if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize) |
| 233 | _throw("Destination buffer is not large enough"); |
| 234 | |
| 235 | bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 236 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
| 237 | |
| 238 | if(tjCompressFromYUV(handle, srcBuf, width, pad, height, subsamp, &jpegBuf, |
| 239 | &jpegSize, jpegQual, flags|TJFLAG_NOREALLOC)==-1) |
| 240 | { |
| 241 | (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); |
| 242 | (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); |
| 243 | jpegBuf=srcBuf=NULL; |
| 244 | _throw(tjGetErrorStr()); |
| 245 | } |
| 246 | |
| 247 | bailout: |
| 248 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); |
| 249 | if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); |
| 250 | return (jint)jpegSize; |
| 251 | } |
| 252 | |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 253 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIII_3BIII |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 254 | (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch, |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 255 | jint height, jint pf, jbyteArray dst, jint pad, jint subsamp, jint flags) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 256 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 257 | tjhandle handle=0; |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 258 | jsize arraySize=0, yuvSize; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 259 | unsigned char *srcBuf=NULL, *dstBuf=NULL; |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 260 | |
| 261 | gethandle(); |
| 262 | |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 263 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 264 | || pitch<0) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 265 | _throw("Invalid argument in encodeYUV()"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 266 | if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) |
| 267 | _throw("Mismatch between Java and C API"); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 268 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 269 | arraySize=(pitch==0)? width*tjPixelSize[pf]*height:pitch*height; |
| 270 | if((*env)->GetArrayLength(env, src)<arraySize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 271 | _throw("Source buffer is not large enough"); |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 272 | yuvSize=(jsize)tjBufSizeYUV2(width, pad, height, subsamp); |
| 273 | if(yuvSize==(unsigned long)-1) |
| 274 | _throw(tjGetErrorStr()); |
| 275 | if((*env)->GetArrayLength(env, dst)<yuvSize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 276 | _throw("Destination buffer is not large enough"); |
| 277 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 278 | bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 279 | bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 280 | |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 281 | if(tjEncodeYUV3(handle, srcBuf, width, pitch, height, pf, dstBuf, pad, |
| 282 | subsamp, flags)==-1) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 283 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 284 | (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 285 | (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); |
| 286 | dstBuf=srcBuf=NULL; |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 287 | _throw(tjGetErrorStr()); |
| 288 | } |
| 289 | |
| 290 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 291 | if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 292 | if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 296 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIII_3BII |
| 297 | (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch, |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 298 | jint height, jint pf, jbyteArray dst, jint subsamp, jint flags) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 299 | { |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 300 | Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIII_3BIII( |
| 301 | env, obj, src, width, pitch, height, pf, dst, 4, subsamp, flags); |
| 302 | } |
| 303 | |
| 304 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIII_3BIII |
| 305 | (JNIEnv *env, jobject obj, jintArray src, jint width, jint stride, |
| 306 | jint height, jint pf, jbyteArray dst, jint pad, jint subsamp, jint flags) |
| 307 | { |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 308 | tjhandle handle=0; |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 309 | jsize arraySize=0, yuvSize; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 310 | unsigned char *srcBuf=NULL, *dstBuf=NULL; |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 311 | |
| 312 | gethandle(); |
| 313 | |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 314 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 |
DRC | 1d29c5f | 2013-04-27 20:54:44 +0000 | [diff] [blame] | 315 | || stride<0) |
| 316 | _throw("Invalid argument in encodeYUV()"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 317 | if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) |
| 318 | _throw("Mismatch between Java and C API"); |
| 319 | if(tjPixelSize[pf]!=sizeof(jint)) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 320 | _throw("Pixel format must be 32-bit when encoding from an integer buffer."); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 321 | |
DRC | 1d29c5f | 2013-04-27 20:54:44 +0000 | [diff] [blame] | 322 | arraySize=(stride==0)? width*height:stride*height; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 323 | if((*env)->GetArrayLength(env, src)<arraySize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 324 | _throw("Source buffer is not large enough"); |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 325 | yuvSize=(jsize)tjBufSizeYUV2(width, pad, height, subsamp); |
| 326 | if(yuvSize==(unsigned long)-1) |
| 327 | _throw(tjGetErrorStr()); |
| 328 | if((*env)->GetArrayLength(env, dst)<yuvSize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 329 | _throw("Destination buffer is not large enough"); |
| 330 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 331 | bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 332 | bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 333 | |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 334 | if(tjEncodeYUV3(handle, srcBuf, width, stride*sizeof(jint), height, pf, |
| 335 | dstBuf, pad, subsamp, flags)==-1) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 336 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 337 | (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 338 | (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); |
| 339 | dstBuf=srcBuf=NULL; |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 340 | _throw(tjGetErrorStr()); |
| 341 | } |
| 342 | |
| 343 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 344 | if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 345 | if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 346 | return; |
| 347 | } |
| 348 | |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 349 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIII_3BII |
| 350 | (JNIEnv *env, jobject obj, jintArray src, jint width, jint pitch, |
| 351 | jint height, jint pf, jbyteArray dst, jint subsamp, jint flags) |
| 352 | { |
| 353 | Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIII_3BIII( |
| 354 | env, obj, src, width, pitch, height, pf, dst, 4, subsamp, flags); |
| 355 | } |
| 356 | |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 357 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 358 | (JNIEnv *env, jobject obj) |
| 359 | { |
| 360 | tjhandle handle=0; |
| 361 | |
| 362 | gethandle(); |
| 363 | |
| 364 | if(tjDestroy(handle)==-1) _throw(tjGetErrorStr()); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 365 | (*env)->SetLongField(env, obj, _fid, 0); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 366 | |
| 367 | bailout: |
| 368 | return; |
| 369 | } |
| 370 | |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 371 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_init |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 372 | (JNIEnv *env, jobject obj) |
| 373 | { |
| 374 | jclass cls; |
| 375 | jfieldID fid; |
| 376 | tjhandle handle; |
| 377 | |
DRC | 1a3dbe6 | 2011-02-28 10:51:55 +0000 | [diff] [blame] | 378 | if((handle=tjInitDecompress())==NULL) _throw(tjGetErrorStr()); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 379 | |
| 380 | bailif0(cls=(*env)->GetObjectClass(env, obj)); |
| 381 | bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); |
DRC | 23da0c0 | 2011-03-04 15:28:16 +0000 | [diff] [blame] | 382 | (*env)->SetLongField(env, obj, fid, (jlong)handle); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 383 | |
| 384 | bailout: |
| 385 | return; |
| 386 | } |
| 387 | |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 388 | JNIEXPORT jobjectArray JNICALL Java_org_libjpegturbo_turbojpeg_TJ_getScalingFactors |
| 389 | (JNIEnv *env, jclass cls) |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 390 | { |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 391 | jclass sfcls=NULL; jfieldID fid=0; |
| 392 | tjscalingfactor *sf=NULL; int n=0, i; |
| 393 | jobject sfobj=NULL; |
| 394 | jobjectArray sfjava=NULL; |
| 395 | |
| 396 | if((sf=tjGetScalingFactors(&n))==NULL || n==0) |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 397 | _throw(tjGetErrorStr()); |
| 398 | |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 399 | bailif0(sfcls=(*env)->FindClass(env, "org/libjpegturbo/turbojpeg/TJScalingFactor")); |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 400 | bailif0(sfjava=(jobjectArray)(*env)->NewObjectArray(env, n, sfcls, 0)); |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 401 | |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 402 | for(i=0; i<n; i++) |
| 403 | { |
| 404 | bailif0(sfobj=(*env)->AllocObject(env, sfcls)); |
| 405 | bailif0(fid=(*env)->GetFieldID(env, sfcls, "num", "I")); |
| 406 | (*env)->SetIntField(env, sfobj, fid, sf[i].num); |
| 407 | bailif0(fid=(*env)->GetFieldID(env, sfcls, "denom", "I")); |
| 408 | (*env)->SetIntField(env, sfobj, fid, sf[i].denom); |
| 409 | (*env)->SetObjectArrayElement(env, sfjava, i, sfobj); |
| 410 | } |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 411 | |
| 412 | bailout: |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 413 | return sfjava; |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 414 | } |
| 415 | |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 416 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressHeader |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 417 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 418 | { |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 419 | tjhandle handle=0; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 420 | unsigned char *jpegBuf=NULL; |
DRC | 38cb1ec | 2013-08-23 04:45:43 +0000 | [diff] [blame] | 421 | int width=0, height=0, jpegSubsamp=-1, jpegColorspace=-1; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 422 | |
| 423 | gethandle(); |
| 424 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 425 | if((*env)->GetArrayLength(env, src)<jpegSize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 426 | _throw("Source buffer is not large enough"); |
| 427 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 428 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 429 | |
DRC | 38cb1ec | 2013-08-23 04:45:43 +0000 | [diff] [blame] | 430 | if(tjDecompressHeader3(handle, jpegBuf, (unsigned long)jpegSize, |
| 431 | &width, &height, &jpegSubsamp, &jpegColorspace)==-1) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 432 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 433 | (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 434 | _throw(tjGetErrorStr()); |
| 435 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 436 | (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); jpegBuf=NULL; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 437 | |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 438 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 439 | (*env)->SetIntField(env, obj, _fid, jpegSubsamp); |
DRC | 38cb1ec | 2013-08-23 04:45:43 +0000 | [diff] [blame] | 440 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegColorspace", "I")); |
| 441 | (*env)->SetIntField(env, obj, _fid, jpegColorspace); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 442 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); |
| 443 | (*env)->SetIntField(env, obj, _fid, width); |
| 444 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); |
| 445 | (*env)->SetIntField(env, obj, _fid, height); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 446 | |
| 447 | bailout: |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 448 | return; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 449 | } |
| 450 | |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 451 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIIIII |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 452 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst, |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 453 | jint x, jint y, jint width, jint pitch, jint height, jint pf, jint flags) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 454 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 455 | tjhandle handle=0; |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 456 | jsize arraySize=0, actualPitch; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 457 | unsigned char *jpegBuf=NULL, *dstBuf=NULL; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 458 | |
| 459 | gethandle(); |
| 460 | |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 461 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 462 | _throw("Invalid argument in decompress()"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 463 | if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) |
| 464 | _throw("Mismatch between Java and C API"); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 465 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 466 | if((*env)->GetArrayLength(env, src)<jpegSize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 467 | _throw("Source buffer is not large enough"); |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 468 | actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch; |
DRC | dc31f0b | 2012-06-07 09:38:57 +0000 | [diff] [blame] | 469 | arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf]; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 470 | if((*env)->GetArrayLength(env, dst)<arraySize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 471 | _throw("Destination buffer is not large enough"); |
| 472 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 473 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 474 | bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 475 | |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 476 | if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize, |
| 477 | &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf, |
| 478 | flags)==-1) |
| 479 | { |
| 480 | (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 481 | (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
| 482 | dstBuf=jpegBuf=NULL; |
| 483 | _throw(tjGetErrorStr()); |
| 484 | } |
| 485 | |
| 486 | bailout: |
| 487 | if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 488 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIII |
| 493 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst, |
| 494 | jint width, jint pitch, jint height, jint pf, jint flags) |
| 495 | { |
| 496 | Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIIIII |
| 497 | (env, obj, src, jpegSize, dst, 0, 0, width, pitch, height, pf, flags); |
| 498 | } |
| 499 | |
| 500 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIIIII |
| 501 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst, |
| 502 | jint x, jint y, jint width, jint stride, jint height, jint pf, jint flags) |
| 503 | { |
| 504 | tjhandle handle=0; |
| 505 | jsize arraySize=0, actualStride; |
| 506 | unsigned char *jpegBuf=NULL, *dstBuf=NULL; |
| 507 | |
| 508 | gethandle(); |
| 509 | |
| 510 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) |
| 511 | _throw("Invalid argument in decompress()"); |
| 512 | if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) |
| 513 | _throw("Mismatch between Java and C API"); |
| 514 | if(tjPixelSize[pf]!=sizeof(jint)) |
| 515 | _throw("Pixel format must be 32-bit when decompressing to an integer buffer."); |
| 516 | |
| 517 | if((*env)->GetArrayLength(env, src)<jpegSize) |
| 518 | _throw("Source buffer is not large enough"); |
| 519 | actualStride=(stride==0)? width:stride; |
DRC | dc31f0b | 2012-06-07 09:38:57 +0000 | [diff] [blame] | 520 | arraySize=(y+height-1)*actualStride + x+width; |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 521 | if((*env)->GetArrayLength(env, dst)<arraySize) |
| 522 | _throw("Destination buffer is not large enough"); |
| 523 | |
| 524 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 525 | bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
| 526 | |
| 527 | if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize, |
| 528 | &dstBuf[(y*actualStride + x)*sizeof(int)], width, stride*sizeof(jint), |
| 529 | height, pf, flags)==-1) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 530 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 531 | (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 532 | (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
| 533 | dstBuf=jpegBuf=NULL; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 534 | _throw(tjGetErrorStr()); |
| 535 | } |
| 536 | |
| 537 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 538 | if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 539 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 540 | return; |
| 541 | } |
| 542 | |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 543 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIII |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 544 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst, |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 545 | jint width, jint stride, jint height, jint pf, jint flags) |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 546 | { |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 547 | Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIIIII |
| 548 | (env, obj, src, jpegSize, dst, 0, 0, width, stride, height, pf, flags); |
| 549 | |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 550 | } |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 551 | |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 552 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV___3BI_3BIIII |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 553 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst, |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 554 | jint desiredWidth, jint pad, jint desiredHeight, jint flags) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 555 | { |
| 556 | tjhandle handle=0; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 557 | unsigned char *jpegBuf=NULL, *dstBuf=NULL; |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 558 | int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0; |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 559 | jsize yuvSize; |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 560 | |
| 561 | gethandle(); |
| 562 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 563 | if((*env)->GetArrayLength(env, src)<jpegSize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 564 | _throw("Source buffer is not large enough"); |
| 565 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); |
| 566 | jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); |
| 567 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); |
| 568 | jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); |
| 569 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); |
| 570 | jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 571 | |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 572 | yuvSize=(jsize)tjBufSizeYUV2(desiredWidth==0? jpegWidth:desiredWidth, |
| 573 | pad, desiredHeight==0? jpegHeight:desiredHeight, jpegSubsamp); |
| 574 | if(yuvSize==(unsigned long)-1) |
| 575 | _throw(tjGetErrorStr()); |
| 576 | if((*env)->GetArrayLength(env, dst)<yuvSize) |
| 577 | _throw("Destination buffer is not large enough"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 578 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 579 | bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 580 | |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 581 | if(tjDecompressToYUV2(handle, jpegBuf, (unsigned long)jpegSize, dstBuf, |
| 582 | desiredWidth, pad, desiredHeight, flags)==-1) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 583 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 584 | (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 585 | (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
| 586 | dstBuf=jpegBuf=NULL; |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 587 | _throw(tjGetErrorStr()); |
| 588 | } |
| 589 | |
| 590 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 591 | if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 592 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 593 | return; |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 594 | } |
| 595 | |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 596 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV___3BI_3BI |
| 597 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst, |
| 598 | jint flags) |
| 599 | { |
| 600 | Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV___3BI_3BIIII( |
| 601 | env, obj, src, jpegSize, dst, 0, 4, 0, flags); |
| 602 | } |
| 603 | |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 604 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_init |
| 605 | (JNIEnv *env, jobject obj) |
| 606 | { |
| 607 | jclass cls; |
| 608 | jfieldID fid; |
| 609 | tjhandle handle; |
| 610 | |
| 611 | if((handle=tjInitTransform())==NULL) _throw(tjGetErrorStr()); |
| 612 | |
| 613 | bailif0(cls=(*env)->GetObjectClass(env, obj)); |
| 614 | bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); |
DRC | 23da0c0 | 2011-03-04 15:28:16 +0000 | [diff] [blame] | 615 | (*env)->SetLongField(env, obj, fid, (jlong)handle); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 616 | |
| 617 | bailout: |
| 618 | return; |
| 619 | } |
| 620 | |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 621 | typedef struct _JNICustomFilterParams |
| 622 | { |
| 623 | JNIEnv *env; |
| 624 | jobject tobj; |
| 625 | jobject cfobj; |
| 626 | } JNICustomFilterParams; |
| 627 | |
| 628 | static int JNICustomFilter(short *coeffs, tjregion arrayRegion, |
| 629 | tjregion planeRegion, int componentIndex, int transformIndex, |
| 630 | tjtransform *transform) |
| 631 | { |
| 632 | JNICustomFilterParams *params=(JNICustomFilterParams *)transform->data; |
| 633 | JNIEnv *env=params->env; |
| 634 | jobject tobj=params->tobj, cfobj=params->cfobj; |
| 635 | jobject arrayRegionObj, planeRegionObj, bufobj, borobj; |
| 636 | jclass cls; jmethodID mid; jfieldID fid; |
| 637 | |
| 638 | bailif0(bufobj=(*env)->NewDirectByteBuffer(env, coeffs, |
| 639 | sizeof(short)*arrayRegion.w*arrayRegion.h)); |
| 640 | bailif0(cls=(*env)->FindClass(env, "java/nio/ByteOrder")); |
| 641 | bailif0(mid=(*env)->GetStaticMethodID(env, cls, "nativeOrder", |
| 642 | "()Ljava/nio/ByteOrder;")); |
| 643 | bailif0(borobj=(*env)->CallStaticObjectMethod(env, cls, mid)); |
| 644 | bailif0(cls=(*env)->GetObjectClass(env, bufobj)); |
| 645 | bailif0(mid=(*env)->GetMethodID(env, cls, "order", |
| 646 | "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;")); |
| 647 | (*env)->CallObjectMethod(env, bufobj, mid, borobj); |
| 648 | bailif0(mid=(*env)->GetMethodID(env, cls, "asShortBuffer", |
| 649 | "()Ljava/nio/ShortBuffer;")); |
| 650 | bailif0(bufobj=(*env)->CallObjectMethod(env, bufobj, mid)); |
| 651 | |
| 652 | bailif0(cls=(*env)->FindClass(env, "java/awt/Rectangle")); |
| 653 | bailif0(arrayRegionObj=(*env)->AllocObject(env, cls)); |
| 654 | bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I")); |
| 655 | (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.x); |
| 656 | bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I")); |
| 657 | (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.y); |
| 658 | bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I")); |
| 659 | (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.w); |
| 660 | bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I")); |
| 661 | (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.h); |
| 662 | |
| 663 | bailif0(planeRegionObj=(*env)->AllocObject(env, cls)); |
| 664 | bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I")); |
| 665 | (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.x); |
| 666 | bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I")); |
| 667 | (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.y); |
| 668 | bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I")); |
| 669 | (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.w); |
| 670 | bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I")); |
| 671 | (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.h); |
| 672 | |
| 673 | bailif0(cls=(*env)->GetObjectClass(env, cfobj)); |
| 674 | bailif0(mid=(*env)->GetMethodID(env, cls, "customFilter", |
| 675 | "(Ljava/nio/ShortBuffer;Ljava/awt/Rectangle;Ljava/awt/Rectangle;IILorg/libjpegturbo/turbojpeg/TJTransform;)V")); |
| 676 | (*env)->CallVoidMethod(env, cfobj, mid, bufobj, arrayRegionObj, |
| 677 | planeRegionObj, componentIndex, transformIndex, tobj); |
| 678 | |
| 679 | return 0; |
| 680 | |
| 681 | bailout: |
| 682 | return -1; |
| 683 | } |
| 684 | |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 685 | JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transform |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 686 | (JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize, |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 687 | jobjectArray dstobjs, jobjectArray tobjs, jint flags) |
| 688 | { |
| 689 | tjhandle handle=0; int i; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 690 | unsigned char *jpegBuf=NULL, **dstBufs=NULL; jsize n=0; |
| 691 | unsigned long *dstSizes=NULL; tjtransform *t=NULL; |
| 692 | jbyteArray *jdstBufs=NULL; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 693 | int jpegWidth=0, jpegHeight=0, jpegSubsamp; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 694 | jintArray jdstSizes=0; jint *dstSizesi=NULL; |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 695 | JNICustomFilterParams *params=NULL; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 696 | |
| 697 | gethandle(); |
| 698 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 699 | if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 700 | _throw("Source buffer is not large enough"); |
| 701 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); |
| 702 | jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); |
| 703 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); |
| 704 | jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 705 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); |
| 706 | jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 707 | |
| 708 | n=(*env)->GetArrayLength(env, dstobjs); |
| 709 | if(n!=(*env)->GetArrayLength(env, tobjs)) |
| 710 | _throw("Mismatch between size of transforms array and destination buffers array"); |
| 711 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 712 | if((dstBufs=(unsigned char **)malloc(sizeof(unsigned char *)*n))==NULL) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 713 | _throw("Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 714 | if((jdstBufs=(jbyteArray *)malloc(sizeof(jbyteArray)*n))==NULL) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 715 | _throw("Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 716 | if((dstSizes=(unsigned long *)malloc(sizeof(unsigned long)*n))==NULL) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 717 | _throw("Memory allocation failure"); |
| 718 | if((t=(tjtransform *)malloc(sizeof(tjtransform)*n))==NULL) |
| 719 | _throw("Memory allocation failure"); |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 720 | if((params=(JNICustomFilterParams *)malloc(sizeof(JNICustomFilterParams)*n)) |
| 721 | ==NULL) |
| 722 | _throw("Memory allocation failure"); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 723 | for(i=0; i<n; i++) |
| 724 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 725 | dstBufs[i]=NULL; jdstBufs[i]=NULL; dstSizes[i]=0; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 726 | memset(&t[i], 0, sizeof(tjtransform)); |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 727 | memset(¶ms[i], 0, sizeof(JNICustomFilterParams)); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | for(i=0; i<n; i++) |
| 731 | { |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 732 | jobject tobj, cfobj; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 733 | |
| 734 | bailif0(tobj=(*env)->GetObjectArrayElement(env, tobjs, i)); |
| 735 | bailif0(_cls=(*env)->GetObjectClass(env, tobj)); |
| 736 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "op", "I")); |
| 737 | t[i].op=(*env)->GetIntField(env, tobj, _fid); |
| 738 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "options", "I")); |
| 739 | t[i].options=(*env)->GetIntField(env, tobj, _fid); |
| 740 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "x", "I")); |
| 741 | t[i].r.x=(*env)->GetIntField(env, tobj, _fid); |
| 742 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "y", "I")); |
| 743 | t[i].r.y=(*env)->GetIntField(env, tobj, _fid); |
| 744 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "width", "I")); |
| 745 | t[i].r.w=(*env)->GetIntField(env, tobj, _fid); |
| 746 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "height", "I")); |
| 747 | t[i].r.h=(*env)->GetIntField(env, tobj, _fid); |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 748 | |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 749 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "cf", |
| 750 | "Lorg/libjpegturbo/turbojpeg/TJCustomFilter;")); |
DRC | 06420c4 | 2011-09-26 18:46:09 +0000 | [diff] [blame] | 751 | cfobj=(*env)->GetObjectField(env, tobj, _fid); |
| 752 | if(cfobj) |
| 753 | { |
| 754 | params[i].env=env; |
| 755 | params[i].tobj=tobj; |
| 756 | params[i].cfobj=cfobj; |
| 757 | t[i].customFilter=JNICustomFilter; |
| 758 | t[i].data=(void *)¶ms[i]; |
| 759 | } |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 760 | } |
| 761 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 762 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0)); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 763 | for(i=0; i<n; i++) |
| 764 | { |
| 765 | int w=jpegWidth, h=jpegHeight; |
| 766 | if(t[i].r.w!=0) w=t[i].r.w; |
| 767 | if(t[i].r.h!=0) h=t[i].r.h; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 768 | bailif0(jdstBufs[i]=(*env)->GetObjectArrayElement(env, dstobjs, i)); |
DRC | efe28ce | 2012-01-17 11:48:38 +0000 | [diff] [blame] | 769 | if((unsigned long)(*env)->GetArrayLength(env, jdstBufs[i]) |
| 770 | <tjBufSize(w, h, jpegSubsamp)) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 771 | _throw("Destination buffer is not large enough"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 772 | bailif0(dstBufs[i]=(*env)->GetPrimitiveArrayCritical(env, jdstBufs[i], 0)); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 773 | } |
| 774 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 775 | if(tjTransform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t, |
DRC | 4db92ad | 2011-05-25 04:52:25 +0000 | [diff] [blame] | 776 | flags|TJFLAG_NOREALLOC)==-1) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 777 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 778 | (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0); |
| 779 | jpegBuf=NULL; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 780 | for(i=0; i<n; i++) |
| 781 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 782 | (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0); |
| 783 | dstBufs[i]=NULL; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 784 | } |
| 785 | _throw(tjGetErrorStr()); |
| 786 | } |
| 787 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 788 | jdstSizes=(*env)->NewIntArray(env, n); |
| 789 | bailif0(dstSizesi=(*env)->GetIntArrayElements(env, jdstSizes, 0)); |
| 790 | for(i=0; i<n; i++) dstSizesi[i]=(int)dstSizes[i]; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 791 | |
| 792 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 793 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0); |
| 794 | if(dstBufs) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 795 | { |
| 796 | for(i=0; i<n; i++) |
| 797 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 798 | if(dstBufs[i] && jdstBufs && jdstBufs[i]) |
| 799 | (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 800 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 801 | free(dstBufs); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 802 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 803 | if(jdstBufs) free(jdstBufs); |
| 804 | if(dstSizes) free(dstSizes); |
| 805 | if(dstSizesi) (*env)->ReleaseIntArrayElements(env, jdstSizes, dstSizesi, 0); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 806 | if(t) free(t); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 807 | return jdstSizes; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 808 | } |
| 809 | |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 810 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_destroy |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 811 | (JNIEnv *env, jobject obj) |
| 812 | { |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 813 | Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy(env, obj); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 814 | } |