Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: turbojpeg-jni.c

Issue 1934113002: Update libjpeg_turbo to 1.4.90 from https://github.com/libjpeg-turbo/ (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/libjpeg_turbo.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C)2011-2013 D. R. Commander. All Rights Reserved. 2 * Copyright (C)2011-2016 D. R. Commander. All Rights Reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met: 5 * modification, are permitted provided that the following conditions are met:
6 * 6 *
7 * - Redistributions of source code must retain the above copyright notice, 7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer. 8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice, 9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation 10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution. 11 * and/or other materials provided with the distribution.
12 * - Neither the name of the libjpeg-turbo Project nor the names of its 12 * - Neither the name of the libjpeg-turbo Project nor the names of its
(...skipping 17 matching lines...) Expand all
30 #include <string.h> 30 #include <string.h>
31 #include "turbojpeg.h" 31 #include "turbojpeg.h"
32 #ifdef WIN32 32 #ifdef WIN32
33 #include "tjutil.h" 33 #include "tjutil.h"
34 #endif 34 #endif
35 #include <jni.h> 35 #include <jni.h>
36 #include "java/org_libjpegturbo_turbojpeg_TJCompressor.h" 36 #include "java/org_libjpegturbo_turbojpeg_TJCompressor.h"
37 #include "java/org_libjpegturbo_turbojpeg_TJDecompressor.h" 37 #include "java/org_libjpegturbo_turbojpeg_TJDecompressor.h"
38 #include "java/org_libjpegturbo_turbojpeg_TJ.h" 38 #include "java/org_libjpegturbo_turbojpeg_TJ.h"
39 39
40 #define _throw(msg) { \ 40 #define PAD(v, p) ((v+(p)-1)&(~((p)-1)))
41 » jclass _exccls=(*env)->FindClass(env, "java/lang/Exception"); \ 41
42 » if(!_exccls) goto bailout; \ 42 #define _throw(msg, exceptionClass) { \
43 » jclass _exccls=(*env)->FindClass(env, exceptionClass); \
44 » if(!_exccls || (*env)->ExceptionCheck(env)) goto bailout; \
43 (*env)->ThrowNew(env, _exccls, msg); \ 45 (*env)->ThrowNew(env, _exccls, msg); \
44 goto bailout; \ 46 goto bailout; \
45 } 47 }
46 48
47 #define bailif0(f) {if(!(f)) { \ 49 #define _throwtj() _throw(tjGetErrorStr(), "org/libjpegturbo/turbojpeg/TJExcepti on")
48 » char temps[80]; \ 50
49 » snprintf(temps, 80, "Unexpected NULL condition in line %d", __LINE__); \ 51 #define _throwarg(msg) _throw(msg, "java/lang/IllegalArgumentException")
50 » _throw(temps); \ 52
53 #define _throwmem() _throw("Memory allocation failure", "java/lang/OutOfMemoryEr ror");
54
55 #define bailif0(f) {if(!(f) || (*env)->ExceptionCheck(env)) { \
56 » goto bailout; \
51 }} 57 }}
52 58
53 #define gethandle() \ 59 #define gethandle() \
54 jclass _cls=(*env)->GetObjectClass(env, obj); \ 60 jclass _cls=(*env)->GetObjectClass(env, obj); \
55 jfieldID _fid; \ 61 jfieldID _fid; \
56 » if(!_cls) goto bailout; \ 62 » if(!_cls || (*env)->ExceptionCheck(env)) goto bailout; \
57 bailif0(_fid=(*env)->GetFieldID(env, _cls, "handle", "J")); \ 63 bailif0(_fid=(*env)->GetFieldID(env, _cls, "handle", "J")); \
58 » handle=(tjhandle)(jlong)(*env)->GetLongField(env, obj, _fid); \ 64 » handle=(tjhandle)(size_t)(*env)->GetLongField(env, obj, _fid); \
59 65
66 #ifdef _WIN32
67 #define setenv(envvar, value, dummy) _putenv_s(envvar, value)
68 #endif
69
70 #define prop2env(property, envvar) \
71 { \
72 if((jName=(*env)->NewStringUTF(env, property))!=NULL \
73 && (jValue=(*env)->CallStaticObjectMethod(env, cls, mid, jName)) !=NULL) \
74 { \
75 if((value=(*env)->GetStringUTFChars(env, jValue, 0))!=NULL) \
76 { \
77 setenv(envvar, value, 1); \
78 (*env)->ReleaseStringUTFChars(env, jValue, value); \
79 } \
80 } \
81 }
82
83 int ProcessSystemProperties(JNIEnv *env)
84 {
85 jclass cls; jmethodID mid;
86 jstring jName, jValue;
87 const char *value;
88
89 bailif0(cls=(*env)->FindClass(env, "java/lang/System"));
90 bailif0(mid=(*env)->GetStaticMethodID(env, cls, "getProperty",
91 "(Ljava/lang/String;)Ljava/lang/String;"));
92
93 prop2env("turbojpeg.optimize", "TJ_OPTIMIZE");
94 prop2env("turbojpeg.arithmetic", "TJ_ARITHMETIC");
95 prop2env("turbojpeg.restart", "TJ_RESTART");
96 prop2env("turbojpeg.progressive", "TJ_PROGRESSIVE");
97 return 0;
98
99 bailout:
100 return -1;
101 }
102
103 /* TurboJPEG 1.2.x: TJ::bufSize() */
60 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize 104 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize
61 (JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp) 105 (JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp)
62 { 106 {
63 jint retval=(jint)tjBufSize(width, height, jpegSubsamp); 107 jint retval=(jint)tjBufSize(width, height, jpegSubsamp);
64 » if(retval==-1) _throw(tjGetErrorStr()); 108 » if(retval==-1) _throwarg(tjGetErrorStr());
65 109
66 bailout: 110 bailout:
67 return retval; 111 return retval;
68 } 112 }
69 113
70 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV 114 /* TurboJPEG 1.4.x: TJ::bufSizeYUV() */
71 » (JNIEnv *env, jclass cls, jint width, jint height, jint subsamp) 115 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII
116 » (JNIEnv *env, jclass cls, jint width, jint pad, jint height, jint subsam p)
72 { 117 {
73 » jint retval=(jint)tjBufSizeYUV(width, height, subsamp); 118 » jint retval=(jint)tjBufSizeYUV2(width, pad, height, subsamp);
74 » if(retval==-1) _throw(tjGetErrorStr()); 119 » if(retval==-1) _throwarg(tjGetErrorStr());
75 120
76 bailout: 121 bailout:
77 return retval; 122 return retval;
78 } 123 }
79 124
125 /* TurboJPEG 1.2.x: TJ::bufSizeYUV() */
126 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__III
127 (JNIEnv *env, jclass cls, jint width, jint height, jint subsamp)
128 {
129 return Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII(env, cls, wid th,
130 4, height, subsamp);
131 }
132
133 /* TurboJPEG 1.4.x: TJ::planeSizeYUV() */
134 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeSizeYUV__IIIII
135 (JNIEnv *env, jclass cls, jint componentID, jint width, jint stride,
136 jint height, jint subsamp)
137 {
138 jint retval=(jint)tjPlaneSizeYUV(componentID, width, stride, height,
139 subsamp);
140 if(retval==-1) _throwarg(tjGetErrorStr());
141
142 bailout:
143 return retval;
144 }
145
146 /* TurboJPEG 1.4.x: TJ::planeWidth() */
147 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeWidth__III
148 (JNIEnv *env, jclass cls, jint componentID, jint width, jint subsamp)
149 {
150 jint retval=(jint)tjPlaneWidth(componentID, width, subsamp);
151 if(retval==-1) _throwarg(tjGetErrorStr());
152
153 bailout:
154 return retval;
155 }
156
157 /* TurboJPEG 1.4.x: TJ::planeHeight() */
158 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeHeight__III
159 (JNIEnv *env, jclass cls, jint componentID, jint height, jint subsamp)
160 {
161 jint retval=(jint)tjPlaneHeight(componentID, height, subsamp);
162 if(retval==-1) _throwarg(tjGetErrorStr());
163
164 bailout:
165 return retval;
166 }
167
168 /* TurboJPEG 1.2.x: TJCompressor::init() */
80 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_init 169 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_init
81 (JNIEnv *env, jobject obj) 170 (JNIEnv *env, jobject obj)
82 { 171 {
83 jclass cls; 172 jclass cls;
84 jfieldID fid; 173 jfieldID fid;
85 tjhandle handle; 174 tjhandle handle;
86 175
87 if((handle=tjInitCompress())==NULL) 176 if((handle=tjInitCompress())==NULL)
88 » » _throw(tjGetErrorStr()); 177 » » _throwtj();
89 178
90 bailif0(cls=(*env)->GetObjectClass(env, obj)); 179 bailif0(cls=(*env)->GetObjectClass(env, obj));
91 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); 180 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J"));
92 » (*env)->SetLongField(env, obj, fid, (jlong)handle); 181 » (*env)->SetLongField(env, obj, fid, (size_t)handle);
93 182
94 bailout: 183 bailout:
95 return; 184 return;
96 } 185 }
97 186
98 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3 BIIIIII_3BIII 187 static jint TJCompressor_compress
99 » (JNIEnv *env, jobject obj, jbyteArray src, jint x, jint y, jint width, 188 » (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint x, jint y,
100 » » jint pitch, jint height, jint pf, jbyteArray dst, jint jpegSubsa mp, 189 » » jint width, jint pitch, jint height, jint pf, jbyteArray dst,
101 » » jint jpegQual, jint flags) 190 » » jint jpegSubsamp, jint jpegQual, jint flags)
102 { 191 {
103 tjhandle handle=0; 192 tjhandle handle=0;
104 unsigned long jpegSize=0; 193 unsigned long jpegSize=0;
105 jsize arraySize=0, actualPitch; 194 jsize arraySize=0, actualPitch;
106 unsigned char *srcBuf=NULL, *jpegBuf=NULL; 195 unsigned char *srcBuf=NULL, *jpegBuf=NULL;
107 196
108 gethandle(); 197 gethandle();
109 198
110 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height< 1 199 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height< 1
111 || pitch<0) 200 || pitch<0)
112 » » _throw("Invalid argument in compress()"); 201 » » _throwarg("Invalid argument in compress()");
113 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) 202 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
114 » » _throw("Mismatch between Java and C API"); 203 » » _throwarg("Mismatch between Java and C API");
115 204
116 actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch; 205 actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
117 » arraySize=(y+height-1)*actualPitch + x+width; 206 » arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf];
118 » if((*env)->GetArrayLength(env, src)<arraySize) 207 » if((*env)->GetArrayLength(env, src)*srcElementSize<arraySize)
119 » » _throw("Source buffer is not large enough"); 208 » » _throwarg("Source buffer is not large enough");
120 jpegSize=tjBufSize(width, height, jpegSubsamp); 209 jpegSize=tjBufSize(width, height, jpegSubsamp);
121 if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize) 210 if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize)
122 » » _throw("Destination buffer is not large enough"); 211 » » _throwarg("Destination buffer is not large enough");
123 212
124 bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); 213 bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
125 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); 214 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
126 215
216 if(ProcessSystemProperties(env)<0) goto bailout;
217
127 if(tjCompress2(handle, &srcBuf[y*actualPitch + x*tjPixelSize[pf]], width , 218 if(tjCompress2(handle, &srcBuf[y*actualPitch + x*tjPixelSize[pf]], width ,
128 pitch, height, pf, &jpegBuf, &jpegSize, jpegSubsamp, jpegQual, 219 pitch, height, pf, &jpegBuf, &jpegSize, jpegSubsamp, jpegQual,
129 flags|TJFLAG_NOREALLOC)==-1) 220 flags|TJFLAG_NOREALLOC)==-1)
130 » { 221 » » _throwtj();
131 » » (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
132 » » (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
133 » » jpegBuf=srcBuf=NULL;
134 » » _throw(tjGetErrorStr());
135 » }
136 222
137 bailout: 223 bailout:
138 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); 224 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
139 if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); 225 if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
140 return (jint)jpegSize; 226 return (jint)jpegSize;
141 } 227 }
142 228
229 /* TurboJPEG 1.3.x: TJCompressor::compress() byte source */
230 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3 BIIIIII_3BIII
231 (JNIEnv *env, jobject obj, jbyteArray src, jint x, jint y, jint width,
232 jint pitch, jint height, jint pf, jbyteArray dst, jint jpegSubsa mp,
233 jint jpegQual, jint flags)
234 {
235 return TJCompressor_compress(env, obj, src, 1, x, y, width, pitch, heigh t,
236 pf, dst, jpegSubsamp, jpegQual, flags);
237 }
238
239 /* TurboJPEG 1.2.x: TJCompressor::compress() byte source */
143 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3 BIIII_3BIII 240 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3 BIIII_3BIII
144 (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch, 241 (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch,
145 jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpe gQual, 242 jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpe gQual,
146 jint flags) 243 jint flags)
147 { 244 {
148 » return Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIIIII_ 3BIII( 245 » return TJCompressor_compress(env, obj, src, 1, 0, 0, width, pitch, heigh t,
149 » » env, obj, src, 0, 0, width, pitch, height, pf, dst, jpegSubsamp, jpegQual, 246 » » pf, dst, jpegSubsamp, jpegQual, flags);
150 » » flags); 247 }
151 } 248
152 249 /* TurboJPEG 1.3.x: TJCompressor::compress() int source */
153 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3 IIIIIII_3BIII 250 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3 IIIIIII_3BIII
154 (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width, 251 (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width,
155 jint stride, jint height, jint pf, jbyteArray dst, jint jpegSubs amp, 252 jint stride, jint height, jint pf, jbyteArray dst, jint jpegSubs amp,
156 jint jpegQual, jint flags) 253 jint jpegQual, jint flags)
157 { 254 {
255 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
256 _throwarg("Invalid argument in compress()");
257 if(tjPixelSize[pf]!=sizeof(jint))
258 _throwarg("Pixel format must be 32-bit when compressing from an integer buffer.");
259
260 return TJCompressor_compress(env, obj, src, sizeof(jint), x, y, width,
261 stride*sizeof(jint), height, pf, dst, jpegSubsamp, jpegQual, fla gs);
262
263 bailout:
264 return 0;
265 }
266
267 /* TurboJPEG 1.2.x: TJCompressor::compress() int source */
268 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3 IIIII_3BIII
269 (JNIEnv *env, jobject obj, jintArray src, jint width, jint stride,
270 jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpe gQual,
271 jint flags)
272 {
273 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
274 _throwarg("Invalid argument in compress()");
275 if(tjPixelSize[pf]!=sizeof(jint))
276 _throwarg("Pixel format must be 32-bit when compressing from an integer buffer.");
277
278 return TJCompressor_compress(env, obj, src, sizeof(jint), 0, 0, width,
279 stride*sizeof(jint), height, pf, dst, jpegSubsamp, jpegQual, fla gs);
280
281 bailout:
282 return 0;
283 }
284
285 /* TurboJPEG 1.4.x: TJCompressor::compressFromYUV() */
286 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compressFrom YUV___3_3B_3II_3III_3BII
287 (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
288 jint width, jintArray jSrcStrides, jint height, jint subsamp,
289 jbyteArray dst, jint jpegQual, jint flags)
290 {
158 tjhandle handle=0; 291 tjhandle handle=0;
159 unsigned long jpegSize=0; 292 unsigned long jpegSize=0;
160 » jsize arraySize=0, actualStride; 293 » jbyteArray jSrcPlanes[3]={NULL, NULL, NULL};
161 » unsigned char *srcBuf=NULL, *jpegBuf=NULL; 294 » const unsigned char *srcPlanes[3];
295 » unsigned char *jpegBuf=NULL;
296 » int *srcOffsets=NULL, *srcStrides=NULL;
297 » int nc=(subsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3), i;
162 298
163 gethandle(); 299 gethandle();
164 300
301 if(subsamp<0 || subsamp>=org_libjpegturbo_turbojpeg_TJ_NUMSAMP)
302 _throwarg("Invalid argument in compressFromYUV()");
303 if(org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP)
304 _throwarg("Mismatch between Java and C API");
305
306 if((*env)->GetArrayLength(env, srcobjs)<nc)
307 _throwarg("Planes array is too small for the subsampling type");
308 if((*env)->GetArrayLength(env, jSrcOffsets)<nc)
309 _throwarg("Offsets array is too small for the subsampling type") ;
310 if((*env)->GetArrayLength(env, jSrcStrides)<nc)
311 _throwarg("Strides array is too small for the subsampling type") ;
312
313 jpegSize=tjBufSize(width, height, subsamp);
314 if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize)
315 _throwarg("Destination buffer is not large enough");
316
317 bailif0(srcOffsets=(*env)->GetPrimitiveArrayCritical(env, jSrcOffsets, 0 ));
318 bailif0(srcStrides=(*env)->GetPrimitiveArrayCritical(env, jSrcStrides, 0 ));
319 for(i=0; i<nc; i++)
320 {
321 int planeSize=tjPlaneSizeYUV(i, width, srcStrides[i], height, su bsamp);
322 int pw=tjPlaneWidth(i, width, subsamp);
323
324 if(planeSize<0 || pw<0)
325 _throwarg(tjGetErrorStr());
326
327 if(srcOffsets[i]<0)
328 _throwarg("Invalid argument in compressFromYUV()");
329 if(srcStrides[i]<0 && srcOffsets[i]-planeSize+pw<0)
330 _throwarg("Negative plane stride would cause memory to b e accessed below plane boundary");
331
332 bailif0(jSrcPlanes[i]=(*env)->GetObjectArrayElement(env, srcobjs , i));
333 if((*env)->GetArrayLength(env, jSrcPlanes[i])<srcOffsets[i]+plan eSize)
334 _throwarg("Source plane is not large enough");
335
336 bailif0(srcPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jSrc Planes[i],
337 0));
338 srcPlanes[i]=&srcPlanes[i][srcOffsets[i]];
339 }
340 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
341
342 if(ProcessSystemProperties(env)<0) goto bailout;
343
344 if(tjCompressFromYUVPlanes(handle, srcPlanes, width, srcStrides, height,
345 subsamp, &jpegBuf, &jpegSize, jpegQual, flags|TJFLAG_NOREALLOC)= =-1)
346 _throwtj();
347
348 bailout:
349 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
350 for(i=0; i<nc; i++)
351 {
352 if(srcPlanes[i] && jSrcPlanes[i])
353 (*env)->ReleasePrimitiveArrayCritical(env, jSrcPlanes[i] ,
354 (unsigned char *)srcPlanes[i], 0);
355 }
356 if(srcStrides)
357 (*env)->ReleasePrimitiveArrayCritical(env, jSrcStrides, srcStrid es, 0);
358 if(srcOffsets)
359 (*env)->ReleasePrimitiveArrayCritical(env, jSrcOffsets, srcOffse ts, 0);
360 return (jint)jpegSize;
361 }
362
363 static void TJCompressor_encodeYUV
364 (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint x, jint y,
365 jint width, jint pitch, jint height, jint pf, jobjectArray dstob js,
366 jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags)
367 {
368 tjhandle handle=0;
369 jsize arraySize=0, actualPitch;
370 jbyteArray jDstPlanes[3]={NULL, NULL, NULL};
371 unsigned char *srcBuf=NULL, *dstPlanes[3];
372 int *dstOffsets=NULL, *dstStrides=NULL;
373 int nc=(subsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3), i;
374
375 gethandle();
376
165 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height< 1 377 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height< 1
166 » » || stride<0) 378 » » || pitch<0 || subsamp<0 || subsamp>=org_libjpegturbo_turbojpeg_T J_NUMSAMP)
167 » » _throw("Invalid argument in compress()"); 379 » » _throwarg("Invalid argument in encodeYUV()");
168 » if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) 380 » if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF
169 » » _throw("Mismatch between Java and C API"); 381 » » || org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP)
382 » » _throwarg("Mismatch between Java and C API");
383
384 » if((*env)->GetArrayLength(env, dstobjs)<nc)
385 » » _throwarg("Planes array is too small for the subsampling type");
386 » if((*env)->GetArrayLength(env, jDstOffsets)<nc)
387 » » _throwarg("Offsets array is too small for the subsampling type") ;
388 » if((*env)->GetArrayLength(env, jDstStrides)<nc)
389 » » _throwarg("Strides array is too small for the subsampling type") ;
390
391 » actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
392 » arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf];
393 » if((*env)->GetArrayLength(env, src)*srcElementSize<arraySize)
394 » » _throwarg("Source buffer is not large enough");
395
396 » bailif0(dstOffsets=(*env)->GetPrimitiveArrayCritical(env, jDstOffsets, 0 ));
397 » bailif0(dstStrides=(*env)->GetPrimitiveArrayCritical(env, jDstStrides, 0 ));
398 » for(i=0; i<nc; i++)
399 » {
400 » » int planeSize=tjPlaneSizeYUV(i, width, dstStrides[i], height, su bsamp);
401 » » int pw=tjPlaneWidth(i, width, subsamp);
402
403 » » if(planeSize<0 || pw<0)
404 » » » _throwarg(tjGetErrorStr());
405
406 » » if(dstOffsets[i]<0)
407 » » » _throwarg("Invalid argument in encodeYUV()");
408 » » if(dstStrides[i]<0 && dstOffsets[i]-planeSize+pw<0)
409 » » » _throwarg("Negative plane stride would cause memory to b e accessed below plane boundary");
410
411 » » bailif0(jDstPlanes[i]=(*env)->GetObjectArrayElement(env, dstobjs , i));
412 » » if((*env)->GetArrayLength(env, jDstPlanes[i])<dstOffsets[i]+plan eSize)
413 » » » _throwarg("Destination plane is not large enough");
414
415 » » bailif0(dstPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jDst Planes[i],
416 » » » 0));
417 » » dstPlanes[i]=&dstPlanes[i][dstOffsets[i]];
418 » }
419 » bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
420
421 » if(tjEncodeYUVPlanes(handle, &srcBuf[y*actualPitch + x*tjPixelSize[pf]],
422 » » width, pitch, height, pf, dstPlanes, dstStrides, subsamp, flags) ==-1)
423 » » _throwtj();
424
425 » bailout:
426 » if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
427 » for(i=0; i<nc; i++)
428 » {
429 » » if(dstPlanes[i] && jDstPlanes[i])
430 » » » (*env)->ReleasePrimitiveArrayCritical(env, jDstPlanes[i] , dstPlanes[i],
431 » » » » 0);
432 » }
433 » if(dstStrides)
434 » » (*env)->ReleasePrimitiveArrayCritical(env, jDstStrides, dstStrid es, 0);
435 » if(dstOffsets)
436 » » (*env)->ReleasePrimitiveArrayCritical(env, jDstOffsets, dstOffse ts, 0);
437 » return;
438 }
439
440 /* TurboJPEG 1.4.x: TJCompressor::encodeYUV() byte source */
441 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___ 3BIIIIII_3_3B_3I_3III
442 » (JNIEnv *env, jobject obj, jbyteArray src, jint x, jint y, jint width,
443 » » jint pitch, jint height, jint pf, jobjectArray dstobjs,
444 » » jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags)
445 {
446 » TJCompressor_encodeYUV(env, obj, src, 1, x, y, width, pitch, height, pf,
447 » » dstobjs, jDstOffsets, jDstStrides, subsamp, flags);
448 }
449
450 /* TurboJPEG 1.4.x: TJCompressor::encodeYUV() int source */
451 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___ 3IIIIIII_3_3B_3I_3III
452 » (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width,
453 » » jint stride, jint height, jint pf, jobjectArray dstobjs,
454 » » jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags)
455 {
456 » if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
457 » » _throwarg("Invalid argument in encodeYUV()");
170 if(tjPixelSize[pf]!=sizeof(jint)) 458 if(tjPixelSize[pf]!=sizeof(jint))
171 » » _throw("Pixel format must be 32-bit when compressing from an int eger buffer."); 459 » » _throwarg("Pixel format must be 32-bit when encoding from an int eger buffer.");
172 460
173 » actualStride=(stride==0)? width:stride; 461 » TJCompressor_encodeYUV(env, obj, src, sizeof(jint), x, y, width,
174 » arraySize=(y+height-1)*actualStride + x+width; 462 » » stride*sizeof(jint), height, pf, dstobjs, jDstOffsets, jDstStrid es,
175 » if((*env)->GetArrayLength(env, src)<arraySize) 463 » » subsamp, flags);
176 » » _throw("Source buffer is not large enough"); 464
177 » jpegSize=tjBufSize(width, height, jpegSubsamp); 465 » bailout:
178 » if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize) 466 » return;
179 » » _throw("Destination buffer is not large enough"); 467 }
180 468
181 » bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); 469 JNIEXPORT void JNICALL TJCompressor_encodeYUV_12
182 » bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); 470 » (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint width,
183 471 » » jint pitch, jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
184 » if(tjCompress2(handle, &srcBuf[(y*actualStride + x)*sizeof(int)], width,
185 » » stride*sizeof(jint), height, pf, &jpegBuf, &jpegSize, jpegSubsam p,
186 » » jpegQual, flags|TJFLAG_NOREALLOC)==-1)
187 » {
188 » » (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
189 » » (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
190 » » jpegBuf=srcBuf=NULL;
191 » » _throw(tjGetErrorStr());
192 » }
193
194 » bailout:
195 » if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
196 » if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
197 » return (jint)jpegSize;
198 }
199
200 JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3 IIIII_3BIII
201 » (JNIEnv *env, jobject obj, jintArray src, jint width, jint pitch,
202 » » jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpe gQual,
203 » » jint flags)
204 {
205 » return Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIIIII_ 3BIII(
206 » » env, obj, src, 0, 0, width, pitch, height, pf, dst, jpegSubsamp, jpegQual,
207 » » flags);
208 }
209
210 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___ 3BIIII_3BII
211 » (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch,
212 » » jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
213 { 472 {
214 tjhandle handle=0; 473 tjhandle handle=0;
215 jsize arraySize=0; 474 jsize arraySize=0;
216 unsigned char *srcBuf=NULL, *dstBuf=NULL; 475 unsigned char *srcBuf=NULL, *dstBuf=NULL;
217 476
218 gethandle(); 477 gethandle();
219 478
220 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height< 1 479 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height< 1
221 || pitch<0) 480 || pitch<0)
222 » » _throw("Invalid argument in encodeYUV()"); 481 » » _throwarg("Invalid argument in encodeYUV()");
223 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) 482 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
224 » » _throw("Mismatch between Java and C API"); 483 » » _throwarg("Mismatch between Java and C API");
225 484
226 arraySize=(pitch==0)? width*tjPixelSize[pf]*height:pitch*height; 485 arraySize=(pitch==0)? width*tjPixelSize[pf]*height:pitch*height;
227 » if((*env)->GetArrayLength(env, src)<arraySize) 486 » if((*env)->GetArrayLength(env, src)*srcElementSize<arraySize)
228 » » _throw("Source buffer is not large enough"); 487 » » _throwarg("Source buffer is not large enough");
229 if((*env)->GetArrayLength(env, dst) 488 if((*env)->GetArrayLength(env, dst)
230 <(jsize)tjBufSizeYUV(width, height, subsamp)) 489 <(jsize)tjBufSizeYUV(width, height, subsamp))
231 » » _throw("Destination buffer is not large enough"); 490 » » _throwarg("Destination buffer is not large enough");
232 491
233 bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); 492 bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
234 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); 493 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
235 494
236 if(tjEncodeYUV2(handle, srcBuf, width, pitch, height, pf, dstBuf, subsam p, 495 if(tjEncodeYUV2(handle, srcBuf, width, pitch, height, pf, dstBuf, subsam p,
237 flags)==-1) 496 flags)==-1)
238 » { 497 » » _throwtj();
239 » » (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
240 » » (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
241 » » dstBuf=srcBuf=NULL;
242 » » _throw(tjGetErrorStr());
243 » }
244 498
245 bailout: 499 bailout:
246 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); 500 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
247 if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); 501 if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
248 return; 502 return;
249 } 503 }
250 504
505 /* TurboJPEG 1.2.x: TJCompressor::encodeYUV() byte source */
506 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___ 3BIIII_3BII
507 (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch,
508 jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
509 {
510 TJCompressor_encodeYUV_12(env, obj, src, 1, width, pitch, height, pf, ds t,
511 subsamp, flags);
512 }
513
514 /* TurboJPEG 1.2.x: TJCompressor::encodeYUV() int source */
251 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___ 3IIIII_3BII 515 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___ 3IIIII_3BII
252 (JNIEnv *env, jobject obj, jintArray src, jint width, jint stride, 516 (JNIEnv *env, jobject obj, jintArray src, jint width, jint stride,
253 jint height, jint pf, jbyteArray dst, jint subsamp, jint flags) 517 jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
254 { 518 {
255 » tjhandle handle=0; 519 » if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
256 » jsize arraySize=0; 520 » » _throwarg("Invalid argument in encodeYUV()");
257 » unsigned char *srcBuf=NULL, *dstBuf=NULL; 521 » if(tjPixelSize[pf]!=sizeof(jint))
522 » » _throwarg("Pixel format must be 32-bit when encoding from an int eger buffer.");
258 523
259 » gethandle(); 524 » TJCompressor_encodeYUV_12(env, obj, src, sizeof(jint), width,
260 525 » » stride*sizeof(jint), height, pf, dst, subsamp, flags);
261 » if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height< 1
262 » » || stride<0)
263 » » _throw("Invalid argument in encodeYUV()");
264 » if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
265 » » _throw("Mismatch between Java and C API");
266 » if(tjPixelSize[pf]!=sizeof(jint))
267 » » _throw("Pixel format must be 32-bit when encoding from an intege r buffer.");
268
269 » arraySize=(stride==0)? width*height:stride*height;
270 » if((*env)->GetArrayLength(env, src)<arraySize)
271 » » _throw("Source buffer is not large enough");
272 » if((*env)->GetArrayLength(env, dst)
273 » » <(jsize)tjBufSizeYUV(width, height, subsamp))
274 » » _throw("Destination buffer is not large enough");
275
276 » bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
277 » bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
278
279 » if(tjEncodeYUV2(handle, srcBuf, width, stride*sizeof(jint), height, pf,
280 » » dstBuf, subsamp, flags)==-1)
281 » {
282 » » (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
283 » » (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
284 » » dstBuf=srcBuf=NULL;
285 » » _throw(tjGetErrorStr());
286 » }
287 526
288 bailout: 527 bailout:
289 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
290 if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
291 return; 528 return;
292 } 529 }
293 530
531 /* TurboJPEG 1.2.x: TJCompressor::destroy() */
294 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy 532 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy
295 (JNIEnv *env, jobject obj) 533 (JNIEnv *env, jobject obj)
296 { 534 {
297 tjhandle handle=0; 535 tjhandle handle=0;
298 536
299 gethandle(); 537 gethandle();
300 538
301 » if(tjDestroy(handle)==-1) _throw(tjGetErrorStr()); 539 » if(tjDestroy(handle)==-1) _throwtj();
302 (*env)->SetLongField(env, obj, _fid, 0); 540 (*env)->SetLongField(env, obj, _fid, 0);
303 541
304 bailout: 542 bailout:
305 return; 543 return;
306 } 544 }
307 545
546 /* TurboJPEG 1.2.x: TJDecompressor::init() */
308 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_init 547 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_init
309 (JNIEnv *env, jobject obj) 548 (JNIEnv *env, jobject obj)
310 { 549 {
311 jclass cls; 550 jclass cls;
312 jfieldID fid; 551 jfieldID fid;
313 tjhandle handle; 552 tjhandle handle;
314 553
315 » if((handle=tjInitDecompress())==NULL) _throw(tjGetErrorStr()); 554 » if((handle=tjInitDecompress())==NULL) _throwtj();
316 555
317 bailif0(cls=(*env)->GetObjectClass(env, obj)); 556 bailif0(cls=(*env)->GetObjectClass(env, obj));
318 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); 557 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J"));
319 » (*env)->SetLongField(env, obj, fid, (jlong)handle); 558 » (*env)->SetLongField(env, obj, fid, (size_t)handle);
320 559
321 bailout: 560 bailout:
322 return; 561 return;
323 } 562 }
324 563
564 /* TurboJPEG 1.2.x: TJDecompressor::getScalingFactors() */
325 JNIEXPORT jobjectArray JNICALL Java_org_libjpegturbo_turbojpeg_TJ_getScalingFact ors 565 JNIEXPORT jobjectArray JNICALL Java_org_libjpegturbo_turbojpeg_TJ_getScalingFact ors
326 (JNIEnv *env, jclass cls) 566 (JNIEnv *env, jclass cls)
327 { 567 {
328 jclass sfcls=NULL; jfieldID fid=0; 568 » jclass sfcls=NULL; jfieldID fid=0;
329 tjscalingfactor *sf=NULL; int n=0, i; 569 tjscalingfactor *sf=NULL; int n=0, i;
330 jobject sfobj=NULL; 570 jobject sfobj=NULL;
331 jobjectArray sfjava=NULL; 571 jobjectArray sfjava=NULL;
332 572
333 if((sf=tjGetScalingFactors(&n))==NULL || n==0) 573 if((sf=tjGetScalingFactors(&n))==NULL || n==0)
334 » » _throw(tjGetErrorStr()); 574 » » _throwarg(tjGetErrorStr());
335 575
336 bailif0(sfcls=(*env)->FindClass(env, "org/libjpegturbo/turbojpeg/TJScali ngFactor")); 576 bailif0(sfcls=(*env)->FindClass(env, "org/libjpegturbo/turbojpeg/TJScali ngFactor"));
337 bailif0(sfjava=(jobjectArray)(*env)->NewObjectArray(env, n, sfcls, 0)); 577 bailif0(sfjava=(jobjectArray)(*env)->NewObjectArray(env, n, sfcls, 0));
338 578
339 for(i=0; i<n; i++) 579 for(i=0; i<n; i++)
340 { 580 {
341 bailif0(sfobj=(*env)->AllocObject(env, sfcls)); 581 bailif0(sfobj=(*env)->AllocObject(env, sfcls));
342 bailif0(fid=(*env)->GetFieldID(env, sfcls, "num", "I")); 582 bailif0(fid=(*env)->GetFieldID(env, sfcls, "num", "I"));
343 (*env)->SetIntField(env, sfobj, fid, sf[i].num); 583 (*env)->SetIntField(env, sfobj, fid, sf[i].num);
344 bailif0(fid=(*env)->GetFieldID(env, sfcls, "denom", "I")); 584 bailif0(fid=(*env)->GetFieldID(env, sfcls, "denom", "I"));
345 (*env)->SetIntField(env, sfobj, fid, sf[i].denom); 585 (*env)->SetIntField(env, sfobj, fid, sf[i].denom);
346 (*env)->SetObjectArrayElement(env, sfjava, i, sfobj); 586 (*env)->SetObjectArrayElement(env, sfjava, i, sfobj);
347 } 587 }
348 588
349 bailout: 589 bailout:
350 return sfjava; 590 return sfjava;
351 } 591 }
352 592
593 /* TurboJPEG 1.2.x: TJDecompressor::decompressHeader() */
353 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress Header 594 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress Header
354 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize) 595 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize)
355 { 596 {
356 tjhandle handle=0; 597 tjhandle handle=0;
357 unsigned char *jpegBuf=NULL; 598 unsigned char *jpegBuf=NULL;
358 » int width=0, height=0, jpegSubsamp=-1; 599 » int width=0, height=0, jpegSubsamp=-1, jpegColorspace=-1;
359 600
360 gethandle(); 601 gethandle();
361 602
362 if((*env)->GetArrayLength(env, src)<jpegSize) 603 if((*env)->GetArrayLength(env, src)<jpegSize)
363 » » _throw("Source buffer is not large enough"); 604 » » _throwarg("Source buffer is not large enough");
364 605
365 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); 606 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
366 607
367 » if(tjDecompressHeader2(handle, jpegBuf, (unsigned long)jpegSize, 608 » if(tjDecompressHeader3(handle, jpegBuf, (unsigned long)jpegSize,
368 » » &width, &height, &jpegSubsamp)==-1) 609 » » &width, &height, &jpegSubsamp, &jpegColorspace)==-1)
369 » { 610 » » _throwtj();
370 » » (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); 611
371 » » _throw(tjGetErrorStr());
372 » }
373 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); jpegBuf=NU LL; 612 (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); jpegBuf=NU LL;
374 613
375 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); 614 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
376 (*env)->SetIntField(env, obj, _fid, jpegSubsamp); 615 (*env)->SetIntField(env, obj, _fid, jpegSubsamp);
616 if((_fid=(*env)->GetFieldID(env, _cls, "jpegColorspace", "I"))==0)
617 (*env)->ExceptionClear(env);
618 else
619 (*env)->SetIntField(env, obj, _fid, jpegColorspace);
377 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); 620 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
378 (*env)->SetIntField(env, obj, _fid, width); 621 (*env)->SetIntField(env, obj, _fid, width);
379 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); 622 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
380 (*env)->SetIntField(env, obj, _fid, height); 623 (*env)->SetIntField(env, obj, _fid, height);
381 624
382 bailout: 625 bailout:
626 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
383 return; 627 return;
384 } 628 }
385 629
386 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress ___3BI_3BIIIIIII 630 static void TJDecompressor_decompress
387 » (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst , 631 » (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jarray dst,
388 » » jint x, jint y, jint width, jint pitch, jint height, jint pf, ji nt flags) 632 » » jint dstElementSize, jint x, jint y, jint width, jint pitch, jin t height,
633 » » jint pf, jint flags)
389 { 634 {
390 tjhandle handle=0; 635 tjhandle handle=0;
391 jsize arraySize=0, actualPitch; 636 jsize arraySize=0, actualPitch;
392 unsigned char *jpegBuf=NULL, *dstBuf=NULL; 637 unsigned char *jpegBuf=NULL, *dstBuf=NULL;
393 638
394 gethandle(); 639 gethandle();
395 640
396 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) 641 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
397 » » _throw("Invalid argument in decompress()"); 642 » » _throwarg("Invalid argument in decompress()");
398 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) 643 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
399 » » _throw("Mismatch between Java and C API"); 644 » » _throwarg("Mismatch between Java and C API");
400 645
401 if((*env)->GetArrayLength(env, src)<jpegSize) 646 if((*env)->GetArrayLength(env, src)<jpegSize)
402 » » _throw("Source buffer is not large enough"); 647 » » _throwarg("Source buffer is not large enough");
403 actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch; 648 actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
404 arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf]; 649 arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf];
405 » if((*env)->GetArrayLength(env, dst)<arraySize) 650 » if((*env)->GetArrayLength(env, dst)*dstElementSize<arraySize)
406 » » _throw("Destination buffer is not large enough"); 651 » » _throwarg("Destination buffer is not large enough");
407 652
408 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); 653 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
409 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); 654 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
410 655
411 if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize, 656 if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize,
412 &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height , pf, 657 &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height , pf,
413 flags)==-1) 658 flags)==-1)
414 » { 659 » » _throwtj();
415 » » (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
416 » » (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
417 » » dstBuf=jpegBuf=NULL;
418 » » _throw(tjGetErrorStr());
419 » }
420 660
421 bailout: 661 bailout:
422 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); 662 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
423 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); 663 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
424 return; 664 return;
425 } 665 }
426 666
667 /* TurboJPEG 1.3.x: TJDecompressor::decompress() byte destination */
668 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress ___3BI_3BIIIIIII
669 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst ,
670 jint x, jint y, jint width, jint pitch, jint height, jint pf, ji nt flags)
671 {
672 TJDecompressor_decompress(env, obj, src, jpegSize, dst, 1, x, y, width,
673 pitch, height, pf, flags);
674 }
675
676 /* TurboJPEG 1.2.x: TJDecompressor::decompress() byte destination */
427 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress ___3BI_3BIIIII 677 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress ___3BI_3BIIIII
428 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst , 678 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst ,
429 jint width, jint pitch, jint height, jint pf, jint flags) 679 jint width, jint pitch, jint height, jint pf, jint flags)
430 { 680 {
431 » Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIIII I 681 » TJDecompressor_decompress(env, obj, src, jpegSize, dst, 1, 0, 0, width,
432 » » (env, obj, src, jpegSize, dst, 0, 0, width, pitch, height, pf, f lags); 682 » » pitch, height, pf, flags);
433 } 683 }
434 684
685 /* TurboJPEG 1.3.x: TJDecompressor::decompress() int destination */
435 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress ___3BI_3IIIIIIII 686 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress ___3BI_3IIIIIIII
436 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst, 687 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst,
437 jint x, jint y, jint width, jint stride, jint height, jint pf, j int flags) 688 jint x, jint y, jint width, jint stride, jint height, jint pf, j int flags)
438 { 689 {
439 » tjhandle handle=0; 690 » if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
440 » jsize arraySize=0, actualStride; 691 » » _throwarg("Invalid argument in decompress()");
441 » unsigned char *jpegBuf=NULL, *dstBuf=NULL; 692 » if(tjPixelSize[pf]!=sizeof(jint))
693 » » _throwarg("Pixel format must be 32-bit when decompressing to an integer buffer.");
442 694
443 » gethandle(); 695 » TJDecompressor_decompress(env, obj, src, jpegSize, dst, sizeof(jint), x, y,
444 696 » » width, stride*sizeof(jint), height, pf, flags);
445 » if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
446 » » _throw("Invalid argument in decompress()");
447 » if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
448 » » _throw("Mismatch between Java and C API");
449 » if(tjPixelSize[pf]!=sizeof(jint))
450 » » _throw("Pixel format must be 32-bit when decompressing to an int eger buffer.");
451
452 » if((*env)->GetArrayLength(env, src)<jpegSize)
453 » » _throw("Source buffer is not large enough");
454 » actualStride=(stride==0)? width:stride;
455 » arraySize=(y+height-1)*actualStride + x+width;
456 » if((*env)->GetArrayLength(env, dst)<arraySize)
457 » » _throw("Destination buffer is not large enough");
458
459 » bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
460 » bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
461
462 » if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize,
463 » » &dstBuf[(y*actualStride + x)*sizeof(int)], width, stride*sizeof( jint),
464 » » height, pf, flags)==-1)
465 » {
466 » » (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
467 » » (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
468 » » dstBuf=jpegBuf=NULL;
469 » » _throw(tjGetErrorStr());
470 » }
471 697
472 bailout: 698 bailout:
473 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
474 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
475 return; 699 return;
476 } 700 }
477 701
702 /* TurboJPEG 1.2.x: TJDecompressor::decompress() int destination */
478 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress ___3BI_3IIIIII 703 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress ___3BI_3IIIIII
479 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst, 704 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst,
480 jint width, jint stride, jint height, jint pf, jint flags) 705 jint width, jint stride, jint height, jint pf, jint flags)
481 { 706 {
482 » Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIIII I 707 » if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
483 » » (env, obj, src, jpegSize, dst, 0, 0, width, stride, height, pf, flags); 708 » » _throwarg("Invalid argument in decompress()");
484 » 709 » if(tjPixelSize[pf]!=sizeof(jint))
710 » » _throwarg("Pixel format must be 32-bit when decompressing to an integer buffer.");
711
712 » TJDecompressor_decompress(env, obj, src, jpegSize, dst, sizeof(jint), 0, 0,
713 » » width, stride*sizeof(jint), height, pf, flags);
714
715 » bailout:
716 » return;
717
485 } 718 }
486 719
487 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress ToYUV 720 /* TurboJPEG 1.4.x: TJDecompressor::decompressToYUV() */
721 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress ToYUV___3BI_3_3B_3II_3III
722 » (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize,
723 » » jobjectArray dstobjs, jintArray jDstOffsets, jint desiredWidth,
724 » » jintArray jDstStrides, jint desiredHeight, jint flags)
725 {
726 » tjhandle handle=0;
727 » jbyteArray jDstPlanes[3]={NULL, NULL, NULL};
728 » unsigned char *jpegBuf=NULL, *dstPlanes[3];
729 » int *dstOffsets=NULL, *dstStrides=NULL;
730 » int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0;
731 » int nc=0, i, width, height, scaledWidth, scaledHeight, nsf=0;
732 » tjscalingfactor *sf;
733
734
735 » gethandle();
736
737 » if((*env)->GetArrayLength(env, src)<jpegSize)
738 » » _throwarg("Source buffer is not large enough");
739 » bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
740 » jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
741 » bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
742 » jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
743 » bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
744 » jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
745
746 » nc=(jpegSubsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3);
747
748 » width=desiredWidth; height=desiredHeight;
749 » if(width==0) width=jpegWidth;
750 » if(height==0) height=jpegHeight;
751 » sf=tjGetScalingFactors(&nsf);
752 » if(!sf || nsf<1)
753 » » _throwarg(tjGetErrorStr());
754 » for(i=0; i<nsf; i++)
755 » {
756 » » scaledWidth=TJSCALED(jpegWidth, sf[i]);
757 » » scaledHeight=TJSCALED(jpegHeight, sf[i]);
758 » » if(scaledWidth<=width && scaledHeight<=height)
759 » » » break;
760 » }
761 » if(i>=nsf)
762 » » _throwarg("Could not scale down to desired image dimensions");
763
764 » bailif0(dstOffsets=(*env)->GetPrimitiveArrayCritical(env, jDstOffsets, 0 ));
765 » bailif0(dstStrides=(*env)->GetPrimitiveArrayCritical(env, jDstStrides, 0 ));
766 » for(i=0; i<nc; i++)
767 » {
768 » » int planeSize=tjPlaneSizeYUV(i, scaledWidth, dstStrides[i], scal edHeight,
769 » » » jpegSubsamp);
770 » » int pw=tjPlaneWidth(i, scaledWidth, jpegSubsamp);
771
772 » » if(planeSize<0 || pw<0)
773 » » » _throwarg(tjGetErrorStr());
774
775 » » if(dstOffsets[i]<0)
776 » » » _throwarg("Invalid argument in decompressToYUV()");
777 » » if(dstStrides[i]<0 && dstOffsets[i]-planeSize+pw<0)
778 » » » _throwarg("Negative plane stride would cause memory to b e accessed below plane boundary");
779
780 » » bailif0(jDstPlanes[i]=(*env)->GetObjectArrayElement(env, dstobjs , i));
781 » » if((*env)->GetArrayLength(env, jDstPlanes[i])<dstOffsets[i]+plan eSize)
782 » » » _throwarg("Destination plane is not large enough");
783
784 » » bailif0(dstPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jDst Planes[i],
785 » » » 0));
786 » » dstPlanes[i]=&dstPlanes[i][dstOffsets[i]];
787 » }
788 » bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
789
790 » if(tjDecompressToYUVPlanes(handle, jpegBuf, (unsigned long)jpegSize,
791 » » dstPlanes, desiredWidth, dstStrides, desiredHeight, flags)==-1)
792 » » _throwtj();
793
794 » bailout:
795 » if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
796 » for(i=0; i<nc; i++)
797 » {
798 » » if(dstPlanes[i] && jDstPlanes[i])
799 » » » (*env)->ReleasePrimitiveArrayCritical(env, jDstPlanes[i] , dstPlanes[i],
800 » » » » 0);
801 » }
802 » if(dstStrides)
803 » » (*env)->ReleasePrimitiveArrayCritical(env, jDstStrides, dstStrid es, 0);
804 » if(dstOffsets)
805 » » (*env)->ReleasePrimitiveArrayCritical(env, jDstOffsets, dstOffse ts, 0);
806 » return;
807 }
808
809 /* TurboJPEG 1.2.x: TJDecompressor::decompressToYUV() */
810 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress ToYUV___3BI_3BI
488 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst , 811 (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst ,
489 jint flags) 812 jint flags)
490 { 813 {
491 tjhandle handle=0; 814 tjhandle handle=0;
492 unsigned char *jpegBuf=NULL, *dstBuf=NULL; 815 unsigned char *jpegBuf=NULL, *dstBuf=NULL;
493 int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0; 816 int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0;
494 817
495 gethandle(); 818 gethandle();
496 819
497 if((*env)->GetArrayLength(env, src)<jpegSize) 820 if((*env)->GetArrayLength(env, src)<jpegSize)
498 » » _throw("Source buffer is not large enough"); 821 » » _throwarg("Source buffer is not large enough");
499 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); 822 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
500 jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); 823 jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
501 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); 824 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
502 jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); 825 jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
503 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); 826 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
504 jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); 827 jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
505 if((*env)->GetArrayLength(env, dst) 828 if((*env)->GetArrayLength(env, dst)
506 <(jsize)tjBufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp)) 829 <(jsize)tjBufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp))
507 » » _throw("Destination buffer is not large enough"); 830 » » _throwarg("Destination buffer is not large enough");
508 831
509 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); 832 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
510 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); 833 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
511 834
512 if(tjDecompressToYUV(handle, jpegBuf, (unsigned long)jpegSize, dstBuf, 835 if(tjDecompressToYUV(handle, jpegBuf, (unsigned long)jpegSize, dstBuf,
513 flags)==-1) 836 flags)==-1)
514 » { 837 » » _throwtj();
515 » » (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
516 » » (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
517 » » dstBuf=jpegBuf=NULL;
518 » » _throw(tjGetErrorStr());
519 » }
520 838
521 bailout: 839 bailout:
522 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); 840 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
523 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); 841 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
524 return; 842 return;
525 } 843 }
526 844
845 static void TJDecompressor_decodeYUV
846 (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
847 jintArray jSrcStrides, jint subsamp, jarray dst, jint dstElement Size,
848 jint x, jint y, jint width, jint pitch, jint height, jint pf, ji nt flags)
849 {
850 tjhandle handle=0;
851 jsize arraySize=0, actualPitch;
852 jbyteArray jSrcPlanes[3]={NULL, NULL, NULL};
853 const unsigned char *srcPlanes[3];
854 unsigned char *dstBuf=NULL;
855 int *srcOffsets=NULL, *srcStrides=NULL;
856 int nc=(subsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3), i;
857
858 gethandle();
859
860 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || subsamp<0
861 || subsamp>=org_libjpegturbo_turbojpeg_TJ_NUMSAMP)
862 _throwarg("Invalid argument in decodeYUV()");
863 if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF
864 || org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP)
865 _throwarg("Mismatch between Java and C API");
866
867 if((*env)->GetArrayLength(env, srcobjs)<nc)
868 _throwarg("Planes array is too small for the subsampling type");
869 if((*env)->GetArrayLength(env, jSrcOffsets)<nc)
870 _throwarg("Offsets array is too small for the subsampling type") ;
871 if((*env)->GetArrayLength(env, jSrcStrides)<nc)
872 _throwarg("Strides array is too small for the subsampling type") ;
873
874 actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
875 arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf];
876 if((*env)->GetArrayLength(env, dst)*dstElementSize<arraySize)
877 _throwarg("Destination buffer is not large enough");
878
879 bailif0(srcOffsets=(*env)->GetPrimitiveArrayCritical(env, jSrcOffsets, 0 ));
880 bailif0(srcStrides=(*env)->GetPrimitiveArrayCritical(env, jSrcStrides, 0 ));
881 for(i=0; i<nc; i++)
882 {
883 int planeSize=tjPlaneSizeYUV(i, width, srcStrides[i], height, su bsamp);
884 int pw=tjPlaneWidth(i, width, subsamp);
885
886 if(planeSize<0 || pw<0)
887 _throwarg(tjGetErrorStr());
888
889 if(srcOffsets[i]<0)
890 _throwarg("Invalid argument in decodeYUV()");
891 if(srcStrides[i]<0 && srcOffsets[i]-planeSize+pw<0)
892 _throwarg("Negative plane stride would cause memory to b e accessed below plane boundary");
893
894 bailif0(jSrcPlanes[i]=(*env)->GetObjectArrayElement(env, srcobjs , i));
895 if((*env)->GetArrayLength(env, jSrcPlanes[i])<srcOffsets[i]+plan eSize)
896 _throwarg("Source plane is not large enough");
897
898 bailif0(srcPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jSrc Planes[i],
899 0));
900 srcPlanes[i]=&srcPlanes[i][srcOffsets[i]];
901 }
902 bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
903
904 if(tjDecodeYUVPlanes(handle, srcPlanes, srcStrides, subsamp,
905 &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height , pf,
906 flags)==-1)
907 _throwtj();
908
909 bailout:
910 if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
911 for(i=0; i<nc; i++)
912 {
913 if(srcPlanes[i] && jSrcPlanes[i])
914 (*env)->ReleasePrimitiveArrayCritical(env, jSrcPlanes[i] ,
915 (unsigned char *)srcPlanes[i], 0);
916 }
917 if(srcStrides)
918 (*env)->ReleasePrimitiveArrayCritical(env, jSrcStrides, srcStrid es, 0);
919 if(srcOffsets)
920 (*env)->ReleasePrimitiveArrayCritical(env, jSrcOffsets, srcOffse ts, 0);
921 return;
922 }
923
924 /* TurboJPEG 1.4.x: TJDecompressor::decodeYUV() byte destination */
925 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decodeYUV_ __3_3B_3I_3II_3BIIIIIII
926 (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
927 jintArray jSrcStrides, jint subsamp, jbyteArray dst, jint x, jin t y,
928 jint width, jint pitch, jint height, jint pf, jint flags)
929 {
930 TJDecompressor_decodeYUV(env, obj, srcobjs, jSrcOffsets, jSrcStrides,
931 subsamp, dst, 1, x, y, width, pitch, height, pf, flags);
932 }
933
934 /* TurboJPEG 1.4.x: TJDecompressor::decodeYUV() int destination */
935 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decodeYUV_ __3_3B_3I_3II_3IIIIIIII
936 (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
937 jintArray jSrcStrides, jint subsamp, jintArray dst, jint x, jint y,
938 jint width, jint stride, jint height, jint pf, jint flags)
939 {
940 if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
941 _throwarg("Invalid argument in decodeYUV()");
942 if(tjPixelSize[pf]!=sizeof(jint))
943 _throwarg("Pixel format must be 32-bit when decoding to an integ er buffer.");
944
945 TJDecompressor_decodeYUV(env, obj, srcobjs, jSrcOffsets, jSrcStrides,
946 subsamp, dst, sizeof(jint), x, y, width, stride*sizeof(jint), he ight, pf,
947 flags);
948
949 bailout:
950 return;
951 }
952
953 /* TurboJPEG 1.2.x: TJTransformer::init() */
527 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_init 954 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_init
528 (JNIEnv *env, jobject obj) 955 (JNIEnv *env, jobject obj)
529 { 956 {
530 jclass cls; 957 jclass cls;
531 jfieldID fid; 958 jfieldID fid;
532 tjhandle handle; 959 tjhandle handle;
533 960
534 » if((handle=tjInitTransform())==NULL) _throw(tjGetErrorStr()); 961 » if((handle=tjInitTransform())==NULL) _throwtj();
535 962
536 bailif0(cls=(*env)->GetObjectClass(env, obj)); 963 bailif0(cls=(*env)->GetObjectClass(env, obj));
537 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); 964 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J"));
538 » (*env)->SetLongField(env, obj, fid, (jlong)handle); 965 » (*env)->SetLongField(env, obj, fid, (size_t)handle);
539 966
540 bailout: 967 bailout:
541 return; 968 return;
542 } 969 }
543 970
544 typedef struct _JNICustomFilterParams 971 typedef struct _JNICustomFilterParams
545 { 972 {
546 JNIEnv *env; 973 JNIEnv *env;
547 jobject tobj; 974 jobject tobj;
548 jobject cfobj; 975 jobject cfobj;
549 } JNICustomFilterParams; 976 } JNICustomFilterParams;
550 977
551 static int JNICustomFilter(short *coeffs, tjregion arrayRegion, 978 static int JNICustomFilter(short *coeffs, tjregion arrayRegion,
552 tjregion planeRegion, int componentIndex, int transformIndex, 979 tjregion planeRegion, int componentIndex, int transformIndex,
553 tjtransform *transform) 980 tjtransform *transform)
554 { 981 {
555 JNICustomFilterParams *params=(JNICustomFilterParams *)transform->data; 982 JNICustomFilterParams *params=(JNICustomFilterParams *)transform->data;
556 JNIEnv *env=params->env; 983 JNIEnv *env=params->env;
557 jobject tobj=params->tobj, cfobj=params->cfobj; 984 jobject tobj=params->tobj, cfobj=params->cfobj;
558 jobject arrayRegionObj, planeRegionObj, bufobj, borobj; 985 » jobject arrayRegionObj, planeRegionObj, bufobj, borobj;
559 jclass cls; jmethodID mid; jfieldID fid; 986 jclass cls; jmethodID mid; jfieldID fid;
560 987
561 bailif0(bufobj=(*env)->NewDirectByteBuffer(env, coeffs, 988 bailif0(bufobj=(*env)->NewDirectByteBuffer(env, coeffs,
562 sizeof(short)*arrayRegion.w*arrayRegion.h)); 989 sizeof(short)*arrayRegion.w*arrayRegion.h));
563 bailif0(cls=(*env)->FindClass(env, "java/nio/ByteOrder")); 990 bailif0(cls=(*env)->FindClass(env, "java/nio/ByteOrder"));
564 bailif0(mid=(*env)->GetStaticMethodID(env, cls, "nativeOrder", 991 » bailif0(mid=(*env)->GetStaticMethodID(env, cls, "nativeOrder",
565 "()Ljava/nio/ByteOrder;")); 992 "()Ljava/nio/ByteOrder;"));
566 bailif0(borobj=(*env)->CallStaticObjectMethod(env, cls, mid)); 993 bailif0(borobj=(*env)->CallStaticObjectMethod(env, cls, mid));
567 bailif0(cls=(*env)->GetObjectClass(env, bufobj)); 994 bailif0(cls=(*env)->GetObjectClass(env, bufobj));
568 bailif0(mid=(*env)->GetMethodID(env, cls, "order", 995 bailif0(mid=(*env)->GetMethodID(env, cls, "order",
569 "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;")); 996 "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;"));
570 (*env)->CallObjectMethod(env, bufobj, mid, borobj); 997 (*env)->CallObjectMethod(env, bufobj, mid, borobj);
571 bailif0(mid=(*env)->GetMethodID(env, cls, "asShortBuffer", 998 » bailif0(mid=(*env)->GetMethodID(env, cls, "asShortBuffer",
572 "()Ljava/nio/ShortBuffer;")); 999 "()Ljava/nio/ShortBuffer;"));
573 bailif0(bufobj=(*env)->CallObjectMethod(env, bufobj, mid)); 1000 bailif0(bufobj=(*env)->CallObjectMethod(env, bufobj, mid));
574 1001
575 bailif0(cls=(*env)->FindClass(env, "java/awt/Rectangle")); 1002 bailif0(cls=(*env)->FindClass(env, "java/awt/Rectangle"));
576 bailif0(arrayRegionObj=(*env)->AllocObject(env, cls)); 1003 bailif0(arrayRegionObj=(*env)->AllocObject(env, cls));
577 bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I")); 1004 bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I"));
578 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.x); 1005 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.x);
579 bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I")); 1006 bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I"));
580 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.y); 1007 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.y);
581 bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I")); 1008 bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I"));
(...skipping 16 matching lines...) Expand all
598 "(Ljava/nio/ShortBuffer;Ljava/awt/Rectangle;Ljava/awt/Rectangle; IILorg/libjpegturbo/turbojpeg/TJTransform;)V")); 1025 "(Ljava/nio/ShortBuffer;Ljava/awt/Rectangle;Ljava/awt/Rectangle; IILorg/libjpegturbo/turbojpeg/TJTransform;)V"));
599 (*env)->CallVoidMethod(env, cfobj, mid, bufobj, arrayRegionObj, 1026 (*env)->CallVoidMethod(env, cfobj, mid, bufobj, arrayRegionObj,
600 planeRegionObj, componentIndex, transformIndex, tobj); 1027 planeRegionObj, componentIndex, transformIndex, tobj);
601 1028
602 return 0; 1029 return 0;
603 1030
604 bailout: 1031 bailout:
605 return -1; 1032 return -1;
606 } 1033 }
607 1034
1035 /* TurboJPEG 1.2.x: TJTransformer::transform() */
608 JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transf orm 1036 JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transf orm
609 (JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize, 1037 (JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize,
610 jobjectArray dstobjs, jobjectArray tobjs, jint flags) 1038 jobjectArray dstobjs, jobjectArray tobjs, jint flags)
611 { 1039 {
612 tjhandle handle=0; int i; 1040 tjhandle handle=0; int i;
613 unsigned char *jpegBuf=NULL, **dstBufs=NULL; jsize n=0; 1041 unsigned char *jpegBuf=NULL, **dstBufs=NULL; jsize n=0;
614 unsigned long *dstSizes=NULL; tjtransform *t=NULL; 1042 unsigned long *dstSizes=NULL; tjtransform *t=NULL;
615 jbyteArray *jdstBufs=NULL; 1043 jbyteArray *jdstBufs=NULL;
616 int jpegWidth=0, jpegHeight=0, jpegSubsamp; 1044 int jpegWidth=0, jpegHeight=0, jpegSubsamp;
617 jintArray jdstSizes=0; jint *dstSizesi=NULL; 1045 jintArray jdstSizes=0; jint *dstSizesi=NULL;
618 JNICustomFilterParams *params=NULL; 1046 JNICustomFilterParams *params=NULL;
619 1047
620 gethandle(); 1048 gethandle();
621 1049
622 if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize) 1050 if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize)
623 » » _throw("Source buffer is not large enough"); 1051 » » _throwarg("Source buffer is not large enough");
624 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); 1052 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
625 jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); 1053 jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
626 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); 1054 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
627 jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); 1055 jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
628 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); 1056 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
629 jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); 1057 jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
630 1058
631 n=(*env)->GetArrayLength(env, dstobjs); 1059 n=(*env)->GetArrayLength(env, dstobjs);
632 if(n!=(*env)->GetArrayLength(env, tobjs)) 1060 if(n!=(*env)->GetArrayLength(env, tobjs))
633 » » _throw("Mismatch between size of transforms array and destinatio n buffers array"); 1061 » » _throwarg("Mismatch between size of transforms array and destina tion buffers array");
634 1062
635 if((dstBufs=(unsigned char **)malloc(sizeof(unsigned char *)*n))==NULL) 1063 if((dstBufs=(unsigned char **)malloc(sizeof(unsigned char *)*n))==NULL)
636 » » _throw("Memory allocation failure"); 1064 » » _throwmem();
637 if((jdstBufs=(jbyteArray *)malloc(sizeof(jbyteArray)*n))==NULL) 1065 if((jdstBufs=(jbyteArray *)malloc(sizeof(jbyteArray)*n))==NULL)
638 » » _throw("Memory allocation failure"); 1066 » » _throwmem();
639 if((dstSizes=(unsigned long *)malloc(sizeof(unsigned long)*n))==NULL) 1067 if((dstSizes=(unsigned long *)malloc(sizeof(unsigned long)*n))==NULL)
640 » » _throw("Memory allocation failure"); 1068 » » _throwmem();
641 if((t=(tjtransform *)malloc(sizeof(tjtransform)*n))==NULL) 1069 if((t=(tjtransform *)malloc(sizeof(tjtransform)*n))==NULL)
642 » » _throw("Memory allocation failure"); 1070 » » _throwmem();
643 if((params=(JNICustomFilterParams *)malloc(sizeof(JNICustomFilterParams) *n)) 1071 if((params=(JNICustomFilterParams *)malloc(sizeof(JNICustomFilterParams) *n))
644 ==NULL) 1072 ==NULL)
645 » » _throw("Memory allocation failure"); 1073 » » _throwmem();
646 for(i=0; i<n; i++) 1074 for(i=0; i<n; i++)
647 { 1075 {
648 dstBufs[i]=NULL; jdstBufs[i]=NULL; dstSizes[i]=0; 1076 dstBufs[i]=NULL; jdstBufs[i]=NULL; dstSizes[i]=0;
649 memset(&t[i], 0, sizeof(tjtransform)); 1077 memset(&t[i], 0, sizeof(tjtransform));
650 memset(&params[i], 0, sizeof(JNICustomFilterParams)); 1078 memset(&params[i], 0, sizeof(JNICustomFilterParams));
651 } 1079 }
652 1080
653 for(i=0; i<n; i++) 1081 for(i=0; i<n; i++)
654 { 1082 {
655 jobject tobj, cfobj; 1083 jobject tobj, cfobj;
(...skipping 19 matching lines...) Expand all
675 if(cfobj) 1103 if(cfobj)
676 { 1104 {
677 params[i].env=env; 1105 params[i].env=env;
678 params[i].tobj=tobj; 1106 params[i].tobj=tobj;
679 params[i].cfobj=cfobj; 1107 params[i].cfobj=cfobj;
680 t[i].customFilter=JNICustomFilter; 1108 t[i].customFilter=JNICustomFilter;
681 t[i].data=(void *)&params[i]; 1109 t[i].data=(void *)&params[i];
682 } 1110 }
683 } 1111 }
684 1112
685 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0));
686 for(i=0; i<n; i++) 1113 for(i=0; i<n; i++)
687 { 1114 {
688 int w=jpegWidth, h=jpegHeight; 1115 int w=jpegWidth, h=jpegHeight;
689 if(t[i].r.w!=0) w=t[i].r.w; 1116 if(t[i].r.w!=0) w=t[i].r.w;
690 if(t[i].r.h!=0) h=t[i].r.h; 1117 if(t[i].r.h!=0) h=t[i].r.h;
691 bailif0(jdstBufs[i]=(*env)->GetObjectArrayElement(env, dstobjs, i)); 1118 bailif0(jdstBufs[i]=(*env)->GetObjectArrayElement(env, dstobjs, i));
692 if((unsigned long)(*env)->GetArrayLength(env, jdstBufs[i]) 1119 if((unsigned long)(*env)->GetArrayLength(env, jdstBufs[i])
693 <tjBufSize(w, h, jpegSubsamp)) 1120 <tjBufSize(w, h, jpegSubsamp))
694 » » » _throw("Destination buffer is not large enough"); 1121 » » » _throwarg("Destination buffer is not large enough");
1122 » }
1123 » bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0));
1124 » for(i=0; i<n; i++)
695 bailif0(dstBufs[i]=(*env)->GetPrimitiveArrayCritical(env, jdstBu fs[i], 0)); 1125 bailif0(dstBufs[i]=(*env)->GetPrimitiveArrayCritical(env, jdstBu fs[i], 0));
696 }
697 1126
698 if(tjTransform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t, 1127 if(tjTransform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t,
699 flags|TJFLAG_NOREALLOC)==-1) 1128 flags|TJFLAG_NOREALLOC)==-1)
1129 _throwtj();
1130
1131 for(i=0; i<n; i++)
700 { 1132 {
701 » » (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0); 1133 » » (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[ i], 0);
702 » » jpegBuf=NULL; 1134 » » dstBufs[i]=NULL;
703 » » for(i=0; i<n; i++)
704 » » {
705 » » » (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0);
706 » » » dstBufs[i]=NULL;
707 » » }
708 » » _throw(tjGetErrorStr());
709 } 1135 }
1136 (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0);
1137 jpegBuf=NULL;
710 1138
711 jdstSizes=(*env)->NewIntArray(env, n); 1139 jdstSizes=(*env)->NewIntArray(env, n);
712 bailif0(dstSizesi=(*env)->GetIntArrayElements(env, jdstSizes, 0)); 1140 bailif0(dstSizesi=(*env)->GetIntArrayElements(env, jdstSizes, 0));
713 for(i=0; i<n; i++) dstSizesi[i]=(int)dstSizes[i]; 1141 for(i=0; i<n; i++) dstSizesi[i]=(int)dstSizes[i];
714 1142
715 bailout: 1143 bailout:
716 » if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0); 1144 » if(dstSizesi) (*env)->ReleaseIntArrayElements(env, jdstSizes, dstSizesi, 0);
717 if(dstBufs) 1145 if(dstBufs)
718 { 1146 {
719 for(i=0; i<n; i++) 1147 for(i=0; i<n; i++)
720 { 1148 {
721 if(dstBufs[i] && jdstBufs && jdstBufs[i]) 1149 if(dstBufs[i] && jdstBufs && jdstBufs[i])
722 (*env)->ReleasePrimitiveArrayCritical(env, jdstB ufs[i], dstBufs[i], 0); 1150 (*env)->ReleasePrimitiveArrayCritical(env, jdstB ufs[i], dstBufs[i], 0);
723 } 1151 }
724 free(dstBufs); 1152 free(dstBufs);
725 } 1153 }
1154 if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0);
726 if(jdstBufs) free(jdstBufs); 1155 if(jdstBufs) free(jdstBufs);
727 if(dstSizes) free(dstSizes); 1156 if(dstSizes) free(dstSizes);
728 if(dstSizesi) (*env)->ReleaseIntArrayElements(env, jdstSizes, dstSizesi, 0);
729 if(t) free(t); 1157 if(t) free(t);
730 return jdstSizes; 1158 return jdstSizes;
731 } 1159 }
732 1160
1161 /* TurboJPEG 1.2.x: TJDecompressor::destroy() */
733 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_destroy 1162 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_destroy
734 (JNIEnv *env, jobject obj) 1163 (JNIEnv *env, jobject obj)
735 { 1164 {
736 Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy(env, obj); 1165 Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy(env, obj);
737 } 1166 }
OLDNEW
« simd/jccolext-sse2-64.asm ('K') | « turbojpeg.c ('k') | turbojpeg-mapfile » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698