shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Copyright (C)2011-2020 D. R. Commander.  All Rights Reserved.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Redistribution and use in source and binary forms, with or without
shun-iwasawa 82a8f5
 * modification, are permitted provided that the following conditions are met:
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * - Redistributions of source code must retain the above copyright notice,
shun-iwasawa 82a8f5
 *   this list of conditions and the following disclaimer.
shun-iwasawa 82a8f5
 * - Redistributions in binary form must reproduce the above copyright notice,
shun-iwasawa 82a8f5
 *   this list of conditions and the following disclaimer in the documentation
shun-iwasawa 82a8f5
 *   and/or other materials provided with the distribution.
shun-iwasawa 82a8f5
 * - Neither the name of the libjpeg-turbo Project nor the names of its
shun-iwasawa 82a8f5
 *   contributors may be used to endorse or promote products derived from this
shun-iwasawa 82a8f5
 *   software without specific prior written permission.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
shun-iwasawa 82a8f5
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
shun-iwasawa 82a8f5
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
shun-iwasawa 82a8f5
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
shun-iwasawa 82a8f5
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
shun-iwasawa 82a8f5
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
shun-iwasawa 82a8f5
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
shun-iwasawa 82a8f5
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
shun-iwasawa 82a8f5
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
shun-iwasawa 82a8f5
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
shun-iwasawa 82a8f5
 * POSSIBILITY OF SUCH DAMAGE.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include <stdlib.h></stdlib.h>
