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