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