Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "timage_io.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
#include "tproperty.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "tiio.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
shun-iwasawa 93fdd4
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// boost includes
Toshihiro Shimizu 890ddd
#include <boost range.hpp=""></boost>
Toshihiro Shimizu 890ddd
e280ae
#ifdef _MSC_VER
e280ae
#pragma warning(disable : 4996)
e280ae
#endif
e280ae
Toshihiro Shimizu 890ddd
// OS-specific includes
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#include <io.h></io.h>
Toshihiro Shimizu 890ddd
#endif
Rozhuk Ivan ac51ab
#if defined(LINUX) || defined(FREEBSD)
Toshihiro Shimizu 890ddd
#include <unistd.h></unistd.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// System V includes
Shinya Kitaoka 120a6e
#include <fcntl.h>     // didn't even know Windows had them  :D</fcntl.h>
Shinya Kitaoka 120a6e
#include <sys stat.h="">  // btw, why are they here?</sys>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
DEFINE_CLASS_CODE(TImageReader, 5)
Toshihiro Shimizu 890ddd
DEFINE_CLASS_CODE(TImageWriter, 6)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
std::map<qstring, *="" timagereadercreateproc=""> ImageReaderTable;</qstring,>
Toshihiro Shimizu 890ddd
std::map<qstring, *,="" bool="" std::pair<timagewritercreateproc="">> ImageWriterTable;</qstring,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TImageReader::m_safeMode = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageReader::TImageReader(const TFilePath &path)
Shinya Kitaoka 120a6e
    : TSmartObject(m_classCode)
Shinya Kitaoka 120a6e
    , m_path(path)
Shinya Kitaoka 120a6e
    , m_reader(0)
Shinya Kitaoka 120a6e
    , m_vectorReader(0)
Shinya Kitaoka 120a6e
    , m_readGreytones(true)
Shinya Kitaoka 120a6e
    , m_file(NULL)
Shinya Kitaoka 120a6e
    , m_is64BitEnabled(false)
Shinya Kitaoka 120a6e
    , m_shrink(1)
