static int doBmpTest()

in tjunittest.c [946:1101]


static int doBmpTest(const char *ext, int width, int align, int height, int pf,
                     int bottomUp)
{
  tjhandle handle = NULL;
  char filename[80], *md5sum, md5buf[65];
  int ps = tjPixelSize[pf], pitch = PAD(width * ps, align), loadWidth = 0,
    loadHeight = 0, retval = 0, pixelFormat = pf;
  void *buf = NULL;
  char *md5ref;

  if ((handle = tj3Init(TJINIT_TRANSFORM)) == NULL)
    THROW_TJ(NULL);
  TRY_TJ(handle, tj3Set(handle, TJPARAM_BOTTOMUP, bottomUp));

  if (pf == TJPF_GRAY) {
    if (precision == 8)
      md5ref = !strcasecmp(ext, "ppm") ? "112c682e82ce5de1cca089e20d60000b" :
                                         "51976530acf75f02beddf5d21149101d";
    else if (precision == 12)
      md5ref = "0d1895c7e6f2b2c9af6e821a655c239c";
    else
      md5ref = "64f3320b226ea37fb58080713b4df1b2";
  } else {
    if (precision == 8)
      md5ref = !strcasecmp(ext, "ppm") ? "c0c9f772b464d1896326883a5c79c545" :
                                         "6d659071b9bfcdee2def22cb58ddadca";
    else if (precision == 12)
      md5ref = "2ff5299287017502832c99718450c90a";
    else
      md5ref = "623f54661b928d170bd2324bc3620565";
  }

  if ((buf = tj3Alloc(pitch * height * sampleSize)) == NULL)
    THROW("Could not allocate memory");
  initBitmap(buf, width, pitch, height, pf, bottomUp);

  SNPRINTF(filename, 80, "test_bmp%d_%s_%d_%s_%d.%s", precision, pixFormatStr[pf],
           align, bottomUp ? "bu" : "td", getpid(), ext);
  if (precision == 8) {
    TRY_TJ(handle, tj3SaveImage8(handle, filename, (unsigned char *)buf, width,
                                 pitch, height, pf));
  } else if (precision == 12) {
    TRY_TJ(handle, tj3SaveImage12(handle, filename, (short *)buf, width, pitch,
                                  height, pf));
  } else {
    TRY_TJ(handle, tj3SaveImage16(handle, filename, (unsigned short *)buf,
                                  width, pitch, height, pf));
  }
  md5sum = MD5File(filename, md5buf);
  if (!md5sum) {
    printf("\n   Could not determine MD5 sum of %s\n", filename);
    retval = -1;  goto bailout;
  }
  if (strcasecmp(md5sum, md5ref))
    THROW_MD5(filename, md5sum, md5ref);

  tj3Free(buf);  buf = NULL;
  if (precision == 8) {
    if ((buf = tj3LoadImage8(handle, filename, &loadWidth, align, &loadHeight,
                             &pf)) == NULL)
      THROW_TJ(handle);
  } else if (precision == 12) {
    if ((buf = tj3LoadImage12(handle, filename, &loadWidth, align, &loadHeight,
                              &pf)) == NULL)
      THROW_TJ(handle);
  } else {
    if ((buf = tj3LoadImage16(handle, filename, &loadWidth, align, &loadHeight,
                              &pf)) == NULL)
      THROW_TJ(handle);
  }
  if (width != loadWidth || height != loadHeight) {
    printf("\n   Image dimensions of %s are bogus\n", filename);
    retval = -1;  goto bailout;
  }
  if (!cmpBitmap(buf, width, pitch, height, pf, bottomUp, 0)) {
    printf("\n   Pixel data in %s is bogus\n", filename);
    retval = -1;  goto bailout;
  }
  if (pf == TJPF_GRAY) {
    tj3Free(buf);  buf = NULL;
    pf = TJPF_XBGR;
    if (precision == 8) {
      if ((buf = tj3LoadImage8(handle, filename, &loadWidth, align,
                               &loadHeight, &pf)) == NULL)
        THROW_TJ(handle);
    } else if (precision == 12) {
      if ((buf = tj3LoadImage12(handle, filename, &loadWidth, align,
                                &loadHeight, &pf)) == NULL)
        THROW_TJ(handle);
    } else {
      if ((buf = tj3LoadImage16(handle, filename, &loadWidth, align,
                                &loadHeight, &pf)) == NULL)
        THROW_TJ(handle);
    }
    pitch = PAD(width * tjPixelSize[pf], align);
    if (!cmpBitmap(buf, width, pitch, height, pf, bottomUp, 1)) {
      printf("\n   Converting %s to RGB failed\n", filename);
      retval = -1;  goto bailout;
    }

    tj3Free(buf);  buf = NULL;
    pf = TJPF_CMYK;
    if (precision == 8) {
      if ((buf = tj3LoadImage8(handle, filename, &loadWidth, align,
                               &loadHeight, &pf)) == NULL)
        THROW_TJ(handle);
    } else if (precision == 12) {
      if ((buf = tj3LoadImage12(handle, filename, &loadWidth, align,
                                &loadHeight, &pf)) == NULL)
        THROW_TJ(handle);
    } else {
      if ((buf = tj3LoadImage16(handle, filename, &loadWidth, align,
                                &loadHeight, &pf)) == NULL)
        THROW_TJ(handle);
    }
    pitch = PAD(width * tjPixelSize[pf], align);
    if (!cmpBitmap(buf, width, pitch, height, pf, bottomUp, 1)) {
      printf("\n   Converting %s to CMYK failed\n", filename);
      retval = -1;  goto bailout;
    }
  }
  /* Verify that tj3LoadImage*() returns the proper "preferred" pixel format
     for the file type. */
  tj3Free(buf);  buf = NULL;
  pf = pixelFormat;
  pixelFormat = TJPF_UNKNOWN;
  if (precision == 8) {
    if ((buf = tj3LoadImage8(handle, filename, &loadWidth, align, &loadHeight,
                             &pixelFormat)) == NULL)
      THROW_TJ(handle);
  } else if (precision == 12) {
    if ((buf = tj3LoadImage12(handle, filename, &loadWidth, align, &loadHeight,
                              &pixelFormat)) == NULL)
      THROW_TJ(handle);
  } else {
    if ((buf = tj3LoadImage16(handle, filename, &loadWidth, align, &loadHeight,
                              &pixelFormat)) == NULL)
      THROW_TJ(handle);
  }
  if ((pf == TJPF_GRAY && pixelFormat != TJPF_GRAY) ||
      (pf != TJPF_GRAY && !strcasecmp(ext, "bmp") &&
       pixelFormat != TJPF_BGR) ||
      (pf != TJPF_GRAY && !strcasecmp(ext, "ppm") &&
       pixelFormat != TJPF_RGB)) {
    printf("\n   tj3LoadImage8() returned unexpected pixel format: %s\n",
           pixFormatStr[pixelFormat]);
    retval = -1;
  }
  unlink(filename);

bailout:
  tj3Destroy(handle);
  tj3Free(buf);
  if (exitStatus < 0) return exitStatus;
  return retval;
}