-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbitmap_image.hpp
More file actions
470 lines (414 loc) · 13 KB
/
bitmap_image.hpp
File metadata and controls
470 lines (414 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
***************************************************************************
* *
* Platform Independent *
* Bitmap Image Reader Writer Library *
* *
* Author: Arash Partow - 2002 *
* URL: http://www.partow.net *
* *
* Note: This library only supports 24-bits per pixel bitmap format files. *
* *
* Copyright notice: *
* Free use of the Platform Independent Bitmap Image Reader Writer Library *
* is permitted under the guidelines and in accordance with the most *
* current version of the Common Public License. *
* http://www.opensource.org/licenses/cpl1.0.php *
* *
***************************************************************************
*/
#ifndef INCLUDE_BITMAP_IMAGE_HPP
#define INCLUDE_BITMAP_IMAGE_HPP
#include <iostream>
#include <string>
#include <fstream>
#include <iterator>
#include <limits>
#include <cmath>
#include <cstdlib>
struct bitmap_file_header
{
unsigned short type;
unsigned int size;
unsigned short reserved1;
unsigned short reserved2;
unsigned int off_bits;
unsigned int struct_size()
{
return sizeof(type) +
sizeof(size) +
sizeof(reserved1) +
sizeof(reserved2) +
sizeof(off_bits);
}
};
struct bitmap_information_header
{
unsigned int size;
unsigned int width;
unsigned int height;
unsigned short planes;
unsigned short bit_count;
unsigned int compression;
unsigned int size_image;
unsigned int x_pels_per_meter;
unsigned int y_pels_per_meter;
unsigned int clr_used;
unsigned int clr_important;
unsigned int struct_size()
{
return sizeof(size) +
sizeof(width) +
sizeof(height) +
sizeof(planes) +
sizeof(bit_count) +
sizeof(compression) +
sizeof(size_image) +
sizeof(x_pels_per_meter) +
sizeof(y_pels_per_meter) +
sizeof(clr_used) +
sizeof(clr_important);
}
};
inline void read_bih(std::ifstream& stream,bitmap_information_header& bih);
inline void read_bfh(std::ifstream& stream, bitmap_file_header& bfh);
inline void write_bih(std::ofstream& stream, const bitmap_information_header& bih);
inline void write_bfh(std::ofstream& stream, const bitmap_file_header& bfh);
template<typename T>
T clamp(const T& v, const T& lower_range, const T& upper_range)
{
if (v < lower_range)
return lower_range;
else if (v > upper_range)
return upper_range;
else
return v;
}
class bitmap_image
{
public:
enum channel_mode {
rgb_mode = 0,
bgr_mode = 1
};
enum color_plane {
blue_plane = 0,
green_plane = 1,
red_plane = 2
};
bitmap_image()
: file_name_(""),
data_(0),
bytes_per_pixel_(3),
length_(0),
width_(0),
height_(0),
row_increment_(0),
channel_mode_(bgr_mode)
{}
bitmap_image(const std::string& _filename)
: file_name_(_filename),
data_(0),
bytes_per_pixel_(0),
length_(0),
width_(0),
height_(0),
row_increment_(0),
channel_mode_(bgr_mode)
{
load_bitmap();
}
bitmap_image(const unsigned int width, const unsigned int height)
: file_name_(""),
data_(0),
bytes_per_pixel_(3),
length_(0),
width_(width),
height_(height),
row_increment_(0),
channel_mode_(bgr_mode)
{
create_bitmap();
}
bitmap_image(const bitmap_image& image)
: file_name_(image.file_name_),
data_(0),
bytes_per_pixel_(3),
width_(image.width_),
height_(image.height_),
row_increment_(0),
channel_mode_(bgr_mode)
{
create_bitmap();
std::copy(image.data_, image.data_ + image.length_, data_);
}
~bitmap_image()
{
delete [] data_;
}
bitmap_image& operator=(const bitmap_image& image)
{
if (this != &image)
{
file_name_ = image.file_name_;
bytes_per_pixel_ = image.bytes_per_pixel_;
width_ = image.width_;
height_ = image.height_;
row_increment_ = 0;
channel_mode_ = image.channel_mode_;
create_bitmap();
std::copy(image.data_, image.data_ + image.length_, data_);
}
return *this;
}
inline void get_pixel(const unsigned int x, const unsigned int y,
unsigned char& red,
unsigned char& green,
unsigned char& blue)
{
blue = data_[(y * row_increment_) + (x * bytes_per_pixel_ + 0)];
green = data_[(y * row_increment_) + (x * bytes_per_pixel_ + 1)];
red = data_[(y * row_increment_) + (x * bytes_per_pixel_ + 2)];
}
inline void set_pixel(const unsigned int x, const unsigned int y,
const unsigned char red,
const unsigned char green,
const unsigned char blue)
{
data_[(y * row_increment_) + (x * bytes_per_pixel_ + 0)] = blue;
data_[(y * row_increment_) + (x * bytes_per_pixel_ + 1)] = green;
data_[(y * row_increment_) + (x * bytes_per_pixel_ + 2)] = red;
}
inline bool copy_from(const bitmap_image& image)
{
if ((image.height_ != height_) ||
(image.width_ != width_))
{
return false;
}
std::copy(image.data_,image.data_ + image.length_,data_);
return true;
}
void save_image(const std::string& file_name)
{
std::ofstream stream(file_name.c_str(),std::ios::binary);
if (!stream)
{
std::cout << "bitmap_image::save_image(): Error - Could not open file " << file_name << " for writing!" << std::endl;
return;
}
bitmap_file_header bfh;
bitmap_information_header bih;
bih.width = width_;
bih.height = height_;
bih.bit_count = static_cast<unsigned short>(bytes_per_pixel_ << 3);
bih.clr_important = 0;
bih.clr_used = 0;
bih.compression = 0;
bih.planes = 1;
bih.size = 40;
bih.x_pels_per_meter = 0;
bih.y_pels_per_meter = 0;
bih.size_image = (((bih.width * bytes_per_pixel_) + 3) & 0xFFFC) * bih.height;
bfh.type = 19778;
bfh.size = 55 + bih.size_image;
bfh.reserved1 = 0;
bfh.reserved2 = 0;
bfh.off_bits = bih.struct_size() + bfh.struct_size();
write_bfh(stream,bfh);
write_bih(stream,bih);
unsigned int padding = (4 - ((3 * width_) % 4)) % 4;
char padding_data[4] = {0x0,0x0,0x0,0x0};
for (unsigned int i = 0; i < height_; ++i)
{
unsigned char* data_ptr = data_ + (row_increment_ * (height_ - i - 1));
stream.write(reinterpret_cast<char*>(data_ptr),sizeof(unsigned char) * bytes_per_pixel_ * width_);
stream.write(padding_data,padding);
}
stream.close();
}
inline const unsigned char* data()
{
return data_;
}
int width(){return width_;}
int height(){return height_;}
private:
void create_bitmap()
{
length_ = width_ * height_ * bytes_per_pixel_;
row_increment_ = width_ * bytes_per_pixel_;
if (0 != data_)
{
delete[] data_;
}
data_ = new unsigned char[length_];
valid_ = true;
}
inline unsigned char* row(unsigned int row_index) const
{
return data_ + (row_index * row_increment_);
}
void load_bitmap()
{
std::ifstream stream(file_name_.c_str(),std::ios::binary);
if (!stream)
{
std::cerr << "bitmap_image::load_bitmap() ERROR: bitmap_image - file " << file_name_ << " not found!" << std::endl;
return;
}
bitmap_file_header bfh;
bitmap_information_header bih;
read_bfh(stream,bfh);
read_bih(stream,bih);
if (bfh.type != 19778)
{
stream.close();
std::cerr << "bitmap_image::load_bitmap() ERROR: bitmap_image - Invalid type value " << bfh.type << " expected 19778." << std::endl;
return;
}
if (bih.bit_count != 24)
{
stream.close();
std::cerr << "bitmap_image::load_bitmap() ERROR: bitmap_image - Invalid bit depth " << bih.bit_count << " expected 24." << std::endl;
return;
}
height_ = bih.height;
width_ = bih.width;
bytes_per_pixel_ = bih.bit_count >> 3;
unsigned int padding = (4 - ((3 * width_) % 4)) % 4;
char padding_data[4] = {0,0,0,0};
create_bitmap();
for (unsigned int i = 0; i < height_; ++i)
{
unsigned char* data_ptr = row(height_ - i - 1); // read in inverted row order
stream.read(reinterpret_cast<char*>(data_ptr),sizeof(char) * bytes_per_pixel_ * width_);
stream.read(padding_data,padding);
}
valid_ = true;
}
bool valid_;
std::string file_name_;
unsigned char* data_;
unsigned int bytes_per_pixel_;
unsigned int length_;
unsigned int width_;
unsigned int height_;
unsigned int row_increment_;
channel_mode channel_mode_;
};
inline bool big_endian()
{
unsigned int v = 0x01;
return (1 != reinterpret_cast<char*>(&v)[0]);
}
inline unsigned short flip(const unsigned short& v)
{
return ((v >> 8) | (v << 8));
}
inline unsigned int flip(const unsigned int& v)
{
return (((v & 0xFF000000) >> 0x18) |
((v & 0x000000FF) << 0x18) |
((v & 0x00FF0000) >> 0x08) |
((v & 0x0000FF00) << 0x08));
}
template<typename T>
inline void read_from_stream(std::ifstream& stream,T& t)
{
stream.read(reinterpret_cast<char*>(&t),sizeof(T));
}
template<typename T>
inline void write_to_stream(std::ofstream& stream,const T& t)
{
stream.write(reinterpret_cast<const char*>(&t),sizeof(T));
}
inline void read_bfh(std::ifstream& stream, bitmap_file_header& bfh)
{
read_from_stream(stream,bfh.type);
read_from_stream(stream,bfh.size);
read_from_stream(stream,bfh.reserved1);
read_from_stream(stream,bfh.reserved2);
read_from_stream(stream,bfh.off_bits);
if (big_endian())
{
flip(bfh.type);
flip(bfh.size);
flip(bfh.reserved1);
flip(bfh.reserved2);
flip(bfh.off_bits);
}
}
inline void write_bfh(std::ofstream& stream, const bitmap_file_header& bfh)
{
if (big_endian())
{
flip(bfh.type);
flip(bfh.size);
flip(bfh.reserved1);
flip(bfh.reserved2);
flip(bfh.off_bits);
}
write_to_stream(stream,bfh.type);
write_to_stream(stream,bfh.size);
write_to_stream(stream,bfh.reserved1);
write_to_stream(stream,bfh.reserved2);
write_to_stream(stream,bfh.off_bits);
}
inline void read_bih(std::ifstream& stream,bitmap_information_header& bih)
{
read_from_stream(stream,bih.size);
read_from_stream(stream,bih.width);
read_from_stream(stream,bih.height);
read_from_stream(stream,bih.planes);
read_from_stream(stream,bih.bit_count);
read_from_stream(stream,bih.compression);
read_from_stream(stream,bih.size_image);
read_from_stream(stream,bih.x_pels_per_meter);
read_from_stream(stream,bih.y_pels_per_meter);
read_from_stream(stream,bih.clr_used);
read_from_stream(stream,bih.clr_important);
if (big_endian())
{
flip(bih.size);
flip(bih.width);
flip(bih.height);
flip(bih.planes);
flip(bih.bit_count);
flip(bih.compression);
flip(bih.size_image);
flip(bih.x_pels_per_meter);
flip(bih.y_pels_per_meter);
flip(bih.clr_used);
flip(bih.clr_important);
}
}
inline void write_bih(std::ofstream& stream, const bitmap_information_header& bih)
{
if (big_endian())
{
flip(bih.size);
flip(bih.width);
flip(bih.height);
flip(bih.planes);
flip(bih.bit_count);
flip(bih.compression);
flip(bih.size_image);
flip(bih.x_pels_per_meter);
flip(bih.y_pels_per_meter);
flip(bih.clr_used);
flip(bih.clr_important);
}
write_to_stream(stream,bih.size);
write_to_stream(stream,bih.width);
write_to_stream(stream,bih.height);
write_to_stream(stream,bih.planes);
write_to_stream(stream,bih.bit_count);
write_to_stream(stream,bih.compression);
write_to_stream(stream,bih.size_image);
write_to_stream(stream,bih.x_pels_per_meter);
write_to_stream(stream,bih.y_pels_per_meter);
write_to_stream(stream,bih.clr_used);
write_to_stream(stream,bih.clr_important);
}
#endif