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 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 40 | #define PAD(v, p) ((v+(p)-1)&(~((p)-1))) |
| 41 | |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 42 | #define _throw(msg) { \ |
| 43 | jclass _exccls=(*env)->FindClass(env, "java/lang/Exception"); \ |
| 44 | if(!_exccls) goto bailout; \ |
| 45 | (*env)->ThrowNew(env, _exccls, msg); \ |
| 46 | goto bailout; \ |
| 47 | } |
| 48 | |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 49 | #define bailif0(f) {if(!(f)) { \ |
| 50 | char temps[80]; \ |
| 51 | snprintf(temps, 80, "Unexpected NULL condition in line %d", __LINE__); \ |
| 52 | _throw(temps); \ |
| 53 | }} |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 54 | |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 55 | #define gethandle() \ |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 56 | jclass _cls=(*env)->GetObjectClass(env, obj); \ |
| 57 | jfieldID _fid; \ |
| 58 | if(!_cls) goto bailout; \ |
| 59 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "handle", "J")); \ |
DRC | 46a0392 | 2014-08-15 14:40:05 +0000 | [diff] [blame] | 60 | handle=(tjhandle)(size_t)(*env)->GetLongField(env, obj, _fid); \ |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 61 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 62 | /* TurboJPEG 1.2.x: TJ::bufSize() */ |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 63 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 64 | (JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 65 | { |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 66 | jint retval=(jint)tjBufSize(width, height, jpegSubsamp); |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 67 | if(retval==-1) _throw(tjGetErrorStr()); |
| 68 | |
| 69 | bailout: |
| 70 | return retval; |
| 71 | } |
| 72 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 73 | /* TurboJPEG 1.4.x: TJ::bufSizeYUV() */ |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 74 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII |
| 75 | (JNIEnv *env, jclass cls, jint width, jint pad, jint height, jint subsamp) |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 76 | { |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 77 | jint retval=(jint)tjBufSizeYUV2(width, pad, height, subsamp); |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 78 | if(retval==-1) _throw(tjGetErrorStr()); |
| 79 | |
| 80 | bailout: |
| 81 | return retval; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 82 | } |
| 83 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 84 | /* TurboJPEG 1.2.x: TJ::bufSizeYUV() */ |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 85 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__III |
| 86 | (JNIEnv *env, jclass cls, jint width, jint height, jint subsamp) |
| 87 | { |
| 88 | return Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII(env, cls, width, |
| 89 | 4, height, subsamp); |
| 90 | } |
| 91 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 92 | /* TurboJPEG 1.4.x: TJ::planeSizeYUV() */ |
| 93 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeSizeYUV__IIIII |
| 94 | (JNIEnv *env, jclass cls, jint componentID, jint width, jint stride, |
| 95 | jint height, jint subsamp) |
| 96 | { |
| 97 | jint retval=(jint)tjPlaneSizeYUV(componentID, width, stride, height, |
| 98 | subsamp); |
| 99 | if(retval==-1) _throw(tjGetErrorStr()); |
| 100 | |
| 101 | bailout: |
| 102 | return retval; |
| 103 | } |
| 104 | |
| 105 | /* TurboJPEG 1.4.x: TJ::planeWidth() */ |
| 106 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeWidth__III |
| 107 | (JNIEnv *env, jclass cls, jint componentID, jint width, jint subsamp) |
| 108 | { |
| 109 | jint retval=(jint)tjPlaneWidth(componentID, width, subsamp); |
| 110 | if(retval==-1) _throw(tjGetErrorStr()); |
| 111 | |
| 112 | bailout: |
| 113 | return retval; |
| 114 | } |
| 115 | |
| 116 | /* TurboJPEG 1.4.x: TJ::planeHeight() */ |
| 117 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeHeight__III |
| 118 | (JNIEnv *env, jclass cls, jint componentID, jint height, jint subsamp) |
| 119 | { |
| 120 | jint retval=(jint)tjPlaneHeight(componentID, height, subsamp); |
| 121 | if(retval==-1) _throw(tjGetErrorStr()); |
| 122 | |
| 123 | bailout: |
| 124 | return retval; |
| 125 | } |
| 126 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 127 | /* TurboJPEG 1.2.x: TJCompressor::init() */ |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 128 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_init |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 129 | (JNIEnv *env, jobject obj) |
| 130 | { |
| 131 | jclass cls; |
| 132 | jfieldID fid; |
| 133 | tjhandle handle; |
| 134 | |
DRC | 1a3dbe6 | 2011-02-28 10:51:55 +0000 | [diff] [blame] | 135 | if((handle=tjInitCompress())==NULL) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 136 | _throw(tjGetErrorStr()); |
| 137 | |
| 138 | bailif0(cls=(*env)->GetObjectClass(env, obj)); |
| 139 | bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); |
DRC | 46a0392 | 2014-08-15 14:40:05 +0000 | [diff] [blame] | 140 | (*env)->SetLongField(env, obj, fid, (size_t)handle); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 141 | |
| 142 | bailout: |
| 143 | return; |
| 144 | } |
| 145 | |
DRC | 5d87f6d | 2014-08-15 16:40:56 +0000 | [diff] [blame] | 146 | static jint TJCompressor_compress |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 147 | (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint x, jint y, |
| 148 | jint width, jint pitch, jint height, jint pf, jbyteArray dst, |
| 149 | jint jpegSubsamp, jint jpegQual, jint flags) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 150 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 151 | tjhandle handle=0; |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 152 | unsigned long jpegSize=0; |
| 153 | jsize arraySize=0, actualPitch; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 154 | unsigned char *srcBuf=NULL, *jpegBuf=NULL; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 155 | |
| 156 | gethandle(); |
| 157 | |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 158 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 159 | || pitch<0) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 160 | _throw("Invalid argument in compress()"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 161 | if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) |
| 162 | _throw("Mismatch between Java and C API"); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 163 | |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 164 | actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch; |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 165 | arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf]; |
| 166 | if((*env)->GetArrayLength(env, src)*srcElementSize<arraySize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 167 | _throw("Source buffer is not large enough"); |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 168 | jpegSize=tjBufSize(width, height, jpegSubsamp); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 169 | if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 170 | _throw("Destination buffer is not large enough"); |
| 171 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 172 | bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 173 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 174 | |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 175 | if(tjCompress2(handle, &srcBuf[y*actualPitch + x*tjPixelSize[pf]], width, |
| 176 | pitch, height, pf, &jpegBuf, &jpegSize, jpegSubsamp, jpegQual, |
| 177 | flags|TJFLAG_NOREALLOC)==-1) |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 178 | _throw(tjGetErrorStr()); |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 179 | |
| 180 | bailout: |
| 181 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); |
| 182 | if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); |
| 183 | return (jint)jpegSize; |
| 184 | } |
| 185 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 186 | /* TurboJPEG 1.3.x: TJCompressor::compress() byte source */ |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 187 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIIIII_3BIII |
| 188 | (JNIEnv *env, jobject obj, jbyteArray src, jint x, jint y, jint width, |
| 189 | jint pitch, jint height, jint pf, jbyteArray dst, jint jpegSubsamp, |
| 190 | jint jpegQual, jint flags) |
| 191 | { |
| 192 | return TJCompressor_compress(env, obj, src, 1, x, y, width, pitch, height, |
| 193 | pf, dst, jpegSubsamp, jpegQual, flags); |
| 194 | } |
| 195 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 196 | /* TurboJPEG 1.2.x: TJCompressor::compress() byte source */ |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 197 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIII_3BIII |
| 198 | (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch, |
| 199 | jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual, |
| 200 | jint flags) |
| 201 | { |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 202 | return TJCompressor_compress(env, obj, src, 1, 0, 0, width, pitch, height, |
| 203 | pf, dst, jpegSubsamp, jpegQual, flags); |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 204 | } |
| 205 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 206 | /* TurboJPEG 1.3.x: TJCompressor::compress() int source */ |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 207 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIIIII_3BIII |
| 208 | (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width, |
| 209 | jint stride, jint height, jint pf, jbyteArray dst, jint jpegSubsamp, |
| 210 | jint jpegQual, jint flags) |
| 211 | { |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 212 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 213 | _throw("Invalid argument in compress()"); |
DRC | fac3bea | 2012-09-24 02:27:55 +0000 | [diff] [blame] | 214 | if(tjPixelSize[pf]!=sizeof(jint)) |
| 215 | _throw("Pixel format must be 32-bit when compressing from an integer buffer."); |
| 216 | |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 217 | return TJCompressor_compress(env, obj, src, sizeof(jint), x, y, width, |
| 218 | stride*sizeof(jint), height, pf, dst, jpegSubsamp, jpegQual, flags); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 219 | |
| 220 | bailout: |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 221 | return 0; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 222 | } |
| 223 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 224 | /* TurboJPEG 1.2.x: TJCompressor::compress() int source */ |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 225 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIII_3BIII |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 226 | (JNIEnv *env, jobject obj, jintArray src, jint width, jint stride, |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 227 | jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual, |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 228 | jint flags) |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 229 | { |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 230 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) |
| 231 | _throw("Invalid argument in compress()"); |
| 232 | if(tjPixelSize[pf]!=sizeof(jint)) |
| 233 | _throw("Pixel format must be 32-bit when compressing from an integer buffer."); |
| 234 | |
| 235 | return TJCompressor_compress(env, obj, src, sizeof(jint), 0, 0, width, |
| 236 | stride*sizeof(jint), height, pf, dst, jpegSubsamp, jpegQual, flags); |
| 237 | |
| 238 | bailout: |
| 239 | return 0; |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 240 | } |
| 241 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 242 | /* TurboJPEG 1.4.x: TJCompressor::compressFromYUV() */ |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 243 | JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compressFromYUV___3_3B_3II_3III_3BII |
| 244 | (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets, |
| 245 | jint width, jintArray jSrcStrides, jint height, jint subsamp, |
| 246 | jbyteArray dst, jint jpegQual, jint flags) |
DRC | 1e67274 | 2013-10-31 05:04:51 +0000 | [diff] [blame] | 247 | { |
| 248 | tjhandle handle=0; |
| 249 | unsigned long jpegSize=0; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 250 | jbyteArray jSrcPlanes[3]={NULL, NULL, NULL}; |
| 251 | unsigned char *srcPlanes[3], *jpegBuf=NULL; |
| 252 | int *srcOffsets=NULL, *srcStrides=NULL; |
| 253 | int nc=(subsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3), i; |
DRC | 1e67274 | 2013-10-31 05:04:51 +0000 | [diff] [blame] | 254 | |
| 255 | gethandle(); |
| 256 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 257 | if(subsamp<0 || subsamp>=org_libjpegturbo_turbojpeg_TJ_NUMSAMP) |
| 258 | _throw("Invalid argument in compressFromYUV()"); |
| 259 | if(org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP) |
| 260 | _throw("Mismatch between Java and C API"); |
| 261 | |
| 262 | if((*env)->GetArrayLength(env, srcobjs)<nc) |
| 263 | _throw("Planes array is too small for the subsampling type"); |
| 264 | if((*env)->GetArrayLength(env, jSrcOffsets)<nc) |
| 265 | _throw("Offsets array is too small for the subsampling type"); |
| 266 | if((*env)->GetArrayLength(env, jSrcStrides)<nc) |
| 267 | _throw("Strides array is too small for the subsampling type"); |
| 268 | |
DRC | 1e67274 | 2013-10-31 05:04:51 +0000 | [diff] [blame] | 269 | jpegSize=tjBufSize(width, height, subsamp); |
| 270 | if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize) |
| 271 | _throw("Destination buffer is not large enough"); |
| 272 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 273 | bailif0(srcOffsets=(*env)->GetPrimitiveArrayCritical(env, jSrcOffsets, 0)); |
| 274 | bailif0(srcStrides=(*env)->GetPrimitiveArrayCritical(env, jSrcStrides, 0)); |
| 275 | for(i=0; i<nc; i++) |
| 276 | { |
| 277 | int planeSize=tjPlaneSizeYUV(i, width, srcStrides[i], height, subsamp); |
| 278 | int pw=tjPlaneWidth(i, width, subsamp); |
| 279 | |
| 280 | if(planeSize<0 || pw<0) |
| 281 | _throw(tjGetErrorStr()); |
| 282 | |
| 283 | if(srcOffsets[i]<0) |
| 284 | _throw("Invalid argument in compressFromYUV()"); |
| 285 | if(srcStrides[i]<0 && srcOffsets[i]-planeSize+pw<0) |
| 286 | _throw("Negative plane stride would cause memory to be accessed below plane boundary"); |
| 287 | |
| 288 | bailif0(jSrcPlanes[i]=(*env)->GetObjectArrayElement(env, srcobjs, i)); |
| 289 | if((*env)->GetArrayLength(env, jSrcPlanes[i])<srcOffsets[i]+planeSize) |
| 290 | _throw("Source plane is not large enough"); |
| 291 | |
| 292 | bailif0(srcPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jSrcPlanes[i], |
| 293 | 0)); |
| 294 | srcPlanes[i]=&srcPlanes[i][srcOffsets[i]]; |
| 295 | } |
DRC | 1e67274 | 2013-10-31 05:04:51 +0000 | [diff] [blame] | 296 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
| 297 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 298 | if(tjCompressFromYUVPlanes(handle, srcPlanes, width, srcStrides, height, |
| 299 | subsamp, &jpegBuf, &jpegSize, jpegQual, flags|TJFLAG_NOREALLOC)==-1) |
DRC | 1e67274 | 2013-10-31 05:04:51 +0000 | [diff] [blame] | 300 | _throw(tjGetErrorStr()); |
DRC | 1e67274 | 2013-10-31 05:04:51 +0000 | [diff] [blame] | 301 | |
| 302 | bailout: |
| 303 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 304 | for(i=0; i<nc; i++) |
| 305 | { |
| 306 | if(srcPlanes[i] && jSrcPlanes[i]) |
| 307 | (*env)->ReleasePrimitiveArrayCritical(env, jSrcPlanes[i], srcPlanes[i], |
| 308 | 0); |
| 309 | } |
| 310 | if(srcStrides) |
| 311 | (*env)->ReleasePrimitiveArrayCritical(env, jSrcStrides, srcStrides, 0); |
| 312 | if(srcOffsets) |
| 313 | (*env)->ReleasePrimitiveArrayCritical(env, jSrcOffsets, srcOffsets, 0); |
DRC | 1e67274 | 2013-10-31 05:04:51 +0000 | [diff] [blame] | 314 | return (jint)jpegSize; |
| 315 | } |
| 316 | |
DRC | 5d87f6d | 2014-08-15 16:40:56 +0000 | [diff] [blame] | 317 | static void TJCompressor_encodeYUV |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 318 | (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint x, jint y, |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 319 | jint width, jint pitch, jint height, jint pf, jobjectArray dstobjs, |
| 320 | jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 321 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 322 | tjhandle handle=0; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 323 | jsize arraySize=0, actualPitch; |
| 324 | jbyteArray jDstPlanes[3]={NULL, NULL, NULL}; |
| 325 | unsigned char *srcBuf=NULL, *dstPlanes[3]; |
| 326 | int *dstOffsets=NULL, *dstStrides=NULL; |
| 327 | int nc=(subsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3), i; |
| 328 | |
| 329 | gethandle(); |
| 330 | |
| 331 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 |
| 332 | || pitch<0 || subsamp<0 || subsamp>=org_libjpegturbo_turbojpeg_TJ_NUMSAMP) |
| 333 | _throw("Invalid argument in encodeYUV()"); |
| 334 | if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF |
| 335 | || org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP) |
| 336 | _throw("Mismatch between Java and C API"); |
| 337 | |
| 338 | if((*env)->GetArrayLength(env, dstobjs)<nc) |
| 339 | _throw("Planes array is too small for the subsampling type"); |
| 340 | if((*env)->GetArrayLength(env, jDstOffsets)<nc) |
| 341 | _throw("Offsets array is too small for the subsampling type"); |
| 342 | if((*env)->GetArrayLength(env, jDstStrides)<nc) |
| 343 | _throw("Strides array is too small for the subsampling type"); |
| 344 | |
| 345 | actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch; |
| 346 | arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf]; |
| 347 | if((*env)->GetArrayLength(env, src)*srcElementSize<arraySize) |
| 348 | _throw("Source buffer is not large enough"); |
| 349 | |
| 350 | bailif0(dstOffsets=(*env)->GetPrimitiveArrayCritical(env, jDstOffsets, 0)); |
| 351 | bailif0(dstStrides=(*env)->GetPrimitiveArrayCritical(env, jDstStrides, 0)); |
| 352 | for(i=0; i<nc; i++) |
| 353 | { |
| 354 | int planeSize=tjPlaneSizeYUV(i, width, dstStrides[i], height, subsamp); |
| 355 | int pw=tjPlaneWidth(i, width, subsamp); |
| 356 | |
| 357 | if(planeSize<0 || pw<0) |
| 358 | _throw(tjGetErrorStr()); |
| 359 | |
| 360 | if(dstOffsets[i]<0) |
| 361 | _throw("Invalid argument in encodeYUV()"); |
| 362 | if(dstStrides[i]<0 && dstOffsets[i]-planeSize+pw<0) |
| 363 | _throw("Negative plane stride would cause memory to be accessed below plane boundary"); |
| 364 | |
| 365 | bailif0(jDstPlanes[i]=(*env)->GetObjectArrayElement(env, dstobjs, i)); |
| 366 | if((*env)->GetArrayLength(env, jDstPlanes[i])<dstOffsets[i]+planeSize) |
| 367 | _throw("Destination plane is not large enough"); |
| 368 | |
| 369 | bailif0(dstPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jDstPlanes[i], |
| 370 | 0)); |
| 371 | dstPlanes[i]=&dstPlanes[i][dstOffsets[i]]; |
| 372 | } |
| 373 | bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 374 | |
| 375 | if(tjEncodeYUVPlanes(handle, &srcBuf[y*actualPitch + x*tjPixelSize[pf]], |
| 376 | width, pitch, height, pf, dstPlanes, dstStrides, subsamp, flags)==-1) |
| 377 | _throw(tjGetErrorStr()); |
| 378 | |
| 379 | bailout: |
| 380 | if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); |
| 381 | for(i=0; i<nc; i++) |
| 382 | { |
| 383 | if(dstPlanes[i] && jDstPlanes[i]) |
| 384 | (*env)->ReleasePrimitiveArrayCritical(env, jDstPlanes[i], dstPlanes[i], |
| 385 | 0); |
| 386 | } |
| 387 | if(dstStrides) |
| 388 | (*env)->ReleasePrimitiveArrayCritical(env, jDstStrides, dstStrides, 0); |
| 389 | if(dstOffsets) |
| 390 | (*env)->ReleasePrimitiveArrayCritical(env, jDstOffsets, dstOffsets, 0); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | /* TurboJPEG 1.4.x: TJCompressor::encodeYUV() byte source */ |
| 395 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIIIII_3_3B_3I_3III |
| 396 | (JNIEnv *env, jobject obj, jbyteArray src, jint x, jint y, jint width, |
| 397 | jint pitch, jint height, jint pf, jobjectArray dstobjs, |
| 398 | jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags) |
| 399 | { |
| 400 | TJCompressor_encodeYUV(env, obj, src, 1, x, y, width, pitch, height, pf, |
| 401 | dstobjs, jDstOffsets, jDstStrides, subsamp, flags); |
| 402 | } |
| 403 | |
| 404 | /* TurboJPEG 1.4.x: TJCompressor::encodeYUV() int source */ |
| 405 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIIIII_3_3B_3I_3III |
| 406 | (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width, |
| 407 | jint stride, jint height, jint pf, jobjectArray dstobjs, |
| 408 | jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags) |
| 409 | { |
| 410 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) |
| 411 | _throw("Invalid argument in encodeYUV()"); |
| 412 | if(tjPixelSize[pf]!=sizeof(jint)) |
| 413 | _throw("Pixel format must be 32-bit when encoding from an integer buffer."); |
| 414 | |
| 415 | TJCompressor_encodeYUV(env, obj, src, sizeof(jint), x, y, width, |
| 416 | stride*sizeof(jint), height, pf, dstobjs, jDstOffsets, jDstStrides, |
| 417 | subsamp, flags); |
| 418 | |
| 419 | bailout: |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | JNIEXPORT void JNICALL TJCompressor_encodeYUV_12 |
| 424 | (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint width, |
| 425 | jint pitch, jint height, jint pf, jbyteArray dst, jint subsamp, jint flags) |
| 426 | { |
| 427 | tjhandle handle=0; |
| 428 | jsize arraySize=0; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 429 | unsigned char *srcBuf=NULL, *dstBuf=NULL; |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 430 | |
| 431 | gethandle(); |
| 432 | |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 433 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 434 | || pitch<0) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 435 | _throw("Invalid argument in encodeYUV()"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 436 | if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) |
| 437 | _throw("Mismatch between Java and C API"); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 438 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 439 | arraySize=(pitch==0)? width*tjPixelSize[pf]*height:pitch*height; |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 440 | if((*env)->GetArrayLength(env, src)*srcElementSize<arraySize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 441 | _throw("Source buffer is not large enough"); |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 442 | if((*env)->GetArrayLength(env, dst) |
| 443 | <(jsize)tjBufSizeYUV(width, height, subsamp)) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 444 | _throw("Destination buffer is not large enough"); |
| 445 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 446 | bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 447 | bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 448 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 449 | if(tjEncodeYUV2(handle, srcBuf, width, pitch, height, pf, dstBuf, subsamp, |
| 450 | flags)==-1) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 451 | _throw(tjGetErrorStr()); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 452 | |
| 453 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 454 | if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 455 | if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 456 | return; |
| 457 | } |
| 458 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 459 | /* TurboJPEG 1.2.x: TJCompressor::encodeYUV() byte source */ |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 460 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIII_3BII |
| 461 | (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch, |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 462 | jint height, jint pf, jbyteArray dst, jint subsamp, jint flags) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 463 | { |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 464 | TJCompressor_encodeYUV_12(env, obj, src, 1, width, pitch, height, pf, dst, |
| 465 | subsamp, flags); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 466 | } |
| 467 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 468 | /* TurboJPEG 1.2.x: TJCompressor::encodeYUV() int source */ |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 469 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIII_3BII |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 470 | (JNIEnv *env, jobject obj, jintArray src, jint width, jint stride, |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 471 | jint height, jint pf, jbyteArray dst, jint subsamp, jint flags) |
| 472 | { |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 473 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) |
| 474 | _throw("Invalid argument in encodeYUV()"); |
| 475 | if(tjPixelSize[pf]!=sizeof(jint)) |
| 476 | _throw("Pixel format must be 32-bit when encoding from an integer buffer."); |
| 477 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 478 | TJCompressor_encodeYUV_12(env, obj, src, sizeof(jint), width, |
| 479 | stride*sizeof(jint), height, pf, dst, subsamp, flags); |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 480 | |
| 481 | bailout: |
| 482 | return; |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 483 | } |
| 484 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 485 | /* TurboJPEG 1.2.x: TJCompressor::destroy() */ |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 486 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 487 | (JNIEnv *env, jobject obj) |
| 488 | { |
| 489 | tjhandle handle=0; |
| 490 | |
| 491 | gethandle(); |
| 492 | |
| 493 | if(tjDestroy(handle)==-1) _throw(tjGetErrorStr()); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 494 | (*env)->SetLongField(env, obj, _fid, 0); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 495 | |
| 496 | bailout: |
| 497 | return; |
| 498 | } |
| 499 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 500 | /* TurboJPEG 1.2.x: TJDecompressor::init() */ |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 501 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_init |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 502 | (JNIEnv *env, jobject obj) |
| 503 | { |
| 504 | jclass cls; |
| 505 | jfieldID fid; |
| 506 | tjhandle handle; |
| 507 | |
DRC | 1a3dbe6 | 2011-02-28 10:51:55 +0000 | [diff] [blame] | 508 | if((handle=tjInitDecompress())==NULL) _throw(tjGetErrorStr()); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 509 | |
| 510 | bailif0(cls=(*env)->GetObjectClass(env, obj)); |
| 511 | bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); |
DRC | 46a0392 | 2014-08-15 14:40:05 +0000 | [diff] [blame] | 512 | (*env)->SetLongField(env, obj, fid, (size_t)handle); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 513 | |
| 514 | bailout: |
| 515 | return; |
| 516 | } |
| 517 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 518 | /* TurboJPEG 1.2.x: TJDecompressor::getScalingFactors() */ |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 519 | JNIEXPORT jobjectArray JNICALL Java_org_libjpegturbo_turbojpeg_TJ_getScalingFactors |
| 520 | (JNIEnv *env, jclass cls) |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 521 | { |
DRC | b7c8c86 | 2014-08-15 16:20:59 +0000 | [diff] [blame] | 522 | jclass sfcls=NULL; jfieldID fid=0; |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 523 | tjscalingfactor *sf=NULL; int n=0, i; |
| 524 | jobject sfobj=NULL; |
| 525 | jobjectArray sfjava=NULL; |
| 526 | |
| 527 | if((sf=tjGetScalingFactors(&n))==NULL || n==0) |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 528 | _throw(tjGetErrorStr()); |
| 529 | |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 530 | bailif0(sfcls=(*env)->FindClass(env, "org/libjpegturbo/turbojpeg/TJScalingFactor")); |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 531 | bailif0(sfjava=(jobjectArray)(*env)->NewObjectArray(env, n, sfcls, 0)); |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 532 | |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 533 | for(i=0; i<n; i++) |
| 534 | { |
| 535 | bailif0(sfobj=(*env)->AllocObject(env, sfcls)); |
| 536 | bailif0(fid=(*env)->GetFieldID(env, sfcls, "num", "I")); |
| 537 | (*env)->SetIntField(env, sfobj, fid, sf[i].num); |
| 538 | bailif0(fid=(*env)->GetFieldID(env, sfcls, "denom", "I")); |
| 539 | (*env)->SetIntField(env, sfobj, fid, sf[i].denom); |
| 540 | (*env)->SetObjectArrayElement(env, sfjava, i, sfobj); |
| 541 | } |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 542 | |
| 543 | bailout: |
DRC | 109a578 | 2011-03-01 09:53:07 +0000 | [diff] [blame] | 544 | return sfjava; |
DRC | 36336fc | 2011-02-22 10:27:31 +0000 | [diff] [blame] | 545 | } |
| 546 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 547 | /* TurboJPEG 1.2.x: TJDecompressor::decompressHeader() */ |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 548 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressHeader |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 549 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 550 | { |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 551 | tjhandle handle=0; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 552 | unsigned char *jpegBuf=NULL; |
DRC | 38cb1ec | 2013-08-23 04:45:43 +0000 | [diff] [blame] | 553 | int width=0, height=0, jpegSubsamp=-1, jpegColorspace=-1; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 554 | |
| 555 | gethandle(); |
| 556 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 557 | if((*env)->GetArrayLength(env, src)<jpegSize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 558 | _throw("Source buffer is not large enough"); |
| 559 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 560 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 561 | |
DRC | 1a45b81 | 2014-05-09 18:06:58 +0000 | [diff] [blame] | 562 | if(tjDecompressHeader3(handle, jpegBuf, (unsigned long)jpegSize, |
DRC | 38cb1ec | 2013-08-23 04:45:43 +0000 | [diff] [blame] | 563 | &width, &height, &jpegSubsamp, &jpegColorspace)==-1) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 564 | _throw(tjGetErrorStr()); |
DRC | 8951cf0 | 2014-08-14 16:54:04 +0000 | [diff] [blame] | 565 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 566 | (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); jpegBuf=NULL; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 567 | |
DRC | 26dd86b | 2014-08-15 14:01:21 +0000 | [diff] [blame] | 568 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 569 | (*env)->SetIntField(env, obj, _fid, jpegSubsamp); |
DRC | 26dd86b | 2014-08-15 14:01:21 +0000 | [diff] [blame] | 570 | if((_fid=(*env)->GetFieldID(env, _cls, "jpegColorspace", "I"))==0) |
| 571 | (*env)->ExceptionClear(env); |
| 572 | else |
| 573 | (*env)->SetIntField(env, obj, _fid, jpegColorspace); |
| 574 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 575 | (*env)->SetIntField(env, obj, _fid, width); |
DRC | 26dd86b | 2014-08-15 14:01:21 +0000 | [diff] [blame] | 576 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 577 | (*env)->SetIntField(env, obj, _fid, height); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 578 | |
| 579 | bailout: |
DRC | 8951cf0 | 2014-08-14 16:54:04 +0000 | [diff] [blame] | 580 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 581 | return; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 582 | } |
| 583 | |
DRC | 5d87f6d | 2014-08-15 16:40:56 +0000 | [diff] [blame] | 584 | static void TJDecompressor_decompress |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 585 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jarray dst, |
| 586 | jint dstElementSize, jint x, jint y, jint width, jint pitch, jint height, |
| 587 | jint pf, jint flags) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 588 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 589 | tjhandle handle=0; |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 590 | jsize arraySize=0, actualPitch; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 591 | unsigned char *jpegBuf=NULL, *dstBuf=NULL; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 592 | |
| 593 | gethandle(); |
| 594 | |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 595 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 596 | _throw("Invalid argument in decompress()"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 597 | if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) |
| 598 | _throw("Mismatch between Java and C API"); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 599 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 600 | if((*env)->GetArrayLength(env, src)<jpegSize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 601 | _throw("Source buffer is not large enough"); |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 602 | actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch; |
DRC | dc31f0b | 2012-06-07 09:38:57 +0000 | [diff] [blame] | 603 | arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf]; |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 604 | if((*env)->GetArrayLength(env, dst)*dstElementSize<arraySize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 605 | _throw("Destination buffer is not large enough"); |
| 606 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 607 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 608 | bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 609 | |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 610 | if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize, |
| 611 | &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf, |
| 612 | flags)==-1) |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 613 | _throw(tjGetErrorStr()); |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 614 | |
| 615 | bailout: |
| 616 | if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 617 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
| 618 | return; |
| 619 | } |
| 620 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 621 | /* TurboJPEG 1.3.x: TJDecompressor::decompress() byte destination */ |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 622 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIIIII |
| 623 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst, |
| 624 | jint x, jint y, jint width, jint pitch, jint height, jint pf, jint flags) |
| 625 | { |
| 626 | TJDecompressor_decompress(env, obj, src, jpegSize, dst, 1, x, y, width, |
| 627 | pitch, height, pf, flags); |
| 628 | } |
| 629 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 630 | /* TurboJPEG 1.2.x: TJDecompressor::decompress() byte destination */ |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 631 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIII |
| 632 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst, |
| 633 | jint width, jint pitch, jint height, jint pf, jint flags) |
| 634 | { |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 635 | TJDecompressor_decompress(env, obj, src, jpegSize, dst, 1, 0, 0, width, |
| 636 | pitch, height, pf, flags); |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 637 | } |
| 638 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 639 | /* TurboJPEG 1.3.x: TJDecompressor::decompress() int destination */ |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 640 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIIIII |
| 641 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst, |
| 642 | jint x, jint y, jint width, jint stride, jint height, jint pf, jint flags) |
| 643 | { |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 644 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) |
| 645 | _throw("Invalid argument in decompress()"); |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 646 | if(tjPixelSize[pf]!=sizeof(jint)) |
| 647 | _throw("Pixel format must be 32-bit when decompressing to an integer buffer."); |
| 648 | |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 649 | TJDecompressor_decompress(env, obj, src, jpegSize, dst, sizeof(jint), x, y, |
| 650 | width, stride*sizeof(jint), height, pf, flags); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 651 | |
| 652 | bailout: |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 653 | return; |
| 654 | } |
| 655 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 656 | /* TurboJPEG 1.2.x: TJDecompressor::decompress() int destination */ |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 657 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIII |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 658 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst, |
DRC | f659f43 | 2012-06-06 08:41:06 +0000 | [diff] [blame] | 659 | jint width, jint stride, jint height, jint pf, jint flags) |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 660 | { |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 661 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) |
| 662 | _throw("Invalid argument in decompress()"); |
| 663 | if(tjPixelSize[pf]!=sizeof(jint)) |
| 664 | _throw("Pixel format must be 32-bit when decompressing to an integer buffer."); |
| 665 | |
| 666 | TJDecompressor_decompress(env, obj, src, jpegSize, dst, sizeof(jint), 0, 0, |
| 667 | width, stride*sizeof(jint), height, pf, flags); |
| 668 | |
| 669 | bailout: |
| 670 | return; |
DRC | 1a45b81 | 2014-05-09 18:06:58 +0000 | [diff] [blame] | 671 | |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 672 | } |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 673 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 674 | /* TurboJPEG 1.4.x: TJDecompressor::decompressToYUV() */ |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 675 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV___3BI_3_3B_3II_3III |
| 676 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, |
| 677 | jobjectArray dstobjs, jintArray jDstOffsets, jint desiredWidth, |
| 678 | jintArray jDstStrides, jint desiredHeight, jint flags) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 679 | { |
| 680 | tjhandle handle=0; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 681 | jbyteArray jDstPlanes[3]={NULL, NULL, NULL}; |
| 682 | unsigned char *jpegBuf=NULL, *dstPlanes[3]; |
| 683 | int *dstOffsets=NULL, *dstStrides=NULL; |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 684 | int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 685 | int nc=0, i, width, height, scaledWidth, scaledHeight, nsf=0; |
| 686 | tjscalingfactor *sf; |
| 687 | |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 688 | |
| 689 | gethandle(); |
| 690 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 691 | if((*env)->GetArrayLength(env, src)<jpegSize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 692 | _throw("Source buffer is not large enough"); |
DRC | 26dd86b | 2014-08-15 14:01:21 +0000 | [diff] [blame] | 693 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 694 | jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); |
DRC | 26dd86b | 2014-08-15 14:01:21 +0000 | [diff] [blame] | 695 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 696 | jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); |
DRC | 26dd86b | 2014-08-15 14:01:21 +0000 | [diff] [blame] | 697 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 698 | jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 699 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 700 | nc=(jpegSubsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 701 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 702 | width=desiredWidth; height=desiredHeight; |
| 703 | if(width==0) width=jpegWidth; |
| 704 | if(height==0) height=jpegHeight; |
| 705 | sf=tjGetScalingFactors(&nsf); |
| 706 | if(!sf || nsf<1) |
| 707 | _throw(tjGetErrorStr()); |
| 708 | for(i=0; i<nsf; i++) |
| 709 | { |
| 710 | scaledWidth=TJSCALED(jpegWidth, sf[i]); |
| 711 | scaledHeight=TJSCALED(jpegHeight, sf[i]); |
| 712 | if(scaledWidth<=width && scaledHeight<=height) |
| 713 | break; |
| 714 | } |
| 715 | |
| 716 | bailif0(dstOffsets=(*env)->GetPrimitiveArrayCritical(env, jDstOffsets, 0)); |
| 717 | bailif0(dstStrides=(*env)->GetPrimitiveArrayCritical(env, jDstStrides, 0)); |
| 718 | for(i=0; i<nc; i++) |
| 719 | { |
| 720 | int planeSize=tjPlaneSizeYUV(i, scaledWidth, dstStrides[i], scaledHeight, |
| 721 | jpegSubsamp); |
| 722 | int pw=tjPlaneWidth(i, scaledWidth, jpegSubsamp); |
| 723 | |
| 724 | if(planeSize<0 || pw<0) |
| 725 | _throw(tjGetErrorStr()); |
| 726 | |
| 727 | if(dstOffsets[i]<0) |
| 728 | _throw("Invalid argument in decompressToYUV()"); |
| 729 | if(dstStrides[i]<0 && dstOffsets[i]-planeSize+pw<0) |
| 730 | _throw("Negative plane stride would cause memory to be accessed below plane boundary"); |
| 731 | |
| 732 | bailif0(jDstPlanes[i]=(*env)->GetObjectArrayElement(env, dstobjs, i)); |
| 733 | if((*env)->GetArrayLength(env, jDstPlanes[i])<dstOffsets[i]+planeSize) |
| 734 | _throw("Destination plane is not large enough"); |
| 735 | |
| 736 | bailif0(dstPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jDstPlanes[i], |
| 737 | 0)); |
| 738 | dstPlanes[i]=&dstPlanes[i][dstOffsets[i]]; |
| 739 | } |
| 740 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 741 | |
| 742 | if(tjDecompressToYUVPlanes(handle, jpegBuf, (unsigned long)jpegSize, |
| 743 | dstPlanes, desiredWidth, dstStrides, desiredHeight, flags)==-1) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 744 | _throw(tjGetErrorStr()); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 745 | |
| 746 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 747 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 748 | for(i=0; i<nc; i++) |
| 749 | { |
| 750 | if(dstPlanes[i] && jDstPlanes[i]) |
| 751 | (*env)->ReleasePrimitiveArrayCritical(env, jDstPlanes[i], dstPlanes[i], |
| 752 | 0); |
| 753 | } |
| 754 | if(dstStrides) |
| 755 | (*env)->ReleasePrimitiveArrayCritical(env, jDstStrides, dstStrides, 0); |
| 756 | if(dstOffsets) |
| 757 | (*env)->ReleasePrimitiveArrayCritical(env, jDstOffsets, dstOffsets, 0); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 758 | return; |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 759 | } |
| 760 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 761 | /* TurboJPEG 1.2.x: TJDecompressor::decompressToYUV() */ |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 762 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV___3BI_3BI |
| 763 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst, |
| 764 | jint flags) |
| 765 | { |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 766 | tjhandle handle=0; |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 767 | unsigned char *jpegBuf=NULL, *dstBuf=NULL; |
| 768 | int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0; |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 769 | |
| 770 | gethandle(); |
| 771 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 772 | if((*env)->GetArrayLength(env, src)<jpegSize) |
| 773 | _throw("Source buffer is not large enough"); |
| 774 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); |
| 775 | jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); |
| 776 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); |
| 777 | jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); |
| 778 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); |
| 779 | jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); |
| 780 | if((*env)->GetArrayLength(env, dst) |
| 781 | <(jsize)tjBufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp)) |
| 782 | _throw("Destination buffer is not large enough"); |
| 783 | |
| 784 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 785 | bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
| 786 | |
| 787 | if(tjDecompressToYUV(handle, jpegBuf, (unsigned long)jpegSize, dstBuf, |
| 788 | flags)==-1) |
| 789 | _throw(tjGetErrorStr()); |
| 790 | |
| 791 | bailout: |
| 792 | if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 793 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | static void TJDecompressor_decodeYUV |
| 798 | (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets, |
| 799 | jintArray jSrcStrides, jint subsamp, jarray dst, jint dstElementSize, |
| 800 | jint x, jint y, jint width, jint pitch, jint height, jint pf, jint flags) |
| 801 | { |
| 802 | tjhandle handle=0; |
| 803 | jsize arraySize=0, actualPitch; |
| 804 | jbyteArray jSrcPlanes[3]={NULL, NULL, NULL}; |
| 805 | unsigned char *srcPlanes[3], *dstBuf=NULL; |
| 806 | int *srcOffsets=NULL, *srcStrides=NULL; |
| 807 | int nc=(subsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3), i; |
| 808 | |
| 809 | gethandle(); |
| 810 | |
| 811 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || subsamp<0 |
| 812 | || subsamp>=org_libjpegturbo_turbojpeg_TJ_NUMSAMP) |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 813 | _throw("Invalid argument in decodeYUV()"); |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 814 | if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF |
| 815 | || org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP) |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 816 | _throw("Mismatch between Java and C API"); |
| 817 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 818 | if((*env)->GetArrayLength(env, srcobjs)<nc) |
| 819 | _throw("Planes array is too small for the subsampling type"); |
| 820 | if((*env)->GetArrayLength(env, jSrcOffsets)<nc) |
| 821 | _throw("Offsets array is too small for the subsampling type"); |
| 822 | if((*env)->GetArrayLength(env, jSrcStrides)<nc) |
| 823 | _throw("Strides array is too small for the subsampling type"); |
| 824 | |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 825 | actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch; |
| 826 | arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf]; |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 827 | if((*env)->GetArrayLength(env, dst)*dstElementSize<arraySize) |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 828 | _throw("Destination buffer is not large enough"); |
| 829 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 830 | bailif0(srcOffsets=(*env)->GetPrimitiveArrayCritical(env, jSrcOffsets, 0)); |
| 831 | bailif0(srcStrides=(*env)->GetPrimitiveArrayCritical(env, jSrcStrides, 0)); |
| 832 | for(i=0; i<nc; i++) |
| 833 | { |
| 834 | int planeSize=tjPlaneSizeYUV(i, width, srcStrides[i], height, subsamp); |
| 835 | int pw=tjPlaneWidth(i, width, subsamp); |
| 836 | |
| 837 | if(planeSize<0 || pw<0) |
| 838 | _throw(tjGetErrorStr()); |
| 839 | |
| 840 | if(srcOffsets[i]<0) |
| 841 | _throw("Invalid argument in decodeYUV()"); |
| 842 | if(srcStrides[i]<0 && srcOffsets[i]-planeSize+pw<0) |
| 843 | _throw("Negative plane stride would cause memory to be accessed below plane boundary"); |
| 844 | |
| 845 | bailif0(jSrcPlanes[i]=(*env)->GetObjectArrayElement(env, srcobjs, i)); |
| 846 | if((*env)->GetArrayLength(env, jSrcPlanes[i])<srcOffsets[i]+planeSize) |
| 847 | _throw("Source plane is not large enough"); |
| 848 | |
| 849 | bailif0(srcPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jSrcPlanes[i], |
| 850 | 0)); |
| 851 | srcPlanes[i]=&srcPlanes[i][srcOffsets[i]]; |
| 852 | } |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 853 | bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
| 854 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 855 | if(tjDecodeYUVPlanes(handle, srcPlanes, srcStrides, subsamp, |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 856 | &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf, |
| 857 | flags)==-1) |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 858 | _throw(tjGetErrorStr()); |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 859 | |
| 860 | bailout: |
| 861 | if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 862 | for(i=0; i<nc; i++) |
| 863 | { |
| 864 | if(srcPlanes[i] && jSrcPlanes[i]) |
| 865 | (*env)->ReleasePrimitiveArrayCritical(env, jSrcPlanes[i], srcPlanes[i], |
| 866 | 0); |
| 867 | } |
| 868 | if(srcStrides) |
| 869 | (*env)->ReleasePrimitiveArrayCritical(env, jSrcStrides, srcStrides, 0); |
| 870 | if(srcOffsets) |
| 871 | (*env)->ReleasePrimitiveArrayCritical(env, jSrcOffsets, srcOffsets, 0); |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 872 | return; |
| 873 | } |
| 874 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 875 | /* TurboJPEG 1.4.x: TJDecompressor::decodeYUV() byte destination */ |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 876 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decodeYUV___3_3B_3I_3II_3BIIIIIII |
| 877 | (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets, |
| 878 | jintArray jSrcStrides, jint subsamp, jbyteArray dst, jint x, jint y, |
| 879 | jint width, jint pitch, jint height, jint pf, jint flags) |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 880 | { |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 881 | TJDecompressor_decodeYUV(env, obj, srcobjs, jSrcOffsets, jSrcStrides, |
| 882 | subsamp, dst, 1, x, y, width, pitch, height, pf, flags); |
DRC | 927a10d | 2014-08-15 13:18:58 +0000 | [diff] [blame] | 883 | } |
| 884 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 885 | /* TurboJPEG 1.4.x: TJDecompressor::decodeYUV() int destination */ |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 886 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decodeYUV___3_3B_3I_3II_3IIIIIIII |
| 887 | (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets, |
| 888 | jintArray jSrcStrides, jint subsamp, jintArray dst, jint x, jint y, |
| 889 | jint width, jint stride, jint height, jint pf, jint flags) |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 890 | { |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 891 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) |
| 892 | _throw("Invalid argument in decodeYUV()"); |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 893 | if(tjPixelSize[pf]!=sizeof(jint)) |
| 894 | _throw("Pixel format must be 32-bit when decoding to an integer buffer."); |
| 895 | |
DRC | 40dd314 | 2014-08-17 12:23:49 +0000 | [diff] [blame^] | 896 | TJDecompressor_decodeYUV(env, obj, srcobjs, jSrcOffsets, jSrcStrides, |
| 897 | subsamp, dst, sizeof(jint), x, y, width, stride*sizeof(jint), height, pf, |
| 898 | flags); |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 899 | |
| 900 | bailout: |
DRC | fc26b65 | 2014-03-16 22:56:26 +0000 | [diff] [blame] | 901 | return; |
| 902 | } |
| 903 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 904 | /* TurboJPEG 1.2.x: TJTransformer::init() */ |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 905 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_init |
| 906 | (JNIEnv *env, jobject obj) |
| 907 | { |
| 908 | jclass cls; |
| 909 | jfieldID fid; |
| 910 | tjhandle handle; |
| 911 | |
| 912 | if((handle=tjInitTransform())==NULL) _throw(tjGetErrorStr()); |
| 913 | |
| 914 | bailif0(cls=(*env)->GetObjectClass(env, obj)); |
| 915 | bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); |
DRC | 46a0392 | 2014-08-15 14:40:05 +0000 | [diff] [blame] | 916 | (*env)->SetLongField(env, obj, fid, (size_t)handle); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 917 | |
| 918 | bailout: |
| 919 | return; |
| 920 | } |
| 921 | |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 922 | typedef struct _JNICustomFilterParams |
| 923 | { |
| 924 | JNIEnv *env; |
| 925 | jobject tobj; |
| 926 | jobject cfobj; |
| 927 | } JNICustomFilterParams; |
| 928 | |
| 929 | static int JNICustomFilter(short *coeffs, tjregion arrayRegion, |
| 930 | tjregion planeRegion, int componentIndex, int transformIndex, |
| 931 | tjtransform *transform) |
| 932 | { |
| 933 | JNICustomFilterParams *params=(JNICustomFilterParams *)transform->data; |
| 934 | JNIEnv *env=params->env; |
| 935 | jobject tobj=params->tobj, cfobj=params->cfobj; |
DRC | b7c8c86 | 2014-08-15 16:20:59 +0000 | [diff] [blame] | 936 | jobject arrayRegionObj, planeRegionObj, bufobj, borobj; |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 937 | jclass cls; jmethodID mid; jfieldID fid; |
| 938 | |
| 939 | bailif0(bufobj=(*env)->NewDirectByteBuffer(env, coeffs, |
| 940 | sizeof(short)*arrayRegion.w*arrayRegion.h)); |
| 941 | bailif0(cls=(*env)->FindClass(env, "java/nio/ByteOrder")); |
DRC | b7c8c86 | 2014-08-15 16:20:59 +0000 | [diff] [blame] | 942 | bailif0(mid=(*env)->GetStaticMethodID(env, cls, "nativeOrder", |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 943 | "()Ljava/nio/ByteOrder;")); |
| 944 | bailif0(borobj=(*env)->CallStaticObjectMethod(env, cls, mid)); |
| 945 | bailif0(cls=(*env)->GetObjectClass(env, bufobj)); |
| 946 | bailif0(mid=(*env)->GetMethodID(env, cls, "order", |
| 947 | "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;")); |
| 948 | (*env)->CallObjectMethod(env, bufobj, mid, borobj); |
DRC | b7c8c86 | 2014-08-15 16:20:59 +0000 | [diff] [blame] | 949 | bailif0(mid=(*env)->GetMethodID(env, cls, "asShortBuffer", |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 950 | "()Ljava/nio/ShortBuffer;")); |
| 951 | bailif0(bufobj=(*env)->CallObjectMethod(env, bufobj, mid)); |
| 952 | |
| 953 | bailif0(cls=(*env)->FindClass(env, "java/awt/Rectangle")); |
| 954 | bailif0(arrayRegionObj=(*env)->AllocObject(env, cls)); |
| 955 | bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I")); |
| 956 | (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.x); |
| 957 | bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I")); |
| 958 | (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.y); |
| 959 | bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I")); |
| 960 | (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.w); |
| 961 | bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I")); |
| 962 | (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.h); |
| 963 | |
| 964 | bailif0(planeRegionObj=(*env)->AllocObject(env, cls)); |
| 965 | bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I")); |
| 966 | (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.x); |
| 967 | bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I")); |
| 968 | (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.y); |
| 969 | bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I")); |
| 970 | (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.w); |
| 971 | bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I")); |
| 972 | (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.h); |
| 973 | |
| 974 | bailif0(cls=(*env)->GetObjectClass(env, cfobj)); |
| 975 | bailif0(mid=(*env)->GetMethodID(env, cls, "customFilter", |
| 976 | "(Ljava/nio/ShortBuffer;Ljava/awt/Rectangle;Ljava/awt/Rectangle;IILorg/libjpegturbo/turbojpeg/TJTransform;)V")); |
| 977 | (*env)->CallVoidMethod(env, cfobj, mid, bufobj, arrayRegionObj, |
| 978 | planeRegionObj, componentIndex, transformIndex, tobj); |
| 979 | |
| 980 | return 0; |
| 981 | |
| 982 | bailout: |
| 983 | return -1; |
| 984 | } |
| 985 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 986 | /* TurboJPEG 1.2.x: TJTransformer::transform() */ |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 987 | JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transform |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 988 | (JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize, |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 989 | jobjectArray dstobjs, jobjectArray tobjs, jint flags) |
| 990 | { |
| 991 | tjhandle handle=0; int i; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 992 | unsigned char *jpegBuf=NULL, **dstBufs=NULL; jsize n=0; |
| 993 | unsigned long *dstSizes=NULL; tjtransform *t=NULL; |
| 994 | jbyteArray *jdstBufs=NULL; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 995 | int jpegWidth=0, jpegHeight=0, jpegSubsamp; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 996 | jintArray jdstSizes=0; jint *dstSizesi=NULL; |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 997 | JNICustomFilterParams *params=NULL; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 998 | |
| 999 | gethandle(); |
| 1000 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1001 | if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1002 | _throw("Source buffer is not large enough"); |
DRC | 26dd86b | 2014-08-15 14:01:21 +0000 | [diff] [blame] | 1003 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1004 | jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); |
DRC | 26dd86b | 2014-08-15 14:01:21 +0000 | [diff] [blame] | 1005 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1006 | jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); |
DRC | 26dd86b | 2014-08-15 14:01:21 +0000 | [diff] [blame] | 1007 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 1008 | jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1009 | |
| 1010 | n=(*env)->GetArrayLength(env, dstobjs); |
| 1011 | if(n!=(*env)->GetArrayLength(env, tobjs)) |
| 1012 | _throw("Mismatch between size of transforms array and destination buffers array"); |
| 1013 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1014 | if((dstBufs=(unsigned char **)malloc(sizeof(unsigned char *)*n))==NULL) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1015 | _throw("Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1016 | if((jdstBufs=(jbyteArray *)malloc(sizeof(jbyteArray)*n))==NULL) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1017 | _throw("Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1018 | if((dstSizes=(unsigned long *)malloc(sizeof(unsigned long)*n))==NULL) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1019 | _throw("Memory allocation failure"); |
| 1020 | if((t=(tjtransform *)malloc(sizeof(tjtransform)*n))==NULL) |
| 1021 | _throw("Memory allocation failure"); |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 1022 | if((params=(JNICustomFilterParams *)malloc(sizeof(JNICustomFilterParams)*n)) |
| 1023 | ==NULL) |
| 1024 | _throw("Memory allocation failure"); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1025 | for(i=0; i<n; i++) |
| 1026 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1027 | dstBufs[i]=NULL; jdstBufs[i]=NULL; dstSizes[i]=0; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1028 | memset(&t[i], 0, sizeof(tjtransform)); |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 1029 | memset(¶ms[i], 0, sizeof(JNICustomFilterParams)); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | for(i=0; i<n; i++) |
| 1033 | { |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 1034 | jobject tobj, cfobj; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1035 | |
| 1036 | bailif0(tobj=(*env)->GetObjectArrayElement(env, tobjs, i)); |
| 1037 | bailif0(_cls=(*env)->GetObjectClass(env, tobj)); |
| 1038 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "op", "I")); |
| 1039 | t[i].op=(*env)->GetIntField(env, tobj, _fid); |
| 1040 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "options", "I")); |
| 1041 | t[i].options=(*env)->GetIntField(env, tobj, _fid); |
| 1042 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "x", "I")); |
| 1043 | t[i].r.x=(*env)->GetIntField(env, tobj, _fid); |
| 1044 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "y", "I")); |
| 1045 | t[i].r.y=(*env)->GetIntField(env, tobj, _fid); |
| 1046 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "width", "I")); |
| 1047 | t[i].r.w=(*env)->GetIntField(env, tobj, _fid); |
| 1048 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "height", "I")); |
| 1049 | t[i].r.h=(*env)->GetIntField(env, tobj, _fid); |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 1050 | |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 1051 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "cf", |
| 1052 | "Lorg/libjpegturbo/turbojpeg/TJCustomFilter;")); |
DRC | 06420c4 | 2011-09-26 18:46:09 +0000 | [diff] [blame] | 1053 | cfobj=(*env)->GetObjectField(env, tobj, _fid); |
| 1054 | if(cfobj) |
| 1055 | { |
| 1056 | params[i].env=env; |
| 1057 | params[i].tobj=tobj; |
| 1058 | params[i].cfobj=cfobj; |
| 1059 | t[i].customFilter=JNICustomFilter; |
| 1060 | t[i].data=(void *)¶ms[i]; |
| 1061 | } |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1064 | for(i=0; i<n; i++) |
| 1065 | { |
| 1066 | int w=jpegWidth, h=jpegHeight; |
| 1067 | if(t[i].r.w!=0) w=t[i].r.w; |
| 1068 | if(t[i].r.h!=0) h=t[i].r.h; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1069 | bailif0(jdstBufs[i]=(*env)->GetObjectArrayElement(env, dstobjs, i)); |
DRC | efe28ce | 2012-01-17 11:48:38 +0000 | [diff] [blame] | 1070 | if((unsigned long)(*env)->GetArrayLength(env, jdstBufs[i]) |
| 1071 | <tjBufSize(w, h, jpegSubsamp)) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1072 | _throw("Destination buffer is not large enough"); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1073 | } |
DRC | 8951cf0 | 2014-08-14 16:54:04 +0000 | [diff] [blame] | 1074 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0)); |
| 1075 | for(i=0; i<n; i++) |
| 1076 | bailif0(dstBufs[i]=(*env)->GetPrimitiveArrayCritical(env, jdstBufs[i], 0)); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1077 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1078 | if(tjTransform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t, |
DRC | 4db92ad | 2011-05-25 04:52:25 +0000 | [diff] [blame] | 1079 | flags|TJFLAG_NOREALLOC)==-1) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1080 | _throw(tjGetErrorStr()); |
DRC | 8951cf0 | 2014-08-14 16:54:04 +0000 | [diff] [blame] | 1081 | |
| 1082 | for(i=0; i<n; i++) |
| 1083 | { |
| 1084 | (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0); |
| 1085 | dstBufs[i]=NULL; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1086 | } |
DRC | 8951cf0 | 2014-08-14 16:54:04 +0000 | [diff] [blame] | 1087 | (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0); |
| 1088 | jpegBuf=NULL; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1089 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1090 | jdstSizes=(*env)->NewIntArray(env, n); |
| 1091 | bailif0(dstSizesi=(*env)->GetIntArrayElements(env, jdstSizes, 0)); |
| 1092 | for(i=0; i<n; i++) dstSizesi[i]=(int)dstSizes[i]; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1093 | |
| 1094 | bailout: |
DRC | 8951cf0 | 2014-08-14 16:54:04 +0000 | [diff] [blame] | 1095 | if(dstSizesi) (*env)->ReleaseIntArrayElements(env, jdstSizes, dstSizesi, 0); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1096 | if(dstBufs) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1097 | { |
| 1098 | for(i=0; i<n; i++) |
| 1099 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1100 | if(dstBufs[i] && jdstBufs && jdstBufs[i]) |
| 1101 | (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1102 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1103 | free(dstBufs); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1104 | } |
DRC | 8951cf0 | 2014-08-14 16:54:04 +0000 | [diff] [blame] | 1105 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1106 | if(jdstBufs) free(jdstBufs); |
| 1107 | if(dstSizes) free(dstSizes); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1108 | if(t) free(t); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 1109 | return jdstSizes; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
DRC | a4940d1 | 2014-08-15 16:07:15 +0000 | [diff] [blame] | 1112 | /* TurboJPEG 1.2.x: TJDecompressor::destroy() */ |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 1113 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_destroy |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 1114 | (JNIEnv *env, jobject obj) |
| 1115 | { |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 1116 | Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy(env, obj); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 1117 | } |