| 1 | #include "cvd/image_io.h" |
| 2 | #include <iostream> |
| 3 | #include <iterator> |
| 4 | #include <sstream> |
| 5 | #include <vector> |
| 6 | |
| 7 | using namespace CVD; |
| 8 | using namespace CVD::TEXT; |
| 9 | using namespace CVD::Exceptions::Image_IO; |
| 10 | using namespace std; |
| 11 | |
| 12 | //////////////////////////////////////////////////////////////////////////////// |
| 13 | // |
| 14 | // Private implementation of TIFF reading |
| 15 | // |
| 16 | |
| 17 | class CVD::TEXT::ReadPimpl |
| 18 | { |
| 19 | public: |
| 20 | ReadPimpl(istream&); |
| 21 | ~ReadPimpl(); |
| 22 | ImageRef size(); |
| 23 | string datatype(); |
| 24 | void get_raw_pixel_line(double* data); |
| 25 | |
| 26 | private: |
| 27 | istream& i; |
| 28 | unsigned long row; |
| 29 | ImageRef my_size; |
| 30 | |
| 31 | vector<vector<double>> raster_data; |
| 32 | }; |
| 33 | |
| 34 | void ReadPimpl::get_raw_pixel_line(double* d) |
| 35 | { |
| 36 | if(row > (unsigned long)my_size.y) |
| 37 | throw InternalLibraryError("CVD", "Read past end of image."); |
| 38 | |
| 39 | copy(raster_data[row].begin(), raster_data[row].end(), d); |
| 40 | row++; |
| 41 | } |
| 42 | |
| 43 | string ReadPimpl::datatype() |
| 44 | { |
| 45 | return "double"; |
| 46 | } |
| 47 | |
| 48 | ImageRef ReadPimpl::size() |
| 49 | { |
| 50 | return my_size; |
| 51 | } |
| 52 | |
| 53 | ReadPimpl::~ReadPimpl() |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | ReadPimpl::ReadPimpl(istream& is) |
| 58 | : i(is) |
| 59 | , row(0) |
| 60 | { |
| 61 | my_size.x = -1; |
| 62 | my_size.y = 0; |
| 63 | |
| 64 | string line; |
| 65 | raster_data.reserve(8192); |
| 66 | |
| 67 | for(;;) |
| 68 | { |
| 69 | getline(i, line); |
| 70 | |
| 71 | if(i.fail()) |
| 72 | break; |
| 73 | |
| 74 | istringstream l(line); |
| 75 | raster_data.resize(my_size.y + 1); |
| 76 | copy(istream_iterator<double>(l), istream_iterator<double>(), back_inserter(raster_data.back())); |
| 77 | |
| 78 | if(raster_data.back().size() == 0) |
| 79 | break; |
| 80 | |
| 81 | if(my_size.y == 0) |
| 82 | my_size.x = static_cast<int>(raster_data[0].size()); |
| 83 | else if(my_size.x != (int)raster_data.back().size()) |
| 84 | { |
| 85 | ostringstream err; |
| 86 | err << "All rows must have the same number of columns: bad row is " |
| 87 | << my_size.y; |
| 88 | throw MalformedImage(err.str()); |
| 89 | } |
| 90 | |
| 91 | my_size.y++; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | //////////////////////////////////////////////////////////////////////////////// |
| 96 | // |
| 97 | // Implementation of public parts of text reading |
| 98 | // |
| 99 | |
| 100 | Reader::Reader(istream& i) |
| 101 | : t(new ReadPimpl(i)) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | Reader::~Reader() |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | string Reader::datatype() |
| 110 | { |
| 111 | return t->datatype(); |
| 112 | } |
| 113 | |
| 114 | bool Reader::top_row_first() |
| 115 | { |
| 116 | return true; |
| 117 | } |
| 118 | string Reader::name() |
| 119 | { |
| 120 | return "TEXT"; |
| 121 | } |
| 122 | |
| 123 | ImageRef Reader::size() |
| 124 | { |
| 125 | return t->size(); |
| 126 | }; |
| 127 | |
| 128 | //Mechanically generate the pixel reading calls. |
| 129 | void Reader::get_raw_pixel_line(double* d) { t->get_raw_pixel_line(d); } |