shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Copyright (C)2009-2014, 2017-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
/*
shun-iwasawa 82a8f5
 * This program tests the various code paths in the TurboJPEG C Wrapper
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 <errno.h></errno.h>
shun-iwasawa 82a8f5
#include "tjutil.h"
shun-iwasawa 82a8f5
#include "turbojpeg.h"
shun-iwasawa 82a8f5
#include "md5/md5.h"
shun-iwasawa 82a8f5
#include "cmyk.h"
shun-iwasawa 82a8f5
#ifdef _WIN32
shun-iwasawa 82a8f5
#include <time.h></time.h>
shun-iwasawa 82a8f5
#define random()  rand()
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
#include <unistd.h></unistd.h>
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void usage(char *progName)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  printf("\nUSAGE: %s [options]\n\n", progName);
shun-iwasawa 82a8f5
  printf("Options:\n");
shun-iwasawa 82a8f5
  printf("-yuv = test YUV encoding/decoding support\n");
shun-iwasawa 82a8f5
  printf("-noyuvpad = do not pad each line of each Y, U, and V plane to the nearest\n");
shun-iwasawa 82a8f5
  printf("            4-byte boundary\n");
shun-iwasawa 82a8f5
  printf("-alloc = test automatic buffer allocation\n");
shun-iwasawa 82a8f5
  printf("-bmp = tjLoadImage()/tjSaveImage() unit test\n\n");
shun-iwasawa 82a8f5
  exit(1);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define THROW_TJ() { \
shun-iwasawa 82a8f5
  printf("TurboJPEG ERROR:\n%s\n", tjGetErrorStr()); \
shun-iwasawa 82a8f5
  BAILOUT() \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
#define TRY_TJ(f) { if ((f) == -1) THROW_TJ(); }
shun-iwasawa 82a8f5
#define THROW(m) { printf("ERROR: %s\n", m);  BAILOUT() }
shun-iwasawa 82a8f5
#define THROW_MD5(filename, md5sum, ref) { \
shun-iwasawa 82a8f5
  printf("\n%s has an MD5 sum of %s.\n   Should be %s.\n", filename, md5sum, \
shun-iwasawa 82a8f5
         ref); \
shun-iwasawa 82a8f5
  BAILOUT() \
shun-iwasawa 82a8f5
}
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 *subName[TJ_NUMSAMP] = {
shun-iwasawa 82a8f5
  "444", "422", "420", "GRAY", "440", "411"
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
const char *pixFormatStr[TJ_NUMPF] = {
shun-iwasawa 82a8f5
  "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale",
shun-iwasawa 82a8f5
  "RGBA", "BGRA", "ABGR", "ARGB", "CMYK"
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
const int _3byteFormats[] = { TJPF_RGB, TJPF_BGR };
shun-iwasawa 82a8f5
const int _4byteFormats[] = {
shun-iwasawa 82a8f5
  TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB, TJPF_CMYK
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
const int _onlyGray[] = { TJPF_GRAY };
shun-iwasawa 82a8f5
const int _onlyRGB[] = { TJPF_RGB };
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
int doYUV = 0, alloc = 0, pad = 4;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
int exitStatus = 0;
shun-iwasawa 82a8f5
#define BAILOUT() { exitStatus = -1;  goto bailout; }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void initBuf(unsigned char *buf, int w, int h, int pf, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int roffset = tjRedOffset[pf];
shun-iwasawa 82a8f5
  int goffset = tjGreenOffset[pf];
shun-iwasawa 82a8f5
  int boffset = tjBlueOffset[pf];
shun-iwasawa 82a8f5
  int ps = tjPixelSize[pf];
shun-iwasawa 82a8f5
  int index, row, col, halfway = 16;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pf == TJPF_GRAY) {
shun-iwasawa 82a8f5
    memset(buf, 0, w * h * ps);
shun-iwasawa 82a8f5
    for (row = 0; row < h; row++) {
shun-iwasawa 82a8f5
      for (col = 0; col < w; col++) {
shun-iwasawa 82a8f5
        if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col;
shun-iwasawa 82a8f5
        else index = row * w + col;
shun-iwasawa 82a8f5
        if (((row / 8) + (col / 8)) % 2 == 0)
shun-iwasawa 82a8f5
          buf[index] = (row < halfway) ? 255 : 0;
shun-iwasawa 82a8f5
        else buf[index] = (row < halfway) ? 76 : 226;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else if (pf == TJPF_CMYK) {
shun-iwasawa 82a8f5
    memset(buf, 255, w * h * ps);
shun-iwasawa 82a8f5
    for (row = 0; row < h; row++) {
shun-iwasawa 82a8f5
      for (col = 0; col < w; col++) {
shun-iwasawa 82a8f5
        if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col;
shun-iwasawa 82a8f5
        else index = row * w + col;
shun-iwasawa 82a8f5
        if (((row / 8) + (col / 8)) % 2 == 0) {
shun-iwasawa 82a8f5
          if (row >= halfway) buf[index * ps + 3] = 0;
shun-iwasawa 82a8f5
        } else {
shun-iwasawa 82a8f5
          buf[index * ps + 2] = 0;
shun-iwasawa 82a8f5
          if (row < halfway) buf[index * ps + 1] = 0;
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    memset(buf, 0, w * h * ps);
shun-iwasawa 82a8f5
    for (row = 0; row < h; row++) {
shun-iwasawa 82a8f5
      for (col = 0; col < w; col++) {
shun-iwasawa 82a8f5
        if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col;
shun-iwasawa 82a8f5
        else index = row * w + col;
shun-iwasawa 82a8f5
        if (((row / 8) + (col / 8)) % 2 == 0) {
shun-iwasawa 82a8f5
          if (row < halfway) {
shun-iwasawa 82a8f5
            buf[index * ps + roffset] = 255;
shun-iwasawa 82a8f5
            buf[index * ps + goffset] = 255;
shun-iwasawa 82a8f5
            buf[index * ps + boffset] = 255;
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
        } else {
shun-iwasawa 82a8f5
          buf[index * ps + roffset] = 255;
shun-iwasawa 82a8f5
          if (row >= halfway) buf[index * ps + goffset] = 255;
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define CHECKVAL(v, cv) { \
shun-iwasawa 82a8f5
  if (v < cv - 1 || v > cv + 1) { \
shun-iwasawa 82a8f5
    printf("\nComp. %s at %d,%d should be %d, not %d\n", #v, row, col, cv, \
shun-iwasawa 82a8f5
           v); \
shun-iwasawa 82a8f5
    retval = 0;  exitStatus = -1;  goto bailout; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define CHECKVAL0(v) { \
shun-iwasawa 82a8f5
  if (v > 1) { \
shun-iwasawa 82a8f5
    printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, row, col, v); \
shun-iwasawa 82a8f5
    retval = 0;  exitStatus = -1;  goto bailout; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define CHECKVAL255(v) { \
shun-iwasawa 82a8f5
  if (v < 254) { \
shun-iwasawa 82a8f5
    printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, row, col, v); \
shun-iwasawa 82a8f5
    retval = 0;  exitStatus = -1;  goto bailout; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int checkBuf(unsigned char *buf, int w, int h, int pf, int subsamp,
shun-iwasawa 82a8f5
                    tjscalingfactor sf, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int roffset = tjRedOffset[pf];
shun-iwasawa 82a8f5
  int goffset = tjGreenOffset[pf];
shun-iwasawa 82a8f5
  int boffset = tjBlueOffset[pf];
shun-iwasawa 82a8f5
  int aoffset = tjAlphaOffset[pf];
shun-iwasawa 82a8f5
  int ps = tjPixelSize[pf];
shun-iwasawa 82a8f5
  int index, row, col, retval = 1;
shun-iwasawa 82a8f5
  int halfway = 16 * sf.num / sf.denom;
shun-iwasawa 82a8f5
  int blocksize = 8 * sf.num / sf.denom;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pf == TJPF_GRAY) roffset = goffset = boffset = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pf == TJPF_CMYK) {
shun-iwasawa 82a8f5
    for (row = 0; row < h; row++) {
shun-iwasawa 82a8f5
      for (col = 0; col < w; col++) {
shun-iwasawa 82a8f5
        unsigned char c, m, y, k;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col;
shun-iwasawa 82a8f5
        else index = row * w + col;
shun-iwasawa 82a8f5
        c = buf[index * ps];
shun-iwasawa 82a8f5
        m = buf[index * ps + 1];
shun-iwasawa 82a8f5
        y = buf[index * ps + 2];
shun-iwasawa 82a8f5
        k = buf[index * ps + 3];
shun-iwasawa 82a8f5
        if (((row / blocksize) + (col / blocksize)) % 2 == 0) {
shun-iwasawa 82a8f5
          CHECKVAL255(c);  CHECKVAL255(m);  CHECKVAL255(y);
shun-iwasawa 82a8f5
          if (row < halfway) CHECKVAL255(k)
shun-iwasawa 82a8f5
          else CHECKVAL0(k)
shun-iwasawa 82a8f5
        } else {
shun-iwasawa 82a8f5
          CHECKVAL255(c);  CHECKVAL0(y);  CHECKVAL255(k);
shun-iwasawa 82a8f5
          if (row < halfway) CHECKVAL0(m)
shun-iwasawa 82a8f5
          else CHECKVAL255(m)
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    return 1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (row = 0; row < h; row++) {
shun-iwasawa 82a8f5
    for (col = 0; col < w; col++) {
shun-iwasawa 82a8f5
      unsigned char r, g, b, a;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col;
shun-iwasawa 82a8f5
      else index = row * w + col;
shun-iwasawa 82a8f5
      r = buf[index * ps + roffset];
shun-iwasawa 82a8f5
      g = buf[index * ps + goffset];
shun-iwasawa 82a8f5
      b = buf[index * ps + boffset];
shun-iwasawa 82a8f5
      a = aoffset >= 0 ? buf[index * ps + aoffset] : 0xFF;
shun-iwasawa 82a8f5
      if (((row / blocksize) + (col / blocksize)) % 2 == 0) {
shun-iwasawa 82a8f5
        if (row < halfway) {
shun-iwasawa 82a8f5
          CHECKVAL255(r);  CHECKVAL255(g);  CHECKVAL255(b);
shun-iwasawa 82a8f5
        } else {
shun-iwasawa 82a8f5
          CHECKVAL0(r);  CHECKVAL0(g);  CHECKVAL0(b);
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      } else {
shun-iwasawa 82a8f5
        if (subsamp == TJSAMP_GRAY) {
shun-iwasawa 82a8f5
          if (row < halfway) {
shun-iwasawa 82a8f5
            CHECKVAL(r, 76);  CHECKVAL(g, 76);  CHECKVAL(b, 76);
shun-iwasawa 82a8f5
          } else {
shun-iwasawa 82a8f5
            CHECKVAL(r, 226);  CHECKVAL(g, 226);  CHECKVAL(b, 226);
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
        } else {
shun-iwasawa 82a8f5
          if (row < halfway) {
shun-iwasawa 82a8f5
            CHECKVAL255(r);  CHECKVAL0(g);  CHECKVAL0(b);
shun-iwasawa 82a8f5
          } else {
shun-iwasawa 82a8f5
            CHECKVAL255(r);  CHECKVAL255(g);  CHECKVAL0(b);
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      CHECKVAL255(a);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (retval == 0) {
shun-iwasawa 82a8f5
    for (row = 0; row < h; row++) {
shun-iwasawa 82a8f5
      for (col = 0; col < w; col++) {
shun-iwasawa 82a8f5
        if (pf == TJPF_CMYK)
shun-iwasawa 82a8f5
          printf("%.3d/%.3d/%.3d/%.3d ", buf[(row * w + col) * ps],
shun-iwasawa 82a8f5
                 buf[(row * w + col) * ps + 1], buf[(row * w + col) * ps + 2],
shun-iwasawa 82a8f5
                 buf[(row * w + col) * ps + 3]);
shun-iwasawa 82a8f5
        else
shun-iwasawa 82a8f5
          printf("%.3d/%.3d/%.3d ", buf[(row * w + col) * ps + roffset],
shun-iwasawa 82a8f5
                 buf[(row * w + col) * ps + goffset],
shun-iwasawa 82a8f5
                 buf[(row * w + col) * ps + boffset]);
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      printf("\n");
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PAD(v, p)  ((v + (p) - 1) & (~((p) - 1)))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int checkBufYUV(unsigned char *buf, int w, int h, int subsamp,
shun-iwasawa 82a8f5
                       tjscalingfactor sf)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int row, col;
shun-iwasawa 82a8f5
  int hsf = tjMCUWidth[subsamp] / 8, vsf = tjMCUHeight[subsamp] / 8;
shun-iwasawa 82a8f5
  int pw = PAD(w, hsf), ph = PAD(h, vsf);
shun-iwasawa 82a8f5
  int cw = pw / hsf, ch = ph / vsf;
shun-iwasawa 82a8f5
  int ypitch = PAD(pw, pad), uvpitch = PAD(cw, pad);
shun-iwasawa 82a8f5
  int retval = 1;
shun-iwasawa 82a8f5
  int halfway = 16 * sf.num / sf.denom;
shun-iwasawa 82a8f5
  int blocksize = 8 * sf.num / sf.denom;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (row = 0; row < ph; row++) {
shun-iwasawa 82a8f5
    for (col = 0; col < pw; col++) {
shun-iwasawa 82a8f5
      unsigned char y = buf[ypitch * row + col];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      if (((row / blocksize) + (col / blocksize)) % 2 == 0) {
shun-iwasawa 82a8f5
        if (row < halfway) CHECKVAL255(y)
shun-iwasawa 82a8f5
        else CHECKVAL0(y);
shun-iwasawa 82a8f5
      } else {
shun-iwasawa 82a8f5
        if (row < halfway) CHECKVAL(y, 76)
shun-iwasawa 82a8f5
        else CHECKVAL(y, 226);
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (subsamp != TJSAMP_GRAY) {
shun-iwasawa 82a8f5
    halfway = 16 / vsf * sf.num / sf.denom;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    for (row = 0; row < ch; row++) {
shun-iwasawa 82a8f5
      for (col = 0; col < cw; col++) {
shun-iwasawa 82a8f5
        unsigned char u = buf[ypitch * ph + (uvpitch * row + col)],
shun-iwasawa 82a8f5
          v = buf[ypitch * ph + uvpitch * ch + (uvpitch * row + col)];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        if (((row * vsf / blocksize) + (col * hsf / blocksize)) % 2 == 0) {
shun-iwasawa 82a8f5
          CHECKVAL(u, 128);  CHECKVAL(v, 128);
shun-iwasawa 82a8f5
        } else {
shun-iwasawa 82a8f5
          if (row < halfway) {
shun-iwasawa 82a8f5
            CHECKVAL(u, 85);  CHECKVAL255(v);
shun-iwasawa 82a8f5
          } else {
shun-iwasawa 82a8f5
            CHECKVAL0(u);  CHECKVAL(v, 149);
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (retval == 0) {
shun-iwasawa 82a8f5
    for (row = 0; row < ph; row++) {
shun-iwasawa 82a8f5
      for (col = 0; col < pw; col++)
shun-iwasawa 82a8f5
        printf("%.3d ", buf[ypitch * row + col]);
shun-iwasawa 82a8f5
      printf("\n");
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    printf("\n");
shun-iwasawa 82a8f5
    for (row = 0; row < ch; row++) {
shun-iwasawa 82a8f5
      for (col = 0; col < cw; col++)
shun-iwasawa 82a8f5
        printf("%.3d ", buf[ypitch * ph + (uvpitch * row + col)]);
shun-iwasawa 82a8f5
      printf("\n");
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    printf("\n");
shun-iwasawa 82a8f5
    for (row = 0; row < ch; row++) {
shun-iwasawa 82a8f5
      for (col = 0; col < cw; col++)
shun-iwasawa 82a8f5
        printf("%.3d ",
shun-iwasawa 82a8f5
               buf[ypitch * ph + uvpitch * ch + (uvpitch * row + col)]);
shun-iwasawa 82a8f5
      printf("\n");
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
static void writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize,
shun-iwasawa 82a8f5
                      char *filename)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  FILE *file = fopen(filename, "wb");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!file || fwrite(jpegBuf, jpegSize, 1, file) != 1) {
shun-iwasawa 82a8f5
    printf("ERROR: Could not write to %s.\n%s\n", filename, strerror(errno));
shun-iwasawa 82a8f5
    BAILOUT()
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (file) fclose(file);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void compTest(tjhandle handle, unsigned char **dstBuf,
shun-iwasawa 82a8f5
                     unsigned long *dstSize, int w, int h, int pf,
shun-iwasawa 82a8f5
                     char *basename, int subsamp, int jpegQual, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  char tempStr[1024];
shun-iwasawa 82a8f5
  unsigned char *srcBuf = NULL, *yuvBuf = NULL;
shun-iwasawa 82a8f5
  const char *pfStr = pixFormatStr[pf];
shun-iwasawa 82a8f5
  const char *buStrLong =
shun-iwasawa 82a8f5
    (flags & TJFLAG_BOTTOMUP) ? "Bottom-Up" : "Top-Down ";
shun-iwasawa 82a8f5
  const char *buStr = (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD";
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((srcBuf = (unsigned char *)malloc(w * h * tjPixelSize[pf])) == NULL)
shun-iwasawa 82a8f5
    THROW("Memory allocation failure");
shun-iwasawa 82a8f5
  initBuf(srcBuf, w, h, pf, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (*dstBuf && *dstSize > 0) memset(*dstBuf, 0, *dstSize);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!alloc) flags |= TJFLAG_NOREALLOC;
shun-iwasawa 82a8f5
  if (doYUV) {
shun-iwasawa 82a8f5
    unsigned long yuvSize = tjBufSizeYUV2(w, pad, h, subsamp);
shun-iwasawa 82a8f5
    tjscalingfactor sf = { 1, 1 };
shun-iwasawa 82a8f5
    tjhandle handle2 = tjInitCompress();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (!handle2) THROW_TJ();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
shun-iwasawa 82a8f5
      THROW("Memory allocation failure");
shun-iwasawa 82a8f5
    memset(yuvBuf, 0, yuvSize);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    printf("%s %s -> YUV %s ... ", pfStr, buStrLong, subNameLong[subsamp]);
shun-iwasawa 82a8f5
    TRY_TJ(tjEncodeYUV3(handle2, srcBuf, w, 0, h, pf, yuvBuf, pad, subsamp,
shun-iwasawa 82a8f5
                        flags));
shun-iwasawa 82a8f5
    tjDestroy(handle2);
shun-iwasawa 82a8f5
    if (checkBufYUV(yuvBuf, w, h, subsamp, sf)) printf("Passed.\n");
shun-iwasawa 82a8f5
    else printf("FAILED!\n");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    printf("YUV %s %s -> JPEG Q%d ... ", subNameLong[subsamp], buStrLong,
shun-iwasawa 82a8f5
           jpegQual);
shun-iwasawa 82a8f5
    TRY_TJ(tjCompressFromYUV(handle, yuvBuf, w, pad, h, subsamp, dstBuf,
shun-iwasawa 82a8f5
                             dstSize, jpegQual, flags));
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    printf("%s %s -> %s Q%d ... ", pfStr, buStrLong, subNameLong[subsamp],
shun-iwasawa 82a8f5
           jpegQual);
shun-iwasawa 82a8f5
    TRY_TJ(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp,
shun-iwasawa 82a8f5
                       jpegQual, flags));
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, pfStr, buStr,
shun-iwasawa 82a8f5
           subName[subsamp], jpegQual);
shun-iwasawa 82a8f5
  writeJPEG(*dstBuf, *dstSize, tempStr);
shun-iwasawa 82a8f5
  printf("Done.\n  Result in %s\n", tempStr);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  free(yuvBuf);
shun-iwasawa 82a8f5
  free(srcBuf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void _decompTest(tjhandle handle, unsigned char *jpegBuf,
shun-iwasawa 82a8f5
                        unsigned long jpegSize, int w, int h, int pf,
shun-iwasawa 82a8f5
                        char *basename, int subsamp, int flags,
shun-iwasawa 82a8f5
                        tjscalingfactor sf)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  unsigned char *dstBuf = NULL, *yuvBuf = NULL;
shun-iwasawa 82a8f5
  int _hdrw = 0, _hdrh = 0, _hdrsubsamp = -1;
shun-iwasawa 82a8f5
  int scaledWidth = TJSCALED(w, sf);
shun-iwasawa 82a8f5
  int scaledHeight = TJSCALED(h, sf);
shun-iwasawa 82a8f5
  unsigned long dstSize = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  TRY_TJ(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh,
shun-iwasawa 82a8f5
                             &_hdrsubsamp));
shun-iwasawa 82a8f5
  if (_hdrw != w || _hdrh != h || _hdrsubsamp != subsamp)
shun-iwasawa 82a8f5
    THROW("Incorrect JPEG header");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  dstSize = scaledWidth * scaledHeight * tjPixelSize[pf];
shun-iwasawa 82a8f5
  if ((dstBuf = (unsigned char *)malloc(dstSize)) == NULL)
shun-iwasawa 82a8f5
    THROW("Memory allocation failure");
shun-iwasawa 82a8f5
  memset(dstBuf, 0, dstSize);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (doYUV) {
shun-iwasawa 82a8f5
    unsigned long yuvSize = tjBufSizeYUV2(scaledWidth, pad, scaledHeight,
shun-iwasawa 82a8f5
                                          subsamp);
shun-iwasawa 82a8f5
    tjhandle handle2 = tjInitDecompress();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (!handle2) THROW_TJ();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
shun-iwasawa 82a8f5
      THROW("Memory allocation failure");
shun-iwasawa 82a8f5
    memset(yuvBuf, 0, yuvSize);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    printf("JPEG -> YUV %s ", subNameLong[subsamp]);
shun-iwasawa 82a8f5
    if (sf.num != 1 || sf.denom != 1)
shun-iwasawa 82a8f5
      printf("%d/%d ... ", sf.num, sf.denom);
shun-iwasawa 82a8f5
    else printf("... ");
shun-iwasawa 82a8f5
    TRY_TJ(tjDecompressToYUV2(handle, jpegBuf, jpegSize, yuvBuf, scaledWidth,
shun-iwasawa 82a8f5
                              pad, scaledHeight, flags));
shun-iwasawa 82a8f5
    if (checkBufYUV(yuvBuf, scaledWidth, scaledHeight, subsamp, sf))
shun-iwasawa 82a8f5
      printf("Passed.\n");
shun-iwasawa 82a8f5
    else printf("FAILED!\n");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    printf("YUV %s -> %s %s ... ", subNameLong[subsamp], pixFormatStr[pf],
shun-iwasawa 82a8f5
           (flags & TJFLAG_BOTTOMUP) ? "Bottom-Up" : "Top-Down ");
shun-iwasawa 82a8f5
    TRY_TJ(tjDecodeYUV(handle2, yuvBuf, pad, subsamp, dstBuf, scaledWidth, 0,
shun-iwasawa 82a8f5
                       scaledHeight, pf, flags));
shun-iwasawa 82a8f5
    tjDestroy(handle2);
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    printf("JPEG -> %s %s ", pixFormatStr[pf],
shun-iwasawa 82a8f5
           (flags & TJFLAG_BOTTOMUP) ? "Bottom-Up" : "Top-Down ");
shun-iwasawa 82a8f5
    if (sf.num != 1 || sf.denom != 1)
shun-iwasawa 82a8f5
      printf("%d/%d ... ", sf.num, sf.denom);
shun-iwasawa 82a8f5
    else printf("... ");
shun-iwasawa 82a8f5
    TRY_TJ(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0,
shun-iwasawa 82a8f5
                         scaledHeight, pf, flags));
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags))
shun-iwasawa 82a8f5
    printf("Passed.");
shun-iwasawa 82a8f5
  else printf("FAILED!");
shun-iwasawa 82a8f5
  printf("\n");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  free(yuvBuf);
shun-iwasawa 82a8f5
  free(dstBuf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void decompTest(tjhandle handle, unsigned char *jpegBuf,
shun-iwasawa 82a8f5
                       unsigned long jpegSize, int w, int h, int pf,
shun-iwasawa 82a8f5
                       char *basename, int subsamp, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int i, n = 0;
shun-iwasawa 82a8f5
  tjscalingfactor *sf = tjGetScalingFactors(&n);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!sf || !n) THROW_TJ();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0; i < n; i++) {
shun-iwasawa 82a8f5
    if (subsamp == TJSAMP_444 || subsamp == TJSAMP_GRAY ||
shun-iwasawa 82a8f5
        (subsamp == TJSAMP_411 && sf[i].num == 1 &&
shun-iwasawa 82a8f5
         (sf[i].denom == 2 || sf[i].denom == 1)) ||
shun-iwasawa 82a8f5
        (subsamp != TJSAMP_411 && sf[i].num == 1 &&
shun-iwasawa 82a8f5
         (sf[i].denom == 4 || sf[i].denom == 2 || sf[i].denom == 1)))
shun-iwasawa 82a8f5
      _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp,
shun-iwasawa 82a8f5
                  flags, sf[i]);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void doTest(int w, int h, const int *formats, int nformats, int subsamp,
shun-iwasawa 82a8f5
                   char *basename)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tjhandle chandle = NULL, dhandle = NULL;
shun-iwasawa 82a8f5
  unsigned char *dstBuf = NULL;
shun-iwasawa 82a8f5
  unsigned long size = 0;
shun-iwasawa 82a8f5
  int pfi, pf, i;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!alloc)
shun-iwasawa 82a8f5
    size = tjBufSize(w, h, subsamp);
shun-iwasawa 82a8f5
  if (size != 0)
shun-iwasawa 82a8f5
    if ((dstBuf = (unsigned char *)tjAlloc(size)) == NULL)
shun-iwasawa 82a8f5
      THROW("Memory allocation failure.");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((chandle = tjInitCompress()) == NULL ||
shun-iwasawa 82a8f5
      (dhandle = tjInitDecompress()) == NULL)
shun-iwasawa 82a8f5
    THROW_TJ();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (pfi = 0; pfi < nformats; pfi++) {
shun-iwasawa 82a8f5
    for (i = 0; i < 2; i++) {
shun-iwasawa 82a8f5
      int flags = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      if (subsamp == TJSAMP_422 || subsamp == TJSAMP_420 ||
shun-iwasawa 82a8f5
          subsamp == TJSAMP_440 || subsamp == TJSAMP_411)
shun-iwasawa 82a8f5
        flags |= TJFLAG_FASTUPSAMPLE;
shun-iwasawa 82a8f5
      if (i == 1) flags |= TJFLAG_BOTTOMUP;
shun-iwasawa 82a8f5
      pf = formats[pfi];
shun-iwasawa 82a8f5
      compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100,
shun-iwasawa 82a8f5
               flags);
shun-iwasawa 82a8f5
      decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp, flags);
shun-iwasawa 82a8f5
      if (pf >= TJPF_RGBX && pf <= TJPF_XRGB) {
shun-iwasawa 82a8f5
        printf("\n");
shun-iwasawa 82a8f5
        decompTest(dhandle, dstBuf, size, w, h, pf + (TJPF_RGBA - TJPF_RGBX),
shun-iwasawa 82a8f5
                   basename, subsamp, flags);
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      printf("\n");
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  printf("--------------------\n\n");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  if (chandle) tjDestroy(chandle);
shun-iwasawa 82a8f5
  if (dhandle) tjDestroy(dhandle);
shun-iwasawa 82a8f5
  tjFree(dstBuf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#if SIZEOF_SIZE_T == 8
shun-iwasawa 82a8f5
#define CHECKSIZE(function) { \
shun-iwasawa 82a8f5
  if ((unsigned long long)size < (unsigned long long)0xFFFFFFFF) \
shun-iwasawa 82a8f5
    THROW(#function " overflow"); \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
#define CHECKSIZE(function) { \
shun-iwasawa 82a8f5
  if (size != (unsigned long)(-1) || \
shun-iwasawa 82a8f5
      !strcmp(tjGetErrorStr2(NULL), "No error")) \
shun-iwasawa 82a8f5
    THROW(#function " overflow"); \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void overflowTest(void)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  /* Ensure that the various buffer size functions don't overflow */
