| 1 | #include "cvd/internal/io/bmp.h" |
| 2 | |
| 3 | #include "cvd/image_io.h" |
| 4 | using namespace std; |
| 5 | #include <iostream> |
| 6 | |
| 7 | namespace CVD |
| 8 | { |
| 9 | namespace BMP |
| 10 | { |
| 11 | |
| 12 | void readBMPHeader(unsigned int& width, unsigned int& height, unsigned int& channels, unsigned int& compression, istream& in); |
| 13 | |
| 14 | class ReadPimpl |
| 15 | { |
| 16 | public: |
| 17 | ReadPimpl(std::istream&); |
| 18 | ImageRef size() { return s; } |
| 19 | void get_raw_pixel_lines(unsigned char*, unsigned long nlines); |
| 20 | string datatype() |
| 21 | { |
| 22 | return type; |
| 23 | } |
| 24 | |
| 25 | void get_raw_pixel_line(byte* d) |
| 26 | { |
| 27 | char dummy[4]; |
| 28 | //Read the data |
| 29 | i.read((char*)d, s.x); |
| 30 | |
| 31 | //Eat the padding |
| 32 | i.read(dummy, rowSize - s.x); |
| 33 | } |
| 34 | |
| 35 | void get_raw_pixel_line(Rgb<byte>* d) |
| 36 | { |
| 37 | char dummy[4]; |
| 38 | //if(channels == 1) |
| 39 | //Depalette image |
| 40 | //Not supported |
| 41 | |
| 42 | i.read((char*)d, s.x * 3); |
| 43 | //Turn bgr into rgb |
| 44 | for(int x = 0; x < s.x; x++) |
| 45 | swap(d[x].red, d[x].blue); |
| 46 | |
| 47 | //Eat the padding |
| 48 | i.read(dummy, rowSize - s.x * 3); |
| 49 | } |
| 50 | |
| 51 | template <class T> |
| 52 | void get_raw_pixel_line(T* d) |
| 53 | { |
| 54 | if(datatype() != PNM::type_name<T>::name()) |
| 55 | throw CVD::Exceptions::Image_IO::ReadTypeMismatch(datatype(), PNM::type_name<T>::name()); |
| 56 | get_raw_pixel_line(d); |
| 57 | //FIXME: check rows. |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | ImageRef s; |
| 62 | std::istream& i; |
| 63 | string type; |
| 64 | int channels; |
| 65 | int rowSize; |
| 66 | bool notgray; |
| 67 | //Rgb<byte> palette[256]; |
| 68 | }; |
| 69 | |
| 70 | ReadPimpl::ReadPimpl(std::istream& in) |
| 71 | : i(in) |
| 72 | { |
| 73 | unsigned int w, h, ch, comp; |
| 74 | readBMPHeader(w, h, ch, comp, in); |
| 75 | |
| 76 | channels = ch; |
| 77 | s.x = w; |
| 78 | s.y = h; |
| 79 | |
| 80 | if(channels == 1) |
| 81 | { |
| 82 | //Read the palette. Greyscale BMPs are implemented using palettes |
| 83 | notgray = false; |
| 84 | for(int i = 0; i < 256; i++) |
| 85 | { |
| 86 | byte buf[4]; |
| 87 | in.read((char*)buf, 4); |
| 88 | //palette[i].red = buf[2]; |
| 89 | //palette[i].green = buf[1]; |
| 90 | //palette[i].blue = buf[0]; |
| 91 | if(buf[0] != i || buf[0] != buf[1] || buf[1] != buf[2]) |
| 92 | notgray = true; |
| 93 | } |
| 94 | |
| 95 | if(notgray) |
| 96 | throw(Exceptions::Image_IO::UnsupportedImageSubType("Windows BMP/DIB", "Nontrivial paletted images.")); |
| 97 | |
| 98 | rowSize = s.x; |
| 99 | } |
| 100 | else |
| 101 | rowSize = s.x * 3; |
| 102 | |
| 103 | if(rowSize % 4) |
| 104 | rowSize += 4 - (rowSize % 4); |
| 105 | |
| 106 | if(channels == 3 || notgray) |
| 107 | type = PNM::type_name<Rgb<byte>>::name(); |
| 108 | else |
| 109 | type = PNM::type_name<byte>::name(); |
| 110 | } |
| 111 | |
| 112 | //////////////////////////////////////////////////////////////////////////////// |
| 113 | // |
| 114 | // The public interface |
| 115 | // |
| 116 | |
| 117 | ImageRef Reader::size() |
| 118 | { |
| 119 | return t->size(); |
| 120 | } |
| 121 | |
| 122 | void Reader::get_raw_pixel_line(unsigned char* d) |
| 123 | { |
| 124 | t->get_raw_pixel_line(d); |
| 125 | } |
| 126 | |
| 127 | void Reader::get_raw_pixel_line(Rgb<byte>* d) |
| 128 | { |
| 129 | t->get_raw_pixel_line(d); |
| 130 | } |
| 131 | |
| 132 | string Reader::datatype() |
| 133 | { |
| 134 | return t->datatype(); |
| 135 | } |
| 136 | |
| 137 | bool Reader::top_row_first() |
| 138 | { |
| 139 | return false; |
| 140 | } |
| 141 | string Reader::name() |
| 142 | { |
| 143 | return "BMP"; |
| 144 | } |
| 145 | |
| 146 | Reader::~Reader() |
| 147 | { |
| 148 | } |
| 149 | |
| 150 | Reader::Reader(std::istream& i) |
| 151 | : t(new ReadPimpl(i)) |
| 152 | { |
| 153 | } |
| 154 | |
| 155 | } |
| 156 | } |