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