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