Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tiio_tzp.h"
Toshihiro Shimizu 890ddd
//#include "tiio.h"
Toshihiro Shimizu 890ddd
#include "trastercm.h"
Toshihiro Shimizu 890ddd
#include "toonztags.h"
Toshihiro Shimizu 890ddd
#include "texception.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tiffio.h"
Toshihiro Shimizu 890ddd
#include "tiffiop.h"
Toshihiro Shimizu 890ddd
//#include "tspecialstyleid.h"
Toshihiro Shimizu 890ddd
#include <set></set>
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
Toshihiro Shimizu 890ddd
class TzpReader : public Tiio::Reader
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIFF *m_tiff;
Toshihiro Shimizu 890ddd
	int m_row;
Toshihiro Shimizu 890ddd
	bool m_tiled, m_stripped;
Toshihiro Shimizu 890ddd
	int m_rowsPerStrip;
Toshihiro Shimizu 890ddd
	int m_stripIndex;
Toshihiro Shimizu 890ddd
	int m_rowLength;
Toshihiro Shimizu 890ddd
	UCHAR *m_stripBuffer;
Toshihiro Shimizu 890ddd
	int m_x, m_y, m_lx, m_ly;
Toshihiro Shimizu 890ddd
	bool m_isCmap24;
Shinya Kitaoka 3bfa54
	std::string m_history;
Toshihiro Shimizu 890ddd
	int m_nColor;
Toshihiro Shimizu 890ddd
	int m_nPencil;
Toshihiro Shimizu 890ddd
	bool m_isBigEndian;
Toshihiro Shimizu 890ddd
	bool m_isFirstLineRead;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TzpReader();
Toshihiro Shimizu 890ddd
	~TzpReader();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void open(FILE *file);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Tiio::RowOrder getRowOrder() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return Tiio::BOTTOM2TOP;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int skipLines(int lineCount);
Toshihiro Shimizu 890ddd
	void readLine(char *buffer, int x0, int x1, int shrink);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TzpReader::TzpReader()
Toshihiro Shimizu 890ddd
	: m_tiff(0), m_row(0), m_rowsPerStrip(0), m_stripIndex(-1), m_stripBuffer(0), m_rowLength(0), m_x(0), m_y(0), m_lx(0), m_ly(0), m_isCmap24(false), m_nColor(0), m_nPencil(0), m_isBigEndian(false), m_isFirstLineRead(true)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TzpReader::~TzpReader()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	delete m_stripBuffer;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TzpReader::open(FILE *file)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int fd = fileno(file);
Toshihiro Shimizu 890ddd
	TIFFErrorHandler oldhandler;
Toshihiro Shimizu 890ddd
	oldhandler = TIFFSetWarningHandler(NULL);
Toshihiro Shimizu 890ddd
	m_tiff = TIFFFdOpen(fd, "", "rb");
Toshihiro Shimizu 890ddd
	TIFFSetWarningHandler(oldhandler);
Toshihiro Shimizu 890ddd
	if (!m_tiff)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	uint32 w = 0, h = 0, bps = 0, spp = 0, rps = 0;
Toshihiro Shimizu 890ddd
	uint32 tileWidth = 0, tileLength = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIFFGetField(m_tiff, TIFFTAG_IMAGEWIDTH, &w);
Toshihiro Shimizu 890ddd
	TIFFGetField(m_tiff, TIFFTAG_IMAGELENGTH, &h);
Toshihiro Shimizu 890ddd
	TIFFGetField(m_tiff, TIFFTAG_BITSPERSAMPLE, &bps);
Toshihiro Shimizu 890ddd
	TIFFGetField(m_tiff, TIFFTAG_SAMPLESPERPIXEL, &spp);
Toshihiro Shimizu 890ddd
	TIFFGetField(m_tiff, TIFFTAG_ROWSPERSTRIP, &rps);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	float xdpi, ydpi;
Toshihiro Shimizu 890ddd
	TIFFGetField(m_tiff, TIFFTAG_XRESOLUTION, &xdpi);
Toshihiro Shimizu 890ddd
	TIFFGetField(m_tiff, TIFFTAG_YRESOLUTION, &ydpi);
