DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C)2011 D. R. Commander. All Rights Reserved. |
| 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 | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 353 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIII |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 354 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst, |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 355 | 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; |
| 358 | jsize arraySize=0; |
| 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 | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 370 | arraySize=(pitch==0)? width*tjPixelSize[pf]*height:pitch*height; |
| 371 | if((*env)->GetArrayLength(env, dst)<arraySize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 372 | _throw("Destination buffer is not large enough"); |
| 373 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 374 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 375 | bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 376 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 377 | if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize, dstBuf, width, |
| 378 | pitch, height, pf, flags)==-1) |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 379 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 380 | (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 381 | (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
| 382 | dstBuf=jpegBuf=NULL; |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 383 | _throw(tjGetErrorStr()); |
| 384 | } |
| 385 | |
| 386 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 387 | if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 388 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 389 | return; |
| 390 | } |
| 391 | |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 392 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIII |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 393 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst, |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 394 | jint width, jint pitch, jint height, jint pf, jint flags) |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 395 | { |
DRC | 1a3dbe6 | 2011-02-28 10:51:55 +0000 | [diff] [blame] | 396 | tjhandle handle=0; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 397 | jsize arraySize=0; |
| 398 | unsigned char *jpegBuf=NULL, *dstBuf=NULL; |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 399 | |
| 400 | gethandle(); |
| 401 | |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 402 | if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 403 | _throw("Invalid argument in decompress()"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 404 | if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) |
| 405 | _throw("Mismatch between Java and C API"); |
| 406 | if(tjPixelSize[pf]!=sizeof(jint)) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 407 | _throw("Pixel format must be 32-bit when decompressing to an integer buffer."); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 408 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 409 | if((*env)->GetArrayLength(env, src)<jpegSize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 410 | _throw("Source buffer is not large enough"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 411 | arraySize=(pitch==0)? width*height:pitch*height; |
| 412 | if((*env)->GetArrayLength(env, dst)<arraySize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 413 | _throw("Destination buffer is not large enough"); |
| 414 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 415 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 416 | bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 417 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 418 | if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize, dstBuf, width, |
| 419 | pitch*sizeof(jint), height, pf, flags)==-1) |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 420 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 421 | (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 422 | (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
| 423 | dstBuf=jpegBuf=NULL; |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 424 | _throw(tjGetErrorStr()); |
| 425 | } |
| 426 | |
| 427 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 428 | if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 429 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 430 | return; |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 431 | } |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 432 | |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 433 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 434 | (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst, |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 435 | jint flags) |
| 436 | { |
| 437 | tjhandle handle=0; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 438 | unsigned char *jpegBuf=NULL, *dstBuf=NULL; |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 439 | int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0; |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 440 | |
| 441 | gethandle(); |
| 442 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 443 | if((*env)->GetArrayLength(env, src)<jpegSize) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 444 | _throw("Source buffer is not large enough"); |
| 445 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); |
| 446 | jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); |
| 447 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); |
| 448 | jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); |
| 449 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); |
| 450 | jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); |
| 451 | if((*env)->GetArrayLength(env, dst) |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 452 | <(jsize)tjBufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp)) |
DRC | 6acf52b | 2011-03-02 01:09:20 +0000 | [diff] [blame] | 453 | _throw("Destination buffer is not large enough"); |
| 454 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 455 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); |
| 456 | bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 457 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 458 | if(tjDecompressToYUV(handle, jpegBuf, (unsigned long)jpegSize, dstBuf, |
| 459 | flags)==-1) |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 460 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 461 | (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 462 | (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
| 463 | dstBuf=jpegBuf=NULL; |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 464 | _throw(tjGetErrorStr()); |
| 465 | } |
| 466 | |
| 467 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 468 | if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); |
| 469 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 470 | return; |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 471 | } |
| 472 | |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 473 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_init |
| 474 | (JNIEnv *env, jobject obj) |
| 475 | { |
| 476 | jclass cls; |
| 477 | jfieldID fid; |
| 478 | tjhandle handle; |
| 479 | |
| 480 | if((handle=tjInitTransform())==NULL) _throw(tjGetErrorStr()); |
| 481 | |
| 482 | bailif0(cls=(*env)->GetObjectClass(env, obj)); |
| 483 | bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); |
DRC | 23da0c0 | 2011-03-04 15:28:16 +0000 | [diff] [blame] | 484 | (*env)->SetLongField(env, obj, fid, (jlong)handle); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 485 | |
| 486 | bailout: |
| 487 | return; |
| 488 | } |
| 489 | |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame^] | 490 | typedef struct _JNICustomFilterParams |
| 491 | { |
| 492 | JNIEnv *env; |
| 493 | jobject tobj; |
| 494 | jobject cfobj; |
| 495 | } JNICustomFilterParams; |
| 496 | |
| 497 | static int JNICustomFilter(short *coeffs, tjregion arrayRegion, |
| 498 | tjregion planeRegion, int componentIndex, int transformIndex, |
| 499 | tjtransform *transform) |
| 500 | { |
| 501 | JNICustomFilterParams *params=(JNICustomFilterParams *)transform->data; |
| 502 | JNIEnv *env=params->env; |
| 503 | jobject tobj=params->tobj, cfobj=params->cfobj; |
| 504 | jobject arrayRegionObj, planeRegionObj, bufobj, borobj; |
| 505 | jclass cls; jmethodID mid; jfieldID fid; |
| 506 | |
| 507 | bailif0(bufobj=(*env)->NewDirectByteBuffer(env, coeffs, |
| 508 | sizeof(short)*arrayRegion.w*arrayRegion.h)); |
| 509 | bailif0(cls=(*env)->FindClass(env, "java/nio/ByteOrder")); |
| 510 | bailif0(mid=(*env)->GetStaticMethodID(env, cls, "nativeOrder", |
| 511 | "()Ljava/nio/ByteOrder;")); |
| 512 | bailif0(borobj=(*env)->CallStaticObjectMethod(env, cls, mid)); |
| 513 | bailif0(cls=(*env)->GetObjectClass(env, bufobj)); |
| 514 | bailif0(mid=(*env)->GetMethodID(env, cls, "order", |
| 515 | "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;")); |
| 516 | (*env)->CallObjectMethod(env, bufobj, mid, borobj); |
| 517 | bailif0(mid=(*env)->GetMethodID(env, cls, "asShortBuffer", |
| 518 | "()Ljava/nio/ShortBuffer;")); |
| 519 | bailif0(bufobj=(*env)->CallObjectMethod(env, bufobj, mid)); |
| 520 | |
| 521 | bailif0(cls=(*env)->FindClass(env, "java/awt/Rectangle")); |
| 522 | bailif0(arrayRegionObj=(*env)->AllocObject(env, cls)); |
| 523 | bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I")); |
| 524 | (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.x); |
| 525 | bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I")); |
| 526 | (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.y); |
| 527 | bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I")); |
| 528 | (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.w); |
| 529 | bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I")); |
| 530 | (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.h); |
| 531 | |
| 532 | bailif0(planeRegionObj=(*env)->AllocObject(env, cls)); |
| 533 | bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I")); |
| 534 | (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.x); |
| 535 | bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I")); |
| 536 | (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.y); |
| 537 | bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I")); |
| 538 | (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.w); |
| 539 | bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I")); |
| 540 | (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.h); |
| 541 | |
| 542 | bailif0(cls=(*env)->GetObjectClass(env, cfobj)); |
| 543 | bailif0(mid=(*env)->GetMethodID(env, cls, "customFilter", |
| 544 | "(Ljava/nio/ShortBuffer;Ljava/awt/Rectangle;Ljava/awt/Rectangle;IILorg/libjpegturbo/turbojpeg/TJTransform;)V")); |
| 545 | (*env)->CallVoidMethod(env, cfobj, mid, bufobj, arrayRegionObj, |
| 546 | planeRegionObj, componentIndex, transformIndex, tobj); |
| 547 | |
| 548 | return 0; |
| 549 | |
| 550 | bailout: |
| 551 | return -1; |
| 552 | } |
| 553 | |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 554 | JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transform |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 555 | (JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize, |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 556 | jobjectArray dstobjs, jobjectArray tobjs, jint flags) |
| 557 | { |
| 558 | tjhandle handle=0; int i; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 559 | unsigned char *jpegBuf=NULL, **dstBufs=NULL; jsize n=0; |
| 560 | unsigned long *dstSizes=NULL; tjtransform *t=NULL; |
| 561 | jbyteArray *jdstBufs=NULL; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 562 | int jpegWidth=0, jpegHeight=0, jpegSubsamp; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 563 | jintArray jdstSizes=0; jint *dstSizesi=NULL; |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame^] | 564 | JNICustomFilterParams *params=NULL; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 565 | |
| 566 | gethandle(); |
| 567 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 568 | if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 569 | _throw("Source buffer is not large enough"); |
| 570 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); |
| 571 | jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); |
| 572 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); |
| 573 | jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 574 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); |
| 575 | jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 576 | |
| 577 | n=(*env)->GetArrayLength(env, dstobjs); |
| 578 | if(n!=(*env)->GetArrayLength(env, tobjs)) |
| 579 | _throw("Mismatch between size of transforms array and destination buffers array"); |
| 580 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 581 | if((dstBufs=(unsigned char **)malloc(sizeof(unsigned char *)*n))==NULL) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 582 | _throw("Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 583 | if((jdstBufs=(jbyteArray *)malloc(sizeof(jbyteArray)*n))==NULL) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 584 | _throw("Memory allocation failure"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 585 | if((dstSizes=(unsigned long *)malloc(sizeof(unsigned long)*n))==NULL) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 586 | _throw("Memory allocation failure"); |
| 587 | if((t=(tjtransform *)malloc(sizeof(tjtransform)*n))==NULL) |
| 588 | _throw("Memory allocation failure"); |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame^] | 589 | if((params=(JNICustomFilterParams *)malloc(sizeof(JNICustomFilterParams)*n)) |
| 590 | ==NULL) |
| 591 | _throw("Memory allocation failure"); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 592 | for(i=0; i<n; i++) |
| 593 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 594 | dstBufs[i]=NULL; jdstBufs[i]=NULL; dstSizes[i]=0; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 595 | memset(&t[i], 0, sizeof(tjtransform)); |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame^] | 596 | memset(¶ms[i], 0, sizeof(JNICustomFilterParams)); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | for(i=0; i<n; i++) |
| 600 | { |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame^] | 601 | jobject tobj, cfobj; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 602 | |
| 603 | bailif0(tobj=(*env)->GetObjectArrayElement(env, tobjs, i)); |
| 604 | bailif0(_cls=(*env)->GetObjectClass(env, tobj)); |
| 605 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "op", "I")); |
| 606 | t[i].op=(*env)->GetIntField(env, tobj, _fid); |
| 607 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "options", "I")); |
| 608 | t[i].options=(*env)->GetIntField(env, tobj, _fid); |
| 609 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "x", "I")); |
| 610 | t[i].r.x=(*env)->GetIntField(env, tobj, _fid); |
| 611 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "y", "I")); |
| 612 | t[i].r.y=(*env)->GetIntField(env, tobj, _fid); |
| 613 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "width", "I")); |
| 614 | t[i].r.w=(*env)->GetIntField(env, tobj, _fid); |
| 615 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "height", "I")); |
| 616 | t[i].r.h=(*env)->GetIntField(env, tobj, _fid); |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame^] | 617 | |
| 618 | t[i].customFilter=JNICustomFilter; |
| 619 | bailif0(_fid=(*env)->GetFieldID(env, _cls, "cf", |
| 620 | "Lorg/libjpegturbo/turbojpeg/TJCustomFilter;")); |
| 621 | bailif0(cfobj=(*env)->GetObjectField(env, tobj, _fid)); |
| 622 | params[i].env=env; |
| 623 | params[i].tobj=tobj; |
| 624 | params[i].cfobj=cfobj; |
| 625 | t[i].data=(void *)¶ms[i]; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 626 | } |
| 627 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 628 | bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0)); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 629 | for(i=0; i<n; i++) |
| 630 | { |
| 631 | int w=jpegWidth, h=jpegHeight; |
| 632 | if(t[i].r.w!=0) w=t[i].r.w; |
| 633 | if(t[i].r.h!=0) h=t[i].r.h; |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 634 | bailif0(jdstBufs[i]=(*env)->GetObjectArrayElement(env, dstobjs, i)); |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 635 | if((*env)->GetArrayLength(env, jdstBufs[i])<tjBufSize(w, h, jpegSubsamp)) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 636 | _throw("Destination buffer is not large enough"); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 637 | bailif0(dstBufs[i]=(*env)->GetPrimitiveArrayCritical(env, jdstBufs[i], 0)); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 638 | } |
| 639 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 640 | if(tjTransform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t, |
DRC | 4db92ad | 2011-05-25 04:52:25 +0000 | [diff] [blame] | 641 | flags|TJFLAG_NOREALLOC)==-1) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 642 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 643 | (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0); |
| 644 | jpegBuf=NULL; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 645 | for(i=0; i<n; i++) |
| 646 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 647 | (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0); |
| 648 | dstBufs[i]=NULL; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 649 | } |
| 650 | _throw(tjGetErrorStr()); |
| 651 | } |
| 652 | |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 653 | jdstSizes=(*env)->NewIntArray(env, n); |
| 654 | bailif0(dstSizesi=(*env)->GetIntArrayElements(env, jdstSizes, 0)); |
| 655 | for(i=0; i<n; i++) dstSizesi[i]=(int)dstSizes[i]; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 656 | |
| 657 | bailout: |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 658 | if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0); |
| 659 | if(dstBufs) |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 660 | { |
| 661 | for(i=0; i<n; i++) |
| 662 | { |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 663 | if(dstBufs[i] && jdstBufs && jdstBufs[i]) |
| 664 | (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 665 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 666 | free(dstBufs); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 667 | } |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 668 | if(jdstBufs) free(jdstBufs); |
| 669 | if(dstSizes) free(dstSizes); |
| 670 | if(dstSizesi) (*env)->ReleaseIntArrayElements(env, jdstSizes, dstSizesi, 0); |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 671 | if(t) free(t); |
DRC | 9b28def | 2011-05-21 14:37:15 +0000 | [diff] [blame] | 672 | return jdstSizes; |
DRC | e857301 | 2011-03-04 10:13:59 +0000 | [diff] [blame] | 673 | } |
| 674 | |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 675 | JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_destroy |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 676 | (JNIEnv *env, jobject obj) |
| 677 | { |
DRC | c5a4199 | 2011-02-08 06:54:36 +0000 | [diff] [blame] | 678 | Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy(env, obj); |
DRC | f8e0055 | 2011-02-04 11:06:36 +0000 | [diff] [blame] | 679 | } |