shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Copyright (C)2009-2019 D. R. Commander.  All Rights Reserved.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Redistribution and use in source and binary forms, with or without
shun-iwasawa 82a8f5
 * modification, are permitted provided that the following conditions are met:
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * - Redistributions of source code must retain the above copyright notice,
shun-iwasawa 82a8f5
 *   this list of conditions and the following disclaimer.
shun-iwasawa 82a8f5
 * - Redistributions in binary form must reproduce the above copyright notice,
shun-iwasawa 82a8f5
 *   this list of conditions and the following disclaimer in the documentation
shun-iwasawa 82a8f5
 *   and/or other materials provided with the distribution.
shun-iwasawa 82a8f5
 * - Neither the name of the libjpeg-turbo Project nor the names of its
shun-iwasawa 82a8f5
 *   contributors may be used to endorse or promote products derived from this
shun-iwasawa 82a8f5
 *   software without specific prior written permission.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
shun-iwasawa 82a8f5
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
shun-iwasawa 82a8f5
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
shun-iwasawa 82a8f5
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
shun-iwasawa 82a8f5
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
shun-iwasawa 82a8f5
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
shun-iwasawa 82a8f5
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
shun-iwasawa 82a8f5
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
shun-iwasawa 82a8f5
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
shun-iwasawa 82a8f5
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
shun-iwasawa 82a8f5
 * POSSIBILITY OF SUCH DAMAGE.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include <stdio.h></stdio.h>
shun-iwasawa 82a8f5
#include <stdlib.h></stdlib.h>
shun-iwasawa 82a8f5
#include <string.h></string.h>
shun-iwasawa 82a8f5
#include <ctype.h></ctype.h>
shun-iwasawa 82a8f5
#include <math.h></math.h>
shun-iwasawa 82a8f5
#include <errno.h></errno.h>
shun-iwasawa 82a8f5
#include <limits.h></limits.h>
shun-iwasawa 82a8f5
#include <cdjpeg.h></cdjpeg.h>
shun-iwasawa 82a8f5
#include "./tjutil.h"
shun-iwasawa 82a8f5
#include "./turbojpeg.h"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define THROW(op, err) { \
shun-iwasawa 82a8f5
  printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); \
shun-iwasawa 82a8f5
  retval = -1;  goto bailout; \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
