Debian Codegraph @

cvd_src/image_io/bmp.cxx

raw symbols SCIP
1 #include <cvd/internal/io/bmp.h>
2
3 using namespace std;
4 #include <iomanip>
5 #include <iostream>
6 #include <setjmp.h>
7 #include <sstream>
8
9 namespace CVD
10 {
11 namespace BMP
12 {
13
14 struct BMPHeader
15 {
16 uint32_t size; //!< header size
17 uint16_t reserved[2]; //!< reserved
18 uint32_t dataOffset; //!< offset to beginning of data
19 uint32_t infoSize; //!< size of info
20 uint32_t width; //!< width of image
21 uint32_t height; //!< height of image
22 uint16_t planes; //!< planes
23 uint16_t bpp; //!< colour depth
24 uint32_t compression; //!< compression type
25 uint32_t dataSize; //!< size of data
26 uint32_t xPelsPerMeter; //!< x resolution in pixels/meter
27 uint32_t yPelsPerMeter; //!< y resolution in pixels/meter
28 uint32_t colors; //!< number of specified colors in the map
29 uint32_t importantColors; //!< number of important colors
30 };
31
32 void write_u4(ostream& o, unsigned long l)
33 {
34 o << (unsigned char)(0xff & (l))
35 << (unsigned char)(0xff & (l >> 8))
36 << (unsigned char)(0xff & (l >> 16))
37 << (unsigned char)(0xff & (l >> 24));
38 }
39
40 void write_u1(ostream& o, unsigned long l)
41 {
42 o << (unsigned char)(0xff & (l));
43 }
44
45 void write_u2(ostream& o, unsigned long l)
46 {
47 o << (unsigned char)(0xff & (l))
48 << (unsigned char)(0xff & (l >> 8));
49 }
50
51 inline uint32_t read_u4(istream& i)
52 {
53 //Data is little endian:
54 //unsigned long r = i.get() | (i.get() << 8) | (i.get() << 16) | (i.get() << 24 );
55 int a = i.get();
56 int b = i.get();
57 int c = i.get();
58 int d = i.get();
59 uint32_t r = a | (b << 8) | (c << 16) | (d << 24);
60 if(i.eof())
61 throw(Exceptions::Image_IO::MalformedImage("EOF in header."));
62
63 return r;
64 }
65
66 inline uint16_t read_u2(istream& i)
67 {
68 //Data is little endian:
69 //unsigned long r = i.get() | (i.get() << 8);
70 int a = i.get();
71 int b = i.get();
72 if(i.eof())
73 throw(Exceptions::Image_IO::MalformedImage("EOF in header."));
74
75 return static_cast<uint16_t>(a | (b << 8));
76 }
77
78 BMPHeader read_header(istream& i)
79 {
80 //Read the header
81 BMPHeader h;
82 h.size = read_u4(i);
83 h.reserved[0] = read_u2(i);
84 h.reserved[1] = read_u2(i);
85 h.dataOffset = read_u4(i);
86
87 //Read the info block
88 h.infoSize = read_u4(i);
89
90 if(h.infoSize != 40)
91 {
92
93 string type;
94
95 switch(h.infoSize)
96 {
97 case 40:
98 type = "Windows 3.x style bitmap unsupported.";
99 break;
100 case 12:
101 type = "OS/2 1.x style bitmap unsupported.";
102 break;
103 case 64:
104 type = "OS/2 2.x style bitmap unsupported.";
105 break;
106 case 108:
107 type = "Windows V4 (95/NT) style bitmap unsupported.";
108 break;
109 case 124:
110 type = "Windows V5 (98/2000) style bitmap unsupported.";
111 break;
112 default:
113 {
114 ostringstream ts;
115 ts << "unknown bitmap type (info size = " << h.infoSize << ")";
116 type = ts.str();
117 }
118 }
119
120 throw(Exceptions::Image_IO::UnsupportedImageSubType("Windows BMP/DIB", type));
121 }
122 h.width = read_u4(i);
123 h.height = read_u4(i);
124 h.planes = read_u2(i);
125 h.bpp = read_u2(i);
126 h.compression = read_u4(i);
127 h.dataSize = read_u4(i);
128 h.xPelsPerMeter = read_u4(i);
129 h.yPelsPerMeter = read_u4(i);
130 h.colors = read_u4(i);
131 h.importantColors = read_u4(i);
132
133 return h;
134 }
135
136 void writeBMPHeader(unsigned int width, unsigned int height, unsigned int channels, ostream& out)
137 {
138 unsigned int rowSize = channels * width;
139 if(rowSize % 4)
140 rowSize += 4 - (rowSize % 4);
141
142 unsigned long dataSize = rowSize * height;
143 unsigned long offset = 14 + 40 + (channels == 1 ? 1024 : 0);
144
145 //Output header
146 /* 0 */ out << "BM";
147 /* 2 */ write_u4(out, offset + dataSize); //File size
148 /* 6 */ write_u2(out, 0); //Reserved
149 /* 8 */ write_u2(out, 0); //Reserved
150 /* 10 */ write_u4(out, offset); //Data offset
151
152 //Output info
153 /* 14 */ write_u4(out, 40); //3.x style header size
154 /* 18 */ write_u4(out, width);
155 /* 22 */ write_u4(out, height);
156 /* 26 */ write_u2(out, 1); //Planes (always 1);
157 /* 28 */ write_u2(out, channels * 8); //Bpp
158 /* 30 */ write_u4(out, 0); //Compression method
159 /* 34 */ write_u4(out, dataSize);
160 /* 38 */ write_u4(out, 0); //Pixels per meter (x)
161 /* 42 */ write_u4(out, 0); //Pixels per meter (y)
162 /* 46 */ write_u4(out, 0); //Number of colours (0 == 2^N)
163 /* 50 */ write_u4(out, 0); //Number of important colora
164
165 //Write the palette
166 if(channels == 1)
167 {
168 for(unsigned int i = 0; i < 256; i++)
169 {
170 write_u1(out, i); //Blue
171 write_u1(out, i); //Green
172 write_u1(out, i); //Red
173 write_u1(out, 0); //Reserved
174 }
175 }
176 }
177
178 void readBMPHeader(unsigned int& width, unsigned int& height, unsigned int& channels, unsigned int& compression, istream& in)
179 {
180 char tag[3] = { 0 };
181 in.read(tag, 2);
182 if(strcmp(tag, "BM") != 0)
183 throw CVD::Exceptions::Image_IO::MalformedImage("BMP files must start with 'BM'");
184 struct BMPHeader header = read_header(in);
185
186 if(header.bpp == 8)
187 channels = 1;
188 else if(header.bpp == 24)
189 channels = 3;
190 else
191 throw(Exceptions::Image_IO::UnsupportedImageSubType("Windows BMP/DIB", "image is not 8 or 24 bits per pixel."));
192
193 compression = header.compression;
194 if(compression != 0)
195 throw(Exceptions::Image_IO::UnsupportedImageSubType("Windows BMP/DIB", "compressed images not supported."));
196
197 width = header.width;
198 height = header.height;
199 }
200 }
201 }