turbojpeg-jni.c 46 KB

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