/*
- * Copyright (C)2011-2021 D. R. Commander. All Rights Reserved.
+ * Copyright (C)2011-2023 D. R. Commander. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* POSSIBILITY OF SUCH DAMAGE.
*/
-#include <stdlib.h>
-#include <string.h>
+#include <limits.h>
#include "turbojpeg.h"
-#ifdef WIN32
-#include "tjutil.h"
-#endif
+#include "jinclude.h"
#include <jni.h>
#include "java/org_libjpegturbo_turbojpeg_TJCompressor.h"
#include "java/org_libjpegturbo_turbojpeg_TJDecompressor.h"
jobject _excobj; \
jstring _errstr; \
\
- BAILIF0(_errstr = (*env)->NewStringUTF(env, tjGetErrorStr2(handle))); \
+ BAILIF0(_errstr = (*env)->NewStringUTF(env, tj3GetErrorStr(handle))); \
BAILIF0(_exccls = (*env)->FindClass(env, \
"org/libjpegturbo/turbojpeg/TJException")); \
BAILIF0(_excid = (*env)->GetMethodID(env, _exccls, "<init>", \
"(Ljava/lang/String;I)V")); \
BAILIF0(_excobj = (*env)->NewObject(env, _exccls, _excid, _errstr, \
- tjGetErrorCode(handle))); \
+ tj3GetErrorCode(handle))); \
(*env)->Throw(env, _excobj); \
goto bailout; \
}
BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "handle", "J")); \
handle = (tjhandle)(size_t)(*env)->GetLongField(env, obj, _fid);
-#ifdef _WIN32
-#define setenv(envvar, value, dummy) _putenv_s(envvar, value)
-#endif
-
-#define PROP2ENV(property, envvar) { \
- if ((jName = (*env)->NewStringUTF(env, property)) != NULL) { \
- jboolean exception; \
- jValue = (*env)->CallStaticObjectMethod(env, cls, mid, jName); \
- exception = (*env)->ExceptionCheck(env); \
- if (jValue && !exception && \
- (value = (*env)->GetStringUTFChars(env, jValue, 0)) != NULL) { \
- setenv(envvar, value, 1); \
- (*env)->ReleaseStringUTFChars(env, jValue, value); \
- } \
- } \
-}
-
#define SAFE_RELEASE(javaArray, cArray) { \
if (javaArray && cArray) \
(*env)->ReleasePrimitiveArrayCritical(env, javaArray, (void *)cArray, 0); \
cArray = NULL; \
}
-static int ProcessSystemProperties(JNIEnv *env)
-{
- jclass cls;
- jmethodID mid;
- jstring jName, jValue;
- const char *value;
-
- BAILIF0(cls = (*env)->FindClass(env, "java/lang/System"));
- BAILIF0(mid = (*env)->GetStaticMethodID(env, cls, "getProperty",
- "(Ljava/lang/String;)Ljava/lang/String;"));
-
- PROP2ENV("turbojpeg.optimize", "TJ_OPTIMIZE");
- PROP2ENV("turbojpeg.arithmetic", "TJ_ARITHMETIC");
- PROP2ENV("turbojpeg.restart", "TJ_RESTART");
- PROP2ENV("turbojpeg.progressive", "TJ_PROGRESSIVE");
- return 0;
-
-bailout:
- return -1;
-}
-
/* TurboJPEG 1.2.x: TJ::bufSize() */
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize
(JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp)
{
- jint retval = (jint)tjBufSize(width, height, jpegSubsamp);
+ size_t retval = tj3JPEGBufSize(width, height, jpegSubsamp);
- if (retval == -1) THROW_ARG(tjGetErrorStr());
+ if (retval == 0) THROW_ARG(tj3GetErrorStr(NULL));
+ if (retval > (size_t)INT_MAX)
+ THROW_ARG("Image is too large");
bailout:
- return retval;
+ return (jint)retval;
}
/* TurboJPEG 1.4.x: TJ::bufSizeYUV() */
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII
- (JNIEnv *env, jclass cls, jint width, jint pad, jint height, jint subsamp)
+ (JNIEnv *env, jclass cls, jint width, jint align, jint height, jint subsamp)
{
- jint retval = (jint)tjBufSizeYUV2(width, pad, height, subsamp);
+ size_t retval = tj3YUVBufSize(width, align, height, subsamp);
- if (retval == -1) THROW_ARG(tjGetErrorStr());
+ if (retval == 0) THROW_ARG(tj3GetErrorStr(NULL));
+ if (retval > (size_t)INT_MAX)
+ THROW_ARG("Image is too large");
bailout:
- return retval;
-}
-
-/* TurboJPEG 1.2.x: TJ::bufSizeYUV() */
-JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__III
- (JNIEnv *env, jclass cls, jint width, jint height, jint subsamp)
-{
- return Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII(env, cls, width,
- 4, height,
- subsamp);
+ return (jint)retval;
}
/* TurboJPEG 1.4.x: TJ::planeSizeYUV() */
(JNIEnv *env, jclass cls, jint componentID, jint width, jint stride,
jint height, jint subsamp)
{
- jint retval = (jint)tjPlaneSizeYUV(componentID, width, stride, height,
- subsamp);
+ size_t retval = tj3YUVPlaneSize(componentID, width, stride, height, subsamp);
- if (retval == -1) THROW_ARG(tjGetErrorStr());
+ if (retval == 0) THROW_ARG(tj3GetErrorStr(NULL));
+ if (retval > (size_t)INT_MAX)
+ THROW_ARG("Image is too large");
bailout:
- return retval;
+ return (jint)retval;
}
/* TurboJPEG 1.4.x: TJ::planeWidth() */
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeWidth__III
(JNIEnv *env, jclass cls, jint componentID, jint width, jint subsamp)
{
- jint retval = (jint)tjPlaneWidth(componentID, width, subsamp);
+ jint retval = (jint)tj3YUVPlaneWidth(componentID, width, subsamp);
- if (retval == -1) THROW_ARG(tjGetErrorStr());
+ if (retval == 0) THROW_ARG(tj3GetErrorStr(NULL));
bailout:
return retval;
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeHeight__III
(JNIEnv *env, jclass cls, jint componentID, jint height, jint subsamp)
{
- jint retval = (jint)tjPlaneHeight(componentID, height, subsamp);
+ jint retval = (jint)tj3YUVPlaneHeight(componentID, height, subsamp);
- if (retval == -1) THROW_ARG(tjGetErrorStr());
+ if (retval == 0) THROW_ARG(tj3GetErrorStr(NULL));
bailout:
return retval;
jfieldID fid;
tjhandle handle;
- if ((handle = tjInitCompress()) == NULL)
- THROW(tjGetErrorStr(), "org/libjpegturbo/turbojpeg/TJException");
+ if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL)
+ THROW(tj3GetErrorStr(NULL), "org/libjpegturbo/turbojpeg/TJException");
BAILIF0(cls = (*env)->GetObjectClass(env, obj));
BAILIF0(fid = (*env)->GetFieldID(env, cls, "handle", "J"));
return;
}
+/* TurboJPEG 3: TJCompressor::set() */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_set
+ (JNIEnv *env, jobject obj, jint param, jint value)
+{
+ tjhandle handle = 0;
+
+ GET_HANDLE();
+
+ if (tj3Set(handle, param, value) == -1)
+ THROW_TJ();
+
+bailout:
+ return;
+}
+
+/* TurboJPEG 3: TJCompressor::get() */
+JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_get
+ (JNIEnv *env, jobject obj, jint param)
+{
+ tjhandle handle = 0;
+
+ GET_HANDLE();
+
+ return tj3Get(handle, param);
+
+bailout:
+ return -1;
+}
+
static jint TJCompressor_compress
- (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint x, jint y,
- jint width, jint pitch, jint height, jint pf, jbyteArray dst,
- jint jpegSubsamp, jint jpegQual, jint flags)
+ (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint precision,
+ jint x, jint y, jint width, jint pitch, jint height, jint pf,
+ jbyteArray dst)
{
tjhandle handle = 0;
- unsigned long jpegSize = 0;
+ size_t jpegSize = 0;
jsize arraySize = 0, actualPitch;
- unsigned char *srcBuf = NULL, *jpegBuf = NULL;
+ void *srcBuf = NULL;
+ unsigned char *jpegBuf = NULL;
+ int jpegSubsamp;
GET_HANDLE();
if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF || width < 1 ||
height < 1 || pitch < 0)
- THROW_ARG("Invalid argument in compress()");
+ THROW_ARG("Invalid argument in compress*()");
if (org_libjpegturbo_turbojpeg_TJ_NUMPF != TJ_NUMPF)
THROW_ARG("Mismatch between Java and C API");
arraySize = (y + height - 1) * actualPitch + (x + width) * tjPixelSize[pf];
if ((*env)->GetArrayLength(env, src) * srcElementSize < arraySize)
THROW_ARG("Source buffer is not large enough");
- jpegSize = tjBufSize(width, height, jpegSubsamp);
+ jpegSubsamp = tj3Get(handle, TJPARAM_SUBSAMP);
+ if (tj3Get(handle, TJPARAM_LOSSLESS) && jpegSubsamp != TJSAMP_GRAY)
+ jpegSubsamp = TJSAMP_444;
+ else if (jpegSubsamp == TJSAMP_UNKNOWN)
+ THROW_ARG("TJPARAM_SUBSAMP must be specified");
+ jpegSize = tj3JPEGBufSize(width, height, jpegSubsamp);
if ((*env)->GetArrayLength(env, dst) < (jsize)jpegSize)
THROW_ARG("Destination buffer is not large enough");
- if (ProcessSystemProperties(env) < 0) goto bailout;
+ if (tj3Set(handle, TJPARAM_NOREALLOC, 1) == -1)
+ THROW_TJ();
BAILIF0NOEC(srcBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
BAILIF0NOEC(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, dst, 0));
- if (tjCompress2(handle, &srcBuf[y * actualPitch + x * tjPixelSize[pf]],
- width, pitch, height, pf, &jpegBuf, &jpegSize, jpegSubsamp,
- jpegQual, flags | TJFLAG_NOREALLOC) == -1) {
- SAFE_RELEASE(dst, jpegBuf);
- SAFE_RELEASE(src, srcBuf);
- THROW_TJ();
+ if (precision == 8) {
+ if (tj3Compress8(handle, &((unsigned char *)srcBuf)[y * actualPitch +
+ x * tjPixelSize[pf]],
+ width, pitch, height, pf, &jpegBuf, &jpegSize) == -1) {
+ SAFE_RELEASE(dst, jpegBuf);
+ SAFE_RELEASE(src, srcBuf);
+ THROW_TJ();
+ }
+ } else if (precision == 12) {
+ if (tj3Compress12(handle, &((short *)srcBuf)[y * actualPitch +
+ x * tjPixelSize[pf]],
+ width, pitch, height, pf, &jpegBuf, &jpegSize) == -1) {
+ SAFE_RELEASE(dst, jpegBuf);
+ SAFE_RELEASE(src, srcBuf);
+ THROW_TJ();
+ }
+ } else {
+ if (tj3Compress16(handle, &((unsigned short *)srcBuf)[y * actualPitch +
+ x * tjPixelSize[pf]],
+ width, pitch, height, pf, &jpegBuf, &jpegSize) == -1) {
+ SAFE_RELEASE(dst, jpegBuf);
+ SAFE_RELEASE(src, srcBuf);
+ THROW_TJ();
+ }
}
bailout:
return (jint)jpegSize;
}
-/* TurboJPEG 1.3.x: TJCompressor::compress() byte source */
-JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIIIII_3BIII
+/* TurboJPEG 3: TJCompressor::compress8() byte source */
+JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress8___3BIIIIII_3B
(JNIEnv *env, jobject obj, jbyteArray src, jint x, jint y, jint width,
- jint pitch, jint height, jint pf, jbyteArray dst, jint jpegSubsamp,
- jint jpegQual, jint flags)
+ jint pitch, jint height, jint pf, jbyteArray dst)
{
- return TJCompressor_compress(env, obj, src, 1, x, y, width, pitch, height,
- pf, dst, jpegSubsamp, jpegQual, flags);
+ return TJCompressor_compress(env, obj, src, 1, 8, x, y, width, pitch, height,
+ pf, dst);
}
-/* TurboJPEG 1.2.x: TJCompressor::compress() byte source */
-JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIII_3BIII
- (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch,
- jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual,
- jint flags)
+/* TurboJPEG 3: TJCompressor::compress12() */
+JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress12
+ (JNIEnv *env, jobject obj, jshortArray src, jint x, jint y, jint width,
+ jint pitch, jint height, jint pf, jbyteArray dst)
{
- return TJCompressor_compress(env, obj, src, 1, 0, 0, width, pitch, height,
- pf, dst, jpegSubsamp, jpegQual, flags);
+ return TJCompressor_compress(env, obj, src, 1, 12, x, y, width, pitch,
+ height, pf, dst);
}
-/* TurboJPEG 1.3.x: TJCompressor::compress() int source */
-JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIIIII_3BIII
- (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width,
- jint stride, jint height, jint pf, jbyteArray dst, jint jpegSubsamp,
- jint jpegQual, jint flags)
+/* TurboJPEG 3: TJCompressor::compress16() */
+JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress16
+ (JNIEnv *env, jobject obj, jshortArray src, jint x, jint y, jint width,
+ jint pitch, jint height, jint pf, jbyteArray dst)
{
- if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
- THROW_ARG("Invalid argument in compress()");
- if (tjPixelSize[pf] != sizeof(jint))
- THROW_ARG("Pixel format must be 32-bit when compressing from an integer buffer.");
-
- return TJCompressor_compress(env, obj, src, sizeof(jint), x, y, width,
- stride * sizeof(jint), height, pf, dst,
- jpegSubsamp, jpegQual, flags);
-
-bailout:
- return 0;
+ return TJCompressor_compress(env, obj, src, 1, 16, x, y, width, pitch,
+ height, pf, dst);
}
-/* TurboJPEG 1.2.x: TJCompressor::compress() int source */
-JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIII_3BIII
- (JNIEnv *env, jobject obj, jintArray src, jint width, jint stride,
- jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual,
- jint flags)
+/* TurboJPEG 3: TJCompressor::compress8() int source */
+JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress8___3IIIIIII_3B
+ (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width,
+ jint stride, jint height, jint pf, jbyteArray dst)
{
if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
- THROW_ARG("Invalid argument in compress()");
+ THROW_ARG("Invalid argument in compress8()");
if (tjPixelSize[pf] != sizeof(jint))
THROW_ARG("Pixel format must be 32-bit when compressing from an integer buffer.");
- return TJCompressor_compress(env, obj, src, sizeof(jint), 0, 0, width,
- stride * sizeof(jint), height, pf, dst,
- jpegSubsamp, jpegQual, flags);
+ return TJCompressor_compress(env, obj, src, sizeof(jint), 8, x, y, width,
+ stride * sizeof(jint), height, pf, dst);
bailout:
return 0;
}
-/* TurboJPEG 1.4.x: TJCompressor::compressFromYUV() */
-JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compressFromYUV___3_3B_3II_3III_3BII
+/* TurboJPEG 3: TJCompressor::compressFromYUV8() */
+JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compressFromYUV8
(JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
- jint width, jintArray jSrcStrides, jint height, jint subsamp,
- jbyteArray dst, jint jpegQual, jint flags)
+ jint width, jintArray jSrcStrides, jint height, jbyteArray dst)
{
tjhandle handle = 0;
- unsigned long jpegSize = 0;
+ size_t jpegSize = 0;
jbyteArray jSrcPlanes[3] = { NULL, NULL, NULL };
const unsigned char *srcPlanesTmp[3] = { NULL, NULL, NULL };
const unsigned char *srcPlanes[3] = { NULL, NULL, NULL };
+ jint srcOffsetsTmp[3] = { 0, 0, 0 }, srcStridesTmp[3] = { 0, 0, 0 };
int srcOffsets[3] = { 0, 0, 0 }, srcStrides[3] = { 0, 0, 0 };
unsigned char *jpegBuf = NULL;
- int nc = (subsamp == org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY ? 1 : 3), i;
+ int nc = 0, i, subsamp;
GET_HANDLE();
- if (subsamp < 0 || subsamp >= org_libjpegturbo_turbojpeg_TJ_NUMSAMP)
- THROW_ARG("Invalid argument in compressFromYUV()");
if (org_libjpegturbo_turbojpeg_TJ_NUMSAMP != TJ_NUMSAMP)
THROW_ARG("Mismatch between Java and C API");
+ if ((subsamp = tj3Get(handle, TJPARAM_SUBSAMP)) == TJSAMP_UNKNOWN)
+ THROW_ARG("TJPARAM_SUBSAMP must be specified");
+ nc = subsamp == TJSAMP_GRAY ? 1 : 3;
if ((*env)->GetArrayLength(env, srcobjs) < nc)
THROW_ARG("Planes array is too small for the subsampling type");
if ((*env)->GetArrayLength(env, jSrcOffsets) < nc)
if ((*env)->GetArrayLength(env, jSrcStrides) < nc)
THROW_ARG("Strides array is too small for the subsampling type");
- jpegSize = tjBufSize(width, height, subsamp);
+ jpegSize = tj3JPEGBufSize(width, height, subsamp);
if ((*env)->GetArrayLength(env, dst) < (jsize)jpegSize)
THROW_ARG("Destination buffer is not large enough");
- if (ProcessSystemProperties(env) < 0) goto bailout;
+ if (tj3Set(handle, TJPARAM_NOREALLOC, 1) == -1)
+ THROW_TJ();
- (*env)->GetIntArrayRegion(env, jSrcOffsets, 0, nc, srcOffsets);
+ (*env)->GetIntArrayRegion(env, jSrcOffsets, 0, nc, srcOffsetsTmp);
if ((*env)->ExceptionCheck(env)) goto bailout;
+ for (i = 0; i < 3; i++)
+ srcOffsets[i] = srcOffsetsTmp[i];
- (*env)->GetIntArrayRegion(env, jSrcStrides, 0, nc, srcStrides);
+ (*env)->GetIntArrayRegion(env, jSrcStrides, 0, nc, srcStridesTmp);
if ((*env)->ExceptionCheck(env)) goto bailout;
+ for (i = 0; i < 3; i++)
+ srcStrides[i] = srcStridesTmp[i];
for (i = 0; i < nc; i++) {
- int planeSize = tjPlaneSizeYUV(i, width, srcStrides[i], height, subsamp);
- int pw = tjPlaneWidth(i, width, subsamp);
+ size_t planeSize = tj3YUVPlaneSize(i, width, srcStrides[i], height,
+ subsamp);
+ int pw = tj3YUVPlaneWidth(i, width, subsamp);
- if (planeSize < 0 || pw < 0)
- THROW_ARG(tjGetErrorStr());
+ if (planeSize == 0 || pw == 0)
+ THROW_ARG(tj3GetErrorStr(NULL));
+ if (planeSize > (size_t)INT_MAX)
+ THROW_ARG("Source plane is too large");
if (srcOffsets[i] < 0)
- THROW_ARG("Invalid argument in compressFromYUV()");
- if (srcStrides[i] < 0 && srcOffsets[i] - planeSize + pw < 0)
+ THROW_ARG("Invalid argument in compressFromYUV8()");
+ if (srcStrides[i] < 0 && srcOffsets[i] - (int)planeSize + pw < 0)
THROW_ARG("Negative plane stride would cause memory to be accessed below plane boundary");
BAILIF0(jSrcPlanes[i] = (*env)->GetObjectArrayElement(env, srcobjs, i));
if ((*env)->GetArrayLength(env, jSrcPlanes[i]) <
- srcOffsets[i] + planeSize)
+ srcOffsets[i] + (int)planeSize)
THROW_ARG("Source plane is not large enough");
}
for (i = 0; i < nc; i++) {
}
BAILIF0NOEC(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, dst, 0));
- if (tjCompressFromYUVPlanes(handle, srcPlanes, width, srcStrides, height,
- subsamp, &jpegBuf, &jpegSize, jpegQual,
- flags | TJFLAG_NOREALLOC) == -1) {
+ if (tj3CompressFromYUVPlanes8(handle, srcPlanes, width, srcStrides, height,
+ &jpegBuf, &jpegSize) == -1) {
SAFE_RELEASE(dst, jpegBuf);
for (i = 0; i < nc; i++)
SAFE_RELEASE(jSrcPlanes[i], srcPlanesTmp[i]);
return (jint)jpegSize;
}
-static void TJCompressor_encodeYUV
+static void TJCompressor_encodeYUV8
(JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint x, jint y,
jint width, jint pitch, jint height, jint pf, jobjectArray dstobjs,
- jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags)
+ jintArray jDstOffsets, jintArray jDstStrides)
{
tjhandle handle = 0;
jsize arraySize = 0, actualPitch;
jbyteArray jDstPlanes[3] = { NULL, NULL, NULL };
unsigned char *dstPlanesTmp[3] = { NULL, NULL, NULL };
unsigned char *dstPlanes[3] = { NULL, NULL, NULL };
+ jint dstOffsetsTmp[3] = { 0, 0, 0 }, dstStridesTmp[3] = { 0, 0, 0 };
int dstOffsets[3] = { 0, 0, 0 }, dstStrides[3] = { 0, 0, 0 };
- int nc = (subsamp == org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY ? 1 : 3), i;
+ int nc = 0, i, subsamp;
GET_HANDLE();
if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF || width < 1 ||
- height < 1 || pitch < 0 || subsamp < 0 ||
- subsamp >= org_libjpegturbo_turbojpeg_TJ_NUMSAMP)
- THROW_ARG("Invalid argument in encodeYUV()");
+ height < 1 || pitch < 0)
+ THROW_ARG("Invalid argument in encodeYUV8()");
if (org_libjpegturbo_turbojpeg_TJ_NUMPF != TJ_NUMPF ||
org_libjpegturbo_turbojpeg_TJ_NUMSAMP != TJ_NUMSAMP)
THROW_ARG("Mismatch between Java and C API");
+ if ((subsamp = tj3Get(handle, TJPARAM_SUBSAMP)) == TJSAMP_UNKNOWN)
+ THROW_ARG("TJPARAM_SUBSAMP must be specified");
+ nc = subsamp == TJSAMP_GRAY ? 1 : 3;
if ((*env)->GetArrayLength(env, dstobjs) < nc)
THROW_ARG("Planes array is too small for the subsampling type");
if ((*env)->GetArrayLength(env, jDstOffsets) < nc)
if ((*env)->GetArrayLength(env, src) * srcElementSize < arraySize)
THROW_ARG("Source buffer is not large enough");
- (*env)->GetIntArrayRegion(env, jDstOffsets, 0, nc, dstOffsets);
+ (*env)->GetIntArrayRegion(env, jDstOffsets, 0, nc, dstOffsetsTmp);
if ((*env)->ExceptionCheck(env)) goto bailout;
+ for (i = 0; i < 3; i++)
+ dstOffsets[i] = dstOffsetsTmp[i];
- (*env)->GetIntArrayRegion(env, jDstStrides, 0, nc, dstStrides);
+ (*env)->GetIntArrayRegion(env, jDstStrides, 0, nc, dstStridesTmp);
if ((*env)->ExceptionCheck(env)) goto bailout;
+ for (i = 0; i < 3; i++)
+ dstStrides[i] = dstStridesTmp[i];
for (i = 0; i < nc; i++) {
- int planeSize = tjPlaneSizeYUV(i, width, dstStrides[i], height, subsamp);
- int pw = tjPlaneWidth(i, width, subsamp);
+ size_t planeSize = tj3YUVPlaneSize(i, width, dstStrides[i], height,
+ subsamp);
+ int pw = tj3YUVPlaneWidth(i, width, subsamp);
- if (planeSize < 0 || pw < 0)
- THROW_ARG(tjGetErrorStr());
+ if (planeSize == 0 || pw == 0)
+ THROW_ARG(tj3GetErrorStr(NULL));
+ if (planeSize > (size_t)INT_MAX)
+ THROW_ARG("Destination plane is too large");
if (dstOffsets[i] < 0)
- THROW_ARG("Invalid argument in encodeYUV()");
- if (dstStrides[i] < 0 && dstOffsets[i] - planeSize + pw < 0)
+ THROW_ARG("Invalid argument in encodeYUV8()");
+ if (dstStrides[i] < 0 && dstOffsets[i] - (int)planeSize + pw < 0)
THROW_ARG("Negative plane stride would cause memory to be accessed below plane boundary");
BAILIF0(jDstPlanes[i] = (*env)->GetObjectArrayElement(env, dstobjs, i));
if ((*env)->GetArrayLength(env, jDstPlanes[i]) <
- dstOffsets[i] + planeSize)
+ dstOffsets[i] + (int)planeSize)
THROW_ARG("Destination plane is not large enough");
}
for (i = 0; i < nc; i++) {
}
BAILIF0NOEC(srcBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
- if (tjEncodeYUVPlanes(handle, &srcBuf[y * actualPitch + x * tjPixelSize[pf]],
- width, pitch, height, pf, dstPlanes, dstStrides,
- subsamp, flags) == -1) {
+ if (tj3EncodeYUVPlanes8(handle,
+ &srcBuf[y * actualPitch + x * tjPixelSize[pf]],
+ width, pitch, height, pf, dstPlanes,
+ dstStrides) == -1) {
SAFE_RELEASE(src, srcBuf);
for (i = 0; i < nc; i++)
SAFE_RELEASE(jDstPlanes[i], dstPlanesTmp[i]);
SAFE_RELEASE(jDstPlanes[i], dstPlanesTmp[i]);
}
-/* TurboJPEG 1.4.x: TJCompressor::encodeYUV() byte source */
-JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIIIII_3_3B_3I_3III
+/* TurboJPEG 3: TJCompressor::encodeYUV8() byte source */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV8___3BIIIIII_3_3B_3I_3I
(JNIEnv *env, jobject obj, jbyteArray src, jint x, jint y, jint width,
jint pitch, jint height, jint pf, jobjectArray dstobjs,
- jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags)
+ jintArray jDstOffsets, jintArray jDstStrides)
{
- TJCompressor_encodeYUV(env, obj, src, 1, x, y, width, pitch, height, pf,
- dstobjs, jDstOffsets, jDstStrides, subsamp, flags);
+ TJCompressor_encodeYUV8(env, obj, src, 1, x, y, width, pitch, height, pf,
+ dstobjs, jDstOffsets, jDstStrides);
}
-/* TurboJPEG 1.4.x: TJCompressor::encodeYUV() int source */
-JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIIIII_3_3B_3I_3III
+/* TurboJPEG 3: TJCompressor::encodeYUV8() int source */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV8___3IIIIIII_3_3B_3I_3I
(JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width,
jint stride, jint height, jint pf, jobjectArray dstobjs,
- jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags)
+ jintArray jDstOffsets, jintArray jDstStrides)
{
if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
- THROW_ARG("Invalid argument in encodeYUV()");
+ THROW_ARG("Invalid argument in encodeYUV8()");
if (tjPixelSize[pf] != sizeof(jint))
THROW_ARG("Pixel format must be 32-bit when encoding from an integer buffer.");
- TJCompressor_encodeYUV(env, obj, src, sizeof(jint), x, y, width,
- stride * sizeof(jint), height, pf, dstobjs,
- jDstOffsets, jDstStrides, subsamp, flags);
-
-bailout:
- return;
-}
-
-static void JNICALL TJCompressor_encodeYUV_12
- (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint width,
- jint pitch, jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
-{
- tjhandle handle = 0;
- jsize arraySize = 0;
- unsigned char *srcBuf = NULL, *dstBuf = NULL;
-
- GET_HANDLE();
-
- if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF || width < 1 ||
- height < 1 || pitch < 0)
- THROW_ARG("Invalid argument in encodeYUV()");
- if (org_libjpegturbo_turbojpeg_TJ_NUMPF != TJ_NUMPF)
- THROW_ARG("Mismatch between Java and C API");
-
- arraySize = (pitch == 0) ? width * tjPixelSize[pf] * height : pitch * height;
- if ((*env)->GetArrayLength(env, src) * srcElementSize < arraySize)
- THROW_ARG("Source buffer is not large enough");
- if ((*env)->GetArrayLength(env, dst) <
- (jsize)tjBufSizeYUV(width, height, subsamp))
- THROW_ARG("Destination buffer is not large enough");
-
- BAILIF0NOEC(srcBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
- BAILIF0NOEC(dstBuf = (*env)->GetPrimitiveArrayCritical(env, dst, 0));
-
- if (tjEncodeYUV2(handle, srcBuf, width, pitch, height, pf, dstBuf, subsamp,
- flags) == -1) {
- SAFE_RELEASE(dst, dstBuf);
- SAFE_RELEASE(src, srcBuf);
- THROW_TJ();
- }
-
-bailout:
- SAFE_RELEASE(dst, dstBuf);
- SAFE_RELEASE(src, srcBuf);
-}
-
-/* TurboJPEG 1.2.x: TJCompressor::encodeYUV() byte source */
-JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIII_3BII
- (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch,
- jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
-{
- TJCompressor_encodeYUV_12(env, obj, src, 1, width, pitch, height, pf, dst,
- subsamp, flags);
-}
-
-/* TurboJPEG 1.2.x: TJCompressor::encodeYUV() int source */
-JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIII_3BII
- (JNIEnv *env, jobject obj, jintArray src, jint width, jint stride,
- jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
-{
- if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
- THROW_ARG("Invalid argument in encodeYUV()");
- if (tjPixelSize[pf] != sizeof(jint))
- THROW_ARG("Pixel format must be 32-bit when encoding from an integer buffer.");
-
- TJCompressor_encodeYUV_12(env, obj, src, sizeof(jint), width,
- stride * sizeof(jint), height, pf, dst, subsamp,
- flags);
+ TJCompressor_encodeYUV8(env, obj, src, sizeof(jint), x, y, width,
+ stride * sizeof(jint), height, pf, dstobjs,
+ jDstOffsets, jDstStrides);
bailout:
return;
GET_HANDLE();
- if (tjDestroy(handle) == -1) THROW_TJ();
+ tj3Destroy(handle);
(*env)->SetLongField(env, obj, _fid, 0);
bailout:
jfieldID fid;
tjhandle handle;
- if ((handle = tjInitDecompress()) == NULL)
- THROW(tjGetErrorStr(), "org/libjpegturbo/turbojpeg/TJException");
+ if ((handle = tj3Init(TJINIT_DECOMPRESS)) == NULL)
+ THROW(tj3GetErrorStr(NULL), "org/libjpegturbo/turbojpeg/TJException");
BAILIF0(cls = (*env)->GetObjectClass(env, obj));
BAILIF0(fid = (*env)->GetFieldID(env, cls, "handle", "J"));
return;
}
+/* TurboJPEG 3: TJDecompressor::set() */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_set
+ (JNIEnv *env, jobject obj, jint param, jint value)
+{
+ Java_org_libjpegturbo_turbojpeg_TJCompressor_set(env, obj, param, value);
+}
+
+/* TurboJPEG 3: TJDecompressor::get() */
+JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_get
+ (JNIEnv *env, jobject obj, jint param)
+{
+ return Java_org_libjpegturbo_turbojpeg_TJCompressor_get(env, obj, param);
+}
+
/* TurboJPEG 1.2.x: TJDecompressor::getScalingFactors() */
JNIEXPORT jobjectArray JNICALL Java_org_libjpegturbo_turbojpeg_TJ_getScalingFactors
(JNIEnv *env, jclass cls)
jobject sfobj = NULL;
jobjectArray sfjava = NULL;
- if ((sf = tjGetScalingFactors(&n)) == NULL || n == 0)
- THROW_ARG(tjGetErrorStr());
+ if ((sf = tj3GetScalingFactors(&n)) == NULL || n == 0)
+ THROW_ARG(tj3GetErrorStr(NULL));
BAILIF0(sfcls = (*env)->FindClass(env,
"org/libjpegturbo/turbojpeg/TJScalingFactor"));
{
tjhandle handle = 0;
unsigned char *jpegBuf = NULL;
- int width = 0, height = 0, jpegSubsamp = -1, jpegColorspace = -1;
GET_HANDLE();
BAILIF0NOEC(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
- if (tjDecompressHeader3(handle, jpegBuf, (unsigned long)jpegSize, &width,
- &height, &jpegSubsamp, &jpegColorspace) == -1) {
+ if (tj3DecompressHeader(handle, jpegBuf, (size_t)jpegSize) == -1) {
SAFE_RELEASE(src, jpegBuf);
THROW_TJ();
}
+bailout:
SAFE_RELEASE(src, jpegBuf);
+}
- BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
- (*env)->SetIntField(env, obj, _fid, jpegSubsamp);
- if ((_fid = (*env)->GetFieldID(env, _cls, "jpegColorspace", "I")) == 0)
- (*env)->ExceptionClear(env);
- else
- (*env)->SetIntField(env, obj, _fid, jpegColorspace);
- BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
- (*env)->SetIntField(env, obj, _fid, width);
- BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
- (*env)->SetIntField(env, obj, _fid, height);
+/* TurboJPEG 3: TJDecompressor::setCroppingRegion() */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_setCroppingRegion
+ (JNIEnv *env, jobject obj)
+{
+ tjhandle handle = 0;
+ jclass sfcls, crcls;
+ jobject sfobj, crobj;
+ tjregion croppingRegion;
+ tjscalingfactor scalingFactor;
+
+ GET_HANDLE();
+
+ BAILIF0(sfcls = (*env)->FindClass(env,
+ "org/libjpegturbo/turbojpeg/TJScalingFactor"));
+ BAILIF0(_fid =
+ (*env)->GetFieldID(env, _cls, "scalingFactor",
+ "Lorg/libjpegturbo/turbojpeg/TJScalingFactor;"));
+ BAILIF0(sfobj = (*env)->GetObjectField(env, obj, _fid));
+ BAILIF0(_fid = (*env)->GetFieldID(env, sfcls, "num", "I"));
+ scalingFactor.num = (*env)->GetIntField(env, sfobj, _fid);
+ BAILIF0(_fid = (*env)->GetFieldID(env, sfcls, "denom", "I"));
+ scalingFactor.denom = (*env)->GetIntField(env, sfobj, _fid);
+
+ if (tj3SetScalingFactor(handle, scalingFactor) == -1)
+ THROW_TJ();
+
+ BAILIF0(crcls = (*env)->FindClass(env, "java/awt/Rectangle"));
+ BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "croppingRegion",
+ "Ljava/awt/Rectangle;"));
+ BAILIF0(crobj = (*env)->GetObjectField(env, obj, _fid));
+ BAILIF0(_fid = (*env)->GetFieldID(env, crcls, "x", "I"));
+ croppingRegion.x = (*env)->GetIntField(env, crobj, _fid);
+ BAILIF0(_fid = (*env)->GetFieldID(env, crcls, "y", "I"));
+ croppingRegion.y = (*env)->GetIntField(env, crobj, _fid);
+ BAILIF0(_fid = (*env)->GetFieldID(env, crcls, "width", "I"));
+ croppingRegion.w = (*env)->GetIntField(env, crobj, _fid);
+ BAILIF0(_fid = (*env)->GetFieldID(env, crcls, "height", "I"));
+ croppingRegion.h = (*env)->GetIntField(env, crobj, _fid);
+
+ if (tj3SetCroppingRegion(handle, croppingRegion) == -1)
+ THROW_TJ();
bailout:
- SAFE_RELEASE(src, jpegBuf);
+ return;
}
static void TJDecompressor_decompress
(JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jarray dst,
- jint dstElementSize, jint x, jint y, jint width, jint pitch, jint height,
- jint pf, jint flags)
+ jint dstElementSize, int precision, jint x, jint y, jint pitch, jint pf)
{
tjhandle handle = 0;
jsize arraySize = 0, actualPitch;
- unsigned char *jpegBuf = NULL, *dstBuf = NULL;
+ unsigned char *jpegBuf = NULL;
+ void *dstBuf = NULL;
+ jclass sfcls, crcls;
+ jobject sfobj, crobj;
+ tjscalingfactor scalingFactor;
+ tjregion cr;
+ int jpegWidth, jpegHeight, scaledWidth, scaledHeight;
GET_HANDLE();
if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
- THROW_ARG("Invalid argument in decompress()");
+ THROW_ARG("Invalid argument in decompress*()");
if (org_libjpegturbo_turbojpeg_TJ_NUMPF != TJ_NUMPF)
THROW_ARG("Mismatch between Java and C API");
if ((*env)->GetArrayLength(env, src) < jpegSize)
THROW_ARG("Source buffer is not large enough");
- actualPitch = (pitch == 0) ? width * tjPixelSize[pf] : pitch;
- arraySize = (y + height - 1) * actualPitch + (x + width) * tjPixelSize[pf];
+ if ((jpegWidth = tj3Get(handle, TJPARAM_JPEGWIDTH)) == -1)
+ THROW_ARG("JPEG header has not yet been read");
+ if ((jpegHeight = tj3Get(handle, TJPARAM_JPEGHEIGHT)) == -1)
+ THROW_ARG("JPEG header has not yet been read");
+
+ BAILIF0(sfcls = (*env)->FindClass(env,
+ "org/libjpegturbo/turbojpeg/TJScalingFactor"));
+ BAILIF0(_fid =
+ (*env)->GetFieldID(env, _cls, "scalingFactor",
+ "Lorg/libjpegturbo/turbojpeg/TJScalingFactor;"));
+ BAILIF0(sfobj = (*env)->GetObjectField(env, obj, _fid));
+ BAILIF0(_fid = (*env)->GetFieldID(env, sfcls, "num", "I"));
+ scalingFactor.num = (*env)->GetIntField(env, sfobj, _fid);
+ BAILIF0(_fid = (*env)->GetFieldID(env, sfcls, "denom", "I"));
+ scalingFactor.denom = (*env)->GetIntField(env, sfobj, _fid);
+
+ if (tj3SetScalingFactor(handle, scalingFactor) == -1)
+ THROW_TJ();
+ scaledWidth = TJSCALED(jpegWidth, scalingFactor);
+ scaledHeight = TJSCALED(jpegHeight, scalingFactor);
+
+ BAILIF0(crcls = (*env)->FindClass(env, "java/awt/Rectangle"));
+ BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "croppingRegion",
+ "Ljava/awt/Rectangle;"));
+ BAILIF0(crobj = (*env)->GetObjectField(env, obj, _fid));
+ BAILIF0(_fid = (*env)->GetFieldID(env, crcls, "x", "I"));
+ cr.x = (*env)->GetIntField(env, crobj, _fid);
+ BAILIF0(_fid = (*env)->GetFieldID(env, crcls, "y", "I"));
+ cr.y = (*env)->GetIntField(env, crobj, _fid);
+ BAILIF0(_fid = (*env)->GetFieldID(env, crcls, "width", "I"));
+ cr.w = (*env)->GetIntField(env, crobj, _fid);
+ BAILIF0(_fid = (*env)->GetFieldID(env, crcls, "height", "I"));
+ cr.h = (*env)->GetIntField(env, crobj, _fid);
+ if (cr.x != 0 || cr.y != 0 || cr.w != 0 || cr.h != 0) {
+ scaledWidth = cr.w ? cr.w : scaledWidth - cr.x;
+ scaledHeight = cr.h ? cr.h : scaledHeight - cr.y;
+ }
+
+ actualPitch = (pitch == 0) ? scaledWidth * tjPixelSize[pf] : pitch;
+ arraySize = (y + scaledHeight - 1) * actualPitch +
+ (x + scaledWidth) * tjPixelSize[pf];
if ((*env)->GetArrayLength(env, dst) * dstElementSize < arraySize)
THROW_ARG("Destination buffer is not large enough");
BAILIF0NOEC(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
BAILIF0NOEC(dstBuf = (*env)->GetPrimitiveArrayCritical(env, dst, 0));
- if (tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize,
- &dstBuf[y * actualPitch + x * tjPixelSize[pf]], width,
- pitch, height, pf, flags) == -1) {
- SAFE_RELEASE(dst, dstBuf);
- SAFE_RELEASE(src, jpegBuf);
- THROW_TJ();
+ if (precision == 8) {
+ if (tj3Decompress8(handle, jpegBuf, (size_t)jpegSize,
+ &((unsigned char *)dstBuf)[y * actualPitch +
+ x * tjPixelSize[pf]],
+ pitch, pf) == -1) {
+ SAFE_RELEASE(dst, dstBuf);
+ SAFE_RELEASE(src, jpegBuf);
+ THROW_TJ();
+ }
+ } else if (precision == 12) {
+ if (tj3Decompress12(handle, jpegBuf, (size_t)jpegSize,
+ &((short *)dstBuf)[y * actualPitch +
+ x * tjPixelSize[pf]],
+ pitch, pf) == -1) {
+ SAFE_RELEASE(dst, dstBuf);
+ SAFE_RELEASE(src, jpegBuf);
+ THROW_TJ();
+ }
+ } else {
+ if (tj3Decompress16(handle, jpegBuf, (size_t)jpegSize,
+ &((unsigned short *)dstBuf)[y * actualPitch +
+ x * tjPixelSize[pf]],
+ pitch, pf) == -1) {
+ SAFE_RELEASE(dst, dstBuf);
+ SAFE_RELEASE(src, jpegBuf);
+ THROW_TJ();
+ }
}
bailout:
SAFE_RELEASE(src, jpegBuf);
}
-/* TurboJPEG 1.3.x: TJDecompressor::decompress() byte destination */
-JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIIIII
+/* TurboJPEG 3: TJDecompressor::decompress8() byte destination */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress8___3BI_3BIIII
(JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
- jint x, jint y, jint width, jint pitch, jint height, jint pf, jint flags)
+ jint x, jint y, jint pitch, jint pf)
{
- TJDecompressor_decompress(env, obj, src, jpegSize, dst, 1, x, y, width,
- pitch, height, pf, flags);
+ TJDecompressor_decompress(env, obj, src, jpegSize, dst, 1, 8, x, y, pitch,
+ pf);
}
-/* TurboJPEG 1.2.x: TJDecompressor::decompress() byte destination */
-JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIII
- (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
- jint width, jint pitch, jint height, jint pf, jint flags)
+/* TurboJPEG 3: TJDecompressor::decompress12() */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress12
+ (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jshortArray dst,
+ jint x, jint y, jint pitch, jint pf)
{
- TJDecompressor_decompress(env, obj, src, jpegSize, dst, 1, 0, 0, width,
- pitch, height, pf, flags);
+ TJDecompressor_decompress(env, obj, src, jpegSize, dst, 1, 12, x, y, pitch,
+ pf);
}
-/* TurboJPEG 1.3.x: TJDecompressor::decompress() int destination */
-JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIIIII
- (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst,
- jint x, jint y, jint width, jint stride, jint height, jint pf, jint flags)
+/* TurboJPEG 3: TJDecompressor::decompress16() */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress16
+ (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jshortArray dst,
+ jint x, jint y, jint pitch, jint pf)
{
- if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
- THROW_ARG("Invalid argument in decompress()");
- if (tjPixelSize[pf] != sizeof(jint))
- THROW_ARG("Pixel format must be 32-bit when decompressing to an integer buffer.");
-
- TJDecompressor_decompress(env, obj, src, jpegSize, dst, sizeof(jint), x, y,
- width, stride * sizeof(jint), height, pf, flags);
-
-bailout:
- return;
+ TJDecompressor_decompress(env, obj, src, jpegSize, dst, 1, 16, x, y, pitch,
+ pf);
}
-/* TurboJPEG 1.2.x: TJDecompressor::decompress() int destination */
-JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIII
+/* TurboJPEG 3: TJDecompressor::decompress8() int destination */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress8___3BI_3IIIII
(JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst,
- jint width, jint stride, jint height, jint pf, jint flags)
+ jint x, jint y, jint stride, jint pf)
{
if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
- THROW_ARG("Invalid argument in decompress()");
+ THROW_ARG("Invalid argument in decompress8()");
if (tjPixelSize[pf] != sizeof(jint))
THROW_ARG("Pixel format must be 32-bit when decompressing to an integer buffer.");
- TJDecompressor_decompress(env, obj, src, jpegSize, dst, sizeof(jint), 0, 0,
- width, stride * sizeof(jint), height, pf, flags);
+ TJDecompressor_decompress(env, obj, src, jpegSize, dst, sizeof(jint), 8, x,
+ y, stride * sizeof(jint), pf);
bailout:
return;
}
-/* TurboJPEG 1.4.x: TJDecompressor::decompressToYUV() */
-JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV___3BI_3_3B_3II_3III
+/* TurboJPEG 3: TJDecompressor::decompressToYUV8() */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV8
(JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize,
- jobjectArray dstobjs, jintArray jDstOffsets, jint desiredWidth,
- jintArray jDstStrides, jint desiredHeight, jint flags)
+ jobjectArray dstobjs, jintArray jDstOffsets, jintArray jDstStrides)
{
tjhandle handle = 0;
unsigned char *jpegBuf = NULL;
jbyteArray jDstPlanes[3] = { NULL, NULL, NULL };
unsigned char *dstPlanesTmp[3] = { NULL, NULL, NULL };
unsigned char *dstPlanes[3] = { NULL, NULL, NULL };
+ jint dstOffsetsTmp[3] = { 0, 0, 0 }, dstStridesTmp[3] = { 0, 0, 0 };
int dstOffsets[3] = { 0, 0, 0 }, dstStrides[3] = { 0, 0, 0 };
- int jpegSubsamp = -1, jpegWidth = 0, jpegHeight = 0;
- int nc = 0, i, width, height, scaledWidth, scaledHeight, nsf = 0;
- tjscalingfactor *sf;
+ jclass sfcls;
+ jobject sfobj;
+ int jpegSubsamp, jpegWidth = 0, jpegHeight = 0;
+ int nc = 0, i, scaledWidth, scaledHeight;
+ tjscalingfactor scalingFactor;
GET_HANDLE();
if ((*env)->GetArrayLength(env, src) < jpegSize)
THROW_ARG("Source buffer is not large enough");
- BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
- jpegSubsamp = (int)(*env)->GetIntField(env, obj, _fid);
- BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
- jpegWidth = (int)(*env)->GetIntField(env, obj, _fid);
- BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
- jpegHeight = (int)(*env)->GetIntField(env, obj, _fid);
-
- nc = (jpegSubsamp == org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY ? 1 : 3);
-
- width = desiredWidth;
- height = desiredHeight;
- if (width == 0) width = jpegWidth;
- if (height == 0) height = jpegHeight;
- sf = tjGetScalingFactors(&nsf);
- if (!sf || nsf < 1)
- THROW_ARG(tjGetErrorStr());
- for (i = 0; i < nsf; i++) {
- scaledWidth = TJSCALED(jpegWidth, sf[i]);
- scaledHeight = TJSCALED(jpegHeight, sf[i]);
- if (scaledWidth <= width && scaledHeight <= height)
- break;
- }
- if (i >= nsf)
- THROW_ARG("Could not scale down to desired image dimensions");
+ if ((jpegWidth = tj3Get(handle, TJPARAM_JPEGWIDTH)) == -1)
+ THROW_ARG("JPEG header has not yet been read");
+ if ((jpegHeight = tj3Get(handle, TJPARAM_JPEGHEIGHT)) == -1)
+ THROW_ARG("JPEG header has not yet been read");
- (*env)->GetIntArrayRegion(env, jDstOffsets, 0, nc, dstOffsets);
+ BAILIF0(sfcls = (*env)->FindClass(env,
+ "org/libjpegturbo/turbojpeg/TJScalingFactor"));
+ BAILIF0(_fid =
+ (*env)->GetFieldID(env, _cls, "scalingFactor",
+ "Lorg/libjpegturbo/turbojpeg/TJScalingFactor;"));
+ BAILIF0(sfobj = (*env)->GetObjectField(env, obj, _fid));
+ BAILIF0(_fid = (*env)->GetFieldID(env, sfcls, "num", "I"));
+ scalingFactor.num = (*env)->GetIntField(env, sfobj, _fid);
+ BAILIF0(_fid = (*env)->GetFieldID(env, sfcls, "denom", "I"));
+ scalingFactor.denom = (*env)->GetIntField(env, sfobj, _fid);
+
+ if (tj3SetScalingFactor(handle, scalingFactor) == -1)
+ THROW_TJ();
+ scaledWidth = TJSCALED(jpegWidth, scalingFactor);
+ scaledHeight = TJSCALED(jpegHeight, scalingFactor);
+
+ if ((jpegSubsamp = tj3Get(handle, TJPARAM_SUBSAMP)) == TJSAMP_UNKNOWN)
+ THROW_ARG("TJPARAM_SUBSAMP must be specified");
+ nc = jpegSubsamp == TJSAMP_GRAY ? 1 : 3;
+
+ (*env)->GetIntArrayRegion(env, jDstOffsets, 0, nc, dstOffsetsTmp);
if ((*env)->ExceptionCheck(env)) goto bailout;
+ for (i = 0; i < 3; i++)
+ dstOffsets[i] = dstOffsetsTmp[i];
- (*env)->GetIntArrayRegion(env, jDstStrides, 0, nc, dstStrides);
+ (*env)->GetIntArrayRegion(env, jDstStrides, 0, nc, dstStridesTmp);
if ((*env)->ExceptionCheck(env)) goto bailout;
+ for (i = 0; i < 3; i++)
+ dstStrides[i] = dstStridesTmp[i];
for (i = 0; i < nc; i++) {
- int planeSize = tjPlaneSizeYUV(i, scaledWidth, dstStrides[i], scaledHeight,
- jpegSubsamp);
- int pw = tjPlaneWidth(i, scaledWidth, jpegSubsamp);
+ size_t planeSize = tj3YUVPlaneSize(i, scaledWidth, dstStrides[i],
+ scaledHeight, jpegSubsamp);
+ int pw = tj3YUVPlaneWidth(i, scaledWidth, jpegSubsamp);
- if (planeSize < 0 || pw < 0)
- THROW_ARG(tjGetErrorStr());
+ if (planeSize == 0 || pw == 0)
+ THROW_ARG(tj3GetErrorStr(NULL));
+ if (planeSize > (size_t)INT_MAX)
+ THROW_ARG("Destination plane is too large");
if (dstOffsets[i] < 0)
- THROW_ARG("Invalid argument in decompressToYUV()");
- if (dstStrides[i] < 0 && dstOffsets[i] - planeSize + pw < 0)
+ THROW_ARG("Invalid argument in decompressToYUV8()");
+ if (dstStrides[i] < 0 && dstOffsets[i] - (int)planeSize + pw < 0)
THROW_ARG("Negative plane stride would cause memory to be accessed below plane boundary");
BAILIF0(jDstPlanes[i] = (*env)->GetObjectArrayElement(env, dstobjs, i));
if ((*env)->GetArrayLength(env, jDstPlanes[i]) <
- dstOffsets[i] + planeSize)
+ dstOffsets[i] + (int)planeSize)
THROW_ARG("Destination plane is not large enough");
}
for (i = 0; i < nc; i++) {
}
BAILIF0NOEC(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
- if (tjDecompressToYUVPlanes(handle, jpegBuf, (unsigned long)jpegSize,
- dstPlanes, desiredWidth, dstStrides,
- desiredHeight, flags) == -1) {
+ if (tj3DecompressToYUVPlanes8(handle, jpegBuf, (size_t)jpegSize, dstPlanes,
+ dstStrides) == -1) {
SAFE_RELEASE(src, jpegBuf);
for (i = 0; i < nc; i++)
SAFE_RELEASE(jDstPlanes[i], dstPlanesTmp[i]);
SAFE_RELEASE(jDstPlanes[i], dstPlanesTmp[i]);
}
-/* TurboJPEG 1.2.x: TJDecompressor::decompressToYUV() */
-JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV___3BI_3BI
- (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
- jint flags)
-{
- tjhandle handle = 0;
- unsigned char *jpegBuf = NULL, *dstBuf = NULL;
- int jpegSubsamp = -1, jpegWidth = 0, jpegHeight = 0;
-
- GET_HANDLE();
-
- if ((*env)->GetArrayLength(env, src) < jpegSize)
- THROW_ARG("Source buffer is not large enough");
- BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
- jpegSubsamp = (int)(*env)->GetIntField(env, obj, _fid);
- BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
- jpegWidth = (int)(*env)->GetIntField(env, obj, _fid);
- BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
- jpegHeight = (int)(*env)->GetIntField(env, obj, _fid);
- if ((*env)->GetArrayLength(env, dst) <
- (jsize)tjBufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp))
- THROW_ARG("Destination buffer is not large enough");
-
- BAILIF0NOEC(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
- BAILIF0NOEC(dstBuf = (*env)->GetPrimitiveArrayCritical(env, dst, 0));
-
- if (tjDecompressToYUV(handle, jpegBuf, (unsigned long)jpegSize, dstBuf,
- flags) == -1) {
- SAFE_RELEASE(dst, dstBuf);
- SAFE_RELEASE(src, jpegBuf);
- THROW_TJ();
- }
-
-bailout:
- SAFE_RELEASE(dst, dstBuf);
- SAFE_RELEASE(src, jpegBuf);
-}
-
-static void TJDecompressor_decodeYUV
+static void TJDecompressor_decodeYUV8
(JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
- jintArray jSrcStrides, jint subsamp, jarray dst, jint dstElementSize,
- jint x, jint y, jint width, jint pitch, jint height, jint pf, jint flags)
+ jintArray jSrcStrides, jarray dst, jint dstElementSize, jint x, jint y,
+ jint width, jint pitch, jint height, jint pf)
{
tjhandle handle = 0;
jsize arraySize = 0, actualPitch;
jbyteArray jSrcPlanes[3] = { NULL, NULL, NULL };
const unsigned char *srcPlanesTmp[3] = { NULL, NULL, NULL };
const unsigned char *srcPlanes[3] = { NULL, NULL, NULL };
+ jint srcOffsetsTmp[3] = { 0, 0, 0 }, srcStridesTmp[3] = { 0, 0, 0 };
int srcOffsets[3] = { 0, 0, 0 }, srcStrides[3] = { 0, 0, 0 };
unsigned char *dstBuf = NULL;
- int nc = (subsamp == org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY ? 1 : 3), i;
+ int nc = 0, i, subsamp;
GET_HANDLE();
- if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF || subsamp < 0 ||
- subsamp >= org_libjpegturbo_turbojpeg_TJ_NUMSAMP)
- THROW_ARG("Invalid argument in decodeYUV()");
+ if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
+ THROW_ARG("Invalid argument in decodeYUV8()");
if (org_libjpegturbo_turbojpeg_TJ_NUMPF != TJ_NUMPF ||
org_libjpegturbo_turbojpeg_TJ_NUMSAMP != TJ_NUMSAMP)
THROW_ARG("Mismatch between Java and C API");
+ if ((subsamp = tj3Get(handle, TJPARAM_SUBSAMP)) == TJSAMP_UNKNOWN)
+ THROW_ARG("TJPARAM_SUBSAMP must be specified");
+ nc = subsamp == TJSAMP_GRAY ? 1 : 3;
if ((*env)->GetArrayLength(env, srcobjs) < nc)
THROW_ARG("Planes array is too small for the subsampling type");
if ((*env)->GetArrayLength(env, jSrcOffsets) < nc)
if ((*env)->GetArrayLength(env, dst) * dstElementSize < arraySize)
THROW_ARG("Destination buffer is not large enough");
- (*env)->GetIntArrayRegion(env, jSrcOffsets, 0, nc, srcOffsets);
+ (*env)->GetIntArrayRegion(env, jSrcOffsets, 0, nc, srcOffsetsTmp);
if ((*env)->ExceptionCheck(env)) goto bailout;
+ for (i = 0; i < 3; i++)
+ srcOffsets[i] = srcOffsetsTmp[i];
- (*env)->GetIntArrayRegion(env, jSrcStrides, 0, nc, srcStrides);
+ (*env)->GetIntArrayRegion(env, jSrcStrides, 0, nc, srcStridesTmp);
if ((*env)->ExceptionCheck(env)) goto bailout;
+ for (i = 0; i < 3; i++)
+ srcStrides[i] = srcStridesTmp[i];
for (i = 0; i < nc; i++) {
- int planeSize = tjPlaneSizeYUV(i, width, srcStrides[i], height, subsamp);
- int pw = tjPlaneWidth(i, width, subsamp);
+ size_t planeSize = tj3YUVPlaneSize(i, width, srcStrides[i], height,
+ subsamp);
+ int pw = tj3YUVPlaneWidth(i, width, subsamp);
- if (planeSize < 0 || pw < 0)
- THROW_ARG(tjGetErrorStr());
+ if (planeSize == 0 || pw == 0)
+ THROW_ARG(tj3GetErrorStr(NULL));
+ if (planeSize > (size_t)INT_MAX)
+ THROW_ARG("Source plane is too large");
if (srcOffsets[i] < 0)
- THROW_ARG("Invalid argument in decodeYUV()");
- if (srcStrides[i] < 0 && srcOffsets[i] - planeSize + pw < 0)
+ THROW_ARG("Invalid argument in decodeYUV8()");
+ if (srcStrides[i] < 0 && srcOffsets[i] - (int)planeSize + pw < 0)
THROW_ARG("Negative plane stride would cause memory to be accessed below plane boundary");
BAILIF0(jSrcPlanes[i] = (*env)->GetObjectArrayElement(env, srcobjs, i));
if ((*env)->GetArrayLength(env, jSrcPlanes[i]) <
- srcOffsets[i] + planeSize)
+ srcOffsets[i] + (int)planeSize)
THROW_ARG("Source plane is not large enough");
}
for (i = 0; i < nc; i++) {
}
BAILIF0NOEC(dstBuf = (*env)->GetPrimitiveArrayCritical(env, dst, 0));
- if (tjDecodeYUVPlanes(handle, srcPlanes, srcStrides, subsamp,
- &dstBuf[y * actualPitch + x * tjPixelSize[pf]], width,
- pitch, height, pf, flags) == -1) {
+ if (tj3DecodeYUVPlanes8(handle, srcPlanes, srcStrides,
+ &dstBuf[y * actualPitch + x * tjPixelSize[pf]],
+ width, pitch, height, pf) == -1) {
SAFE_RELEASE(dst, dstBuf);
for (i = 0; i < nc; i++)
SAFE_RELEASE(jSrcPlanes[i], srcPlanesTmp[i]);
SAFE_RELEASE(jSrcPlanes[i], srcPlanesTmp[i]);
}
-/* TurboJPEG 1.4.x: TJDecompressor::decodeYUV() byte destination */
-JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decodeYUV___3_3B_3I_3II_3BIIIIIII
+/* TurboJPEG 3: TJDecompressor::decodeYUV8() byte destination */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decodeYUV8___3_3B_3I_3I_3BIIIIII
(JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
- jintArray jSrcStrides, jint subsamp, jbyteArray dst, jint x, jint y,
- jint width, jint pitch, jint height, jint pf, jint flags)
+ jintArray jSrcStrides, jbyteArray dst, jint x, jint y, jint width,
+ jint pitch, jint height, jint pf)
{
- TJDecompressor_decodeYUV(env, obj, srcobjs, jSrcOffsets, jSrcStrides,
- subsamp, dst, 1, x, y, width, pitch, height, pf,
- flags);
+ TJDecompressor_decodeYUV8(env, obj, srcobjs, jSrcOffsets, jSrcStrides, dst,
+ 1, x, y, width, pitch, height, pf);
}
-/* TurboJPEG 1.4.x: TJDecompressor::decodeYUV() int destination */
-JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decodeYUV___3_3B_3I_3II_3IIIIIIII
+/* TurboJPEG 3: TJDecompressor::decodeYUV8() int destination */
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decodeYUV8___3_3B_3I_3I_3IIIIIII
(JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
- jintArray jSrcStrides, jint subsamp, jintArray dst, jint x, jint y,
- jint width, jint stride, jint height, jint pf, jint flags)
+ jintArray jSrcStrides, jintArray dst, jint x, jint y, jint width,
+ jint stride, jint height, jint pf)
{
if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
- THROW_ARG("Invalid argument in decodeYUV()");
+ THROW_ARG("Invalid argument in decodeYUV8()");
if (tjPixelSize[pf] != sizeof(jint))
THROW_ARG("Pixel format must be 32-bit when decoding to an integer buffer.");
- TJDecompressor_decodeYUV(env, obj, srcobjs, jSrcOffsets, jSrcStrides,
- subsamp, dst, sizeof(jint), x, y, width,
- stride * sizeof(jint), height, pf, flags);
+ TJDecompressor_decodeYUV8(env, obj, srcobjs, jSrcOffsets, jSrcStrides, dst,
+ sizeof(jint), x, y, width, stride * sizeof(jint),
+ height, pf);
bailout:
return;
jfieldID fid;
tjhandle handle;
- if ((handle = tjInitTransform()) == NULL)
- THROW(tjGetErrorStr(), "org/libjpegturbo/turbojpeg/TJException");
+ if ((handle = tj3Init(TJINIT_TRANSFORM)) == NULL)
+ THROW(tj3GetErrorStr(NULL), "org/libjpegturbo/turbojpeg/TJException");
BAILIF0(cls = (*env)->GetObjectClass(env, obj));
BAILIF0(fid = (*env)->GetFieldID(env, cls, "handle", "J"));
/* TurboJPEG 1.2.x: TJTransformer::transform() */
JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transform
(JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize,
- jobjectArray dstobjs, jobjectArray tobjs, jint flags)
+ jobjectArray dstobjs, jobjectArray tobjs)
{
tjhandle handle = 0;
unsigned char *jpegBuf = NULL, **dstBufs = NULL;
jsize n = 0;
- unsigned long *dstSizes = NULL;
+ size_t *dstSizes = NULL;
tjtransform *t = NULL;
jbyteArray *jdstBufs = NULL;
int i, jpegWidth = 0, jpegHeight = 0, jpegSubsamp;
if ((*env)->GetArrayLength(env, jsrcBuf) < jpegSize)
THROW_ARG("Source buffer is not large enough");
- BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
- jpegWidth = (int)(*env)->GetIntField(env, obj, _fid);
- BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
- jpegHeight = (int)(*env)->GetIntField(env, obj, _fid);
- BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
- jpegSubsamp = (int)(*env)->GetIntField(env, obj, _fid);
+ if ((jpegWidth = tj3Get(handle, TJPARAM_JPEGWIDTH)) == -1)
+ THROW_ARG("JPEG header has not yet been read");
+ if ((jpegHeight = tj3Get(handle, TJPARAM_JPEGHEIGHT)) == -1)
+ THROW_ARG("JPEG header has not yet been read");
+ if ((jpegSubsamp = tj3Get(handle, TJPARAM_SUBSAMP)) == TJSAMP_UNKNOWN)
+ THROW_ARG("TJPARAM_SUBSAMP must be specified");
n = (*env)->GetArrayLength(env, dstobjs);
if (n != (*env)->GetArrayLength(env, tobjs))
THROW_MEM();
if ((jdstBufs = (jbyteArray *)malloc(sizeof(jbyteArray) * n)) == NULL)
THROW_MEM();
- if ((dstSizes = (unsigned long *)malloc(sizeof(unsigned long) * n)) == NULL)
+ if ((dstSizes = (size_t *)malloc(sizeof(size_t) * n)) == NULL)
THROW_MEM();
if ((t = (tjtransform *)malloc(sizeof(tjtransform) * n)) == NULL)
THROW_MEM();
}
}
+ if (tj3Set(handle, TJPARAM_NOREALLOC, 1) == -1)
+ THROW_TJ();
+
for (i = 0; i < n; i++) {
int w = jpegWidth, h = jpegHeight;
+ if (t[i].op == TJXOP_TRANSPOSE || t[i].op == TJXOP_TRANSVERSE ||
+ t[i].op == TJXOP_ROT90 || t[i].op == TJXOP_ROT270) {
+ w = jpegHeight; h = jpegWidth;
+ }
if (t[i].r.w != 0) w = t[i].r.w;
if (t[i].r.h != 0) h = t[i].r.h;
BAILIF0(jdstBufs[i] = (*env)->GetObjectArrayElement(env, dstobjs, i));
- if ((unsigned long)(*env)->GetArrayLength(env, jdstBufs[i]) <
- tjBufSize(w, h, jpegSubsamp))
+ if ((size_t)(*env)->GetArrayLength(env, jdstBufs[i]) <
+ tj3JPEGBufSize(w, h, jpegSubsamp))
THROW_ARG("Destination buffer is not large enough");
}
BAILIF0NOEC(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0));
BAILIF0NOEC(dstBufs[i] =
(*env)->GetPrimitiveArrayCritical(env, jdstBufs[i], 0));
- if (tjTransform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t,
- flags | TJFLAG_NOREALLOC) == -1) {
+ if (tj3Transform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t) == -1) {
for (i = 0; i < n; i++)
SAFE_RELEASE(jdstBufs[i], dstBufs[i]);
SAFE_RELEASE(jsrcBuf, jpegBuf);
{
Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy(env, obj);
}
+
+/* Private image I/O routines (used only by TJBench) */
+JNIEXPORT jobject JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_loadImage
+ (JNIEnv *env, jobject obj, jint precision, jstring jfilename,
+ jintArray jwidth, jint align, jintArray jheight, jintArray jpixelFormat)
+{
+ tjhandle handle = NULL;
+ void *dstBuf = NULL, *jdstPtr;
+ int width, *warr, height, *harr, pixelFormat, *pfarr, n;
+ const char *filename = NULL;
+ jboolean isCopy;
+ jobject jdstBuf = NULL;
+
+ GET_HANDLE();
+
+ if ((precision != 8 && precision != 12 && precision != 16) ||
+ jfilename == NULL || jwidth == NULL ||
+ (*env)->GetArrayLength(env, jwidth) < 1 || jheight == NULL ||
+ (*env)->GetArrayLength(env, jheight) < 1 || jpixelFormat == NULL ||
+ (*env)->GetArrayLength(env, jpixelFormat) < 1)
+ THROW_ARG("Invalid argument in loadImage()");
+
+ BAILIF0NOEC(warr = (*env)->GetPrimitiveArrayCritical(env, jwidth, 0));
+ width = warr[0];
+ (*env)->ReleasePrimitiveArrayCritical(env, jwidth, warr, 0);
+ BAILIF0NOEC(harr = (*env)->GetPrimitiveArrayCritical(env, jheight, 0));
+ height = harr[0];
+ (*env)->ReleasePrimitiveArrayCritical(env, jheight, harr, 0);
+ BAILIF0NOEC(pfarr = (*env)->GetPrimitiveArrayCritical(env, jpixelFormat, 0));
+ pixelFormat = pfarr[0];
+ (*env)->ReleasePrimitiveArrayCritical(env, jpixelFormat, pfarr, 0);
+ BAILIF0(filename = (*env)->GetStringUTFChars(env, jfilename, &isCopy));
+
+ if (precision == 8) {
+ if ((dstBuf = tj3LoadImage8(handle, filename, &width, align, &height,
+ &pixelFormat)) == NULL)
+ THROW_TJ();
+ } else if (precision == 12) {
+ if ((dstBuf = tj3LoadImage12(handle, filename, &width, align, &height,
+ &pixelFormat)) == NULL)
+ THROW_TJ();
+ } else {
+ if ((dstBuf = tj3LoadImage16(handle, filename, &width, align, &height,
+ &pixelFormat)) == NULL)
+ THROW_TJ();
+ }
+
+ (*env)->ReleaseStringUTFChars(env, jfilename, filename);
+ filename = NULL;
+
+ if ((unsigned long long)width * (unsigned long long)height *
+ (unsigned long long)tjPixelSize[pixelFormat] >
+ (unsigned long long)((unsigned int)-1))
+ THROW_ARG("Image is too large");
+
+ BAILIF0NOEC(warr = (*env)->GetPrimitiveArrayCritical(env, jwidth, 0));
+ warr[0] = width;
+ (*env)->ReleasePrimitiveArrayCritical(env, jwidth, warr, 0);
+ BAILIF0NOEC(harr = (*env)->GetPrimitiveArrayCritical(env, jheight, 0));
+ harr[0] = height;
+ (*env)->ReleasePrimitiveArrayCritical(env, jheight, harr, 0);
+ BAILIF0NOEC(pfarr = (*env)->GetPrimitiveArrayCritical(env, jpixelFormat, 0));
+ pfarr[0] = pixelFormat;
+ (*env)->ReleasePrimitiveArrayCritical(env, jpixelFormat, pfarr, 0);
+
+ n = width * height * tjPixelSize[pixelFormat];
+ if (precision == 8)
+ jdstBuf = (*env)->NewByteArray(env, n);
+ else
+ jdstBuf = (*env)->NewShortArray(env, n);
+ BAILIF0NOEC(jdstPtr = (*env)->GetPrimitiveArrayCritical(env, jdstBuf, 0));
+ memcpy(jdstPtr, dstBuf, n * (precision > 8 ? 2 : 1));
+ (*env)->ReleasePrimitiveArrayCritical(env, jdstBuf, jdstPtr, 0);
+
+bailout:
+ if (filename) (*env)->ReleaseStringUTFChars(env, jfilename, filename);
+ tj3Free(dstBuf);
+ return jdstBuf;
+}
+
+
+JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_saveImage
+ (JNIEnv *env, jobject obj, jint precision, jstring jfilename,
+ jobject jsrcBuf, jint width, jint pitch, jint height, jint pixelFormat)
+{
+ tjhandle handle = NULL;
+ void *srcBuf = NULL, *jsrcPtr;
+ const char *filename = NULL;
+ int n;
+ jboolean isCopy;
+
+ GET_HANDLE();
+
+ if ((precision != 8 && precision != 12 && precision != 16) ||
+ jfilename == NULL || jsrcBuf == NULL || width < 1 || height < 1 ||
+ pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
+ THROW_ARG("Invalid argument in saveImage()");
+
+ if ((unsigned long long)width * (unsigned long long)height *
+ (unsigned long long)tjPixelSize[pixelFormat] >
+ (unsigned long long)((unsigned int)-1))
+ THROW_ARG("Image is too large");
+ n = width * height * tjPixelSize[pixelFormat];
+ if ((*env)->GetArrayLength(env, jsrcBuf) < n)
+ THROW_ARG("Source buffer is not large enough");
+
+ if ((srcBuf = malloc(n * (precision > 8 ? 2 : 1))) == NULL)
+ THROW_MEM();
+
+ BAILIF0NOEC(jsrcPtr = (*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0));
+ memcpy(srcBuf, jsrcPtr, n * (precision > 8 ? 2 : 1));
+ (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jsrcPtr, 0);
+ BAILIF0(filename = (*env)->GetStringUTFChars(env, jfilename, &isCopy));
+
+ if (precision == 8) {
+ if (tj3SaveImage8(handle, filename, srcBuf, width, pitch, height,
+ pixelFormat) == -1)
+ THROW_TJ();
+ } else if (precision == 12) {
+ if (tj3SaveImage12(handle, filename, srcBuf, width, pitch, height,
+ pixelFormat) == -1)
+ THROW_TJ();
+ } else {
+ if (tj3SaveImage16(handle, filename, srcBuf, width, pitch, height,
+ pixelFormat) == -1)
+ THROW_TJ();
+ }
+
+bailout:
+ if (filename) (*env)->ReleaseStringUTFChars(env, jfilename, filename);
+ free(srcBuf);
+}