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