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