summaryrefslogtreecommitdiff
path: root/tiff/contrib/stream/tiffstream.cpp
blob: ffff687581eb06271c5d73969b1407b1673cfc51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// tiff stream interface class implementation

#include "tiffstream.h"

const char* TiffStream::m_name = "TiffStream";

TiffStream::TiffStream()
{
    m_tif = NULL;


    m_inStream = NULL;
	m_outStream = NULL;
	m_ioStream = NULL;

	m_streamLength = 0;

	m_this = reinterpret_cast<thandle_t>(this);
};

TiffStream::~TiffStream()
{
    if(m_tif != NULL) TIFFClose(m_tif);
}

TIFF*
TiffStream::makeFileStream(istream* str)
{
    m_inStream = str;
	m_outStream = NULL;
	m_ioStream = NULL;
    m_streamLength = getSize(m_this);

    m_tif =  TIFFClientOpen(m_name, 
                           "r",
						   m_this,
						   read,
						   write,
						   seek,
						   close,
						   size,
						   map,
						   unmap);
    return m_tif;
}

TIFF*
TiffStream::makeFileStream(ostream* str)
{
	m_inStream = NULL;
    m_outStream = str;
	m_ioStream = NULL;
	m_streamLength = getSize(m_this);

	m_tif =  TIFFClientOpen(m_name, 
                           "w",
						   m_this,
						   read,
						   write,
						   seek,
						   close,
						   size,
						   map,
						   unmap);
    return m_tif;
}

TIFF*
TiffStream::makeFileStream(iostream* str)
{
	m_inStream = NULL;
	m_outStream = NULL;
    m_ioStream = str;
	m_streamLength = getSize(m_this);

    m_tif =  TIFFClientOpen(m_name, 
	                       "r+w",
						   m_this,
						   read,
						   write,
						   seek,
						   close,
						   size,
						   map,
						   unmap);
    return m_tif;
}

tsize_t
TiffStream::read(thandle_t fd, tdata_t buf, tsize_t size)
{
	istream* istr;
	TiffStream* ts = reinterpret_cast<TiffStream*>(fd);
    if(ts->m_inStream != NULL) {
	    istr = ts->m_inStream;
	} else if(ts->m_ioStream != NULL) {
	    istr = ts->m_ioStream;
	}

	int remain = ts->m_streamLength - ts->tell(fd);
	int actual = remain < size ? remain : size;
	istr->read(reinterpret_cast<char*>(buf), actual);
    return istr->gcount();
}

tsize_t
TiffStream::write(thandle_t fd, tdata_t buf, tsize_t size)
{
	TiffStream* ts = reinterpret_cast<TiffStream*>(fd);
	ostream* ostr;
	if(ts->m_outStream != NULL) {
	    ostr = ts->m_outStream;
	} else if(ts->m_ioStream != NULL) {
	    ostr = ts->m_ioStream;
	}

	streampos start = ostr->tellp();
	ostr->write(reinterpret_cast<const char*>(buf), size);
	return ostr->tellp() - start;
}

toff_t
TiffStream::seek(thandle_t fd, toff_t offset, int origin)
{
    TiffStream* ts = reinterpret_cast<TiffStream*>(fd);
    if(ts->seekInt(fd, offset, origin) == true) return offset;
	else return -1;
}

int
TiffStream::close(thandle_t fd)
{
    TiffStream* ts = reinterpret_cast<TiffStream*>(fd);
	if(ts->m_inStream != NULL) {
		ts->m_inStream = NULL;
		return 0;
	} else if(ts->m_outStream != NULL) {
		ts->m_outStream = NULL;
		return 0;
	} else if(ts->m_ioStream != NULL) {
		ts->m_ioStream = NULL;
		return 0;
	}
    return -1;
}

toff_t
TiffStream::size(thandle_t fd)
{
    TiffStream* ts = reinterpret_cast<TiffStream*>(fd);
    return ts->getSize(fd);
}

int 
TiffStream::map(thandle_t fd, tdata_t* phase, toff_t* psize)
{
    return 0;
}

void
TiffStream::unmap(thandle_t fd, tdata_t base, tsize_t size)
{
}

unsigned int
TiffStream::getSize(thandle_t fd)
{
	if(!isOpen(fd)) return 0;

    unsigned int pos = tell(fd);
	seekInt(fd, 0, end);
	unsigned int size = tell(fd);
	seekInt(fd, pos, beg);

	return size;
}

unsigned int
TiffStream::tell(thandle_t fd)
{
    TiffStream* ts = reinterpret_cast<TiffStream*>(fd);
    if(ts->m_inStream != NULL) {
	    return ts->m_inStream->tellg();
	} else if(ts->m_outStream != NULL) {
	    return ts->m_outStream->tellp();
	} else if(ts->m_ioStream != NULL) {
		return ts->m_ioStream->tellg();
	}
	return 0;
}

bool
TiffStream::seekInt(thandle_t fd, unsigned int offset, int origin)
{
	if(!isOpen(fd)) return false;

	ios::seek_dir org;
	switch(origin) {
	case beg: 
		org = ios::beg;
		break;
	case cur: 
		org = ios::cur;
		break;
	case end: 
		org = ios::end;
		break;
	}

	TiffStream* ts = reinterpret_cast<TiffStream*>(fd);
    if(ts->m_inStream != NULL) {
	    ts->m_inStream->seekg(offset, org);
		return true;
	} else if(ts->m_outStream != NULL) {
	    ts->m_outStream->seekp(offset, org);
		return true;
	} else if(ts->m_ioStream != NULL) {
	    ts->m_ioStream->seekg(offset, org);
		ts->m_ioStream->seekp(offset, org);
		return true;
	}
    return false;
}

bool
TiffStream::isOpen(thandle_t fd)
{
	TiffStream* ts = reinterpret_cast<TiffStream*>(fd);
	return (ts->m_inStream != NULL ||
		   ts->m_outStream != NULL ||
		   ts->m_ioStream != NULL);
}/*
 * Local Variables:
 * mode: c++
 * c-basic-offset: 8
 * fill-column: 78
 * End:
 */