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