This source file includes following definitions.
- _tiffosReadProc
- _tiffisReadProc
- _tiffosWriteProc
- _tiffisWriteProc
- _tiffosSeekProc
- _tiffisSeekProc
- _tiffosSizeProc
- _tiffisSizeProc
- _tiffosCloseProc
- _tiffisCloseProc
- _tiffDummyMapProc
- _tiffDummyUnmapProc
- _tiffStreamOpen
- TIFFStreamOpen
- TIFFStreamOpen
#include "tiffiop.h"
#include "tiffio.hxx"
#include <iostream>
#ifndef __VMS
using namespace std;
#endif
struct tiffis_data;
struct tiffos_data;
extern "C" {
static tmsize_t _tiffosReadProc(thandle_t, void*, tmsize_t);
static tmsize_t _tiffisReadProc(thandle_t fd, void* buf, tmsize_t size);
static tmsize_t _tiffosWriteProc(thandle_t fd, void* buf, tmsize_t size);
static tmsize_t _tiffisWriteProc(thandle_t, void*, tmsize_t);
static uint64 _tiffosSeekProc(thandle_t fd, uint64 off, int whence);
static uint64 _tiffisSeekProc(thandle_t fd, uint64 off, int whence);
static uint64 _tiffosSizeProc(thandle_t fd);
static uint64 _tiffisSizeProc(thandle_t fd);
static int _tiffosCloseProc(thandle_t fd);
static int _tiffisCloseProc(thandle_t fd);
static int _tiffDummyMapProc(thandle_t , void** base, toff_t* size );
static void _tiffDummyUnmapProc(thandle_t , void* base, toff_t size );
static TIFF* _tiffStreamOpen(const char* name, const char* mode, void *fd);
struct tiffis_data
{
istream *stream;
ios::pos_type start_pos;
};
struct tiffos_data
{
ostream *stream;
ios::pos_type start_pos;
};
static tmsize_t
_tiffosReadProc(thandle_t, void*, tmsize_t)
{
return 0;
}
static tmsize_t
_tiffisReadProc(thandle_t fd, void* buf, tmsize_t size)
{
tiffis_data *data = reinterpret_cast<tiffis_data *>(fd);
streamsize request_size = size;
if (static_cast<tmsize_t>(request_size) != size)
return static_cast<tmsize_t>(-1);
data->stream->read((char *) buf, request_size);
return static_cast<tmsize_t>(data->stream->gcount());
}
static tmsize_t
_tiffosWriteProc(thandle_t fd, void* buf, tmsize_t size)
{
tiffos_data *data = reinterpret_cast<tiffos_data *>(fd);
ostream *os = data->stream;
ios::pos_type pos = os->tellp();
streamsize request_size = size;
if (static_cast<tmsize_t>(request_size) != size)
return static_cast<tmsize_t>(-1);
os->write(reinterpret_cast<const char *>(buf), request_size);
return static_cast<tmsize_t>(os->tellp() - pos);
}
static tmsize_t
_tiffisWriteProc(thandle_t, void*, tmsize_t)
{
return 0;
}
static uint64
_tiffosSeekProc(thandle_t fd, uint64 off, int whence)
{
tiffos_data *data = reinterpret_cast<tiffos_data *>(fd);
ostream *os = data->stream;
if( os->fail() )
return static_cast<uint64>(-1);
switch(whence) {
case SEEK_SET:
{
uint64 new_offset = static_cast<uint64>(data->start_pos) + off;
ios::off_type offset = static_cast<ios::off_type>(new_offset);
if (static_cast<uint64>(offset) != new_offset)
return static_cast<uint64>(-1);
os->seekp(offset, ios::beg);
break;
}
case SEEK_CUR:
{
ios::off_type offset = static_cast<ios::off_type>(off);
if (static_cast<uint64>(offset) != off)
return static_cast<uint64>(-1);
os->seekp(offset, ios::cur);
break;
}
case SEEK_END:
{
ios::off_type offset = static_cast<ios::off_type>(off);
if (static_cast<uint64>(offset) != off)
return static_cast<uint64>(-1);
os->seekp(offset, ios::end);
break;
}
}
if( os->fail() ) {
#ifdef __VMS
int old_state;
#else
ios::iostate old_state;
#endif
ios::pos_type origin;
old_state = os->rdstate();
os->clear(os->rdstate() & ~ios::failbit);
switch( whence ) {
case SEEK_SET:
default:
origin = data->start_pos;
break;
case SEEK_CUR:
origin = os->tellp();
break;
case SEEK_END:
os->seekp(0, ios::end);
origin = os->tellp();
break;
}
os->clear(old_state);
if( (static_cast<uint64>(origin) + off) > static_cast<uint64>(data->start_pos) ) {
uint64 num_fill;
os->clear(os->rdstate() & ~ios::failbit);
os->seekp(0, ios::end);
num_fill = (static_cast<uint64>(origin)) + off - os->tellp();
for( uint64 i = 0; i < num_fill; i++ )
os->put('\0');
os->seekp(static_cast<ios::off_type>(static_cast<uint64>(origin) + off), ios::beg);
}
}
return static_cast<uint64>(os->tellp());
}
static uint64
_tiffisSeekProc(thandle_t fd, uint64 off, int whence)
{
tiffis_data *data = reinterpret_cast<tiffis_data *>(fd);
switch(whence) {
case SEEK_SET:
{
uint64 new_offset = static_cast<uint64>(data->start_pos) + off;
ios::off_type offset = static_cast<ios::off_type>(new_offset);
if (static_cast<uint64>(offset) != new_offset)
return static_cast<uint64>(-1);
data->stream->seekg(offset, ios::beg);
break;
}
case SEEK_CUR:
{
ios::off_type offset = static_cast<ios::off_type>(off);
if (static_cast<uint64>(offset) != off)
return static_cast<uint64>(-1);
data->stream->seekg(offset, ios::cur);
break;
}
case SEEK_END:
{
ios::off_type offset = static_cast<ios::off_type>(off);
if (static_cast<uint64>(offset) != off)
return static_cast<uint64>(-1);
data->stream->seekg(offset, ios::end);
break;
}
}
return (uint64) (data->stream->tellg() - data->start_pos);
}
static uint64
_tiffosSizeProc(thandle_t fd)
{
tiffos_data *data = reinterpret_cast<tiffos_data *>(fd);
ostream *os = data->stream;
ios::pos_type pos = os->tellp();
ios::pos_type len;
os->seekp(0, ios::end);
len = os->tellp();
os->seekp(pos);
return (uint64) len;
}
static uint64
_tiffisSizeProc(thandle_t fd)
{
tiffis_data *data = reinterpret_cast<tiffis_data *>(fd);
ios::pos_type pos = data->stream->tellg();
ios::pos_type len;
data->stream->seekg(0, ios::end);
len = data->stream->tellg();
data->stream->seekg(pos);
return (uint64) len;
}
static int
_tiffosCloseProc(thandle_t fd)
{
delete reinterpret_cast<tiffos_data *>(fd);
return 0;
}
static int
_tiffisCloseProc(thandle_t fd)
{
delete reinterpret_cast<tiffis_data *>(fd);
return 0;
}
static int
_tiffDummyMapProc(thandle_t , void** base, toff_t* size )
{
return (0);
}
static void
_tiffDummyUnmapProc(thandle_t , void* base, toff_t size )
{
}
static TIFF*
_tiffStreamOpen(const char* name, const char* mode, void *fd)
{
TIFF* tif;
if( strchr(mode, 'w') ) {
tiffos_data *data = new tiffos_data;
data->stream = reinterpret_cast<ostream *>(fd);
data->start_pos = data->stream->tellp();
tif = TIFFClientOpen(name, mode,
reinterpret_cast<thandle_t>(data),
_tiffosReadProc,
_tiffosWriteProc,
_tiffosSeekProc,
_tiffosCloseProc,
_tiffosSizeProc,
_tiffDummyMapProc,
_tiffDummyUnmapProc);
} else {
tiffis_data *data = new tiffis_data;
data->stream = reinterpret_cast<istream *>(fd);
data->start_pos = data->stream->tellg();
tif = TIFFClientOpen(name, mode,
reinterpret_cast<thandle_t>(data),
_tiffisReadProc,
_tiffisWriteProc,
_tiffisSeekProc,
_tiffisCloseProc,
_tiffisSizeProc,
_tiffDummyMapProc,
_tiffDummyUnmapProc);
}
return (tif);
}
}
TIFF*
TIFFStreamOpen(const char* name, ostream *os)
{
if( !os->fail() && static_cast<int>(os->tellp()) < 0 ) {
*os << '\0';
os->seekp(0);
}
return _tiffStreamOpen(name, "wm", os);
}
TIFF*
TIFFStreamOpen(const char* name, istream *is)
{
return _tiffStreamOpen(name, "rm", is);
}