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