shun-iwasawa 82a8f5
#include <string.h></string.h>
shun-iwasawa 82a8f5
#include "turbojpeg.h"
shun-iwasawa 82a8f5
#ifdef WIN32
shun-iwasawa 82a8f5
#include "tjutil.h"
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
#include <jni.h></jni.h>
shun-iwasawa 82a8f5
#include "java/org_libjpegturbo_turbojpeg_TJCompressor.h"
shun-iwasawa 82a8f5
#include "java/org_libjpegturbo_turbojpeg_TJDecompressor.h"
shun-iwasawa 82a8f5
#include "java/org_libjpegturbo_turbojpeg_TJTransformer.h"
shun-iwasawa 82a8f5
#include "java/org_libjpegturbo_turbojpeg_TJ.h"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define BAILIF0(f) { \
shun-iwasawa 82a8f5
  if (!(f) || (*env)->ExceptionCheck(env)) { \
shun-iwasawa 82a8f5
    goto bailout; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define THROW(msg, exceptionClass) { \
shun-iwasawa 82a8f5
  jclass _exccls = (*env)->FindClass(env, exceptionClass); \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  BAILIF0(_exccls); \
shun-iwasawa 82a8f5
  (*env)->ThrowNew(env, _exccls, msg); \
shun-iwasawa 82a8f5
  goto bailout; \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define THROW_TJ() { \
shun-iwasawa 82a8f5
  jclass _exccls; \
shun-iwasawa 82a8f5
  jmethodID _excid; \
shun-iwasawa 82a8f5
  jobject _excobj; \
shun-iwasawa 82a8f5
  jstring _errstr; \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  BAILIF0(_errstr = (*env)->NewStringUTF(env, tjGetErrorStr2(handle))); \
shun-iwasawa 82a8f5
  BAILIF0(_exccls = (*env)->FindClass(env, \
shun-iwasawa 82a8f5
    "org/libjpegturbo/turbojpeg/TJException")); \
shun-iwasawa 82a8f5
  BAILIF0(_excid = (*env)->GetMethodID(env, _exccls, "<init>", \</init>
shun-iwasawa 82a8f5
                                       "(Ljava/lang/String;I)V")); \
shun-iwasawa 82a8f5
  BAILIF0(_excobj = (*env)->NewObject(env, _exccls, _excid, _errstr, \
shun-iwasawa 82a8f5
                                      tjGetErrorCode(handle))); \
shun-iwasawa 82a8f5
  (*env)->Throw(env, _excobj); \
shun-iwasawa 82a8f5
  goto bailout; \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define THROW_ARG(msg)  THROW(msg, "java/lang/IllegalArgumentException")
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define THROW_MEM() \
shun-iwasawa 82a8f5
  THROW("Memory allocation failure", "java/lang/OutOfMemoryError");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define GET_HANDLE() \
shun-iwasawa 82a8f5
  jclass _cls = (*env)->GetObjectClass(env, obj); \
shun-iwasawa 82a8f5
  jfieldID _fid; \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  BAILIF0(_cls); \
shun-iwasawa 82a8f5
  BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "handle", "J")); \
shun-iwasawa 82a8f5
  handle = (tjhandle)(size_t)(*env)->GetLongField(env, obj, _fid);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef _WIN32
shun-iwasawa 82a8f5
#define setenv(envvar, value, dummy)  _putenv_s(envvar, value)
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PROP2ENV(property, envvar) { \
shun-iwasawa 82a8f5
  if ((jName = (*env)->NewStringUTF(env, property)) != NULL && \
shun-iwasawa 82a8f5
      (jValue = (*env)->CallStaticObjectMethod(env, cls, mid, \
shun-iwasawa 82a8f5
                                               jName)) != NULL) { \
shun-iwasawa 82a8f5
    if ((value = (*env)->GetStringUTFChars(env, jValue, 0)) != NULL) { \
shun-iwasawa 82a8f5
      setenv(envvar, value, 1); \
shun-iwasawa 82a8f5
      (*env)->ReleaseStringUTFChars(env, jValue, value); \
shun-iwasawa 82a8f5
    } \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define SAFE_RELEASE(javaArray, cArray) { \
shun-iwasawa 82a8f5
  if (javaArray && cArray) \
shun-iwasawa 82a8f5
    (*env)->ReleasePrimitiveArrayCritical(env, javaArray, (void *)cArray, 0); \
shun-iwasawa 82a8f5
  cArray = NULL; \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int ProcessSystemProperties(JNIEnv *env)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  jclass cls;
shun-iwasawa 82a8f5
  jmethodID mid;
shun-iwasawa 82a8f5
  jstring jName, jValue;
shun-iwasawa 82a8f5
  const char *value;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(cls = (*env)->FindClass(env, "java/lang/System"));
shun-iwasawa 82a8f5
  BAILIF0(mid = (*env)->GetStaticMethodID(env, cls, "getProperty",
shun-iwasawa 82a8f5
    "(Ljava/lang/String;)Ljava/lang/String;"));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  PROP2ENV("turbojpeg.optimize", "TJ_OPTIMIZE");
shun-iwasawa 82a8f5
  PROP2ENV("turbojpeg.arithmetic", "TJ_ARITHMETIC");
shun-iwasawa 82a8f5
  PROP2ENV("turbojpeg.restart", "TJ_RESTART");
shun-iwasawa 82a8f5
  PROP2ENV("turbojpeg.progressive", "TJ_PROGRESSIVE");
shun-iwasawa 82a8f5
  return 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return -1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJ::bufSize() */
shun-iwasawa 82a8f5
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize
shun-iwasawa 82a8f5
  (JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  jint retval = (jint)tjBufSize(width, height, jpegSubsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (retval == -1) THROW_ARG(tjGetErrorStr());
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.4.x: TJ::bufSizeYUV() */
shun-iwasawa 82a8f5
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII
shun-iwasawa 82a8f5
  (JNIEnv *env, jclass cls, jint width, jint pad, jint height, jint subsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  jint retval = (jint)tjBufSizeYUV2(width, pad, height, subsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (retval == -1) THROW_ARG(tjGetErrorStr());
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJ::bufSizeYUV() */
shun-iwasawa 82a8f5
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__III
shun-iwasawa 82a8f5
  (JNIEnv *env, jclass cls, jint width, jint height, jint subsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  return Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII(env, cls, width,
shun-iwasawa 82a8f5
                                                             4, height,
shun-iwasawa 82a8f5
                                                             subsamp);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.4.x: TJ::planeSizeYUV() */
shun-iwasawa 82a8f5
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeSizeYUV__IIIII
shun-iwasawa 82a8f5
  (JNIEnv *env, jclass cls, jint componentID, jint width, jint stride,
shun-iwasawa 82a8f5
   jint height, jint subsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  jint retval = (jint)tjPlaneSizeYUV(componentID, width, stride, height,
shun-iwasawa 82a8f5
                                     subsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (retval == -1) THROW_ARG(tjGetErrorStr());
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.4.x: TJ::planeWidth() */
shun-iwasawa 82a8f5
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeWidth__III
shun-iwasawa 82a8f5
  (JNIEnv *env, jclass cls, jint componentID, jint width, jint subsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  jint retval = (jint)tjPlaneWidth(componentID, width, subsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (retval == -1) THROW_ARG(tjGetErrorStr());
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.4.x: TJ::planeHeight() */
shun-iwasawa 82a8f5
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeHeight__III
shun-iwasawa 82a8f5
  (JNIEnv *env, jclass cls, jint componentID, jint height, jint subsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  jint retval = (jint)tjPlaneHeight(componentID, height, subsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (retval == -1) THROW_ARG(tjGetErrorStr());
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJCompressor::init() */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_init
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  jclass cls;
shun-iwasawa 82a8f5
  jfieldID fid;
shun-iwasawa 82a8f5
  tjhandle handle;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((handle = tjInitCompress()) == NULL)
shun-iwasawa 82a8f5
    THROW(tjGetErrorStr(), "org/libjpegturbo/turbojpeg/TJException");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(cls = (*env)->GetObjectClass(env, obj));
shun-iwasawa 82a8f5
  BAILIF0(fid = (*env)->GetFieldID(env, cls, "handle", "J"));
shun-iwasawa 82a8f5
  (*env)->SetLongField(env, obj, fid, (size_t)handle);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static jint TJCompressor_compress
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint x, jint y,
shun-iwasawa 82a8f5
   jint width, jint pitch, jint height, jint pf, jbyteArray dst,
shun-iwasawa 82a8f5
   jint jpegSubsamp, jint jpegQual, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjhandle handle = 0;
shun-iwasawa 82a8f5
  unsigned long jpegSize = 0;
shun-iwasawa 82a8f5
  jsize arraySize = 0, actualPitch;
shun-iwasawa 82a8f5
  unsigned char *srcBuf = NULL, *jpegBuf = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_HANDLE();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF || width < 1 ||
shun-iwasawa 82a8f5
      height < 1 || pitch < 0)
shun-iwasawa 82a8f5
    THROW_ARG("Invalid argument in compress()");
shun-iwasawa 82a8f5
  if (org_libjpegturbo_turbojpeg_TJ_NUMPF != TJ_NUMPF)
shun-iwasawa 82a8f5
    THROW_ARG("Mismatch between Java and C API");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  actualPitch = (pitch == 0) ? width * tjPixelSize[pf] : pitch;
shun-iwasawa 82a8f5
  arraySize = (y + height - 1) * actualPitch + (x + width) * tjPixelSize[pf];
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, src) * srcElementSize < arraySize)
shun-iwasawa 82a8f5
    THROW_ARG("Source buffer is not large enough");
shun-iwasawa 82a8f5
  jpegSize = tjBufSize(width, height, jpegSubsamp);
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, dst) < (jsize)jpegSize)
shun-iwasawa 82a8f5
    THROW_ARG("Destination buffer is not large enough");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (ProcessSystemProperties(env) < 0) goto bailout;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(srcBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
shun-iwasawa 82a8f5
  BAILIF0(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, dst, 0));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (tjCompress2(handle, &srcBuf[y * actualPitch + x * tjPixelSize[pf]],
shun-iwasawa 82a8f5
                  width, pitch, height, pf, &jpegBuf, &jpegSize, jpegSubsamp,
shun-iwasawa 82a8f5
                  jpegQual, flags | TJFLAG_NOREALLOC) == -1) {
shun-iwasawa 82a8f5
    SAFE_RELEASE(dst, jpegBuf);
shun-iwasawa 82a8f5
    SAFE_RELEASE(src, srcBuf);
shun-iwasawa 82a8f5
    THROW_TJ();
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  SAFE_RELEASE(dst, jpegBuf);
shun-iwasawa 82a8f5
  SAFE_RELEASE(src, srcBuf);
shun-iwasawa 82a8f5
  return (jint)jpegSize;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.3.x: TJCompressor::compress() byte source */
shun-iwasawa 82a8f5
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIIIII_3BIII
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jbyteArray src, jint x, jint y, jint width,
shun-iwasawa 82a8f5
   jint pitch, jint height, jint pf, jbyteArray dst, jint jpegSubsamp,
shun-iwasawa 82a8f5
   jint jpegQual, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  return TJCompressor_compress(env, obj, src, 1, x, y, width, pitch, height,
shun-iwasawa 82a8f5
                               pf, dst, jpegSubsamp, jpegQual, flags);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJCompressor::compress() byte source */
shun-iwasawa 82a8f5
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIII_3BIII
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch,
shun-iwasawa 82a8f5
   jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual,
shun-iwasawa 82a8f5
   jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  return TJCompressor_compress(env, obj, src, 1, 0, 0, width, pitch, height,
shun-iwasawa 82a8f5
                               pf, dst, jpegSubsamp, jpegQual, flags);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.3.x: TJCompressor::compress() int source */
shun-iwasawa 82a8f5
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIIIII_3BIII
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width,
shun-iwasawa 82a8f5
   jint stride, jint height, jint pf, jbyteArray dst, jint jpegSubsamp,
shun-iwasawa 82a8f5
   jint jpegQual, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
shun-iwasawa 82a8f5
    THROW_ARG("Invalid argument in compress()");
shun-iwasawa 82a8f5
  if (tjPixelSize[pf] != sizeof(jint))
shun-iwasawa 82a8f5
    THROW_ARG("Pixel format must be 32-bit when compressing from an integer buffer.");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return TJCompressor_compress(env, obj, src, sizeof(jint), x, y, width,
shun-iwasawa 82a8f5
                               stride * sizeof(jint), height, pf, dst,
shun-iwasawa 82a8f5
                               jpegSubsamp, jpegQual, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return 0;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJCompressor::compress() int source */
shun-iwasawa 82a8f5
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIII_3BIII
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jintArray src, jint width, jint stride,
shun-iwasawa 82a8f5
   jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual,
shun-iwasawa 82a8f5
   jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
shun-iwasawa 82a8f5
    THROW_ARG("Invalid argument in compress()");
shun-iwasawa 82a8f5
  if (tjPixelSize[pf] != sizeof(jint))
shun-iwasawa 82a8f5
    THROW_ARG("Pixel format must be 32-bit when compressing from an integer buffer.");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return TJCompressor_compress(env, obj, src, sizeof(jint), 0, 0, width,
shun-iwasawa 82a8f5
                               stride * sizeof(jint), height, pf, dst,
shun-iwasawa 82a8f5
                               jpegSubsamp, jpegQual, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return 0;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.4.x: TJCompressor::compressFromYUV() */
shun-iwasawa 82a8f5
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compressFromYUV___3_3B_3II_3III_3BII
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
shun-iwasawa 82a8f5
   jint width, jintArray jSrcStrides, jint height, jint subsamp,
shun-iwasawa 82a8f5
   jbyteArray dst, jint jpegQual, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjhandle handle = 0;
shun-iwasawa 82a8f5
  unsigned long jpegSize = 0;
shun-iwasawa 82a8f5
  jbyteArray jSrcPlanes[3] = { NULL, NULL, NULL };
shun-iwasawa 82a8f5
  const unsigned char *srcPlanesTmp[3] = { NULL, NULL, NULL };
shun-iwasawa 82a8f5
  const unsigned char *srcPlanes[3] = { NULL, NULL, NULL };
shun-iwasawa 82a8f5
  int *srcOffsetsTmp = NULL, srcOffsets[3] = { 0, 0, 0 };
shun-iwasawa 82a8f5
  int *srcStridesTmp = NULL, srcStrides[3] = { 0, 0, 0 };
shun-iwasawa 82a8f5
  unsigned char *jpegBuf = NULL;
shun-iwasawa 82a8f5
  int nc = (subsamp == org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY ? 1 : 3), i;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_HANDLE();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (subsamp < 0 || subsamp >= org_libjpegturbo_turbojpeg_TJ_NUMSAMP)
shun-iwasawa 82a8f5
    THROW_ARG("Invalid argument in compressFromYUV()");
shun-iwasawa 82a8f5
  if (org_libjpegturbo_turbojpeg_TJ_NUMSAMP != TJ_NUMSAMP)
shun-iwasawa 82a8f5
    THROW_ARG("Mismatch between Java and C API");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, srcobjs) < nc)
shun-iwasawa 82a8f5
    THROW_ARG("Planes array is too small for the subsampling type");
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, jSrcOffsets) < nc)
shun-iwasawa 82a8f5
    THROW_ARG("Offsets array is too small for the subsampling type");
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, jSrcStrides) < nc)
shun-iwasawa 82a8f5
    THROW_ARG("Strides array is too small for the subsampling type");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpegSize = tjBufSize(width, height, subsamp);
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, dst) < (jsize)jpegSize)
shun-iwasawa 82a8f5
    THROW_ARG("Destination buffer is not large enough");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (ProcessSystemProperties(env) < 0) goto bailout;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(srcOffsetsTmp =
shun-iwasawa 82a8f5
          (*env)->GetPrimitiveArrayCritical(env, jSrcOffsets, 0));
shun-iwasawa 82a8f5
  for (i = 0; i < nc; i++) srcOffsets[i] = srcOffsetsTmp[i];
shun-iwasawa 82a8f5
  SAFE_RELEASE(jSrcOffsets, srcOffsetsTmp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(srcStridesTmp =
shun-iwasawa 82a8f5
          (*env)->GetPrimitiveArrayCritical(env, jSrcStrides, 0));
shun-iwasawa 82a8f5
  for (i = 0; i < nc; i++) srcStrides[i] = srcStridesTmp[i];
shun-iwasawa 82a8f5
  SAFE_RELEASE(jSrcStrides, srcStridesTmp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < nc; i++) {
shun-iwasawa 82a8f5
    int planeSize = tjPlaneSizeYUV(i, width, srcStrides[i], height, subsamp);
shun-iwasawa 82a8f5
    int pw = tjPlaneWidth(i, width, subsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (planeSize < 0 || pw < 0)
shun-iwasawa 82a8f5
      THROW_ARG(tjGetErrorStr());
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (srcOffsets[i] < 0)
shun-iwasawa 82a8f5
      THROW_ARG("Invalid argument in compressFromYUV()");
shun-iwasawa 82a8f5
    if (srcStrides[i] < 0 && srcOffsets[i] - planeSize + pw < 0)
shun-iwasawa 82a8f5
      THROW_ARG("Negative plane stride would cause memory to be accessed below plane boundary");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    BAILIF0(jSrcPlanes[i] = (*env)->GetObjectArrayElement(env, srcobjs, i));
shun-iwasawa 82a8f5
    if ((*env)->GetArrayLength(env, jSrcPlanes[i]) <
shun-iwasawa 82a8f5
        srcOffsets[i] + planeSize)
shun-iwasawa 82a8f5
      THROW_ARG("Source plane is not large enough");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    BAILIF0(srcPlanesTmp[i] =
shun-iwasawa 82a8f5
            (*env)->GetPrimitiveArrayCritical(env, jSrcPlanes[i], 0));
shun-iwasawa 82a8f5
    srcPlanes[i] = &srcPlanesTmp[i][srcOffsets[i]];
shun-iwasawa 82a8f5
    SAFE_RELEASE(jSrcPlanes[i], srcPlanesTmp[i]);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  BAILIF0(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, dst, 0));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (tjCompressFromYUVPlanes(handle, srcPlanes, width, srcStrides, height,
shun-iwasawa 82a8f5
                              subsamp, &jpegBuf, &jpegSize, jpegQual,
shun-iwasawa 82a8f5
                              flags | TJFLAG_NOREALLOC) == -1) {
shun-iwasawa 82a8f5
    SAFE_RELEASE(dst, jpegBuf);
shun-iwasawa 82a8f5
    THROW_TJ();
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  SAFE_RELEASE(dst, jpegBuf);
shun-iwasawa 82a8f5
  return (jint)jpegSize;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void TJCompressor_encodeYUV
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint x, jint y,
shun-iwasawa 82a8f5
   jint width, jint pitch, jint height, jint pf, jobjectArray dstobjs,
shun-iwasawa 82a8f5
   jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjhandle handle = 0;
shun-iwasawa 82a8f5
  jsize arraySize = 0, actualPitch;
shun-iwasawa 82a8f5
  unsigned char *srcBuf = NULL;
shun-iwasawa 82a8f5
  jbyteArray jDstPlanes[3] = { NULL, NULL, NULL };
shun-iwasawa 82a8f5
  unsigned char *dstPlanesTmp[3] = { NULL, NULL, NULL };
shun-iwasawa 82a8f5
  unsigned char *dstPlanes[3] = { NULL, NULL, NULL };
shun-iwasawa 82a8f5
  int *dstOffsetsTmp = NULL, dstOffsets[3] = { 0, 0, 0 };
shun-iwasawa 82a8f5
  int *dstStridesTmp = NULL, dstStrides[3] = { 0, 0, 0 };
shun-iwasawa 82a8f5
  int nc = (subsamp == org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY ? 1 : 3), i;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_HANDLE();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF || width < 1 ||
shun-iwasawa 82a8f5
      height < 1 || pitch < 0 || subsamp < 0 ||
shun-iwasawa 82a8f5
      subsamp >= org_libjpegturbo_turbojpeg_TJ_NUMSAMP)
shun-iwasawa 82a8f5
    THROW_ARG("Invalid argument in encodeYUV()");
shun-iwasawa 82a8f5
  if (org_libjpegturbo_turbojpeg_TJ_NUMPF != TJ_NUMPF ||
shun-iwasawa 82a8f5
      org_libjpegturbo_turbojpeg_TJ_NUMSAMP != TJ_NUMSAMP)
shun-iwasawa 82a8f5
    THROW_ARG("Mismatch between Java and C API");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, dstobjs) < nc)
shun-iwasawa 82a8f5
    THROW_ARG("Planes array is too small for the subsampling type");
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, jDstOffsets) < nc)
shun-iwasawa 82a8f5
    THROW_ARG("Offsets array is too small for the subsampling type");
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, jDstStrides) < nc)
shun-iwasawa 82a8f5
    THROW_ARG("Strides array is too small for the subsampling type");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  actualPitch = (pitch == 0) ? width * tjPixelSize[pf] : pitch;
shun-iwasawa 82a8f5
  arraySize = (y + height - 1) * actualPitch + (x + width) * tjPixelSize[pf];
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, src) * srcElementSize < arraySize)
shun-iwasawa 82a8f5
    THROW_ARG("Source buffer is not large enough");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(dstOffsetsTmp =
shun-iwasawa 82a8f5
          (*env)->GetPrimitiveArrayCritical(env, jDstOffsets, 0));
