shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Copyright (C)2009-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
/* TurboJPEG/LJT:  this implements the TurboJPEG API using libjpeg or
shun-iwasawa 82a8f5
   libjpeg-turbo */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include <stdio.h></stdio.h>
shun-iwasawa 82a8f5
#include <stdlib.h></stdlib.h>
shun-iwasawa 82a8f5
#include <ctype.h></ctype.h>
shun-iwasawa 82a8f5
#include <jinclude.h></jinclude.h>
shun-iwasawa 82a8f5
#define JPEG_INTERNALS
shun-iwasawa 82a8f5
#include <jpeglib.h></jpeglib.h>
shun-iwasawa 82a8f5
#include <jerror.h></jerror.h>
shun-iwasawa 82a8f5
#include <setjmp.h></setjmp.h>
shun-iwasawa 82a8f5
#include <errno.h></errno.h>
shun-iwasawa 82a8f5
#include "./turbojpeg.h"
shun-iwasawa 82a8f5
#include "./tjutil.h"
shun-iwasawa 82a8f5
#include "transupp.h"
shun-iwasawa 82a8f5
#include "./jpegcomp.h"
shun-iwasawa 82a8f5
#include "./cdjpeg.h"
shun-iwasawa 82a8f5
#include "jconfigint.h"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
extern void jpeg_mem_dest_tj(j_compress_ptr, unsigned char **, unsigned long *,
shun-iwasawa 82a8f5
                             boolean);
shun-iwasawa 82a8f5
extern void jpeg_mem_src_tj(j_decompress_ptr, const unsigned char *,
shun-iwasawa 82a8f5
                            unsigned long);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PAD(v, p)  ((v + (p) - 1) & (~((p) - 1)))
