| 1 | #include "cvd/image_io.h" |
| 2 | #include "cvd/internal/io/fits.h" |
| 3 | #include "cvd_src/config_internal.h" |
| 4 | #include <algorithm> |
| 5 | #include <iomanip> |
| 6 | #include <iostream> |
| 7 | #include <sstream> |
| 8 | |
| 9 | using namespace CVD; |
| 10 | using namespace CVD::FITS; |
| 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::FITS::WritePimpl |
| 20 | { |
| 21 | public: |
| 22 | WritePimpl(ostream&, ImageRef size, const string& t); |
| 23 | ~WritePimpl(); |
| 24 | |
| 25 | template <class C> |
| 26 | void write_raw_pixel_line(const C* data); |
| 27 | template <class C> |
| 28 | void write_raw_pixel_line_(const C* data); |
| 29 | void write_raw_pixel_line_(const unsigned short* dat); |
| 30 | void write_raw_pixel_line_(const Rgb<unsigned short>* dat); |
| 31 | void write_raw_pixel_line_(const Rgba<unsigned short>* dat); |
| 32 | void axes(const string& s, int x, int y); |
| 33 | void write(const string& s); |
| 34 | void write_ushort(uint16_t s); |
| 35 | |
| 36 | private: |
| 37 | ostream& o; |
| 38 | ImageRef my_size; |
| 39 | string type; |
| 40 | unsigned long row; |
| 41 | unsigned int cards; |
| 42 | int bpp, a1, a2, a3; |
| 43 | bool raw; |
| 44 | vector<unsigned char> data; |
| 45 | }; |
| 46 | |
| 47 | void WritePimpl::write(const string& s) |
| 48 | { |
| 49 | string header; |
| 50 | header.resize(80); |
| 51 | fill(header.begin(), header.end(), ' '); |
| 52 | copy(s.begin(), s.end(), header.begin()); |
| 53 | o << header; |
| 54 | cards++; |
| 55 | } |
| 56 | |
| 57 | string int8(int i) |
| 58 | { |
| 59 | ostringstream o; |
| 60 | o << setw(8) << setfill(' ') << i; |
| 61 | return o.str(); |
| 62 | } |
| 63 | |
| 64 | void WritePimpl::axes(const string& s, int x, int y) |
| 65 | { |
| 66 | if(s.substr(0, 9) == "CVD::Rgba") |
| 67 | { |
| 68 | write("NAXIS = 3"); |
| 69 | write("NAXIS1 = " + int8(x)); |
| 70 | write("NAXIS2 = " + int8(y)); |
| 71 | write("NAXIS3 = 4"); |
| 72 | a3 = 4; |
| 73 | } |
| 74 | else if(s.substr(0, 8) == "CVD::Rgb") |
| 75 | { |
| 76 | write("NAXIS = 3"); |
| 77 | write("NAXIS1 = " + int8(x)); |
| 78 | write("NAXIS2 = " + int8(y)); |
| 79 | write("NAXIS3 = 3"); |
| 80 | a3 = 3; |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | write("NAXIS = 2"); |
| 85 | write("NAXIS1 = " + int8(x)); |
| 86 | write("NAXIS2 = " + int8(y)); |
| 87 | a3 = 1; |
| 88 | } |
| 89 | a1 = x; |
| 90 | a2 = y; |
| 91 | } |
| 92 | |
| 93 | template <class C> |
| 94 | void WritePimpl::write_raw_pixel_line(const C* dat) |
| 95 | { |
| 96 | //Do some type checking |
| 97 | if(type != PNM::type_name<C>::name()) |
| 98 | throw WriteTypeMismatch(type, PNM::type_name<C>::name()); |
| 99 | |
| 100 | //Do some sanity checking |
| 101 | if(row >= (unsigned long)my_size.y) |
| 102 | throw InternalLibraryError("CVD", "Write past end of image."); |
| 103 | |
| 104 | write_raw_pixel_line_(dat); |
| 105 | row++; |
| 106 | } |
| 107 | |
| 108 | template <class C> |
| 109 | void WritePimpl::write_raw_pixel_line_(const C* dat) |
| 110 | { |
| 111 | const unsigned char* d = reinterpret_cast<const unsigned char*>(dat); |
| 112 | for(unsigned int i = 0; i < my_size.x * sizeof(C); i++) |
| 113 | data.push_back(d[i]); |
| 114 | } |
| 115 | |
| 116 | void WritePimpl::write_ushort(uint16_t s) |
| 117 | { |
| 118 | int num = s - 32768; |
| 119 | data.push_back(static_cast<uint8_t>((num & 0xff00) >> 8)); |
| 120 | data.push_back(static_cast<uint8_t>(num & 0xff)); |
| 121 | } |
| 122 | |
| 123 | void WritePimpl::write_raw_pixel_line_(const unsigned short* dat) |
| 124 | { |
| 125 | for(int i = 0; i < my_size.x; i++) |
| 126 | write_ushort(dat[i]); |
| 127 | } |
| 128 | |
| 129 | void WritePimpl::write_raw_pixel_line_(const Rgb<unsigned short>* dat) |
| 130 | { |
| 131 | for(int i = 0; i < my_size.x; i++) |
| 132 | { |
| 133 | write_ushort(dat[i].red); |
| 134 | write_ushort(dat[i].green); |
| 135 | write_ushort(dat[i].blue); |
| 136 | } |
| 137 | } |
| 138 | void WritePimpl::write_raw_pixel_line_(const Rgba<unsigned short>* dat) |
| 139 | { |
| 140 | for(int i = 0; i < my_size.x; i++) |
| 141 | { |
| 142 | write_ushort(dat[i].red); |
| 143 | write_ushort(dat[i].green); |
| 144 | write_ushort(dat[i].blue); |
| 145 | write_ushort(dat[i].alpha); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | WritePimpl::WritePimpl(ostream& os, ImageRef s, const string& t) |
| 150 | : o(os) |
| 151 | , my_size(s) |
| 152 | , type(t) |
| 153 | , row(0) |
| 154 | , cards(0) |
| 155 | { |
| 156 | raw = 1; |
| 157 | write("SIMPLE = T"); |
| 158 | |
| 159 | if(type == "unsigned char" || type == "CVD::Rgb<unsigned char>" || type == "CVD::Rgba<unsigned char>") |
| 160 | { |
| 161 | write("BITPIX = 8"); |
| 162 | axes(type, s.x, s.y); |
| 163 | bpp = 1; |
| 164 | } |
| 165 | else if(type == "short" || type == "CVD::Rgb<short>" || type == "CVD::Rgba<short>") |
| 166 | { |
| 167 | write("BITPIX = 16"); |
| 168 | axes(type, s.x, s.y); |
| 169 | bpp = 2; |
| 170 | } |
| 171 | else if(type == "unsigned short" || type == "CVD::Rgb<unsigned short>" || type == "CVD::Rgba<unsigned short>") |
| 172 | { |
| 173 | write("BITPIX = 16"); |
| 174 | axes(type, s.x, s.y); |
| 175 | write("BZERO = 32768"); |
| 176 | bpp = 2; |
| 177 | raw = 0; |
| 178 | } |
| 179 | else if(type == "int" || type == "CVD::Rgb<int>" || type == "CVD::Rgba<int>") |
| 180 | { |
| 181 | write("BITPIX = 32"); |
| 182 | axes(type, s.x, s.y); |
| 183 | bpp = 4; |
| 184 | } |
| 185 | else if(type == "float" || type == "CVD::Rgb<float>" || type == "CVD::Rgba<float>") |
| 186 | { |
| 187 | write("BITPIX = -32"); |
| 188 | axes(type, s.x, s.y); |
| 189 | bpp = 4; |
| 190 | } |
| 191 | else if(type == "double" || type == "CVD::Rgb<double>" || type == "CVD::Rgba<double>") |
| 192 | { |
| 193 | write("BITPIX = -64"); |
| 194 | axes(type, s.x, s.y); |
| 195 | bpp = 8; |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | throw UnsupportedImageSubType("FITS", t); |
| 200 | } |
| 201 | |
| 202 | write("END"); |
| 203 | |
| 204 | while(cards % 36) |
| 205 | write(""); |
| 206 | } |
| 207 | |
| 208 | WritePimpl::~WritePimpl() |
| 209 | { |
| 210 | //Make the data big endian |
| 211 | #ifdef CVD_INTERNAL_ARCH_LITTLE_ENDIAN |
| 212 | size_t nelems = data.size() / bpp; |
| 213 | if(raw) |
| 214 | for(size_t i = 0; i < nelems; i++) |
| 215 | reverse(&data[i * bpp], &data[i * bpp] + bpp); |
| 216 | #elif defined CVD_INTERNAL_ARCH_BIG_ENDIAN |
| 217 | |
| 218 | #else |
| 219 | #error No endianness specified |
| 220 | #endif |
| 221 | |
| 222 | vector<unsigned char> raw_data(data.size()); |
| 223 | |
| 224 | //Convert inline to planar and flip |
| 225 | for(int c = 0; c < a1; c++) |
| 226 | for(int r = 0; r < a2; r++) |
| 227 | for(int p = 0; p < a3; p++) |
| 228 | for(int b = 0; b < bpp; b++) |
| 229 | raw_data[p * a1 * a2 * bpp + (r * a1 + c) * bpp + b] = data[(((a2 - r - 1) * a1 + c) * a3 + p) * bpp + b]; |
| 230 | |
| 231 | o.write((char*)&raw_data[0], raw_data.size()); |
| 232 | } |
| 233 | |
| 234 | //////////////////////////////////////////////////////////////////////////////// |
| 235 | // |
| 236 | // Implementation of public parts of FITS reading |
| 237 | // |
| 238 | |
| 239 | CVD::FITS::writer::writer(ostream& o, ImageRef size, const string& type, const map<string, Parameter<>>&) |
| 240 | : t(new WritePimpl(o, size, type)) |
| 241 | { |
| 242 | } |
| 243 | |
| 244 | CVD::FITS::writer::~writer() |
| 245 | { |
| 246 | delete t; |
| 247 | } |
| 248 | |
| 249 | //Mechanically generate the pixel reading calls. |
| 250 | #define GEN1(X) \ |
| 251 | void CVD::FITS::writer::write_raw_pixel_line(const X* d) { t->write_raw_pixel_line(d); } |
| 252 | #define GEN3(X) \ |
| 253 | GEN1(X) \ |
| 254 | GEN1(Rgb<X>) \ |
| 255 | GEN1(Rgba<X>) |
| 256 | |
| 257 | GEN3(unsigned char) |
| 258 | GEN3(unsigned short) |
| 259 | GEN3(short) |
| 260 | GEN3(int) |
| 261 | GEN3(float) |
| 262 | GEN3(double) |