shun-iwasawa 82a8f5
  for (i = 0; i < nc; i++) dstOffsets[i] = dstOffsetsTmp[i];
shun-iwasawa 82a8f5
  SAFE_RELEASE(jDstOffsets, dstOffsetsTmp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(dstStridesTmp =
shun-iwasawa 82a8f5
          (*env)->GetPrimitiveArrayCritical(env, jDstStrides, 0));
shun-iwasawa 82a8f5
  for (i = 0; i < nc; i++) dstStrides[i] = dstStridesTmp[i];
shun-iwasawa 82a8f5
  SAFE_RELEASE(jDstStrides, dstStridesTmp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < nc; i++) {
shun-iwasawa 82a8f5
    int planeSize = tjPlaneSizeYUV(i, width, dstStrides[i], height, subsamp);
shun-iwasawa 82a8f5
    int pw = tjPlaneWidth(i, width, subsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (planeSize < 0 || pw < 0)
shun-iwasawa 82a8f5
      THROW_ARG(tjGetErrorStr());
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (dstOffsets[i] < 0)
shun-iwasawa 82a8f5
      THROW_ARG("Invalid argument in encodeYUV()");
shun-iwasawa 82a8f5
    if (dstStrides[i] < 0 && dstOffsets[i] - planeSize + pw < 0)
shun-iwasawa 82a8f5
      THROW_ARG("Negative plane stride would cause memory to be accessed below plane boundary");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    BAILIF0(jDstPlanes[i] = (*env)->GetObjectArrayElement(env, dstobjs, i));
shun-iwasawa 82a8f5
    if ((*env)->GetArrayLength(env, jDstPlanes[i]) <
shun-iwasawa 82a8f5
        dstOffsets[i] + planeSize)
shun-iwasawa 82a8f5
      THROW_ARG("Destination plane is not large enough");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    BAILIF0(dstPlanesTmp[i] =
shun-iwasawa 82a8f5
            (*env)->GetPrimitiveArrayCritical(env, jDstPlanes[i], 0));
shun-iwasawa 82a8f5
    dstPlanes[i] = &dstPlanesTmp[i][dstOffsets[i]];
shun-iwasawa 82a8f5
    SAFE_RELEASE(jDstPlanes[i], dstPlanesTmp[i]);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  BAILIF0(srcBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (tjEncodeYUVPlanes(handle, &srcBuf[y * actualPitch + x * tjPixelSize[pf]],
shun-iwasawa 82a8f5
                        width, pitch, height, pf, dstPlanes, dstStrides,
shun-iwasawa 82a8f5
                        subsamp, flags) == -1) {
shun-iwasawa 82a8f5
    SAFE_RELEASE(src, srcBuf);
shun-iwasawa 82a8f5
    THROW_TJ();
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  SAFE_RELEASE(src, srcBuf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.4.x: TJCompressor::encodeYUV() byte source */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIIIII_3_3B_3I_3III
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jbyteArray src, jint x, jint y, jint width,
shun-iwasawa 82a8f5
   jint pitch, jint height, jint pf, jobjectArray dstobjs,
shun-iwasawa 82a8f5
   jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  TJCompressor_encodeYUV(env, obj, src, 1, x, y, width, pitch, height, pf,
shun-iwasawa 82a8f5
                         dstobjs, jDstOffsets, jDstStrides, subsamp, flags);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.4.x: TJCompressor::encodeYUV() int source */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIIIII_3_3B_3I_3III
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width,
shun-iwasawa 82a8f5
   jint stride, jint height, jint pf, jobjectArray dstobjs,
shun-iwasawa 82a8f5
   jintArray jDstOffsets, jintArray jDstStrides, jint subsamp, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
shun-iwasawa 82a8f5
    THROW_ARG("Invalid argument in encodeYUV()");
shun-iwasawa 82a8f5
  if (tjPixelSize[pf] != sizeof(jint))
shun-iwasawa 82a8f5
    THROW_ARG("Pixel format must be 32-bit when encoding from an integer buffer.");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  TJCompressor_encodeYUV(env, obj, src, sizeof(jint), x, y, width,
shun-iwasawa 82a8f5
                         stride * sizeof(jint), height, pf, dstobjs,
shun-iwasawa 82a8f5
                         jDstOffsets, jDstStrides, subsamp, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void JNICALL TJCompressor_encodeYUV_12
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint width,
shun-iwasawa 82a8f5
   jint pitch, jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjhandle handle = 0;
shun-iwasawa 82a8f5
  jsize arraySize = 0;
shun-iwasawa 82a8f5
  unsigned char *srcBuf = NULL, *dstBuf = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_HANDLE();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF || width < 1 ||
shun-iwasawa 82a8f5
      height < 1 || pitch < 0)
shun-iwasawa 82a8f5
    THROW_ARG("Invalid argument in encodeYUV()");
shun-iwasawa 82a8f5
  if (org_libjpegturbo_turbojpeg_TJ_NUMPF != TJ_NUMPF)
shun-iwasawa 82a8f5
    THROW_ARG("Mismatch between Java and C API");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  arraySize = (pitch == 0) ? width * tjPixelSize[pf] * height : pitch * height;
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, src) * srcElementSize < arraySize)
shun-iwasawa 82a8f5
    THROW_ARG("Source buffer is not large enough");
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, dst) <
shun-iwasawa 82a8f5
      (jsize)tjBufSizeYUV(width, height, subsamp))
shun-iwasawa 82a8f5
    THROW_ARG("Destination buffer is not large enough");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(srcBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
shun-iwasawa 82a8f5
  BAILIF0(dstBuf = (*env)->GetPrimitiveArrayCritical(env, dst, 0));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (tjEncodeYUV2(handle, srcBuf, width, pitch, height, pf, dstBuf, subsamp,
shun-iwasawa 82a8f5
                   flags) == -1) {
shun-iwasawa 82a8f5
    SAFE_RELEASE(dst, dstBuf);
shun-iwasawa 82a8f5
    SAFE_RELEASE(src, srcBuf);
shun-iwasawa 82a8f5
    THROW_TJ();
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  SAFE_RELEASE(dst, dstBuf);
shun-iwasawa 82a8f5
  SAFE_RELEASE(src, srcBuf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJCompressor::encodeYUV() byte source */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3BIIII_3BII
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch,
shun-iwasawa 82a8f5
   jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  TJCompressor_encodeYUV_12(env, obj, src, 1, width, pitch, height, pf, dst,
shun-iwasawa 82a8f5
                            subsamp, flags);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJCompressor::encodeYUV() int source */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIII_3BII
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jintArray src, jint width, jint stride,
shun-iwasawa 82a8f5
   jint height, jint pf, jbyteArray dst, jint subsamp, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
shun-iwasawa 82a8f5
    THROW_ARG("Invalid argument in encodeYUV()");
shun-iwasawa 82a8f5
  if (tjPixelSize[pf] != sizeof(jint))
shun-iwasawa 82a8f5
    THROW_ARG("Pixel format must be 32-bit when encoding from an integer buffer.");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  TJCompressor_encodeYUV_12(env, obj, src, sizeof(jint), width,
shun-iwasawa 82a8f5
                            stride * sizeof(jint), height, pf, dst, subsamp,
shun-iwasawa 82a8f5
                            flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJCompressor::destroy() */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjhandle handle = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_HANDLE();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (tjDestroy(handle) == -1) THROW_TJ();
shun-iwasawa 82a8f5
  (*env)->SetLongField(env, obj, _fid, 0);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJDecompressor::init() */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_init
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  jclass cls;
shun-iwasawa 82a8f5
  jfieldID fid;
shun-iwasawa 82a8f5
  tjhandle handle;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((handle = tjInitDecompress()) == NULL)
shun-iwasawa 82a8f5
    THROW(tjGetErrorStr(), "org/libjpegturbo/turbojpeg/TJException");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(cls = (*env)->GetObjectClass(env, obj));
shun-iwasawa 82a8f5
  BAILIF0(fid = (*env)->GetFieldID(env, cls, "handle", "J"));
shun-iwasawa 82a8f5
  (*env)->SetLongField(env, obj, fid, (size_t)handle);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJDecompressor::getScalingFactors() */
shun-iwasawa 82a8f5
JNIEXPORT jobjectArray JNICALL Java_org_libjpegturbo_turbojpeg_TJ_getScalingFactors
shun-iwasawa 82a8f5
  (JNIEnv *env, jclass cls)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  jclass sfcls = NULL;
shun-iwasawa 82a8f5
  jfieldID fid = 0;
shun-iwasawa 82a8f5
  tjscalingfactor *sf = NULL;
shun-iwasawa 82a8f5
  int n = 0, i;
shun-iwasawa 82a8f5
  jobject sfobj = NULL;
shun-iwasawa 82a8f5
  jobjectArray sfjava = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((sf = tjGetScalingFactors(&n)) == NULL || n == 0)
shun-iwasawa 82a8f5
    THROW_ARG(tjGetErrorStr());
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(sfcls = (*env)->FindClass(env,
shun-iwasawa 82a8f5
    "org/libjpegturbo/turbojpeg/TJScalingFactor"));
shun-iwasawa 82a8f5
  BAILIF0(sfjava = (jobjectArray)(*env)->NewObjectArray(env, n, sfcls, 0));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < n; i++) {
shun-iwasawa 82a8f5
    BAILIF0(sfobj = (*env)->AllocObject(env, sfcls));
shun-iwasawa 82a8f5
    BAILIF0(fid = (*env)->GetFieldID(env, sfcls, "num", "I"));
shun-iwasawa 82a8f5
    (*env)->SetIntField(env, sfobj, fid, sf[i].num);
shun-iwasawa 82a8f5
    BAILIF0(fid = (*env)->GetFieldID(env, sfcls, "denom", "I"));
shun-iwasawa 82a8f5
    (*env)->SetIntField(env, sfobj, fid, sf[i].denom);
shun-iwasawa 82a8f5
    (*env)->SetObjectArrayElement(env, sfjava, i, sfobj);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return sfjava;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJDecompressor::decompressHeader() */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressHeader
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjhandle handle = 0;
shun-iwasawa 82a8f5
  unsigned char *jpegBuf = NULL;
shun-iwasawa 82a8f5
  int width = 0, height = 0, jpegSubsamp = -1, jpegColorspace = -1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_HANDLE();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, src) < jpegSize)
shun-iwasawa 82a8f5
    THROW_ARG("Source buffer is not large enough");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (tjDecompressHeader3(handle, jpegBuf, (unsigned long)jpegSize, &width,
shun-iwasawa 82a8f5
                          &height, &jpegSubsamp, &jpegColorspace) == -1) {
shun-iwasawa 82a8f5
    SAFE_RELEASE(src, jpegBuf);
shun-iwasawa 82a8f5
    THROW_TJ();
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  SAFE_RELEASE(src, jpegBuf);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
shun-iwasawa 82a8f5
  (*env)->SetIntField(env, obj, _fid, jpegSubsamp);
shun-iwasawa 82a8f5
  if ((_fid = (*env)->GetFieldID(env, _cls, "jpegColorspace", "I")) == 0)
shun-iwasawa 82a8f5
    (*env)->ExceptionClear(env);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    (*env)->SetIntField(env, obj, _fid, jpegColorspace);
shun-iwasawa 82a8f5
  BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
shun-iwasawa 82a8f5
  (*env)->SetIntField(env, obj, _fid, width);
shun-iwasawa 82a8f5
  BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
shun-iwasawa 82a8f5
  (*env)->SetIntField(env, obj, _fid, height);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  SAFE_RELEASE(src, jpegBuf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void TJDecompressor_decompress
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jarray dst,
shun-iwasawa 82a8f5
   jint dstElementSize, jint x, jint y, jint width, jint pitch, jint height,
shun-iwasawa 82a8f5
   jint pf, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjhandle handle = 0;
shun-iwasawa 82a8f5
  jsize arraySize = 0, actualPitch;
shun-iwasawa 82a8f5
  unsigned char *jpegBuf = NULL, *dstBuf = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_HANDLE();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
shun-iwasawa 82a8f5
    THROW_ARG("Invalid argument in decompress()");
shun-iwasawa 82a8f5
  if (org_libjpegturbo_turbojpeg_TJ_NUMPF != TJ_NUMPF)
shun-iwasawa 82a8f5
    THROW_ARG("Mismatch between Java and C API");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, src) < jpegSize)
shun-iwasawa 82a8f5
    THROW_ARG("Source buffer is not large enough");
shun-iwasawa 82a8f5
  actualPitch = (pitch == 0) ? width * tjPixelSize[pf] : pitch;
shun-iwasawa 82a8f5
  arraySize = (y + height - 1) * actualPitch + (x + width) * tjPixelSize[pf];
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, dst) * dstElementSize < arraySize)
shun-iwasawa 82a8f5
    THROW_ARG("Destination buffer is not large enough");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
shun-iwasawa 82a8f5
  BAILIF0(dstBuf = (*env)->GetPrimitiveArrayCritical(env, dst, 0));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize,
shun-iwasawa 82a8f5
                    &dstBuf[y * actualPitch + x * tjPixelSize[pf]], width,
shun-iwasawa 82a8f5
                    pitch, height, pf, flags) == -1) {
shun-iwasawa 82a8f5
    SAFE_RELEASE(dst, dstBuf);
shun-iwasawa 82a8f5
    SAFE_RELEASE(src, jpegBuf);
shun-iwasawa 82a8f5
    THROW_TJ();
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  SAFE_RELEASE(dst, dstBuf);
shun-iwasawa 82a8f5
  SAFE_RELEASE(src, jpegBuf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.3.x: TJDecompressor::decompress() byte destination */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIIIII
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
shun-iwasawa 82a8f5
   jint x, jint y, jint width, jint pitch, jint height, jint pf, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  TJDecompressor_decompress(env, obj, src, jpegSize, dst, 1, x, y, width,
shun-iwasawa 82a8f5
                            pitch, height, pf, flags);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJDecompressor::decompress() byte destination */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIII
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
shun-iwasawa 82a8f5
   jint width, jint pitch, jint height, jint pf, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  TJDecompressor_decompress(env, obj, src, jpegSize, dst, 1, 0, 0, width,
shun-iwasawa 82a8f5
                            pitch, height, pf, flags);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.3.x: TJDecompressor::decompress() int destination */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIIIII
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst,
shun-iwasawa 82a8f5
   jint x, jint y, jint width, jint stride, jint height, jint pf, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
shun-iwasawa 82a8f5
    THROW_ARG("Invalid argument in decompress()");
shun-iwasawa 82a8f5
  if (tjPixelSize[pf] != sizeof(jint))
shun-iwasawa 82a8f5
    THROW_ARG("Pixel format must be 32-bit when decompressing to an integer buffer.");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  TJDecompressor_decompress(env, obj, src, jpegSize, dst, sizeof(jint), x, y,
shun-iwasawa 82a8f5
                            width, stride * sizeof(jint), height, pf, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJDecompressor::decompress() int destination */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIII
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst,
shun-iwasawa 82a8f5
   jint width, jint stride, jint height, jint pf, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
shun-iwasawa 82a8f5
    THROW_ARG("Invalid argument in decompress()");
shun-iwasawa 82a8f5
  if (tjPixelSize[pf] != sizeof(jint))
shun-iwasawa 82a8f5
    THROW_ARG("Pixel format must be 32-bit when decompressing to an integer buffer.");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  TJDecompressor_decompress(env, obj, src, jpegSize, dst, sizeof(jint), 0, 0,
shun-iwasawa 82a8f5
                            width, stride * sizeof(jint), height, pf, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.4.x: TJDecompressor::decompressToYUV() */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV___3BI_3_3B_3II_3III
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize,
shun-iwasawa 82a8f5
   jobjectArray dstobjs, jintArray jDstOffsets, jint desiredWidth,
shun-iwasawa 82a8f5
   jintArray jDstStrides, jint desiredHeight, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjhandle handle = 0;
shun-iwasawa 82a8f5
  unsigned char *jpegBuf = NULL;
shun-iwasawa 82a8f5
  jbyteArray jDstPlanes[3] = { NULL, NULL, NULL };
shun-iwasawa 82a8f5
  unsigned char *dstPlanesTmp[3] = { NULL, NULL, NULL };
shun-iwasawa 82a8f5
  unsigned char *dstPlanes[3] = { NULL, NULL, NULL };
shun-iwasawa 82a8f5
  int *dstOffsetsTmp = NULL, dstOffsets[3] = { 0, 0, 0 };
shun-iwasawa 82a8f5
  int *dstStridesTmp = NULL, dstStrides[3] = { 0, 0, 0 };
shun-iwasawa 82a8f5
  int jpegSubsamp = -1, jpegWidth = 0, jpegHeight = 0;
shun-iwasawa 82a8f5
  int nc = 0, i, width, height, scaledWidth, scaledHeight, nsf = 0;
shun-iwasawa 82a8f5
  tjscalingfactor *sf;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_HANDLE();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, src) < jpegSize)
shun-iwasawa 82a8f5
    THROW_ARG("Source buffer is not large enough");
shun-iwasawa 82a8f5
  BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
shun-iwasawa 82a8f5
  jpegSubsamp = (int)(*env)->GetIntField(env, obj, _fid);
shun-iwasawa 82a8f5
  BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
shun-iwasawa 82a8f5
  jpegWidth = (int)(*env)->GetIntField(env, obj, _fid);
shun-iwasawa 82a8f5
  BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
shun-iwasawa 82a8f5
  jpegHeight = (int)(*env)->GetIntField(env, obj, _fid);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  nc = (jpegSubsamp == org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY ? 1 : 3);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  width = desiredWidth;
shun-iwasawa 82a8f5
  height = desiredHeight;
shun-iwasawa 82a8f5
  if (width == 0) width = jpegWidth;
shun-iwasawa 82a8f5
  if (height == 0) height = jpegHeight;
shun-iwasawa 82a8f5
  sf = tjGetScalingFactors(&nsf);
shun-iwasawa 82a8f5
  if (!sf || nsf < 1)
shun-iwasawa 82a8f5
    THROW_ARG(tjGetErrorStr());
shun-iwasawa 82a8f5
  for (i = 0; i < nsf; i++) {
shun-iwasawa 82a8f5
    scaledWidth = TJSCALED(jpegWidth, sf[i]);
shun-iwasawa 82a8f5
    scaledHeight = TJSCALED(jpegHeight, sf[i]);
shun-iwasawa 82a8f5
    if (scaledWidth <= width && scaledHeight <= height)
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (i >= nsf)
shun-iwasawa 82a8f5
    THROW_ARG("Could not scale down to desired image dimensions");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(dstOffsetsTmp =
shun-iwasawa 82a8f5
          (*env)->GetPrimitiveArrayCritical(env, jDstOffsets, 0));
shun-iwasawa 82a8f5
  for (i = 0; i < nc; i++) dstOffsets[i] = dstOffsetsTmp[i];
shun-iwasawa 82a8f5
  SAFE_RELEASE(jDstOffsets, dstOffsetsTmp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(dstStridesTmp =
shun-iwasawa 82a8f5
          (*env)->GetPrimitiveArrayCritical(env, jDstStrides, 0));
shun-iwasawa 82a8f5
  for (i = 0; i < nc; i++) dstStrides[i] = dstStridesTmp[i];
shun-iwasawa 82a8f5
  SAFE_RELEASE(jDstStrides, dstStridesTmp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < nc; i++) {
shun-iwasawa 82a8f5
    int planeSize = tjPlaneSizeYUV(i, scaledWidth, dstStrides[i], scaledHeight,
shun-iwasawa 82a8f5
                                   jpegSubsamp);
shun-iwasawa 82a8f5
    int pw = tjPlaneWidth(i, scaledWidth, jpegSubsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (planeSize < 0 || pw < 0)
shun-iwasawa 82a8f5
      THROW_ARG(tjGetErrorStr());
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (dstOffsets[i] < 0)
shun-iwasawa 82a8f5
      THROW_ARG("Invalid argument in decompressToYUV()");
shun-iwasawa 82a8f5
    if (dstStrides[i] < 0 && dstOffsets[i] - planeSize + pw < 0)
shun-iwasawa 82a8f5
      THROW_ARG("Negative plane stride would cause memory to be accessed below plane boundary");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    BAILIF0(jDstPlanes[i] = (*env)->GetObjectArrayElement(env, dstobjs, i));
shun-iwasawa 82a8f5
    if ((*env)->GetArrayLength(env, jDstPlanes[i]) <
shun-iwasawa 82a8f5
        dstOffsets[i] + planeSize)
shun-iwasawa 82a8f5
      THROW_ARG("Destination plane is not large enough");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    BAILIF0(dstPlanesTmp[i] =
shun-iwasawa 82a8f5
            (*env)->GetPrimitiveArrayCritical(env, jDstPlanes[i], 0));
shun-iwasawa 82a8f5
    dstPlanes[i] = &dstPlanesTmp[i][dstOffsets[i]];
shun-iwasawa 82a8f5
    SAFE_RELEASE(jDstPlanes[i], dstPlanesTmp[i]);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  BAILIF0(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (tjDecompressToYUVPlanes(handle, jpegBuf, (unsigned long)jpegSize,
shun-iwasawa 82a8f5
                              dstPlanes, desiredWidth, dstStrides,
shun-iwasawa 82a8f5
                              desiredHeight, flags) == -1) {
shun-iwasawa 82a8f5
    SAFE_RELEASE(src, jpegBuf);
shun-iwasawa 82a8f5
    THROW_TJ();
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  SAFE_RELEASE(src, jpegBuf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJDecompressor::decompressToYUV() */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV___3BI_3BI
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst,
shun-iwasawa 82a8f5
   jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjhandle handle = 0;
shun-iwasawa 82a8f5
  unsigned char *jpegBuf = NULL, *dstBuf = NULL;
shun-iwasawa 82a8f5
  int jpegSubsamp = -1, jpegWidth = 0, jpegHeight = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_HANDLE();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, src) < jpegSize)
shun-iwasawa 82a8f5
    THROW_ARG("Source buffer is not large enough");
shun-iwasawa 82a8f5
  BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
shun-iwasawa 82a8f5
  jpegSubsamp = (int)(*env)->GetIntField(env, obj, _fid);
shun-iwasawa 82a8f5
  BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
shun-iwasawa 82a8f5
  jpegWidth = (int)(*env)->GetIntField(env, obj, _fid);
shun-iwasawa 82a8f5
  BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
shun-iwasawa 82a8f5
  jpegHeight = (int)(*env)->GetIntField(env, obj, _fid);
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, dst) <
shun-iwasawa 82a8f5
      (jsize)tjBufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp))
shun-iwasawa 82a8f5
    THROW_ARG("Destination buffer is not large enough");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, src, 0));
shun-iwasawa 82a8f5
  BAILIF0(dstBuf = (*env)->GetPrimitiveArrayCritical(env, dst, 0));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (tjDecompressToYUV(handle, jpegBuf, (unsigned long)jpegSize, dstBuf,
shun-iwasawa 82a8f5
                        flags) == -1) {
shun-iwasawa 82a8f5
    SAFE_RELEASE(dst, dstBuf);
shun-iwasawa 82a8f5
    SAFE_RELEASE(src, jpegBuf);
shun-iwasawa 82a8f5
    THROW_TJ();
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  SAFE_RELEASE(dst, dstBuf);
shun-iwasawa 82a8f5
  SAFE_RELEASE(src, jpegBuf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void TJDecompressor_decodeYUV
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
shun-iwasawa 82a8f5
   jintArray jSrcStrides, jint subsamp, jarray dst, jint dstElementSize,
shun-iwasawa 82a8f5
   jint x, jint y, jint width, jint pitch, jint height, jint pf, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjhandle handle = 0;
shun-iwasawa 82a8f5
  jsize arraySize = 0, actualPitch;
shun-iwasawa 82a8f5
  jbyteArray jSrcPlanes[3] = { NULL, NULL, NULL };
shun-iwasawa 82a8f5
  const unsigned char *srcPlanesTmp[3] = { NULL, NULL, NULL };
shun-iwasawa 82a8f5
  const unsigned char *srcPlanes[3] = { NULL, NULL, NULL };
shun-iwasawa 82a8f5
  int *srcOffsetsTmp = NULL, srcOffsets[3] = { 0, 0, 0 };
shun-iwasawa 82a8f5
  int *srcStridesTmp = NULL, srcStrides[3] = { 0, 0, 0 };
shun-iwasawa 82a8f5
  unsigned char *dstBuf = NULL;
shun-iwasawa 82a8f5
  int nc = (subsamp == org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY ? 1 : 3), i;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_HANDLE();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF || subsamp < 0 ||
shun-iwasawa 82a8f5
      subsamp >= org_libjpegturbo_turbojpeg_TJ_NUMSAMP)
shun-iwasawa 82a8f5
    THROW_ARG("Invalid argument in decodeYUV()");
shun-iwasawa 82a8f5
  if (org_libjpegturbo_turbojpeg_TJ_NUMPF != TJ_NUMPF ||
shun-iwasawa 82a8f5
      org_libjpegturbo_turbojpeg_TJ_NUMSAMP != TJ_NUMSAMP)
shun-iwasawa 82a8f5
    THROW_ARG("Mismatch between Java and C API");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, srcobjs) < nc)
shun-iwasawa 82a8f5
    THROW_ARG("Planes array is too small for the subsampling type");
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, jSrcOffsets) < nc)
shun-iwasawa 82a8f5
    THROW_ARG("Offsets array is too small for the subsampling type");
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, jSrcStrides) < nc)
shun-iwasawa 82a8f5
    THROW_ARG("Strides array is too small for the subsampling type");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  actualPitch = (pitch == 0) ? width * tjPixelSize[pf] : pitch;