shun-iwasawa 82a8f5
  unsigned long size;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  size = tjBufSize(26755, 26755, TJSAMP_444);
shun-iwasawa 82a8f5
  CHECKSIZE(tjBufSize());
shun-iwasawa 82a8f5
  size = TJBUFSIZE(26755, 26755);
shun-iwasawa 82a8f5
  CHECKSIZE(TJBUFSIZE());
shun-iwasawa 82a8f5
  size = tjBufSizeYUV2(37838, 1, 37838, TJSAMP_444);
shun-iwasawa 82a8f5
  CHECKSIZE(tjBufSizeYUV2());
shun-iwasawa 82a8f5
  size = TJBUFSIZEYUV(37838, 37838, TJSAMP_444);
shun-iwasawa 82a8f5
  CHECKSIZE(TJBUFSIZEYUV());
shun-iwasawa 82a8f5
  size = tjBufSizeYUV(37838, 37838, TJSAMP_444);
shun-iwasawa 82a8f5
  CHECKSIZE(tjBufSizeYUV());
shun-iwasawa 82a8f5
  size = tjPlaneSizeYUV(0, 65536, 0, 65536, TJSAMP_444);
shun-iwasawa 82a8f5
  CHECKSIZE(tjPlaneSizeYUV());
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  return;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void bufSizeTest(void)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int w, h, i, subsamp;
shun-iwasawa 82a8f5
  unsigned char *srcBuf = NULL, *dstBuf = NULL;
