Campbell Barton d869b5
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
Toshihiro Shimizu 890ddd
#define _CRT_SECURE_NO_DEPRECATE 1
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#define IS_TIFF_MAIN
Toshihiro Shimizu 890ddd
Campbell Barton d869b5
#if defined(_WIN32)
kusano 6139ac
#include <io.h></io.h>
kusano 6139ac
#else
roentgen 19af1b
#include <unistd.h></unistd.h>
kusano 6139ac
#endif
roentgen 19af1b
Shinya Kitaoka 79e39b
#include <memory></memory>
Shinya Kitaoka 79e39b
Toshihiro Shimizu 890ddd
#include "tiio.h"
Toshihiro Shimizu 890ddd
#include "tpixel.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "../tzp/toonztags.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
#include "traster.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
extern "C" {
Toshihiro Shimizu 890ddd
#include "tiffio.h"
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tiio_tif.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4996)
Toshihiro Shimizu 890ddd
#include "windows.h"
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**************************************************************************
Toshihiro Shimizu 890ddd
//    TifReader  implementation
Toshihiro Shimizu 890ddd
//**************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class TifReader final : public Tiio::Reader {
Shinya Kitaoka 120a6e
  TIFF *m_tiff;
Shinya Kitaoka 120a6e
  int m_row;
Shinya Kitaoka 120a6e
  bool m_tiled, m_stripped;
Shinya Kitaoka 120a6e
  int m_rowsPerStrip;
Shinya Kitaoka 120a6e
  int m_stripIndex;
Shinya Kitaoka 120a6e
  int m_rowLength;
Shinya Kitaoka 120a6e
  UCHAR *m_stripBuffer;
Shinya Kitaoka 120a6e
  double m_xdpi, m_ydpi;
Shinya Kitaoka 120a6e
  Tiio::RowOrder m_rowOrder;
Shinya Kitaoka 120a6e
  bool is16bitEnabled;
Shinya Kitaoka 120a6e
  bool m_isTzi;
Shinya Kitaoka 120a6e
  TRasterGR8P m_tmpRas;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TifReader(bool isTzi);
Shinya Kitaoka 120a6e
  ~TifReader();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void open(FILE *file) override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  Tiio::RowOrder getRowOrder() const override { return m_rowOrder; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  bool read16BitIsEnabled() const override { return false; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void enable16BitRead(bool enabled) override { is16bitEnabled = enabled; }
Shinya Kitaoka 473e70
Shinya Kitaoka 473e70
  int skipLines(int lineCount) override;
Shinya Kitaoka 473e70
  void readLine(char *buffer, int x0, int x1, int shrink) override;
Shinya Kitaoka 473e70
  void readLine(short *buffer, int x0, int x1, int shrink) override;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TifReader::TifReader(bool isTzi)
Shinya Kitaoka 120a6e
    : m_tiff(0)
Shinya Kitaoka 120a6e
    , m_row(0)
Shinya Kitaoka 120a6e
    , m_rowsPerStrip(0)
Shinya Kitaoka 120a6e
    , m_stripIndex(-1)
Shinya Kitaoka 120a6e
    //, m_stripBuffer(0)
Shinya Kitaoka 120a6e
    , m_rowLength(0)
Shinya Kitaoka 120a6e
    , m_xdpi(0)
Shinya Kitaoka 120a6e
    , m_ydpi(0)
Shinya Kitaoka 120a6e
    , m_rowOrder(Tiio::TOP2BOTTOM)
Shinya Kitaoka 120a6e
    , is16bitEnabled(true)
Shinya Kitaoka 120a6e
    , m_isTzi(isTzi)
Shinya Kitaoka 120a6e
    , m_tmpRas(0) {
Shinya Kitaoka 120a6e
  TIFFSetWarningHandler(0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TifReader::~TifReader() {
Shinya Kitaoka 120a6e
  if (m_tiff) TIFFClose(m_tiff);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (m_tmpRas) m_tmpRas->unlock();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  delete m_info.m_properties;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TifReader::open(FILE *file) {
Shinya Kitaoka 120a6e
  int fd = fileno(file);
roentgen 19af1b
#if 0
Toshihiro Shimizu 890ddd
	m_tiff = TIFFFdOpenNoCloseProc(fd, "", "rb");
roentgen 19af1b
#else
Shinya Kitaoka 120a6e
  m_tiff = TIFFFdOpen(dup(fd), "", "rb");
roentgen 19af1b
#endif
Shinya Kitaoka 120a6e
  if (!m_tiff) {
Shinya Kitaoka 120a6e
    std::string str("Tiff file closed");
Shinya Kitaoka 120a6e
    throw(str);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  uint32 w = 0, h = 0, rps = 0;
Shinya Kitaoka 120a6e
  uint16 bps = 0, spp = 0;
Shinya Kitaoka 120a6e
  uint32 tileWidth = 0, tileLength = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // TIFFSetDirectory(m_tiff,1);
Shinya Kitaoka 120a6e
  // TIFFGetField(m_tiff, TIFFTAG_PAGENUMBER, &pn);
Shinya Kitaoka 120a6e
  // int pn = TIFFNumberOfDirectories(m_tiff);
Shinya Kitaoka 120a6e
  // TIFFSetDirectory(m_tiff,1);
Shinya Kitaoka 120a6e
  TIFFGetField(m_tiff, TIFFTAG_IMAGEWIDTH, &w);
Shinya Kitaoka 120a6e
  TIFFGetField(m_tiff, TIFFTAG_IMAGELENGTH, &h);
Shinya Kitaoka 120a6e
  TIFFGetField(m_tiff, TIFFTAG_BITSPERSAMPLE, &bps);
Shinya Kitaoka 120a6e
  TIFFGetField(m_tiff, TIFFTAG_SAMPLESPERPIXEL, &spp);
Shinya Kitaoka 120a6e
  TIFFGetField(m_tiff, TIFFTAG_ROWSPERSTRIP, &rps);
Shinya Kitaoka 120a6e
  // int stripCount = TIFFNumberOfStrips(m_tiff);
Shinya Kitaoka 120a6e
  // int tileCount = TIFFNumberOfTiles(m_tiff);
Shinya Kitaoka 120a6e
  TIFFGetField(m_tiff, TIFFTAG_TILEWIDTH, &tileWidth);
Shinya Kitaoka 120a6e
  TIFFGetField(m_tiff, TIFFTAG_TILELENGTH, &tileLength);
Shinya Kitaoka 120a6e
  Tiio::TifWriterProperties *prop = new Tiio::TifWriterProperties();
Shinya Kitaoka 120a6e
  m_info.m_properties             = prop;
Shinya Kitaoka 120a6e
  uint16 orient                   = Tiio::TOP2BOTTOM;
Shinya Kitaoka 120a6e
  if (TIFFGetField(m_tiff, TIFFTAG_ORIENTATION, &orient)) {
Shinya Kitaoka 120a6e
    switch (orient) {
Shinya Kitaoka 120a6e
    case ORIENTATION_TOPLEFT: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_orientation.setValue(TNZ_INFO_ORIENT_TOPLEFT);
Shinya Kitaoka 120a6e
      m_rowOrder = Tiio::TOP2BOTTOM;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case ORIENTATION_TOPRIGHT: /* row 0 top, col 0 rhs */
Shinya Kitaoka 120a6e
      prop->m_orientation.setValue(TNZ_INFO_ORIENT_TOPRIGHT);
Shinya Kitaoka 120a6e
      m_rowOrder = Tiio::TOP2BOTTOM;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case ORIENTATION_LEFTTOP: /* row 0 lhs, col 0 top */
Shinya Kitaoka 120a6e
      prop->m_orientation.setValue(TNZ_INFO_ORIENT_LEFTTOP);
Shinya Kitaoka 120a6e
      m_rowOrder = Tiio::TOP2BOTTOM;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case ORIENTATION_RIGHTTOP: /* row 0 rhs, col 0 top */
Shinya Kitaoka 120a6e
      prop->m_orientation.setValue(TNZ_INFO_ORIENT_RIGHTTOP);
Shinya Kitaoka 120a6e
      m_rowOrder = Tiio::TOP2BOTTOM;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    case ORIENTATION_BOTRIGHT: /* row 0 bottom, col 0 rhs */
Shinya Kitaoka 120a6e
      prop->m_orientation.setValue(TNZ_INFO_ORIENT_BOTRIGHT);
Shinya Kitaoka 120a6e
      m_rowOrder = Tiio::BOTTOM2TOP;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case ORIENTATION_BOTLEFT: /* row 0 bottom, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_orientation.setValue(TNZ_INFO_ORIENT_BOTLEFT);
Shinya Kitaoka 120a6e
      m_rowOrder = Tiio::BOTTOM2TOP;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case ORIENTATION_RIGHTBOT: /* row 0 rhs, col 0 bottom */
Shinya Kitaoka 120a6e
      prop->m_orientation.setValue(TNZ_INFO_ORIENT_RIGHTBOT);
Shinya Kitaoka 120a6e
      m_rowOrder = Tiio::BOTTOM2TOP;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case ORIENTATION_LEFTBOT: /* row 0 lhs, col 0 bottom */
Shinya Kitaoka 120a6e
      prop->m_orientation.setValue(TNZ_INFO_ORIENT_LEFTBOT);
Shinya Kitaoka 120a6e
      m_rowOrder = Tiio::BOTTOM2TOP;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    default:
Shinya Kitaoka 120a6e
      prop->m_orientation.setValue(TNZ_INFO_ORIENT_NONE);
Shinya Kitaoka 120a6e
      m_rowOrder = Tiio::TOP2BOTTOM;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  USHORT compression;
Shinya Kitaoka 120a6e
  TIFFGetField(m_tiff, TIFFTAG_COMPRESSION, &compression);
Shinya Kitaoka 120a6e
  switch (compression) {
Shinya Kitaoka 120a6e
  case COMPRESSION_LZW:
Shinya Kitaoka 120a6e
    prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_LZW);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case COMPRESSION_PACKBITS:
Shinya Kitaoka 120a6e
    prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_PACKBITS);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case COMPRESSION_THUNDERSCAN:
Shinya Kitaoka 120a6e
    prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_THUNDERSCAN);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case COMPRESSION_CCITTFAX3:
Shinya Kitaoka 120a6e
    prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_CCITTFAX3);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case COMPRESSION_CCITTFAX4:
Shinya Kitaoka 120a6e
    prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_CCITTFAX4);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case COMPRESSION_CCITTRLE:
Shinya Kitaoka 120a6e
    prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_CCITTRLE);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case COMPRESSION_JPEG:
Shinya Kitaoka 120a6e
    prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_JPEG);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case COMPRESSION_OJPEG:
Shinya Kitaoka 120a6e
    prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_OJPEG);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case COMPRESSION_NONE:
Shinya Kitaoka 120a6e
    prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_NONE);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case COMPRESSION_SGILOG:
Shinya Kitaoka 120a6e
    prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_SGILOG);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case COMPRESSION_SGILOG24:
