| 1 | #include "cvd/image_io.h" |
| 2 | #include "cvd/internal/io/tiff.h" |
| 3 | #include <algorithm> |
| 4 | #include <cstdint> |
| 5 | #include <iostream> |
| 6 | #include <map> |
| 7 | #include <tiffio.h> |
| 8 | #include <vector> |
| 9 | |
| 10 | using namespace CVD; |
| 11 | using namespace CVD::TIFF; |
| 12 | using namespace CVD::Exceptions::Image_IO; |
| 13 | using namespace std; |
| 14 | |
| 15 | //////////////////////////////////////////////////////////////////////////////// |
| 16 | // |
| 17 | // Private implementation of TIFF reading |
| 18 | // |
| 19 | |
| 20 | class CVD::TIFF::TIFFWritePimpl |
| 21 | { |
| 22 | public: |
| 23 | TIFFWritePimpl(ostream&, ImageRef size, const string& t); |
| 24 | ~TIFFWritePimpl(); |
| 25 | |
| 26 | template <class C> |
| 27 | void write_raw_pixel_line(const C* data); |
| 28 | void write_raw_pixel_line(const bool* data); |
| 29 | |
| 30 | private: |
| 31 | ostream& o; |
| 32 | ImageRef my_size; |
| 33 | string type; |
| 34 | uint32_t row; |
| 35 | ::TIFF* tif; |
| 36 | streamoff length; |
| 37 | long strip_size; |
| 38 | vector<uint8_t> bool_rowbuf; |
| 39 | |
| 40 | static tsize_t write(thandle_t vis, tdata_t data, tsize_t count); |
| 41 | static tsize_t read(thandle_t vis, tdata_t data, tsize_t count); |
| 42 | static toff_t seek(thandle_t vis, toff_t off, int dir); |
| 43 | static toff_t size(thandle_t vis); |
| 44 | static int close(thandle_t vis); |
| 45 | static int map(thandle_t, tdata_t*, toff_t*); |
| 46 | static void unmap(thandle_t, tdata_t, toff_t); |
| 47 | }; |
| 48 | |
| 49 | tsize_t TIFFWritePimpl::write(thandle_t vis, tdata_t data, tsize_t count) |
| 50 | { |
| 51 | TIFFWritePimpl* o = (TIFFWritePimpl*)vis; |
| 52 | streamoff p = o->o.tellp(); |
| 53 | o->o.write((const char*)data, count); |
| 54 | return o->o.tellp() - p; |
| 55 | } |
| 56 | |
| 57 | tsize_t TIFFWritePimpl::read(thandle_t, tdata_t, tsize_t) |
| 58 | { |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | toff_t TIFFWritePimpl::seek(thandle_t vis, toff_t off, int dir) |
| 63 | { |
| 64 | TIFFWritePimpl* p = (TIFFWritePimpl*)vis; |
| 65 | ostream& o(p->o); |
| 66 | |
| 67 | if(dir == SEEK_SET) |
| 68 | o.seekp(off, ios_base::beg); |
| 69 | else if(dir == SEEK_CUR) |
| 70 | o.seekp(off, ios_base::cur); |
| 71 | else if(dir == SEEK_END) |
| 72 | o.seekp(off, ios_base::end); |
| 73 | |
| 74 | //From comments in libtiff: |
| 75 | |
| 76 | // Attempt to workaround problems with seeking past the end of the |
| 77 | // stream. ofstream doesn't have a problem with this but |
| 78 | // ostrstream/ostringstream does. In that situation, add intermediate |
| 79 | // '\0' characters. |
| 80 | if(o.fail()) |
| 81 | { |
| 82 | ios::iostate old_state = o.rdstate(); |
| 83 | ios::iostate safe_state = old_state & ~ios_base::failbit; |
| 84 | streamoff crnt = 0; |
| 85 | |
| 86 | // clear the fail bit so else tellp() works |
| 87 | o.clear(safe_state); |
| 88 | if(dir == SEEK_SET) |
| 89 | crnt = 0; |
| 90 | else if(dir == SEEK_CUR) |
| 91 | crnt = o.tellp(); |
| 92 | else if(dir == SEEK_END) |
| 93 | { |
| 94 | o.seekp(0, ios_base::end); |
| 95 | crnt = o.tellp(); |
| 96 | } |
| 97 | |
| 98 | // restore original stream state |
| 99 | o.clear(old_state); |
| 100 | |
| 101 | // only do something if desired seek position is valid |
| 102 | if(crnt + off > 0) |
| 103 | { |
| 104 | |
| 105 | // clear the fail bit |
| 106 | o.clear(safe_state); |
| 107 | |
| 108 | // extend the stream by writing zeros |
| 109 | o.seekp(0, ios::end); |
| 110 | streamoff extra = crnt + off - o.tellp(); |
| 111 | for(streamoff i = 0; i < extra; i++) |
| 112 | o.put(0); |
| 113 | |
| 114 | // retry the seek, just to make sure. |
| 115 | o.seekp(crnt + off, ios_base::beg); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return o.tellp(); |
| 120 | } |
| 121 | |
| 122 | toff_t TIFFWritePimpl::size(thandle_t vis) |
| 123 | { |
| 124 | TIFFWritePimpl* ii = (TIFFWritePimpl*)vis; |
| 125 | return ii->length; |
| 126 | } |
| 127 | |
| 128 | int TIFFWritePimpl::close(thandle_t) |
| 129 | { |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | int TIFFWritePimpl::map(thandle_t, tdata_t*, toff_t*) |
| 134 | { |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | void TIFFWritePimpl::unmap(thandle_t, tdata_t, toff_t) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | static const int error_size = 512; |
| 143 | static char error_msg[error_size] = ""; |
| 144 | |
| 145 | static void tiff_error_handler(const char*, const char* fmt, va_list ap) |
| 146 | { |
| 147 | int n = vsnprintf(error_msg, error_size, fmt, ap); |
| 148 | if(n == error_size) |
| 149 | error_msg[n - 1] = 0; |
| 150 | } |
| 151 | |
| 152 | template <class T> |
| 153 | void TIFFWritePimpl::write_raw_pixel_line(const T* data) |
| 154 | { |
| 155 | //Do some type checking |
| 156 | if(type != PNM::type_name<T>::name()) |
| 157 | throw WriteTypeMismatch(type, PNM::type_name<T>::name()); |
| 158 | |
| 159 | //Do some sanity checking |
| 160 | if(row >= (unsigned long)my_size.y) |
| 161 | throw InternalLibraryError("CVD", "Write past end of image."); |
| 162 | |
| 163 | if(TIFFWriteEncodedStrip(tif, row, const_cast<T*>(data), strip_size) == -1) |
| 164 | throw WriteError(error_msg); |
| 165 | |
| 166 | row++; |
| 167 | } |
| 168 | |
| 169 | void TIFFWritePimpl::write_raw_pixel_line(const bool* data) |
| 170 | { |
| 171 | //Do some type checking |
| 172 | if(type != PNM::type_name<bool>::name()) |
| 173 | throw WriteTypeMismatch(type, PNM::type_name<bool>::name()); |
| 174 | |
| 175 | //Do some sanity checking |
| 176 | if(row >= (unsigned long)my_size.y) |
| 177 | throw InternalLibraryError("CVD", "Write past end of image."); |
| 178 | |
| 179 | fill(bool_rowbuf.begin(), bool_rowbuf.end(), 0); |
| 180 | |
| 181 | //Pack bools |
| 182 | for(int i = 0; i < my_size.x; i++) |
| 183 | bool_rowbuf[i / 8] |= static_cast<unsigned char>((data[i] & 1) << (7 - i % 8)); |
| 184 | |
| 185 | if(TIFFWriteScanline(tif, &bool_rowbuf[0], row) == -1) |
| 186 | throw WriteError(error_msg); |
| 187 | |
| 188 | row++; |
| 189 | } |
| 190 | |
| 191 | TIFFWritePimpl::~TIFFWritePimpl() |
| 192 | { |
| 193 | if(tif) |
| 194 | TIFFClose(tif); |
| 195 | } |
| 196 | |
| 197 | TIFFWritePimpl::TIFFWritePimpl(ostream& os, ImageRef s, const string& t) |
| 198 | : o(os) |
| 199 | , my_size(s) |
| 200 | , type(t) |
| 201 | , row(0) |
| 202 | , tif(0) |
| 203 | { |
| 204 | TIFFSetErrorHandler(tiff_error_handler); |
| 205 | |
| 206 | //Find out the file size, and the suitability of the stream |
| 207 | o.seekp(0, ios_base::end); |
| 208 | length = o.tellp(); |
| 209 | if(length == -1) |
| 210 | throw UnseekableIstream("TIFF"); |
| 211 | o.seekp(0, ios_base::beg); |
| 212 | |
| 213 | tif = TIFFClientOpen("std::ostream", "w", this, |
| 214 | read, write, seek, close, size, map, unmap); |
| 215 | |
| 216 | if(tif == NULL) |
| 217 | throw WriteError(error_msg); |
| 218 | |
| 219 | //Set the correct tags. |
| 220 | TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, my_size.x); |
| 221 | TIFFSetField(tif, TIFFTAG_IMAGELENGTH, my_size.y); |
| 222 | TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
| 223 | TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); |
| 224 | |
| 225 | uint16_t alpha[] = { EXTRASAMPLE_UNASSALPHA }; |
| 226 | if(t == "bool") |
| 227 | { |
| 228 | TIFFSetField(tif, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); |
| 229 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); |
| 230 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); |
| 231 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); |
| 232 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 1); |
| 233 | TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, my_size.y); |
| 234 | bool_rowbuf.resize((my_size.x + 7) / 8); |
| 235 | } |
| 236 | else if(t == "unsigned char") |
| 237 | { |
| 238 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 239 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); |
| 240 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); |
| 241 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
| 242 | } |
| 243 | else if(t == "unsigned short") |
| 244 | { |
| 245 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 246 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); |
| 247 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); |
| 248 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16); |
| 249 | } |
| 250 | else if(t == "float") |
| 251 | { |
| 252 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 253 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); |
| 254 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); |
| 255 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32); |
| 256 | TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP); |
| 257 | } |
| 258 | else if(t == "double") |
| 259 | { |
| 260 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 261 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); |
| 262 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); |
| 263 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 64); |
| 264 | TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP); |
| 265 | } |
| 266 | else if(t == "CVD::Rgb<unsigned char>") |
| 267 | { |
| 268 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 269 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
| 270 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); |
| 271 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
| 272 | } |
| 273 | else if(t == "CVD::Rgb<unsigned short>") |
| 274 | { |
| 275 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 276 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
| 277 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); |
| 278 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16); |
| 279 | } |
| 280 | else if(t == "CVD::Rgb<float>") |
| 281 | { |
| 282 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 283 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
| 284 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); |
| 285 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32); |
| 286 | TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP); |
| 287 | } |
| 288 | else if(t == "CVD::Rgb<double>") |
| 289 | { |
| 290 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 291 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
| 292 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); |
| 293 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 64); |
| 294 | TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP); |
| 295 | } |
| 296 | else if(t == "CVD::Rgba<unsigned char>") |
| 297 | { |
| 298 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 299 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
| 300 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 4); |
| 301 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
| 302 | TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, 1, alpha); |
| 303 | } |
| 304 | else if(t == "CVD::Rgba<unsigned short>") |
| 305 | { |
| 306 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 307 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
| 308 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 4); |
| 309 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16); |
| 310 | TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, 1, alpha); |
| 311 | } |
| 312 | else if(t == "CVD::Rgba<float>") |
| 313 | { |
| 314 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 315 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
| 316 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 4); |
| 317 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32); |
| 318 | TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP); |
| 319 | TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, 1, alpha); |
| 320 | } |
| 321 | else if(t == "CVD::Rgba<double>") |
| 322 | { |
| 323 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); |
| 324 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
| 325 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 4); |
| 326 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 64); |
| 327 | TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP); |
| 328 | TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, 1, alpha); |
| 329 | } |
| 330 | else |
| 331 | throw UnsupportedImageSubType("TIFF", t); |
| 332 | |
| 333 | strip_size = TIFFStripSize(tif); |
| 334 | } |
| 335 | |
| 336 | //////////////////////////////////////////////////////////////////////////////// |
| 337 | // |
| 338 | // Implementation of public parts of TIFF reading |
| 339 | // |
| 340 | |
| 341 | tiff_writer::tiff_writer(ostream& o, ImageRef size, const string& type, const std::map<std::string, Parameter<>>&) |
| 342 | : t(new TIFFWritePimpl(o, size, type)) |
| 343 | { |
| 344 | } |
| 345 | |
| 346 | tiff_writer::~tiff_writer() |
| 347 | { |
| 348 | } |
| 349 | |
| 350 | //Mechanically generate the pixel reading calls. |
| 351 | #define GEN1(X) \ |
| 352 | void tiff_writer::write_raw_pixel_line(const X* d) { t->write_raw_pixel_line(d); } |
| 353 | #define GEN3(X) \ |
| 354 | GEN1(X) \ |
| 355 | GEN1(Rgb<X>) \ |
| 356 | GEN1(Rgba<X>) |
| 357 | |
| 358 | GEN1(bool) |
| 359 | GEN3(unsigned char) |
| 360 | GEN3(unsigned short) |
| 361 | GEN3(float) |
| 362 | GEN3(double) |