shun-iwasawa 82a8f5
  tjhandle handle = NULL;
shun-iwasawa 82a8f5
  unsigned long dstSize = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((handle = tjInitCompress()) == NULL) THROW_TJ();
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  printf("Buffer size regression test\n");
shun-iwasawa 82a8f5
  for (subsamp = 0; subsamp < TJ_NUMSAMP; subsamp++) {
shun-iwasawa 82a8f5
    for (w = 1; w < 48; w++) {
shun-iwasawa 82a8f5
      int maxh = (w == 1) ? 2048 : 48;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      for (h = 1; h < maxh; h++) {
shun-iwasawa 82a8f5
        if (h % 100 == 0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h);
shun-iwasawa 82a8f5
        if ((srcBuf = (unsigned char *)malloc(w * h * 4)) == NULL)
shun-iwasawa 82a8f5
          THROW("Memory allocation failure");
shun-iwasawa 82a8f5
        if (!alloc || doYUV) {
shun-iwasawa 82a8f5
          if (doYUV) dstSize = tjBufSizeYUV2(w, pad, h, subsamp);
shun-iwasawa 82a8f5
          else dstSize = tjBufSize(w, h, subsamp);
shun-iwasawa 82a8f5
          if ((dstBuf = (unsigned char *)tjAlloc(dstSize)) == NULL)
shun-iwasawa 82a8f5
            THROW("Memory allocation failure");
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        for (i = 0; i < w * h * 4; i++) {
shun-iwasawa 82a8f5
          if (random() < RAND_MAX / 2) srcBuf[i] = 0;
shun-iwasawa 82a8f5
          else srcBuf[i] = 255;
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        if (doYUV) {
shun-iwasawa 82a8f5
          TRY_TJ(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad,
shun-iwasawa 82a8f5
                              subsamp, 0));
shun-iwasawa 82a8f5
        } else {
shun-iwasawa 82a8f5
          TRY_TJ(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf,
shun-iwasawa 82a8f5
                             &dstSize, subsamp, 100,
shun-iwasawa 82a8f5
                             alloc ? 0 : TJFLAG_NOREALLOC));
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
        free(srcBuf);  srcBuf = NULL;
shun-iwasawa 82a8f5
        if (!alloc || doYUV) {
shun-iwasawa 82a8f5
          tjFree(dstBuf);  dstBuf = NULL;
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        if ((srcBuf = (unsigned char *)malloc(h * w * 4)) == NULL)
shun-iwasawa 82a8f5
          THROW("Memory allocation failure");
shun-iwasawa 82a8f5
        if (!alloc || doYUV) {
shun-iwasawa 82a8f5
          if (doYUV) dstSize = tjBufSizeYUV2(h, pad, w, subsamp);
shun-iwasawa 82a8f5
          else dstSize = tjBufSize(h, w, subsamp);
shun-iwasawa 82a8f5
          if ((dstBuf = (unsigned char *)tjAlloc(dstSize)) == NULL)
shun-iwasawa 82a8f5
            THROW("Memory allocation failure");
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        for (i = 0; i < h * w * 4; i++) {
shun-iwasawa 82a8f5
          if (random() < RAND_MAX / 2) srcBuf[i] = 0;
shun-iwasawa 82a8f5
          else srcBuf[i] = 255;
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        if (doYUV) {
shun-iwasawa 82a8f5
          TRY_TJ(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad,
shun-iwasawa 82a8f5
                              subsamp, 0));
shun-iwasawa 82a8f5
        } else {
shun-iwasawa 82a8f5
          TRY_TJ(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf,
shun-iwasawa 82a8f5
                             &dstSize, subsamp, 100,
shun-iwasawa 82a8f5
                             alloc ? 0 : TJFLAG_NOREALLOC));
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
        free(srcBuf);  srcBuf = NULL;
shun-iwasawa 82a8f5
        if (!alloc || doYUV) {
shun-iwasawa 82a8f5
          tjFree(dstBuf);  dstBuf = NULL;
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  printf("Done.      \n");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  free(srcBuf);
shun-iwasawa 82a8f5
  tjFree(dstBuf);
shun-iwasawa 82a8f5
  if (handle) tjDestroy(handle);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static void initBitmap(unsigned char *buf, int width, int pitch, int height,
shun-iwasawa 82a8f5
                       int pf, int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int roffset = tjRedOffset[pf];
shun-iwasawa 82a8f5
  int goffset = tjGreenOffset[pf];
shun-iwasawa 82a8f5
  int boffset = tjBlueOffset[pf];
shun-iwasawa 82a8f5
  int ps = tjPixelSize[pf];
shun-iwasawa 82a8f5
  int i, j;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (j = 0; j < height; j++) {
shun-iwasawa 82a8f5
    int row = (flags & TJFLAG_BOTTOMUP) ? height - j - 1 : j;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    for (i = 0; i < width; i++) {
shun-iwasawa 82a8f5
      unsigned char r = (i * 256 / width) % 256;
shun-iwasawa 82a8f5
      unsigned char g = (j * 256 / height) % 256;
shun-iwasawa 82a8f5
      unsigned char b = (j * 256 / height + i * 256 / width) % 256;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      memset(&buf[row * pitch + i * ps], 0, ps);
shun-iwasawa 82a8f5
      if (pf == TJPF_GRAY) buf[row * pitch + i * ps] = b;
shun-iwasawa 82a8f5
      else if (pf == TJPF_CMYK)
shun-iwasawa 82a8f5
        rgb_to_cmyk(r, g, b, &buf[row * pitch + i * ps + 0],
shun-iwasawa 82a8f5
                    &buf[row * pitch + i * ps + 1],
shun-iwasawa 82a8f5
                    &buf[row * pitch + i * ps + 2],
shun-iwasawa 82a8f5
                    &buf[row * pitch + i * ps + 3]);
shun-iwasawa 82a8f5
      else {
shun-iwasawa 82a8f5
        buf[row * pitch + i * ps + roffset] = r;
shun-iwasawa 82a8f5
        buf[row * pitch + i * ps + goffset] = g;
shun-iwasawa 82a8f5
        buf[row * pitch + i * ps + boffset] = b;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int cmpBitmap(unsigned char *buf, int width, int pitch, int height,
shun-iwasawa 82a8f5
                     int pf, int flags, int gray2rgb)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int roffset = tjRedOffset[pf];
shun-iwasawa 82a8f5
  int goffset = tjGreenOffset[pf];
shun-iwasawa 82a8f5
  int boffset = tjBlueOffset[pf];
shun-iwasawa 82a8f5
  int aoffset = tjAlphaOffset[pf];
shun-iwasawa 82a8f5
  int ps = tjPixelSize[pf];
shun-iwasawa 82a8f5
  int i, j;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (j = 0; j < height; j++) {
shun-iwasawa 82a8f5
    int row = (flags & TJFLAG_BOTTOMUP) ? height - j - 1 : j;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    for (i = 0; i < width; i++) {
shun-iwasawa 82a8f5
      unsigned char r = (i * 256 / width) % 256;
shun-iwasawa 82a8f5
      unsigned char g = (j * 256 / height) % 256;
shun-iwasawa 82a8f5
      unsigned char b = (j * 256 / height + i * 256 / width) % 256;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      if (pf == TJPF_GRAY) {
shun-iwasawa 82a8f5
        if (buf[row * pitch + i * ps] != b)
shun-iwasawa 82a8f5
          return 0;
shun-iwasawa 82a8f5
      } else if (pf == TJPF_CMYK) {
shun-iwasawa 82a8f5
        unsigned char rf, gf, bf;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        cmyk_to_rgb(buf[row * pitch + i * ps + 0],
shun-iwasawa 82a8f5
                    buf[row * pitch + i * ps + 1],
shun-iwasawa 82a8f5
                    buf[row * pitch + i * ps + 2],
shun-iwasawa 82a8f5
                    buf[row * pitch + i * ps + 3], &rf, &gf, &bf);
shun-iwasawa 82a8f5
        if (gray2rgb) {
shun-iwasawa 82a8f5
          if (rf != b || gf != b || bf != b)
shun-iwasawa 82a8f5
            return 0;
shun-iwasawa 82a8f5
        } else if (rf != r || gf != g || bf != b) return 0;
shun-iwasawa 82a8f5
      } else {
shun-iwasawa 82a8f5
        if (gray2rgb) {
shun-iwasawa 82a8f5
          if (buf[row * pitch + i * ps + roffset] != b ||
shun-iwasawa 82a8f5
              buf[row * pitch + i * ps + goffset] != b ||
shun-iwasawa 82a8f5
              buf[row * pitch + i * ps + boffset] != b)
shun-iwasawa 82a8f5
            return 0;
shun-iwasawa 82a8f5
        } else if (buf[row * pitch + i * ps + roffset] != r ||
shun-iwasawa 82a8f5
                   buf[row * pitch + i * ps + goffset] != g ||
shun-iwasawa 82a8f5
                   buf[row * pitch + i * ps + boffset] != b)
shun-iwasawa 82a8f5
          return 0;
shun-iwasawa 82a8f5
        if (aoffset >= 0 && buf[row * pitch + i * ps + aoffset] != 0xFF)
shun-iwasawa 82a8f5
          return 0;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int doBmpTest(const char *ext, int width, int align, int height, int pf,
shun-iwasawa 82a8f5
                     int flags)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  char filename[80], *md5sum, md5buf[65];
shun-iwasawa 82a8f5
  int ps = tjPixelSize[pf], pitch = PAD(width * ps, align), loadWidth = 0,
shun-iwasawa 82a8f5
    loadHeight = 0, retval = 0, pixelFormat = pf;
shun-iwasawa 82a8f5
  unsigned char *buf = NULL;
shun-iwasawa 82a8f5
  char *md5ref;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (pf == TJPF_GRAY) {
shun-iwasawa 82a8f5
    md5ref = !strcasecmp(ext, "ppm") ? "112c682e82ce5de1cca089e20d60000b" :
shun-iwasawa 82a8f5
                                       "51976530acf75f02beddf5d21149101d";
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    md5ref = !strcasecmp(ext, "ppm") ? "c0c9f772b464d1896326883a5c79c545" :
shun-iwasawa 82a8f5
                                       "6d659071b9bfcdee2def22cb58ddadca";
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((buf = (unsigned char *)tjAlloc(pitch * height)) == NULL)
shun-iwasawa 82a8f5
    THROW("Could not allocate memory");
shun-iwasawa 82a8f5
  initBitmap(buf, width, pitch, height, pf, flags);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  snprintf(filename, 80, "test_bmp_%s_%d_%s.%s", pixFormatStr[pf], align,
shun-iwasawa 82a8f5
           (flags & TJFLAG_BOTTOMUP) ? "bu" : "td", ext);
shun-iwasawa 82a8f5
  TRY_TJ(tjSaveImage(filename, buf, width, pitch, height, pf, flags));
shun-iwasawa 82a8f5
  md5sum = MD5File(filename, md5buf);
shun-iwasawa 82a8f5
  if (strcasecmp(md5sum, md5ref))
shun-iwasawa 82a8f5
    THROW_MD5(filename, md5sum, md5ref);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  tjFree(buf);  buf = NULL;
shun-iwasawa 82a8f5
  if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf,
shun-iwasawa 82a8f5
                         flags)) == NULL)
shun-iwasawa 82a8f5
    THROW_TJ();
shun-iwasawa 82a8f5
  if (width != loadWidth || height != loadHeight) {
shun-iwasawa 82a8f5
    printf("\n   Image dimensions of %s are bogus\n", filename);
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (!cmpBitmap(buf, width, pitch, height, pf, flags, 0)) {
shun-iwasawa 82a8f5
    printf("\n   Pixel data in %s is bogus\n", filename);
shun-iwasawa 82a8f5
    retval = -1;  goto bailout;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (pf == TJPF_GRAY) {
shun-iwasawa 82a8f5
    tjFree(buf);  buf = NULL;
shun-iwasawa 82a8f5
    pf = TJPF_XBGR;
shun-iwasawa 82a8f5
    if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf,
shun-iwasawa 82a8f5
                           flags)) == NULL)
shun-iwasawa 82a8f5
      THROW_TJ();
shun-iwasawa 82a8f5
    pitch = PAD(width * tjPixelSize[pf], align);
shun-iwasawa 82a8f5
    if (!cmpBitmap(buf, width, pitch, height, pf, flags, 1)) {
shun-iwasawa 82a8f5
      printf("\n   Converting %s to RGB failed\n", filename);
shun-iwasawa 82a8f5
      retval = -1;  goto bailout;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    tjFree(buf);  buf = NULL;
shun-iwasawa 82a8f5
    pf = TJPF_CMYK;
shun-iwasawa 82a8f5
    if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf,
shun-iwasawa 82a8f5
                           flags)) == NULL)
shun-iwasawa 82a8f5
      THROW_TJ();
shun-iwasawa 82a8f5
    pitch = PAD(width * tjPixelSize[pf], align);
shun-iwasawa 82a8f5
    if (!cmpBitmap(buf, width, pitch, height, pf, flags, 1)) {
shun-iwasawa 82a8f5
      printf("\n   Converting %s to CMYK failed\n", filename);
shun-iwasawa 82a8f5
      retval = -1;  goto bailout;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  /* Verify that tjLoadImage() returns the proper "preferred" pixel format for
shun-iwasawa 82a8f5
     the file type. */
shun-iwasawa 82a8f5
  tjFree(buf);  buf = NULL;
shun-iwasawa 82a8f5
  pf = pixelFormat;
shun-iwasawa 82a8f5
  pixelFormat = TJPF_UNKNOWN;
shun-iwasawa 82a8f5
  if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight,
shun-iwasawa 82a8f5
                         &pixelFormat, flags)) == NULL)
shun-iwasawa 82a8f5
    THROW_TJ();
shun-iwasawa 82a8f5
  if ((pf == TJPF_GRAY && pixelFormat != TJPF_GRAY) ||
shun-iwasawa 82a8f5
      (pf != TJPF_GRAY && !strcasecmp(ext, "bmp") &&
shun-iwasawa 82a8f5
       pixelFormat != TJPF_BGR) ||
shun-iwasawa 82a8f5
      (pf != TJPF_GRAY && !strcasecmp(ext, "ppm") &&
shun-iwasawa 82a8f5
       pixelFormat != TJPF_RGB)) {
shun-iwasawa 82a8f5
    printf("\n   tjLoadImage() returned unexpected pixel format: %s\n",
shun-iwasawa 82a8f5
           pixFormatStr[pixelFormat]);
shun-iwasawa 82a8f5
    retval = -1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  unlink(filename);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
bailout:
shun-iwasawa 82a8f5
  tjFree(buf);
shun-iwasawa 82a8f5
  if (exitStatus < 0) return exitStatus;
shun-iwasawa 82a8f5
  return retval;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int bmpTest(void)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int align, width = 35, height = 39, format;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (align = 1; align <= 8; align *= 2) {
shun-iwasawa 82a8f5
    for (format = 0; format < TJ_NUMPF; format++) {
shun-iwasawa 82a8f5
      printf("%s Top-Down BMP (row alignment = %d bytes)  ...  ",
shun-iwasawa 82a8f5
             pixFormatStr[format], align);
shun-iwasawa 82a8f5
      if (doBmpTest("bmp", width, align, height, format, 0) == -1)
shun-iwasawa 82a8f5
        return -1;
shun-iwasawa 82a8f5
      printf("OK.\n");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      printf("%s Top-Down PPM (row alignment = %d bytes)  ...  ",
shun-iwasawa 82a8f5
             pixFormatStr[format], align);
shun-iwasawa 82a8f5
      if (doBmpTest("ppm", width, align, height, format,
shun-iwasawa 82a8f5
                    TJFLAG_BOTTOMUP) == -1)
shun-iwasawa 82a8f5
        return -1;
shun-iwasawa 82a8f5
      printf("OK.\n");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      printf("%s Bottom-Up BMP (row alignment = %d bytes)  ...  ",
shun-iwasawa 82a8f5
             pixFormatStr[format], align);
shun-iwasawa 82a8f5
      if (doBmpTest("bmp", width, align, height, format, 0) == -1)
shun-iwasawa 82a8f5
        return -1;
shun-iwasawa 82a8f5
      printf("OK.\n");
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      printf("%s Bottom-Up PPM (row alignment = %d bytes)  ...  ",
shun-iwasawa 82a8f5
             pixFormatStr[format], align);
shun-iwasawa 82a8f5
      if (doBmpTest("ppm", width, align, height, format,
shun-iwasawa 82a8f5
                    TJFLAG_BOTTOMUP) == -1)
shun-iwasawa 82a8f5
        return -1;
shun-iwasawa 82a8f5
      printf("OK.\n");
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return 0;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
int main(int argc, char *argv[])
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int i, num4bf = 5;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef _WIN32
shun-iwasawa 82a8f5
  srand((unsigned int)time(NULL));
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
  if (argc > 1) {
shun-iwasawa 82a8f5
    for (i = 1; i < argc; i++) {
shun-iwasawa 82a8f5
      if (!strcasecmp(argv[i], "-yuv")) doYUV = 1;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-noyuvpad")) pad = 1;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-alloc")) alloc = 1;
shun-iwasawa 82a8f5
      else if (!strcasecmp(argv[i], "-bmp")) return bmpTest();
shun-iwasawa 82a8f5
      else usage(argv[0]);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (alloc) printf("Testing automatic buffer allocation\n");
shun-iwasawa 82a8f5
  if (doYUV) num4bf = 4;
shun-iwasawa 82a8f5
  overflowTest();
shun-iwasawa 82a8f5
  doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test");
shun-iwasawa 82a8f5
  doTest(39, 41, _4byteFormats, num4bf, TJSAMP_444, "test");
shun-iwasawa 82a8f5
  doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test");
shun-iwasawa 82a8f5
  doTest(35, 39, _4byteFormats, num4bf, TJSAMP_422, "test");
shun-iwasawa 82a8f5
  doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test");
shun-iwasawa 82a8f5
  doTest(41, 35, _4byteFormats, num4bf, TJSAMP_420, "test");
shun-iwasawa 82a8f5
  doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test");
shun-iwasawa 82a8f5
  doTest(39, 41, _4byteFormats, num4bf, TJSAMP_440, "test");
shun-iwasawa 82a8f5
  doTest(41, 35, _3byteFormats, 2, TJSAMP_411, "test");
shun-iwasawa 82a8f5
  doTest(35, 39, _4byteFormats, num4bf, TJSAMP_411, "test");
shun-iwasawa 82a8f5
  doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test");
shun-iwasawa 82a8f5
  doTest(41, 35, _3byteFormats, 2, TJSAMP_GRAY, "test");
shun-iwasawa 82a8f5
  doTest(35, 39, _4byteFormats, 4, TJSAMP_GRAY, "test");
shun-iwasawa 82a8f5
  bufSizeTest();
shun-iwasawa 82a8f5
  if (doYUV) {
shun-iwasawa 82a8f5
    printf("\n--------------------\n\n");
shun-iwasawa 82a8f5
    doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0");
shun-iwasawa 82a8f5
    doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0");
shun-iwasawa 82a8f5
    doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0");
shun-iwasawa 82a8f5
    doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0");
shun-iwasawa 82a8f5
    doTest(48, 48, _onlyRGB, 1, TJSAMP_411, "test_yuv0");
shun-iwasawa 82a8f5
    doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0");
shun-iwasawa 82a8f5
    doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0");
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return exitStatus;
shun-iwasawa 82a8f5
}