Shinya Kitaoka 120a6e
    prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_SGILOG24);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case COMPRESSION_ADOBE_DEFLATE:
Shinya Kitaoka 120a6e
    prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_ADOBE_DEFLATE);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case COMPRESSION_DEFLATE:
Shinya Kitaoka 120a6e
    prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_DEFLATE);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  /*default :
Shinya Kitaoka 120a6e
prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_UNKNOWN);
Shinya Kitaoka 120a6e
break;*/
Shinya Kitaoka 120a6e
  default:
Shinya Kitaoka 120a6e
    assert(0);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  float xdpi = 0, ydpi = 0;
Shinya Kitaoka 120a6e
  TIFFGetField(m_tiff, TIFFTAG_XRESOLUTION, &xdpi);
Shinya Kitaoka 120a6e
  TIFFGetField(m_tiff, TIFFTAG_YRESOLUTION, &ydpi);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool swapxy = false;  // orient == ORIENTATION_RIGHTTOP;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (swapxy) {
Shinya Kitaoka 120a6e
    tswap(w, h);
Shinya Kitaoka 120a6e
    tswap(xdpi, ydpi);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_xdpi        = xdpi;
Shinya Kitaoka 120a6e
  m_ydpi        = ydpi;
Shinya Kitaoka 120a6e
  m_info.m_lx   = w;
Shinya Kitaoka 120a6e
  m_info.m_ly   = h;
Shinya Kitaoka 120a6e
  m_info.m_dpix = xdpi;
Shinya Kitaoka 120a6e
  m_info.m_dpiy = ydpi;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_info.m_samplePerPixel = spp;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (bps == 64 && spp == 3) bps = 16;  // immagine con bpp = 192
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  uint16 photometric;  // codice di controllo
Shinya Kitaoka 120a6e
  TIFFGetField(m_tiff, TIFFTAG_PHOTOMETRIC, &photometric);
Shinya Kitaoka 120a6e
  if (photometric == 3 &&
Shinya Kitaoka 120a6e
      (bps == 2 || bps == 4))  // immagini con PHOTOMATRIC_PALETTE
Shinya Kitaoka 120a6e
    bps = 8;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (photometric == 1 && (bps == 12 || bps == 24)) bps = 16;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (bps == 6) bps = 4;  // immagini con bps = 6
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (bps == 10 || bps == 12 ||
Shinya Kitaoka 120a6e
      bps == 14)  // immagini con bps = 10 , 12 , 14 , 24 , 32
Shinya Kitaoka 120a6e
    bps                           = 8;
