Skip to content

Commit 07cfa5e

Browse files
committed
Code improvements
1 parent 9aa4875 commit 07cfa5e

File tree

1 file changed

+87
-77
lines changed

1 file changed

+87
-77
lines changed

BitmapPlusPlus.hpp

Lines changed: 87 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,86 +18,94 @@ namespace bmp {
1818
#pragma pack(push, 1)
1919
struct BitmapHeader {
2020
/* Bitmap file header structure */
21-
std::uint16_t magic; /* Magic number for file always BM which is 0x4D42 */
22-
std::uint32_t file_size; /* Size of file */
23-
std::uint16_t reserved1; /* Reserved */
24-
std::uint16_t reserved2; /* Reserved */
25-
std::uint32_t offset_bits; /* Offset to bitmap data */
21+
std::uint16_t magic; /* Magic number for file always BM which is 0x4D42 */
22+
std::uint32_t file_size; /* Size of file */
23+
std::uint16_t reserved1; /* Reserved */
24+
std::uint16_t reserved2; /* Reserved */
25+
std::uint32_t offset_bits; /* Offset to bitmap data */
2626
/* Bitmap file info structure */
27-
std::uint32_t size; /* Size of info header */
28-
std::int32_t width; /* Width of image */
29-
std::int32_t height; /* Height of image */
30-
std::uint16_t planes; /* Number of color planes */
31-
std::uint16_t bits_per_pixel; /* Number of bits per pixel */
32-
std::uint32_t compression; /* Type of compression to use */
33-
std::uint32_t size_image; /* Size of image data */
34-
std::int32_t x_pixels_per_meter; /* X pixels per meter */
35-
std::int32_t y_pixels_per_meter; /* Y pixels per meter */
36-
std::uint32_t clr_used; /* Number of colors used */
37-
std::uint32_t clr_important; /* Number of important colors */
27+
std::uint32_t size; /* Size of info header */
28+
std::int32_t width; /* Width of image */
29+
std::int32_t height; /* Height of image */
30+
std::uint16_t planes; /* Number of color planes */
31+
std::uint16_t bits_per_pixel; /* Number of bits per pixel */
32+
std::uint32_t compression; /* Type of compression to use */
33+
std::uint32_t size_image; /* Size of image data */
34+
std::int32_t x_pixels_per_meter; /* X pixels per meter */
35+
std::int32_t y_pixels_per_meter; /* Y pixels per meter */
36+
std::uint32_t clr_used; /* Number of colors used */
37+
std::uint32_t clr_important; /* Number of important colors */
3838
};
39+
3940
static_assert(sizeof(BitmapHeader) == 54, "Bitmap header size must be 54 bytes");
4041

4142
struct Pixel {
4243
std::uint8_t r; /* Blue value */
4344
std::uint8_t g; /* Green value */
4445
std::uint8_t b; /* Red value */
4546

46-
constexpr Pixel() noexcept: r(0), g(0), b(0) {}
47+
constexpr Pixel() noexcept: r(0), g(0), b(0) {
48+
}
4749

4850
explicit constexpr Pixel(const std::int32_t rgb) noexcept: r((rgb >> 16) & 0xff), g((rgb >> 8) & 0xff),
49-
b((rgb >> 0x0) & 0xff) {}
51+
b((rgb >> 0x0) & 0xff) {
52+
}
5053

51-
constexpr Pixel(std::uint8_t red, std::uint8_t green, std::uint8_t blue) noexcept: r(red), g(green), b(blue) {}
54+
constexpr Pixel(const std::uint8_t red, const std::uint8_t green,
55+
const std::uint8_t blue) noexcept: r(red), g(green), b(blue) {
56+
}
5257

53-
constexpr bool operator==(const Pixel other) const noexcept {
58+
constexpr bool operator==(const Pixel &other) const noexcept {
59+
if (this == &other)
60+
return true;
5461
return r == other.r && g == other.g && b == other.b;
5562
}
5663

57-
constexpr bool operator!=(const Pixel other) const noexcept { return !((*this) == other); }
64+
constexpr bool operator!=(const Pixel &other) const noexcept { return !((*this) == other); }
5865
};
5966

6067
static_assert(sizeof(Pixel) == 3, "Bitmap Pixel size must be 3 bytes");
6168
#pragma pack(pop)
6269

63-
static constexpr const Pixel Aqua{std::uint8_t(0), std::uint8_t(255), std::uint8_t(255)};
64-
static constexpr const Pixel Beige{std::uint8_t(245), std::uint8_t(245), std::uint8_t(220)};
65-
static constexpr const Pixel Black{std::uint8_t(0), std::uint8_t(0), std::uint8_t(0)};
66-
static constexpr const Pixel Blue{std::uint8_t(0), std::uint8_t(0), std::uint8_t(255)};
67-
static constexpr const Pixel Brown{std::uint8_t(165), std::uint8_t(42), std::uint8_t(42)};
68-
static constexpr const Pixel Chocolate{std::uint8_t(210), std::uint8_t(105), std::uint8_t(30)};
69-
static constexpr const Pixel Coral{std::uint8_t(255), std::uint8_t(127), std::uint8_t(80)};
70-
static constexpr const Pixel Crimson{std::uint8_t(220), std::uint8_t(20), std::uint8_t(60)};
71-
static constexpr const Pixel Cyan{std::uint8_t(0), std::uint8_t(255), std::uint8_t(255)};
72-
static constexpr const Pixel Firebrick{std::uint8_t(178), std::uint8_t(34), std::uint8_t(34)};
73-
static constexpr const Pixel Gold{std::uint8_t(255), std::uint8_t(215), std::uint8_t(0)};
74-
static constexpr const Pixel Gray{std::uint8_t(128), std::uint8_t(128), std::uint8_t(128)};
75-
static constexpr const Pixel Green{std::uint8_t(0), std::uint8_t(255), std::uint8_t(0)};
76-
static constexpr const Pixel Indigo{std::uint8_t(75), std::uint8_t(0), std::uint8_t(130)};
77-
static constexpr const Pixel Lavender{std::uint8_t(230), std::uint8_t(230), std::uint8_t(250)};
78-
static constexpr const Pixel Lime{std::uint8_t(0), std::uint8_t(255), std::uint8_t(0)};
79-
static constexpr const Pixel Magenta{std::uint8_t(255), std::uint8_t(0), std::uint8_t(255)};
80-
static constexpr const Pixel Maroon{std::uint8_t(128), std::uint8_t(0), std::uint8_t(0)};
81-
static constexpr const Pixel Navy{std::uint8_t(0), std::uint8_t(0), std::uint8_t(128)};
82-
static constexpr const Pixel Olive{std::uint8_t(128), std::uint8_t(128), std::uint8_t(0)};
83-
static constexpr const Pixel Orange{std::uint8_t(255), std::uint8_t(165), std::uint8_t(0)};
84-
static constexpr const Pixel Pink{std::uint8_t(255), std::uint8_t(192), std::uint8_t(203)};
85-
static constexpr const Pixel Purple{std::uint8_t(128), std::uint8_t(0), std::uint8_t(128)};
86-
static constexpr const Pixel Red{std::uint8_t(255), std::uint8_t(0), std::uint8_t(0)};
87-
static constexpr const Pixel Salmon{std::uint8_t(250), std::uint8_t(128), std::uint8_t(114)};
88-
static constexpr const Pixel Silver{std::uint8_t(192), std::uint8_t(192), std::uint8_t(192)};
89-
static constexpr const Pixel Snow{std::uint8_t(255), std::uint8_t(250), std::uint8_t(250)};
90-
static constexpr const Pixel Teal{std::uint8_t(0), std::uint8_t(128), std::uint8_t(128)};
91-
static constexpr const Pixel Tomato{std::uint8_t(255), std::uint8_t(99), std::uint8_t(71)};
92-
static constexpr const Pixel Turquoise{std::uint8_t(64), std::uint8_t(224), std::uint8_t(208)};
93-
static constexpr const Pixel Violet{std::uint8_t(238), std::uint8_t(130), std::uint8_t(238)};
94-
static constexpr const Pixel White{std::uint8_t(255), std::uint8_t(255), std::uint8_t(255)};
95-
static constexpr const Pixel Wheat{std::uint8_t(245), std::uint8_t(222), std::uint8_t(179)};
96-
static constexpr const Pixel Yellow{std::uint8_t(255), std::uint8_t(255), std::uint8_t(0)};
70+
static constexpr Pixel Aqua{0, 255, 255};
71+
static constexpr Pixel Beige{245, 245, 220};
72+
static constexpr Pixel Black{0, 0, 0};
73+
static constexpr Pixel Blue{0, 0, 255};
74+
static constexpr Pixel Brown{165, 42, 42};
75+
static constexpr Pixel Chocolate{210, 105, 30};
76+
static constexpr Pixel Coral{255, 127, 80};
77+
static constexpr Pixel Crimson{220, 20, 60};
78+
static constexpr Pixel Cyan{0, 255, 255};
79+
static constexpr Pixel Firebrick{178, 34, 34};
80+
static constexpr Pixel Gold{255, 215, 0};
81+
static constexpr Pixel Gray{128, 128, 128};
82+
static constexpr Pixel Green{0, 255, 0};
83+
static constexpr Pixel Indigo{75, 0, 130};
84+
static constexpr Pixel Lavender{230, 230, 250};
85+
static constexpr Pixel Lime{0, 255, 0};
86+
static constexpr Pixel Magenta{255, 0, 255};
87+
static constexpr Pixel Maroon{128, 0, 0};
88+
static constexpr Pixel Navy{0, 0, 128};
89+
static constexpr Pixel Olive{128, 128, 0};
90+
static constexpr Pixel Orange{255, 165, 0};
91+
static constexpr Pixel Pink{255, 192, 203};
92+
static constexpr Pixel Purple{128, 0, 128};
93+
static constexpr Pixel Red{255, 0, 0};
94+
static constexpr Pixel Salmon{250, 128, 114};
95+
static constexpr Pixel Silver{192, 192, 192};
96+
static constexpr Pixel Snow{255, 250, 250};
97+
static constexpr Pixel Teal{0, 128, 128};
98+
static constexpr Pixel Tomato{255, 99, 71};
99+
static constexpr Pixel Turquoise{64, 224, 208};
100+
static constexpr Pixel Violet{238, 130, 238};
101+
static constexpr Pixel White{255, 255, 255};
102+
static constexpr Pixel Wheat{245, 222, 179};
103+
static constexpr Pixel Yellow{255, 255, 0};
97104

98105
class Exception : public std::runtime_error {
99106
public:
100-
explicit Exception(const std::string &message) : std::runtime_error(message) {}
107+
explicit Exception(const std::string &message) : std::runtime_error(message) {
108+
}
101109
};
102110

103111
class Bitmap {
@@ -228,9 +236,11 @@ namespace bmp {
228236
throw Exception("Bitmap::fill_triangle: One or more points are out of bounds");
229237

230238
// Sort the vertices by y-coordinate (top to bottom)
231-
std::vector<std::pair<std::int32_t, std::int32_t>> vertices = {{x1, y1},
232-
{x2, y2},
233-
{x3, y3}};
239+
std::vector<std::pair<std::int32_t, std::int32_t> > vertices = {
240+
{x1, y1},
241+
{x2, y2},
242+
{x3, y3}
243+
};
234244
std::sort(vertices.begin(), vertices.end(), [](const auto &a, const auto &b) {
235245
return a.second < b.second;
236246
});
@@ -244,10 +254,10 @@ namespace bmp {
244254
const float slope_right = static_cast<float>(x_bot - x_top) / (y_bot - y_top);
245255

246256
// Initialize the starting and ending x-coordinates for each scanline
247-
std::vector<std::pair<std::int32_t, std::int32_t>> scanlines(y_mid - y_top + 1);
257+
std::vector<std::pair<std::int32_t, std::int32_t> > scanlines(y_mid - y_top + 1);
248258
for (std::int32_t y = y_top; y <= y_mid; ++y) {
249-
const std::int32_t x_start = static_cast<std::int32_t>(x_top + (y - y_top) * slope_left);
250-
const std::int32_t x_end = static_cast<std::int32_t>(x_top + (y - y_top) * slope_right);
259+
const auto x_start = static_cast<std::int32_t>(x_top + (y - y_top) * slope_left);
260+
const auto x_end = static_cast<std::int32_t>(x_top + (y - y_top) * slope_right);
251261
scanlines[y - y_top] = {x_start, x_end};
252262
}
253263

@@ -263,8 +273,8 @@ namespace bmp {
263273

264274
// Update the x-coordinates for the scanlines in the lower part of the triangle
265275
for (std::int32_t y = y_mid + 1; y <= y_bot; ++y) {
266-
const std::int32_t x_start = static_cast<std::int32_t>(x_mid + (y - y_mid) * slope_left);
267-
const std::int32_t x_end = static_cast<std::int32_t>(x_top + (y - y_top) * new_slope_right);
276+
const auto x_start = static_cast<std::int32_t>(x_mid + (y - y_mid) * slope_left);
277+
const auto x_end = static_cast<std::int32_t>(x_top + (y - y_top) * new_slope_right);
268278
scanlines[y - y_top] = {x_start, x_end};
269279
}
270280

@@ -364,7 +374,7 @@ namespace bmp {
364374
/**
365375
* Get const pixel at position x,y
366376
*/
367-
const Pixel &get(const std::int32_t x, const std::int32_t y) const {
377+
[[nodiscard]] const Pixel &get(const std::int32_t x, const std::int32_t y) const {
368378
if (!in_bounds(x, y))
369379
throw Exception("Bitmap::Get(" + std::to_string(x) + ", " + std::to_string(y) + "): x,y out of bounds");
370380
return m_pixels[IX(x, y)];
@@ -373,12 +383,12 @@ namespace bmp {
373383
/**
374384
* Returns the width of the Bitmap image
375385
*/
376-
std::int32_t width() const noexcept { return m_width; }
386+
[[nodiscard]] std::int32_t width() const noexcept { return m_width; }
377387

378388
/**
379389
* Returns the height of the Bitmap image
380390
*/
381-
std::int32_t height() const noexcept { return m_height; }
391+
[[nodiscard]] std::int32_t height() const noexcept { return m_height; }
382392

383393
/**
384394
* Clears Bitmap pixels with an rgb color
@@ -427,21 +437,21 @@ namespace bmp {
427437
}
428438

429439
public: /** foreach iterators access */
430-
std::vector<Pixel>::iterator begin() noexcept { return m_pixels.begin(); }
440+
[[nodiscard]] std::vector<Pixel>::iterator begin() noexcept { return m_pixels.begin(); }
431441

432-
std::vector<Pixel>::iterator end() noexcept { return m_pixels.end(); }
442+
[[nodiscard]] std::vector<Pixel>::iterator end() noexcept { return m_pixels.end(); }
433443

434-
std::vector<Pixel>::const_iterator cbegin() const noexcept { return m_pixels.cbegin(); }
444+
[[nodiscard]] std::vector<Pixel>::const_iterator cbegin() const noexcept { return m_pixels.cbegin(); }
435445

436-
std::vector<Pixel>::const_iterator cend() const noexcept { return m_pixels.cend(); }
446+
[[nodiscard]] std::vector<Pixel>::const_iterator cend() const noexcept { return m_pixels.cend(); }
437447

438-
std::vector<Pixel>::reverse_iterator rbegin() noexcept { return m_pixels.rbegin(); }
448+
[[nodiscard]] std::vector<Pixel>::reverse_iterator rbegin() noexcept { return m_pixels.rbegin(); }
439449

440-
std::vector<Pixel>::reverse_iterator rend() noexcept { return m_pixels.rend(); }
450+
[[nodiscard]] std::vector<Pixel>::reverse_iterator rend() noexcept { return m_pixels.rend(); }
441451

442-
std::vector<Pixel>::const_reverse_iterator crbegin() const noexcept { return m_pixels.crbegin(); }
452+
[[nodiscard]] std::vector<Pixel>::const_reverse_iterator crbegin() const noexcept { return m_pixels.crbegin(); }
443453

444-
std::vector<Pixel>::const_reverse_iterator crend() const noexcept { return m_pixels.crend(); }
454+
[[nodiscard]] std::vector<Pixel>::const_reverse_iterator crend() const noexcept { return m_pixels.crend(); }
445455

446456
public: /* Modifiers */
447457
/**
@@ -459,7 +469,7 @@ namespace bmp {
459469
* Saves Bitmap pixels into a file
460470
* @throws bmp::Exception on error
461471
*/
462-
void save(const std::string &filename) {
472+
void save(const std::string &filename) const {
463473
// Calculate row and bitmap size
464474
const std::int32_t row_size = m_width * 3 + m_width % 4;
465475
const std::uint32_t bitmap_size = row_size * m_height;
@@ -586,4 +596,4 @@ namespace bmp {
586596
std::int32_t m_width;
587597
std::int32_t m_height;
588598
};
589-
}
599+
}

0 commit comments

Comments
 (0)