Skip to content

Commit 7b671cb

Browse files
committed
C++ Bitmap Image Reader Writer Library http://partow.net/programming/bitmap/index.html
1 parent 2d2d5be commit 7b671cb

File tree

1 file changed

+51
-52
lines changed

1 file changed

+51
-52
lines changed

bitmap_image.hpp

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class bitmap_image
119119

120120
inline void clear(const unsigned char v = 0x00)
121121
{
122-
std::fill(data_.begin(),data_.end(),v);
122+
std::fill(data_.begin(), data_.end(), v);
123123
}
124124

125125
inline unsigned char red_channel(const unsigned int x, const unsigned int y) const
@@ -160,7 +160,7 @@ class bitmap_image
160160
inline void get_pixel(const unsigned int x, const unsigned int y,
161161
unsigned char& red,
162162
unsigned char& green,
163-
unsigned char& blue)
163+
unsigned char& blue) const
164164
{
165165
const unsigned int y_offset = y * row_increment_;
166166
const unsigned int x_offset = x * bytes_per_pixel_;
@@ -171,8 +171,7 @@ class bitmap_image
171171
}
172172

173173
template <typename RGB>
174-
inline void get_pixel(const unsigned int x, const unsigned int y,
175-
RGB& colour)
174+
inline void get_pixel(const unsigned int x, const unsigned int y, RGB& colour) const
176175
{
177176
get_pixel(x, y, colour.red, colour.green, colour.blue);
178177
}
@@ -224,7 +223,7 @@ class bitmap_image
224223
const unsigned char* itr2 = source_image.row(y);
225224
const unsigned char* itr2_end = itr2 + source_image.width_ * bytes_per_pixel_;
226225

227-
std::copy(itr2,itr2_end,itr1);
226+
std::copy(itr2, itr2_end, itr1);
228227
}
229228

230229
return true;
@@ -234,7 +233,7 @@ class bitmap_image
234233
const unsigned int& y ,
235234
const unsigned int& width ,
236235
const unsigned int& height,
237-
bitmap_image& dest_image )
236+
bitmap_image& dest_image ) const
238237
{
239238
if ((x + width ) > width_ ) { return false; }
240239
if ((y + height) > height_) { return false; }
@@ -253,7 +252,7 @@ class bitmap_image
253252
unsigned char* itr1_end = itr1 + (width * bytes_per_pixel_);
254253
unsigned char* itr2 = dest_image.row(r);
255254

256-
std::copy(itr1,itr1_end,itr2);
255+
std::copy(itr1, itr1_end, itr2);
257256
}
258257

259258
return true;
@@ -263,7 +262,7 @@ class bitmap_image
263262
const unsigned int& cy ,
264263
const unsigned int& width ,
265264
const unsigned int& height,
266-
bitmap_image& dest_image )
265+
bitmap_image& dest_image ) const
267266
{
268267
return region(cx - (width / 2), cy - (height / 2),
269268
width, height,
@@ -284,7 +283,7 @@ class bitmap_image
284283
unsigned char* itr = row(r + y) + x * bytes_per_pixel_;
285284
unsigned char* itr_end = itr + (width * bytes_per_pixel_);
286285

287-
std::fill(itr,itr_end,value);
286+
std::fill(itr, itr_end, value);
288287
}
289288

290289
return true;
@@ -409,7 +408,7 @@ class bitmap_image
409408

410409
if (clear)
411410
{
412-
std::fill(data_.begin(),data_.end(),static_cast<unsigned char>(0x00));
411+
std::fill(data_.begin(), data_.end(), static_cast<unsigned char>(0x00));
413412
}
414413
}
415414

@@ -749,7 +748,7 @@ class bitmap_image
749748
}
750749
}
751750

752-
inline void export_ycbcr(double* y, double* cb, double* cr)
751+
inline void export_ycbcr(double* y, double* cb, double* cr) const
753752
{
754753
if (bgr_mode != channel_mode_)
755754
return;
@@ -917,7 +916,7 @@ class bitmap_image
917916
}
918917
}
919918

