]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/contrib/stream/tiffstream.cpp
1 // tiff stream interface class implementation
3 #include "tiffstream.h"
5 const char* TiffStream::m_name
= "TiffStream";
7 TiffStream::TiffStream()
18 m_this
= reinterpret_cast<thandle_t
>(this);
21 TiffStream::~TiffStream()
23 if(m_tif
!= NULL
) TIFFClose(m_tif
);
27 TiffStream::makeFileStream(istream
* str
)
32 m_streamLength
= getSize(m_this
);
34 m_tif
= TIFFClientOpen(m_name
,
48 TiffStream::makeFileStream(ostream
* str
)
53 m_streamLength
= getSize(m_this
);
55 m_tif
= TIFFClientOpen(m_name
,
69 TiffStream::makeFileStream(iostream
* str
)
74 m_streamLength
= getSize(m_this
);
76 m_tif
= TIFFClientOpen(m_name
,
90 TiffStream::read(thandle_t fd
, tdata_t buf
, tsize_t size
)
93 TiffStream
* ts
= reinterpret_cast<TiffStream
*>(fd
);
94 if(ts
->m_inStream
!= NULL
) {
95 istr
= ts
->m_inStream
;
96 } else if(ts
->m_ioStream
!= NULL
) {
97 istr
= ts
->m_ioStream
;
100 int remain
= ts
->m_streamLength
- ts
->tell(fd
);
101 int actual
= remain
< size
? remain
: size
;
102 istr
->read(reinterpret_cast<char*>(buf
), actual
);
103 return istr
->gcount();
107 TiffStream::write(thandle_t fd
, tdata_t buf
, tsize_t size
)
109 TiffStream
* ts
= reinterpret_cast<TiffStream
*>(fd
);
111 if(ts
->m_outStream
!= NULL
) {
112 ostr
= ts
->m_outStream
;
113 } else if(ts
->m_ioStream
!= NULL
) {
114 ostr
= ts
->m_ioStream
;
117 streampos start
= ostr
->tellp();
118 ostr
->write(reinterpret_cast<const char*>(buf
), size
);
119 return ostr
->tellp() - start
;
123 TiffStream::seek(thandle_t fd
, toff_t offset
, int origin
)
125 TiffStream
* ts
= reinterpret_cast<TiffStream
*>(fd
);
126 if(ts
->seekInt(fd
, offset
, origin
) == true) return offset
;
131 TiffStream::close(thandle_t fd
)
133 TiffStream
* ts
= reinterpret_cast<TiffStream
*>(fd
);
134 if(ts
->m_inStream
!= NULL
) {
135 ts
->m_inStream
= NULL
;
137 } else if(ts
->m_outStream
!= NULL
) {
138 ts
->m_outStream
= NULL
;
140 } else if(ts
->m_ioStream
!= NULL
) {
141 ts
->m_ioStream
= NULL
;
148 TiffStream::size(thandle_t fd
)
150 TiffStream
* ts
= reinterpret_cast<TiffStream
*>(fd
);
151 return ts
->getSize(fd
);
155 TiffStream::map(thandle_t fd
, tdata_t
* phase
, toff_t
* psize
)
161 TiffStream::unmap(thandle_t fd
, tdata_t base
, tsize_t size
)
166 TiffStream::getSize(thandle_t fd
)
168 if(!isOpen(fd
)) return 0;
170 unsigned int pos
= tell(fd
);
172 unsigned int size
= tell(fd
);
173 seekInt(fd
, pos
, beg
);
179 TiffStream::tell(thandle_t fd
)
181 TiffStream
* ts
= reinterpret_cast<TiffStream
*>(fd
);
182 if(ts
->m_inStream
!= NULL
) {
183 return ts
->m_inStream
->tellg();
184 } else if(ts
->m_outStream
!= NULL
) {
185 return ts
->m_outStream
->tellp();
186 } else if(ts
->m_ioStream
!= NULL
) {
187 return ts
->m_ioStream
->tellg();
193 TiffStream::seekInt(thandle_t fd
, unsigned int offset
, int origin
)
195 if(!isOpen(fd
)) return false;
210 TiffStream
* ts
= reinterpret_cast<TiffStream
*>(fd
);
211 if(ts
->m_inStream
!= NULL
) {
212 ts
->m_inStream
->seekg(offset
, org
);
214 } else if(ts
->m_outStream
!= NULL
) {
215 ts
->m_outStream
->seekp(offset
, org
);
217 } else if(ts
->m_ioStream
!= NULL
) {
218 ts
->m_ioStream
->seekg(offset
, org
);
219 ts
->m_ioStream
->seekp(offset
, org
);
226 TiffStream::isOpen(thandle_t fd
)
228 TiffStream
* ts
= reinterpret_cast<TiffStream
*>(fd
);
229 return (ts
->m_inStream
!= NULL
||
230 ts
->m_outStream
!= NULL
||
231 ts
->m_ioStream
!= NULL
);