#define THROW_UNIX(m)  THROW(m, strerror(errno))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
char tjErrorStr[JMSG_LENGTH_MAX] = "\0", tjErrorMsg[JMSG_LENGTH_MAX] = "\0";
shun-iwasawa 82a8f5
int tjErrorLine = -1, tjErrorCode = -1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define THROW_TJG(m) { \
shun-iwasawa 82a8f5
  printf("ERROR in line %d while %s:\n%s\n", __LINE__, m, \
shun-iwasawa 82a8f5
         tjGetErrorStr2(NULL)); \
shun-iwasawa 82a8f5
  retval = -1;  goto bailout; \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define THROW_TJ(m) { \
shun-iwasawa 82a8f5
  int _tjErrorCode = tjGetErrorCode(handle); \
shun-iwasawa 82a8f5
  char *_tjErrorStr = tjGetErrorStr2(handle); \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  if (!(flags & TJFLAG_STOPONWARNING) && _tjErrorCode == TJERR_WARNING) { \
shun-iwasawa 82a8f5
    if (strncmp(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX) || \
shun-iwasawa 82a8f5
        strncmp(tjErrorMsg, m, JMSG_LENGTH_MAX) || \
shun-iwasawa 82a8f5
        tjErrorCode != _tjErrorCode || tjErrorLine != __LINE__) { \
shun-iwasawa 82a8f5
      strncpy(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX - 1); \
shun-iwasawa 82a8f5
      strncpy(tjErrorMsg, m, JMSG_LENGTH_MAX - 1); \
shun-iwasawa 82a8f5
      tjErrorCode = _tjErrorCode; \
shun-iwasawa 82a8f5
      tjErrorLine = __LINE__; \
shun-iwasawa 82a8f5
      printf("WARNING in line %d while %s:\n%s\n", __LINE__, m, _tjErrorStr); \
shun-iwasawa 82a8f5
    } \
shun-iwasawa 82a8f5
  } else { \
shun-iwasawa 82a8f5
    printf("%s in line %d while %s:\n%s\n", \
shun-iwasawa 82a8f5
           _tjErrorCode == TJERR_WARNING ? "WARNING" : "ERROR", __LINE__, m, \
shun-iwasawa 82a8f5
           _tjErrorStr); \
shun-iwasawa 82a8f5
    retval = -1;  goto bailout; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
int flags = TJFLAG_NOREALLOC, compOnly = 0, decompOnly = 0, doYUV = 0,
shun-iwasawa 82a8f5
  quiet = 0, doTile = 0, pf = TJPF_BGR, yuvPad = 1, doWrite = 1;
shun-iwasawa 82a8f5
char *ext = "ppm";
shun-iwasawa 82a8f5
const char *pixFormatStr[TJ_NUMPF] = {
shun-iwasawa 82a8f5
  "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY", "", "", "", "", "CMYK"
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
const char *subNameLong[TJ_NUMSAMP] = {
shun-iwasawa 82a8f5
  "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
const char *csName[TJ_NUMCS] = {
shun-iwasawa 82a8f5
  "RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
const char *subName[TJ_NUMSAMP] = {
shun-iwasawa 82a8f5
  "444", "422", "420", "GRAY", "440", "411"
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
tjscalingfactor *scalingFactors = NULL, sf = { 1, 1 };
shun-iwasawa 82a8f5
int nsf = 0, xformOp = TJXOP_NONE, xformOpt = 0;
shun-iwasawa 82a8f5
int (*customFilter) (short *, tjregion, tjregion, int, int, tjtransform *);
shun-iwasawa 82a8f5
double benchTime = 5.0, warmup = 1.0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static char *formatName(int subsamp, int cs, char *buf)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (cs == TJCS_YCbCr)
shun-iwasawa 82a8f5
    return (char *)subNameLong[subsamp];
shun-iwasawa 82a8f5
  else if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
shun-iwasawa 82a8f5
    snprintf(buf, 80, "%s %s", csName[cs], subNameLong[subsamp]);
shun-iwasawa 82a8f5
    return buf;
shun-iwasawa 82a8f5
  } else
shun-iwasawa 82a8f5
    return (char *)csName[cs];
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static char *sigfig(double val, int figs, char *buf, int len)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  char format[80];
shun-iwasawa 82a8f5
  int digitsAfterDecimal = figs - (int)ceil(log10(fabs(val)));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (digitsAfterDecimal < 1)
shun-iwasawa 82a8f5
    snprintf(format, 80, "%%.0f");
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    snprintf(format, 80, "%%.%df", digitsAfterDecimal);
shun-iwasawa 82a8f5
  snprintf(buf, len, format, val);
shun-iwasawa 82a8f5
  return buf;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Custom DCT filter which produces a negative of the image */
shun-iwasawa 82a8f5
static int dummyDCTFilter(short *coeffs, tjregion arrayRegion,
shun-iwasawa 82a8f5
                          tjregion planeRegion, int componentIndex,
shun-iwasawa 82a8f5
                          int transformIndex, tjtransform *transform)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int i;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
shun-iwasawa 82a8f5
    coeffs[i] = -coeffs[i];
shun-iwasawa 82a8f5
  return 0;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Decompression test */
shun-iwasawa 82a8f5
static int decomp(unsigned char *srcBuf, unsigned char **jpegBuf,
shun-iwasawa 82a8f5
                  unsigned long *jpegSize, unsigned char *dstBuf, int w, int h,
shun-iwasawa 82a8f5
                  int subsamp, int jpegQual, char *fileName, int tilew,
shun-iwasawa 82a8f5
                  int tileh)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  char tempStr[1024], sizeStr[24] = "\0", qualStr[13] = "\0", *ptr;
shun-iwasawa 82a8f5
  FILE *file = NULL;
shun-iwasawa 82a8f5
  tjhandle handle = NULL;
shun-iwasawa 82a8f5
  int row, col, iter = 0, dstBufAlloc = 0, retval = 0;
shun-iwasawa 82a8f5
  double elapsed, elapsedDecode;
shun-iwasawa 82a8f5
  int ps = tjPixelSize[pf];
shun-iwasawa 82a8f5
  int scaledw = TJSCALED(w, sf);
shun-iwasawa 82a8f5
  int scaledh = TJSCALED(h, sf);
shun-iwasawa 82a8f5
  int pitch = scaledw * ps;
shun-iwasawa 82a8f5
  int ntilesw = (w + tilew - 1) / tilew, ntilesh = (h + tileh - 1) / tileh;
shun-iwasawa 82a8f5
  unsigned char *dstPtr, *dstPtr2, *yuvBuf = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (jpegQual > 0) {
shun-iwasawa 82a8f5
    snprintf(qualStr, 13, "_Q%d", jpegQual);
shun-iwasawa 82a8f5
    qualStr[12] = 0;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((handle = tjInitDecompress()) == NULL)
shun-iwasawa 82a8f5
    THROW_TJ("executing tjInitDecompress()");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (dstBuf == NULL) {
shun-iwasawa 82a8f5
    if ((unsigned long long)pitch * (unsigned long long)scaledh >
shun-iwasawa 82a8f5
        (unsigned long long)((size_t)-1))
shun-iwasawa 82a8f5
      THROW("allocating destination buffer", "Image is too large");
shun-iwasawa 82a8f5
    if ((dstBuf = (unsigned char *)malloc((size_t)pitch * scaledh)) == NULL)
shun-iwasawa 82a8f5
      THROW_UNIX("allocating destination buffer");
shun-iwasawa 82a8f5
    dstBufAlloc = 1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  /* Set the destination buffer to gray so we know whether the decompressor
shun-iwasawa 82a8f5
     attempted to write to it */
shun-iwasawa 82a8f5
  memset(dstBuf, 127, (size_t)pitch * scaledh);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (doYUV) {
shun-iwasawa 82a8f5
    int width = doTile ? tilew : scaledw;
shun-iwasawa 82a8f5
    int height = doTile ? tileh : scaledh;
shun-iwasawa 82a8f5
    unsigned long yuvSize = tjBufSizeYUV2(width, yuvPad, height, subsamp);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (yuvSize == (unsigned long)-1)
shun-iwasawa 82a8f5
      THROW_TJ("allocating YUV buffer");
shun-iwasawa 82a8f5
    if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
shun-iwasawa 82a8f5
      THROW_UNIX("allocating YUV buffer");
shun-iwasawa 82a8f5
    memset(yuvBuf, 127, yuvSize);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Benchmark */
shun-iwasawa 82a8f5
  iter = -1;
shun-iwasawa 82a8f5
  elapsed = elapsedDecode = 0.;
shun-iwasawa 82a8f5
  while (1) {
shun-iwasawa 82a8f5
    int tile = 0;
shun-iwasawa 82a8f5
    double start = getTime();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    for (row = 0, dstPtr = dstBuf; row < ntilesh;
shun-iwasawa 82a8f5
         row++, dstPtr += (size_t)pitch * tileh) {
shun-iwasawa 82a8f5
      for (col = 0, dstPtr2 = dstPtr; col < ntilesw;
shun-iwasawa 82a8f5
           col++, tile++, dstPtr2 += ps * tilew) {
shun-iwasawa 82a8f5
        int width = doTile ? min(tilew, w - col * tilew) : scaledw;
shun-iwasawa 82a8f5
        int height = doTile ? min(tileh, h - row * tileh) : scaledh;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        if (doYUV) {
shun-iwasawa 82a8f5
          double startDecode;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
          if (tjDecompressToYUV2(handle, jpegBuf[tile], jpegSize[tile], yuvBuf,
shun-iwasawa 82a8f5
                                 width, yuvPad, height, flags) == -1)
shun-iwasawa 82a8f5
            THROW_TJ("executing tjDecompressToYUV2()");
shun-iwasawa 82a8f5
          startDecode = getTime();
shun-iwasawa 82a8f5
          if (tjDecodeYUV(handle, yuvBuf, yuvPad, subsamp, dstPtr2, width,
shun-iwasawa 82a8f5
                          pitch, height, pf, flags) == -1)
shun-iwasawa 82a8f5
            THROW_TJ("executing tjDecodeYUV()");
shun-iwasawa 82a8f5
          if (iter >= 0) elapsedDecode += getTime() - startDecode;
shun-iwasawa 82a8f5
        } else if (tjDecompress2(handle, jpegBuf[tile], jpegSize[tile],
shun-iwasawa 82a8f5
                                 dstPtr2, width, pitch, height, pf,
shun-iwasawa 82a8f5
                                 flags) == -1)
shun-iwasawa 82a8f5
          THROW_TJ("executing tjDecompress2()");
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    elapsed += getTime() - start;
shun-iwasawa 82a8f5
    if (iter >= 0) {
shun-iwasawa 82a8f5
      iter++;
shun-iwasawa 82a8f5
      if (elapsed >= benchTime) break;
shun-iwasawa 82a8f5
    } else if (elapsed >= warmup) {
shun-iwasawa 82a8f5
      iter = 0;
shun-iwasawa 82a8f5
      elapsed = elapsedDecode = 0.;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (doYUV) elapsed -= elapsedDecode;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (tjDestroy(handle) == -1) THROW_TJ("executing tjDestroy()");
shun-iwasawa 82a8f5
  handle = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (quiet) {
shun-iwasawa 82a8f5
    printf("%-6s%s",
shun-iwasawa 82a8f5
           sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
shun-iwasawa 82a8f5
                  tempStr, 1024),
shun-iwasawa 82a8f5
           quiet == 2 ? "\n" : "  ");
shun-iwasawa 82a8f5
    if (doYUV)
shun-iwasawa 82a8f5
      printf("%s\n",
shun-iwasawa 82a8f5
             sigfig((double)(w * h) / 1000000. * (double)iter / elapsedDecode,
shun-iwasawa 82a8f5
                    4, tempStr, 1024));
shun-iwasawa 82a8f5
    else if (quiet != 2) printf("\n");
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    printf("%s --> Frame rate:         %f fps\n",
shun-iwasawa 82a8f5
           doYUV ? "Decomp to YUV" : "Decompress   ", (double)iter / elapsed);
shun-iwasawa 82a8f5
    printf("                  Throughput:         %f Megapixels/sec\n",
shun-iwasawa 82a8f5
           (double)(w * h) / 1000000. * (double)iter / elapsed);
shun-iwasawa 82a8f5
    if (doYUV) {
shun-iwasawa 82a8f5
      printf("YUV Decode    --> Frame rate:         %f fps\n",
shun-iwasawa 82a8f5
             (double)iter / elapsedDecode);
shun-iwasawa 82a8f5
      printf("                  Throughput:         %f Megapixels/sec\n",
shun-iwasawa 82a8f5
             (double)(w * h) / 1000000. * (double)iter / elapsedDecode);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!doWrite) goto bailout;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (sf.num != 1 || sf.denom != 1)
shun-iwasawa 82a8f5
    snprintf(sizeStr, 24, "%d_%d", sf.num, sf.denom);
shun-iwasawa 82a8f5
  else if (tilew != w || tileh != h)
shun-iwasawa 82a8f5
    snprintf(sizeStr, 24, "%dx%d", tilew, tileh);
shun-iwasawa 82a8f5
  else snprintf(sizeStr, 24, "full");
shun-iwasawa 82a8f5
  if (decompOnly)
shun-iwasawa 82a8f5
    snprintf(tempStr, 1024, "%s_%s.%s", fileName, sizeStr, ext);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    snprintf(tempStr, 1024, "%s_%s%s_%s.%s", fileName, subName[subsamp],
shun-iwasawa 82a8f5
             qualStr, sizeStr, ext);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (tjSaveImage(tempStr, dstBuf, scaledw, 0, scaledh, pf, flags) == -1)
shun-iwasawa 82a8f5
    THROW_TJG("saving bitmap");
shun-iwasawa 82a8f5
  ptr = strrchr(tempStr, '.');
shun-iwasawa 82a8f5
  snprintf(ptr, 1024 - (ptr - tempStr), "-err.%s", ext);
shun-iwasawa 82a8f5
  if (srcBuf && sf.num == 1 && sf.denom == 1) {
shun-iwasawa 82a8f5
    if (!quiet) printf("Compression error written to %s.\n", tempStr);
shun-iwasawa 82a8f5
    if (subsamp == TJ_GRAYSCALE) {
shun-iwasawa 82a8f5
      unsigned long index, index2;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      for (row = 0, index = 0; row < h; row++, index += pitch) {
shun-iwasawa 82a8f5
        for (col = 0, index2 = index; col < w; col++, index2 += ps) {
shun-iwasawa 82a8f5
          unsigned long rindex = index2 + tjRedOffset[pf];
shun-iwasawa 82a8f5
          unsigned long gindex = index2 + tjGreenOffset[pf];
shun-iwasawa 82a8f5
          unsigned long bindex = index2 + tjBlueOffset[pf];
shun-iwasawa 82a8f5
          int y = (int)((double)srcBuf[rindex] * 0.299 +
shun-iwasawa 82a8f5
                        (double)srcBuf[gindex] * 0.587 +
shun-iwasawa 82a8f5
                        (double)srcBuf[bindex] * 0.114 + 0.5);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
          if (y > 255) y = 255;
shun-iwasawa 82a8f5
          if (y < 0) y = 0;
shun-iwasawa 82a8f5
          dstBuf[rindex] = abs(dstBuf[rindex] - y);
shun-iwasawa 82a8f5
          dstBuf[gindex] = abs(dstBuf[gindex] - y);
shun-iwasawa 82a8f5
          dstBuf[bindex] = abs(dstBuf[bindex] - y);
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      for (row = 0; row < h; row++)
shun-iwasawa 82a8f5
        for (col = 0; col < w * ps; col++)
shun-iwasawa 82a8f5
          dstBuf[pitch * row + col] =
shun-iwasawa 82a8f5
            abs(dstBuf[pitch * row + col] - srcBuf[pitch * row + col]);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if (tjSaveImage(tempStr, dstBuf, w, 0, h, pf, flags) == -1)
shun-iwasawa 82a8f5
      THROW_TJG("saving bitmap");
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (file) fclose(file);
shun-iwasawa 82a8f5
  if (handle) tjDestroy(handle);
shun-iwasawa 82a8f5
  if (dstBufAlloc) free(dstBuf);
shun-iwasawa 82a8f5
  free(yuvBuf);
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int fullTest(unsigned char *srcBuf, int w, int h, int subsamp,
shun-iwasawa 82a8f5
                    int jpegQual, char *fileName)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  char tempStr[1024], tempStr2[80];
shun-iwasawa 82a8f5
  FILE *file = NULL;
shun-iwasawa 82a8f5
  tjhandle handle = NULL;
shun-iwasawa 82a8f5
  unsigned char **jpegBuf = NULL, *yuvBuf = NULL, *tmpBuf = NULL, *srcPtr,
shun-iwasawa 82a8f5
    *srcPtr2;
shun-iwasawa 82a8f5
  double start, elapsed, elapsedEncode;
shun-iwasawa 82a8f5
  int totalJpegSize = 0, row, col, i, tilew = w, tileh = h, retval = 0;
shun-iwasawa 82a8f5
  int iter;
shun-iwasawa 82a8f5
  unsigned long *jpegSize = NULL, yuvSize = 0;
shun-iwasawa 82a8f5
  int ps = tjPixelSize[pf];
shun-iwasawa 82a8f5
  int ntilesw = 1, ntilesh = 1, pitch = w * ps;
shun-iwasawa 82a8f5
  const char *pfStr = pixFormatStr[pf];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((unsigned long long)pitch * (unsigned long long)h >
shun-iwasawa 82a8f5
      (unsigned long long)((size_t)-1))
shun-iwasawa 82a8f5
    THROW("allocating temporary image buffer", "Image is too large");
shun-iwasawa 82a8f5
  if ((tmpBuf = (unsigned char *)malloc((size_t)pitch * h)) == NULL)
shun-iwasawa 82a8f5
    THROW_UNIX("allocating temporary image buffer");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!quiet)
shun-iwasawa 82a8f5
    printf(">>>>>  %s (%s) <--> JPEG %s Q%d  <<<<<\n", pfStr,
shun-iwasawa 82a8f5
           (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down",
shun-iwasawa 82a8f5
           subNameLong[subsamp], jpegQual);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (tilew = doTile ? 8 : w, tileh = doTile ? 8 : h; ;
shun-iwasawa 82a8f5
       tilew *= 2, tileh *= 2) {
shun-iwasawa 82a8f5
    if (tilew > w) tilew = w;
shun-iwasawa 82a8f5
    if (tileh > h) tileh = h;
shun-iwasawa 82a8f5
    ntilesw = (w + tilew - 1) / tilew;
shun-iwasawa 82a8f5
    ntilesh = (h + tileh - 1) / tileh;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
shun-iwasawa 82a8f5
                                            ntilesw * ntilesh)) == NULL)
shun-iwasawa 82a8f5
      THROW_UNIX("allocating JPEG tile array");
shun-iwasawa 82a8f5
    memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
shun-iwasawa 82a8f5
    if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
shun-iwasawa 82a8f5
                                            ntilesw * ntilesh)) == NULL)
shun-iwasawa 82a8f5
      THROW_UNIX("allocating JPEG size array");
shun-iwasawa 82a8f5
    memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if ((flags & TJFLAG_NOREALLOC) != 0)
shun-iwasawa 82a8f5
      for (i = 0; i < ntilesw * ntilesh; i++) {
shun-iwasawa 82a8f5
        if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
shun-iwasawa 82a8f5
          THROW("getting buffer size", "Image is too large");
shun-iwasawa 82a8f5
        if ((jpegBuf[i] = (unsigned char *)
shun-iwasawa 82a8f5
                          tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
shun-iwasawa 82a8f5
          THROW_UNIX("allocating JPEG tiles");
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Compression test */
shun-iwasawa 82a8f5
    if (quiet == 1)
shun-iwasawa 82a8f5
      printf("%-4s (%s)  %-5s    %-3d   ", pfStr,
shun-iwasawa 82a8f5
             (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", subNameLong[subsamp],
shun-iwasawa 82a8f5
             jpegQual);
shun-iwasawa 82a8f5
    for (i = 0; i < h; i++)
shun-iwasawa 82a8f5
      memcpy(&tmpBuf[pitch * i], &srcBuf[w * ps * i], w * ps);
shun-iwasawa 82a8f5
    if ((handle = tjInitCompress()) == NULL)
shun-iwasawa 82a8f5
      THROW_TJ("executing tjInitCompress()");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (doYUV) {
shun-iwasawa 82a8f5
      yuvSize = tjBufSizeYUV2(tilew, yuvPad, tileh, subsamp);
shun-iwasawa 82a8f5
      if (yuvSize == (unsigned long)-1)
shun-iwasawa 82a8f5
        THROW_TJ("allocating YUV buffer");
shun-iwasawa 82a8f5
      if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
shun-iwasawa 82a8f5
        THROW_UNIX("allocating YUV buffer");
shun-iwasawa 82a8f5
      memset(yuvBuf, 127, yuvSize);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Benchmark */
shun-iwasawa 82a8f5
    iter = -1;
shun-iwasawa 82a8f5
    elapsed = elapsedEncode = 0.;
shun-iwasawa 82a8f5
    while (1) {
shun-iwasawa 82a8f5
      int tile = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      totalJpegSize = 0;
shun-iwasawa 82a8f5
      start = getTime();
shun-iwasawa 82a8f5
      for (row = 0, srcPtr = srcBuf; row < ntilesh;
shun-iwasawa 82a8f5
           row++, srcPtr += pitch * tileh) {
shun-iwasawa 82a8f5
        for (col = 0, srcPtr2 = srcPtr; col < ntilesw;
shun-iwasawa 82a8f5
             col++, tile++, srcPtr2 += ps * tilew) {
shun-iwasawa 82a8f5
          int width = min(tilew, w - col * tilew);
shun-iwasawa 82a8f5
          int height = min(tileh, h - row * tileh);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
          if (doYUV) {
shun-iwasawa 82a8f5
            double startEncode = getTime();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
            if (tjEncodeYUV3(handle, srcPtr2, width, pitch, height, pf, yuvBuf,
shun-iwasawa 82a8f5
                             yuvPad, subsamp, flags) == -1)
shun-iwasawa 82a8f5
              THROW_TJ("executing tjEncodeYUV3()");
shun-iwasawa 82a8f5
            if (iter >= 0) elapsedEncode += getTime() - startEncode;
shun-iwasawa 82a8f5
            if (tjCompressFromYUV(handle, yuvBuf, width, yuvPad, height,
shun-iwasawa 82a8f5
                                  subsamp, &jpegBuf[tile], &jpegSize[tile],
shun-iwasawa 82a8f5
                                  jpegQual, flags) == -1)
shun-iwasawa 82a8f5
              THROW_TJ("executing tjCompressFromYUV()");
shun-iwasawa 82a8f5
          } else {
shun-iwasawa 82a8f5
            if (tjCompress2(handle, srcPtr2, width, pitch, height, pf,
shun-iwasawa 82a8f5
                            &jpegBuf[tile], &jpegSize[tile], subsamp, jpegQual,
shun-iwasawa 82a8f5
                            flags) == -1)
shun-iwasawa 82a8f5
              THROW_TJ("executing tjCompress2()");
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
          totalJpegSize += jpegSize[tile];
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      elapsed += getTime() - start;
shun-iwasawa 82a8f5
      if (iter >= 0) {
shun-iwasawa 82a8f5
        iter++;
shun-iwasawa 82a8f5
        if (elapsed >= benchTime) break;
shun-iwasawa 82a8f5
      } else if (elapsed >= warmup) {
shun-iwasawa 82a8f5
        iter = 0;
shun-iwasawa 82a8f5
        elapsed = elapsedEncode = 0.;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if (doYUV) elapsed -= elapsedEncode;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (tjDestroy(handle) == -1) THROW_TJ("executing tjDestroy()");
shun-iwasawa 82a8f5
    handle = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (quiet == 1) printf("%-5d  %-5d   ", tilew, tileh);
shun-iwasawa 82a8f5
    if (quiet) {
shun-iwasawa 82a8f5
      if (doYUV)
shun-iwasawa 82a8f5
        printf("%-6s%s",
shun-iwasawa 82a8f5
               sigfig((double)(w * h) / 1000000. *
shun-iwasawa 82a8f5
                      (double)iter / elapsedEncode, 4, tempStr, 1024),
shun-iwasawa 82a8f5
               quiet == 2 ? "\n" : "  ");
shun-iwasawa 82a8f5
      printf("%-6s%s",
shun-iwasawa 82a8f5
             sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
shun-iwasawa 82a8f5
                    tempStr, 1024),
shun-iwasawa 82a8f5
             quiet == 2 ? "\n" : "  ");
shun-iwasawa 82a8f5
      printf("%-6s%s",
shun-iwasawa 82a8f5
             sigfig((double)(w * h * ps) / (double)totalJpegSize, 4, tempStr2,
shun-iwasawa 82a8f5
                    80),
shun-iwasawa 82a8f5
             quiet == 2 ? "\n" : "  ");
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      printf("\n%s size: %d x %d\n", doTile ? "Tile" : "Image", tilew, tileh);
shun-iwasawa 82a8f5
      if (doYUV) {
shun-iwasawa 82a8f5
        printf("Encode YUV    --> Frame rate:         %f fps\n",
shun-iwasawa 82a8f5
               (double)iter / elapsedEncode);
shun-iwasawa 82a8f5
        printf("                  Output image size:  %lu bytes\n", yuvSize);
shun-iwasawa 82a8f5
        printf("                  Compression ratio:  %f:1\n",
shun-iwasawa 82a8f5
               (double)(w * h * ps) / (double)yuvSize);
shun-iwasawa 82a8f5
        printf("                  Throughput:         %f Megapixels/sec\n",
shun-iwasawa 82a8f5
               (double)(w * h) / 1000000. * (double)iter / elapsedEncode);
shun-iwasawa 82a8f5
        printf("                  Output bit stream:  %f Megabits/sec\n",
shun-iwasawa 82a8f5
               (double)yuvSize * 8. / 1000000. * (double)iter / elapsedEncode);
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      printf("%s --> Frame rate:         %f fps\n",
shun-iwasawa 82a8f5
             doYUV ? "Comp from YUV" : "Compress     ",
shun-iwasawa 82a8f5
             (double)iter / elapsed);
shun-iwasawa 82a8f5
      printf("                  Output image size:  %d bytes\n",
shun-iwasawa 82a8f5
             totalJpegSize);
shun-iwasawa 82a8f5
      printf("                  Compression ratio:  %f:1\n",
shun-iwasawa 82a8f5
             (double)(w * h * ps) / (double)totalJpegSize);
shun-iwasawa 82a8f5
      printf("                  Throughput:         %f Megapixels/sec\n",
shun-iwasawa 82a8f5
             (double)(w * h) / 1000000. * (double)iter / elapsed);
shun-iwasawa 82a8f5
      printf("                  Output bit stream:  %f Megabits/sec\n",
shun-iwasawa 82a8f5
             (double)totalJpegSize * 8. / 1000000. * (double)iter / elapsed);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if (tilew == w && tileh == h && doWrite) {
shun-iwasawa 82a8f5
      snprintf(tempStr, 1024, "%s_%s_Q%d.jpg", fileName, subName[subsamp],
shun-iwasawa 82a8f5
               jpegQual);
shun-iwasawa 82a8f5
      if ((file = fopen(tempStr, "wb")) == NULL)
shun-iwasawa 82a8f5
        THROW_UNIX("opening reference image");
shun-iwasawa 82a8f5
      if (fwrite(jpegBuf[0], jpegSize[0], 1, file) != 1)
shun-iwasawa 82a8f5
        THROW_UNIX("writing reference image");
shun-iwasawa 82a8f5
      fclose(file);  file = NULL;
shun-iwasawa 82a8f5
      if (!quiet) printf("Reference image written to %s\n", tempStr);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Decompression test */
shun-iwasawa 82a8f5
    if (!compOnly) {
shun-iwasawa 82a8f5
      if (decomp(srcBuf, jpegBuf, jpegSize, tmpBuf, w, h, subsamp, jpegQual,
shun-iwasawa 82a8f5
                 fileName, tilew, tileh) == -1)
shun-iwasawa 82a8f5
        goto bailout;
shun-iwasawa 82a8f5
    } else if (quiet == 1) printf("N/A\n");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    for (i = 0; i < ntilesw * ntilesh; i++) {
shun-iwasawa 82a8f5
      tjFree(jpegBuf[i]);
shun-iwasawa 82a8f5
      jpegBuf[i] = NULL;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    free(jpegBuf);  jpegBuf = NULL;
shun-iwasawa 82a8f5
    free(jpegSize);  jpegSize = NULL;
shun-iwasawa 82a8f5
    if (doYUV) {
shun-iwasawa 82a8f5
      free(yuvBuf);  yuvBuf = NULL;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (tilew == w && tileh == h) break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (file) fclose(file);
shun-iwasawa 82a8f5
  if (jpegBuf) {
shun-iwasawa 82a8f5
    for (i = 0; i < ntilesw * ntilesh; i++)
shun-iwasawa 82a8f5
      tjFree(jpegBuf[i]);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  free(jpegBuf);
shun-iwasawa 82a8f5
  free(yuvBuf);
shun-iwasawa 82a8f5
  free(jpegSize);
shun-iwasawa 82a8f5
  free(tmpBuf);
shun-iwasawa 82a8f5
  if (handle) tjDestroy(handle);
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int decompTest(char *fileName)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  FILE *file = NULL;
shun-iwasawa 82a8f5
  tjhandle handle = NULL;
shun-iwasawa 82a8f5
  unsigned char **jpegBuf = NULL, *srcBuf = NULL;
shun-iwasawa 82a8f5
  unsigned long *jpegSize = NULL, srcSize, totalJpegSize;
shun-iwasawa 82a8f5
  tjtransform *t = NULL;
shun-iwasawa 82a8f5
  double start, elapsed;
shun-iwasawa 82a8f5
  int ps = tjPixelSize[pf], tile, row, col, i, iter, retval = 0, decompsrc = 0;
shun-iwasawa 82a8f5
  char *temp = NULL, tempStr[80], tempStr2[80];
shun-iwasawa 82a8f5
  /* Original image */
shun-iwasawa 82a8f5
  int w = 0, h = 0, tilew, tileh, ntilesw = 1, ntilesh = 1, subsamp = -1,
shun-iwasawa 82a8f5
    cs = -1;
shun-iwasawa 82a8f5
  /* Transformed image */
shun-iwasawa 82a8f5
  int tw, th, ttilew, ttileh, tntilesw, tntilesh, tsubsamp;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((file = fopen(fileName, "rb")) == NULL)
shun-iwasawa 82a8f5
    THROW_UNIX("opening file");
shun-iwasawa 82a8f5
  if (fseek(file, 0, SEEK_END) < 0 ||
shun-iwasawa 82a8f5
      (srcSize = ftell(file)) == (unsigned long)-1)
shun-iwasawa 82a8f5
    THROW_UNIX("determining file size");
shun-iwasawa 82a8f5
  if ((srcBuf = (unsigned char *)malloc(srcSize)) == NULL)
shun-iwasawa 82a8f5
    THROW_UNIX("allocating memory");
shun-iwasawa 82a8f5
  if (fseek(file, 0, SEEK_SET) < 0)
shun-iwasawa 82a8f5
    THROW_UNIX("setting file position");
shun-iwasawa 82a8f5
  if (fread(srcBuf, srcSize, 1, file) < 1)
shun-iwasawa 82a8f5
    THROW_UNIX("reading JPEG data");
shun-iwasawa 82a8f5
  fclose(file);  file = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  temp = strrchr(fileName, '.');
shun-iwasawa 82a8f5
  if (temp != NULL) *temp = '\0';
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((handle = tjInitTransform()) == NULL)
shun-iwasawa 82a8f5
    THROW_TJ("executing tjInitTransform()");
shun-iwasawa 82a8f5
  if (tjDecompressHeader3(handle, srcBuf, srcSize, &w, &h, &subsamp,
shun-iwasawa 82a8f5
                          &cs) == -1)
shun-iwasawa 82a8f5
    THROW_TJ("executing tjDecompressHeader3()");
shun-iwasawa 82a8f5
  if (w < 1 || h < 1)
shun-iwasawa 82a8f5
    THROW("reading JPEG header", "Invalid image dimensions");
shun-iwasawa 82a8f5
  if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
shun-iwasawa 82a8f5
    pf = TJPF_CMYK;  ps = tjPixelSize[pf];
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (quiet == 1) {
shun-iwasawa 82a8f5
    printf("All performance values in Mpixels/sec\n\n");
shun-iwasawa 82a8f5
    printf("Bitmap     JPEG   JPEG     %s  %s   Xform   Comp    Decomp  ",
shun-iwasawa 82a8f5
           doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
shun-iwasawa 82a8f5
    if (doYUV) printf("Decode");
shun-iwasawa 82a8f5
    printf("\n");
shun-iwasawa 82a8f5
    printf("Format     CS     Subsamp  Width  Height  Perf    Ratio   Perf    ");
shun-iwasawa 82a8f5
    if (doYUV) printf("Perf");
shun-iwasawa 82a8f5
    printf("\n\n");
shun-iwasawa 82a8f5
  } else if (!quiet)
shun-iwasawa 82a8f5
    printf(">>>>>  JPEG %s --> %s (%s)  <<<<<\n",
shun-iwasawa 82a8f5
           formatName(subsamp, cs, tempStr), pixFormatStr[pf],
shun-iwasawa 82a8f5
           (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (tilew = doTile ? 16 : w, tileh = doTile ? 16 : h; ;
shun-iwasawa 82a8f5
       tilew *= 2, tileh *= 2) {
shun-iwasawa 82a8f5
    if (tilew > w) tilew = w;
shun-iwasawa 82a8f5
    if (tileh > h) tileh = h;
shun-iwasawa 82a8f5
    ntilesw = (w + tilew - 1) / tilew;
shun-iwasawa 82a8f5
    ntilesh = (h + tileh - 1) / tileh;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
shun-iwasawa 82a8f5
                                            ntilesw * ntilesh)) == NULL)
shun-iwasawa 82a8f5
      THROW_UNIX("allocating JPEG tile array");
shun-iwasawa 82a8f5
    memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
shun-iwasawa 82a8f5
    if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
shun-iwasawa 82a8f5
                                            ntilesw * ntilesh)) == NULL)
shun-iwasawa 82a8f5
      THROW_UNIX("allocating JPEG size array");
shun-iwasawa 82a8f5
    memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if ((flags & TJFLAG_NOREALLOC) != 0 &&
shun-iwasawa 82a8f5
        (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter))
shun-iwasawa 82a8f5
      for (i = 0; i < ntilesw * ntilesh; i++) {
shun-iwasawa 82a8f5
        if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
shun-iwasawa 82a8f5
          THROW("getting buffer size", "Image is too large");
shun-iwasawa 82a8f5
        if ((jpegBuf[i] = (unsigned char *)
shun-iwasawa 82a8f5
                          tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
shun-iwasawa 82a8f5
          THROW_UNIX("allocating JPEG tiles");
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    tw = w;  th = h;  ttilew = tilew;  ttileh = tileh;
shun-iwasawa 82a8f5
    if (!quiet) {
shun-iwasawa 82a8f5
      printf("\n%s size: %d x %d", doTile ? "Tile" : "Image", ttilew, ttileh);
shun-iwasawa 82a8f5
      if (sf.num != 1 || sf.denom != 1)
shun-iwasawa 82a8f5
        printf(" --> %d x %d", TJSCALED(tw, sf), TJSCALED(th, sf));
shun-iwasawa 82a8f5
      printf("\n");
shun-iwasawa 82a8f5
    } else if (quiet == 1) {
shun-iwasawa 82a8f5
      printf("%-4s (%s)  %-5s  %-5s    ", pixFormatStr[pf],
shun-iwasawa 82a8f5
             (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", csName[cs],
shun-iwasawa 82a8f5
             subNameLong[subsamp]);
shun-iwasawa 82a8f5
      printf("%-5d  %-5d   ", tilew, tileh);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    tsubsamp = subsamp;
shun-iwasawa 82a8f5
    if (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter) {
shun-iwasawa 82a8f5
      if ((t = (tjtransform *)malloc(sizeof(tjtransform) * ntilesw *
shun-iwasawa 82a8f5
                                     ntilesh)) == NULL)
shun-iwasawa 82a8f5
        THROW_UNIX("allocating image transform array");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
shun-iwasawa 82a8f5
          xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
shun-iwasawa 82a8f5
        tw = h;  th = w;  ttilew = tileh;  ttileh = tilew;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      if (xformOpt & TJXOPT_GRAY) tsubsamp = TJ_GRAYSCALE;
shun-iwasawa 82a8f5
      if (xformOp == TJXOP_HFLIP || xformOp == TJXOP_ROT180)
shun-iwasawa 82a8f5
        tw = tw - (tw % tjMCUWidth[tsubsamp]);
shun-iwasawa 82a8f5
      if (xformOp == TJXOP_VFLIP || xformOp == TJXOP_ROT180)
shun-iwasawa 82a8f5
        th = th - (th % tjMCUHeight[tsubsamp]);
shun-iwasawa 82a8f5
      if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT90)
shun-iwasawa 82a8f5
        tw = tw - (tw % tjMCUHeight[tsubsamp]);
shun-iwasawa 82a8f5
      if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT270)
shun-iwasawa 82a8f5
        th = th - (th % tjMCUWidth[tsubsamp]);
shun-iwasawa 82a8f5
      tntilesw = (tw + ttilew - 1) / ttilew;
shun-iwasawa 82a8f5
      tntilesh = (th + ttileh - 1) / ttileh;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
shun-iwasawa 82a8f5
          xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
shun-iwasawa 82a8f5
        if (tsubsamp == TJSAMP_422) tsubsamp = TJSAMP_440;
shun-iwasawa 82a8f5
        else if (tsubsamp == TJSAMP_440) tsubsamp = TJSAMP_422;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      for (row = 0, tile = 0; row < tntilesh; row++) {
shun-iwasawa 82a8f5
        for (col = 0; col < tntilesw; col++, tile++) {
shun-iwasawa 82a8f5
          t[tile].r.w = min(ttilew, tw - col * ttilew);
shun-iwasawa 82a8f5
          t[tile].r.h = min(ttileh, th - row * ttileh);
shun-iwasawa 82a8f5
          t[tile].r.x = col * ttilew;
shun-iwasawa 82a8f5
          t[tile].r.y = row * ttileh;
shun-iwasawa 82a8f5
          t[tile].op = xformOp;
shun-iwasawa 82a8f5
          t[tile].options = xformOpt | TJXOPT_TRIM;
shun-iwasawa 82a8f5
          t[tile].customFilter = customFilter;
shun-iwasawa 82a8f5
          if (t[tile].options & TJXOPT_NOOUTPUT && jpegBuf[tile]) {
shun-iwasawa 82a8f5
            tjFree(jpegBuf[tile]);  jpegBuf[tile] = NULL;
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      iter = -1;
shun-iwasawa 82a8f5
      elapsed = 0.;
shun-iwasawa 82a8f5
      while (1) {
shun-iwasawa 82a8f5
        start = getTime();
shun-iwasawa 82a8f5
        if (tjTransform(handle, srcBuf, srcSize, tntilesw * tntilesh, jpegBuf,
shun-iwasawa 82a8f5
                        jpegSize, t, flags) == -1)
shun-iwasawa 82a8f5
          THROW_TJ("executing tjTransform()");
shun-iwasawa 82a8f5
        elapsed += getTime() - start;
shun-iwasawa 82a8f5
        if (iter >= 0) {
shun-iwasawa 82a8f5
          iter++;
shun-iwasawa 82a8f5
          if (elapsed >= benchTime) break;
shun-iwasawa 82a8f5
        } else if (elapsed >= warmup) {
shun-iwasawa 82a8f5
          iter = 0;
shun-iwasawa 82a8f5
          elapsed = 0.;
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      free(t);  t = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      for (tile = 0, totalJpegSize = 0; tile < tntilesw * tntilesh; tile++)
shun-iwasawa 82a8f5
        totalJpegSize += jpegSize[tile];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      if (quiet) {
shun-iwasawa 82a8f5
        printf("%-6s%s%-6s%s",
shun-iwasawa 82a8f5
               sigfig((double)(w * h) / 1000000. / elapsed, 4, tempStr, 80),
shun-iwasawa 82a8f5
               quiet == 2 ? "\n" : "  ",
shun-iwasawa 82a8f5
               sigfig((double)(w * h * ps) / (double)totalJpegSize, 4,
shun-iwasawa 82a8f5
                      tempStr2, 80),
shun-iwasawa 82a8f5
               quiet == 2 ? "\n" : "  ");
shun-iwasawa 82a8f5
      } else if (!quiet) {
shun-iwasawa 82a8f5
        printf("Transform     --> Frame rate:         %f fps\n",
shun-iwasawa 82a8f5
               1.0 / elapsed);
shun-iwasawa 82a8f5
        printf("                  Output image size:  %lu bytes\n",
shun-iwasawa 82a8f5
               totalJpegSize);
shun-iwasawa 82a8f5
        printf("                  Compression ratio:  %f:1\n",
shun-iwasawa 82a8f5
               (double)(w * h * ps) / (double)totalJpegSize);
shun-iwasawa 82a8f5
        printf("                  Throughput:         %f Megapixels/sec\n",
shun-iwasawa 82a8f5
               (double)(w * h) / 1000000. / elapsed);
shun-iwasawa 82a8f5
        printf("                  Output bit stream:  %f Megabits/sec\n",
shun-iwasawa 82a8f5
               (double)totalJpegSize * 8. / 1000000. / elapsed);
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      if (quiet == 1) printf("N/A     N/A     ");
shun-iwasawa 82a8f5
      tjFree(jpegBuf[0]);
shun-iwasawa 82a8f5
      jpegBuf[0] = NULL;
shun-iwasawa 82a8f5
      decompsrc = 1;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (w == tilew) ttilew = tw;
shun-iwasawa 82a8f5
    if (h == tileh) ttileh = th;
shun-iwasawa 82a8f5
    if (!(xformOpt & TJXOPT_NOOUTPUT)) {
shun-iwasawa 82a8f5
      if (decomp(NULL, decompsrc ? &srcBuf : jpegBuf,
shun-iwasawa 82a8f5
                 decompsrc ? &srcSize : jpegSize, NULL, tw, th, tsubsamp, 0,
shun-iwasawa 82a8f5
                 fileName, ttilew, ttileh) == -1)
shun-iwasawa 82a8f5
        goto bailout;
shun-iwasawa 82a8f5
    } else if (quiet == 1) printf("N/A\n");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    for (i = 0; i < ntilesw * ntilesh; i++) {
shun-iwasawa 82a8f5
      tjFree(jpegBuf[i]);
shun-iwasawa 82a8f5
      jpegBuf[i] = NULL;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    free(jpegBuf);  jpegBuf = NULL;
shun-iwasawa 82a8f5
    free(jpegSize);  jpegSize = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (tilew == w && tileh == h) break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (file) fclose(file);
shun-iwasawa 82a8f5
  if (jpegBuf) {
shun-iwasawa 82a8f5
    for (i = 0; i < ntilesw * ntilesh; i++)
shun-iwasawa 82a8f5
      tjFree(jpegBuf[i]);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  free(jpegBuf);
shun-iwasawa 82a8f5
  free(jpegSize);
shun-iwasawa 82a8f5
  free(srcBuf);
shun-iwasawa 82a8f5
  free(t);
shun-iwasawa 82a8f5
  if (handle) { tjDestroy(handle);  handle = NULL; }
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void usage(char *progName)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int i;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  printf("USAGE: %s\n", progName);
shun-iwasawa 82a8f5
  printf("       <inputfile (bmp|ppm)=""> <quality> [options]\n\n");</quality></inputfile>
shun-iwasawa 82a8f5
  printf("       %s\n", progName);
shun-iwasawa 82a8f5
  printf("       <inputfile (jpg)=""> [options]\n\n");</inputfile>
shun-iwasawa 82a8f5
  printf("Options:\n\n");
shun-iwasawa 82a8f5
  printf("-alloc = Dynamically allocate JPEG image buffers\n");
shun-iwasawa 82a8f5
  printf("-bmp = Generate output images in Windows Bitmap format (default = PPM)\n");
shun-iwasawa 82a8f5
  printf("-bottomup = Test bottom-up compression/decompression\n");
shun-iwasawa 82a8f5
  printf("-tile = Test performance of the codec when the image is encoded as separate\n");
shun-iwasawa 82a8f5
  printf("     tiles of varying sizes.\n");
shun-iwasawa 82a8f5
  printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
shun-iwasawa 82a8f5
  printf("     Test the specified color conversion path in the codec (default = BGR)\n");
shun-iwasawa 82a8f5
  printf("-cmyk = Indirectly test YCCK JPEG compression/decompression (the source\n");
shun-iwasawa 82a8f5
  printf("     and destination bitmaps are still RGB.  The conversion is done\n");
shun-iwasawa 82a8f5
  printf("     internally prior to compression or after decompression.)\n");
shun-iwasawa 82a8f5
  printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n");
shun-iwasawa 82a8f5
  printf("     the underlying codec\n");
shun-iwasawa 82a8f5
  printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n");
shun-iwasawa 82a8f5
  printf("     codec\n");
shun-iwasawa 82a8f5
  printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n");
shun-iwasawa 82a8f5
  printf("     underlying codec\n");
shun-iwasawa 82a8f5
  printf("-progressive = Use progressive entropy coding in JPEG images generated by\n");
shun-iwasawa 82a8f5
  printf("     compression and transform operations.\n");
shun-iwasawa 82a8f5
  printf("-subsamp <s> = When testing JPEG compression, this option specifies the level\n");</s>
shun-iwasawa 82a8f5
  printf("     of chrominance subsampling to use (<s> = 444, 422, 440, 420, 411, or\n");</s>
shun-iwasawa 82a8f5
  printf("     GRAY).  The default is to test Grayscale, 4:2:0, 4:2:2, and 4:4:4 in\n");
shun-iwasawa 82a8f5
  printf("     sequence.\n");
shun-iwasawa 82a8f5
  printf("-quiet = Output results in tabular rather than verbose format\n");
shun-iwasawa 82a8f5
  printf("-yuv = Test YUV encoding/decoding functions\n");
shun-iwasawa 82a8f5
  printf("-yuvpad 

= If testing YUV encoding/decoding, this specifies the number of\n");

shun-iwasawa 82a8f5
  printf("     bytes to which each row of each plane in the intermediate YUV image is\n");
shun-iwasawa 82a8f5
  printf("     padded (default = 1)\n");
shun-iwasawa 82a8f5
  printf("-scale M/N = Scale down the width/height of the decompressed JPEG image by a\n");
shun-iwasawa 82a8f5
  printf("     factor of M/N (M/N = ");
shun-iwasawa 82a8f5
  for (i = 0; i < nsf; i++) {
shun-iwasawa 82a8f5
    printf("%d/%d", scalingFactors[i].num, scalingFactors[i].denom);
shun-iwasawa 82a8f5
    if (nsf == 2 && i != nsf - 1) printf(" or ");
shun-iwasawa 82a8f5
    else if (nsf > 2) {
shun-iwasawa 82a8f5
      if (i != nsf - 1) printf(", ");
shun-iwasawa 82a8f5
      if (i == nsf - 2) printf("or ");
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if (i % 8 == 0 && i != 0) printf("\n     ");
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  printf(")\n");
shun-iwasawa 82a8f5
  printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
shun-iwasawa 82a8f5
  printf("     Perform the corresponding lossless transform prior to\n");
shun-iwasawa 82a8f5
  printf("     decompression (these options are mutually exclusive)\n");
shun-iwasawa 82a8f5
  printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
shun-iwasawa 82a8f5
  printf("     test (can be combined with the other transforms above)\n");
shun-iwasawa 82a8f5
  printf("-copynone = Do not copy any extra markers (including EXIF and ICC profile data)\n");
shun-iwasawa 82a8f5
  printf("     when transforming the image.\n");
shun-iwasawa 82a8f5
  printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n");</t></t>
shun-iwasawa 82a8f5
  printf("-warmup <t> = Run each benchmark for <t> seconds (default = 1.0) prior to\n");</t></t>
shun-iwasawa 82a8f5
  printf("     starting the timer, in order to prime the caches and thus improve the\n");
shun-iwasawa 82a8f5
  printf("     consistency of the results.\n");
shun-iwasawa 82a8f5
  printf("-componly = Stop after running compression tests.  Do not test decompression.\n");
shun-iwasawa 82a8f5
  printf("-nowrite = Do not write reference or output images (improves consistency of\n");
shun-iwasawa 82a8f5
  printf("     performance measurements.)\n");
shun-iwasawa 82a8f5
  printf("-stoponwarning = Immediately discontinue the current\n");
shun-iwasawa 82a8f5
  printf("     compression/decompression/transform operation if the underlying codec\n");
shun-iwasawa 82a8f5
  printf("     throws a warning (non-fatal error)\n\n");
shun-iwasawa 82a8f5
  printf("NOTE:  If the quality is specified as a range (e.g. 90-100), a separate\n");
shun-iwasawa 82a8f5
  printf("test will be performed for all quality values in the range.\n\n");
shun-iwasawa 82a8f5
  exit(1);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
int main(int argc, char *argv[])
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  unsigned char *srcBuf = NULL;
shun-iwasawa 82a8f5
  int w = 0, h = 0, i, j, minQual = -1, maxQual = -1;
shun-iwasawa 82a8f5
  char *temp;
shun-iwasawa 82a8f5
  int minArg = 2, retval = 0, subsamp = -1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((scalingFactors = tjGetScalingFactors(&nsf)) == NULL || nsf == 0)
shun-iwasawa 82a8f5
    THROW("executing tjGetScalingFactors()", tjGetErrorStr());
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (argc < minArg) usage(argv[0]);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  temp = strrchr(argv[1], '.');
shun-iwasawa 82a8f5
  if (temp != NULL) {
shun-iwasawa 82a8f5
    if (!strcasecmp(temp, ".bmp")) ext = "bmp";
shun-iwasawa 82a8f5
    if (!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg"))
shun-iwasawa 82a8f5
      decompOnly = 1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  printf("\n");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!decompOnly) {
shun-iwasawa 82a8f5
    minArg = 3;
shun-iwasawa 82a8f5
    if (argc < minArg) usage(argv[0]);
shun-iwasawa 82a8f5
    if ((minQual = atoi(argv[2])) < 1 || minQual > 100) {
shun-iwasawa 82a8f5
      puts("ERROR: Quality must be between 1 and 100.");
shun-iwasawa 82a8f5
      exit(1);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if ((temp = strchr(argv[2], '-')) != NULL && strlen(temp) > 1 &&
shun-iwasawa 82a8f5
        sscanf(&temp[1], "%d", &maxQual) == 1 && maxQual > minQual &&
shun-iwasawa 82a8f5
        maxQual >= 1 && maxQual <= 100) {}
shun-iwasawa 82a8f5
    else maxQual = minQual;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (argc > minArg) {
shun-iwasawa 82a8f5
    for (i = minArg; i < argc; i++) {
shun-iwasawa 82a8f5
      if (!strcasecmp(argv[i], "-tile")) {
shun-iwasawa 82a8f5
        doTile = 1;  xformOpt |= TJXOPT_CROP;
shun-iwasawa 82a8f5
      } else if (!strcasecmp(argv[i], "-fastupsample")) {
shun-iwasawa 82a8f5
        printf("Using fast upsampling code\n\n");
shun-iwasawa 82a8f5
        flags |= TJFLAG_FASTUPSAMPLE;
shun-iwasawa 82a8f5
      } else if (!strcasecmp(argv[i], "-fastdct")) {
shun-iwasawa 82a8f5
        printf("Using fastest DCT/IDCT algorithm\n\n");
shun-iwasawa 82a8f5
        flags |= TJFLAG_FASTDCT;
shun-iwasawa 82a8f5
      } else if (!strcasecmp(argv[i], "-accuratedct")) {
shun-iwasawa 82a8f5
        printf("Using most accurate DCT/IDCT algorithm\n\n");
shun-iwasawa 82a8f5
        flags |= TJFLAG_ACCURATEDCT;
shun-iwasawa 82a8f5
      } else if (!strcasecmp(argv[i], "-progressive")) {
shun-iwasawa 82a8f5
        printf("Using progressive entropy coding\n\n");
shun-iwasawa 82a8f5
        flags |= TJFLAG_PROGRESSIVE;
shun-iwasawa 82a8f5
      } else if (!strcasecmp(argv[i], "-rgb"))
shun-iwasawa 82a8f5
        pf = TJPF_RGB;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-rgbx"))
shun-iwasawa 82a8f5
        pf = TJPF_RGBX;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-bgr"))
shun-iwasawa 82a8f5
        pf = TJPF_BGR;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-bgrx"))
shun-iwasawa 82a8f5
        pf = TJPF_BGRX;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-xbgr"))
shun-iwasawa 82a8f5
        pf = TJPF_XBGR;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-xrgb"))
shun-iwasawa 82a8f5
        pf = TJPF_XRGB;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-cmyk"))
shun-iwasawa 82a8f5
        pf = TJPF_CMYK;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-bottomup"))
shun-iwasawa 82a8f5
        flags |= TJFLAG_BOTTOMUP;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-quiet"))
shun-iwasawa 82a8f5
        quiet = 1;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-qq"))
shun-iwasawa 82a8f5
        quiet = 2;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-scale") && i < argc - 1) {
shun-iwasawa 82a8f5
        int temp1 = 0, temp2 = 0, match = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        if (sscanf(argv[++i], "%d/%d", &temp1, &temp2) == 2) {
shun-iwasawa 82a8f5
          for (j = 0; j < nsf; j++) {
shun-iwasawa 82a8f5
            if ((double)temp1 / (double)temp2 ==
shun-iwasawa 82a8f5
                (double)scalingFactors[j].num /
shun-iwasawa 82a8f5
                (double)scalingFactors[j].denom) {
shun-iwasawa 82a8f5
              sf = scalingFactors[j];
shun-iwasawa 82a8f5
              match = 1;  break;
shun-iwasawa 82a8f5
            }
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
          if (!match) usage(argv[0]);
shun-iwasawa 82a8f5
        } else usage(argv[0]);
shun-iwasawa 82a8f5
      } else if (!strcasecmp(argv[i], "-hflip"))
shun-iwasawa 82a8f5
        xformOp = TJXOP_HFLIP;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-vflip"))
shun-iwasawa 82a8f5
        xformOp = TJXOP_VFLIP;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-transpose"))
shun-iwasawa 82a8f5
        xformOp = TJXOP_TRANSPOSE;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-transverse"))
shun-iwasawa 82a8f5
        xformOp = TJXOP_TRANSVERSE;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-rot90"))
shun-iwasawa 82a8f5
        xformOp = TJXOP_ROT90;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-rot180"))
shun-iwasawa 82a8f5
        xformOp = TJXOP_ROT180;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-rot270"))
shun-iwasawa 82a8f5
        xformOp = TJXOP_ROT270;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-grayscale"))
shun-iwasawa 82a8f5
        xformOpt |= TJXOPT_GRAY;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-custom"))
shun-iwasawa 82a8f5
        customFilter = dummyDCTFilter;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-nooutput"))
shun-iwasawa 82a8f5
        xformOpt |= TJXOPT_NOOUTPUT;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-copynone"))
shun-iwasawa 82a8f5
        xformOpt |= TJXOPT_COPYNONE;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-benchtime") && i < argc - 1) {
shun-iwasawa 82a8f5
        double tempd = atof(argv[++i]);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        if (tempd > 0.0) benchTime = tempd;
shun-iwasawa 82a8f5
        else usage(argv[0]);
shun-iwasawa 82a8f5
      } else if (!strcasecmp(argv[i], "-warmup") && i < argc - 1) {
shun-iwasawa 82a8f5
        double tempd = atof(argv[++i]);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        if (tempd >= 0.0) warmup = tempd;
shun-iwasawa 82a8f5
        else usage(argv[0]);
shun-iwasawa 82a8f5
        printf("Warmup time = %.1f seconds\n\n", warmup);
shun-iwasawa 82a8f5
      } else if (!strcasecmp(argv[i], "-alloc"))
shun-iwasawa 82a8f5
        flags &= (~TJFLAG_NOREALLOC);
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-bmp"))
shun-iwasawa 82a8f5
        ext = "bmp";
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-yuv")) {
shun-iwasawa 82a8f5
        printf("Testing YUV planar encoding/decoding\n\n");
shun-iwasawa 82a8f5
        doYUV = 1;
shun-iwasawa 82a8f5
      } else if (!strcasecmp(argv[i], "-yuvpad") && i < argc - 1) {
shun-iwasawa 82a8f5
        int tempi = atoi(argv[++i]);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        if (tempi >= 1) yuvPad = tempi;
shun-iwasawa 82a8f5
      } else if (!strcasecmp(argv[i], "-subsamp") && i < argc - 1) {
shun-iwasawa 82a8f5
        i++;
shun-iwasawa 82a8f5
        if (toupper(argv[i][0]) == 'G') subsamp = TJSAMP_GRAY;
shun-iwasawa 82a8f5
        else {
shun-iwasawa 82a8f5
          int tempi = atoi(argv[i]);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
          switch (tempi) {
shun-iwasawa 82a8f5
          case 444:  subsamp = TJSAMP_444;  break;
shun-iwasawa 82a8f5
          case 422:  subsamp = TJSAMP_422;  break;
shun-iwasawa 82a8f5
          case 440:  subsamp = TJSAMP_440;  break;
shun-iwasawa 82a8f5
          case 420:  subsamp = TJSAMP_420;  break;
shun-iwasawa 82a8f5
          case 411:  subsamp = TJSAMP_411;  break;
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      } else if (!strcasecmp(argv[i], "-componly"))
shun-iwasawa 82a8f5
        compOnly = 1;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-nowrite"))
shun-iwasawa 82a8f5
        doWrite = 0;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-stoponwarning"))
shun-iwasawa 82a8f5
        flags |= TJFLAG_STOPONWARNING;
shun-iwasawa 82a8f5
      else usage(argv[0]);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((sf.num != 1 || sf.denom != 1) && doTile) {
shun-iwasawa 82a8f5
    printf("Disabling tiled compression/decompression tests, because those tests do not\n");
shun-iwasawa 82a8f5
    printf("work when scaled decompression is enabled.\n");
shun-iwasawa 82a8f5
    doTile = 0;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((flags & TJFLAG_NOREALLOC) == 0 && doTile) {
shun-iwasawa 82a8f5
    printf("Disabling tiled compression/decompression tests, because those tests do not\n");
shun-iwasawa 82a8f5
    printf("work when dynamic JPEG buffer allocation is enabled.\n\n");
shun-iwasawa 82a8f5
    doTile = 0;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!decompOnly) {
shun-iwasawa 82a8f5
    if ((srcBuf = tjLoadImage(argv[1], &w, 1, &h, &pf, flags)) == NULL)
shun-iwasawa 82a8f5
      THROW_TJG("loading bitmap");
shun-iwasawa 82a8f5
    temp = strrchr(argv[1], '.');
shun-iwasawa 82a8f5
    if (temp != NULL) *temp = '\0';
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (quiet == 1 && !decompOnly) {
shun-iwasawa 82a8f5
    printf("All performance values in Mpixels/sec\n\n");
shun-iwasawa 82a8f5
    printf("Bitmap     JPEG     JPEG  %s  %s   ",
shun-iwasawa 82a8f5
           doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
shun-iwasawa 82a8f5
    if (doYUV) printf("Encode  ");
shun-iwasawa 82a8f5
    printf("Comp    Comp    Decomp  ");
shun-iwasawa 82a8f5
    if (doYUV) printf("Decode");
shun-iwasawa 82a8f5
    printf("\n");
shun-iwasawa 82a8f5
    printf("Format     Subsamp  Qual  Width  Height  ");
shun-iwasawa 82a8f5
    if (doYUV) printf("Perf    ");
shun-iwasawa 82a8f5
    printf("Perf    Ratio   Perf    ");
shun-iwasawa 82a8f5
    if (doYUV) printf("Perf");
shun-iwasawa 82a8f5
    printf("\n\n");
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (decompOnly) {
shun-iwasawa 82a8f5
    decompTest(argv[1]);
shun-iwasawa 82a8f5
    printf("\n");
shun-iwasawa 82a8f5
    goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (subsamp >= 0 && subsamp < TJ_NUMSAMP) {
shun-iwasawa 82a8f5
    for (i = maxQual; i >= minQual; i--)
shun-iwasawa 82a8f5
      fullTest(srcBuf, w, h, subsamp, i, argv[1]);
shun-iwasawa 82a8f5
    printf("\n");
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    if (pf != TJPF_CMYK) {
shun-iwasawa 82a8f5
      for (i = maxQual; i >= minQual; i--)
shun-iwasawa 82a8f5
        fullTest(srcBuf, w, h, TJSAMP_GRAY, i, argv[1]);
shun-iwasawa 82a8f5
      printf("\n");
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    for (i = maxQual; i >= minQual; i--)
shun-iwasawa 82a8f5
      fullTest(srcBuf, w, h, TJSAMP_420, i, argv[1]);
shun-iwasawa 82a8f5
    printf("\n");
shun-iwasawa 82a8f5
    for (i = maxQual; i >= minQual; i--)
shun-iwasawa 82a8f5
      fullTest(srcBuf, w, h, TJSAMP_422, i, argv[1]);
shun-iwasawa 82a8f5
    printf("\n");
shun-iwasawa 82a8f5
    for (i = maxQual; i >= minQual; i--)
shun-iwasawa 82a8f5
      fullTest(srcBuf, w, h, TJSAMP_444, i, argv[1]);
shun-iwasawa 82a8f5
    printf("\n");
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  tjFree(srcBuf);
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}