Shinya Kitaoka 120a6e
  if (bps == 24 || bps == 32) bps = 16;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_info.m_bitsPerSample = bps;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (bps == 8) switch (spp) {
Shinya Kitaoka 120a6e
    case 1: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L" 8(GREYTONES)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case 3: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L"24(RGB)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case 4: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L"32(RGBM)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  else if (bps == 16)
Shinya Kitaoka 120a6e
    switch (spp) {
Shinya Kitaoka 120a6e
    case 1: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      // prop->m_bitsPerPixel.setValue(L"16(GREYTONES)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case 3: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L"48(RGB)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case 4: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L"64(RGBM)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  else if (bps == 2)
Shinya Kitaoka 120a6e
    switch (spp) {
Shinya Kitaoka 120a6e
    case 1: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L"32(RGBM)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case 3: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L"32(RGBM)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case 4: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L"32(RGBM)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  else if (bps == 1)
Shinya Kitaoka 120a6e
    switch (spp) {
Shinya Kitaoka 120a6e
    case 1: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L" 1(BW)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case 3: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L"32(RGBM)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case 4: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L"32(RGBM)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  else if (bps == 4)
Shinya Kitaoka 120a6e
    switch (spp) {
Shinya Kitaoka 120a6e
    case 1: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L" 8(GREYTONES)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case 3: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L"32(RGBM)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case 4: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
      prop->m_bitsPerPixel.setValue(L"32(RGBM)");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  else if (bps == 64 && spp == 3)
Shinya Kitaoka 120a6e
    prop->m_bitsPerPixel.setValue(L"64(RGBM)");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    assert(false);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (TIFFIsTiled(m_tiff)) {
Shinya Kitaoka 120a6e
    m_rowsPerStrip  = tileLength;
Shinya Kitaoka 120a6e
    int tilesPerRow = (w + tileWidth - 1) / tileWidth;
Shinya Kitaoka 120a6e
    // m_rowLength = tileWidth * tilesPerRow;
Shinya Kitaoka 120a6e
    m_rowLength   = m_info.m_lx;
Shinya Kitaoka 120a6e
    int pixelSize = bps == 16 ? 8 : 4;
Shinya Kitaoka 120a6e
    int stripSize = m_rowsPerStrip * m_rowLength * pixelSize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_tmpRas = TRasterGR8P(stripSize, 1);
Shinya Kitaoka 120a6e
    m_tmpRas->lock();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_stripBuffer = m_tmpRas->getRawData();
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    m_rowsPerStrip = rps;
Shinya Kitaoka d1f6c4
    // if(m_rowsPerStrip<=0) m_rowsPerStrip = 1;			//potrei
Shinya Kitaoka d1f6c4
    // mettere
Shinya Kitaoka 38fd86
    // qualsiasi
Shinya Kitaoka 120a6e
    // valore
Shinya Kitaoka 120a6e
    // purchè sia lo stesso in tif_getimage.c linea 2512
Shinya Kitaoka 120a6e
    // if(m_rowsPerStrip==-1) assert(0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (m_rowsPerStrip <= 0) m_rowsPerStrip = m_info.m_ly;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    int stripSize = m_rowsPerStrip * w * 4;  // + 4096;  TIFFStripSize(m_tiff);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (bps == 16) stripSize *= 2;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_tmpRas = TRasterGR8P(stripSize, 1);
Shinya Kitaoka 120a6e
    m_tmpRas->lock();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_stripBuffer = m_tmpRas->getRawData();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_rowLength = m_info.m_lx;  // w;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
int TIFFTileRowSize(m_tiff);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
m_rowsPerStrip = 0;
Shinya Kitaoka 120a6e
if(TIFFGetField(m_tiff, TIFFTAG_ROWSPERSTRIP, &rps) )
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
int stripSize = TIFFStripSize(m_tiff);
Shinya Kitaoka 120a6e
if(stripSize>0)
Shinya Kitaoka 120a6e
 {
Shinya Kitaoka 120a6e
 }
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  if (m_isTzi) {
Shinya Kitaoka 120a6e
    USHORT risCount  = 0;
Shinya Kitaoka 120a6e
    USHORT *risArray = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (TIFFGetField(m_tiff, TIFFTAG_TOONZWINDOW, &risCount, &risArray)) {
Shinya Kitaoka 120a6e
      if (m_info.m_lx == risArray[2] &&
Shinya Kitaoka 120a6e
          m_info.m_ly ==
Shinya Kitaoka 120a6e
              risArray[3])  // se sono diverse, la lettura tif crasha....
Shinya Kitaoka 120a6e
      {
Shinya Kitaoka 120a6e
        // m_info.m_lx = risArray[2];
Shinya Kitaoka 120a6e
        // m_info.m_ly = risArray[3];
Shinya Kitaoka 120a6e
        m_info.m_x0 = risArray[0];
Shinya Kitaoka 120a6e
        m_info.m_y0 = risArray[1];
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      //      USHORT extraMask = risArray[4];
Shinya Kitaoka 120a6e
      // bool isEduFile = risArray[TOONZWINDOW_COUNT - 1] & 1;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      m_info.m_x0 = 0;
Shinya Kitaoka 120a6e
      m_info.m_y0 = 0;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (swapxy) {
Shinya Kitaoka 120a6e
      tswap(m_info.m_x0, m_info.m_y0);
Shinya Kitaoka 120a6e
      tswap(m_info.m_lx, m_info.m_ly);
Toshihiro Shimizu 890ddd
    }
Shinya Kitaoka 120a6e
    m_info.m_x1 = m_info.m_x0 + w;
Shinya Kitaoka 120a6e
    m_info.m_y1 = m_info.m_y0 + h;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    m_info.m_x0 = m_info.m_y0 = 0;
Shinya Kitaoka 120a6e
    m_info.m_x1               = m_info.m_x0 + m_info.m_lx - 1;
Shinya Kitaoka 120a6e
    m_info.m_y1               = m_info.m_y0 + m_info.m_ly - 1;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int TifReader::skipLines(int lineCount) {
Shinya Kitaoka 120a6e
  m_row += lineCount;
Shinya Kitaoka 120a6e
  return lineCount;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "timage_io.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TifReader::readLine(short *buffer, int x0, int x1, int shrink) {
Shinya Kitaoka 120a6e
  assert(shrink > 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  const int pixelSize = 8;
Shinya Kitaoka 120a6e
  int stripRowSize    = m_rowLength * pixelSize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_row < m_info.m_y0 || m_row > m_info.m_y1) {
Shinya Kitaoka 120a6e
    memset(buffer, 0, (x1 - x0 + 1) * pixelSize);
Shinya Kitaoka 120a6e
    m_row++;
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int stripIndex = m_row / m_rowsPerStrip;
Shinya Kitaoka 120a6e
  if (m_stripIndex != stripIndex) {
Shinya Kitaoka 120a6e
    // Retrieve the strip holding current row. Please, observe that
Shinya Kitaoka 120a6e
    // TIFF functions will return the strip buffer in the BOTTOM-UP orientation,
Shinya Kitaoka 120a6e
    // no matter the internal tif's orientation storage
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_stripIndex = stripIndex;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (TIFFIsTiled(m_tiff)) {
Shinya Kitaoka 120a6e
      // Retrieve tiles size
Shinya Kitaoka 120a6e
      uint32 tileWidth = 0, tileHeight = 0;
Shinya Kitaoka 120a6e
      TIFFGetField(m_tiff, TIFFTAG_TILEWIDTH, &tileWidth);
Shinya Kitaoka 120a6e
      TIFFGetField(m_tiff, TIFFTAG_TILELENGTH, &tileHeight);
Shinya Kitaoka 120a6e
      assert(tileWidth > 0 && tileHeight > 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      // Allocate a sufficient buffer to store a single tile
Shinya Kitaoka 120a6e
      int tileSize = tileWidth * tileHeight;
Shinya Kitaoka 120a6e
      std::unique_ptr<uint64[]> tile(new uint64[tileSize]);</uint64[]>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      int x = 0;
Shinya Kitaoka 120a6e
      int y = tileHeight * m_stripIndex;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      // In case it's the last tiles row, the tile size might exceed the image
Shinya Kitaoka 120a6e
      // bounds
Shinya Kitaoka 120a6e
      int lastTy = std::min((int)tileHeight, m_info.m_ly - y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      // Traverse the tiles row
Shinya Kitaoka 120a6e
      while (x < m_info.m_lx) {
Shinya Kitaoka 120a6e
        int ret = TIFFReadRGBATile_64(m_tiff, x, y, tile.get());
Shinya Kitaoka 120a6e
        assert(ret);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        int tileRowSize = std::min((int)tileWidth, m_info.m_lx - x) * pixelSize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        // Copy the tile rows in the corresponding output strip rows
Shinya Kitaoka 120a6e
        for (int ty = 0; ty < lastTy; ++ty) {
Shinya Kitaoka 120a6e
          memcpy(m_stripBuffer + (ty * m_rowLength + x) * pixelSize,
Shinya Kitaoka 120a6e
                 (UCHAR *)tile.get() + ty * tileWidth * pixelSize, tileRowSize);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        x += tileWidth;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      int y  = m_rowsPerStrip * m_stripIndex;
Shinya Kitaoka 120a6e
      int ok = TIFFReadRGBAStrip_64(m_tiff, y, (uint64 *)m_stripBuffer);
Shinya Kitaoka 120a6e
      assert(ok);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  uint16 orient = ORIENTATION_TOPLEFT;
Shinya Kitaoka 120a6e
  TIFFGetField(m_tiff, TIFFTAG_ORIENTATION, &orient);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int r = m_rowsPerStrip - 1 - (m_row % m_rowsPerStrip);
Shinya Kitaoka 120a6e
  switch (orient)  // Pretty weak check for top/bottom orientation
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
  case ORIENTATION_TOPLEFT:
Shinya Kitaoka 120a6e
  case ORIENTATION_TOPRIGHT:
Shinya Kitaoka 120a6e
  case ORIENTATION_LEFTTOP:
Shinya Kitaoka 120a6e
  case ORIENTATION_RIGHTTOP:
Shinya Kitaoka 120a6e
    // We have to invert the fixed BOTTOM-UP returned by TIFF functions - since
Shinya Kitaoka 120a6e
    // this function is
Shinya Kitaoka 120a6e
    // supposed to ignore orientation issues (which are managed outside).
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // The last tiles row will actually start at the END OF THE IMAGE (not
Shinya Kitaoka 120a6e
    // necessarily at
Shinya Kitaoka 120a6e
    // m_rowsPerStrip multiples). So, we must adjust for that.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    r = std::min(m_rowsPerStrip, m_info.m_ly - m_rowsPerStrip * m_stripIndex) -
Shinya Kitaoka 120a6e
        1 - (m_row % m_rowsPerStrip);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  case ORIENTATION_BOTRIGHT:
Shinya Kitaoka 120a6e
  case ORIENTATION_BOTLEFT:
Shinya Kitaoka 120a6e
  case ORIENTATION_RIGHTBOT:
Shinya Kitaoka 120a6e
  case ORIENTATION_LEFTBOT:
Shinya Kitaoka 120a6e
    r = m_row % m_rowsPerStrip;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Finally, copy the strip row to the output row buffer
Shinya Kitaoka 120a6e
  TPixel64 *pix = (TPixel64 *)buffer;
Shinya Kitaoka 120a6e
  USHORT *v     = (USHORT *)(m_stripBuffer + r * stripRowSize);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  pix += x0;
Shinya Kitaoka 120a6e
  v += 4 * x0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int width =
Shinya Kitaoka 120a6e
      (x1 < x0) ? (m_info.m_lx - 1) / shrink + 1 : (x1 - x0) / shrink + 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int i = 0; i < width; i++) {
Shinya Kitaoka 120a6e
    USHORT c = *v++;
Shinya Kitaoka 120a6e
    pix->r   = c;
Shinya Kitaoka 120a6e
    c        = *v++;
Shinya Kitaoka 120a6e
    pix->g   = c;
Shinya Kitaoka 120a6e
    c        = *v++;
Shinya Kitaoka 120a6e
    pix->b   = c;
Shinya Kitaoka 120a6e
    c        = *v++;
Shinya Kitaoka 120a6e
    pix->m   = c;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    pix += shrink;
Shinya Kitaoka 120a6e
    v += 4 * (shrink - 1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_row++;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===============================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TifReader::readLine(char *buffer, int x0, int x1, int shrink) {
Shinya Kitaoka 120a6e
  if (this->m_info.m_bitsPerSample == 16 &&
Shinya Kitaoka 120a6e
      this->m_info.m_samplePerPixel >= 3) {
Shinya Kitaoka 120a6e
    std::vector<short> app(4 * (m_info.m_lx));</short>
Shinya Kitaoka 120a6e
    readLine(&app[0], x0, x1, shrink);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TPixel64 *pixin = (TPixel64 *)&app[0];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TPixel32 *pixout = (TPixel32 *)buffer;
Shinya Kitaoka 120a6e
    for (int j = 0; j < x0; j++) {
Shinya Kitaoka 120a6e
      pixout++;
Shinya Kitaoka 120a6e
      pixin++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    for (int i = 0; i < (x1 - x0) + 1; i++)
Shinya Kitaoka 120a6e
      *pixout++ = PixelConverter<tpixel32>::from(*pixin++);</tpixel32>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(shrink > 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  const int pixelSize = 4;
Shinya Kitaoka 120a6e
  int stripRowSize    = m_rowLength * pixelSize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_row < m_info.m_y0 || m_row > m_info.m_y1) {
Shinya Kitaoka 120a6e
    memset(buffer, 0, (x1 - x0 + 1) * pixelSize);
Shinya Kitaoka 120a6e
    m_row++;
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int stripIndex = m_row / m_rowsPerStrip;
Shinya Kitaoka 120a6e
  if (m_stripIndex != stripIndex) {
Shinya Kitaoka 120a6e
    m_stripIndex = stripIndex;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (TIFFIsTiled(m_tiff)) {
Shinya Kitaoka 120a6e
      uint32 tileWidth = 0, tileHeight = 0;
Shinya Kitaoka 120a6e
      TIFFGetField(m_tiff, TIFFTAG_TILEWIDTH, &tileWidth);
Shinya Kitaoka 120a6e
      TIFFGetField(m_tiff, TIFFTAG_TILELENGTH, &tileHeight);
Shinya Kitaoka 120a6e
      assert(tileWidth > 0 && tileHeight > 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      int tileSize = tileWidth * tileHeight;
Shinya Kitaoka 120a6e
      std::unique_ptr<uint32[]> tile(new uint32[tileSize]);</uint32[]>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      int x = 0;
Shinya Kitaoka 120a6e
      int y = tileHeight * m_stripIndex;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      int lastTy = std::min((int)tileHeight, m_info.m_ly - y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      while (x < m_info.m_lx) {
Shinya Kitaoka 120a6e
        int ret = TIFFReadRGBATile(m_tiff, x, y, tile.get());
Shinya Kitaoka 120a6e
        assert(ret);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        int tileRowSize =
Shinya Kitaoka 120a6e
            std::min((int)tileWidth, (int)(m_info.m_lx - x)) * pixelSize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        for (int ty = 0; ty < lastTy; ++ty) {
Shinya Kitaoka 120a6e
          memcpy(m_stripBuffer + (ty * m_rowLength + x) * pixelSize,
Shinya Kitaoka 120a6e
                 (UCHAR *)tile.get() + ty * tileWidth * pixelSize, tileRowSize);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        x += tileWidth;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      int y  = m_rowsPerStrip * m_stripIndex;
Shinya Kitaoka 120a6e
      int ok = TIFFReadRGBAStrip(m_tiff, y, (uint32 *)m_stripBuffer);
Shinya Kitaoka 120a6e
      assert(ok);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  uint16 orient = ORIENTATION_TOPLEFT;
Shinya Kitaoka 120a6e
  TIFFGetField(m_tiff, TIFFTAG_ORIENTATION, &orient);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int r = m_rowsPerStrip - 1 - (m_row % m_rowsPerStrip);
Shinya Kitaoka 120a6e
  switch (orient)  // Pretty weak check for top/bottom orientation
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
  case ORIENTATION_TOPLEFT:
Shinya Kitaoka 120a6e
  case ORIENTATION_TOPRIGHT:
Shinya Kitaoka 120a6e
  case ORIENTATION_LEFTTOP:
Shinya Kitaoka 120a6e
  case ORIENTATION_RIGHTTOP:
Shinya Kitaoka 120a6e
    // We have to invert the fixed BOTTOM-UP returned by TIFF functions - since
Shinya Kitaoka 120a6e
    // this function is
Shinya Kitaoka 120a6e
    // supposed to ignore orientation issues (which are managed outside).
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // The last tiles row will actually start at the END OF THE IMAGE (not
Shinya Kitaoka 120a6e
    // necessarily at
Shinya Kitaoka 120a6e
    // m_rowsPerStrip multiples). So, we must adjust for that.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    r = std::min(m_rowsPerStrip, m_info.m_ly - m_rowsPerStrip * m_stripIndex) -
Shinya Kitaoka 120a6e
        1 - (m_row % m_rowsPerStrip);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  case ORIENTATION_BOTRIGHT:
Shinya Kitaoka 120a6e
  case ORIENTATION_BOTLEFT:
Shinya Kitaoka 120a6e
  case ORIENTATION_RIGHTBOT:
Shinya Kitaoka 120a6e
  case ORIENTATION_LEFTBOT:
Shinya Kitaoka 120a6e
    r = m_row % m_rowsPerStrip;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPixel32 *pix = (TPixel32 *)buffer;
Shinya Kitaoka 120a6e
  uint32 *v     = (uint32 *)(m_stripBuffer + r * stripRowSize);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  pix += x0;
Shinya Kitaoka 120a6e
  v += x0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int width =
Shinya Kitaoka 120a6e
      (x1 < x0) ? (m_info.m_lx - 1) / shrink + 1 : (x1 - x0) / shrink + 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int i = 0; i < width; i++) {
Shinya Kitaoka 120a6e
    uint32 c = *v;
Shinya Kitaoka 120a6e
    pix->r   = (UCHAR)TIFFGetR(c);
Shinya Kitaoka 120a6e
    pix->g   = (UCHAR)TIFFGetG(c);
Shinya Kitaoka 120a6e
    pix->b   = (UCHAR)TIFFGetB(c);
Shinya Kitaoka 120a6e
    pix->m   = (UCHAR)TIFFGetA(c);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    v += shrink;
Shinya Kitaoka 120a6e
    pix += shrink;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_row++;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//============================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Tiio::TifWriterProperties::TifWriterProperties()
Shinya Kitaoka 120a6e
    : m_byteOrdering("Byte Ordering")
Shinya Kitaoka 120a6e
    , m_compressionType("Compression Type")
Shinya Kitaoka 120a6e
    , m_bitsPerPixel("Bits Per Pixel")
Shinya Kitaoka 120a6e
    , m_orientation("Orientation") {
Shinya Kitaoka 120a6e
  m_byteOrdering.addValue(L"IBM PC");
Shinya Kitaoka 120a6e
  m_byteOrdering.addValue(L"Mac");
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
  m_byteOrdering.setValue(L"IBM PC");
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  m_byteOrdering.setValue(L"Mac");
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_LZW);
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_NONE);
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_PACKBITS);
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_THUNDERSCAN);
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_CCITTFAX3);
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_CCITTFAX4);
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_CCITTRLE);
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_JPEG);
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_OJPEG);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_SGILOG);
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_SGILOG24);
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_ADOBE_DEFLATE);
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_DEFLATE);
Shinya Kitaoka 120a6e
  m_compressionType.addValue(TNZ_INFO_COMPRESS_UNKNOWN);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_compressionType.setValue(TNZ_INFO_COMPRESS_LZW);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_bitsPerPixel.addValue(L"24(RGB)");
Shinya Kitaoka 120a6e
  m_bitsPerPixel.addValue(L"48(RGB)");
Shinya Kitaoka 120a6e
  m_bitsPerPixel.addValue(
Shinya Kitaoka 120a6e
      L" 1(BW)");  // WATCH OUT! If you reorder this remember to look for
Shinya Kitaoka 120a6e
  m_bitsPerPixel.addValue(L" 8(GREYTONES)");  // TRasterImage::isScanBW() usage
Shinya Kitaoka 120a6e
                                              // that bpp choice index
Shinya Kitaoka 120a6e
  // m_bitsPerPixel.addValue(L"16(GREYTONES)");                  // is HARDCODED
Shinya Kitaoka 120a6e
  // nearby...                   -.-'
Shinya Kitaoka 120a6e
  m_bitsPerPixel.addValue(L"32(RGBM)");
Shinya Kitaoka 120a6e
  m_bitsPerPixel.addValue(L"64(RGBM)");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_bitsPerPixel.setValue(L"32(RGBM)");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_orientation.addValue(TNZ_INFO_ORIENT_TOPLEFT);
Shinya Kitaoka 120a6e
  m_orientation.addValue(TNZ_INFO_ORIENT_TOPRIGHT);
Shinya Kitaoka 120a6e
  m_orientation.addValue(TNZ_INFO_ORIENT_BOTRIGHT);
Shinya Kitaoka 120a6e
  m_orientation.addValue(TNZ_INFO_ORIENT_BOTLEFT);
Shinya Kitaoka 120a6e
  m_orientation.addValue(TNZ_INFO_ORIENT_LEFTTOP);
Shinya Kitaoka 120a6e
  m_orientation.addValue(TNZ_INFO_ORIENT_RIGHTTOP);
Shinya Kitaoka 120a6e
  m_orientation.addValue(TNZ_INFO_ORIENT_RIGHTBOT);
Shinya Kitaoka 120a6e
  m_orientation.addValue(TNZ_INFO_ORIENT_LEFTBOT);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // m_orientation.setValue(TNZ_INFO_ORIENT_TOPLEFT);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bind(m_byteOrdering);
Shinya Kitaoka 120a6e
  bind(m_compressionType);
Shinya Kitaoka 120a6e
  bind(m_bitsPerPixel);
Shinya Kitaoka 120a6e
  bind(m_orientation);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//============================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class TifWriter final : public Tiio::Writer {
Shinya Kitaoka 120a6e
  TIFF *m_tiff;
Shinya Kitaoka 120a6e
  int m_row;
Shinya Kitaoka 120a6e
  // Tiio::TifWriterProperties m_properties;
Shinya Kitaoka 120a6e
  unsigned char *m_lineBuffer;
Shinya Kitaoka 120a6e
  Tiio::RowOrder m_rowOrder;
Shinya Kitaoka 120a6e
  int m_bpp;
Shinya Kitaoka 120a6e
  int m_RightToLeft;
Shinya Kitaoka 120a6e
  void fillBits(UCHAR *bufout, UCHAR *bufin, int lx, int incr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TifWriter();
Shinya Kitaoka 120a6e
  ~TifWriter();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void open(FILE *file, const TImageInfo &info) override;
Shinya Kitaoka 473e70
  void writeLine(char *buffer) override;
Shinya Kitaoka 473e70
  void writeLine(short *buffer) override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void flush() override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  Tiio::RowOrder getRowOrder() const override { return m_rowOrder; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TifWriter::TifWriter()
Shinya Kitaoka 120a6e
    : m_tiff(0), m_row(-1), m_lineBuffer(0), m_RightToLeft(false) {
Shinya Kitaoka 120a6e
  TIFFSetWarningHandler(0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TifWriter::~TifWriter() {
Shinya Kitaoka 120a6e
  if (m_tiff) TIFFClose(m_tiff);
Toshihiro Shimizu 890ddd
Campbell Barton a9208f
  delete[] m_lineBuffer;
Shinya Kitaoka 120a6e
  delete m_properties;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TifWriter::open(FILE *file, const TImageInfo &info) {
Shinya Kitaoka 120a6e
  m_info           = info;
Shinya Kitaoka 120a6e
  std::string mode = "w";
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!m_properties) m_properties = new Tiio::TifWriterProperties();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::wstring byteOrdering =
Shinya Kitaoka 120a6e
      ((TEnumProperty *)(m_properties->getProperty("Byte Ordering")))
Shinya Kitaoka 120a6e
          ->getValue();
Shinya Kitaoka 120a6e
  if (byteOrdering == L"IBM PC")
Shinya Kitaoka 120a6e
    mode += "l";
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    mode += "b";
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TEnumProperty *p =
Shinya Kitaoka 120a6e
      (TEnumProperty *)(m_properties->getProperty("Bits Per Pixel"));
Shinya Kitaoka 120a6e
  assert(p);
Shinya Kitaoka 120a6e
  std::string str = ::to_string(p->getValue());
Shinya Kitaoka 120a6e
  m_bpp           = atoi(str.c_str());
Shinya Kitaoka 120a6e
  assert(m_bpp == 1 || m_bpp == 8 || m_bpp == 16 || m_bpp == 24 ||
Shinya Kitaoka 120a6e
         m_bpp == 32 || m_bpp == 48 || m_bpp == 64);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int fd = fileno(file);
roentgen 19af1b
#if 0
Toshihiro Shimizu 890ddd
	m_tiff = TIFFFdOpenNoCloseProc(fd, "", mode.c_str());
roentgen 19af1b
#else
Shinya Kitaoka 120a6e
  m_tiff = TIFFFdOpen(dup(fd), "", mode.c_str());
roentgen 19af1b
#endif
Shinya Kitaoka 120a6e
  if (!m_tiff) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::wstring worientation =
Shinya Kitaoka 120a6e
      ((TEnumProperty *)(m_properties->getProperty("Orientation")))->getValue();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int orientation;
Shinya Kitaoka 120a6e
  if (worientation == TNZ_INFO_ORIENT_TOPLEFT)
Shinya Kitaoka 120a6e
    orientation = ORIENTATION_TOPLEFT;
Shinya Kitaoka 120a6e
  else if (worientation == TNZ_INFO_ORIENT_TOPRIGHT)
Shinya Kitaoka 120a6e
    orientation = ORIENTATION_TOPRIGHT;
Shinya Kitaoka 120a6e
  else if (worientation == TNZ_INFO_ORIENT_BOTRIGHT)
Shinya Kitaoka 120a6e
    orientation = ORIENTATION_BOTRIGHT;
Shinya Kitaoka 120a6e
  else if (worientation == TNZ_INFO_ORIENT_BOTLEFT)
Shinya Kitaoka 120a6e
    orientation = ORIENTATION_BOTLEFT;
Shinya Kitaoka 120a6e
  else if (worientation == TNZ_INFO_ORIENT_LEFTTOP)
Shinya Kitaoka 120a6e
    orientation = ORIENTATION_LEFTTOP;
Shinya Kitaoka 120a6e
  else if (worientation == TNZ_INFO_ORIENT_RIGHTTOP)
Shinya Kitaoka 120a6e
    orientation = ORIENTATION_RIGHTTOP;
Shinya Kitaoka 120a6e
  else if (worientation == TNZ_INFO_ORIENT_RIGHTBOT)
Shinya Kitaoka 120a6e
    orientation = ORIENTATION_RIGHTBOT;
Shinya Kitaoka 120a6e
  else if (worientation == TNZ_INFO_ORIENT_LEFTBOT)
Shinya Kitaoka 120a6e
    orientation = ORIENTATION_LEFTBOT;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    assert(0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  switch (orientation) {
Shinya Kitaoka 120a6e
  case ORIENTATION_TOPLEFT: /* row 0 top, col 0 lhs */
Shinya Kitaoka 120a6e
    m_rowOrder = Tiio::TOP2BOTTOM;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case ORIENTATION_TOPRIGHT: /* row 0 top, col 0 rhs */
Shinya Kitaoka 120a6e
    m_rowOrder = Tiio::TOP2BOTTOM;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case ORIENTATION_LEFTTOP: /* row 0 lhs, col 0 top */
Shinya Kitaoka 120a6e
    m_rowOrder = Tiio::TOP2BOTTOM;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case ORIENTATION_RIGHTTOP: /* row 0 rhs, col 0 top */
Shinya Kitaoka 120a6e
    m_rowOrder = Tiio::TOP2BOTTOM;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  case ORIENTATION_BOTRIGHT: /* row 0 bottom, col 0 rhs */
Shinya Kitaoka 120a6e
    m_rowOrder = Tiio::BOTTOM2TOP;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case ORIENTATION_BOTLEFT: /* row 0 bottom, col 0 lhs */
Shinya Kitaoka 120a6e
    m_rowOrder = Tiio::BOTTOM2TOP;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case ORIENTATION_RIGHTBOT: /* row 0 rhs, col 0 bottom */
Shinya Kitaoka 120a6e
    m_rowOrder = Tiio::BOTTOM2TOP;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case ORIENTATION_LEFTBOT: /* row 0 lhs, col 0 bottom */
Shinya Kitaoka 120a6e
    m_rowOrder = Tiio::BOTTOM2TOP;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  default:
Shinya Kitaoka 120a6e
    m_rowOrder = Tiio::TOP2BOTTOM;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_RightToLeft = false;
Shinya Kitaoka 120a6e
  if (orientation == ORIENTATION_TOPRIGHT ||
Shinya Kitaoka 120a6e
      orientation == ORIENTATION_BOTRIGHT ||
Shinya Kitaoka 120a6e
      orientation == ORIENTATION_RIGHTTOP ||
Shinya Kitaoka 120a6e
      orientation == ORIENTATION_RIGHTBOT)
Shinya Kitaoka 120a6e
    m_RightToLeft = true;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int bitsPerSample =
Shinya Kitaoka 120a6e
      (m_bpp == 1) ? 1 : ((m_bpp == 8 || m_bpp == 24 || m_bpp == 32) ? 8 : 16);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TIFFSetField(m_tiff, TIFFTAG_IMAGEWIDTH, m_info.m_lx);
Shinya Kitaoka 120a6e
  TIFFSetField(m_tiff, TIFFTAG_IMAGELENGTH, m_info.m_ly);
Shinya Kitaoka 120a6e
  TIFFSetField(m_tiff, TIFFTAG_BITSPERSAMPLE, bitsPerSample);
Shinya Kitaoka 120a6e
  TIFFSetField(m_tiff, TIFFTAG_SAMPLESPERPIXEL, m_bpp / bitsPerSample);
Shinya Kitaoka 120a6e
  TIFFSetField(m_tiff, TIFFTAG_ORIENTATION, orientation);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_bpp == 1)
Shinya Kitaoka 120a6e
    TIFFSetField(m_tiff, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    std::wstring compressionType =
Shinya Kitaoka 120a6e
        ((TEnumProperty *)(m_properties->getProperty("Compression Type")))
Shinya Kitaoka 120a6e
            ->getValue();
Shinya Kitaoka 120a6e
    if (compressionType == TNZ_INFO_COMPRESS_LZW)
Shinya Kitaoka 120a6e
      TIFFSetField(m_tiff, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
Shinya Kitaoka 120a6e
    else if (compressionType == TNZ_INFO_COMPRESS_PACKBITS)
Shinya Kitaoka 120a6e
      TIFFSetField(m_tiff, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
Shinya Kitaoka 120a6e
    else if (compressionType == TNZ_INFO_COMPRESS_CCITTFAX3)
Shinya Kitaoka 120a6e
      TIFFSetField(m_tiff, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX3);
Shinya Kitaoka 120a6e
    else if (compressionType == TNZ_INFO_COMPRESS_CCITTFAX4)
Shinya Kitaoka 120a6e
      TIFFSetField(m_tiff, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
Shinya Kitaoka 120a6e
    else if (compressionType == TNZ_INFO_COMPRESS_CCITTRLE)
Shinya Kitaoka 120a6e
      TIFFSetField(m_tiff, TIFFTAG_COMPRESSION, COMPRESSION_CCITTRLE);
Shinya Kitaoka 120a6e
    else if (compressionType == TNZ_INFO_COMPRESS_JPEG)
Shinya Kitaoka 120a6e
      TIFFSetField(m_tiff, TIFFTAG_COMPRESSION, COMPRESSION_JPEG);
Shinya Kitaoka 120a6e
    else if (compressionType == TNZ_INFO_COMPRESS_OJPEG)
Shinya Kitaoka 120a6e
      TIFFSetField(m_tiff, TIFFTAG_COMPRESSION, COMPRESSION_OJPEG);
Shinya Kitaoka 120a6e
    else if (compressionType == TNZ_INFO_COMPRESS_NONE)
Shinya Kitaoka 120a6e
      TIFFSetField(m_tiff, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
Shinya Kitaoka 120a6e
    else if (compressionType == TNZ_INFO_COMPRESS_THUNDERSCAN)
Shinya Kitaoka 120a6e
      TIFFSetField(m_tiff, TIFFTAG_COMPRESSION, COMPRESSION_THUNDERSCAN);
Shinya Kitaoka 120a6e
    else if (compressionType == TNZ_INFO_COMPRESS_ADOBE_DEFLATE)
Shinya Kitaoka 120a6e
      TIFFSetField(m_tiff, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
Shinya Kitaoka 120a6e
    else if (compressionType == TNZ_INFO_COMPRESS_DEFLATE)
Shinya Kitaoka 120a6e
      TIFFSetField(m_tiff, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      assert(false);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TIFFSetField(m_tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
Shinya Kitaoka 120a6e
  TIFFSetField(m_tiff, TIFFTAG_PHOTOMETRIC, (m_bpp == 8 || m_bpp == 1)
Shinya Kitaoka 120a6e
                                                ? PHOTOMETRIC_MINISBLACK
Shinya Kitaoka 120a6e
                                                : PHOTOMETRIC_RGB);
Shinya Kitaoka 120a6e
  TIFFSetField(m_tiff, TIFFTAG_XRESOLUTION, m_info.m_dpix);
Shinya Kitaoka 120a6e
  TIFFSetField(m_tiff, TIFFTAG_YRESOLUTION, m_info.m_dpiy);
Shinya Kitaoka 120a6e
  TIFFSetField(m_tiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
Shinya Kitaoka 120a6e
  TIFFSetField(m_tiff, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(m_tiff, 0));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_row = 0;
Shinya Kitaoka 120a6e
  if (m_bpp == 1)
Shinya Kitaoka 120a6e
    m_lineBuffer = new unsigned char[m_info.m_lx / 8 + 1];
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    m_lineBuffer =
Shinya Kitaoka 120a6e
        new unsigned char[(m_bpp == 1 ? 1 : m_bpp / 8) * m_info.m_lx];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TifWriter::flush() { TIFFFlush(m_tiff); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TifWriter::writeLine(short *buffer) {
Shinya Kitaoka 120a6e
  int delta = 1;
Shinya Kitaoka 120a6e
  int start = 0;
Shinya Kitaoka 120a6e
  if (m_RightToLeft) {
Shinya Kitaoka 120a6e
    delta = -1;
Shinya Kitaoka 120a6e
    start = m_info.m_lx - 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (m_bpp == 16) {
Shinya Kitaoka 120a6e
    unsigned short *pix = ((unsigned short *)buffer) + start;
Shinya Kitaoka 120a6e
    for (int i = 0; i < m_info.m_lx; i++) {
Shinya Kitaoka 120a6e
      unsigned short *b = (unsigned short *)m_lineBuffer + i * 2;
Shinya Kitaoka 120a6e
      b[0]              = pix[0];
Shinya Kitaoka 120a6e
      b[1]              = pix[1];
Shinya Kitaoka 120a6e
      pix               = pix + delta;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    assert(m_bpp == 48 || m_bpp == 64);
Shinya Kitaoka 120a6e
    TPixel64 *pix = ((TPixel64 *)buffer) + start;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (m_bpp == 64)
Shinya Kitaoka 120a6e
      for (int i = 0; i < m_info.m_lx; i++) {
Shinya Kitaoka 120a6e
        unsigned short *b = (unsigned short *)m_lineBuffer + i * 4;
Shinya Kitaoka 120a6e
        b[0]              = pix->r;
Shinya Kitaoka 120a6e
        b[1]              = pix->g;
Shinya Kitaoka 120a6e
        b[2]              = pix->b;
Shinya Kitaoka 120a6e
        b[3]              = pix->m;
Shinya Kitaoka 120a6e
        pix               = pix + delta;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    else if (m_bpp == 48)
Shinya Kitaoka 120a6e
      for (int i = 0; i < m_info.m_lx; i++) {
Shinya Kitaoka 120a6e
        unsigned short *b = (unsigned short *)m_lineBuffer + i * 3;
Shinya Kitaoka 120a6e
        b[0]              = pix->r;
Shinya Kitaoka 120a6e
        b[1]              = pix->g;
Shinya Kitaoka 120a6e
        b[2]              = pix->b;
Shinya Kitaoka 120a6e
        pix               = pix + delta;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TIFFWriteScanline(m_tiff, m_lineBuffer, m_row++, 0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TifWriter::fillBits(UCHAR *bufout, UCHAR *bufin, int lx, int incr) {
Shinya Kitaoka 120a6e
  int lx1 = lx / 8 + ((lx % 8) ? 1 : 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int i = 0; i < lx1; i++, bufout++) {
Shinya Kitaoka 120a6e
    UCHAR pix = 0xff;
Shinya Kitaoka 120a6e
    for (int j = 0; j < 8; j++, bufin += incr)
Shinya Kitaoka 120a6e
      if (*bufin < m_bwThreshold) pix &= ~(0x1 << (7 - j));
Shinya Kitaoka 120a6e
    *bufout = pix;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TifWriter::writeLine(char *buffer) {
Shinya Kitaoka 120a6e
  int delta = 1;
Shinya Kitaoka 120a6e
  int start = 0;
Shinya Kitaoka 120a6e
  if (m_RightToLeft) {
Shinya Kitaoka 120a6e
    delta = -1;
Shinya Kitaoka 120a6e
    start = m_info.m_lx - 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (m_bpp == 1)
Shinya Kitaoka 120a6e
    fillBits(m_lineBuffer, ((unsigned char *)buffer) + start, m_info.m_lx,
Shinya Kitaoka 120a6e
             delta);
Shinya Kitaoka 120a6e
  else if (m_bpp == 8) {
Shinya Kitaoka 120a6e
    unsigned char *pix = ((unsigned char *)buffer) + start;
Shinya Kitaoka 120a6e
    for (int i = 0; i < m_info.m_lx; i++) {
Shinya Kitaoka 120a6e
      unsigned char *b = m_lineBuffer + i;
Shinya Kitaoka 120a6e
      b[0]             = pix[0];
Shinya Kitaoka 120a6e
      pix              = pix + delta;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    assert(m_bpp == 24 || m_bpp == 32);
Shinya Kitaoka 120a6e
    TPixel32 *pix = ((TPixel32 *)buffer) + start;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (m_bpp == 32)
Shinya Kitaoka 120a6e
      for (int i = 0; i < m_info.m_lx; i++) {
Shinya Kitaoka 120a6e
        unsigned char *b = m_lineBuffer + i * 4;
Shinya Kitaoka 120a6e
        b[0]             = pix->r;
Shinya Kitaoka 120a6e
        b[1]             = pix->g;
Shinya Kitaoka 120a6e
        b[2]             = pix->b;
Shinya Kitaoka 120a6e
        b[3]             = pix->m;
Shinya Kitaoka 120a6e
        pix              = pix + delta;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    else if (m_bpp == 24)
Shinya Kitaoka 120a6e
      for (int i = 0; i < m_info.m_lx; i++) {
Shinya Kitaoka 120a6e
        unsigned char *b = m_lineBuffer + i * 3;
Shinya Kitaoka 120a6e
        b[0]             = pix->r;
Shinya Kitaoka 120a6e
        b[1]             = pix->g;
Shinya Kitaoka 120a6e
        b[2]             = pix->b;
Shinya Kitaoka 120a6e
        pix              = pix + delta;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TIFFWriteScanline(m_tiff, m_lineBuffer, m_row++, 0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//============================================================
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Toshihiro Shimizu 890ddd
/* Error & Waring Handler per debug */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
extern "C" {
Shinya Kitaoka 120a6e
static void MyWarningHandler(const char *module, const char *fmt, va_list ap) {
Shinya Kitaoka 120a6e
  std::string outMsg;
Shinya Kitaoka 120a6e
  char msg[2048];
Shinya Kitaoka 120a6e
  msg[0]                     = 0;
Shinya Kitaoka 120a6e
  if (module != NULL) outMsg = std::string(module);
Shinya Kitaoka 120a6e
  outMsg += "Warning, ";
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  _vsnprintf(msg, 2048, fmt, ap);
Shinya Kitaoka 120a6e
  strcat(msg, ".\n");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  outMsg += msg;
Shinya Kitaoka 120a6e
  TSystem::outputDebug(outMsg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void MyErrorHandler(const char *module, const char *fmt, va_list ap) {
Shinya Kitaoka 120a6e
  std::string outMsg;
Shinya Kitaoka 120a6e
  char msg[2048];
Shinya Kitaoka 120a6e
  msg[0]                     = 0;
Shinya Kitaoka 120a6e
  if (module != NULL) outMsg = std::string(module);
Shinya Kitaoka 120a6e
  // outMsg += "Warning, ";
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  _vsnprintf(msg, 2048, fmt, ap);
Shinya Kitaoka 120a6e
  strcat(msg, ".\n");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  outMsg += msg;
Shinya Kitaoka 120a6e
  TSystem::outputDebug(outMsg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Tiio::Reader *Tiio::makeTifReader() {
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Shinya Kitaoka 120a6e
  TIFFSetErrorHandler(MyErrorHandler);
Shinya Kitaoka 120a6e
  TIFFSetWarningHandler(MyWarningHandler);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  return new TifReader(false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Tiio::Reader *Tiio::makeTziReader() {
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Shinya Kitaoka 120a6e
  TIFFSetErrorHandler(MyErrorHandler);
Shinya Kitaoka 120a6e
  TIFFSetWarningHandler(MyWarningHandler);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  return new TifReader(true);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Tiio::Writer *Tiio::makeTifWriter() {
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Shinya Kitaoka 120a6e
  TIFFSetErrorHandler(MyErrorHandler);
Shinya Kitaoka 120a6e
  TIFFSetWarningHandler(MyWarningHandler);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return new TifWriter();
Toshihiro Shimizu 890ddd
}