| 1 | #include "cvd/image_io.h" |
| 2 | #include "cvd/internal/io/text.h" |
| 3 | #include <algorithm> |
| 4 | #include <iomanip> |
| 5 | #include <iostream> |
| 6 | #include <iterator> |
| 7 | |
| 8 | using namespace CVD; |
| 9 | using namespace CVD::TEXT; |
| 10 | using namespace CVD::Exceptions::Image_IO; |
| 11 | using namespace std; |
| 12 | |
| 13 | class CVD::TEXT::WritePimpl |
| 14 | { |
| 15 | public: |
| 16 | WritePimpl(ostream& oo, ImageRef size, const string& t) |
| 17 | : o(oo) |
| 18 | , my_size(size) |
| 19 | , type(t) |
| 20 | , row(0) |
| 21 | { |
| 22 | if(t != "double" && t != "float") |
| 23 | throw UnsupportedImageSubType("TEXT", t); |
| 24 | } |
| 25 | |
| 26 | ~WritePimpl() |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | void write_raw_pixel_line(const double* d) |
| 31 | { |
| 32 | if(type != "double") |
| 33 | throw WriteTypeMismatch(type, "double"); |
| 34 | |
| 35 | //Do some sanity checking |
| 36 | if(row >= (unsigned long)my_size.y) |
| 37 | throw InternalLibraryError("CVD", "Write past end of image."); |
| 38 | o << scientific << setw(24) << setprecision(16); |
| 39 | |
| 40 | copy(d, d + my_size.x - 1, ostream_iterator<double>(o, " ")); |
| 41 | o << d[my_size.x - 1] << endl; |
| 42 | row++; |
| 43 | } |
| 44 | |
| 45 | void write_raw_pixel_line(const float* d) |
| 46 | { |
| 47 | if(type != "float") |
| 48 | throw WriteTypeMismatch(type, "float"); |
| 49 | |
| 50 | //Do some sanity checking |
| 51 | if(row >= (unsigned long)my_size.y) |
| 52 | throw InternalLibraryError("CVD", "Write past end of image."); |
| 53 | o << scientific << setw(15) << setprecision(8); |
| 54 | |
| 55 | copy(d, d + my_size.x - 1, ostream_iterator<float>(o, " ")); |
| 56 | o << d[my_size.x - 1] << endl; |
| 57 | row++; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | ostream& o; |
| 62 | ImageRef my_size; |
| 63 | string type; |
| 64 | unsigned long row; |
| 65 | }; |
| 66 | |
| 67 | TEXT::writer::writer(ostream& o, ImageRef size, const string& type, const std::map<std::string, Parameter<>>&) |
| 68 | : t(new WritePimpl(o, size, type)) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | TEXT::writer::~writer() |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | void TEXT::writer::write_raw_pixel_line(const double* d) |
| 77 | { |
| 78 | t->write_raw_pixel_line(d); |
| 79 | } |
| 80 | |
| 81 | void TEXT::writer::write_raw_pixel_line(const float* d) |
| 82 | { |
| 83 | t->write_raw_pixel_line(d); |
| 84 | } |