shun-iwasawa 82a8f5
  arraySize = (y + height - 1) * actualPitch + (x + width) * tjPixelSize[pf];
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, dst) * dstElementSize < arraySize)
shun-iwasawa 82a8f5
    THROW_ARG("Destination buffer is not large enough");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(srcOffsetsTmp =
shun-iwasawa 82a8f5
          (*env)->GetPrimitiveArrayCritical(env, jSrcOffsets, 0));
shun-iwasawa 82a8f5
  for (i = 0; i < nc; i++) srcOffsets[i] = srcOffsetsTmp[i];
shun-iwasawa 82a8f5
  SAFE_RELEASE(jSrcOffsets, srcOffsetsTmp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(srcStridesTmp =
shun-iwasawa 82a8f5
          (*env)->GetPrimitiveArrayCritical(env, jSrcStrides, 0));
shun-iwasawa 82a8f5
  for (i = 0; i < nc; i++) srcStrides[i] = srcStridesTmp[i];
shun-iwasawa 82a8f5
  SAFE_RELEASE(jSrcStrides, srcStridesTmp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < nc; i++) {
shun-iwasawa 82a8f5
    int planeSize = tjPlaneSizeYUV(i, width, srcStrides[i], height, subsamp);
shun-iwasawa 82a8f5
    int pw = tjPlaneWidth(i, width, subsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (planeSize < 0 || pw < 0)
shun-iwasawa 82a8f5
      THROW_ARG(tjGetErrorStr());
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (srcOffsets[i] < 0)
shun-iwasawa 82a8f5
      THROW_ARG("Invalid argument in decodeYUV()");
shun-iwasawa 82a8f5
    if (srcStrides[i] < 0 && srcOffsets[i] - planeSize + pw < 0)
shun-iwasawa 82a8f5
      THROW_ARG("Negative plane stride would cause memory to be accessed below plane boundary");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    BAILIF0(jSrcPlanes[i] = (*env)->GetObjectArrayElement(env, srcobjs, i));
shun-iwasawa 82a8f5
    if ((*env)->GetArrayLength(env, jSrcPlanes[i]) <
shun-iwasawa 82a8f5
        srcOffsets[i] + planeSize)
shun-iwasawa 82a8f5
      THROW_ARG("Source plane is not large enough");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    BAILIF0(srcPlanesTmp[i] =
shun-iwasawa 82a8f5
            (*env)->GetPrimitiveArrayCritical(env, jSrcPlanes[i], 0));
shun-iwasawa 82a8f5
    srcPlanes[i] = &srcPlanesTmp[i][srcOffsets[i]];
shun-iwasawa 82a8f5
    SAFE_RELEASE(jSrcPlanes[i], srcPlanesTmp[i]);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  BAILIF0(dstBuf = (*env)->GetPrimitiveArrayCritical(env, dst, 0));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (tjDecodeYUVPlanes(handle, srcPlanes, srcStrides, subsamp,
shun-iwasawa 82a8f5
                        &dstBuf[y * actualPitch + x * tjPixelSize[pf]], width,
shun-iwasawa 82a8f5
                        pitch, height, pf, flags) == -1) {
shun-iwasawa 82a8f5
    SAFE_RELEASE(dst, dstBuf);
shun-iwasawa 82a8f5
    THROW_TJ();
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  SAFE_RELEASE(dst, dstBuf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.4.x: TJDecompressor::decodeYUV() byte destination */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decodeYUV___3_3B_3I_3II_3BIIIIIII
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
shun-iwasawa 82a8f5
   jintArray jSrcStrides, jint subsamp, jbyteArray dst, jint x, jint y,
shun-iwasawa 82a8f5
   jint width, jint pitch, jint height, jint pf, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  TJDecompressor_decodeYUV(env, obj, srcobjs, jSrcOffsets, jSrcStrides,
shun-iwasawa 82a8f5
                           subsamp, dst, 1, x, y, width, pitch, height, pf,
shun-iwasawa 82a8f5
                           flags);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.4.x: TJDecompressor::decodeYUV() int destination */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decodeYUV___3_3B_3I_3II_3IIIIIIII
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets,
shun-iwasawa 82a8f5
   jintArray jSrcStrides, jint subsamp, jintArray dst, jint x, jint y,
shun-iwasawa 82a8f5
   jint width, jint stride, jint height, jint pf, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (pf < 0 || pf >= org_libjpegturbo_turbojpeg_TJ_NUMPF)
shun-iwasawa 82a8f5
    THROW_ARG("Invalid argument in decodeYUV()");
shun-iwasawa 82a8f5
  if (tjPixelSize[pf] != sizeof(jint))
shun-iwasawa 82a8f5
    THROW_ARG("Pixel format must be 32-bit when decoding to an integer buffer.");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  TJDecompressor_decodeYUV(env, obj, srcobjs, jSrcOffsets, jSrcStrides,
shun-iwasawa 82a8f5
                           subsamp, dst, sizeof(jint), x, y, width,
shun-iwasawa 82a8f5
                           stride * sizeof(jint), height, pf, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJTransformer::init() */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_init
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  jclass cls;
shun-iwasawa 82a8f5
  jfieldID fid;
shun-iwasawa 82a8f5
  tjhandle handle;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((handle = tjInitTransform()) == NULL)
shun-iwasawa 82a8f5
    THROW(tjGetErrorStr(), "org/libjpegturbo/turbojpeg/TJException");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(cls = (*env)->GetObjectClass(env, obj));
shun-iwasawa 82a8f5
  BAILIF0(fid = (*env)->GetFieldID(env, cls, "handle", "J"));
shun-iwasawa 82a8f5
  (*env)->SetLongField(env, obj, fid, (size_t)handle);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct _JNICustomFilterParams {
shun-iwasawa 82a8f5
  JNIEnv *env;
shun-iwasawa 82a8f5
  jobject tobj;
shun-iwasawa 82a8f5
  jobject cfobj;
shun-iwasawa 82a8f5
} JNICustomFilterParams;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int JNICustomFilter(short *coeffs, tjregion arrayRegion,
shun-iwasawa 82a8f5
                           tjregion planeRegion, int componentIndex,
shun-iwasawa 82a8f5
                           int transformIndex, tjtransform *transform)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  JNICustomFilterParams *params = (JNICustomFilterParams *)transform->data;
shun-iwasawa 82a8f5
  JNIEnv *env = params->env;
shun-iwasawa 82a8f5
  jobject tobj = params->tobj, cfobj = params->cfobj;
shun-iwasawa 82a8f5
  jobject arrayRegionObj, planeRegionObj, bufobj, borobj;
shun-iwasawa 82a8f5
  jclass cls;
shun-iwasawa 82a8f5
  jmethodID mid;
shun-iwasawa 82a8f5
  jfieldID fid;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(bufobj = (*env)->NewDirectByteBuffer(env, coeffs,
shun-iwasawa 82a8f5
    sizeof(short) * arrayRegion.w * arrayRegion.h));
shun-iwasawa 82a8f5
  BAILIF0(cls = (*env)->FindClass(env, "java/nio/ByteOrder"));
shun-iwasawa 82a8f5
  BAILIF0(mid = (*env)->GetStaticMethodID(env, cls, "nativeOrder",
shun-iwasawa 82a8f5
                                          "()Ljava/nio/ByteOrder;"));
shun-iwasawa 82a8f5
  BAILIF0(borobj = (*env)->CallStaticObjectMethod(env, cls, mid));
shun-iwasawa 82a8f5
  BAILIF0(cls = (*env)->GetObjectClass(env, bufobj));
shun-iwasawa 82a8f5
  BAILIF0(mid = (*env)->GetMethodID(env, cls, "order",
shun-iwasawa 82a8f5
    "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;"));
shun-iwasawa 82a8f5
  (*env)->CallObjectMethod(env, bufobj, mid, borobj);
shun-iwasawa 82a8f5
  BAILIF0(mid = (*env)->GetMethodID(env, cls, "asShortBuffer",
shun-iwasawa 82a8f5
                                    "()Ljava/nio/ShortBuffer;"));
shun-iwasawa 82a8f5
  BAILIF0(bufobj = (*env)->CallObjectMethod(env, bufobj, mid));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(cls = (*env)->FindClass(env, "java/awt/Rectangle"));
shun-iwasawa 82a8f5
  BAILIF0(arrayRegionObj = (*env)->AllocObject(env, cls));
shun-iwasawa 82a8f5
  BAILIF0(fid = (*env)->GetFieldID(env, cls, "x", "I"));
shun-iwasawa 82a8f5
  (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.x);
shun-iwasawa 82a8f5
  BAILIF0(fid = (*env)->GetFieldID(env, cls, "y", "I"));
shun-iwasawa 82a8f5
  (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.y);
shun-iwasawa 82a8f5
  BAILIF0(fid = (*env)->GetFieldID(env, cls, "width", "I"));
shun-iwasawa 82a8f5
  (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.w);
shun-iwasawa 82a8f5
  BAILIF0(fid = (*env)->GetFieldID(env, cls, "height", "I"));
shun-iwasawa 82a8f5
  (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.h);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(planeRegionObj = (*env)->AllocObject(env, cls));
shun-iwasawa 82a8f5
  BAILIF0(fid = (*env)->GetFieldID(env, cls, "x", "I"));
shun-iwasawa 82a8f5
  (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.x);
shun-iwasawa 82a8f5
  BAILIF0(fid = (*env)->GetFieldID(env, cls, "y", "I"));
shun-iwasawa 82a8f5
  (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.y);
shun-iwasawa 82a8f5
  BAILIF0(fid = (*env)->GetFieldID(env, cls, "width", "I"));
shun-iwasawa 82a8f5
  (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.w);
shun-iwasawa 82a8f5
  BAILIF0(fid = (*env)->GetFieldID(env, cls, "height", "I"));
shun-iwasawa 82a8f5
  (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.h);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  BAILIF0(cls = (*env)->GetObjectClass(env, cfobj));
shun-iwasawa 82a8f5
  BAILIF0(mid = (*env)->GetMethodID(env, cls, "customFilter",
shun-iwasawa 82a8f5
    "(Ljava/nio/ShortBuffer;Ljava/awt/Rectangle;Ljava/awt/Rectangle;IILorg/libjpegturbo/turbojpeg/TJTransform;)V"));
shun-iwasawa 82a8f5
  (*env)->CallVoidMethod(env, cfobj, mid, bufobj, arrayRegionObj,
shun-iwasawa 82a8f5
                         planeRegionObj, componentIndex, transformIndex, tobj);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return -1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJTransformer::transform() */
shun-iwasawa 82a8f5
JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transform
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize,
shun-iwasawa 82a8f5
   jobjectArray dstobjs, jobjectArray tobjs, jint flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjhandle handle = 0;
shun-iwasawa 82a8f5
  unsigned char *jpegBuf = NULL, **dstBufs = NULL;
shun-iwasawa 82a8f5
  jsize n = 0;
shun-iwasawa 82a8f5
  unsigned long *dstSizes = NULL;
shun-iwasawa 82a8f5
  tjtransform *t = NULL;
shun-iwasawa 82a8f5
  jbyteArray *jdstBufs = NULL;
shun-iwasawa 82a8f5
  int i, jpegWidth = 0, jpegHeight = 0, jpegSubsamp;
shun-iwasawa 82a8f5
  jintArray jdstSizes = 0;
shun-iwasawa 82a8f5
  jint *dstSizesi = NULL;
shun-iwasawa 82a8f5
  JNICustomFilterParams *params = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_HANDLE();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((*env)->GetArrayLength(env, jsrcBuf) < jpegSize)
shun-iwasawa 82a8f5
    THROW_ARG("Source buffer is not large enough");
shun-iwasawa 82a8f5
  BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegWidth", "I"));
shun-iwasawa 82a8f5
  jpegWidth = (int)(*env)->GetIntField(env, obj, _fid);
shun-iwasawa 82a8f5
  BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegHeight", "I"));
shun-iwasawa 82a8f5
  jpegHeight = (int)(*env)->GetIntField(env, obj, _fid);
shun-iwasawa 82a8f5
  BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "jpegSubsamp", "I"));
shun-iwasawa 82a8f5
  jpegSubsamp = (int)(*env)->GetIntField(env, obj, _fid);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  n = (*env)->GetArrayLength(env, dstobjs);
shun-iwasawa 82a8f5
  if (n != (*env)->GetArrayLength(env, tobjs))
shun-iwasawa 82a8f5
    THROW_ARG("Mismatch between size of transforms array and destination buffers array");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((dstBufs =
shun-iwasawa 82a8f5
       (unsigned char **)malloc(sizeof(unsigned char *) * n)) == NULL)
shun-iwasawa 82a8f5
    THROW_MEM();
shun-iwasawa 82a8f5
  if ((jdstBufs = (jbyteArray *)malloc(sizeof(jbyteArray) * n)) == NULL)
shun-iwasawa 82a8f5
    THROW_MEM();
shun-iwasawa 82a8f5
  if ((dstSizes = (unsigned long *)malloc(sizeof(unsigned long) * n)) == NULL)
shun-iwasawa 82a8f5
    THROW_MEM();
shun-iwasawa 82a8f5
  if ((t = (tjtransform *)malloc(sizeof(tjtransform) * n)) == NULL)
shun-iwasawa 82a8f5
    THROW_MEM();
shun-iwasawa 82a8f5
  if ((params = (JNICustomFilterParams *)malloc(sizeof(JNICustomFilterParams) *
shun-iwasawa 82a8f5
                                                n)) == NULL)
shun-iwasawa 82a8f5
    THROW_MEM();
shun-iwasawa 82a8f5
  for (i = 0; i < n; i++) {
shun-iwasawa 82a8f5
    dstBufs[i] = NULL;  jdstBufs[i] = NULL;  dstSizes[i] = 0;
shun-iwasawa 82a8f5
    memset(&t[i], 0, sizeof(tjtransform));
shun-iwasawa 82a8f5
    memset(¶ms[i], 0, sizeof(JNICustomFilterParams));
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < n; i++) {
shun-iwasawa 82a8f5
    jobject tobj, cfobj;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    BAILIF0(tobj = (*env)->GetObjectArrayElement(env, tobjs, i));
shun-iwasawa 82a8f5
    BAILIF0(_cls = (*env)->GetObjectClass(env, tobj));
shun-iwasawa 82a8f5
    BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "op", "I"));
shun-iwasawa 82a8f5
    t[i].op = (*env)->GetIntField(env, tobj, _fid);
shun-iwasawa 82a8f5
    BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "options", "I"));
shun-iwasawa 82a8f5
    t[i].options = (*env)->GetIntField(env, tobj, _fid);
shun-iwasawa 82a8f5
    BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "x", "I"));