Shinya Kitaoka 120a6e
    , m_region(TRect()) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageReader::doReadGraytones(bool readThem) {
Shinya Kitaoka 120a6e
  m_readGreytones = readThem;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TImageReader::~TImageReader() { close(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TImageReader::isOpen() const { return m_file != NULL; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageReader::close() {
Shinya Kitaoka 120a6e
  delete m_reader;
Shinya Kitaoka 120a6e
  delete m_vectorReader;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (m_file != NULL) {
Shinya Kitaoka 120a6e
    int err = 0;
Shinya Kitaoka 120a6e
    err     = fclose(m_file);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    assert(err == 0);  // il file e' stato chiuso senza errori
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_file         = NULL;
Shinya Kitaoka 120a6e
  m_reader       = 0;
Shinya Kitaoka 120a6e
  m_vectorReader = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Opens for reading using \b fopen().
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
void TImageReader::getTzpPaletteColorNames(
Shinya Kitaoka 120a6e
    std::map<int, std::pair<std::string,="" std::string="">> &pltColorNames) {</int,>
Shinya Kitaoka 120a6e
  if (!m_file) open();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!m_file) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(m_reader);
Shinya Kitaoka 120a6e
  m_reader->getTzpPaletteColorNames(pltColorNames);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageReader::open() {
Shinya Kitaoka 120a6e
  assert(m_file == NULL);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::string type = toLower(m_path.getType());
Shinya Kitaoka 38fd86
  m_file = fopen(m_path, "rb");  // Opens for reading. If the file does not
Shinya Kitaoka 120a6e
                                 // exist or cannot be found, the fopen_s call
Shinya Kitaoka 120a6e
                                 // fails
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_file == NULL)  // Non dovrebbe mai andarci!
Shinya Kitaoka 120a6e
    close();  // close() chiude il file se e' aperto e setta m_file a NULL.
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      m_reader = Tiio::makeReader(type);
Shinya Kitaoka 120a6e
      if (m_reader)
Shinya Kitaoka 120a6e
        m_reader->open(m_file);
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        m_vectorReader = Tiio::makeVectorReader(type);
Shinya Kitaoka 120a6e
        if (m_vectorReader)
Shinya Kitaoka 120a6e
          m_vectorReader->open(m_file);
Shinya Kitaoka 120a6e
        else
Shinya Kitaoka 120a6e
          throw TImageException(m_path, "Image format not supported");
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } catch (TException &e) {
Shinya Kitaoka 120a6e
      close();  // close() chiude il file e setta m_file a NULL.
Shinya Kitaoka 120a6e
      QString msg = QString::fromStdWString(e.getMessage());
Shinya Kitaoka 120a6e
      if (msg == QString("Old 4.1 Palette")) throw e;
Shinya Kitaoka 120a6e
    } catch (std::string str) {
Shinya Kitaoka 120a6e
      if (str == "Tiff file closed") m_file = NULL;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageReader::setShrink(int shrink) { m_shrink = shrink; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TImageReaderP::TImageReaderP(const TFilePath &path) {
Shinya Kitaoka 120a6e
  m_pointer = new TImageReader(path);
Shinya Kitaoka 120a6e
  m_pointer->addRef();
Shinya Kitaoka 120a6e
  m_pointer->open();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TImageP TImageReader::load() {
Shinya Kitaoka 120a6e
  TImageP image = load0();
Shinya Kitaoka 120a6e
  if (!image) return TImageP();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TImageInfo info = m_reader->getImageInfo();
Shinya Kitaoka 120a6e
  if (info.m_lx <= 0 || info.m_ly <= 0) return TImageP();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return image;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//============================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename pix=""></typename>
Shinya Kitaoka 120a6e
struct pixel_traits {};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <>
Toshihiro Shimizu 890ddd
struct pixel_traits<tpixel32> {</tpixel32>
Shinya Kitaoka 120a6e
  typedef TPixel32 rgbm_pixel_type;
Shinya Kitaoka 120a6e
  typedef char buffer_type;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <>
Toshihiro Shimizu 890ddd
struct pixel_traits<tpixel64> {</tpixel64>
Shinya Kitaoka 120a6e
  typedef TPixel64 rgbm_pixel_type;
Shinya Kitaoka 120a6e
  typedef short buffer_type;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <>
Toshihiro Shimizu 890ddd
struct pixel_traits<tpixelgr8> {</tpixelgr8>
Shinya Kitaoka 120a6e
  typedef TPixel32 rgbm_pixel_type;
Shinya Kitaoka 120a6e
  typedef char buffer_type;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <>
Toshihiro Shimizu 890ddd
struct pixel_traits<tpixelgr16> {</tpixelgr16>
Shinya Kitaoka 120a6e
  typedef TPixel64 rgbm_pixel_type;
Shinya Kitaoka 120a6e
  typedef short buffer_type;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <>
Toshihiro Shimizu 890ddd
struct pixel_traits<tpixelcm32> {</tpixelcm32>
Shinya Kitaoka 120a6e
  typedef TPixel32 rgbm_pixel_type;
Shinya Kitaoka 120a6e
  typedef char buffer_type;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename pix=""></typename>
Shinya Kitaoka 120a6e
void copyLine(typename pixel_traits<pix>::rgbm_pixel_type *lineIn, Pix *lineOut,</pix>
Shinya Kitaoka 120a6e
              int x0, int length, int shrink) {
Shinya Kitaoka 120a6e
  lineIn += x0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (int i = 0; i < length; ++i, lineIn += shrink, ++lineOut)
Shinya Kitaoka 120a6e
    memcpy(lineOut, lineIn, sizeof(Pix));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <>
Shinya Kitaoka 120a6e
void copyLine<tpixelgr8>(TPixel32 *lineIn, TPixelGR8 *lineOut, int x0,</tpixelgr8>
Shinya Kitaoka 120a6e
                         int length, int shrink) {
Shinya Kitaoka 120a6e
  lineIn += x0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (int i = 0; i < length; ++i, lineIn += shrink, ++lineOut)
Shinya Kitaoka 120a6e
    lineOut->value = lineIn->r;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <>
Shinya Kitaoka 120a6e
void copyLine<tpixelgr16>(TPixel64 *lineIn, TPixelGR16 *lineOut, int x0,</tpixelgr16>
Shinya Kitaoka 120a6e
                          int length, int shrink) {
Shinya Kitaoka 120a6e
  lineIn += x0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (int i = 0; i < length; ++i, lineIn += shrink, ++lineOut)
Shinya Kitaoka 120a6e
    lineOut->value = lineIn->r;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename pix=""></typename>
Toshihiro Shimizu 890ddd
void readRaster_copyLines(const TRasterPT<pix> &ras, Tiio::Reader *reader,</pix>
Shinya Kitaoka 120a6e
                          int x0, int y0, int x1, int y1, int inLx, int inLy,
Shinya Kitaoka 120a6e
                          int shrink) {
Shinya Kitaoka 120a6e
  typedef typename pixel_traits<pix>::buffer_type buffer_type;</pix>
Shinya Kitaoka 120a6e
  typedef typename pixel_traits<pix>::rgbm_pixel_type rgbm_pixel_type;</pix>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int linesToSkip = shrink - 1;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  buffer_type *lineBuffer =
Shinya Kitaoka 120a6e
      (buffer_type *)malloc(inLx * sizeof(rgbm_pixel_type));
Shinya Kitaoka 120a6e
  if (!lineBuffer) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  rgbm_pixel_type *lineIn = (rgbm_pixel_type *)lineBuffer;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (reader->getRowOrder() == Tiio::BOTTOM2TOP) {
Shinya Kitaoka 120a6e
    int start = reader->skipLines(y0);
Shinya Kitaoka 120a6e
    int stop  = y1 + 1;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    for (int y = start; y < stop; ++y) {
Shinya Kitaoka 120a6e
      reader->readLine(lineBuffer, x0, x1, shrink);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      if (y >= y0 && y <= y1 && (y - y0) % shrink == 0) {
Shinya Kitaoka 120a6e
        Pix *line = (Pix *)ras->getRawData(0, (y - y0) / shrink);
Shinya Kitaoka 120a6e
        copyLine<pix>(lineIn, line, x0, ras->getLx(), shrink);</pix>
Shinya Kitaoka 120a6e
      }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      if (linesToSkip > 0 && y + linesToSkip < inLy)
Shinya Kitaoka 120a6e
        y += reader->skipLines(linesToSkip);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else  // TOP2BOTTOM
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    reader->skipLines(inLy - y1 - 1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    for (int y = y1; y >= y0; --y) {
Shinya Kitaoka 120a6e
      reader->readLine(lineBuffer, x0, x1, shrink);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      if ((y - y0) % shrink == 0) {
Shinya Kitaoka 120a6e
        Pix *line = (Pix *)ras->getRawData(0, (y - y0) / shrink);
Shinya Kitaoka 120a6e
        copyLine<pix>(lineIn, line, x0, ras->getLx(), shrink);</pix>
Shinya Kitaoka 120a6e
      }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      if (linesToSkip > 0 && y - linesToSkip > 0)
Shinya Kitaoka 120a6e
        y -= reader->skipLines(linesToSkip);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  free(lineBuffer);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename pix=""></typename>
Shinya Kitaoka 120a6e
void readRaster(const TRasterPT<pix> &ras, Tiio::Reader *reader, int x0, int y0,</pix>
Shinya Kitaoka 120a6e
                int x1, int y1, int inLx, int inLy, int shrink) {
Shinya Kitaoka 120a6e
  typedef typename pixel_traits<pix>::buffer_type buffer_type;</pix>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (shrink == 1) {
Shinya Kitaoka 120a6e
    // Direct read
Shinya Kitaoka 120a6e
    ras->lock();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    ptrdiff_t linePad = -x0 * ras->getPixelSize();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (reader->getRowOrder() == Tiio::BOTTOM2TOP) {
Shinya Kitaoka 120a6e
      int start = reader->skipLines(y0);
Shinya Kitaoka 120a6e
      int stop  = y1 + 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      for (int y = start; y < stop; ++y)
Shinya Kitaoka 120a6e
        if (y >= y0 && y <= y1) {
Shinya Kitaoka 120a6e
          buffer_type *line =
Shinya Kitaoka 120a6e
              (buffer_type *)(ras->getRawData(0, y - y0) + linePad);
Shinya Kitaoka 120a6e
          reader->readLine(line, x0, x1, 1);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
    } else  // TOP2BOTTOM
Shinya Kitaoka 120a6e
    {
Shinya Kitaoka 120a6e
      reader->skipLines(inLy - y1 - 1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      for (int y = y1; y >= y0; --y) {
Shinya Kitaoka 120a6e
        buffer_type *line =
Shinya Kitaoka 120a6e
            (buffer_type *)(ras->getRawData(0, y - y0) + linePad);
Shinya Kitaoka 120a6e
        reader->readLine(line, x0, x1, 1);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    ras->unlock();
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    readRaster_copyLines(ras, reader, x0, y0, x1, y1, inLx, inLy, shrink);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TImageP TImageReader::load0() {
Shinya Kitaoka 120a6e
  if (!m_reader && !m_vectorReader) open();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_reader) {
Shinya Kitaoka 120a6e
    TImageInfo info = m_reader->getImageInfo();
Shinya Kitaoka 120a6e
    if (info.m_lx <= 0 || info.m_ly <= 0) return TImageP();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // Initialize raster info
Shinya Kitaoka 120a6e
    assert(m_shrink > 0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    // Build loading rect
Shinya Kitaoka 120a6e
    int x0 = 0;
Shinya Kitaoka 120a6e
    int x1 = info.m_lx - 1;
Shinya Kitaoka 120a6e
    int y0 = 0;
Shinya Kitaoka 120a6e
    int y1 = info.m_ly - 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (!m_region.isEmpty()) {
Shinya Kitaoka 120a6e
      // Intersect with the externally specified loading region
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      x0 = std::max(x0, m_region.x0);
Shinya Kitaoka 120a6e
      y0 = std::max(y0, m_region.y0);
Shinya Kitaoka 120a6e
      x1 = std::min(x1, m_region.x1);
Shinya Kitaoka 120a6e
      y1 = std::min(y1, m_region.y1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      if (x0 >= x1 || y0 >= y1) return TImageP();
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (m_shrink > 1) {
Shinya Kitaoka 120a6e
      // Crop to shrink multiples
Shinya Kitaoka 120a6e
      x1 -= (x1 - x0) % m_shrink;
Shinya Kitaoka 120a6e
      y1 -= (y1 - y0) % m_shrink;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    assert(x0 <= x1 && y0 <= y1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TDimension imageDimension =
Shinya Kitaoka 120a6e
        TDimension((x1 - x0) / m_shrink + 1, (y1 - y0) / m_shrink + 1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (m_path.getType() == "tzp" || m_path.getType() == "tzu") {
Shinya Kitaoka 120a6e
      // Colormap case
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TRasterCM32P ras(imageDimension);
Shinya Kitaoka 120a6e
      readRaster(ras, m_reader, x0, y0, x1, y1, info.m_lx, info.m_ly, m_shrink);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      // Build the savebox
Shinya Kitaoka 120a6e
      TRect saveBox(info.m_x0, info.m_y0, info.m_x1, info.m_y1);
Shinya Kitaoka 120a6e
      if (!m_region.isEmpty()) {
Shinya Kitaoka 120a6e
        // Intersect with the loading rect
Shinya Kitaoka 120a6e
        if (m_region.overlaps(saveBox)) {
Shinya Kitaoka 120a6e
          saveBox *= m_region;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          int sbx0 = saveBox.x0 - m_region.x0;
Shinya Kitaoka 120a6e
          int sby0 = saveBox.y0 - m_region.y0;
Shinya Kitaoka 120a6e
          int sbx1 = sbx0 + saveBox.getLx() - 1;
Shinya Kitaoka 120a6e
          int sby1 = sby0 + saveBox.getLy() - 1;
Shinya Kitaoka 120a6e
          assert(sbx0 >= 0);
Shinya Kitaoka 120a6e
          assert(sby0 >= 0);
Shinya Kitaoka 120a6e
          assert(sbx1 >= 0);
Shinya Kitaoka 120a6e
          assert(sby1 >= 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          saveBox = TRect(sbx0, sby0, sbx1, sby1);
Shinya Kitaoka 120a6e
        } else
Shinya Kitaoka 120a6e
          saveBox = TRect(0, 0, 1, 1);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      if (m_shrink > 1) {
Shinya Kitaoka 120a6e
        saveBox.x0 = saveBox.x0 / m_shrink;
Shinya Kitaoka 120a6e
        saveBox.y0 = saveBox.y0 / m_shrink;
Shinya Kitaoka 120a6e
        saveBox.x1 = saveBox.x1 / m_shrink;
Shinya Kitaoka 120a6e
        saveBox.y1 = saveBox.y1 / m_shrink;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TToonzImageP ti(ras, ras->getBounds() * saveBox);
Shinya Kitaoka 120a6e
      ti->setDpi(info.m_dpix, info.m_dpiy);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      return ti;
Shinya Kitaoka 120a6e
    } else if (info.m_bitsPerSample >= 8) {
Shinya Kitaoka 120a6e
      // Common byte-based rasters (see below, we have black-and-white bit-based
Shinya Kitaoka 120a6e
      // images too)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      if (info.m_samplePerPixel == 1 && m_readGreytones) {
Shinya Kitaoka 120a6e
        //  Greymap case
Shinya Kitaoka 120a6e
        // NOTE: Uses a full 32-bit raster first, and then copies down to the
Shinya Kitaoka 120a6e
        // GR8
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        // Observe that GR16 file images get immediately down-cast to GR8...
Shinya Kitaoka 120a6e
        // Should we implement that too?
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        TRasterGR8P ras(imageDimension);
Shinya Kitaoka 120a6e
        readRaster_copyLines(ras, m_reader, x0, y0, x1, y1, info.m_lx,
Shinya Kitaoka 120a6e
                             info.m_ly, m_shrink);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        TRasterImageP ri(ras);
Shinya Kitaoka 120a6e
        ri->setDpi(info.m_dpix, info.m_dpiy);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        return ri;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      // assert(info.m_samplePerPixel == 3 || info.m_samplePerPixel == 4);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TRasterP _ras;
shun-iwasawa 7bad49
      // currently m_bitsPerSample == 32 is only possible when loading
shun-iwasawa 7bad49
      // full-float / uint EXR images
shun-iwasawa 7bad49
      if (info.m_bitsPerSample == 16 || info.m_bitsPerSample == 32) {
Shinya Kitaoka 120a6e
        if (m_is64BitEnabled || m_path.getType() != "tif") {
Shinya Kitaoka 120a6e
          //  Standard 64-bit case.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          // Also covers down-casting to 32-bit from a 64-bit image file
Shinya Kitaoka 120a6e
          // whenever
Shinya Kitaoka 120a6e
          // not a tif file (see below).
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          TRaster64P ras(imageDimension);
Shinya Kitaoka 120a6e
          readRaster(ras, m_reader, x0, y0, x1, y1, info.m_lx, info.m_ly,
Shinya Kitaoka 120a6e
                     m_shrink);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          _ras = ras;
Shinya Kitaoka 120a6e
        } else {
Shinya Kitaoka 120a6e
          // The Tif reader has got an automatically down-casting
Shinya Kitaoka 120a6e
          // readLine(char*, ...)
Shinya Kitaoka 120a6e
          // in case the input file is 64-bit. The advantage is that no
Shinya Kitaoka 120a6e
          // intermediate
Shinya Kitaoka 120a6e
          // 64-bit raster is required in this case.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          TRaster32P ras(imageDimension);
Shinya Kitaoka 120a6e
          readRaster(ras, m_reader, x0, y0, x1, y1, info.m_lx, info.m_ly,
Shinya Kitaoka 120a6e
                     m_shrink);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          _ras = ras;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      } else if (info.m_bitsPerSample == 8) {
Shinya Kitaoka 120a6e
        //  Standard 32-bit case
Shinya Kitaoka 120a6e
        TRaster32P ras(imageDimension);
Shinya Kitaoka 120a6e
        readRaster(ras, m_reader, x0, y0, x1, y1, info.m_lx, info.m_ly,
Shinya Kitaoka 120a6e
                   m_shrink);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        _ras = ras;
Shinya Kitaoka 120a6e
      } else
Shinya Kitaoka 120a6e
        throw TImageException(m_path, "Image format not supported");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      // 64-bit to 32-bit conversions if necessary  (64 bit images not allowed)
Shinya Kitaoka 120a6e
      if (!m_is64BitEnabled && (TRaster64P)_ras) {
Shinya Kitaoka 120a6e
        TRaster32P raux(_ras->getLx(), _ras->getLy());
Shinya Kitaoka 120a6e
        TRop::convert(raux, _ras);
Shinya Kitaoka 120a6e
        _ras = raux;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      // Return the image
Shinya Kitaoka 120a6e
      TRasterImageP ri(_ras);
Shinya Kitaoka 120a6e
      ri->setDpi(info.m_dpix, info.m_dpiy);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      return ri;
Shinya Kitaoka 120a6e
    } else if (info.m_samplePerPixel == 1 && info.m_valid == true) {
Shinya Kitaoka 120a6e
      //  Previously dubbed as 'Palette cases'. No clue about what is this... :|
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TRaster32P ras(imageDimension);
Shinya Kitaoka 120a6e
      readRaster(ras, m_reader, x0, y0, x1, y1, info.m_lx, info.m_ly, m_shrink);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TRasterImageP ri(ras);
Shinya Kitaoka 120a6e
      ri->setDpi(info.m_dpix, info.m_dpiy);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      return ri;
Shinya Kitaoka 120a6e
    } else if (info.m_samplePerPixel == 1 && m_readGreytones) {
Shinya Kitaoka 120a6e
      //  Black-and-White case, I guess. Standard greymaps were considered
Shinya Kitaoka 120a6e
      //  above...
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TRasterGR8P ras(imageDimension);
Shinya Kitaoka 120a6e
      readRaster_copyLines(ras, m_reader, x0, y0, x1, y1, info.m_lx, info.m_ly,
Shinya Kitaoka 120a6e
                           m_shrink);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TRasterImageP ri(ras);
Shinya Kitaoka 120a6e
      ri->setDpi(info.m_dpix, info.m_dpiy);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      if (info.m_bitsPerSample == 1)  // I suspect this always evaluates true...
Shinya Kitaoka 120a6e
        ri->setScanBWFlag(true);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      return ri;
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      return TImageP();
Shinya Kitaoka 120a6e
  } else if (m_vectorReader) {
Shinya Kitaoka 120a6e
    TVectorImage *vi = m_vectorReader->read();
Shinya Kitaoka 120a6e
    return TVectorImageP(vi);
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    return TImageP();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageWriter::TImageWriter(const TFilePath &path)
Shinya Kitaoka 120a6e
    : TSmartObject(m_classCode)
Shinya Kitaoka 120a6e
    , m_path(path)
Shinya Kitaoka 120a6e
    , m_writer(0)
Shinya Kitaoka 120a6e
    , m_vectorWriter(0)
Shinya Kitaoka 120a6e
    , m_properties(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TImageWriter::~TImageWriter() {
Shinya Kitaoka 120a6e
  delete m_writer;
Shinya Kitaoka 120a6e
  delete m_vectorWriter;
Shinya Kitaoka 120a6e
  delete m_properties;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageWriter::setProperties(const TPropertyGroup *g) {
Shinya Kitaoka 120a6e
  if (m_properties) {
Shinya Kitaoka 120a6e
    assert(m_properties != g);
Shinya Kitaoka 120a6e
    delete m_properties;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_properties = g ? g->clone() : 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static void convertForWriting(TRasterP &ras, const TRasterP &rin, int bpp) {
Shinya Kitaoka 120a6e
  switch (bpp) {
Shinya Kitaoka 120a6e
  case 1:
Shinya Kitaoka 120a6e
  case 8:
Shinya Kitaoka 120a6e
    ras = TRasterGR8P(rin->getSize());
Shinya Kitaoka 120a6e
    TRop::convert(ras, rin);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case 24:
Shinya Kitaoka 120a6e
  case 32:
Shinya Kitaoka 120a6e
    ras = TRaster32P(rin->getSize());
Shinya Kitaoka 120a6e
    TRop::convert(ras, rin);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case 48:
Shinya Kitaoka 120a6e
  case 64:
Shinya Kitaoka 120a6e
    ras = TRaster64P(rin->getSize());
Shinya Kitaoka 120a6e
    TRop::convert(ras, rin);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  default:
Shinya Kitaoka 120a6e
    assert(false);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageWriter::save(const TImageP &img) {
Shinya Kitaoka 120a6e
  const std::string &type = toLower(m_path.getType());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Tiio::Writer *writer = Tiio::makeWriter(type);
Shinya Kitaoka 120a6e
  if (!writer)
Shinya Kitaoka 120a6e
    throw TImageException(m_path, "unsupported format for raster images");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  writer->setProperties(m_properties);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  FILE *file = fopen(m_path, "wb");
Shinya Kitaoka 120a6e
  if (file == NULL) throw TImageException(m_path, "Can't write file");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (TRasterImageP ri = img) {
Shinya Kitaoka 120a6e
    TImageInfo info;
Shinya Kitaoka 120a6e
    TRasterP ras;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRasterGR8P rasGr = ri->getRaster();
Shinya Kitaoka 120a6e
    TRaster32P ras32  = ri->getRaster();
Shinya Kitaoka 120a6e
    TRaster64P ras64  = ri->getRaster();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TEnumProperty *p =
Shinya Kitaoka 120a6e
        m_properties
Shinya Kitaoka 120a6e
            ? (TEnumProperty *)m_properties->getProperty("Bits Per Pixel")
Shinya Kitaoka 120a6e
            : 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (p && ri->isScanBW()) {
Shinya Kitaoka 120a6e
      const std::vector<std::wstring> &range = p->getRange();</std::wstring>
Shinya Kitaoka 120a6e
      p->setValue(range[2]);  // Horrible. See tiio_tif.cpp (732 or near) -.-'
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    int bpp = p ? std::stoi(p->getValue()) : 32;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    //  bpp       1  8  16 24 32 40  48 56  64
Shinya Kitaoka 120a6e
    int spp[] = {
Shinya Kitaoka 120a6e
        1, 1, 1, 4, 4,
Shinya Kitaoka 120a6e
        0, 4, 0, 4};  // 0s are for pixel sizes which are normally unsupported
Shinya Kitaoka 120a6e
    int bps[] = {
Shinya Kitaoka 120a6e
        1, 8,  16, 8, 8,
Shinya Kitaoka 120a6e
        0, 16, 0,  16};  // by image formats, let alone by Toonz raster ones.
Shinya Kitaoka 120a6e
    // The 24 and 48 cases get automatically promoted to 32 and 64.
Shinya Kitaoka 120a6e
    int bypp = bpp / 8;
Shinya Kitaoka 120a6e
    assert(bypp < boost::size(spp) && spp[bypp] && bps[bypp]);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    info.m_samplePerPixel = spp[bypp];
Shinya Kitaoka 120a6e
    info.m_bitsPerSample  = bps[bypp];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (rasGr) {
Shinya Kitaoka 120a6e
      if (bypp < 2)   // Seems 16 bit greymaps are not handled... why?
Shinya Kitaoka 120a6e
        ras = rasGr;  // we do have a Toonz raster for those...    >:|
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        convertForWriting(ras, rasGr, bpp);
Shinya Kitaoka 120a6e
    } else if (ras32) {
Shinya Kitaoka 120a6e
      if (bpp == 32 || bpp == 24)
Shinya Kitaoka 120a6e
        ras = ras32;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        convertForWriting(ras, ras32, bpp);
Shinya Kitaoka 120a6e
    } else if (ras64) {
Shinya Kitaoka 120a6e
      if (bpp == 64 || bpp == 48)
Shinya Kitaoka 120a6e
        ras = ras64;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        convertForWriting(ras, ras64, bpp);
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      fclose(file);
Shinya Kitaoka 120a6e
      throw TImageException(m_path, "unsupported raster type");
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    info.m_lx = ras->getLx();
Shinya Kitaoka 120a6e
    info.m_ly = ras->getLy();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    ri->getDpi(info.m_dpix, info.m_dpiy);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (writer->getProperties() && m_properties)
Shinya Kitaoka 120a6e
      writer->getProperties()->setProperties(m_properties);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    writer->open(file, info);
Shinya Kitaoka 120a6e
shun-iwasawa 93fdd4
    // add background colors for non alpha-enabled image types
shun-iwasawa 93fdd4
    if ((ras32 || ras64) && !writer->writeAlphaSupported() &&
shun-iwasawa 93fdd4
        TImageWriter::getBackgroundColor() != TPixel::Black) {
shun-iwasawa 0c69c1
      if (bpp == 32 || bpp == 24)
shun-iwasawa 93fdd4
        TRop::addBackground(ras, TImageWriter::getBackgroundColor());
shun-iwasawa 0c69c1
      else if (bpp == 64 || bpp == 48) {
shun-iwasawa 93fdd4
        TRaster64P bgRas(ras->getSize());
shun-iwasawa 93fdd4
        bgRas->fill(toPixel64(TImageWriter::getBackgroundColor()));
shun-iwasawa 93fdd4
        TRop::over(bgRas, ras);
shun-iwasawa 93fdd4
        ras = bgRas;
shun-iwasawa 0c69c1
      }  // for other bpp values, do nothing for now
shun-iwasawa 93fdd4
    }
shun-iwasawa 93fdd4
Shinya Kitaoka 120a6e
    ras->lock();
shun-iwasawa e6b124
Shinya Kitaoka 120a6e
    if (writer->getRowOrder() == Tiio::BOTTOM2TOP) {
Shinya Kitaoka 120a6e
      if (bpp == 1 || bpp == 8 || bpp == 24 || bpp == 32 || bpp == 16)
Shinya Kitaoka 120a6e
        for (int i = 0; i < ras->getLy(); i++)
Shinya Kitaoka 120a6e
          writer->writeLine((char *)ras->getRawData(0, i));
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        for (int i = 0; i < ras->getLy(); i++)
Shinya Kitaoka 120a6e
          writer->writeLine((short *)ras->getRawData(0, i));
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      if (bpp == 1 || bpp == 8 || bpp == 24 || bpp == 32 || bpp == 16)
Shinya Kitaoka 120a6e
        for (int i = ras->getLy() - 1; i >= 0; i--)
Shinya Kitaoka 120a6e
          writer->writeLine((char *)ras->getRawData(0, i));
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        for (int i = ras->getLy() - 1; i >= 0; i--)
Shinya Kitaoka 120a6e
          writer->writeLine((short *)ras->getRawData(0, i));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    ras->unlock();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    writer->flush();
Shinya Kitaoka 120a6e
    delete writer;
Shinya Kitaoka 120a6e
  } else if (TVectorImageP vi = img) {
Shinya Kitaoka 120a6e
    Tiio::VectorWriter *writer = Tiio::makeVectorWriter(type);
Shinya Kitaoka 120a6e
    if (!writer) {
Shinya Kitaoka 120a6e
      fclose(file);
Shinya Kitaoka 120a6e
      throw TImageException(m_path, "unsupported format for vector images");
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    writer->open(file);
Shinya Kitaoka 120a6e
    writer->write(vi.getPointer());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    delete writer;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    fclose(file);
Shinya Kitaoka 120a6e
    throw TImageException(m_path, "Can't write file");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  fclose(file);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TImageWriterP::TImageWriterP(const TFilePath &path) {
Shinya Kitaoka 120a6e
  m_pointer = new TImageWriter(path);
Shinya Kitaoka 120a6e
  m_pointer->addRef();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//============================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// Helper functions statiche
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//============================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TImageReader::load(const TFilePath &path, TRasterP &raster) {
Shinya Kitaoka 120a6e
  raster = TRasterP();
Shinya Kitaoka 120a6e
  TImageReaderP ir(path);
Shinya Kitaoka 120a6e
  if (!ir) return false;
Shinya Kitaoka 120a6e
  TImageP img = ir->load();
Shinya Kitaoka 120a6e
  if (!img) return false;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRasterImageP ri(img);
Shinya Kitaoka 120a6e
  if (!ri) return false;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  raster = ri->getRaster();
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageReader::load(const TRasterP &ras, const TPoint &pos, int shrinkX,
Shinya Kitaoka 120a6e
                        int shrinkY) {
Shinya Kitaoka 120a6e
  TImageP srcImage          = load();
Shinya Kitaoka 120a6e
  TRasterImageP srcRasImage = srcImage;
Shinya Kitaoka 120a6e
  TRaster32P srcRaster      = srcRasImage->getRaster();
Shinya Kitaoka 120a6e
  /*
Toshihiro Shimizu 890ddd
TRaster32P clippedRas = srcRaster->extractT
Shinya Kitaoka 120a6e
  (shrinkX*pos.x, shrinkY*pos.y,
Shinya Kitaoka 120a6e
  (pos.x + ras->getLx()) * shrinkX - 1, (pos.y + ras->getLy()) * shrinkY - 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
if (shrinkX != 1 || shrinkY != 1)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
TRaster32P ras32 = ras;
Shinya Kitaoka 120a6e
if (ras32)
Shinya Kitaoka 120a6e
ras32->fill(TPixel32::Transparent);
Shinya Kitaoka 120a6e
TRop::resample(ras, clippedRas, TScale(1./shrinkX, 1./shrinkY),
Shinya Kitaoka 120a6e
TRop::ClosestPixel);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
else*/
Shinya Kitaoka 120a6e
  ras->copy(srcRaster);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TImageReader::load(const TFilePath &path, TImageP &image) {
Shinya Kitaoka 120a6e
  image = TImageReaderP(path)->load();
Shinya Kitaoka 120a6e
  return image;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageWriter::save(const TFilePath &path, TRasterP raster) {
Shinya Kitaoka 120a6e
  TRasterImageP rasImage(raster);
Shinya Kitaoka 120a6e
  TImageWriterP(path)->save(TImageP(rasImage));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageWriter::save(const TFilePath &path, const TImageP &image) {
Shinya Kitaoka 120a6e
  TImageWriterP(path)->save(image);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===========================================================
shun-iwasawa 93fdd4
// Background color for saving ransparent pixel to the format not
shun-iwasawa 93fdd4
// supporting alpha channel. Specified in the preferences.
shun-iwasawa 93fdd4
shun-iwasawa 93fdd4
TPixel32 TImageWriter::m_backgroundColor;
shun-iwasawa 93fdd4
shun-iwasawa 93fdd4
void TImageWriter::setBackgroundColor(TPixel32 color) {
shun-iwasawa 93fdd4
  m_backgroundColor = color;
shun-iwasawa 93fdd4
}
shun-iwasawa 93fdd4
shun-iwasawa 93fdd4
TPixel32 TImageWriter::getBackgroundColor() { return m_backgroundColor; }
shun-iwasawa 93fdd4
shun-iwasawa 93fdd4
//===========================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// funzioni per la registrazione dei formati (chiamate dal Plugin)
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//===========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageReader::define(QString extension, TImageReaderCreateProc *proc) {
Shinya Kitaoka 120a6e
  ImageReaderTable[extension] = proc;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageWriter::define(QString extension, TImageWriterCreateProc *proc,
Shinya Kitaoka 120a6e
                          bool isRenderFormat) {
Shinya Kitaoka 120a6e
  ImageWriterTable[extension] =
Shinya Kitaoka 120a6e
      std::pair<timagewritercreateproc *,="" bool="">(proc, isRenderFormat);</timagewritercreateproc>
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageReader::getSupportedFormats(QStringList &names) {
Shinya Kitaoka 120a6e
  for (std::map<qstring, *="" timagereadercreateproc="">::iterator it =</qstring,>
Shinya Kitaoka 120a6e
           ImageReaderTable.begin();
Shinya Kitaoka 120a6e
       it != ImageReaderTable.end(); ++it) {
Shinya Kitaoka 120a6e
    names.push_back(it->first);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageWriter::getSupportedFormats(QStringList &names,
Shinya Kitaoka 120a6e
                                       bool onlyRenderFormat) {
Shinya Kitaoka 120a6e
  for (std::map<qstring, *,="" bool="" std::pair<timagewritercreateproc="">>::iterator</qstring,>
Shinya Kitaoka 120a6e
           it = ImageWriterTable.begin();
Shinya Kitaoka 120a6e
       it != ImageWriterTable.end(); ++it) {
Shinya Kitaoka 120a6e
    if (!onlyRenderFormat || it->second.second) names.push_back(it->first);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const TImageInfo *TImageReader::getImageInfo() const {
Shinya Kitaoka 120a6e
  if (m_reader)
Shinya Kitaoka 120a6e
    return &(m_reader->getImageInfo());
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===========================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// Eccezioni
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//===========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TImageException::TImageException(const TFilePath &fp, const std::string &msg)
Shinya Kitaoka 120a6e
    : TException(msg), m_fp(fp) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TString TImageException::getMessage() const {
Shinya Kitaoka 120a6e
  return m_fp.getWideString() + L": " + TException::getMessage();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TImageVersionException::TImageVersionException(const TFilePath &fp, int major,
Shinya Kitaoka 120a6e
                                               int minor)
Jeremy Bullock e122a9
    : TException(L"The file " + fp.getWideString() +
Jeremy Bullock e122a9
                 L" was generated by a newer version of OpenToonz and cannot "
Jeremy Bullock e122a9
                 L"be loaded.")
Shinya Kitaoka 120a6e
    , m_fp(fp)
Shinya Kitaoka 120a6e
    , m_major(major)
Shinya Kitaoka 120a6e
    , m_minor(minor) {}