Toshihiro Shimizu 890ddd
	m_info.m_dpix = xdpi;
Toshihiro Shimizu 890ddd
	m_info.m_dpiy = ydpi;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIFFGetField(m_tiff, TIFFTAG_TILEWIDTH, &tileWidth);
Toshihiro Shimizu 890ddd
	TIFFGetField(m_tiff, TIFFTAG_TILELENGTH, &tileLength);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	uint32 risCount = 0;
Toshihiro Shimizu 890ddd
	USHORT *risArray = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_info.m_lx = w;
Toshihiro Shimizu 890ddd
	m_info.m_ly = h;
Toshihiro Shimizu 890ddd
	m_x = 0;
Toshihiro Shimizu 890ddd
	m_y = 0;
Toshihiro Shimizu 890ddd
	m_lx = w;
Toshihiro Shimizu 890ddd
	m_ly = h;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (TIFFGetField(m_tiff, TIFFTAG_TOONZWINDOW, &risCount, &risArray)) {
Toshihiro Shimizu 890ddd
		m_info.m_x0 = m_x = risArray[0];
Toshihiro Shimizu 890ddd
		m_info.m_y0 = m_y = risArray[1];
Toshihiro Shimizu 890ddd
		m_info.m_lx = risArray[2];
Toshihiro Shimizu 890ddd
		m_info.m_ly = risArray[3];
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		m_info.m_x0 = 0;
Toshihiro Shimizu 890ddd
		m_info.m_y0 = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_info.m_x1 = m_info.m_x0 + w;
Toshihiro Shimizu 890ddd
	m_info.m_y1 = m_info.m_y0 + h;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (TIFFIsTiled(m_tiff)) {
Toshihiro Shimizu 890ddd
		m_rowsPerStrip = tileLength;
Toshihiro Shimizu 890ddd
		int tilesPerRow = (w + tileWidth - 1) / tileWidth;
Toshihiro Shimizu 890ddd
		m_rowLength = tileWidth * tilesPerRow;
Toshihiro Shimizu 890ddd
		int stripSize = m_rowsPerStrip * m_rowLength * 4;
Toshihiro Shimizu 890ddd
		m_stripBuffer = new UCHAR[stripSize];
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		m_rowsPerStrip = rps;
Toshihiro Shimizu 890ddd
		int stripSize = rps * w * 4 + 4096; // TIFFStripSize(m_tiff);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_stripBuffer = new UCHAR[stripSize];
Toshihiro Shimizu 890ddd
		m_rowLength = w;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	uint32 paletteCount;
Toshihiro Shimizu 890ddd
	USHORT *palette;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIFFGetField(m_tiff, TIFFTAG_TOONZPALETTE, &paletteCount, &palette);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_nColor = palette[10];
Toshihiro Shimizu 890ddd
	m_nPencil = palette[11];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_nColor == 128 && m_nPencil == 32) {
Toshihiro Shimizu 890ddd
		throw TException("Old 4.1 Palette");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (bps == 32)
Toshihiro Shimizu 890ddd
		m_isCmap24 = true;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		m_isCmap24 = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	char *data;
Toshihiro Shimizu 890ddd
	uint32 count;
Toshihiro Shimizu 890ddd
	TIFFGetField(m_tiff, TIFFTAG_TOONZHISTORY, &count, &data);
Shinya Kitaoka 3bfa54
	std::string history(data);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	uint16 planarconfig;
Toshihiro Shimizu 890ddd
	TIFFGetField(m_tiff, TIFFTAG_PLANARCONFIG, &planarconfig);
Toshihiro Shimizu 890ddd
	if (planarconfig == PLANARCONFIG_SEPARATE) {
Toshihiro Shimizu 890ddd
		//tmsg_error("separate buffer image not supported yet in .tz(up) files");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TzpReader::skipLines(int lineCount)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//m_row += lineCount;
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
extern int ComboInkIndex[]; //a bad patch....
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TzpReader::readLine(char *buffer, int x0, int x1, int shrink)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPixelCM32 *pix = (TPixelCM32 *)buffer;
Toshihiro Shimizu 890ddd
	for (int i = 0; i < m_info.m_lx; i++)
Toshihiro Shimizu 890ddd
		pix[i] = TPixelCM32();
Toshihiro Shimizu 890ddd
	int y = m_row++;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const int paintOffset = 0;				// paint#1 --> 1
Toshihiro Shimizu 890ddd
	const int inkOffset = 1 + m_nColor - 1; // ink#0 --> nColor-1
Toshihiro Shimizu 890ddd
	// (n.b. c'e' un colore in meno visto che il primo colore e' il BG)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_isCmap24) {
Toshihiro Shimizu 890ddd
		if (m_y <= y && y < m_y + m_ly) {
Shinya Kitaoka 3bfa54
			std::vector<tuint32> line(m_lx);</tuint32>
Toshihiro Shimizu 890ddd
			TIFFReadScanline(m_tiff, (char *)&line[0], y - m_y, 0);
Toshihiro Shimizu 890ddd
			if (m_isFirstLineRead) {
Toshihiro Shimizu 890ddd
				m_isFirstLineRead = false;
Toshihiro Shimizu 890ddd
				for (int i = 0; i < m_lx; i++)
Toshihiro Shimizu 890ddd
					if ((line[i] >> 24) != 0) {
Toshihiro Shimizu 890ddd
						m_isBigEndian = true;
Toshihiro Shimizu 890ddd
						break;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			pix += m_x;
Toshihiro Shimizu 890ddd
			for (int i = 0; i < m_lx; i++) {
Toshihiro Shimizu 890ddd
				TUINT32 inPix = m_isBigEndian ? swapTINT32(line[i]) : line[i];
Toshihiro Shimizu 890ddd
				int tone = inPix & 0xFF;
Toshihiro Shimizu 890ddd
				int paint = ((inPix >> 8) & 0xFF);
Toshihiro Shimizu 890ddd
				int ink = ((inPix >> 16) & 0xFF);
Toshihiro Shimizu 890ddd
				if (paint > 0)
Toshihiro Shimizu 890ddd
					paint += paintOffset;
Toshihiro Shimizu 890ddd
				if (ComboInkIndex[ink] != -1)
Toshihiro Shimizu 890ddd
					ink = ComboInkIndex[ink];
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					ink += inkOffset;
Toshihiro Shimizu 890ddd
				pix[i] = TPixelCM32(ink, paint, tone);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (m_y <= y && y < m_y + m_ly) {
Shinya Kitaoka 3bfa54
			std::vector<unsigned short=""> line(m_lx);</unsigned>
Toshihiro Shimizu 890ddd
			TIFFReadScanline(m_tiff, (char *)&line[0], y - m_y, 0);
Toshihiro Shimizu 890ddd
			pix += m_x;
Toshihiro Shimizu 890ddd
			static std::set<int> table;</int>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			///per le tzp che vengono da Irix
Toshihiro Shimizu 890ddd
			bool bigEndian = (m_tiff->tif_header.classic.tiff_magic == TIFF_BIGENDIAN);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			for (int i = 0; i < m_lx; i++) {
Toshihiro Shimizu 890ddd
				unsigned short inPix = line[i];
Toshihiro Shimizu 890ddd
				if (bigEndian)
Toshihiro Shimizu 890ddd
					inPix = swapUshort(inPix);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				int tone = inPix & 0xF;
Toshihiro Shimizu 890ddd
				int paint = ((inPix >> 4) & 0x7F);
Toshihiro Shimizu 890ddd
				int ink = ((inPix >> 11) & 0x1F);
Toshihiro Shimizu 890ddd
				tone |= tone << 4;
Toshihiro Shimizu 890ddd
				if (paint > 0)
Toshihiro Shimizu 890ddd
					paint += paintOffset;
Toshihiro Shimizu 890ddd
				ink += inkOffset;
Toshihiro Shimizu 890ddd
				pix[i] = TPixelCM32(ink, paint, tone);
Toshihiro Shimizu 890ddd
				if (tone < 255) {
Toshihiro Shimizu 890ddd
					//           int w = ink;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				if (table.find(ink) == table.end()) {
Toshihiro Shimizu 890ddd
					table.insert(ink);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//============================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Tiio::Reader *Tiio::makeTzpReader()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return new TzpReader();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Tiio::Writer *Tiio::makeTzpWriter()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(0);
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------