shun-iwasawa 82a8f5
    t[i].r.x = (*env)->GetIntField(env, tobj, _fid);
shun-iwasawa 82a8f5
    BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "y", "I"));
shun-iwasawa 82a8f5
    t[i].r.y = (*env)->GetIntField(env, tobj, _fid);
shun-iwasawa 82a8f5
    BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "width", "I"));
shun-iwasawa 82a8f5
    t[i].r.w = (*env)->GetIntField(env, tobj, _fid);
shun-iwasawa 82a8f5
    BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "height", "I"));
shun-iwasawa 82a8f5
    t[i].r.h = (*env)->GetIntField(env, tobj, _fid);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    BAILIF0(_fid = (*env)->GetFieldID(env, _cls, "cf",
shun-iwasawa 82a8f5
      "Lorg/libjpegturbo/turbojpeg/TJCustomFilter;"));
shun-iwasawa 82a8f5
    cfobj = (*env)->GetObjectField(env, tobj, _fid);
shun-iwasawa 82a8f5
    if (cfobj) {
shun-iwasawa 82a8f5
      params[i].env = env;
shun-iwasawa 82a8f5
      params[i].tobj = tobj;
shun-iwasawa 82a8f5
      params[i].cfobj = cfobj;
shun-iwasawa 82a8f5
      t[i].customFilter = JNICustomFilter;
shun-iwasawa 82a8f5
      t[i].data = (void *)¶ms[i];
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < n; i++) {
shun-iwasawa 82a8f5
    int w = jpegWidth, h = jpegHeight;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (t[i].r.w != 0) w = t[i].r.w;
shun-iwasawa 82a8f5
    if (t[i].r.h != 0) h = t[i].r.h;
shun-iwasawa 82a8f5
    BAILIF0(jdstBufs[i] = (*env)->GetObjectArrayElement(env, dstobjs, i));
shun-iwasawa 82a8f5
    if ((unsigned long)(*env)->GetArrayLength(env, jdstBufs[i]) <
shun-iwasawa 82a8f5
        tjBufSize(w, h, jpegSubsamp))
shun-iwasawa 82a8f5
      THROW_ARG("Destination buffer is not large enough");
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  BAILIF0(jpegBuf = (*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0));
shun-iwasawa 82a8f5
  for (i = 0; i < n; i++)
shun-iwasawa 82a8f5
    BAILIF0(dstBufs[i] =
shun-iwasawa 82a8f5
            (*env)->GetPrimitiveArrayCritical(env, jdstBufs[i], 0));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (tjTransform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t,
shun-iwasawa 82a8f5
                  flags | TJFLAG_NOREALLOC) == -1) {
shun-iwasawa 82a8f5
    for (i = 0; i < n; i++)
shun-iwasawa 82a8f5
      SAFE_RELEASE(jdstBufs[i], dstBufs[i]);
shun-iwasawa 82a8f5
    SAFE_RELEASE(jsrcBuf, jpegBuf);
shun-iwasawa 82a8f5
    THROW_TJ();
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < n; i++)
shun-iwasawa 82a8f5
    SAFE_RELEASE(jdstBufs[i], dstBufs[i]);
