roentgen b75cab
// tiff stream interface class implementation
roentgen b75cab
roentgen b75cab
#include "tiffstream.h"
roentgen b75cab
roentgen b75cab
const char* TiffStream::m_name = "TiffStream";
roentgen b75cab
roentgen b75cab
TiffStream::TiffStream()
roentgen b75cab
{
roentgen b75cab
    m_tif = NULL;
roentgen b75cab
roentgen b75cab
roentgen b75cab
    m_inStream = NULL;
roentgen b75cab
	m_outStream = NULL;
roentgen b75cab
	m_ioStream = NULL;
roentgen b75cab
roentgen b75cab
	m_streamLength = 0;
roentgen b75cab
roentgen b75cab
	m_this = reinterpret_cast<thandle_t>(this);</thandle_t>
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
TiffStream::~TiffStream()
roentgen b75cab
{
roentgen b75cab
    if(m_tif != NULL) TIFFClose(m_tif);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
TIFF*
roentgen b75cab
TiffStream::makeFileStream(istream* str)
roentgen b75cab
{
roentgen b75cab
    m_inStream = str;
roentgen b75cab
	m_outStream = NULL;
roentgen b75cab
	m_ioStream = NULL;
roentgen b75cab
    m_streamLength = getSize(m_this);
roentgen b75cab
roentgen b75cab
    m_tif =  TIFFClientOpen(m_name, 
roentgen b75cab
                           "r",
roentgen b75cab
						   m_this,
roentgen b75cab
						   read,
roentgen b75cab
						   write,
roentgen b75cab
						   seek,
roentgen b75cab
						   close,
roentgen b75cab
						   size,
roentgen b75cab
						   map,
roentgen b75cab
						   unmap);
roentgen b75cab
    return m_tif;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
TIFF*
roentgen b75cab
TiffStream::makeFileStream(ostream* str)
roentgen b75cab
{
roentgen b75cab
	m_inStream = NULL;
roentgen b75cab
    m_outStream = str;
roentgen b75cab
	m_ioStream = NULL;
roentgen b75cab
	m_streamLength = getSize(m_this);
roentgen b75cab
roentgen b75cab
	m_tif =  TIFFClientOpen(m_name, 
roentgen b75cab
                           "w",
roentgen b75cab
						   m_this,
roentgen b75cab
						   read,
roentgen b75cab
						   write,
roentgen b75cab
						   seek,
roentgen b75cab
						   close,
roentgen b75cab
						   size,
roentgen b75cab
						   map,
roentgen b75cab
						   unmap);
roentgen b75cab
    return m_tif;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
TIFF*
roentgen b75cab
TiffStream::makeFileStream(iostream* str)
roentgen b75cab
{
roentgen b75cab
	m_inStream = NULL;
roentgen b75cab
	m_outStream = NULL;
roentgen b75cab
    m_ioStream = str;
roentgen b75cab
	m_streamLength = getSize(m_this);
roentgen b75cab
roentgen b75cab
    m_tif =  TIFFClientOpen(m_name, 
roentgen b75cab
	                       "r+w",
roentgen b75cab
						   m_this,
roentgen b75cab
						   read,
roentgen b75cab
						   write,
roentgen b75cab
						   seek,
roentgen b75cab
						   close,
roentgen b75cab
						   size,
roentgen b75cab
						   map,
roentgen b75cab
						   unmap);
roentgen b75cab
    return m_tif;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
tsize_t
roentgen b75cab
TiffStream::read(thandle_t fd, tdata_t buf, tsize_t size)
roentgen b75cab
{
roentgen b75cab
	istream* istr;
roentgen b75cab
	TiffStream* ts = reinterpret_cast<tiffstream*>(fd);</tiffstream*>
roentgen b75cab
    if(ts->m_inStream != NULL) {
roentgen b75cab
	    istr = ts->m_inStream;
roentgen b75cab
	} else if(ts->m_ioStream != NULL) {
roentgen b75cab
	    istr = ts->m_ioStream;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	int remain = ts->m_streamLength - ts->tell(fd);
roentgen b75cab
	int actual = remain < size ? remain : size;
roentgen b75cab
	istr->read(reinterpret_cast<char*>(buf), actual);</char*>
roentgen b75cab
    return istr->gcount();
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
tsize_t
roentgen b75cab
TiffStream::write(thandle_t fd, tdata_t buf, tsize_t size)
roentgen b75cab
{
roentgen b75cab
	TiffStream* ts = reinterpret_cast<tiffstream*>(fd);</tiffstream*>
roentgen b75cab
	ostream* ostr;
roentgen b75cab
	if(ts->m_outStream != NULL) {
roentgen b75cab
	    ostr = ts->m_outStream;
roentgen b75cab
	} else if(ts->m_ioStream != NULL) {
roentgen b75cab
	    ostr = ts->m_ioStream;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	streampos start = ostr->tellp();
roentgen b75cab
	ostr->write(reinterpret_cast<const char*="">(buf), size);</const>
roentgen b75cab
	return ostr->tellp() - start;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
toff_t
roentgen b75cab
TiffStream::seek(thandle_t fd, toff_t offset, int origin)
roentgen b75cab
{
roentgen b75cab
    TiffStream* ts = reinterpret_cast<tiffstream*>(fd);</tiffstream*>
roentgen b75cab
    if(ts->seekInt(fd, offset, origin) == true) return offset;
roentgen b75cab
	else return -1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TiffStream::close(thandle_t fd)
roentgen b75cab
{
roentgen b75cab
    TiffStream* ts = reinterpret_cast<tiffstream*>(fd);</tiffstream*>
roentgen b75cab
	if(ts->m_inStream != NULL) {
roentgen b75cab
		ts->m_inStream = NULL;
roentgen b75cab
		return 0;
roentgen b75cab
	} else if(ts->m_outStream != NULL) {
roentgen b75cab
		ts->m_outStream = NULL;
roentgen b75cab
		return 0;
roentgen b75cab
	} else if(ts->m_ioStream != NULL) {
roentgen b75cab
		ts->m_ioStream = NULL;
roentgen b75cab
		return 0;
roentgen b75cab
	}
roentgen b75cab
    return -1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
toff_t
roentgen b75cab
TiffStream::size(thandle_t fd)
roentgen b75cab
{
roentgen b75cab
    TiffStream* ts = reinterpret_cast<tiffstream*>(fd);</tiffstream*>
roentgen b75cab
    return ts->getSize(fd);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int 
roentgen b75cab
TiffStream::map(thandle_t fd, tdata_t* phase, toff_t* psize)
roentgen b75cab
{
roentgen b75cab
    return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
TiffStream::unmap(thandle_t fd, tdata_t base, tsize_t size)
roentgen b75cab
{
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
unsigned int
roentgen b75cab
TiffStream::getSize(thandle_t fd)
roentgen b75cab
{
roentgen b75cab
	if(!isOpen(fd)) return 0;
roentgen b75cab
roentgen b75cab
    unsigned int pos = tell(fd);
roentgen b75cab
	seekInt(fd, 0, end);
roentgen b75cab
	unsigned int size = tell(fd);
roentgen b75cab
	seekInt(fd, pos, beg);
roentgen b75cab
roentgen b75cab
	return size;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
unsigned int
roentgen b75cab
TiffStream::tell(thandle_t fd)
roentgen b75cab
{
roentgen b75cab
    TiffStream* ts = reinterpret_cast<tiffstream*>(fd);</tiffstream*>
roentgen b75cab
    if(ts->m_inStream != NULL) {
roentgen b75cab
	    return ts->m_inStream->tellg();
roentgen b75cab
	} else if(ts->m_outStream != NULL) {
roentgen b75cab
	    return ts->m_outStream->tellp();
roentgen b75cab
	} else if(ts->m_ioStream != NULL) {
roentgen b75cab
		return ts->m_ioStream->tellg();
roentgen b75cab
	}
roentgen b75cab
	return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
bool
roentgen b75cab
TiffStream::seekInt(thandle_t fd, unsigned int offset, int origin)
roentgen b75cab
{
roentgen b75cab
	if(!isOpen(fd)) return false;
roentgen b75cab
roentgen b75cab
	ios::seek_dir org;
roentgen b75cab
	switch(origin) {
roentgen b75cab
	case beg: 
roentgen b75cab
		org = ios::beg;
roentgen b75cab
		break;
roentgen b75cab
	case cur: 
roentgen b75cab
		org = ios::cur;
roentgen b75cab
		break;
roentgen b75cab
	case end: 
roentgen b75cab
		org = ios::end;
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	TiffStream* ts = reinterpret_cast<tiffstream*>(fd);</tiffstream*>
roentgen b75cab
    if(ts->m_inStream != NULL) {
roentgen b75cab
	    ts->m_inStream->seekg(offset, org);
roentgen b75cab
		return true;
roentgen b75cab
	} else if(ts->m_outStream != NULL) {
roentgen b75cab
	    ts->m_outStream->seekp(offset, org);
roentgen b75cab
		return true;
roentgen b75cab
	} else if(ts->m_ioStream != NULL) {
roentgen b75cab
	    ts->m_ioStream->seekg(offset, org);
roentgen b75cab
		ts->m_ioStream->seekp(offset, org);
roentgen b75cab
		return true;
roentgen b75cab
	}
roentgen b75cab
    return false;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
bool
roentgen b75cab
TiffStream::isOpen(thandle_t fd)
roentgen b75cab
{
roentgen b75cab
	TiffStream* ts = reinterpret_cast<tiffstream*>(fd);</tiffstream*>
roentgen b75cab
	return (ts->m_inStream != NULL ||
roentgen b75cab
		   ts->m_outStream != NULL ||
roentgen b75cab
		   ts->m_ioStream != NULL);
roentgen b75cab
}/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c++
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */