turbojpeg-jni.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /*
  2. * Copyright (C)2011-2016 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. #include <stdlib.h>
  29. #include <string.h>
  30. #include "turbojpeg.h"
  31. #ifdef WIN32
  32. #include "tjutil.h"
  33. #endif
  34. #include <jni.h>
  35. #include "java/org_libjpegturbo_turbojpeg_TJCompressor.h"
  36. #include "java/org_libjpegturbo_turbojpeg_TJDecompressor.h"
  37. #include "java/org_libjpegturbo_turbojpeg_TJ.h"
  38. #define PAD(v, p) ((v+(p)-1)&(~((p)-1)))
  39. #define _throw(msg, exceptionClass) { \
  40. jclass _exccls=(*env)->FindClass(env, exceptionClass); \
  41. if(!_exccls || (*env)->ExceptionCheck(env)) goto bailout; \
  42. (*env)->ThrowNew(env, _exccls, msg); \
  43. goto bailout; \
  44. }
  45. #define _throwtj() _throw(tjGetErrorStr(), "org/libjpegturbo/turbojpeg/TJException")
  46. #define _throwarg(msg) _throw(msg, "java/lang/IllegalArgumentException")
  47. #define _throwmem() _throw("Memory allocation failure", "java/lang/OutOfMemoryError");
  48. #define bailif0(f) {if(!(f) || (*env)->ExceptionCheck(env)) { \
  49. goto bailout; \
  50. }}
  51. #define gethandle() \
  52. jclass _cls=(*env)->GetObjectClass(env, obj); \
  53. jfieldID _fid; \
  54. if(!_cls || (*env)->ExceptionCheck(env)) goto bailout; \
  55. bailif0(_fid=(*env)->GetFieldID(env, _cls, "handle", "J")); \
  56. handle=(tjhandle)(size_t)(*env)->GetLongField(env, obj, _fid); \
  57. #ifdef _WIN32
  58. #define setenv(envvar, value, dummy) _putenv_s(envvar, value)
  59. #endif
  60. #define prop2env(property, envvar) \
  61. { \
  62. if((jName=(*env)->NewStringUTF(env, property))!=NULL \
  63. && (jValue=(*env)->CallStaticObjectMethod(env, cls, mid, jName))!=NULL) \
  64. { \
  65. if((value=(*env)->GetStringUTFChars(env, jValue, 0))!=NULL) \
  66. { \
  67. setenv(envvar, value, 1); \
  68. (*env)->ReleaseStringUTFChars(env, jValue, value); \
  69. } \
  70. } \
  71. }
  72. int ProcessSystemProperties(JNIEnv *env)
  73. {
  74. jclass cls; jmethodID mid;
  75. jstring jName, jValue;
  76. const char *value;
  77. bailif0(cls=(*env)->FindClass(env, "java/lang/System"));
  78. bailif0(mid=(*env)->GetStaticMethodID(env, cls, "getProperty",
  79. "(Ljava/lang/String;)Ljava/lang/String;"));
  80. prop2env("turbojpeg.optimize", "TJ_OPTIMIZE");
  81. prop2env("turbojpeg.arithmetic", "TJ_ARITHMETIC");
  82. prop2env("turbojpeg.restart", "TJ_RESTART");
  83. prop2env("turbojpeg.progressive", "TJ_PROGRESSIVE");
  84. return 0;
  85. bailout:
  86. return -1;
  87. }
  88. /* TurboJPEG 1.2.x: TJ::bufSize() */
  89. JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize
  90. (JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp)
  91. {
  92. jint retval=(jint)tjBufSize(width, height, jpegSubsamp);
  93. if(retval==-1) _throwarg(tjGetErrorStr());
  94. bailout:
  95. return retval;
  96. }
  97. /* TurboJPEG 1.4.x: TJ::bufSizeYUV() */
  98. JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII
  99. (JNIEnv *env, jclass cls, jint width, jint pad, jint height, jint subsamp)
  100. {
  101. jint retval=(jint)tjBufSizeYUV2(width, pad, height, subsamp);
  102. if(retval==-1) _throwarg(tjGetErrorStr());
  103. bailout:
  104. return retval;
  105. }
  106. /* TurboJPEG 1.2.x: TJ::bufSizeYUV() */
  107. JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__III
  108. (JNIEnv *env, jclass cls, jint width, jint height, jint subsamp)
  109. {
  110. return Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII(env, cls, width,
  111. 4, height, subsamp);
  112. }
  113. /* TurboJPEG 1.4.x: TJ::planeSizeYUV() */
  114. JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeSizeYUV__IIIII
  115. (JNIEnv *env, jclass cls, jint componentID, jint width, jint stride,
  116. jint height, jint subsamp)
  117. {
  118. jint retval=(jint)tjPlaneSizeYUV(componentID, width, stride, height,
  119. subsamp);
  120. if(retval==-1) _throwarg(tjGetErrorStr());
  121. bailout:
  122. return retval;
  123. }
  124. /* TurboJPEG 1.4.x: TJ::planeWidth() */
  125. JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeWidth__III
  126. (JNIEnv *env, jclass cls, jint componentID, jint width, jint subsamp)
  127. {
  128. jint retval=(jint)tjPlaneWidth(componentID, width, subsamp);
  129. if(retval==-1) _throwarg(tjGetErrorStr());
  130. bailout:
  131. return retval;
  132. }
  133. /* TurboJPEG 1.4.x: TJ::planeHeight() */
  134. JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeHeight__III
  135. (JNIEnv *env, jclass cls, jint componentID, jint height, jint subsamp)
  136. {
  137. jint retval=(jint)tjPlaneHeight(componentID, height, subsamp);
  138. if(retval==-1) _throwarg(tjGetErrorStr());
  139. bailout:
  140. return retval;
  141. }
  142. /* TurboJPEG 1.2.x: TJCompressor::init() */
  143. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_init
  144. (JNIEnv *env, jobject obj)
  145. {
  146. jclass cls;
  147. jfieldID fid;
  148. tjhandle handle;
  149. if((handle=tjInitCompress())==NULL)
  150. _throwtj();
  151. bailif0(cls=(*env)->GetObjectClass(env, obj));
  152. bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J"));
  153. (*env)->SetLongField(env, obj, fid, (size_t)handle);
  154. bailout:
  155. return;
  156. }
  157. static jint TJCompressor_compress
  158. (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint x, jint y,
  159. jint width, jint pitch, jint height, jint pf, jbyteArray dst,
  160. jint jpegSubsamp, jint jpegQual, jint flags)
  161. {
  162. tjhandle handle=0;
  163. unsigned long jpegSize=0;
  164. jsize arraySize=0, actualPitch;
  165. unsigned char *srcBuf=NULL, *jpegBuf=NULL;
  166. gethandle();
  167. if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1
  168. || pitch<0)
  169. _throwarg("Invalid argument in compress()");
  170. if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
  171. _throwarg("Mismatch between Java and C API");
  172. actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
  173. arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf];
  174. if((*env)->GetArrayLength(env, src)*srcElementSize<arraySize)
  175. _throwarg("Source buffer is not large enough");
  176. jpegSize=tjBufSize(width, height, jpegSubsamp);
  177. if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize)
  178. _throwarg("Destination buffer is not large enough");
  179. bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
  180. bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
  181. if(ProcessSystemProperties(env)<0) goto bailout;
  182. if(tjCompress2(handle, &srcBuf[y*actualPitch + x*tjPixelSize[pf]], width,
  183. pitch, height, pf, &jpegBuf, &jpegSize, jpegSubsamp, jpegQual,
  184. flags|TJFLAG_NOREALLOC)==-1)
  185. _throwtj();
  186. bailout:
  187. if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
  188. if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
  189. return (jint)jpegSize;
  190. }
  191. /* TurboJPEG 1.3.x: TJCompressor::compress() byte source */
  192. JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIIIII_3BIII
  193. (JNIEnv *env, jobject obj, jbyteArray src, jint x, jint y, jint width,
  194. jint pitch, jint height, jint pf, jbyteArray dst, jint jpegSubsamp,
  195. jint jpegQual, jint flags)
  196. {
  197. return TJCompressor_compress(env, obj, src, 1, x, y, width, pitch, height,
  198. pf, dst, jpegSubsamp, jpegQual, flags);
  199. }
  200. /* TurboJPEG 1.2.x: TJCompressor::compress() byte source */
  201. JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIII_3BIII
  202. (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch,
  203. jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual,
  204. jint flags)
  205. {
  206. return TJCompressor_compress(env, obj, src, 1, 0, 0, width, pitch, height,
  207. pf, dst, jpegSubsamp, jpegQual, flags);
  208. }
  209. /* TurboJPEG 1.3.x: TJCompressor::compress() int source */
  210. JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIIIII_3BIII
  211. (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width,
  212. jint stride, jint height, jint pf, jbyteArray dst, jint jpegSubsamp,
  213. jint jpegQual, jint flags)
  214. {
  215. if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
  216. _throwarg("Invalid argument in compress()");
  217. if(tjPixelSize[pf]!=sizeof(jint))
  218. _throwarg("Pixel format must be 32-bit when compressing from an integer buffer.");
  219. return TJCompressor_compress(env, obj, src, sizeof(jint), x, y, width,
  220. stride*sizeof(jint), height, pf, dst, jpegSubsamp, jpegQual, flags);
  221. bailout:
  222. return 0;
  223. }
  224. /* TurboJPEG 1.2.x: TJCompressor::compress() int source */
  225. JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIII_3BIII
  226. (JNIEnv *env, jobject obj, jintArray src, jint width, jint stride,
  227. jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual,
  228. jint flags)
  229. {
  230. if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
  231. _throwarg("Invalid argument in compress()");
  232. if(tjPixelSize[pf]!=sizeof(jint))
  233. _throwarg("Pixel format must be 32-bit when compressing from an integer buffer.");
  234. return TJCompressor_compress(env, obj, src, sizeof(jint), 0, 0, width,
  235. stride*sizeof(jint), height, pf, dst, jpegSubsamp, jpegQual, flags);
  236. bailout:
  237. return 0;
  238. }
  239. /* TurboJPEG 1.4.x: TJCompressor::compressFromYUV() */
  240. JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compressFromYUV___3_3B_3II_3III_3BII
  241. (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
  242. jint width, jintArray jSrcStrides, jint height, jint subsamp,
  243. jbyteArray dst, jint jpegQual, jint flags)
  244. {
  245. tjhandle handle=0;
  246. unsigned long jpegSize=0;
  247. jbyteArray jSrcPlanes[3]={NULL, NULL, NULL};
  248. const unsigned char *srcPlanes[3];
  249. unsigned char *jpegBuf=NULL;
  250. int *srcOffsets=NULL, *srcStrides=NULL;
  251. int nc=(subsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3), i;
  252. gethandle();
  253. if(subsamp<0 || subsamp>=org_libjpegturbo_turbojpeg_TJ_NUMSAMP)
  254. _throwarg("Invalid argument in compressFromYUV()");
  255. if(org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP)
  256. _throwarg("Mismatch between Java and C API");
  257. if((*env)->GetArrayLength(env, srcobjs)<nc)
  258. _throwarg("Planes array is too small for the subsampling type");
  259. if((*env)->GetArrayLength(env, jSrcOffsets)<nc)
  260. _throwarg("Offsets array is too small for the subsampling type");
  261. if((*env)->GetArrayLength(env, jSrcStrides)<nc)
  262. _throwarg("Strides array is too small for the subsampling type");
  263. jpegSize=tjBufSize(width, height, subsamp);
  264. if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize)
  265. _throwarg("Destination buffer is not large enough");
  266. bailif0(srcOffsets=(*env)->GetPrimitiveArrayCritical(env, jSrcOffsets, 0));
  267. bailif0(srcStrides=(*env)->GetPrimitiveArrayCritical(env, jSrcStrides, 0));
  268. for(i=0; i<nc; i++)
  269. {
  270. int planeSize=tjPlaneSizeYUV(i, width, srcStrides[i], height, subsamp);
  271. int pw=tjPlaneWidth(i, width, subsamp);
  272. if(planeSize<0 || pw<0)
  273. _throwarg(tjGetErrorStr());
  274. if(srcOffsets[i]<0)
  275. _throwarg("Invalid argument in compressFromYUV()");
  276. if(srcStrides[i]<0 && srcOffsets[i]-planeSize+pw<0)
  277. _throwarg("Negative plane stride would cause memory to be accessed below plane boundary");
  278. bailif0(jSrcPlanes[i]=(*env)->GetObjectArrayElement(env, srcobjs, i));
  279. if((*env)->GetArrayLength(env, jSrcPlanes[i])<srcOffsets[i]+planeSize)
  280. _throwarg("Source plane is not large enough");
  281. bailif0(srcPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jSrcPlanes[i],
  282. 0));
  283. srcPlanes[i]=&srcPlanes[i][srcOffsets[i]];
  284. }
  285. bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
  286. if(ProcessSystemProperties(env)<0) goto bailout;
  287. if(tjCompressFromYUVPlanes(handle, srcPlanes, width, srcStrides, height,
  288. subsamp, &jpegBuf, &jpegSize, jpegQual, flags|TJFLAG_NOREALLOC)==-1)
  289. _throwtj();
  290. bailout:
  291. if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0);
  292. for(i=0; i<nc; i++)
  293. {
  294. if(srcPlanes[i] && jSrcPlanes[i])
  295. (*env)->ReleasePrimitiveArrayCritical(env, jSrcPlanes[i],
  296. (unsigned char *)srcPlanes[i], 0);
  297. }
  298. if(srcStrides)
  299. (*env)->ReleasePrimitiveArrayCritical(env, jSrcStrides, srcStrides, 0);
  300. if(srcOffsets)
  301. (*env)->ReleasePrimitiveArrayCritical(env, jSrcOffsets, srcOffsets, 0);
  302. return (jint)jpegSize;
  303. }
  304. static void TJCompressor_encodeYUV
  305. (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint x, jint y,
  306. jint width, jint pitch, jint height, jint pf, jobjectArray dstobjs,
  307. jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags)
  308. {
  309. tjhandle handle=0;
  310. jsize arraySize=0, actualPitch;
  311. jbyteArray jDstPlanes[3]={NULL, NULL, NULL};
  312. unsigned char *srcBuf=NULL, *dstPlanes[3];
  313. int *dstOffsets=NULL, *dstStrides=NULL;
  314. int nc=(subsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3), i;
  315. gethandle();
  316. if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1
  317. || pitch<0 || subsamp<0 || subsamp>=org_libjpegturbo_turbojpeg_TJ_NUMSAMP)
  318. _throwarg("Invalid argument in encodeYUV()");
  319. if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF
  320. || org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP)
  321. _throwarg("Mismatch between Java and C API");
  322. if((*env)->GetArrayLength(env, dstobjs)<nc)
  323. _throwarg("Planes array is too small for the subsampling type");
  324. if((*env)->GetArrayLength(env, jDstOffsets)<nc)
  325. _throwarg("Offsets array is too small for the subsampling type");
  326. if((*env)->GetArrayLength(env, jDstStrides)<nc)
  327. _throwarg("Strides array is too small for the subsampling type");
  328. actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
  329. arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf];
  330. if((*env)->GetArrayLength(env, src)*srcElementSize<arraySize)
  331. _throwarg("Source buffer is not large enough");
  332. bailif0(dstOffsets=(*env)->GetPrimitiveArrayCritical(env, jDstOffsets, 0));
  333. bailif0(dstStrides=(*env)->GetPrimitiveArrayCritical(env, jDstStrides, 0));
  334. for(i=0; i<nc; i++)
  335. {
  336. int planeSize=tjPlaneSizeYUV(i, width, dstStrides[i], height, subsamp);
  337. int pw=tjPlaneWidth(i, width, subsamp);
  338. if(planeSize<0 || pw<0)
  339. _throwarg(tjGetErrorStr());
  340. if(dstOffsets[i]<0)
  341. _throwarg("Invalid argument in encodeYUV()");
  342. if(dstStrides[i]<0 && dstOffsets[i]-planeSize+pw<0)
  343. _throwarg("Negative plane stride would cause memory to be accessed below plane boundary");
  344. bailif0(jDstPlanes[i]=(*env)->GetObjectArrayElement(env, dstobjs, i));
  345. if((*env)->GetArrayLength(env, jDstPlanes[i])<dstOffsets[i]+planeSize)
  346. _throwarg("Destination plane is not large enough");
  347. bailif0(dstPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jDstPlanes[i],
  348. 0));
  349. dstPlanes[i]=&dstPlanes[i][dstOffsets[i]];
  350. }
  351. bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
  352. if(tjEncodeYUVPlanes(handle, &srcBuf[y*actualPitch + x*tjPixelSize[pf]],
  353. width, pitch, height, pf, dstPlanes, dstStrides, subsamp, flags)==-1)
  354. _throwtj();
  355. bailout:
  356. if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
  357. for(i=0; i<nc; i++)
  358. {
  359. if(dstPlanes[i] && jDstPlanes[i])
  360. (*env)->ReleasePrimitiveArrayCritical(env, jDstPlanes[i], dstPlanes[i],
  361. 0);
  362. }
  363. if(dstStrides)
  364. (*env)->ReleasePrimitiveArrayCritical(env, jDstStrides, dstStrides, 0);
  365. if(dstOffsets)
  366. (*env)->ReleasePrimitiveArrayCritical(env, jDstOffsets, dstOffsets, 0);
  367. return;
  368. }
  369. /* TurboJPEG 1.4.x: TJCompressor::encodeYUV() byte source */
  370. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIIIII_3_3B_3I_3III
  371. (JNIEnv *env, jobject obj, jbyteArray src, jint x, jint y, jint width,
  372. jint pitch, jint height, jint pf, jobjectArray dstobjs,
  373. jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags)
  374. {
  375. TJCompressor_encodeYUV(env, obj, src, 1, x, y, width, pitch, height, pf,
  376. dstobjs, jDstOffsets, jDstStrides, subsamp, flags);
  377. }
  378. /* TurboJPEG 1.4.x: TJCompressor::encodeYUV() int source */
  379. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIIIII_3_3B_3I_3III
  380. (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width,
  381. jint stride, jint height, jint pf, jobjectArray dstobjs,
  382. jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags)
  383. {
  384. if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
  385. _throwarg("Invalid argument in encodeYUV()");
  386. if(tjPixelSize[pf]!=sizeof(jint))
  387. _throwarg("Pixel format must be 32-bit when encoding from an integer buffer.");
  388. TJCompressor_encodeYUV(env, obj, src, sizeof(jint), x, y, width,
  389. stride*sizeof(jint), height, pf, dstobjs, jDstOffsets, jDstStrides,
  390. subsamp, flags);
  391. bailout:
  392. return;
  393. }
  394. JNIEXPORT void JNICALL TJCompressor_encodeYUV_12
  395. (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint width,
  396. jint pitch, jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
  397. {
  398. tjhandle handle=0;
  399. jsize arraySize=0;
  400. unsigned char *srcBuf=NULL, *dstBuf=NULL;
  401. gethandle();
  402. if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1
  403. || pitch<0)
  404. _throwarg("Invalid argument in encodeYUV()");
  405. if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
  406. _throwarg("Mismatch between Java and C API");
  407. arraySize=(pitch==0)? width*tjPixelSize[pf]*height:pitch*height;
  408. if((*env)->GetArrayLength(env, src)*srcElementSize<arraySize)
  409. _throwarg("Source buffer is not large enough");
  410. if((*env)->GetArrayLength(env, dst)
  411. <(jsize)tjBufSizeYUV(width, height, subsamp))
  412. _throwarg("Destination buffer is not large enough");
  413. bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
  414. bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
  415. if(tjEncodeYUV2(handle, srcBuf, width, pitch, height, pf, dstBuf, subsamp,
  416. flags)==-1)
  417. _throwtj();
  418. bailout:
  419. if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
  420. if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0);
  421. return;
  422. }
  423. /* TurboJPEG 1.2.x: TJCompressor::encodeYUV() byte source */
  424. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIII_3BII
  425. (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch,
  426. jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
  427. {
  428. TJCompressor_encodeYUV_12(env, obj, src, 1, width, pitch, height, pf, dst,
  429. subsamp, flags);
  430. }
  431. /* TurboJPEG 1.2.x: TJCompressor::encodeYUV() int source */
  432. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIII_3BII
  433. (JNIEnv *env, jobject obj, jintArray src, jint width, jint stride,
  434. jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
  435. {
  436. if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
  437. _throwarg("Invalid argument in encodeYUV()");
  438. if(tjPixelSize[pf]!=sizeof(jint))
  439. _throwarg("Pixel format must be 32-bit when encoding from an integer buffer.");
  440. TJCompressor_encodeYUV_12(env, obj, src, sizeof(jint), width,
  441. stride*sizeof(jint), height, pf, dst, subsamp, flags);
  442. bailout:
  443. return;
  444. }
  445. /* TurboJPEG 1.2.x: TJCompressor::destroy() */
  446. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy
  447. (JNIEnv *env, jobject obj)
  448. {
  449. tjhandle handle=0;
  450. gethandle();
  451. if(tjDestroy(handle)==-1) _throwtj();
  452. (*env)->SetLongField(env, obj, _fid, 0);
  453. bailout:
  454. return;
  455. }
  456. /* TurboJPEG 1.2.x: TJDecompressor::init() */
  457. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_init
  458. (JNIEnv *env, jobject obj)
  459. {
  460. jclass cls;
  461. jfieldID fid;
  462. tjhandle handle;
  463. if((handle=tjInitDecompress())==NULL) _throwtj();
  464. bailif0(cls=(*env)->GetObjectClass(env, obj));
  465. bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J"));
  466. (*env)->SetLongField(env, obj, fid, (size_t)handle);
  467. bailout:
  468. return;
  469. }
  470. /* TurboJPEG 1.2.x: TJDecompressor::getScalingFactors() */
  471. JNIEXPORT jobjectArray JNICALL Java_org_libjpegturbo_turbojpeg_TJ_getScalingFactors
  472. (JNIEnv *env, jclass cls)
  473. {
  474. jclass sfcls=NULL; jfieldID fid=0;
  475. tjscalingfactor *sf=NULL; int n=0, i;
  476. jobject sfobj=NULL;
  477. jobjectArray sfjava=NULL;
  478. if((sf=tjGetScalingFactors(&n))==NULL || n==0)
  479. _throwarg(tjGetErrorStr());
  480. bailif0(sfcls=(*env)->FindClass(env, "org/libjpegturbo/turbojpeg/TJScalingFactor"));
  481. bailif0(sfjava=(jobjectArray)(*env)->NewObjectArray(env, n, sfcls, 0));
  482. for(i=0; i<n; i++)
  483. {
  484. bailif0(sfobj=(*env)->AllocObject(env, sfcls));
  485. bailif0(fid=(*env)->GetFieldID(env, sfcls, "num", "I"));
  486. (*env)->SetIntField(env, sfobj, fid, sf[i].num);
  487. bailif0(fid=(*env)->GetFieldID(env, sfcls, "denom", "I"));
  488. (*env)->SetIntField(env, sfobj, fid, sf[i].denom);
  489. (*env)->SetObjectArrayElement(env, sfjava, i, sfobj);
  490. }
  491. bailout:
  492. return sfjava;
  493. }
  494. /* TurboJPEG 1.2.x: TJDecompressor::decompressHeader() */
  495. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressHeader
  496. (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize)
  497. {
  498. tjhandle handle=0;
  499. unsigned char *jpegBuf=NULL;
  500. int width=0, height=0, jpegSubsamp=-1, jpegColorspace=-1;
  501. gethandle();
  502. if((*env)->GetArrayLength(env, src)<jpegSize)
  503. _throwarg("Source buffer is not large enough");
  504. bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
  505. if(tjDecompressHeader3(handle, jpegBuf, (unsigned long)jpegSize,
  506. &width, &height, &jpegSubsamp, &jpegColorspace)==-1)
  507. _throwtj();
  508. (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); jpegBuf=NULL;
  509. bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
  510. (*env)->SetIntField(env, obj, _fid, jpegSubsamp);
  511. if((_fid=(*env)->GetFieldID(env, _cls, "jpegColorspace", "I"))==0)
  512. (*env)->ExceptionClear(env);
  513. else
  514. (*env)->SetIntField(env, obj, _fid, jpegColorspace);
  515. bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
  516. (*env)->SetIntField(env, obj, _fid, width);
  517. bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
  518. (*env)->SetIntField(env, obj, _fid, height);
  519. bailout:
  520. if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
  521. return;
  522. }
  523. static void TJDecompressor_decompress
  524. (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jarray dst,
  525. jint dstElementSize, jint x, jint y, jint width, jint pitch, jint height,
  526. jint pf, jint flags)
  527. {
  528. tjhandle handle=0;
  529. jsize arraySize=0, actualPitch;
  530. unsigned char *jpegBuf=NULL, *dstBuf=NULL;
  531. gethandle();
  532. if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
  533. _throwarg("Invalid argument in decompress()");
  534. if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF)
  535. _throwarg("Mismatch between Java and C API");
  536. if((*env)->GetArrayLength(env, src)<jpegSize)
  537. _throwarg("Source buffer is not large enough");
  538. actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
  539. arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf];
  540. if((*env)->GetArrayLength(env, dst)*dstElementSize<arraySize)
  541. _throwarg("Destination buffer is not large enough");
  542. bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
  543. bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
  544. if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize,
  545. &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf,
  546. flags)==-1)
  547. _throwtj();
  548. bailout:
  549. if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
  550. if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
  551. return;
  552. }
  553. /* TurboJPEG 1.3.x: TJDecompressor::decompress() byte destination */
  554. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIIIII
  555. (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
  556. jint x, jint y, jint width, jint pitch, jint height, jint pf, jint flags)
  557. {
  558. TJDecompressor_decompress(env, obj, src, jpegSize, dst, 1, x, y, width,
  559. pitch, height, pf, flags);
  560. }
  561. /* TurboJPEG 1.2.x: TJDecompressor::decompress() byte destination */
  562. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIII
  563. (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
  564. jint width, jint pitch, jint height, jint pf, jint flags)
  565. {
  566. TJDecompressor_decompress(env, obj, src, jpegSize, dst, 1, 0, 0, width,
  567. pitch, height, pf, flags);
  568. }
  569. /* TurboJPEG 1.3.x: TJDecompressor::decompress() int destination */
  570. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIIIII
  571. (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst,
  572. jint x, jint y, jint width, jint stride, jint height, jint pf, jint flags)
  573. {
  574. if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
  575. _throwarg("Invalid argument in decompress()");
  576. if(tjPixelSize[pf]!=sizeof(jint))
  577. _throwarg("Pixel format must be 32-bit when decompressing to an integer buffer.");
  578. TJDecompressor_decompress(env, obj, src, jpegSize, dst, sizeof(jint), x, y,
  579. width, stride*sizeof(jint), height, pf, flags);
  580. bailout:
  581. return;
  582. }
  583. /* TurboJPEG 1.2.x: TJDecompressor::decompress() int destination */
  584. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIII
  585. (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst,
  586. jint width, jint stride, jint height, jint pf, jint flags)
  587. {
  588. if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
  589. _throwarg("Invalid argument in decompress()");
  590. if(tjPixelSize[pf]!=sizeof(jint))
  591. _throwarg("Pixel format must be 32-bit when decompressing to an integer buffer.");
  592. TJDecompressor_decompress(env, obj, src, jpegSize, dst, sizeof(jint), 0, 0,
  593. width, stride*sizeof(jint), height, pf, flags);
  594. bailout:
  595. return;
  596. }
  597. /* TurboJPEG 1.4.x: TJDecompressor::decompressToYUV() */
  598. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV___3BI_3_3B_3II_3III
  599. (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize,
  600. jobjectArray dstobjs, jintArray jDstOffsets, jint desiredWidth,
  601. jintArray jDstStrides, jint desiredHeight, jint flags)
  602. {
  603. tjhandle handle=0;
  604. jbyteArray jDstPlanes[3]={NULL, NULL, NULL};
  605. unsigned char *jpegBuf=NULL, *dstPlanes[3];
  606. int *dstOffsets=NULL, *dstStrides=NULL;
  607. int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0;
  608. int nc=0, i, width, height, scaledWidth, scaledHeight, nsf=0;
  609. tjscalingfactor *sf;
  610. gethandle();
  611. if((*env)->GetArrayLength(env, src)<jpegSize)
  612. _throwarg("Source buffer is not large enough");
  613. bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
  614. jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
  615. bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
  616. jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
  617. bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
  618. jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
  619. nc=(jpegSubsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3);
  620. width=desiredWidth; height=desiredHeight;
  621. if(width==0) width=jpegWidth;
  622. if(height==0) height=jpegHeight;
  623. sf=tjGetScalingFactors(&nsf);
  624. if(!sf || nsf<1)
  625. _throwarg(tjGetErrorStr());
  626. for(i=0; i<nsf; i++)
  627. {
  628. scaledWidth=TJSCALED(jpegWidth, sf[i]);
  629. scaledHeight=TJSCALED(jpegHeight, sf[i]);
  630. if(scaledWidth<=width && scaledHeight<=height)
  631. break;
  632. }
  633. if(i>=nsf)
  634. _throwarg("Could not scale down to desired image dimensions");
  635. bailif0(dstOffsets=(*env)->GetPrimitiveArrayCritical(env, jDstOffsets, 0));
  636. bailif0(dstStrides=(*env)->GetPrimitiveArrayCritical(env, jDstStrides, 0));
  637. for(i=0; i<nc; i++)
  638. {
  639. int planeSize=tjPlaneSizeYUV(i, scaledWidth, dstStrides[i], scaledHeight,
  640. jpegSubsamp);
  641. int pw=tjPlaneWidth(i, scaledWidth, jpegSubsamp);
  642. if(planeSize<0 || pw<0)
  643. _throwarg(tjGetErrorStr());
  644. if(dstOffsets[i]<0)
  645. _throwarg("Invalid argument in decompressToYUV()");
  646. if(dstStrides[i]<0 && dstOffsets[i]-planeSize+pw<0)
  647. _throwarg("Negative plane stride would cause memory to be accessed below plane boundary");
  648. bailif0(jDstPlanes[i]=(*env)->GetObjectArrayElement(env, dstobjs, i));
  649. if((*env)->GetArrayLength(env, jDstPlanes[i])<dstOffsets[i]+planeSize)
  650. _throwarg("Destination plane is not large enough");
  651. bailif0(dstPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jDstPlanes[i],
  652. 0));
  653. dstPlanes[i]=&dstPlanes[i][dstOffsets[i]];
  654. }
  655. bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
  656. if(tjDecompressToYUVPlanes(handle, jpegBuf, (unsigned long)jpegSize,
  657. dstPlanes, desiredWidth, dstStrides, desiredHeight, flags)==-1)
  658. _throwtj();
  659. bailout:
  660. if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
  661. for(i=0; i<nc; i++)
  662. {
  663. if(dstPlanes[i] && jDstPlanes[i])
  664. (*env)->ReleasePrimitiveArrayCritical(env, jDstPlanes[i], dstPlanes[i],
  665. 0);
  666. }
  667. if(dstStrides)
  668. (*env)->ReleasePrimitiveArrayCritical(env, jDstStrides, dstStrides, 0);
  669. if(dstOffsets)
  670. (*env)->ReleasePrimitiveArrayCritical(env, jDstOffsets, dstOffsets, 0);
  671. return;
  672. }
  673. /* TurboJPEG 1.2.x: TJDecompressor::decompressToYUV() */
  674. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV___3BI_3BI
  675. (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
  676. jint flags)
  677. {
  678. tjhandle handle=0;
  679. unsigned char *jpegBuf=NULL, *dstBuf=NULL;
  680. int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0;
  681. gethandle();
  682. if((*env)->GetArrayLength(env, src)<jpegSize)
  683. _throwarg("Source buffer is not large enough");
  684. bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
  685. jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
  686. bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
  687. jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
  688. bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
  689. jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
  690. if((*env)->GetArrayLength(env, dst)
  691. <(jsize)tjBufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp))
  692. _throwarg("Destination buffer is not large enough");
  693. bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0));
  694. bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
  695. if(tjDecompressToYUV(handle, jpegBuf, (unsigned long)jpegSize, dstBuf,
  696. flags)==-1)
  697. _throwtj();
  698. bailout:
  699. if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
  700. if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0);
  701. return;
  702. }
  703. static void TJDecompressor_decodeYUV
  704. (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
  705. jintArray jSrcStrides, jint subsamp, jarray dst, jint dstElementSize,
  706. jint x, jint y, jint width, jint pitch, jint height, jint pf, jint flags)
  707. {
  708. tjhandle handle=0;
  709. jsize arraySize=0, actualPitch;
  710. jbyteArray jSrcPlanes[3]={NULL, NULL, NULL};
  711. const unsigned char *srcPlanes[3];
  712. unsigned char *dstBuf=NULL;
  713. int *srcOffsets=NULL, *srcStrides=NULL;
  714. int nc=(subsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3), i;
  715. gethandle();
  716. if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || subsamp<0
  717. || subsamp>=org_libjpegturbo_turbojpeg_TJ_NUMSAMP)
  718. _throwarg("Invalid argument in decodeYUV()");
  719. if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF
  720. || org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP)
  721. _throwarg("Mismatch between Java and C API");
  722. if((*env)->GetArrayLength(env, srcobjs)<nc)
  723. _throwarg("Planes array is too small for the subsampling type");
  724. if((*env)->GetArrayLength(env, jSrcOffsets)<nc)
  725. _throwarg("Offsets array is too small for the subsampling type");
  726. if((*env)->GetArrayLength(env, jSrcStrides)<nc)
  727. _throwarg("Strides array is too small for the subsampling type");
  728. actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch;
  729. arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf];
  730. if((*env)->GetArrayLength(env, dst)*dstElementSize<arraySize)
  731. _throwarg("Destination buffer is not large enough");
  732. bailif0(srcOffsets=(*env)->GetPrimitiveArrayCritical(env, jSrcOffsets, 0));
  733. bailif0(srcStrides=(*env)->GetPrimitiveArrayCritical(env, jSrcStrides, 0));
  734. for(i=0; i<nc; i++)
  735. {
  736. int planeSize=tjPlaneSizeYUV(i, width, srcStrides[i], height, subsamp);
  737. int pw=tjPlaneWidth(i, width, subsamp);
  738. if(planeSize<0 || pw<0)
  739. _throwarg(tjGetErrorStr());
  740. if(srcOffsets[i]<0)
  741. _throwarg("Invalid argument in decodeYUV()");
  742. if(srcStrides[i]<0 && srcOffsets[i]-planeSize+pw<0)
  743. _throwarg("Negative plane stride would cause memory to be accessed below plane boundary");
  744. bailif0(jSrcPlanes[i]=(*env)->GetObjectArrayElement(env, srcobjs, i));
  745. if((*env)->GetArrayLength(env, jSrcPlanes[i])<srcOffsets[i]+planeSize)
  746. _throwarg("Source plane is not large enough");
  747. bailif0(srcPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jSrcPlanes[i],
  748. 0));
  749. srcPlanes[i]=&srcPlanes[i][srcOffsets[i]];
  750. }
  751. bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0));
  752. if(tjDecodeYUVPlanes(handle, srcPlanes, srcStrides, subsamp,
  753. &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf,
  754. flags)==-1)
  755. _throwtj();
  756. bailout:
  757. if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0);
  758. for(i=0; i<nc; i++)
  759. {
  760. if(srcPlanes[i] && jSrcPlanes[i])
  761. (*env)->ReleasePrimitiveArrayCritical(env, jSrcPlanes[i],
  762. (unsigned char *)srcPlanes[i], 0);
  763. }
  764. if(srcStrides)
  765. (*env)->ReleasePrimitiveArrayCritical(env, jSrcStrides, srcStrides, 0);
  766. if(srcOffsets)
  767. (*env)->ReleasePrimitiveArrayCritical(env, jSrcOffsets, srcOffsets, 0);
  768. return;
  769. }
  770. /* TurboJPEG 1.4.x: TJDecompressor::decodeYUV() byte destination */
  771. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decodeYUV___3_3B_3I_3II_3BIIIIIII
  772. (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
  773. jintArray jSrcStrides, jint subsamp, jbyteArray dst, jint x, jint y,
  774. jint width, jint pitch, jint height, jint pf, jint flags)
  775. {
  776. TJDecompressor_decodeYUV(env, obj, srcobjs, jSrcOffsets, jSrcStrides,
  777. subsamp, dst, 1, x, y, width, pitch, height, pf, flags);
  778. }
  779. /* TurboJPEG 1.4.x: TJDecompressor::decodeYUV() int destination */
  780. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decodeYUV___3_3B_3I_3II_3IIIIIIII
  781. (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
  782. jintArray jSrcStrides, jint subsamp, jintArray dst, jint x, jint y,
  783. jint width, jint stride, jint height, jint pf, jint flags)
  784. {
  785. if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF)
  786. _throwarg("Invalid argument in decodeYUV()");
  787. if(tjPixelSize[pf]!=sizeof(jint))
  788. _throwarg("Pixel format must be 32-bit when decoding to an integer buffer.");
  789. TJDecompressor_decodeYUV(env, obj, srcobjs, jSrcOffsets, jSrcStrides,
  790. subsamp, dst, sizeof(jint), x, y, width, stride*sizeof(jint), height, pf,
  791. flags);
  792. bailout:
  793. return;
  794. }
  795. /* TurboJPEG 1.2.x: TJTransformer::init() */
  796. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_init
  797. (JNIEnv *env, jobject obj)
  798. {
  799. jclass cls;
  800. jfieldID fid;
  801. tjhandle handle;
  802. if((handle=tjInitTransform())==NULL) _throwtj();
  803. bailif0(cls=(*env)->GetObjectClass(env, obj));
  804. bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J"));
  805. (*env)->SetLongField(env, obj, fid, (size_t)handle);
  806. bailout:
  807. return;
  808. }
  809. typedef struct _JNICustomFilterParams
  810. {
  811. JNIEnv *env;
  812. jobject tobj;
  813. jobject cfobj;
  814. } JNICustomFilterParams;
  815. static int JNICustomFilter(short *coeffs, tjregion arrayRegion,
  816. tjregion planeRegion, int componentIndex, int transformIndex,
  817. tjtransform *transform)
  818. {
  819. JNICustomFilterParams *params=(JNICustomFilterParams *)transform->data;
  820. JNIEnv *env=params->env;
  821. jobject tobj=params->tobj, cfobj=params->cfobj;
  822. jobject arrayRegionObj, planeRegionObj, bufobj, borobj;
  823. jclass cls; jmethodID mid; jfieldID fid;
  824. bailif0(bufobj=(*env)->NewDirectByteBuffer(env, coeffs,
  825. sizeof(short)*arrayRegion.w*arrayRegion.h));
  826. bailif0(cls=(*env)->FindClass(env, "java/nio/ByteOrder"));
  827. bailif0(mid=(*env)->GetStaticMethodID(env, cls, "nativeOrder",
  828. "()Ljava/nio/ByteOrder;"));
  829. bailif0(borobj=(*env)->CallStaticObjectMethod(env, cls, mid));
  830. bailif0(cls=(*env)->GetObjectClass(env, bufobj));
  831. bailif0(mid=(*env)->GetMethodID(env, cls, "order",
  832. "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;"));
  833. (*env)->CallObjectMethod(env, bufobj, mid, borobj);
  834. bailif0(mid=(*env)->GetMethodID(env, cls, "asShortBuffer",
  835. "()Ljava/nio/ShortBuffer;"));
  836. bailif0(bufobj=(*env)->CallObjectMethod(env, bufobj, mid));
  837. bailif0(cls=(*env)->FindClass(env, "java/awt/Rectangle"));
  838. bailif0(arrayRegionObj=(*env)->AllocObject(env, cls));
  839. bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I"));
  840. (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.x);
  841. bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I"));
  842. (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.y);
  843. bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I"));
  844. (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.w);
  845. bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I"));
  846. (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.h);
  847. bailif0(planeRegionObj=(*env)->AllocObject(env, cls));
  848. bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I"));
  849. (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.x);
  850. bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I"));
  851. (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.y);
  852. bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I"));
  853. (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.w);
  854. bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I"));
  855. (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.h);
  856. bailif0(cls=(*env)->GetObjectClass(env, cfobj));
  857. bailif0(mid=(*env)->GetMethodID(env, cls, "customFilter",
  858. "(Ljava/nio/ShortBuffer;Ljava/awt/Rectangle;Ljava/awt/Rectangle;IILorg/libjpegturbo/turbojpeg/TJTransform;)V"));
  859. (*env)->CallVoidMethod(env, cfobj, mid, bufobj, arrayRegionObj,
  860. planeRegionObj, componentIndex, transformIndex, tobj);
  861. return 0;
  862. bailout:
  863. return -1;
  864. }
  865. /* TurboJPEG 1.2.x: TJTransformer::transform() */
  866. JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transform
  867. (JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize,
  868. jobjectArray dstobjs, jobjectArray tobjs, jint flags)
  869. {
  870. tjhandle handle=0; int i;
  871. unsigned char *jpegBuf=NULL, **dstBufs=NULL; jsize n=0;
  872. unsigned long *dstSizes=NULL; tjtransform *t=NULL;
  873. jbyteArray *jdstBufs=NULL;
  874. int jpegWidth=0, jpegHeight=0, jpegSubsamp;
  875. jintArray jdstSizes=0; jint *dstSizesi=NULL;
  876. JNICustomFilterParams *params=NULL;
  877. gethandle();
  878. if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize)
  879. _throwarg("Source buffer is not large enough");
  880. bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
  881. jpegWidth=(int)(*env)->GetIntField(env, obj, _fid);
  882. bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
  883. jpegHeight=(int)(*env)->GetIntField(env, obj, _fid);
  884. bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
  885. jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid);
  886. n=(*env)->GetArrayLength(env, dstobjs);
  887. if(n!=(*env)->GetArrayLength(env, tobjs))
  888. _throwarg("Mismatch between size of transforms array and destination buffers array");
  889. if((dstBufs=(unsigned char **)malloc(sizeof(unsigned char *)*n))==NULL)
  890. _throwmem();
  891. if((jdstBufs=(jbyteArray *)malloc(sizeof(jbyteArray)*n))==NULL)
  892. _throwmem();
  893. if((dstSizes=(unsigned long *)malloc(sizeof(unsigned long)*n))==NULL)
  894. _throwmem();
  895. if((t=(tjtransform *)malloc(sizeof(tjtransform)*n))==NULL)
  896. _throwmem();
  897. if((params=(JNICustomFilterParams *)malloc(sizeof(JNICustomFilterParams)*n))
  898. ==NULL)
  899. _throwmem();
  900. for(i=0; i<n; i++)
  901. {
  902. dstBufs[i]=NULL; jdstBufs[i]=NULL; dstSizes[i]=0;
  903. memset(&t[i], 0, sizeof(tjtransform));
  904. memset(&params[i], 0, sizeof(JNICustomFilterParams));
  905. }
  906. for(i=0; i<n; i++)
  907. {
  908. jobject tobj, cfobj;
  909. bailif0(tobj=(*env)->GetObjectArrayElement(env, tobjs, i));
  910. bailif0(_cls=(*env)->GetObjectClass(env, tobj));
  911. bailif0(_fid=(*env)->GetFieldID(env, _cls, "op", "I"));
  912. t[i].op=(*env)->GetIntField(env, tobj, _fid);
  913. bailif0(_fid=(*env)->GetFieldID(env, _cls, "options", "I"));
  914. t[i].options=(*env)->GetIntField(env, tobj, _fid);
  915. bailif0(_fid=(*env)->GetFieldID(env, _cls, "x", "I"));
  916. t[i].r.x=(*env)->GetIntField(env, tobj, _fid);
  917. bailif0(_fid=(*env)->GetFieldID(env, _cls, "y", "I"));
  918. t[i].r.y=(*env)->GetIntField(env, tobj, _fid);
  919. bailif0(_fid=(*env)->GetFieldID(env, _cls, "width", "I"));
  920. t[i].r.w=(*env)->GetIntField(env, tobj, _fid);
  921. bailif0(_fid=(*env)->GetFieldID(env, _cls, "height", "I"));
  922. t[i].r.h=(*env)->GetIntField(env, tobj, _fid);
  923. bailif0(_fid=(*env)->GetFieldID(env, _cls, "cf",
  924. "Lorg/libjpegturbo/turbojpeg/TJCustomFilter;"));
  925. cfobj=(*env)->GetObjectField(env, tobj, _fid);
  926. if(cfobj)
  927. {
  928. params[i].env=env;
  929. params[i].tobj=tobj;
  930. params[i].cfobj=cfobj;
  931. t[i].customFilter=JNICustomFilter;
  932. t[i].data=(void *)&params[i];
  933. }
  934. }
  935. for(i=0; i<n; i++)
  936. {
  937. int w=jpegWidth, h=jpegHeight;
  938. if(t[i].r.w!=0) w=t[i].r.w;
  939. if(t[i].r.h!=0) h=t[i].r.h;
  940. bailif0(jdstBufs[i]=(*env)->GetObjectArrayElement(env, dstobjs, i));
  941. if((unsigned long)(*env)->GetArrayLength(env, jdstBufs[i])
  942. <tjBufSize(w, h, jpegSubsamp))
  943. _throwarg("Destination buffer is not large enough");
  944. }
  945. bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0));
  946. for(i=0; i<n; i++)
  947. bailif0(dstBufs[i]=(*env)->GetPrimitiveArrayCritical(env, jdstBufs[i], 0));
  948. if(tjTransform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t,
  949. flags|TJFLAG_NOREALLOC)==-1)
  950. _throwtj();
  951. for(i=0; i<n; i++)
  952. {
  953. (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0);
  954. dstBufs[i]=NULL;
  955. }
  956. (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0);
  957. jpegBuf=NULL;
  958. jdstSizes=(*env)->NewIntArray(env, n);
  959. bailif0(dstSizesi=(*env)->GetIntArrayElements(env, jdstSizes, 0));
  960. for(i=0; i<n; i++) dstSizesi[i]=(int)dstSizes[i];
  961. bailout:
  962. if(dstSizesi) (*env)->ReleaseIntArrayElements(env, jdstSizes, dstSizesi, 0);
  963. if(dstBufs)
  964. {
  965. for(i=0; i<n; i++)
  966. {
  967. if(dstBufs[i] && jdstBufs && jdstBufs[i])
  968. (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0);
  969. }
  970. free(dstBufs);
  971. }
  972. if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0);
  973. if(jdstBufs) free(jdstBufs);
  974. if(dstSizes) free(dstSizes);
  975. if(t) free(t);
  976. return jdstSizes;
  977. }
  978. /* TurboJPEG 1.2.x: TJDecompressor::destroy() */
  979. JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_destroy
  980. (JNIEnv *env, jobject obj)
  981. {
  982. Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy(env, obj);
  983. }