| 1 | #include "cvd/internal/io/tiff.h" |
| 2 | #include "cvd/image_io.h" |
| 3 | #include "cvd_src/config_internal.h" |
| 4 | #include <algorithm> |
| 5 | #include <iostream> |
| 6 | #include <tiffio.h> |
| 7 | #include <vector> |
| 8 | |
| 9 | using namespace CVD; |
| 10 | using namespace CVD::TIFF; |
| 11 | using namespace CVD::Exceptions::Image_IO; |
| 12 | using namespace std; |
| 13 | |
| 14 | //////////////////////////////////////////////////////////////////////////////// |
| 15 | // |
| 16 | // Private implementation of TIFF reading |
| 17 | // |
| 18 | |
| 19 | class CVD::TIFF::TIFFPimpl |
| 20 | { |
| 21 | public: |
| 22 | TIFFPimpl(istream&); |
| 23 | ~TIFFPimpl(); |
| 24 | ImageRef size(); |
| 25 | string datatype(); |
| 26 | template <class C> |
| 27 | void get_raw_pixel_line(C* data); |
| 28 | void get_raw_pixel_line(bool* data); |
| 29 | void get_raw_pixel_line(Rgba<unsigned char>* data); |
| 30 | |
| 31 | private: |
| 32 | template <class C> |
| 33 | void checkRead(const C*); |
| 34 | |
| 35 | istream& i; |
| 36 | uint32_t row; |
| 37 | ImageRef my_size; |
| 38 | string type; |
| 39 | streamoff length; |
| 40 | ::TIFF* tif; |
| 41 | bool use_cooked_rgba_interface; |
| 42 | bool inverted_grey; |
| 43 | |
| 44 | vector<uint32_t> raster_data; |
| 45 | vector<uint8_t> bool_rowbuf; |
| 46 | |
| 47 | static tsize_t write(thandle_t vis, tdata_t data, tsize_t count); |
| 48 | static tsize_t read(thandle_t vis, tdata_t data, tsize_t count); |
| 49 | static toff_t seek(thandle_t vis, toff_t off, int dir); |
| 50 | static toff_t size(thandle_t vis); |
| 51 | static int close(thandle_t vis); |
| 52 | static int map(thandle_t, tdata_t*, toff_t*); |
| 53 | static void unmap(thandle_t, tdata_t, toff_t); |
| 54 | }; |
| 55 | |
| 56 | tsize_t TIFFPimpl::read(thandle_t vis, tdata_t data, tsize_t count) |
| 57 | { |
| 58 | TIFFPimpl* i = (TIFFPimpl*)vis; |
| 59 | i->i.read((char*)data, count); |
| 60 | return i->i.gcount(); |
| 61 | } |
| 62 | |
| 63 | //tsize_t tiff_in::write(thandle_t vis, tdata_t data, tsize_t count) |
| 64 | tsize_t TIFFPimpl::write(thandle_t, tdata_t, tsize_t) |
| 65 | { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | toff_t TIFFPimpl::seek(thandle_t vis, toff_t off, int dir) |
| 70 | { |
| 71 | TIFFPimpl* i = (TIFFPimpl*)vis; |
| 72 | |
| 73 | if(dir == SEEK_SET) |
| 74 | i->i.seekg(off, ios_base::beg); |
| 75 | else if(dir == SEEK_CUR) |
| 76 | i->i.seekg(off, ios_base::cur); |
| 77 | else if(dir == SEEK_END) |
| 78 | i->i.seekg(off, ios_base::end); |
| 79 | |
| 80 | return i->i.tellg(); |
| 81 | } |
| 82 | |
| 83 | toff_t TIFFPimpl::size(thandle_t vis) |
| 84 | { |
| 85 | TIFFPimpl* ii = (TIFFPimpl*)vis; |
| 86 | return ii->length; |
| 87 | } |
| 88 | |
| 89 | int TIFFPimpl::close(thandle_t) |
| 90 | { |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int TIFFPimpl::map(thandle_t, tdata_t*, toff_t*) |
| 95 | { |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | void TIFFPimpl::unmap(thandle_t, tdata_t, toff_t) |
| 100 | { |
| 101 | } |
| 102 | |
| 103 | static const int error_size = 512; |
| 104 | static char error_msg[error_size] = ""; |
| 105 | |
| 106 | static void tiff_error_handler(const char*, const char* fmt, va_list ap) |
| 107 | { |
| 108 | int n = vsnprintf(error_msg, error_size, fmt, ap); |
| 109 | if(n == error_size) |
| 110 | error_msg[n - 1] = 0; |
| 111 | } |
| 112 | |
| 113 | //Compile-error free geryscale inverter. |
| 114 | //Does nothing for unknown types |
| 115 | template <class C> |
| 116 | void invert(C* data, long num) |
| 117 | { |
| 118 | for(long n = 0; n < num; n++) |
| 119 | data[n] = Pixel::traits<C>::max_intensity - data[n]; |
| 120 | } |
| 121 | |
| 122 | void attempt_invert(...) { } |
| 123 | void attempt_invert(bool* data, long num) { invert(data, num); } |
| 124 | void attempt_invert(unsigned char* data, long num) { invert(data, num); } |
| 125 | void attempt_invert(unsigned short* data, long num) { invert(data, num); } |
| 126 | void attempt_invert(float* data, long num) { invert(data, num); } |
| 127 | void attempt_invert(double* data, long num) { invert(data, num); } |
| 128 | |
| 129 | template <class T> |
| 130 | void TIFFPimpl::checkRead(const T*) |
| 131 | { |
| 132 | if(datatype() != PNM::type_name<T>::name()) |
| 133 | throw ReadTypeMismatch(datatype(), PNM::type_name<T>::name()); |
| 134 | |
| 135 | if(row > (unsigned long)my_size.y) |
| 136 | throw InternalLibraryError("CVD", "Read past end of image."); |
| 137 | } |
| 138 | |
| 139 | void TIFFPimpl::get_raw_pixel_line(Rgba<unsigned char>* data) |
| 140 | { |
| 141 | checkRead(data); |
| 142 | |
| 143 | if(use_cooked_rgba_interface) |
| 144 | { |
| 145 | uint32_t* raster = &raster_data[row * my_size.x]; |
| 146 | uint32_t* end = raster + my_size.x; |
| 147 | |
| 148 | for(; raster < end; raster++, data++) |
| 149 | { |
| 150 | data->red = TIFFGetR(*raster); |
| 151 | data->green = TIFFGetG(*raster); |
| 152 | data->blue = TIFFGetB(*raster); |
| 153 | data->alpha = static_cast<unsigned char>(TIFFGetA(*raster)); |
| 154 | } |
| 155 | |
| 156 | row++; |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | get_raw_pixel_line<Rgba<unsigned char>>(data); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | template <class T> |
| 165 | void TIFFPimpl::get_raw_pixel_line(T* d) |
| 166 | { |
| 167 | checkRead(d); |
| 168 | if(use_cooked_rgba_interface) |
| 169 | { |
| 170 | throw InternalLibraryError("CVD", "Reading non-RGBA tiff data as RGBA"); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | if(TIFFReadScanline(tif, (void*)d, row) == -1) |
| 175 | throw MalformedImage(error_msg); |
| 176 | |
| 177 | if(inverted_grey) |
| 178 | attempt_invert(d, my_size.x); |
| 179 | |
| 180 | row++; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void TIFFPimpl::get_raw_pixel_line(bool* d) |
| 185 | { |
| 186 | if(datatype() != PNM::type_name<bool>::name()) |
| 187 | throw ReadTypeMismatch(datatype(), PNM::type_name<bool>::name()); |
| 188 | |
| 189 | if(TIFFReadScanline(tif, (void*)&bool_rowbuf[0], row) == -1) |
| 190 | throw MalformedImage(error_msg); |
| 191 | |
| 192 | //Unpack the bools |
| 193 | for(int i = 0; i < my_size.x; i++) |
| 194 | d[i] = (bool_rowbuf[i / 8] >> (7 - i % 8)) & 1; |
| 195 | |
| 196 | if(inverted_grey) |
| 197 | invert(d, my_size.x); |
| 198 | |
| 199 | row++; |
| 200 | } |
| 201 | |
| 202 | string TIFFPimpl::datatype() |
| 203 | { |
| 204 | return type; |
| 205 | } |
| 206 | |
| 207 | ImageRef TIFFPimpl::size() |
| 208 | { |
| 209 | return my_size; |
| 210 | } |
| 211 | |
| 212 | TIFFPimpl::~TIFFPimpl() |
| 213 | { |
| 214 | TIFFClose(tif); |
| 215 | } |
| 216 | |
| 217 | //#define CVD_INTERNAL_VERBOSE_TIFF |
| 218 | #ifdef CVD_INTERNAL_VERBOSE_TIFF |
| 219 | #define LOG(X) \ |
| 220 | do \ |
| 221 | { \ |
| 222 | cerr << X; \ |
| 223 | } while(0) |
| 224 | #define VAR(X) #X << " = " << X |
| 225 | #else |
| 226 | #define LOG(X) |
| 227 | #define VAR(X) |
| 228 | #endif |
| 229 | |
| 230 | TIFFPimpl::TIFFPimpl(istream& is) |
| 231 | : i(is) |
| 232 | , row(0) |
| 233 | { |
| 234 | TIFFSetErrorHandler(tiff_error_handler); |
| 235 | |
| 236 | //Find out the file size, and the suitability of the stream |
| 237 | i.seekg(0, ios_base::end); |
| 238 | length = i.tellg(); |
| 239 | if(length == -1) |
| 240 | throw UnseekableIstream("TIFF"); |
| 241 | i.seekg(0, ios_base::beg); |
| 242 | |
| 243 | tif = TIFFClientOpen("std::istream", "r", this, |
| 244 | read, write, seek, close, size, map, unmap); |
| 245 | |
| 246 | if(tif == NULL) |
| 247 | throw MalformedImage(error_msg); |
| 248 | |
| 249 | #ifdef CVD_INTERNAL_VERBOSE_TIFF |
| 250 | { |
| 251 | int dircount = 1; |
| 252 | for(; TIFFReadDirectory(tif); dircount++) |
| 253 | { } |
| 254 | |
| 255 | LOG(VAR(dircount)); |
| 256 | TIFFSetDirectory(tif, 0); |
| 257 | } |
| 258 | #endif |
| 259 | |
| 260 | //Libtiff types |
| 261 | uint32_t w = 0, h = 0; |
| 262 | uint16_t bitspersample = 0, spp = 0, sampleformat = 0, photo = 0, pl_type = 0; |
| 263 | |
| 264 | TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w); |
| 265 | TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); |
| 266 | TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitspersample); |
| 267 | |
| 268 | TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &spp); |
| 269 | |
| 270 | TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photo); |
| 271 | TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pl_type); |
| 272 | |
| 273 | LOG(VAR(w) << endl); |
| 274 | LOG(VAR(h) << endl); |
| 275 | LOG(VAR(bitspersample) << endl); |
| 276 | LOG(VAR(spp) << endl); |
| 277 | LOG(VAR(photo) << endl); |
| 278 | LOG(VAR(pl_type) << endl); |
| 279 | |
| 280 | //Read the sample format. If it is missing, then it |
| 281 | //defaults to unsigned int as per the spec. |
| 282 | if(TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &sampleformat) == 0) |
| 283 | sampleformat = SAMPLEFORMAT_UINT; |
| 284 | |
| 285 | LOG(VAR(sampleformat) << endl); |
| 286 | |
| 287 | my_size.x = w; |
| 288 | my_size.y = h; |
| 289 | use_cooked_rgba_interface = 1; //This is the default |
| 290 | inverted_grey = 0; |
| 291 | |
| 292 | //Can we use our own interface? |
| 293 | //The alternative is the cooked RGBA interface which assumes all |
| 294 | //the world is 8 bits RGBA. This will load almost all images, but |
| 295 | //special features, such as higher bit depths will be lost. |
| 296 | //Also, the entire image has to be loaded at once, so this can use large |
| 297 | //amounts of memory. |
| 298 | if((photo == PHOTOMETRIC_RGB || photo == PHOTOMETRIC_MINISWHITE || photo == PHOTOMETRIC_MINISBLACK)) |
| 299 | { |
| 300 | //We stand a chane here... |
| 301 | if(photo == PHOTOMETRIC_MINISWHITE) |
| 302 | inverted_grey = 1; |
| 303 | |
| 304 | //Figure out the basic datatype |
| 305 | if(sampleformat == SAMPLEFORMAT_UINT) |
| 306 | { |
| 307 | if(bitspersample == 1) |
| 308 | type = PNM::type_name<bool>::name(); |
| 309 | else if(bitspersample == 8) |
| 310 | type = PNM::type_name<unsigned char>::name(); |
| 311 | else if(bitspersample == 16) |
| 312 | type = PNM::type_name<unsigned short>::name(); |
| 313 | else |
| 314 | goto keep_cooked; |
| 315 | } |
| 316 | else if(sampleformat == SAMPLEFORMAT_IEEEFP) |
| 317 | { |
| 318 | if(bitspersample == 32) |
| 319 | type = PNM::type_name<float>::name(); |
| 320 | else if(bitspersample == 64) |
| 321 | type = PNM::type_name<double>::name(); |
| 322 | else |
| 323 | goto keep_cooked; |
| 324 | } |
| 325 | else |
| 326 | goto keep_cooked; |
| 327 | |
| 328 | //Figure out the colourspace |
| 329 | if(spp == 1) |
| 330 | ; |
| 331 | else if(spp == 2) |
| 332 | type = "CVD::GreyAlpha<" + type + ">"; |
| 333 | else if(spp == 3) |
| 334 | type = "CVD::Rgb<" + type + ">"; |
| 335 | else if(spp == 4) |
| 336 | type = "CVD::Rgba<" + type + ">"; |
| 337 | else |
| 338 | goto keep_cooked; |
| 339 | |
| 340 | use_cooked_rgba_interface = 0; |
| 341 | } |
| 342 | |
| 343 | keep_cooked:; |
| 344 | if(use_cooked_rgba_interface == 1) |
| 345 | { |
| 346 | //The format is "complex" and we don't know how to read it. |
| 347 | type = "CVD::Rgba<unsigned char>"; |
| 348 | inverted_grey = 0; |
| 349 | } |
| 350 | |
| 351 | if(type == "bool") |
| 352 | bool_rowbuf.resize((size().x + 7) / 8); |
| 353 | |
| 354 | LOG(VAR(type) << endl); |
| 355 | LOG(VAR(use_cooked_rgba_interface) << endl); |
| 356 | |
| 357 | if(use_cooked_rgba_interface) |
| 358 | { |
| 359 | raster_data.resize(my_size.x * my_size.y); |
| 360 | |
| 361 | //Read the whole image |
| 362 | if(TIFFReadRGBAImageOriented(tif, my_size.x, my_size.y, &raster_data[0], 0, ORIENTATION_TOPLEFT) == -1) |
| 363 | throw MalformedImage(error_msg); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | //////////////////////////////////////////////////////////////////////////////// |
| 368 | // |
| 369 | // Implementation of public parts of TIFF reading |
| 370 | // |
| 371 | |
| 372 | Reader::Reader(istream& i) |
| 373 | : t(new TIFFPimpl(i)) |
| 374 | { |
| 375 | } |
| 376 | |
| 377 | Reader::~Reader() |
| 378 | { |
| 379 | } |
| 380 | |
| 381 | string Reader::datatype() |
| 382 | { |
| 383 | return t->datatype(); |
| 384 | } |
| 385 | |
| 386 | string Reader::name() |
| 387 | { |
| 388 | return "TIFF"; |
| 389 | } |
| 390 | |
| 391 | bool Reader::top_row_first() |
| 392 | { |
| 393 | return true; |
| 394 | }; |
| 395 | ImageRef Reader::size() |
| 396 | { |
| 397 | return t->size(); |
| 398 | }; |
| 399 | |
| 400 | //Mechanically generate the pixel reading calls. |
| 401 | #define GEN1(X) \ |
| 402 | void Reader::get_raw_pixel_line(X* d) { t->get_raw_pixel_line(d); } |
| 403 | #define GEN3(X) \ |
| 404 | GEN1(X) \ |
| 405 | GEN1(Rgb<X>) \ |
| 406 | GEN1(Rgba<X>) |
| 407 | |
| 408 | GEN1(bool) |
| 409 | GEN3(unsigned char) |
| 410 | GEN3(unsigned short) |
| 411 | GEN3(float) |
| 412 | GEN3(double) |