| 1 | #include "cvd/internal/io/fits.h" |
| 2 | #include "cvd/image_io.h" |
| 3 | #include "cvd_src/config_internal.h" |
| 4 | #include <algorithm> |
| 5 | #include <deque> |
| 6 | #include <iostream> |
| 7 | #include <sstream> |
| 8 | #include <vector> |
| 9 | |
| 10 | using namespace CVD; |
| 11 | using namespace CVD::FITS; |
| 12 | using namespace CVD::Exceptions::Image_IO; |
| 13 | using namespace std; |
| 14 | |
| 15 | //////////////////////////////////////////////////////////////////////////////// |
| 16 | // |
| 17 | // Private implementation of FITS reading |
| 18 | // |
| 19 | |
| 20 | //////////////////////////////////////////////////////////////////////////////// |
| 21 | // |
| 22 | // Useful string manipulation functions |
| 23 | // |
| 24 | |
| 25 | //Trim off whitespace on the right hand side |
| 26 | static string rtrim(string str) |
| 27 | { |
| 28 | string ws(" \t\f\v\n\r"); |
| 29 | size_t pos; |
| 30 | |
| 31 | pos = str.find_last_not_of(ws); |
| 32 | if(pos != string::npos) |
| 33 | str.erase(pos + 1); |
| 34 | else |
| 35 | str.clear(); |
| 36 | |
| 37 | return str; |
| 38 | } |
| 39 | |
| 40 | //Trim off whitespace on the left hand side |
| 41 | static string ltrim(const string& str) |
| 42 | { |
| 43 | string ws(" \t\f\v\n\r"); |
| 44 | size_t pos; |
| 45 | |
| 46 | pos = str.find_first_not_of(ws); |
| 47 | if(pos != string::npos) |
| 48 | return string(str.begin() + pos, str.end()); |
| 49 | else |
| 50 | return ""; |
| 51 | } |
| 52 | |
| 53 | static int get_int(const string& s) |
| 54 | { |
| 55 | int i; |
| 56 | istringstream is(s); |
| 57 | is >> i; |
| 58 | |
| 59 | if(s.size() == 0 || is.bad()) |
| 60 | throw(Exceptions::Image_IO::MalformedImage("Invalid integer: `" + s + "'")); |
| 61 | return i; |
| 62 | } |
| 63 | |
| 64 | static int get_uint(const string& s) |
| 65 | { |
| 66 | unsigned int i; |
| 67 | istringstream is(s); |
| 68 | is >> i; |
| 69 | |
| 70 | if(s.size() == 0 || is.bad()) |
| 71 | throw(Exceptions::Image_IO::MalformedImage("Invalid unsigned integer: `" + s + "'")); |
| 72 | return i; |
| 73 | } |
| 74 | |
| 75 | static double get_double(const string& s) |
| 76 | { |
| 77 | double i; |
| 78 | istringstream is(s); |
| 79 | is >> i; |
| 80 | |
| 81 | if(s.size() == 0 || is.bad()) |
| 82 | throw(Exceptions::Image_IO::MalformedImage("Invalid double: `" + s + "'")); |
| 83 | return i; |
| 84 | } |
| 85 | |
| 86 | //////////////////////////////////////////////////////////////////////////////// |
| 87 | // |
| 88 | // Private implementation of FITS reading |
| 89 | // |
| 90 | |
| 91 | class CVD::FITS::ReadPimpl |
| 92 | { |
| 93 | public: |
| 94 | ReadPimpl(istream&); |
| 95 | ~ReadPimpl(); |
| 96 | ImageRef size(); |
| 97 | string datatype(); |
| 98 | template <class C> |
| 99 | void get_raw_pixel_line(C* data); |
| 100 | |
| 101 | template <class C> |
| 102 | void convert_raw_pixel_line(unsigned char*, C* data); |
| 103 | template <template <class> class C> |
| 104 | void convert_raw_pixel_line(unsigned char*, C<unsigned short>* data); |
| 105 | void convert_raw_pixel_line(unsigned char*, unsigned short* data); |
| 106 | |
| 107 | private: |
| 108 | istream& i; |
| 109 | unsigned long row; |
| 110 | ImageRef my_size; |
| 111 | string type; |
| 112 | |
| 113 | //HDU is the FITS term: header description units. |
| 114 | //The HDU has multiple 36 card stacks of 80 column cards. |
| 115 | //which is 2880 bytes |
| 116 | char HDU_card_stack[2880], *card; |
| 117 | |
| 118 | void next_card() |
| 119 | { |
| 120 | if(card != NULL) |
| 121 | card += 80; |
| 122 | |
| 123 | if(card == NULL || card == HDU_card_stack + 2880) |
| 124 | { |
| 125 | i.read(HDU_card_stack, 2880); |
| 126 | if(i.eof()) |
| 127 | throw(Exceptions::Image_IO::MalformedImage("EOF in header.")); |
| 128 | |
| 129 | card = HDU_card_stack; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | bool match_card_start(const string& s) |
| 134 | { |
| 135 | if(s.size() > 80) |
| 136 | return 0; |
| 137 | return equal(s.begin(), s.end(), card); |
| 138 | } |
| 139 | |
| 140 | //keywords are left justified, in columns 1--8 |
| 141 | string get_keyword() |
| 142 | { |
| 143 | string k; |
| 144 | k.resize(8); |
| 145 | copy(card, card + 8, k.begin()); |
| 146 | return rtrim(k); |
| 147 | } |
| 148 | |
| 149 | void check_for_seperator() |
| 150 | { |
| 151 | if(card[8] != '=' || card[9] != ' ') |
| 152 | throw(Exceptions::Image_IO::MalformedImage("Missing `= ' separator in card.")); |
| 153 | } |
| 154 | |
| 155 | void expect_keyword(const string& k) |
| 156 | { |
| 157 | check_for_seperator(); |
| 158 | if(k != get_keyword()) |
| 159 | throw Exceptions::Image_IO::MalformedImage("FITS images missing " + k + "(got" + get_keyword() + ")"); |
| 160 | } |
| 161 | |
| 162 | //numeric fields are right justified, in columns 11--30 |
| 163 | string get_numeric_field() |
| 164 | { |
| 165 | string f; |
| 166 | f.resize(20); |
| 167 | copy(card + 10, card + 30, f.begin()); |
| 168 | return ltrim(f); |
| 169 | } |
| 170 | |
| 171 | vector<unsigned char> data; |
| 172 | int bytes_per_pixel; |
| 173 | }; |
| 174 | |
| 175 | template <class T> |
| 176 | void ReadPimpl::get_raw_pixel_line(T* d) |
| 177 | { |
| 178 | if(datatype() != PNM::type_name<T>::name()) |
| 179 | throw ReadTypeMismatch(datatype(), PNM::type_name<T>::name()); |
| 180 | |
| 181 | if(row > (unsigned long)my_size.y) |
| 182 | throw InternalLibraryError("CVD", "Read past end of image."); |
| 183 | |
| 184 | unsigned char* rowp = &data[row * bytes_per_pixel * my_size.x]; |
| 185 | row++; |
| 186 | |
| 187 | convert_raw_pixel_line(rowp, d); |
| 188 | } |
| 189 | |
| 190 | template <class T> |
| 191 | void ReadPimpl::convert_raw_pixel_line(unsigned char* rowp, T* d) |
| 192 | { |
| 193 | copy(rowp, rowp + my_size.x * bytes_per_pixel, (unsigned char*)(d)); |
| 194 | } |
| 195 | |
| 196 | template <template <class> class T> |
| 197 | void ReadPimpl::convert_raw_pixel_line(unsigned char* rowp, T<unsigned short>* d) |
| 198 | { |
| 199 | unsigned short* ds = (unsigned short*)d; |
| 200 | int elements = my_size.x * static_cast<int>(sizeof(T<unsigned short>) / sizeof(unsigned short)); |
| 201 | for(int i = 0; i < elements; i++) |
| 202 | ds[i] = (unsigned short)(static_cast<int>(((short*)(rowp))[i]) + 32768); |
| 203 | } |
| 204 | |
| 205 | void ReadPimpl::convert_raw_pixel_line(unsigned char* rowp, unsigned short* d) |
| 206 | { |
| 207 | for(int i = 0; i < my_size.x; i++) |
| 208 | d[i] = (unsigned short)(static_cast<int>(((short*)(rowp))[i]) + 32768); |
| 209 | } |
| 210 | |
| 211 | string ReadPimpl::datatype() |
| 212 | { |
| 213 | return type; |
| 214 | } |
| 215 | |
| 216 | ImageRef ReadPimpl::size() |
| 217 | { |
| 218 | return my_size; |
| 219 | } |
| 220 | |
| 221 | ReadPimpl::~ReadPimpl() |
| 222 | { |
| 223 | } |
| 224 | |
| 225 | ReadPimpl::ReadPimpl(istream& is) |
| 226 | : i(is) |
| 227 | , row(0) |
| 228 | , card(NULL) |
| 229 | { |
| 230 | next_card(); |
| 231 | if(!match_card_start("SIMPLE = T")) |
| 232 | throw(Exceptions::Image_IO::MalformedImage("FITS images must start with \"SIMPLE = T\"")); |
| 233 | |
| 234 | next_card(); |
| 235 | expect_keyword("BITPIX"); |
| 236 | int bpp = get_int(get_numeric_field()); |
| 237 | |
| 238 | if(bpp == 8) |
| 239 | type = PNM::type_name<byte>::name(); |
| 240 | else if(bpp == 16) |
| 241 | type = PNM::type_name<short>::name(); |
| 242 | else if(bpp == 32) |
| 243 | type = PNM::type_name<int>::name(); |
| 244 | else if(bpp == -32) |
| 245 | type = PNM::type_name<float>::name(); |
| 246 | else if(bpp == -64) |
| 247 | type = PNM::type_name<double>::name(); |
| 248 | else |
| 249 | throw Exceptions::Image_IO::MalformedImage("FITS images has unrecognised BITPIX (" + get_numeric_field() + ")"); |
| 250 | |
| 251 | unsigned int num_axes; |
| 252 | next_card(); |
| 253 | expect_keyword("NAXIS"); |
| 254 | num_axes = get_uint(get_numeric_field()); |
| 255 | |
| 256 | if(num_axes < 1 || num_axes > 3) |
| 257 | throw Exceptions::Image_IO::UnsupportedImageSubType("FITS", get_numeric_field() + " axes given (1, 2 or 3 supported)."); |
| 258 | |
| 259 | //Read in the axis lengths |
| 260 | //select obvious defailts for missing axes |
| 261 | int a1 = 0, a2 = 1, a3 = 1; |
| 262 | next_card(); |
| 263 | expect_keyword("NAXIS1"); |
| 264 | a1 = get_uint(get_numeric_field()); |
| 265 | |
| 266 | if(num_axes > 1) |
| 267 | { |
| 268 | next_card(); |
| 269 | expect_keyword("NAXIS2"); |
| 270 | a2 = get_uint(get_numeric_field()); |
| 271 | |
| 272 | if(num_axes > 2) |
| 273 | { |
| 274 | next_card(); |
| 275 | expect_keyword("NAXIS3"); |
| 276 | a3 = get_uint(get_numeric_field()); |
| 277 | } |
| 278 | } |
| 279 | my_size.x = a1; |
| 280 | my_size.y = a2; |
| 281 | |
| 282 | double add_for_zero = 0; |
| 283 | string bzero; |
| 284 | |
| 285 | //Read the rest of the header |
| 286 | //The last useful card has END as the first keyword, but no separator. |
| 287 | //The remaining cards in the current stack should be blank. |
| 288 | //Since we read a stack at a time, the read pointer will be in the |
| 289 | //correct place. |
| 290 | for(;;) |
| 291 | { |
| 292 | next_card(); |
| 293 | if(get_keyword() == "BZERO") |
| 294 | { |
| 295 | check_for_seperator(); |
| 296 | add_for_zero = get_double(bzero = get_numeric_field()); |
| 297 | } |
| 298 | else if(get_keyword() == "END") |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | if(add_for_zero == 0) //OK |
| 303 | { |
| 304 | } |
| 305 | else if(add_for_zero == 32768. && bpp == 16) |
| 306 | type = "unsigned short"; |
| 307 | else |
| 308 | throw Exceptions::Image_IO::UnsupportedImageSubType("FITS", "BZERO is " + bzero + ". Only 0 or 32768 (for int16 images) supported"); |
| 309 | |
| 310 | //Figure out the colourspace |
| 311 | if(a3 == 1) |
| 312 | ; |
| 313 | else if(a3 == 2) |
| 314 | type = "CVD::GreyAlpha<" + type + ">"; |
| 315 | else if(a3 == 3) |
| 316 | type = "CVD::Rgb<" + type + ">"; |
| 317 | else if(a3 == 4) |
| 318 | type = "CVD::Rgba<" + type + ">"; |
| 319 | else |
| 320 | throw Exceptions::Image_IO::UnsupportedImageSubType("FITS", "3rd axis (colour planes) has" + get_numeric_field() + " elements(1, 2, 3 or 4 supported)."); |
| 321 | |
| 322 | //We should really use BZERO, too. |
| 323 | |
| 324 | //Images are planar. So, deplanarize here. |
| 325 | //Also, correct the endianness |
| 326 | //FIXME this could be much more efficient. |
| 327 | bpp = abs(bpp) / 8; |
| 328 | |
| 329 | size_t nelems = a1 * a2 * a3; |
| 330 | size_t nbytes = nelems * bpp; |
| 331 | vector<unsigned char> raw_data; |
| 332 | raw_data.resize(nbytes); |
| 333 | data.resize(nbytes); |
| 334 | i.read((char*)(&raw_data[0]), nbytes); |
| 335 | |
| 336 | if(i.eof()) |
| 337 | throw(Exceptions::Image_IO::MalformedImage("EOF in image.")); |
| 338 | |
| 339 | //Make the data follow the native byte ordering |
| 340 | #ifdef CVD_INTERNAL_ARCH_LITTLE_ENDIAN |
| 341 | for(size_t i = 0; i < nelems; i++) |
| 342 | reverse(&raw_data[i * bpp], &raw_data[i * bpp] + bpp); |
| 343 | #elif defined CVD_INTERNAL_ARCH_BIG_ENDIAN |
| 344 | |
| 345 | #else |
| 346 | #error No endianness defined. |
| 347 | #endif |
| 348 | |
| 349 | //Convert planar to inline |
| 350 | for(int c = 0; c < a1; c++) |
| 351 | for(int r = 0; r < a2; r++) |
| 352 | for(int p = 0; p < a3; p++) |
| 353 | for(int b = 0; b < bpp; b++) |
| 354 | data[((r * a1 + c) * a3 + p) * bpp + b] = raw_data[p * a1 * a2 * bpp + ((a2 - r - 1) * a1 + c) * bpp + b]; |
| 355 | |
| 356 | bytes_per_pixel = a3 * bpp; |
| 357 | } |
| 358 | |
| 359 | //////////////////////////////////////////////////////////////////////////////// |
| 360 | // |
| 361 | // Implementation of public parts of TIFF reading |
| 362 | // |
| 363 | |
| 364 | Reader::Reader(istream& i) |
| 365 | : t(new ReadPimpl(i)) |
| 366 | { |
| 367 | } |
| 368 | |
| 369 | Reader::~Reader() |
| 370 | { |
| 371 | } |
| 372 | |
| 373 | string Reader::datatype() |
| 374 | { |
| 375 | return t->datatype(); |
| 376 | } |
| 377 | |
| 378 | string Reader::name() |
| 379 | { |
| 380 | return "FITS"; |
| 381 | } |
| 382 | |
| 383 | ImageRef Reader::size() |
| 384 | { |
| 385 | return t->size(); |
| 386 | }; |
| 387 | |
| 388 | bool Reader::top_row_first() |
| 389 | { |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | //Mechanically generate the pixel reading calls. |
| 394 | #define GEN1(X) \ |
| 395 | void Reader::get_raw_pixel_line(X* d) { t->get_raw_pixel_line(d); } |
| 396 | #define GEN3(X) \ |
| 397 | GEN1(X) \ |
| 398 | GEN1(Rgb<X>) \ |
| 399 | GEN1(Rgba<X>) |
| 400 | |
| 401 | GEN3(unsigned char) |
| 402 | GEN3(signed short) |
| 403 | GEN3(unsigned short) |
| 404 | GEN3(signed int) |
| 405 | GEN3(float) |
| 406 | GEN3(double) |