shun-iwasawa 82a8f5
  SAFE_RELEASE(jsrcBuf, jpegBuf);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jdstSizes = (*env)->NewIntArray(env, n);
shun-iwasawa 82a8f5
  BAILIF0(dstSizesi = (*env)->GetIntArrayElements(env, jdstSizes, 0));
shun-iwasawa 82a8f5
  for (i = 0; i < n; i++) dstSizesi[i] = (int)dstSizes[i];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (dstSizesi) (*env)->ReleaseIntArrayElements(env, jdstSizes, dstSizesi, 0);
shun-iwasawa 82a8f5
  if (dstBufs) {
shun-iwasawa 82a8f5
    for (i = 0; i < n; i++) {
shun-iwasawa 82a8f5
      if (dstBufs[i] && jdstBufs && jdstBufs[i])
shun-iwasawa 82a8f5
        (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    free(dstBufs);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  SAFE_RELEASE(jsrcBuf, jpegBuf);
shun-iwasawa 82a8f5
  free(jdstBufs);
shun-iwasawa 82a8f5
  free(dstSizes);
shun-iwasawa 82a8f5
  free(t);
shun-iwasawa 82a8f5
  return jdstSizes;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* TurboJPEG 1.2.x: TJDecompressor::destroy() */
shun-iwasawa 82a8f5
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_destroy
shun-iwasawa 82a8f5
  (JNIEnv *env, jobject obj)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy(env, obj);
shun-iwasawa 82a8f5
}