shun-iwasawa 82a8f5
#define IS_POW2(x)  (((x) & (x - 1)) == 0)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Error handling (based on example in example.txt) */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static THREAD_LOCAL char errStr[JMSG_LENGTH_MAX] = "No error";
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
struct my_error_mgr {
shun-iwasawa 82a8f5
  struct jpeg_error_mgr pub;
shun-iwasawa 82a8f5
  jmp_buf setjmp_buffer;
shun-iwasawa 82a8f5
  void (*emit_message) (j_common_ptr, int);
shun-iwasawa 82a8f5
  boolean warning, stopOnWarning;
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
typedef struct my_error_mgr *my_error_ptr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define JMESSAGE(code, string)  string,
shun-iwasawa 82a8f5
static const char *turbojpeg_message_table[] = {
shun-iwasawa 82a8f5
#include "cderror.h"
shun-iwasawa 82a8f5
  NULL
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void my_error_exit(j_common_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_error_ptr myerr = (my_error_ptr)cinfo->err;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  (*cinfo->err->output_message) (cinfo);
shun-iwasawa 82a8f5
  longjmp(myerr->setjmp_buffer, 1);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Based on output_message() in jerror.c */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void my_output_message(j_common_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  (*cinfo->err->format_message) (cinfo, errStr);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void my_emit_message(j_common_ptr cinfo, int msg_level)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_error_ptr myerr = (my_error_ptr)cinfo->err;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  myerr->emit_message(cinfo, msg_level);
shun-iwasawa 82a8f5
  if (msg_level < 0) {
shun-iwasawa 82a8f5
    myerr->warning = TRUE;
shun-iwasawa 82a8f5
    if (myerr->stopOnWarning) longjmp(myerr->setjmp_buffer, 1);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Global structures, macros, etc. */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
enum { COMPRESS = 1, DECOMPRESS = 2 };
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct _tjinstance {
shun-iwasawa 82a8f5
  struct jpeg_compress_struct cinfo;
shun-iwasawa 82a8f5
  struct jpeg_decompress_struct dinfo;
shun-iwasawa 82a8f5
  struct my_error_mgr jerr;
shun-iwasawa 82a8f5
  int init, headerRead;
shun-iwasawa 82a8f5
  char errStr[JMSG_LENGTH_MAX];
shun-iwasawa 82a8f5
  boolean isInstanceError;
shun-iwasawa 82a8f5
} tjinstance;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static const int pixelsize[TJ_NUMSAMP] = { 3, 3, 3, 1, 3, 3 };
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static const JXFORM_CODE xformtypes[TJ_NUMXOP] = {
shun-iwasawa 82a8f5
  JXFORM_NONE, JXFORM_FLIP_H, JXFORM_FLIP_V, JXFORM_TRANSPOSE,
shun-iwasawa 82a8f5
  JXFORM_TRANSVERSE, JXFORM_ROT_90, JXFORM_ROT_180, JXFORM_ROT_270
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define NUMSF  16
shun-iwasawa 82a8f5
static const tjscalingfactor sf[NUMSF] = {
shun-iwasawa 82a8f5
  { 2, 1 },
shun-iwasawa 82a8f5
  { 15, 8 },
shun-iwasawa 82a8f5
  { 7, 4 },
shun-iwasawa 82a8f5
  { 13, 8 },
shun-iwasawa 82a8f5
  { 3, 2 },
shun-iwasawa 82a8f5
  { 11, 8 },
shun-iwasawa 82a8f5
  { 5, 4 },
shun-iwasawa 82a8f5
  { 9, 8 },
shun-iwasawa 82a8f5
  { 1, 1 },
shun-iwasawa 82a8f5
  { 7, 8 },
shun-iwasawa 82a8f5
  { 3, 4 },
shun-iwasawa 82a8f5
  { 5, 8 },
shun-iwasawa 82a8f5
  { 1, 2 },
shun-iwasawa 82a8f5
  { 3, 8 },
shun-iwasawa 82a8f5
  { 1, 4 },
shun-iwasawa 82a8f5
  { 1, 8 }
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static J_COLOR_SPACE pf2cs[TJ_NUMPF] = {
shun-iwasawa 82a8f5
  JCS_EXT_RGB, JCS_EXT_BGR, JCS_EXT_RGBX, JCS_EXT_BGRX, JCS_EXT_XBGR,
shun-iwasawa 82a8f5
  JCS_EXT_XRGB, JCS_GRAYSCALE, JCS_EXT_RGBA, JCS_EXT_BGRA, JCS_EXT_ABGR,
shun-iwasawa 82a8f5
  JCS_EXT_ARGB, JCS_CMYK
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int cs2pf[JPEG_NUMCS] = {
shun-iwasawa 82a8f5
  TJPF_UNKNOWN, TJPF_GRAY,
shun-iwasawa 82a8f5
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
shun-iwasawa 82a8f5
  TJPF_RGB,
shun-iwasawa 82a8f5
#elif RGB_RED == 2 && RGB_GREEN == 1 && RGB_BLUE == 0 && RGB_PIXELSIZE == 3
shun-iwasawa 82a8f5
  TJPF_BGR,
shun-iwasawa 82a8f5
#elif RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 4
shun-iwasawa 82a8f5
  TJPF_RGBX,
shun-iwasawa 82a8f5
#elif RGB_RED == 2 && RGB_GREEN == 1 && RGB_BLUE == 0 && RGB_PIXELSIZE == 4
shun-iwasawa 82a8f5
  TJPF_BGRX,
shun-iwasawa 82a8f5
#elif RGB_RED == 3 && RGB_GREEN == 2 && RGB_BLUE == 1 && RGB_PIXELSIZE == 4
shun-iwasawa 82a8f5
  TJPF_XBGR,
shun-iwasawa 82a8f5
#elif RGB_RED == 1 && RGB_GREEN == 2 && RGB_BLUE == 3 && RGB_PIXELSIZE == 4
shun-iwasawa 82a8f5
  TJPF_XRGB,
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
  TJPF_UNKNOWN, TJPF_CMYK, TJPF_UNKNOWN, TJPF_RGB, TJPF_RGBX, TJPF_BGR,
shun-iwasawa 82a8f5
  TJPF_BGRX, TJPF_XBGR, TJPF_XRGB, TJPF_RGBA, TJPF_BGRA, TJPF_ABGR, TJPF_ARGB,
shun-iwasawa 82a8f5
  TJPF_UNKNOWN
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define THROWG(m) { \
shun-iwasawa 82a8f5
  snprintf(errStr, JMSG_LENGTH_MAX, "%s", m); \
shun-iwasawa 82a8f5
  retval = -1;  goto bailout; \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
#define THROW_UNIX(m) { \
shun-iwasawa 82a8f5
  snprintf(errStr, JMSG_LENGTH_MAX, "%s\n%s", m, strerror(errno)); \
shun-iwasawa 82a8f5
  retval = -1;  goto bailout; \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
#define THROW(m) { \
shun-iwasawa 82a8f5
  snprintf(this->errStr, JMSG_LENGTH_MAX, "%s", m); \
shun-iwasawa 82a8f5
  this->isInstanceError = TRUE;  THROWG(m) \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define GET_INSTANCE(handle) \
shun-iwasawa 82a8f5
  tjinstance *this = (tjinstance *)handle; \
shun-iwasawa 82a8f5
  j_compress_ptr cinfo = NULL; \
shun-iwasawa 82a8f5
  j_decompress_ptr dinfo = NULL; \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  if (!this) { \
shun-iwasawa 82a8f5
    snprintf(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \
shun-iwasawa 82a8f5
    return -1; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
  cinfo = &this->cinfo;  dinfo = &this->dinfo; \
shun-iwasawa 82a8f5
  this->jerr.warning = FALSE; \
shun-iwasawa 82a8f5
  this->isInstanceError = FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define GET_CINSTANCE(handle) \
shun-iwasawa 82a8f5
  tjinstance *this = (tjinstance *)handle; \
shun-iwasawa 82a8f5
  j_compress_ptr cinfo = NULL; \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  if (!this) { \
shun-iwasawa 82a8f5
    snprintf(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \
shun-iwasawa 82a8f5
    return -1; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
  cinfo = &this->cinfo; \
shun-iwasawa 82a8f5
  this->jerr.warning = FALSE; \
shun-iwasawa 82a8f5
  this->isInstanceError = FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define GET_DINSTANCE(handle) \
shun-iwasawa 82a8f5
  tjinstance *this = (tjinstance *)handle; \
shun-iwasawa 82a8f5
  j_decompress_ptr dinfo = NULL; \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  if (!this) { \
shun-iwasawa 82a8f5
    snprintf(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \
shun-iwasawa 82a8f5
    return -1; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
  dinfo = &this->dinfo; \
shun-iwasawa 82a8f5
  this->jerr.warning = FALSE; \
shun-iwasawa 82a8f5
  this->isInstanceError = FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int getPixelFormat(int pixelSize, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (pixelSize == 1) return TJPF_GRAY;
shun-iwasawa 82a8f5
  if (pixelSize == 3) {
shun-iwasawa 82a8f5
    if (flags & TJ_BGR) return TJPF_BGR;
shun-iwasawa 82a8f5
    else return TJPF_RGB;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (pixelSize == 4) {
shun-iwasawa 82a8f5
    if (flags & TJ_ALPHAFIRST) {
shun-iwasawa 82a8f5
      if (flags & TJ_BGR) return TJPF_XBGR;
shun-iwasawa 82a8f5
      else return TJPF_XRGB;
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      if (flags & TJ_BGR) return TJPF_BGRX;
shun-iwasawa 82a8f5
      else return TJPF_RGBX;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return -1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void setCompDefaults(struct jpeg_compress_struct *cinfo,
shun-iwasawa 82a8f5
                            int pixelFormat, int subsamp, int jpegQual,
shun-iwasawa 82a8f5
                            int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
#ifndef NO_GETENV
shun-iwasawa 82a8f5
  char *env = NULL;
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo->in_color_space = pf2cs[pixelFormat];
shun-iwasawa 82a8f5
  cinfo->input_components = tjPixelSize[pixelFormat];
shun-iwasawa 82a8f5
  jpeg_set_defaults(cinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifndef NO_GETENV
shun-iwasawa 82a8f5
  if ((env = getenv("TJ_OPTIMIZE")) != NULL && strlen(env) > 0 &&
shun-iwasawa 82a8f5
      !strcmp(env, "1"))
shun-iwasawa 82a8f5
    cinfo->optimize_coding = TRUE;
shun-iwasawa 82a8f5
  if ((env = getenv("TJ_ARITHMETIC")) != NULL && strlen(env) > 0 &&
shun-iwasawa 82a8f5
      !strcmp(env, "1"))
shun-iwasawa 82a8f5
    cinfo->arith_code = TRUE;
shun-iwasawa 82a8f5
  if ((env = getenv("TJ_RESTART")) != NULL && strlen(env) > 0) {
shun-iwasawa 82a8f5
    int temp = -1;
shun-iwasawa 82a8f5
    char tempc = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (sscanf(env, "%d%c", &temp, &tempc) >= 1 && temp >= 0 &&
shun-iwasawa 82a8f5
        temp <= 65535) {
shun-iwasawa 82a8f5
      if (toupper(tempc) == 'B') {
shun-iwasawa 82a8f5
        cinfo->restart_interval = temp;
shun-iwasawa 82a8f5
        cinfo->restart_in_rows = 0;
shun-iwasawa 82a8f5
      } else
shun-iwasawa 82a8f5
        cinfo->restart_in_rows = temp;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (jpegQual >= 0) {
shun-iwasawa 82a8f5
    jpeg_set_quality(cinfo, jpegQual, TRUE);
shun-iwasawa 82a8f5
    if (jpegQual >= 96 || flags & TJFLAG_ACCURATEDCT)
shun-iwasawa 82a8f5
      cinfo->dct_method = JDCT_ISLOW;
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      cinfo->dct_method = JDCT_FASTEST;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (subsamp == TJSAMP_GRAY)
shun-iwasawa 82a8f5
    jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
shun-iwasawa 82a8f5
  else if (pixelFormat == TJPF_CMYK)
shun-iwasawa 82a8f5
    jpeg_set_colorspace(cinfo, JCS_YCCK);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    jpeg_set_colorspace(cinfo, JCS_YCbCr);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (flags & TJFLAG_PROGRESSIVE)
shun-iwasawa 82a8f5
    jpeg_simple_progression(cinfo);
shun-iwasawa 82a8f5
#ifndef NO_GETENV
shun-iwasawa 82a8f5
  else if ((env = getenv("TJ_PROGRESSIVE")) != NULL && strlen(env) > 0 &&
shun-iwasawa 82a8f5
           !strcmp(env, "1"))
shun-iwasawa 82a8f5
    jpeg_simple_progression(cinfo);
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo->comp_info[0].h_samp_factor = tjMCUWidth[subsamp] / 8;
shun-iwasawa 82a8f5
  cinfo->comp_info[1].h_samp_factor = 1;
shun-iwasawa 82a8f5
  cinfo->comp_info[2].h_samp_factor = 1;
shun-iwasawa 82a8f5
  if (cinfo->num_components > 3)
shun-iwasawa 82a8f5
    cinfo->comp_info[3].h_samp_factor = tjMCUWidth[subsamp] / 8;
shun-iwasawa 82a8f5
  cinfo->comp_info[0].v_samp_factor = tjMCUHeight[subsamp] / 8;
shun-iwasawa 82a8f5
  cinfo->comp_info[1].v_samp_factor = 1;
shun-iwasawa 82a8f5
  cinfo->comp_info[2].v_samp_factor = 1;
shun-iwasawa 82a8f5
  if (cinfo->num_components > 3)
shun-iwasawa 82a8f5
    cinfo->comp_info[3].v_samp_factor = tjMCUHeight[subsamp] / 8;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int getSubsamp(j_decompress_ptr dinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int retval = -1, i, k;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* The sampling factors actually have no meaning with grayscale JPEG files,
shun-iwasawa 82a8f5
     and in fact it's possible to generate grayscale JPEGs with sampling
shun-iwasawa 82a8f5
     factors > 1 (even though those sampling factors are ignored by the
shun-iwasawa 82a8f5
     decompressor.)  Thus, we need to treat grayscale as a special case. */
shun-iwasawa 82a8f5
  if (dinfo->num_components == 1 && dinfo->jpeg_color_space == JCS_GRAYSCALE)
shun-iwasawa 82a8f5
    return TJSAMP_GRAY;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < NUMSUBOPT; i++) {
shun-iwasawa 82a8f5
    if (dinfo->num_components == pixelsize[i] ||
shun-iwasawa 82a8f5
        ((dinfo->jpeg_color_space == JCS_YCCK ||
shun-iwasawa 82a8f5
          dinfo->jpeg_color_space == JCS_CMYK) &&
shun-iwasawa 82a8f5
         pixelsize[i] == 3 && dinfo->num_components == 4)) {
shun-iwasawa 82a8f5
      if (dinfo->comp_info[0].h_samp_factor == tjMCUWidth[i] / 8 &&
shun-iwasawa 82a8f5
          dinfo->comp_info[0].v_samp_factor == tjMCUHeight[i] / 8) {
shun-iwasawa 82a8f5
        int match = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        for (k = 1; k < dinfo->num_components; k++) {
shun-iwasawa 82a8f5
          int href = 1, vref = 1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
          if ((dinfo->jpeg_color_space == JCS_YCCK ||
shun-iwasawa 82a8f5
               dinfo->jpeg_color_space == JCS_CMYK) && k == 3) {
shun-iwasawa 82a8f5
            href = tjMCUWidth[i] / 8;  vref = tjMCUHeight[i] / 8;
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
          if (dinfo->comp_info[k].h_samp_factor == href &&
shun-iwasawa 82a8f5
              dinfo->comp_info[k].v_samp_factor == vref)
shun-iwasawa 82a8f5
            match++;
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
        if (match == dinfo->num_components - 1) {
shun-iwasawa 82a8f5
          retval = i;  break;
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      /* Handle 4:2:2 and 4:4:0 images whose sampling factors are specified
shun-iwasawa 82a8f5
         in non-standard ways. */
shun-iwasawa 82a8f5
      if (dinfo->comp_info[0].h_samp_factor == 2 &&
shun-iwasawa 82a8f5
          dinfo->comp_info[0].v_samp_factor == 2 &&
shun-iwasawa 82a8f5
          (i == TJSAMP_422 || i == TJSAMP_440)) {
shun-iwasawa 82a8f5
        int match = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        for (k = 1; k < dinfo->num_components; k++) {
shun-iwasawa 82a8f5
          int href = tjMCUHeight[i] / 8, vref = tjMCUWidth[i] / 8;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
          if ((dinfo->jpeg_color_space == JCS_YCCK ||
shun-iwasawa 82a8f5
               dinfo->jpeg_color_space == JCS_CMYK) && k == 3) {
shun-iwasawa 82a8f5
            href = vref = 2;
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
          if (dinfo->comp_info[k].h_samp_factor == href &&
shun-iwasawa 82a8f5
              dinfo->comp_info[k].v_samp_factor == vref)
shun-iwasawa 82a8f5
            match++;
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
        if (match == dinfo->num_components - 1) {
shun-iwasawa 82a8f5
          retval = i;  break;
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      /* Handle 4:4:4 images whose sampling factors are specified in
shun-iwasawa 82a8f5
         non-standard ways. */
shun-iwasawa 82a8f5
      if (dinfo->comp_info[0].h_samp_factor *
shun-iwasawa 82a8f5
          dinfo->comp_info[0].v_samp_factor <=
shun-iwasawa 82a8f5
          D_MAX_BLOCKS_IN_MCU / pixelsize[i] && i == TJSAMP_444) {
shun-iwasawa 82a8f5
        int match = 0;
shun-iwasawa 82a8f5
        for (k = 1; k < dinfo->num_components; k++) {
shun-iwasawa 82a8f5
          if (dinfo->comp_info[k].h_samp_factor ==
shun-iwasawa 82a8f5
              dinfo->comp_info[0].h_samp_factor &&
shun-iwasawa 82a8f5
              dinfo->comp_info[k].v_samp_factor ==
shun-iwasawa 82a8f5
              dinfo->comp_info[0].v_samp_factor)
shun-iwasawa 82a8f5
            match++;
shun-iwasawa 82a8f5
          if (match == dinfo->num_components - 1) {
shun-iwasawa 82a8f5
            retval = i;  break;
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* General API functions */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT char *tjGetErrorStr2(tjhandle handle)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjinstance *this = (tjinstance *)handle;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (this && this->isInstanceError) {
shun-iwasawa 82a8f5
    this->isInstanceError = FALSE;
shun-iwasawa 82a8f5
    return this->errStr;
shun-iwasawa 82a8f5
  } else
shun-iwasawa 82a8f5
    return errStr;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT char *tjGetErrorStr(void)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  return errStr;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjGetErrorCode(tjhandle handle)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjinstance *this = (tjinstance *)handle;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (this && this->jerr.warning) return TJERR_WARNING;
shun-iwasawa 82a8f5
  else return TJERR_FATAL;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjDestroy(tjhandle handle)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  GET_INSTANCE(handle);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) return -1;
shun-iwasawa 82a8f5
  if (this->init & COMPRESS) jpeg_destroy_compress(cinfo);
shun-iwasawa 82a8f5
  if (this->init & DECOMPRESS) jpeg_destroy_decompress(dinfo);
shun-iwasawa 82a8f5
  free(this);
shun-iwasawa 82a8f5
  return 0;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* These are exposed mainly because Windows can't malloc() and free() across
shun-iwasawa 82a8f5
   DLL boundaries except when the CRT DLL is used, and we don't use the CRT DLL
shun-iwasawa 82a8f5
   with turbojpeg.dll for compatibility reasons.  However, these functions
shun-iwasawa 82a8f5
   can potentially be used for other purposes by different implementations. */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT void tjFree(unsigned char *buf)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  free(buf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT unsigned char *tjAlloc(int bytes)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  return (unsigned char *)malloc(bytes);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Compressor  */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static tjhandle _tjInitCompress(tjinstance *this)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  static unsigned char buffer[1];
shun-iwasawa 82a8f5
  unsigned char *buf = buffer;
shun-iwasawa 82a8f5
  unsigned long size = 1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* This is also straight out of example.txt */
shun-iwasawa 82a8f5
  this->cinfo.err = jpeg_std_error(&this->jerr.pub);
shun-iwasawa 82a8f5
  this->jerr.pub.error_exit = my_error_exit;
shun-iwasawa 82a8f5
  this->jerr.pub.output_message = my_output_message;
shun-iwasawa 82a8f5
  this->jerr.emit_message = this->jerr.pub.emit_message;
shun-iwasawa 82a8f5
  this->jerr.pub.emit_message = my_emit_message;
shun-iwasawa 82a8f5
  this->jerr.pub.addon_message_table = turbojpeg_message_table;
shun-iwasawa 82a8f5
  this->jerr.pub.first_addon_message = JMSG_FIRSTADDONCODE;
shun-iwasawa 82a8f5
  this->jerr.pub.last_addon_message = JMSG_LASTADDONCODE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    free(this);
shun-iwasawa 82a8f5
    return NULL;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_create_compress(&this->cinfo);
shun-iwasawa 82a8f5
  /* Make an initial call so it will create the destination manager */
shun-iwasawa 82a8f5
  jpeg_mem_dest_tj(&this->cinfo, &buf, &size, 0);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  this->init |= COMPRESS;
shun-iwasawa 82a8f5
  return (tjhandle)this;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT tjhandle tjInitCompress(void)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjinstance *this = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((this = (tjinstance *)malloc(sizeof(tjinstance))) == NULL) {
shun-iwasawa 82a8f5
    snprintf(errStr, JMSG_LENGTH_MAX,
shun-iwasawa 82a8f5
             "tjInitCompress(): Memory allocation failure");
shun-iwasawa 82a8f5
    return NULL;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  MEMZERO(this, sizeof(tjinstance));
shun-iwasawa 82a8f5
  snprintf(this->errStr, JMSG_LENGTH_MAX, "No error");
shun-iwasawa 82a8f5
  return _tjInitCompress(this);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT unsigned long tjBufSize(int width, int height, int jpegSubsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  unsigned long long retval = 0;
shun-iwasawa 82a8f5
  int mcuw, mcuh, chromasf;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (width < 1 || height < 1 || jpegSubsamp < 0 || jpegSubsamp >= NUMSUBOPT)
shun-iwasawa 82a8f5
    THROWG("tjBufSize(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* This allows for rare corner cases in which a JPEG image can actually be
shun-iwasawa 82a8f5
     larger than the uncompressed input (we wouldn't mention it if it hadn't
shun-iwasawa 82a8f5
     happened before.) */
shun-iwasawa 82a8f5
  mcuw = tjMCUWidth[jpegSubsamp];
shun-iwasawa 82a8f5
  mcuh = tjMCUHeight[jpegSubsamp];
shun-iwasawa 82a8f5
  chromasf = jpegSubsamp == TJSAMP_GRAY ? 0 : 4 * 64 / (mcuw * mcuh);
shun-iwasawa 82a8f5
  retval = PAD(width, mcuw) * PAD(height, mcuh) * (2ULL + chromasf) + 2048ULL;
shun-iwasawa 82a8f5
  if (retval > (unsigned long long)((unsigned long)-1))
shun-iwasawa 82a8f5
    THROWG("tjBufSize(): Image is too large");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return (unsigned long)retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT unsigned long TJBUFSIZE(int width, int height)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  unsigned long long retval = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (width < 1 || height < 1)
shun-iwasawa 82a8f5
    THROWG("TJBUFSIZE(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* This allows for rare corner cases in which a JPEG image can actually be
shun-iwasawa 82a8f5
     larger than the uncompressed input (we wouldn't mention it if it hadn't
shun-iwasawa 82a8f5
     happened before.) */
shun-iwasawa 82a8f5
  retval = PAD(width, 16) * PAD(height, 16) * 6ULL + 2048ULL;
shun-iwasawa 82a8f5
  if (retval > (unsigned long long)((unsigned long)-1))
shun-iwasawa 82a8f5
    THROWG("TJBUFSIZE(): Image is too large");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return (unsigned long)retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT unsigned long tjBufSizeYUV2(int width, int pad, int height,
shun-iwasawa 82a8f5
                                      int subsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  unsigned long long retval = 0;
shun-iwasawa 82a8f5
  int nc, i;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (subsamp < 0 || subsamp >= NUMSUBOPT)
shun-iwasawa 82a8f5
    THROWG("tjBufSizeYUV2(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  nc = (subsamp == TJSAMP_GRAY ? 1 : 3);
shun-iwasawa 82a8f5
  for (i = 0; i < nc; i++) {
shun-iwasawa 82a8f5
    int pw = tjPlaneWidth(i, width, subsamp);
shun-iwasawa 82a8f5
    int stride = PAD(pw, pad);
shun-iwasawa 82a8f5
    int ph = tjPlaneHeight(i, height, subsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (pw < 0 || ph < 0) return -1;
shun-iwasawa 82a8f5
    else retval += (unsigned long long)stride * ph;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (retval > (unsigned long long)((unsigned long)-1))
shun-iwasawa 82a8f5
    THROWG("tjBufSizeYUV2(): Image is too large");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return (unsigned long)retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT unsigned long tjBufSizeYUV(int width, int height, int subsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  return tjBufSizeYUV2(width, 4, height, subsamp);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int subsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  return tjBufSizeYUV(width, height, subsamp);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int pw, nc, retval = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (width < 1 || subsamp < 0 || subsamp >= TJ_NUMSAMP)
shun-iwasawa 82a8f5
    THROWG("tjPlaneWidth(): Invalid argument");
shun-iwasawa 82a8f5
  nc = (subsamp == TJSAMP_GRAY ? 1 : 3);
shun-iwasawa 82a8f5
  if (componentID < 0 || componentID >= nc)
shun-iwasawa 82a8f5
    THROWG("tjPlaneWidth(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  pw = PAD(width, tjMCUWidth[subsamp] / 8);
shun-iwasawa 82a8f5
  if (componentID == 0)
shun-iwasawa 82a8f5
    retval = pw;
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    retval = pw * 8 / tjMCUWidth[subsamp];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int ph, nc, retval = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (height < 1 || subsamp < 0 || subsamp >= TJ_NUMSAMP)
shun-iwasawa 82a8f5
    THROWG("tjPlaneHeight(): Invalid argument");
shun-iwasawa 82a8f5
  nc = (subsamp == TJSAMP_GRAY ? 1 : 3);
shun-iwasawa 82a8f5
  if (componentID < 0 || componentID >= nc)
shun-iwasawa 82a8f5
    THROWG("tjPlaneHeight(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ph = PAD(height, tjMCUHeight[subsamp] / 8);
shun-iwasawa 82a8f5
  if (componentID == 0)
shun-iwasawa 82a8f5
    retval = ph;
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    retval = ph * 8 / tjMCUHeight[subsamp];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT unsigned long tjPlaneSizeYUV(int componentID, int width, int stride,
shun-iwasawa 82a8f5
                                       int height, int subsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  unsigned long long retval = 0;
shun-iwasawa 82a8f5
  int pw, ph;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (width < 1 || height < 1 || subsamp < 0 || subsamp >= NUMSUBOPT)
shun-iwasawa 82a8f5
    THROWG("tjPlaneSizeYUV(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  pw = tjPlaneWidth(componentID, width, subsamp);
shun-iwasawa 82a8f5
  ph = tjPlaneHeight(componentID, height, subsamp);
shun-iwasawa 82a8f5
  if (pw < 0 || ph < 0) return -1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (stride == 0) stride = pw;
shun-iwasawa 82a8f5
  else stride = abs(stride);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  retval = (unsigned long long)stride * (ph - 1) + pw;
shun-iwasawa 82a8f5
  if (retval > (unsigned long long)((unsigned long)-1))
shun-iwasawa 82a8f5
    THROWG("tjPlaneSizeYUV(): Image is too large");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return (unsigned long)retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf,
shun-iwasawa 82a8f5
                          int width, int pitch, int height, int pixelFormat,
shun-iwasawa 82a8f5
                          unsigned char **jpegBuf, unsigned long *jpegSize,
shun-iwasawa 82a8f5
                          int jpegSubsamp, int jpegQual, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int i, retval = 0, alloc = 1;
shun-iwasawa 82a8f5
  JSAMPROW *row_pointer = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_CINSTANCE(handle)
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
shun-iwasawa 82a8f5
  if ((this->init & COMPRESS) == 0)
shun-iwasawa 82a8f5
    THROW("tjCompress2(): Instance has not been initialized for compression");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
shun-iwasawa 82a8f5
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
shun-iwasawa 82a8f5
      jpegSize == NULL || jpegSubsamp < 0 || jpegSubsamp >= NUMSUBOPT ||
shun-iwasawa 82a8f5
      jpegQual < 0 || jpegQual > 100)
shun-iwasawa 82a8f5
    THROW("tjCompress2(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((row_pointer = (JSAMPROW *)malloc(sizeof(JSAMPROW) * height)) == NULL)
shun-iwasawa 82a8f5
    THROW("tjCompress2(): Memory allocation failure");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo->image_width = width;
shun-iwasawa 82a8f5
  cinfo->image_height = height;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifndef NO_PUTENV
shun-iwasawa 82a8f5
  if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (flags & TJFLAG_NOREALLOC) {
shun-iwasawa 82a8f5
    alloc = 0;  *jpegSize = tjBufSize(width, height, jpegSubsamp);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
shun-iwasawa 82a8f5
  setCompDefaults(cinfo, pixelFormat, jpegSubsamp, jpegQual, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_start_compress(cinfo, TRUE);
shun-iwasawa 82a8f5
  for (i = 0; i < height; i++) {
shun-iwasawa 82a8f5
    if (flags & TJFLAG_BOTTOMUP)
shun-iwasawa 82a8f5
      row_pointer[i] = (JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      row_pointer[i] = (JSAMPROW)&srcBuf[i * (size_t)pitch];
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  while (cinfo->next_scanline < cinfo->image_height)
shun-iwasawa 82a8f5
    jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
shun-iwasawa 82a8f5
                         cinfo->image_height - cinfo->next_scanline);
shun-iwasawa 82a8f5
  jpeg_finish_compress(cinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (cinfo->global_state > CSTATE_START) jpeg_abort_compress(cinfo);
shun-iwasawa 82a8f5
  free(row_pointer);
shun-iwasawa 82a8f5
  if (this->jerr.warning) retval = -1;
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = FALSE;
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjCompress(tjhandle handle, unsigned char *srcBuf, int width,
shun-iwasawa 82a8f5
                         int pitch, int height, int pixelSize,
shun-iwasawa 82a8f5
                         unsigned char *jpegBuf, unsigned long *jpegSize,
shun-iwasawa 82a8f5
                         int jpegSubsamp, int jpegQual, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int retval = 0;
shun-iwasawa 82a8f5
  unsigned long size;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (flags & TJ_YUV) {
shun-iwasawa 82a8f5
    size = tjBufSizeYUV(width, height, jpegSubsamp);
shun-iwasawa 82a8f5
    retval = tjEncodeYUV2(handle, srcBuf, width, pitch, height,
shun-iwasawa 82a8f5
                          getPixelFormat(pixelSize, flags), jpegBuf,
shun-iwasawa 82a8f5
                          jpegSubsamp, flags);
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    retval = tjCompress2(handle, srcBuf, width, pitch, height,
shun-iwasawa 82a8f5
                         getPixelFormat(pixelSize, flags), &jpegBuf, &size,
shun-iwasawa 82a8f5
                         jpegSubsamp, jpegQual, flags | TJFLAG_NOREALLOC);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  *jpegSize = size;
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf,
shun-iwasawa 82a8f5
                                int width, int pitch, int height,
shun-iwasawa 82a8f5
                                int pixelFormat, unsigned char **dstPlanes,
shun-iwasawa 82a8f5
                                int *strides, int subsamp, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  JSAMPROW *row_pointer = NULL;
shun-iwasawa 82a8f5
  JSAMPLE *_tmpbuf[MAX_COMPONENTS], *_tmpbuf2[MAX_COMPONENTS];
shun-iwasawa 82a8f5
  JSAMPROW *tmpbuf[MAX_COMPONENTS], *tmpbuf2[MAX_COMPONENTS];
shun-iwasawa 82a8f5
  JSAMPROW *outbuf[MAX_COMPONENTS];
shun-iwasawa 82a8f5
  int i, retval = 0, row, pw0, ph0, pw[MAX_COMPONENTS], ph[MAX_COMPONENTS];
shun-iwasawa 82a8f5
  JSAMPLE *ptr;
shun-iwasawa 82a8f5
  jpeg_component_info *compptr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_CINSTANCE(handle);
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < MAX_COMPONENTS; i++) {
shun-iwasawa 82a8f5
    tmpbuf[i] = NULL;  _tmpbuf[i] = NULL;
shun-iwasawa 82a8f5
    tmpbuf2[i] = NULL;  _tmpbuf2[i] = NULL;  outbuf[i] = NULL;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((this->init & COMPRESS) == 0)
shun-iwasawa 82a8f5
    THROW("tjEncodeYUVPlanes(): Instance has not been initialized for compression");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
shun-iwasawa 82a8f5
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || !dstPlanes ||
shun-iwasawa 82a8f5
      !dstPlanes[0] || subsamp < 0 || subsamp >= NUMSUBOPT)
shun-iwasawa 82a8f5
    THROW("tjEncodeYUVPlanes(): Invalid argument");
shun-iwasawa 82a8f5
  if (subsamp != TJSAMP_GRAY && (!dstPlanes[1] || !dstPlanes[2]))
shun-iwasawa 82a8f5
    THROW("tjEncodeYUVPlanes(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pixelFormat == TJPF_CMYK)
shun-iwasawa 82a8f5
    THROW("tjEncodeYUVPlanes(): Cannot generate YUV images from CMYK pixels");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo->image_width = width;
shun-iwasawa 82a8f5
  cinfo->image_height = height;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifndef NO_PUTENV
shun-iwasawa 82a8f5
  if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  setCompDefaults(cinfo, pixelFormat, subsamp, -1, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Execute only the parts of jpeg_start_compress() that we need.  If we
shun-iwasawa 82a8f5
     were to call the whole jpeg_start_compress() function, then it would try
shun-iwasawa 82a8f5
     to write the file headers, which could overflow the output buffer if the
shun-iwasawa 82a8f5
     YUV image were very small. */
shun-iwasawa 82a8f5
  if (cinfo->global_state != CSTATE_START)
shun-iwasawa 82a8f5
    THROW("tjEncodeYUVPlanes(): libjpeg API is in the wrong state");
shun-iwasawa 82a8f5
  (*cinfo->err->reset_error_mgr) ((j_common_ptr)cinfo);
shun-iwasawa 82a8f5
  jinit_c_master_control(cinfo, FALSE);
shun-iwasawa 82a8f5
  jinit_color_converter(cinfo);
shun-iwasawa 82a8f5
  jinit_downsampler(cinfo);
shun-iwasawa 82a8f5
  (*cinfo->cconvert->start_pass) (cinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  pw0 = PAD(width, cinfo->max_h_samp_factor);
shun-iwasawa 82a8f5
  ph0 = PAD(height, cinfo->max_v_samp_factor);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((row_pointer = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph0)) == NULL)
shun-iwasawa 82a8f5
    THROW("tjEncodeYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
  for (i = 0; i < height; i++) {
shun-iwasawa 82a8f5
    if (flags & TJFLAG_BOTTOMUP)
shun-iwasawa 82a8f5
      row_pointer[i] = (JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      row_pointer[i] = (JSAMPROW)&srcBuf[i * (size_t)pitch];
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (height < ph0)
shun-iwasawa 82a8f5
    for (i = height; i < ph0; i++) row_pointer[i] = row_pointer[height - 1];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < cinfo->num_components; i++) {
shun-iwasawa 82a8f5
    compptr = &cinfo->comp_info[i];
shun-iwasawa 82a8f5
    _tmpbuf[i] = (JSAMPLE *)malloc(
shun-iwasawa 82a8f5
      PAD((compptr->width_in_blocks * cinfo->max_h_samp_factor * DCTSIZE) /
shun-iwasawa 82a8f5
          compptr->h_samp_factor, 32) *
shun-iwasawa 82a8f5
      cinfo->max_v_samp_factor + 32);
shun-iwasawa 82a8f5
    if (!_tmpbuf[i])
shun-iwasawa 82a8f5
      THROW("tjEncodeYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
    tmpbuf[i] =
shun-iwasawa 82a8f5
      (JSAMPROW *)malloc(sizeof(JSAMPROW) * cinfo->max_v_samp_factor);
shun-iwasawa 82a8f5
    if (!tmpbuf[i])
shun-iwasawa 82a8f5
      THROW("tjEncodeYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
    for (row = 0; row < cinfo->max_v_samp_factor; row++) {
shun-iwasawa 82a8f5
      unsigned char *_tmpbuf_aligned =
shun-iwasawa 82a8f5
        (unsigned char *)PAD((size_t)_tmpbuf[i], 32);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      tmpbuf[i][row] = &_tmpbuf_aligned[
shun-iwasawa 82a8f5
        PAD((compptr->width_in_blocks * cinfo->max_h_samp_factor * DCTSIZE) /
shun-iwasawa 82a8f5
            compptr->h_samp_factor, 32) * row];
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    _tmpbuf2[i] =
shun-iwasawa 82a8f5
      (JSAMPLE *)malloc(PAD(compptr->width_in_blocks * DCTSIZE, 32) *
shun-iwasawa 82a8f5
                        compptr->v_samp_factor + 32);
shun-iwasawa 82a8f5
    if (!_tmpbuf2[i])
shun-iwasawa 82a8f5
      THROW("tjEncodeYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
    tmpbuf2[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * compptr->v_samp_factor);
shun-iwasawa 82a8f5
    if (!tmpbuf2[i])
shun-iwasawa 82a8f5
      THROW("tjEncodeYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
    for (row = 0; row < compptr->v_samp_factor; row++) {
shun-iwasawa 82a8f5
      unsigned char *_tmpbuf2_aligned =
shun-iwasawa 82a8f5
        (unsigned char *)PAD((size_t)_tmpbuf2[i], 32);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      tmpbuf2[i][row] =
shun-iwasawa 82a8f5
        &_tmpbuf2_aligned[PAD(compptr->width_in_blocks * DCTSIZE, 32) * row];
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    pw[i] = pw0 * compptr->h_samp_factor / cinfo->max_h_samp_factor;
shun-iwasawa 82a8f5
    ph[i] = ph0 * compptr->v_samp_factor / cinfo->max_v_samp_factor;
shun-iwasawa 82a8f5
    outbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph[i]);
shun-iwasawa 82a8f5
    if (!outbuf[i])
shun-iwasawa 82a8f5
      THROW("tjEncodeYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
    ptr = dstPlanes[i];
shun-iwasawa 82a8f5
    for (row = 0; row < ph[i]; row++) {
shun-iwasawa 82a8f5
      outbuf[i][row] = ptr;
shun-iwasawa 82a8f5
      ptr += (strides && strides[i] != 0) ? strides[i] : pw[i];
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (row = 0; row < ph0; row += cinfo->max_v_samp_factor) {
shun-iwasawa 82a8f5
    (*cinfo->cconvert->color_convert) (cinfo, &row_pointer[row], tmpbuf, 0,
shun-iwasawa 82a8f5
                                       cinfo->max_v_samp_factor);
shun-iwasawa 82a8f5
    (cinfo->downsample->downsample) (cinfo, tmpbuf, 0, tmpbuf2, 0);
shun-iwasawa 82a8f5
    for (i = 0, compptr = cinfo->comp_info; i < cinfo->num_components;
shun-iwasawa 82a8f5
         i++, compptr++)
shun-iwasawa 82a8f5
      jcopy_sample_rows(tmpbuf2[i], 0, outbuf[i],
shun-iwasawa 82a8f5
        row * compptr->v_samp_factor / cinfo->max_v_samp_factor,
shun-iwasawa 82a8f5
        compptr->v_samp_factor, pw[i]);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  cinfo->next_scanline += height;
shun-iwasawa 82a8f5
  jpeg_abort_compress(cinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (cinfo->global_state > CSTATE_START) jpeg_abort_compress(cinfo);
shun-iwasawa 82a8f5
  free(row_pointer);
shun-iwasawa 82a8f5
  for (i = 0; i < MAX_COMPONENTS; i++) {
shun-iwasawa 82a8f5
    free(tmpbuf[i]);
shun-iwasawa 82a8f5
    free(_tmpbuf[i]);
shun-iwasawa 82a8f5
    free(tmpbuf2[i]);
shun-iwasawa 82a8f5
    free(_tmpbuf2[i]);
shun-iwasawa 82a8f5
    free(outbuf[i]);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (this->jerr.warning) retval = -1;
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = FALSE;
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjEncodeYUV3(tjhandle handle, const unsigned char *srcBuf,
shun-iwasawa 82a8f5
                           int width, int pitch, int height, int pixelFormat,
shun-iwasawa 82a8f5
                           unsigned char *dstBuf, int pad, int subsamp,
shun-iwasawa 82a8f5
                           int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  unsigned char *dstPlanes[3];
shun-iwasawa 82a8f5
  int pw0, ph0, strides[3], retval = -1;
shun-iwasawa 82a8f5
  tjinstance *this = (tjinstance *)handle;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!this) THROWG("tjEncodeYUV3(): Invalid handle");
shun-iwasawa 82a8f5
  this->isInstanceError = FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (width <= 0 || height <= 0 || dstBuf == NULL || pad < 0 ||
shun-iwasawa 82a8f5
      !IS_POW2(pad) || subsamp < 0 || subsamp >= NUMSUBOPT)
shun-iwasawa 82a8f5
    THROW("tjEncodeYUV3(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  pw0 = tjPlaneWidth(0, width, subsamp);
shun-iwasawa 82a8f5
  ph0 = tjPlaneHeight(0, height, subsamp);
shun-iwasawa 82a8f5
  dstPlanes[0] = dstBuf;
shun-iwasawa 82a8f5
  strides[0] = PAD(pw0, pad);
shun-iwasawa 82a8f5
  if (subsamp == TJSAMP_GRAY) {
shun-iwasawa 82a8f5
    strides[1] = strides[2] = 0;
shun-iwasawa 82a8f5
    dstPlanes[1] = dstPlanes[2] = NULL;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    int pw1 = tjPlaneWidth(1, width, subsamp);
shun-iwasawa 82a8f5
    int ph1 = tjPlaneHeight(1, height, subsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    strides[1] = strides[2] = PAD(pw1, pad);
shun-iwasawa 82a8f5
    dstPlanes[1] = dstPlanes[0] + strides[0] * ph0;
shun-iwasawa 82a8f5
    dstPlanes[2] = dstPlanes[1] + strides[1] * ph1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return tjEncodeYUVPlanes(handle, srcBuf, width, pitch, height, pixelFormat,
shun-iwasawa 82a8f5
                           dstPlanes, strides, subsamp, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, int width,
shun-iwasawa 82a8f5
                           int pitch, int height, int pixelFormat,
shun-iwasawa 82a8f5
                           unsigned char *dstBuf, int subsamp, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  return tjEncodeYUV3(handle, srcBuf, width, pitch, height, pixelFormat,
shun-iwasawa 82a8f5
                      dstBuf, 4, subsamp, flags);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, int width,
shun-iwasawa 82a8f5
                          int pitch, int height, int pixelSize,
shun-iwasawa 82a8f5
                          unsigned char *dstBuf, int subsamp, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  return tjEncodeYUV2(handle, srcBuf, width, pitch, height,
shun-iwasawa 82a8f5
                      getPixelFormat(pixelSize, flags), dstBuf, subsamp,
shun-iwasawa 82a8f5
                      flags);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle,
shun-iwasawa 82a8f5
                                      const unsigned char **srcPlanes,
shun-iwasawa 82a8f5
                                      int width, const int *strides,
shun-iwasawa 82a8f5
                                      int height, int subsamp,
shun-iwasawa 82a8f5
                                      unsigned char **jpegBuf,
shun-iwasawa 82a8f5
                                      unsigned long *jpegSize, int jpegQual,
shun-iwasawa 82a8f5
                                      int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int i, row, retval = 0, alloc = 1;
shun-iwasawa 82a8f5
  int pw[MAX_COMPONENTS], ph[MAX_COMPONENTS], iw[MAX_COMPONENTS],
shun-iwasawa 82a8f5
    tmpbufsize = 0, usetmpbuf = 0, th[MAX_COMPONENTS];
shun-iwasawa 82a8f5
  JSAMPLE *_tmpbuf = NULL, *ptr;
shun-iwasawa 82a8f5
  JSAMPROW *inbuf[MAX_COMPONENTS], *tmpbuf[MAX_COMPONENTS];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_CINSTANCE(handle)
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < MAX_COMPONENTS; i++) {
shun-iwasawa 82a8f5
    tmpbuf[i] = NULL;  inbuf[i] = NULL;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((this->init & COMPRESS) == 0)
shun-iwasawa 82a8f5
    THROW("tjCompressFromYUVPlanes(): Instance has not been initialized for compression");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!srcPlanes || !srcPlanes[0] || width <= 0 || height <= 0 ||
shun-iwasawa 82a8f5
      subsamp < 0 || subsamp >= NUMSUBOPT || jpegBuf == NULL ||
shun-iwasawa 82a8f5
      jpegSize == NULL || jpegQual < 0 || jpegQual > 100)
shun-iwasawa 82a8f5
    THROW("tjCompressFromYUVPlanes(): Invalid argument");
shun-iwasawa 82a8f5
  if (subsamp != TJSAMP_GRAY && (!srcPlanes[1] || !srcPlanes[2]))
shun-iwasawa 82a8f5
    THROW("tjCompressFromYUVPlanes(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo->image_width = width;
shun-iwasawa 82a8f5
  cinfo->image_height = height;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifndef NO_PUTENV
shun-iwasawa 82a8f5
  if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (flags & TJFLAG_NOREALLOC) {
shun-iwasawa 82a8f5
    alloc = 0;  *jpegSize = tjBufSize(width, height, subsamp);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
shun-iwasawa 82a8f5
  setCompDefaults(cinfo, TJPF_RGB, subsamp, jpegQual, flags);
shun-iwasawa 82a8f5
  cinfo->raw_data_in = TRUE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_start_compress(cinfo, TRUE);
shun-iwasawa 82a8f5
  for (i = 0; i < cinfo->num_components; i++) {
shun-iwasawa 82a8f5
    jpeg_component_info *compptr = &cinfo->comp_info[i];
shun-iwasawa 82a8f5
    int ih;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    iw[i] = compptr->width_in_blocks * DCTSIZE;
shun-iwasawa 82a8f5
    ih = compptr->height_in_blocks * DCTSIZE;
shun-iwasawa 82a8f5
    pw[i] = PAD(cinfo->image_width, cinfo->max_h_samp_factor) *
shun-iwasawa 82a8f5
            compptr->h_samp_factor / cinfo->max_h_samp_factor;
shun-iwasawa 82a8f5
    ph[i] = PAD(cinfo->image_height, cinfo->max_v_samp_factor) *
shun-iwasawa 82a8f5
            compptr->v_samp_factor / cinfo->max_v_samp_factor;
shun-iwasawa 82a8f5
    if (iw[i] != pw[i] || ih != ph[i]) usetmpbuf = 1;
shun-iwasawa 82a8f5
    th[i] = compptr->v_samp_factor * DCTSIZE;
shun-iwasawa 82a8f5
    tmpbufsize += iw[i] * th[i];
shun-iwasawa 82a8f5
    if ((inbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph[i])) == NULL)
shun-iwasawa 82a8f5
      THROW("tjCompressFromYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
    ptr = (JSAMPLE *)srcPlanes[i];
shun-iwasawa 82a8f5
    for (row = 0; row < ph[i]; row++) {
shun-iwasawa 82a8f5
      inbuf[i][row] = ptr;
shun-iwasawa 82a8f5
      ptr += (strides && strides[i] != 0) ? strides[i] : pw[i];
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (usetmpbuf) {
shun-iwasawa 82a8f5
    if ((_tmpbuf = (JSAMPLE *)malloc(sizeof(JSAMPLE) * tmpbufsize)) == NULL)
shun-iwasawa 82a8f5
      THROW("tjCompressFromYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
    ptr = _tmpbuf;
shun-iwasawa 82a8f5
    for (i = 0; i < cinfo->num_components; i++) {
shun-iwasawa 82a8f5
      if ((tmpbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * th[i])) == NULL)
shun-iwasawa 82a8f5
        THROW("tjCompressFromYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
      for (row = 0; row < th[i]; row++) {
shun-iwasawa 82a8f5
        tmpbuf[i][row] = ptr;
shun-iwasawa 82a8f5
        ptr += iw[i];
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (row = 0; row < (int)cinfo->image_height;
shun-iwasawa 82a8f5
       row += cinfo->max_v_samp_factor * DCTSIZE) {
shun-iwasawa 82a8f5
    JSAMPARRAY yuvptr[MAX_COMPONENTS];
shun-iwasawa 82a8f5
    int crow[MAX_COMPONENTS];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    for (i = 0; i < cinfo->num_components; i++) {
shun-iwasawa 82a8f5
      jpeg_component_info *compptr = &cinfo->comp_info[i];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      crow[i] = row * compptr->v_samp_factor / cinfo->max_v_samp_factor;
shun-iwasawa 82a8f5
      if (usetmpbuf) {
shun-iwasawa 82a8f5
        int j, k;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        for (j = 0; j < MIN(th[i], ph[i] - crow[i]); j++) {
shun-iwasawa 82a8f5
          memcpy(tmpbuf[i][j], inbuf[i][crow[i] + j], pw[i]);
shun-iwasawa 82a8f5
          /* Duplicate last sample in row to fill out MCU */
shun-iwasawa 82a8f5
          for (k = pw[i]; k < iw[i]; k++)
shun-iwasawa 82a8f5
            tmpbuf[i][j][k] = tmpbuf[i][j][pw[i] - 1];
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
        /* Duplicate last row to fill out MCU */
shun-iwasawa 82a8f5
        for (j = ph[i] - crow[i]; j < th[i]; j++)
shun-iwasawa 82a8f5
          memcpy(tmpbuf[i][j], tmpbuf[i][ph[i] - crow[i] - 1], iw[i]);
shun-iwasawa 82a8f5
        yuvptr[i] = tmpbuf[i];
shun-iwasawa 82a8f5
      } else
shun-iwasawa 82a8f5
        yuvptr[i] = &inbuf[i][crow[i]];
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    jpeg_write_raw_data(cinfo, yuvptr, cinfo->max_v_samp_factor * DCTSIZE);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  jpeg_finish_compress(cinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (cinfo->global_state > CSTATE_START) jpeg_abort_compress(cinfo);
shun-iwasawa 82a8f5
  for (i = 0; i < MAX_COMPONENTS; i++) {
shun-iwasawa 82a8f5
    free(tmpbuf[i]);
shun-iwasawa 82a8f5
    free(inbuf[i]);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  free(_tmpbuf);
shun-iwasawa 82a8f5
  if (this->jerr.warning) retval = -1;
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = FALSE;
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjCompressFromYUV(tjhandle handle, const unsigned char *srcBuf,
shun-iwasawa 82a8f5
                                int width, int pad, int height, int subsamp,
shun-iwasawa 82a8f5
                                unsigned char **jpegBuf,
shun-iwasawa 82a8f5
                                unsigned long *jpegSize, int jpegQual,
shun-iwasawa 82a8f5
                                int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  const unsigned char *srcPlanes[3];
shun-iwasawa 82a8f5
  int pw0, ph0, strides[3], retval = -1;
shun-iwasawa 82a8f5
  tjinstance *this = (tjinstance *)handle;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!this) THROWG("tjCompressFromYUV(): Invalid handle");
shun-iwasawa 82a8f5
  this->isInstanceError = FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (srcBuf == NULL || width <= 0 || pad < 1 || height <= 0 || subsamp < 0 ||
shun-iwasawa 82a8f5
      subsamp >= NUMSUBOPT)
shun-iwasawa 82a8f5
    THROW("tjCompressFromYUV(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  pw0 = tjPlaneWidth(0, width, subsamp);
shun-iwasawa 82a8f5
  ph0 = tjPlaneHeight(0, height, subsamp);
shun-iwasawa 82a8f5
  srcPlanes[0] = srcBuf;
shun-iwasawa 82a8f5
  strides[0] = PAD(pw0, pad);
shun-iwasawa 82a8f5
  if (subsamp == TJSAMP_GRAY) {
shun-iwasawa 82a8f5
    strides[1] = strides[2] = 0;
shun-iwasawa 82a8f5
    srcPlanes[1] = srcPlanes[2] = NULL;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    int pw1 = tjPlaneWidth(1, width, subsamp);
shun-iwasawa 82a8f5
    int ph1 = tjPlaneHeight(1, height, subsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    strides[1] = strides[2] = PAD(pw1, pad);
shun-iwasawa 82a8f5
    srcPlanes[1] = srcPlanes[0] + strides[0] * ph0;
shun-iwasawa 82a8f5
    srcPlanes[2] = srcPlanes[1] + strides[1] * ph1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return tjCompressFromYUVPlanes(handle, srcPlanes, width, strides, height,
shun-iwasawa 82a8f5
                                 subsamp, jpegBuf, jpegSize, jpegQual, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Decompressor */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static tjhandle _tjInitDecompress(tjinstance *this)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  static unsigned char buffer[1];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* This is also straight out of example.txt */
shun-iwasawa 82a8f5
  this->dinfo.err = jpeg_std_error(&this->jerr.pub);
shun-iwasawa 82a8f5
  this->jerr.pub.error_exit = my_error_exit;
shun-iwasawa 82a8f5
  this->jerr.pub.output_message = my_output_message;
shun-iwasawa 82a8f5
  this->jerr.emit_message = this->jerr.pub.emit_message;
shun-iwasawa 82a8f5
  this->jerr.pub.emit_message = my_emit_message;
shun-iwasawa 82a8f5
  this->jerr.pub.addon_message_table = turbojpeg_message_table;
shun-iwasawa 82a8f5
  this->jerr.pub.first_addon_message = JMSG_FIRSTADDONCODE;
shun-iwasawa 82a8f5
  this->jerr.pub.last_addon_message = JMSG_LASTADDONCODE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    free(this);
shun-iwasawa 82a8f5
    return NULL;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_create_decompress(&this->dinfo);
shun-iwasawa 82a8f5
  /* Make an initial call so it will create the source manager */
shun-iwasawa 82a8f5
  jpeg_mem_src_tj(&this->dinfo, buffer, 1);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  this->init |= DECOMPRESS;
shun-iwasawa 82a8f5
  return (tjhandle)this;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT tjhandle tjInitDecompress(void)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjinstance *this;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((this = (tjinstance *)malloc(sizeof(tjinstance))) == NULL) {
shun-iwasawa 82a8f5
    snprintf(errStr, JMSG_LENGTH_MAX,
shun-iwasawa 82a8f5
             "tjInitDecompress(): Memory allocation failure");
shun-iwasawa 82a8f5
    return NULL;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  MEMZERO(this, sizeof(tjinstance));
shun-iwasawa 82a8f5
  snprintf(this->errStr, JMSG_LENGTH_MAX, "No error");
shun-iwasawa 82a8f5
  return _tjInitDecompress(this);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjDecompressHeader3(tjhandle handle,
shun-iwasawa 82a8f5
                                  const unsigned char *jpegBuf,
shun-iwasawa 82a8f5
                                  unsigned long jpegSize, int *width,
shun-iwasawa 82a8f5
                                  int *height, int *jpegSubsamp,
shun-iwasawa 82a8f5
                                  int *jpegColorspace)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int retval = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_DINSTANCE(handle);
shun-iwasawa 82a8f5
  if ((this->init & DECOMPRESS) == 0)
shun-iwasawa 82a8f5
    THROW("tjDecompressHeader3(): Instance has not been initialized for decompression");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (jpegBuf == NULL || jpegSize <= 0 || width == NULL || height == NULL ||
shun-iwasawa 82a8f5
      jpegSubsamp == NULL || jpegColorspace == NULL)
shun-iwasawa 82a8f5
    THROW("tjDecompressHeader3(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    return -1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
shun-iwasawa 82a8f5
  jpeg_read_header(dinfo, TRUE);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  *width = dinfo->image_width;
shun-iwasawa 82a8f5
  *height = dinfo->image_height;
shun-iwasawa 82a8f5
  *jpegSubsamp = getSubsamp(dinfo);
shun-iwasawa 82a8f5
  switch (dinfo->jpeg_color_space) {
shun-iwasawa 82a8f5
  case JCS_GRAYSCALE:  *jpegColorspace = TJCS_GRAY;  break;
shun-iwasawa 82a8f5
  case JCS_RGB:        *jpegColorspace = TJCS_RGB;  break;
shun-iwasawa 82a8f5
  case JCS_YCbCr:      *jpegColorspace = TJCS_YCbCr;  break;
shun-iwasawa 82a8f5
  case JCS_CMYK:       *jpegColorspace = TJCS_CMYK;  break;
shun-iwasawa 82a8f5
  case JCS_YCCK:       *jpegColorspace = TJCS_YCCK;  break;
shun-iwasawa 82a8f5
  default:             *jpegColorspace = -1;  break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_abort_decompress(dinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (*jpegSubsamp < 0)
shun-iwasawa 82a8f5
    THROW("tjDecompressHeader3(): Could not determine subsampling type for JPEG image");
shun-iwasawa 82a8f5
  if (*jpegColorspace < 0)
shun-iwasawa 82a8f5
    THROW("tjDecompressHeader3(): Could not determine colorspace of JPEG image");
shun-iwasawa 82a8f5
  if (*width < 1 || *height < 1)
shun-iwasawa 82a8f5
    THROW("tjDecompressHeader3(): Invalid data returned in header");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (this->jerr.warning) retval = -1;
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjDecompressHeader2(tjhandle handle, unsigned char *jpegBuf,
shun-iwasawa 82a8f5
                                  unsigned long jpegSize, int *width,
shun-iwasawa 82a8f5
                                  int *height, int *jpegSubsamp)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int jpegColorspace;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return tjDecompressHeader3(handle, jpegBuf, jpegSize, width, height,
shun-iwasawa 82a8f5
                             jpegSubsamp, &jpegColorspace);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjDecompressHeader(tjhandle handle, unsigned char *jpegBuf,
shun-iwasawa 82a8f5
                                 unsigned long jpegSize, int *width,
shun-iwasawa 82a8f5
                                 int *height)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int jpegSubsamp;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return tjDecompressHeader2(handle, jpegBuf, jpegSize, width, height,
shun-iwasawa 82a8f5
                             &jpegSubsamp);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT tjscalingfactor *tjGetScalingFactors(int *numscalingfactors)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (numscalingfactors == NULL) {
shun-iwasawa 82a8f5
    snprintf(errStr, JMSG_LENGTH_MAX,
shun-iwasawa 82a8f5
             "tjGetScalingFactors(): Invalid argument");
shun-iwasawa 82a8f5
    return NULL;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  *numscalingfactors = NUMSF;
shun-iwasawa 82a8f5
  return (tjscalingfactor *)sf;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,
shun-iwasawa 82a8f5
                            unsigned long jpegSize, unsigned char *dstBuf,
shun-iwasawa 82a8f5
                            int width, int pitch, int height, int pixelFormat,
shun-iwasawa 82a8f5
                            int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  JSAMPROW *row_pointer = NULL;
shun-iwasawa 82a8f5
  int i, retval = 0, jpegwidth, jpegheight, scaledw, scaledh;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_DINSTANCE(handle);
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
shun-iwasawa 82a8f5
  if ((this->init & DECOMPRESS) == 0)
shun-iwasawa 82a8f5
    THROW("tjDecompress2(): Instance has not been initialized for decompression");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || width < 0 ||
shun-iwasawa 82a8f5
      pitch < 0 || height < 0 || pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
shun-iwasawa 82a8f5
    THROW("tjDecompress2(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifndef NO_PUTENV
shun-iwasawa 82a8f5
  if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
shun-iwasawa 82a8f5
  jpeg_read_header(dinfo, TRUE);
shun-iwasawa 82a8f5
  this->dinfo.out_color_space = pf2cs[pixelFormat];
shun-iwasawa 82a8f5
  if (flags & TJFLAG_FASTDCT) this->dinfo.dct_method = JDCT_FASTEST;
shun-iwasawa 82a8f5
  if (flags & TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling = FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpegwidth = dinfo->image_width;  jpegheight = dinfo->image_height;
shun-iwasawa 82a8f5
  if (width == 0) width = jpegwidth;
shun-iwasawa 82a8f5
  if (height == 0) height = jpegheight;
shun-iwasawa 82a8f5
  for (i = 0; i < NUMSF; i++) {
shun-iwasawa 82a8f5
    scaledw = TJSCALED(jpegwidth, sf[i]);
shun-iwasawa 82a8f5
    scaledh = TJSCALED(jpegheight, sf[i]);
shun-iwasawa 82a8f5
    if (scaledw <= width && scaledh <= height)
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (i >= NUMSF)
shun-iwasawa 82a8f5
    THROW("tjDecompress2(): Could not scale down to desired image dimensions");
shun-iwasawa 82a8f5
  width = scaledw;  height = scaledh;
shun-iwasawa 82a8f5
  dinfo->scale_num = sf[i].num;
shun-iwasawa 82a8f5
  dinfo->scale_denom = sf[i].denom;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_start_decompress(dinfo);
shun-iwasawa 82a8f5
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((row_pointer =
shun-iwasawa 82a8f5
       (JSAMPROW *)malloc(sizeof(JSAMPROW) * dinfo->output_height)) == NULL)
shun-iwasawa 82a8f5
    THROW("tjDecompress2(): Memory allocation failure");
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  for (i = 0; i < (int)dinfo->output_height; i++) {
shun-iwasawa 82a8f5
    if (flags & TJFLAG_BOTTOMUP)
shun-iwasawa 82a8f5
      row_pointer[i] = &dstBuf[(dinfo->output_height - i - 1) * (size_t)pitch];
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  while (dinfo->output_scanline < dinfo->output_height)
shun-iwasawa 82a8f5
    jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
shun-iwasawa 82a8f5
                        dinfo->output_height - dinfo->output_scanline);
shun-iwasawa 82a8f5
  jpeg_finish_decompress(dinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
shun-iwasawa 82a8f5
  free(row_pointer);
shun-iwasawa 82a8f5
  if (this->jerr.warning) retval = -1;
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = FALSE;
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjDecompress(tjhandle handle, unsigned char *jpegBuf,
shun-iwasawa 82a8f5
                           unsigned long jpegSize, unsigned char *dstBuf,
shun-iwasawa 82a8f5
                           int width, int pitch, int height, int pixelSize,
shun-iwasawa 82a8f5
                           int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (flags & TJ_YUV)
shun-iwasawa 82a8f5
    return tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    return tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, width, pitch,
shun-iwasawa 82a8f5
                         height, getPixelFormat(pixelSize, flags), flags);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int setDecodeDefaults(struct jpeg_decompress_struct *dinfo,
shun-iwasawa 82a8f5
                             int pixelFormat, int subsamp, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int i;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  dinfo->scale_num = dinfo->scale_denom = 1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (subsamp == TJSAMP_GRAY) {
shun-iwasawa 82a8f5
    dinfo->num_components = dinfo->comps_in_scan = 1;
shun-iwasawa 82a8f5
    dinfo->jpeg_color_space = JCS_GRAYSCALE;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    dinfo->num_components = dinfo->comps_in_scan = 3;
shun-iwasawa 82a8f5
    dinfo->jpeg_color_space = JCS_YCbCr;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  dinfo->comp_info = (jpeg_component_info *)
shun-iwasawa 82a8f5
    (*dinfo->mem->alloc_small) ((j_common_ptr)dinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                dinfo->num_components *
shun-iwasawa 82a8f5
                                sizeof(jpeg_component_info));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < dinfo->num_components; i++) {
shun-iwasawa 82a8f5
    jpeg_component_info *compptr = &dinfo->comp_info[i];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    compptr->h_samp_factor = (i == 0) ? tjMCUWidth[subsamp] / 8 : 1;
shun-iwasawa 82a8f5
    compptr->v_samp_factor = (i == 0) ? tjMCUHeight[subsamp] / 8 : 1;
shun-iwasawa 82a8f5
    compptr->component_index = i;
shun-iwasawa 82a8f5
    compptr->component_id = i + 1;
shun-iwasawa 82a8f5
    compptr->quant_tbl_no = compptr->dc_tbl_no =
shun-iwasawa 82a8f5
      compptr->ac_tbl_no = (i == 0) ? 0 : 1;
shun-iwasawa 82a8f5
    dinfo->cur_comp_info[i] = compptr;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  dinfo->data_precision = 8;
shun-iwasawa 82a8f5
  for (i = 0; i < 2; i++) {
shun-iwasawa 82a8f5
    if (dinfo->quant_tbl_ptrs[i] == NULL)
shun-iwasawa 82a8f5
      dinfo->quant_tbl_ptrs[i] = jpeg_alloc_quant_table((j_common_ptr)dinfo);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return 0;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int my_read_markers(j_decompress_ptr dinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  return JPEG_REACHED_SOS;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void my_reset_marker_reader(j_decompress_ptr dinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle,
shun-iwasawa 82a8f5
                                const unsigned char **srcPlanes,
shun-iwasawa 82a8f5
                                const int *strides, int subsamp,
shun-iwasawa 82a8f5
                                unsigned char *dstBuf, int width, int pitch,
shun-iwasawa 82a8f5
                                int height, int pixelFormat, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  JSAMPROW *row_pointer = NULL;
shun-iwasawa 82a8f5
  JSAMPLE *_tmpbuf[MAX_COMPONENTS];
shun-iwasawa 82a8f5
  JSAMPROW *tmpbuf[MAX_COMPONENTS], *inbuf[MAX_COMPONENTS];
shun-iwasawa 82a8f5
  int i, retval = 0, row, pw0, ph0, pw[MAX_COMPONENTS], ph[MAX_COMPONENTS];
shun-iwasawa 82a8f5
  JSAMPLE *ptr;
shun-iwasawa 82a8f5
  jpeg_component_info *compptr;
shun-iwasawa 82a8f5
  int (*old_read_markers) (j_decompress_ptr);
shun-iwasawa 82a8f5
  void (*old_reset_marker_reader) (j_decompress_ptr);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_DINSTANCE(handle);
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < MAX_COMPONENTS; i++) {
shun-iwasawa 82a8f5
    tmpbuf[i] = NULL;  _tmpbuf[i] = NULL;  inbuf[i] = NULL;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((this->init & DECOMPRESS) == 0)
shun-iwasawa 82a8f5
    THROW("tjDecodeYUVPlanes(): Instance has not been initialized for decompression");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!srcPlanes || !srcPlanes[0] || subsamp < 0 || subsamp >= NUMSUBOPT ||
shun-iwasawa 82a8f5
      dstBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
shun-iwasawa 82a8f5
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
shun-iwasawa 82a8f5
    THROW("tjDecodeYUVPlanes(): Invalid argument");
shun-iwasawa 82a8f5
  if (subsamp != TJSAMP_GRAY && (!srcPlanes[1] || !srcPlanes[2]))
shun-iwasawa 82a8f5
    THROW("tjDecodeYUVPlanes(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pixelFormat == TJPF_CMYK)
shun-iwasawa 82a8f5
    THROW("tjDecodeYUVPlanes(): Cannot decode YUV images into CMYK pixels.");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
shun-iwasawa 82a8f5
  dinfo->image_width = width;
shun-iwasawa 82a8f5
  dinfo->image_height = height;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifndef NO_PUTENV
shun-iwasawa 82a8f5
  if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  dinfo->progressive_mode = dinfo->inputctl->has_multiple_scans = FALSE;
shun-iwasawa 82a8f5
  dinfo->Ss = dinfo->Ah = dinfo->Al = 0;
shun-iwasawa 82a8f5
  dinfo->Se = DCTSIZE2 - 1;
shun-iwasawa 82a8f5
  if (setDecodeDefaults(dinfo, pixelFormat, subsamp, flags) == -1) {
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  old_read_markers = dinfo->marker->read_markers;
shun-iwasawa 82a8f5
  dinfo->marker->read_markers = my_read_markers;
shun-iwasawa 82a8f5
  old_reset_marker_reader = dinfo->marker->reset_marker_reader;
shun-iwasawa 82a8f5
  dinfo->marker->reset_marker_reader = my_reset_marker_reader;
shun-iwasawa 82a8f5
  jpeg_read_header(dinfo, TRUE);
shun-iwasawa 82a8f5
  dinfo->marker->read_markers = old_read_markers;
shun-iwasawa 82a8f5
  dinfo->marker->reset_marker_reader = old_reset_marker_reader;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  this->dinfo.out_color_space = pf2cs[pixelFormat];
shun-iwasawa 82a8f5
  if (flags & TJFLAG_FASTDCT) this->dinfo.dct_method = JDCT_FASTEST;
shun-iwasawa 82a8f5
  dinfo->do_fancy_upsampling = FALSE;
shun-iwasawa 82a8f5
  dinfo->Se = DCTSIZE2 - 1;
shun-iwasawa 82a8f5
  jinit_master_decompress(dinfo);
shun-iwasawa 82a8f5
  (*dinfo->upsample->start_pass) (dinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  pw0 = PAD(width, dinfo->max_h_samp_factor);
shun-iwasawa 82a8f5
  ph0 = PAD(height, dinfo->max_v_samp_factor);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((row_pointer = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph0)) == NULL)
shun-iwasawa 82a8f5
    THROW("tjDecodeYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
  for (i = 0; i < height; i++) {
shun-iwasawa 82a8f5
    if (flags & TJFLAG_BOTTOMUP)
shun-iwasawa 82a8f5
      row_pointer[i] = &dstBuf[(height - i - 1) * (size_t)pitch];
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (height < ph0)
shun-iwasawa 82a8f5
    for (i = height; i < ph0; i++) row_pointer[i] = row_pointer[height - 1];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < dinfo->num_components; i++) {
shun-iwasawa 82a8f5
    compptr = &dinfo->comp_info[i];
shun-iwasawa 82a8f5
    _tmpbuf[i] =
shun-iwasawa 82a8f5
      (JSAMPLE *)malloc(PAD(compptr->width_in_blocks * DCTSIZE, 32) *
shun-iwasawa 82a8f5
                        compptr->v_samp_factor + 32);
shun-iwasawa 82a8f5
    if (!_tmpbuf[i])
shun-iwasawa 82a8f5
      THROW("tjDecodeYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
    tmpbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * compptr->v_samp_factor);
shun-iwasawa 82a8f5
    if (!tmpbuf[i])
shun-iwasawa 82a8f5
      THROW("tjDecodeYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
    for (row = 0; row < compptr->v_samp_factor; row++) {
shun-iwasawa 82a8f5
      unsigned char *_tmpbuf_aligned =
shun-iwasawa 82a8f5
        (unsigned char *)PAD((size_t)_tmpbuf[i], 32);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      tmpbuf[i][row] =
shun-iwasawa 82a8f5
        &_tmpbuf_aligned[PAD(compptr->width_in_blocks * DCTSIZE, 32) * row];
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    pw[i] = pw0 * compptr->h_samp_factor / dinfo->max_h_samp_factor;
shun-iwasawa 82a8f5
    ph[i] = ph0 * compptr->v_samp_factor / dinfo->max_v_samp_factor;
shun-iwasawa 82a8f5
    inbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph[i]);
shun-iwasawa 82a8f5
    if (!inbuf[i])
shun-iwasawa 82a8f5
      THROW("tjDecodeYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
    ptr = (JSAMPLE *)srcPlanes[i];
shun-iwasawa 82a8f5
    for (row = 0; row < ph[i]; row++) {
shun-iwasawa 82a8f5
      inbuf[i][row] = ptr;
shun-iwasawa 82a8f5
      ptr += (strides && strides[i] != 0) ? strides[i] : pw[i];
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (row = 0; row < ph0; row += dinfo->max_v_samp_factor) {
shun-iwasawa 82a8f5
    JDIMENSION inrow = 0, outrow = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    for (i = 0, compptr = dinfo->comp_info; i < dinfo->num_components;
shun-iwasawa 82a8f5
         i++, compptr++)
shun-iwasawa 82a8f5
      jcopy_sample_rows(inbuf[i],
shun-iwasawa 82a8f5
        row * compptr->v_samp_factor / dinfo->max_v_samp_factor, tmpbuf[i], 0,
shun-iwasawa 82a8f5
        compptr->v_samp_factor, pw[i]);
shun-iwasawa 82a8f5
    (dinfo->upsample->upsample) (dinfo, tmpbuf, &inrow,
shun-iwasawa 82a8f5
                                 dinfo->max_v_samp_factor, &row_pointer[row],
shun-iwasawa 82a8f5
                                 &outrow, dinfo->max_v_samp_factor);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  jpeg_abort_decompress(dinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
shun-iwasawa 82a8f5
  free(row_pointer);
shun-iwasawa 82a8f5
  for (i = 0; i < MAX_COMPONENTS; i++) {
shun-iwasawa 82a8f5
    free(tmpbuf[i]);
shun-iwasawa 82a8f5
    free(_tmpbuf[i]);
shun-iwasawa 82a8f5
    free(inbuf[i]);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (this->jerr.warning) retval = -1;
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = FALSE;
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf,
shun-iwasawa 82a8f5
                          int pad, int subsamp, unsigned char *dstBuf,
shun-iwasawa 82a8f5
                          int width, int pitch, int height, int pixelFormat,
shun-iwasawa 82a8f5
                          int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  const unsigned char *srcPlanes[3];
shun-iwasawa 82a8f5
  int pw0, ph0, strides[3], retval = -1;
shun-iwasawa 82a8f5
  tjinstance *this = (tjinstance *)handle;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!this) THROWG("tjDecodeYUV(): Invalid handle");
shun-iwasawa 82a8f5
  this->isInstanceError = FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (srcBuf == NULL || pad < 0 || !IS_POW2(pad) || subsamp < 0 ||
shun-iwasawa 82a8f5
      subsamp >= NUMSUBOPT || width <= 0 || height <= 0)
shun-iwasawa 82a8f5
    THROW("tjDecodeYUV(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  pw0 = tjPlaneWidth(0, width, subsamp);
shun-iwasawa 82a8f5
  ph0 = tjPlaneHeight(0, height, subsamp);
shun-iwasawa 82a8f5
  srcPlanes[0] = srcBuf;
shun-iwasawa 82a8f5
  strides[0] = PAD(pw0, pad);
shun-iwasawa 82a8f5
  if (subsamp == TJSAMP_GRAY) {
shun-iwasawa 82a8f5
    strides[1] = strides[2] = 0;
shun-iwasawa 82a8f5
    srcPlanes[1] = srcPlanes[2] = NULL;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    int pw1 = tjPlaneWidth(1, width, subsamp);
shun-iwasawa 82a8f5
    int ph1 = tjPlaneHeight(1, height, subsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    strides[1] = strides[2] = PAD(pw1, pad);
shun-iwasawa 82a8f5
    srcPlanes[1] = srcPlanes[0] + strides[0] * ph0;
shun-iwasawa 82a8f5
    srcPlanes[2] = srcPlanes[1] + strides[1] * ph1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return tjDecodeYUVPlanes(handle, srcPlanes, strides, subsamp, dstBuf, width,
shun-iwasawa 82a8f5
                           pitch, height, pixelFormat, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle,
shun-iwasawa 82a8f5
                                      const unsigned char *jpegBuf,
shun-iwasawa 82a8f5
                                      unsigned long jpegSize,
shun-iwasawa 82a8f5
                                      unsigned char **dstPlanes, int width,
shun-iwasawa 82a8f5
                                      int *strides, int height, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int i, sfi, row, retval = 0;
shun-iwasawa 82a8f5
  int jpegwidth, jpegheight, jpegSubsamp, scaledw, scaledh;
shun-iwasawa 82a8f5
  int pw[MAX_COMPONENTS], ph[MAX_COMPONENTS], iw[MAX_COMPONENTS],
shun-iwasawa 82a8f5
    tmpbufsize = 0, usetmpbuf = 0, th[MAX_COMPONENTS];
shun-iwasawa 82a8f5
  JSAMPLE *_tmpbuf = NULL, *ptr;
shun-iwasawa 82a8f5
  JSAMPROW *outbuf[MAX_COMPONENTS], *tmpbuf[MAX_COMPONENTS];
shun-iwasawa 82a8f5
  int dctsize;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_DINSTANCE(handle);
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < MAX_COMPONENTS; i++) {
shun-iwasawa 82a8f5
    tmpbuf[i] = NULL;  outbuf[i] = NULL;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((this->init & DECOMPRESS) == 0)
shun-iwasawa 82a8f5
    THROW("tjDecompressToYUVPlanes(): Instance has not been initialized for decompression");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (jpegBuf == NULL || jpegSize <= 0 || !dstPlanes || !dstPlanes[0] ||
shun-iwasawa 82a8f5
      width < 0 || height < 0)
shun-iwasawa 82a8f5
    THROW("tjDecompressToYUVPlanes(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifndef NO_PUTENV
shun-iwasawa 82a8f5
  if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!this->headerRead) {
shun-iwasawa 82a8f5
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
shun-iwasawa 82a8f5
    jpeg_read_header(dinfo, TRUE);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  this->headerRead = 0;
shun-iwasawa 82a8f5
  jpegSubsamp = getSubsamp(dinfo);
shun-iwasawa 82a8f5
  if (jpegSubsamp < 0)
shun-iwasawa 82a8f5
    THROW("tjDecompressToYUVPlanes(): Could not determine subsampling type for JPEG image");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (jpegSubsamp != TJSAMP_GRAY && (!dstPlanes[1] || !dstPlanes[2]))
shun-iwasawa 82a8f5
    THROW("tjDecompressToYUVPlanes(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpegwidth = dinfo->image_width;  jpegheight = dinfo->image_height;
shun-iwasawa 82a8f5
  if (width == 0) width = jpegwidth;
shun-iwasawa 82a8f5
  if (height == 0) height = jpegheight;
shun-iwasawa 82a8f5
  for (i = 0; i < NUMSF; i++) {
shun-iwasawa 82a8f5
    scaledw = TJSCALED(jpegwidth, sf[i]);
shun-iwasawa 82a8f5
    scaledh = TJSCALED(jpegheight, sf[i]);
shun-iwasawa 82a8f5
    if (scaledw <= width && scaledh <= height)
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (i >= NUMSF)
shun-iwasawa 82a8f5
    THROW("tjDecompressToYUVPlanes(): Could not scale down to desired image dimensions");
shun-iwasawa 82a8f5
  if (dinfo->num_components > 3)
shun-iwasawa 82a8f5
    THROW("tjDecompressToYUVPlanes(): JPEG image must have 3 or fewer components");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  width = scaledw;  height = scaledh;
shun-iwasawa 82a8f5
  dinfo->scale_num = sf[i].num;
shun-iwasawa 82a8f5
  dinfo->scale_denom = sf[i].denom;
shun-iwasawa 82a8f5
  sfi = i;
shun-iwasawa 82a8f5
  jpeg_calc_output_dimensions(dinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  dctsize = DCTSIZE * sf[sfi].num / sf[sfi].denom;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < dinfo->num_components; i++) {
shun-iwasawa 82a8f5
    jpeg_component_info *compptr = &dinfo->comp_info[i];
shun-iwasawa 82a8f5
    int ih;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    iw[i] = compptr->width_in_blocks * dctsize;
shun-iwasawa 82a8f5
    ih = compptr->height_in_blocks * dctsize;
shun-iwasawa 82a8f5
    pw[i] = tjPlaneWidth(i, dinfo->output_width, jpegSubsamp);
shun-iwasawa 82a8f5
    ph[i] = tjPlaneHeight(i, dinfo->output_height, jpegSubsamp);
shun-iwasawa 82a8f5
    if (iw[i] != pw[i] || ih != ph[i]) usetmpbuf = 1;
shun-iwasawa 82a8f5
    th[i] = compptr->v_samp_factor * dctsize;
shun-iwasawa 82a8f5
    tmpbufsize += iw[i] * th[i];
shun-iwasawa 82a8f5
    if ((outbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph[i])) == NULL)
shun-iwasawa 82a8f5
      THROW("tjDecompressToYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
    ptr = dstPlanes[i];
shun-iwasawa 82a8f5
    for (row = 0; row < ph[i]; row++) {
shun-iwasawa 82a8f5
      outbuf[i][row] = ptr;
shun-iwasawa 82a8f5
      ptr += (strides && strides[i] != 0) ? strides[i] : pw[i];
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (usetmpbuf) {
shun-iwasawa 82a8f5
    if ((_tmpbuf = (JSAMPLE *)malloc(sizeof(JSAMPLE) * tmpbufsize)) == NULL)
shun-iwasawa 82a8f5
      THROW("tjDecompressToYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
    ptr = _tmpbuf;
shun-iwasawa 82a8f5
    for (i = 0; i < dinfo->num_components; i++) {
shun-iwasawa 82a8f5
      if ((tmpbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * th[i])) == NULL)
shun-iwasawa 82a8f5
        THROW("tjDecompressToYUVPlanes(): Memory allocation failure");
shun-iwasawa 82a8f5
      for (row = 0; row < th[i]; row++) {
shun-iwasawa 82a8f5
        tmpbuf[i][row] = ptr;
shun-iwasawa 82a8f5
        ptr += iw[i];
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (flags & TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling = FALSE;
shun-iwasawa 82a8f5
  if (flags & TJFLAG_FASTDCT) dinfo->dct_method = JDCT_FASTEST;
shun-iwasawa 82a8f5
  dinfo->raw_data_out = TRUE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_start_decompress(dinfo);
shun-iwasawa 82a8f5
  for (row = 0; row < (int)dinfo->output_height;
shun-iwasawa 82a8f5
       row += dinfo->max_v_samp_factor * dinfo->_min_DCT_scaled_size) {
shun-iwasawa 82a8f5
    JSAMPARRAY yuvptr[MAX_COMPONENTS];
shun-iwasawa 82a8f5
    int crow[MAX_COMPONENTS];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    for (i = 0; i < dinfo->num_components; i++) {
shun-iwasawa 82a8f5
      jpeg_component_info *compptr = &dinfo->comp_info[i];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      if (jpegSubsamp == TJ_420) {
shun-iwasawa 82a8f5
        /* When 4:2:0 subsampling is used with IDCT scaling, libjpeg will try
shun-iwasawa 82a8f5
           to be clever and use the IDCT to perform upsampling on the U and V
shun-iwasawa 82a8f5
           planes.  For instance, if the output image is to be scaled by 1/2
shun-iwasawa 82a8f5
           relative to the JPEG image, then the scaling factor and upsampling
shun-iwasawa 82a8f5
           effectively cancel each other, so a normal 8x8 IDCT can be used.
shun-iwasawa 82a8f5
           However, this is not desirable when using the decompress-to-YUV
shun-iwasawa 82a8f5
           functionality in TurboJPEG, since we want to output the U and V
shun-iwasawa 82a8f5
           planes in their subsampled form.  Thus, we have to override some
shun-iwasawa 82a8f5
           internal libjpeg parameters to force it to use the "scaled" IDCT
shun-iwasawa 82a8f5
           functions on the U and V planes. */
shun-iwasawa 82a8f5
        compptr->_DCT_scaled_size = dctsize;
shun-iwasawa 82a8f5
        compptr->MCU_sample_width = tjMCUWidth[jpegSubsamp] *
shun-iwasawa 82a8f5
          sf[sfi].num / sf[sfi].denom *
shun-iwasawa 82a8f5
          compptr->v_samp_factor / dinfo->max_v_samp_factor;
shun-iwasawa 82a8f5
        dinfo->idct->inverse_DCT[i] = dinfo->idct->inverse_DCT[0];
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      crow[i] = row * compptr->v_samp_factor / dinfo->max_v_samp_factor;
shun-iwasawa 82a8f5
      if (usetmpbuf) yuvptr[i] = tmpbuf[i];
shun-iwasawa 82a8f5
      else yuvptr[i] = &outbuf[i][crow[i]];
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    jpeg_read_raw_data(dinfo, yuvptr,
shun-iwasawa 82a8f5
                       dinfo->max_v_samp_factor * dinfo->_min_DCT_scaled_size);
shun-iwasawa 82a8f5
    if (usetmpbuf) {
shun-iwasawa 82a8f5
      int j;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      for (i = 0; i < dinfo->num_components; i++) {
shun-iwasawa 82a8f5
        for (j = 0; j < MIN(th[i], ph[i] - crow[i]); j++) {
shun-iwasawa 82a8f5
          memcpy(outbuf[i][crow[i] + j], tmpbuf[i][j], pw[i]);
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  jpeg_finish_decompress(dinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
shun-iwasawa 82a8f5
  for (i = 0; i < MAX_COMPONENTS; i++) {
shun-iwasawa 82a8f5
    free(tmpbuf[i]);
shun-iwasawa 82a8f5
    free(outbuf[i]);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  free(_tmpbuf);
shun-iwasawa 82a8f5
  if (this->jerr.warning) retval = -1;
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = FALSE;
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjDecompressToYUV2(tjhandle handle, const unsigned char *jpegBuf,
shun-iwasawa 82a8f5
                                 unsigned long jpegSize, unsigned char *dstBuf,
shun-iwasawa 82a8f5
                                 int width, int pad, int height, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  unsigned char *dstPlanes[3];
shun-iwasawa 82a8f5
  int pw0, ph0, strides[3], retval = -1, jpegSubsamp = -1;
shun-iwasawa 82a8f5
  int i, jpegwidth, jpegheight, scaledw, scaledh;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_DINSTANCE(handle);
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || width < 0 ||
shun-iwasawa 82a8f5
      pad < 1 || !IS_POW2(pad) || height < 0)
shun-iwasawa 82a8f5
    THROW("tjDecompressToYUV2(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    return -1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
shun-iwasawa 82a8f5
  jpeg_read_header(dinfo, TRUE);
shun-iwasawa 82a8f5
  jpegSubsamp = getSubsamp(dinfo);
shun-iwasawa 82a8f5
  if (jpegSubsamp < 0)
shun-iwasawa 82a8f5
    THROW("tjDecompressToYUV2(): Could not determine subsampling type for JPEG image");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpegwidth = dinfo->image_width;  jpegheight = dinfo->image_height;
shun-iwasawa 82a8f5
  if (width == 0) width = jpegwidth;
shun-iwasawa 82a8f5
  if (height == 0) height = jpegheight;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < NUMSF; i++) {
shun-iwasawa 82a8f5
    scaledw = TJSCALED(jpegwidth, sf[i]);
shun-iwasawa 82a8f5
    scaledh = TJSCALED(jpegheight, sf[i]);
shun-iwasawa 82a8f5
    if (scaledw <= width && scaledh <= height)
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (i >= NUMSF)
shun-iwasawa 82a8f5
    THROW("tjDecompressToYUV2(): Could not scale down to desired image dimensions");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  pw0 = tjPlaneWidth(0, width, jpegSubsamp);
shun-iwasawa 82a8f5
  ph0 = tjPlaneHeight(0, height, jpegSubsamp);
shun-iwasawa 82a8f5
  dstPlanes[0] = dstBuf;
shun-iwasawa 82a8f5
  strides[0] = PAD(pw0, pad);
shun-iwasawa 82a8f5
  if (jpegSubsamp == TJSAMP_GRAY) {
shun-iwasawa 82a8f5
    strides[1] = strides[2] = 0;
shun-iwasawa 82a8f5
    dstPlanes[1] = dstPlanes[2] = NULL;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    int pw1 = tjPlaneWidth(1, width, jpegSubsamp);
shun-iwasawa 82a8f5
    int ph1 = tjPlaneHeight(1, height, jpegSubsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    strides[1] = strides[2] = PAD(pw1, pad);
shun-iwasawa 82a8f5
    dstPlanes[1] = dstPlanes[0] + strides[0] * ph0;
shun-iwasawa 82a8f5
    dstPlanes[2] = dstPlanes[1] + strides[1] * ph1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  this->headerRead = 1;
shun-iwasawa 82a8f5
  return tjDecompressToYUVPlanes(handle, jpegBuf, jpegSize, dstPlanes, width,
shun-iwasawa 82a8f5
                                 strides, height, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = FALSE;
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjDecompressToYUV(tjhandle handle, unsigned char *jpegBuf,
shun-iwasawa 82a8f5
                                unsigned long jpegSize, unsigned char *dstBuf,
shun-iwasawa 82a8f5
                                int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  return tjDecompressToYUV2(handle, jpegBuf, jpegSize, dstBuf, 0, 4, 0, flags);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Transformer */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT tjhandle tjInitTransform(void)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjinstance *this = NULL;
shun-iwasawa 82a8f5
  tjhandle handle = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((this = (tjinstance *)malloc(sizeof(tjinstance))) == NULL) {
shun-iwasawa 82a8f5
    snprintf(errStr, JMSG_LENGTH_MAX,
shun-iwasawa 82a8f5
             "tjInitTransform(): Memory allocation failure");
shun-iwasawa 82a8f5
    return NULL;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  MEMZERO(this, sizeof(tjinstance));
shun-iwasawa 82a8f5
  snprintf(this->errStr, JMSG_LENGTH_MAX, "No error");
shun-iwasawa 82a8f5
  handle = _tjInitCompress(this);
shun-iwasawa 82a8f5
  if (!handle) return NULL;
shun-iwasawa 82a8f5
  handle = _tjInitDecompress(this);
shun-iwasawa 82a8f5
  return handle;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
shun-iwasawa 82a8f5
                          unsigned long jpegSize, int n,
shun-iwasawa 82a8f5
                          unsigned char **dstBufs, unsigned long *dstSizes,
shun-iwasawa 82a8f5
                          tjtransform *t, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  jpeg_transform_info *xinfo = NULL;
shun-iwasawa 82a8f5
  jvirt_barray_ptr *srccoefs, *dstcoefs;
shun-iwasawa 82a8f5
  int retval = 0, i, jpegSubsamp, saveMarkers = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  GET_INSTANCE(handle);
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
shun-iwasawa 82a8f5
  if ((this->init & COMPRESS) == 0 || (this->init & DECOMPRESS) == 0)
shun-iwasawa 82a8f5
    THROW("tjTransform(): Instance has not been initialized for transformation");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (jpegBuf == NULL || jpegSize <= 0 || n < 1 || dstBufs == NULL ||
shun-iwasawa 82a8f5
      dstSizes == NULL || t == NULL || flags < 0)
shun-iwasawa 82a8f5
    THROW("tjTransform(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifndef NO_PUTENV
shun-iwasawa 82a8f5
  if (flags & TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
shun-iwasawa 82a8f5
  else if (flags & TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((xinfo =
shun-iwasawa 82a8f5
       (jpeg_transform_info *)malloc(sizeof(jpeg_transform_info) * n)) == NULL)
shun-iwasawa 82a8f5
    THROW("tjTransform(): Memory allocation failure");
shun-iwasawa 82a8f5
  MEMZERO(xinfo, sizeof(jpeg_transform_info) * n);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < n; i++) {
shun-iwasawa 82a8f5
    xinfo[i].transform = xformtypes[t[i].op];
shun-iwasawa 82a8f5
    xinfo[i].perfect = (t[i].options & TJXOPT_PERFECT) ? 1 : 0;
shun-iwasawa 82a8f5
    xinfo[i].trim = (t[i].options & TJXOPT_TRIM) ? 1 : 0;
shun-iwasawa 82a8f5
    xinfo[i].force_grayscale = (t[i].options & TJXOPT_GRAY) ? 1 : 0;
shun-iwasawa 82a8f5
    xinfo[i].crop = (t[i].options & TJXOPT_CROP) ? 1 : 0;
shun-iwasawa 82a8f5
    if (n != 1 && t[i].op == TJXOP_HFLIP) xinfo[i].slow_hflip = 1;
shun-iwasawa 82a8f5
    else xinfo[i].slow_hflip = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (xinfo[i].crop) {
shun-iwasawa 82a8f5
      xinfo[i].crop_xoffset = t[i].r.x;  xinfo[i].crop_xoffset_set = JCROP_POS;
shun-iwasawa 82a8f5
      xinfo[i].crop_yoffset = t[i].r.y;  xinfo[i].crop_yoffset_set = JCROP_POS;
shun-iwasawa 82a8f5
      if (t[i].r.w != 0) {
shun-iwasawa 82a8f5
        xinfo[i].crop_width = t[i].r.w;  xinfo[i].crop_width_set = JCROP_POS;
shun-iwasawa 82a8f5
      } else
shun-iwasawa 82a8f5
        xinfo[i].crop_width = JCROP_UNSET;
shun-iwasawa 82a8f5
      if (t[i].r.h != 0) {
shun-iwasawa 82a8f5
        xinfo[i].crop_height = t[i].r.h;  xinfo[i].crop_height_set = JCROP_POS;
shun-iwasawa 82a8f5
      } else
shun-iwasawa 82a8f5
        xinfo[i].crop_height = JCROP_UNSET;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if (!(t[i].options & TJXOPT_COPYNONE)) saveMarkers = 1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jcopy_markers_setup(dinfo, saveMarkers ? JCOPYOPT_ALL : JCOPYOPT_NONE);
shun-iwasawa 82a8f5
  jpeg_read_header(dinfo, TRUE);
shun-iwasawa 82a8f5
  jpegSubsamp = getSubsamp(dinfo);
shun-iwasawa 82a8f5
  if (jpegSubsamp < 0)
shun-iwasawa 82a8f5
    THROW("tjTransform(): Could not determine subsampling type for JPEG image");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < n; i++) {
shun-iwasawa 82a8f5
    if (!jtransform_request_workspace(dinfo, &xinfo[i]))
shun-iwasawa 82a8f5
      THROW("tjTransform(): Transform is not perfect");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (xinfo[i].crop) {
shun-iwasawa 82a8f5
      if ((t[i].r.x % xinfo[i].iMCU_sample_width) != 0 ||
shun-iwasawa 82a8f5
          (t[i].r.y % xinfo[i].iMCU_sample_height) != 0) {
shun-iwasawa 82a8f5
        snprintf(this->errStr, JMSG_LENGTH_MAX,
shun-iwasawa 82a8f5
                 "To crop this JPEG image, x must be a multiple of %d\n"
shun-iwasawa 82a8f5
                 "and y must be a multiple of %d.\n",
shun-iwasawa 82a8f5
                 xinfo[i].iMCU_sample_width, xinfo[i].iMCU_sample_height);
shun-iwasawa 82a8f5
        this->isInstanceError = TRUE;
shun-iwasawa 82a8f5
        retval = -1;  goto bailout;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  srccoefs = jpeg_read_coefficients(dinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < n; i++) {
shun-iwasawa 82a8f5
    int w, h, alloc = 1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (!xinfo[i].crop) {
shun-iwasawa 82a8f5
      w = dinfo->image_width;  h = dinfo->image_height;
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      w = xinfo[i].crop_width;  h = xinfo[i].crop_height;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if (flags & TJFLAG_NOREALLOC) {
shun-iwasawa 82a8f5
      alloc = 0;  dstSizes[i] = tjBufSize(w, h, jpegSubsamp);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if (!(t[i].options & TJXOPT_NOOUTPUT))
shun-iwasawa 82a8f5
      jpeg_mem_dest_tj(cinfo, &dstBufs[i], &dstSizes[i], alloc);
shun-iwasawa 82a8f5
    jpeg_copy_critical_parameters(dinfo, cinfo);
shun-iwasawa 82a8f5
    dstcoefs = jtransform_adjust_parameters(dinfo, cinfo, srccoefs, &xinfo[i]);
shun-iwasawa 82a8f5
    if (flags & TJFLAG_PROGRESSIVE || t[i].options & TJXOPT_PROGRESSIVE)
shun-iwasawa 82a8f5
      jpeg_simple_progression(cinfo);
shun-iwasawa 82a8f5
    if (!(t[i].options & TJXOPT_NOOUTPUT)) {
shun-iwasawa 82a8f5
      jpeg_write_coefficients(cinfo, dstcoefs);
shun-iwasawa 82a8f5
      jcopy_markers_execute(dinfo, cinfo, t[i].options & TJXOPT_COPYNONE ?
shun-iwasawa 82a8f5
                                          JCOPYOPT_NONE : JCOPYOPT_ALL);
shun-iwasawa 82a8f5
    } else
shun-iwasawa 82a8f5
      jinit_c_master_control(cinfo, TRUE);
shun-iwasawa 82a8f5
    jtransform_execute_transformation(dinfo, cinfo, srccoefs, &xinfo[i]);
shun-iwasawa 82a8f5
    if (t[i].customFilter) {
shun-iwasawa 82a8f5
      int ci, y;
shun-iwasawa 82a8f5
      JDIMENSION by;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      for (ci = 0; ci < cinfo->num_components; ci++) {
shun-iwasawa 82a8f5
        jpeg_component_info *compptr = &cinfo->comp_info[ci];
shun-iwasawa 82a8f5
        tjregion arrayRegion = {
shun-iwasawa 82a8f5
          0, 0, compptr->width_in_blocks * DCTSIZE, DCTSIZE
shun-iwasawa 82a8f5
        };
shun-iwasawa 82a8f5
        tjregion planeRegion = {
shun-iwasawa 82a8f5
          0, 0, compptr->width_in_blocks * DCTSIZE,
shun-iwasawa 82a8f5
          compptr->height_in_blocks * DCTSIZE
shun-iwasawa 82a8f5
        };
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        for (by = 0; by < compptr->height_in_blocks;
shun-iwasawa 82a8f5
             by += compptr->v_samp_factor) {
shun-iwasawa 82a8f5
          JBLOCKARRAY barray = (dinfo->mem->access_virt_barray)
shun-iwasawa 82a8f5
            ((j_common_ptr)dinfo, dstcoefs[ci], by, compptr->v_samp_factor,
shun-iwasawa 82a8f5
             TRUE);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
          for (y = 0; y < compptr->v_samp_factor; y++) {
shun-iwasawa 82a8f5
            if (t[i].customFilter(barray[y][0], arrayRegion, planeRegion, ci,
shun-iwasawa 82a8f5
                                  i, &t[i]) == -1)
shun-iwasawa 82a8f5
              THROW("tjTransform(): Error in custom filter");
shun-iwasawa 82a8f5
            arrayRegion.y += DCTSIZE;
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if (!(t[i].options & TJXOPT_NOOUTPUT)) jpeg_finish_compress(cinfo);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jpeg_finish_decompress(dinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (cinfo->global_state > CSTATE_START) jpeg_abort_compress(cinfo);
shun-iwasawa 82a8f5
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
shun-iwasawa 82a8f5
  free(xinfo);
shun-iwasawa 82a8f5
  if (this->jerr.warning) retval = -1;
shun-iwasawa 82a8f5
  this->jerr.stopOnWarning = FALSE;
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
shun-iwasawa 82a8f5
                                     int align, int *height, int *pixelFormat,
shun-iwasawa 82a8f5
                                     int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int retval = 0, tempc;
shun-iwasawa 82a8f5
  size_t pitch;
shun-iwasawa 82a8f5
  tjhandle handle = NULL;
shun-iwasawa 82a8f5
  tjinstance *this;
shun-iwasawa 82a8f5
  j_compress_ptr cinfo = NULL;
shun-iwasawa 82a8f5
  cjpeg_source_ptr src;
shun-iwasawa 82a8f5
  unsigned char *dstBuf = NULL;
shun-iwasawa 82a8f5
  FILE *file = NULL;
shun-iwasawa 82a8f5
  boolean invert;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!filename || !width || align < 1 || !height || !pixelFormat ||
shun-iwasawa 82a8f5
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
shun-iwasawa 82a8f5
    THROWG("tjLoadImage(): Invalid argument");
shun-iwasawa 82a8f5
  if ((align & (align - 1)) != 0)
shun-iwasawa 82a8f5
    THROWG("tjLoadImage(): Alignment must be a power of 2");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((handle = tjInitCompress()) == NULL) return NULL;
shun-iwasawa 82a8f5
  this = (tjinstance *)handle;
shun-iwasawa 82a8f5
  cinfo = &this->cinfo;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((file = fopen(filename, "rb")) == NULL)
shun-iwasawa 82a8f5
    THROW_UNIX("tjLoadImage(): Cannot open input file");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
shun-iwasawa 82a8f5
    THROW_UNIX("tjLoadImage(): Could not read input file")
shun-iwasawa 82a8f5
  else if (tempc == EOF)
shun-iwasawa 82a8f5
    THROWG("tjLoadImage(): Input file contains no data");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
shun-iwasawa 82a8f5
  else cinfo->in_color_space = pf2cs[*pixelFormat];
shun-iwasawa 82a8f5
  if (tempc == 'B') {
shun-iwasawa 82a8f5
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
shun-iwasawa 82a8f5
      THROWG("tjLoadImage(): Could not initialize bitmap loader");
shun-iwasawa 82a8f5
    invert = (flags & TJFLAG_BOTTOMUP) == 0;
shun-iwasawa 82a8f5
  } else if (tempc == 'P') {
shun-iwasawa 82a8f5
    if ((src = jinit_read_ppm(cinfo)) == NULL)
shun-iwasawa 82a8f5
      THROWG("tjLoadImage(): Could not initialize bitmap loader");
shun-iwasawa 82a8f5
    invert = (flags & TJFLAG_BOTTOMUP) != 0;
shun-iwasawa 82a8f5
  } else
shun-iwasawa 82a8f5
    THROWG("tjLoadImage(): Unsupported file type");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  src->input_file = file;
shun-iwasawa 82a8f5
  (*src->start_input) (cinfo, src);
shun-iwasawa 82a8f5
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  *width = cinfo->image_width;  *height = cinfo->image_height;
shun-iwasawa 82a8f5
  *pixelFormat = cs2pf[cinfo->in_color_space];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
shun-iwasawa 82a8f5
  if ((unsigned long long)pitch * (unsigned long long)(*height) >
shun-iwasawa 82a8f5
      (unsigned long long)((size_t)-1) ||
shun-iwasawa 82a8f5
      (dstBuf = (unsigned char *)malloc(pitch * (*height))) == NULL)
shun-iwasawa 82a8f5
    THROWG("tjLoadImage(): Memory allocation failure");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  while (cinfo->next_scanline < cinfo->image_height) {
shun-iwasawa 82a8f5
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    for (i = 0; i < nlines; i++) {
shun-iwasawa 82a8f5
      unsigned char *dstptr;
shun-iwasawa 82a8f5
      int row;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      row = cinfo->next_scanline + i;
shun-iwasawa 82a8f5
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
shun-iwasawa 82a8f5
      else dstptr = &dstBuf[row * pitch];
shun-iwasawa 82a8f5
      memcpy(dstptr, src->buffer[i], (*width) * tjPixelSize[*pixelFormat]);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    cinfo->next_scanline += nlines;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  (*src->finish_input) (cinfo, src);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (handle) tjDestroy(handle);
shun-iwasawa 82a8f5
  if (file) fclose(file);
shun-iwasawa 82a8f5
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
shun-iwasawa 82a8f5
  return dstBuf;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer,
shun-iwasawa 82a8f5
                          int width, int pitch, int height, int pixelFormat,
shun-iwasawa 82a8f5
                          int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int retval = 0;
shun-iwasawa 82a8f5
  tjhandle handle = NULL;
shun-iwasawa 82a8f5
  tjinstance *this;
shun-iwasawa 82a8f5
  j_decompress_ptr dinfo = NULL;
shun-iwasawa 82a8f5
  djpeg_dest_ptr dst;
shun-iwasawa 82a8f5
  FILE *file = NULL;
shun-iwasawa 82a8f5
  char *ptr = NULL;
shun-iwasawa 82a8f5
  boolean invert;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 ||
shun-iwasawa 82a8f5
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
shun-iwasawa 82a8f5
    THROWG("tjSaveImage(): Invalid argument");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((handle = tjInitDecompress()) == NULL)
shun-iwasawa 82a8f5
    return -1;
shun-iwasawa 82a8f5
  this = (tjinstance *)handle;
shun-iwasawa 82a8f5
  dinfo = &this->dinfo;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((file = fopen(filename, "wb")) == NULL)
shun-iwasawa 82a8f5
    THROW_UNIX("tjSaveImage(): Cannot open output file");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (setjmp(this->jerr.setjmp_buffer)) {
shun-iwasawa 82a8f5
    /* If we get here, the JPEG code has signaled an error. */
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  this->dinfo.out_color_space = pf2cs[pixelFormat];
shun-iwasawa 82a8f5
  dinfo->image_width = width;  dinfo->image_height = height;
shun-iwasawa 82a8f5
  dinfo->global_state = DSTATE_READY;
shun-iwasawa 82a8f5
  dinfo->scale_num = dinfo->scale_denom = 1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  ptr = strrchr(filename, '.');
shun-iwasawa 82a8f5
  if (ptr && !strcasecmp(ptr, ".bmp")) {
shun-iwasawa 82a8f5
    if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL)
shun-iwasawa 82a8f5
      THROWG("tjSaveImage(): Could not initialize bitmap writer");
shun-iwasawa 82a8f5
    invert = (flags & TJFLAG_BOTTOMUP) == 0;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    if ((dst = jinit_write_ppm(dinfo)) == NULL)
shun-iwasawa 82a8f5
      THROWG("tjSaveImage(): Could not initialize PPM writer");
shun-iwasawa 82a8f5
    invert = (flags & TJFLAG_BOTTOMUP) != 0;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  dst->output_file = file;
shun-iwasawa 82a8f5
  (*dst->start_output) (dinfo, dst);
shun-iwasawa 82a8f5
  (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  while (dinfo->output_scanline < dinfo->output_height) {
shun-iwasawa 82a8f5
    unsigned char *rowptr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (invert)
shun-iwasawa 82a8f5
      rowptr = &buffer[(height - dinfo->output_scanline - 1) * pitch];
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      rowptr = &buffer[dinfo->output_scanline * pitch];
shun-iwasawa 82a8f5
    memcpy(dst->buffer[0], rowptr, width * tjPixelSize[pixelFormat]);
shun-iwasawa 82a8f5
    (*dst->put_pixel_rows) (dinfo, dst, 1);
shun-iwasawa 82a8f5
    dinfo->output_scanline++;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  (*dst->finish_output) (dinfo, dst);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (handle) tjDestroy(handle);
shun-iwasawa 82a8f5
  if (file) fclose(file);
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}