920-
inline void subsample(bitmap_image& dest)
919+
inline void subsample(bitmap_image& dest) const
921920
{
922921
/*
923922
Half sub-sample of original image.
@@ -1045,7 +1044,7 @@ class bitmap_image
10451044
}
10461045
}
10471046

1048-
inline void upsample(bitmap_image& dest)
1047+
inline void upsample(bitmap_image& dest) const
10491048
{
10501049
/*
10511050
2x up-sample of original image.
@@ -1107,9 +1106,9 @@ class bitmap_image
11071106
return;
11081107
}
11091108

1110-
unsigned char* itr1 = data();
1111-
unsigned char* itr1_end = end();
1112-
const unsigned char* itr2 = image.data();
1109+
unsigned char* itr1 = data();
1110+
const unsigned char* itr1_end = end();
1111+
const unsigned char* itr2 = image.data();
11131112

11141113
double alpha_compliment = 1.0 - alpha;
11151114

@@ -1131,7 +1130,7 @@ class bitmap_image
11311130
return 0.0;
11321131
}
11331132

1134-
unsigned char* itr1 = data();
1133+
const unsigned char* itr1 = data();
11351134
const unsigned char* itr2 = image.data();
11361135

11371136
double mse = 0.0;
@@ -1171,9 +1170,9 @@ class bitmap_image
11711170

11721171
for (unsigned int r = 0; r < height; ++r)
11731172
{
1174-
unsigned char* itr1 = row(r + y) + x * bytes_per_pixel_;
1175-
unsigned char* itr1_end = itr1 + (width * bytes_per_pixel_);
1176-
const unsigned char* itr2 = image.row(r);
1173+
const unsigned char* itr1 = row(r + y) + x * bytes_per_pixel_;
1174+
const unsigned char* itr1_end = itr1 + (width * bytes_per_pixel_);
1175+
const unsigned char* itr2 = image.row(r);
11771176

11781177
while (itr1 != itr1_end)
11791178
{
@@ -1195,17 +1194,17 @@ class bitmap_image
11951194
}
11961195
}
11971196

1198-
inline void histogram(const color_plane color, double hist[256])
1197+
inline void histogram(const color_plane color, double hist[256]) const
11991198
{
1200-
std::fill(hist,hist + 256,0.0);
1199+
std::fill(hist, hist + 256, 0.0);
12011200

1202-
for (unsigned char* itr = (data() + offset(color)); itr < end(); itr += bytes_per_pixel_)
1201+
for (const unsigned char* itr = (data() + offset(color)); itr < end(); itr += bytes_per_pixel_)
12031202
{
12041203
++hist[(*itr)];
12051204
}
12061205
}
12071206

1208-
inline void histogram_normalized(const color_plane color, double hist[256])
1207+
inline void histogram_normalized(const color_plane color, double hist[256]) const
12091208
{
12101209
histogram(color,hist);
12111210

@@ -1219,7 +1218,7 @@ class bitmap_image
12191218
}
12201219
}
12211220

1222-
inline unsigned int offset(const color_plane color)
1221+
inline unsigned int offset(const color_plane color) const
12231222
{
12241223
switch (channel_mode_)
12251224
{
@@ -1303,7 +1302,7 @@ class bitmap_image
13031302

13041303
void clear()
13051304
{
1306-
std::memset(this,0x00,sizeof(bitmap_file_header));
1305+
std::memset(this, 0x00, sizeof(bitmap_file_header));
13071306
}
13081307
};
13091308

@@ -1338,7 +1337,7 @@ class bitmap_image
13381337

13391338
void clear()
13401339
{
1341-
std::memset(this,0x00,sizeof(bitmap_information_header));
1340+
std::memset(this, 0x00, sizeof(bitmap_information_header));
13421341
}
13431342
};
13441343

@@ -1430,13 +1429,13 @@ class bitmap_image
14301429

14311430
if (big_endian())
14321431
{
1433-
bih.size = flip(bih.size );
1434-
bih.width = flip(bih.width );
1435-
bih.height = flip(bih.height );
1436-
bih.planes = flip(bih.planes );
1437-
bih.bit_count = flip(bih.bit_count );
1438-
bih.compression = flip(bih.compression );
1439-
bih.size_image = flip(bih.size_image );
1432+
bih.size = flip(bih.size );
1433+
bih.width = flip(bih.width );
1434+
bih.height = flip(bih.height );
1435+
bih.planes = flip(bih.planes );
1436+
bih.bit_count = flip(bih.bit_count );
1437+
bih.compression = flip(bih.compression );
1438+
bih.size_image = flip(bih.size_image );
14401439
bih.x_pels_per_meter = flip(bih.x_pels_per_meter);
14411440
bih.y_pels_per_meter = flip(bih.y_pels_per_meter);
14421441
bih.clr_used = flip(bih.clr_used );
@@ -1476,7 +1475,7 @@ class bitmap_image
14761475
}
14771476
}
14781477

1479-
inline std::size_t file_size(const std::string& file_name)
1478+
inline std::size_t file_size(const std::string& file_name) const
14801479
{
14811480
std::ifstream file(file_name.c_str(),std::ios::in | std::ios::binary);
14821481
if (!file) return 0;
@@ -1588,7 +1587,7 @@ class bitmap_image
15881587
}
15891588

15901589
template <typename T>
1591-
inline T clamp(const T& v, const T& lower_range, const T& upper_range)
1590+
inline T clamp(const T& v, const T& lower_range, const T& upper_range) const
15921591
{
15931592
if (v < lower_range)
15941593
return lower_range;
@@ -2117,25 +2116,25 @@ class image_drawer
21172116

21182117
void rectangle(int x1, int y1, int x2, int y2)
21192118
{
2120-
line_segment(x1,y1,x2,y1);
2121-
line_segment(x2,y1,x2,y2);
2122-
line_segment(x2,y2,x1,y2);
2123-
line_segment(x1,y2,x1,y1);
2119+
line_segment(x1, y1, x2, y1);
2120+
line_segment(x2, y1, x2, y2);
2121+
line_segment(x2, y2, x1, y2);
2122+
line_segment(x1, y2, x1, y1);
21242123
}
21252124

21262125
void triangle(int x1, int y1, int x2, int y2,int x3, int y3)
21272126
{
2128-
line_segment(x1,y1,x2,y2);
2129-
line_segment(x2,y2,x3,y3);
2130-
line_segment(x3,y3,x1,y1);
2127+
line_segment(x1, y1, x2, y2);
2128+
line_segment(x2, y2, x3, y3);
2129+
line_segment(x3, y3, x1, y1);
21312130
}
21322131

21332132
void quadix(int x1, int y1, int x2, int y2,int x3, int y3, int x4, int y4)
21342133
{
2135-
line_segment(x1,y1,x2,y2);
2136-
line_segment(x2,y2,x3,y3);
2137-
line_segment(x3,y3,x4,y4);
2138-
line_segment(x4,y4,x1,y1);
2134+
line_segment(x1, y1, x2, y2);
2135+
line_segment(x2, y2, x3, y3);
2136+
line_segment(x3, y3, x4, y4);
2137+
line_segment(x4, y4, x1, y1);
21392138
}
21402139

21412140
void line_segment(int x1, int y1, int x2, int y2)
@@ -2538,8 +2537,8 @@ class cartesian_canvas
25382537

25392538
void bottom(const point_t& p0, const point_t& p1, const point_t& p2)
25402539
{
2541-
double m0 = (p1.first - p0.first) / (2.0 * (p1.second - p0.second));
2542-
double m1 = (p2.first - p0.first) / (2.0 * (p2.second - p0.second));
2540+
const double m0 = (p1.first - p0.first) / (2.0 * (p1.second - p0.second));
2541+
const double m1 = (p2.first - p0.first) / (2.0 * (p2.second - p0.second));
25432542

25442543
double x0 = p0.first;
25452544
double x1 = p0.first;
@@ -2555,8 +2554,8 @@ class cartesian_canvas
25552554

25562555
void top(const point_t& p0, const point_t& p1, const point_t& p2)
25572556
{
2558-
double m0 = (p2.first - p0.first) / (2.0 * (p2.second - p0.second));
2559-
double m1 = (p2.first - p1.first) / (2.0 * (p2.second - p1.second));
2557+
const double m0 = (p2.first - p0.first) / (2.0 * (p2.second - p0.second));
2558+
const double m1 = (p2.first - p1.first) / (2.0 * (p2.second - p1.second));
25602559

25612560
double x0 = p2.first;
25622561
double x1 = p2.first;
@@ -2975,7 +2974,7 @@ inline double find_nearest_wave_length(const rgb_t& c, const double increment =
29752974

29762975
for (double i = 0.0; i < max_wave_length; i += increment)
29772976
{
2978-
rgb_t curr_rgb = convert_wave_length_nm_to_rgb(i);
2977+
const rgb_t curr_rgb = convert_wave_length_nm_to_rgb(i);
29792978

29802979
if (c == curr_rgb)
29812980
{

0 commit comments

Comments
 (0)