bitmap-plus-plus 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +40 -0
- data/LICENSE +25 -0
- data/README.md +75 -0
- data/examples/colormaps.rb +1857 -0
- data/examples/draw_primitives.rb +53 -0
- data/examples/julia.rb +68 -0
- data/examples/mandelbrot.rb +68 -0
- data/examples/random_colors.rb +37 -0
- data/examples/transformations.rb +84 -0
- data/ext/BitmapPlusPlus-rb.cpp +420 -0
- data/ext/BitmapPlusPlus-rb.hpp +11 -0
- data/ext/BitmapPlusPlus.hpp +659 -0
- data/ext/CMakeLists.txt +170 -0
- data/ext/CMakePresets.json +209 -0
- data/lib/bitmap-plus-plus/version.rb +3 -0
- data/lib/bitmap-plus-plus.rb +3 -0
- data/sig/Bmp/Bitmap.rbs +39 -0
- data/sig/Bmp/BitmapHeader.rbs +22 -0
- data/sig/Bmp/Exception.rbs +5 -0
- data/sig/Bmp/Pixel.rbs +16 -0
- data/sig/Rice/Arg.rbs +6 -0
- data/sig/Rice/Buffer/342/211/272Rice/352/236/211/352/236/211detail/352/236/211/352/236/211ParameterAbstract/342/210/227/342/211/273.rbs +15 -0
- data/sig/Rice/Buffer/342/211/272char/342/211/273.rbs +16 -0
- data/sig/Rice/ModuleRegistry.rbs +5 -0
- data/sig/Rice/Native.rbs +9 -0
- data/sig/Rice/NativeKind.rbs +20 -0
- data/sig/Rice/NativeRegistry.rbs +5 -0
- data/sig/Rice/Parameter.rbs +7 -0
- data/sig/Rice/Pointer/342/211/272Rice/352/236/211/352/236/211detail/352/236/211/352/236/211ParameterAbstract/342/210/227/342/211/273.rbs +5 -0
- data/sig/Rice/Pointer/342/211/272char/342/211/273.rbs +5 -0
- data/sig/Rice/Reference/342/211/272char/342/211/273.rbs +6 -0
- data/sig/Rice/Reference/342/211/272int/342/211/273.rbs +6 -0
- data/sig/Rice/Reference/342/211/272unsigned/302/240Int64/342/211/273.rbs +6 -0
- data/sig/Rice/Registries.rbs +8 -0
- data/sig/Rice/TypeRegistry.rbs +5 -0
- data/sig/Std/Exception.rbs +6 -0
- data/sig/Std/Filesystem/Path.rbs +6 -0
- data/sig/Std/RuntimeError.rbs +6 -0
- data/sig/Std/Vector/342/211/272Rice/352/236/211/352/236/211detail/352/236/211/352/236/211ParameterAbstract/302/240const/342/210/227/342/211/273.rbs +33 -0
- metadata +116 -0
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <fstream> // std::*fstream
|
|
4
|
+
#include <vector> // std::vector
|
|
5
|
+
#include <memory> // std::unique_ptr
|
|
6
|
+
#include <algorithm> // std::fill
|
|
7
|
+
#include <cstdint> // std::int*_t
|
|
8
|
+
#include <cstddef> // std::size_t
|
|
9
|
+
#include <string> // std::string
|
|
10
|
+
#include <cstring> // std::memcmp
|
|
11
|
+
#include <filesystem> // std::filesystem::path
|
|
12
|
+
#include <stdexcept> // std::runtime_error
|
|
13
|
+
#include <utility> // std::exchange
|
|
14
|
+
|
|
15
|
+
namespace bmp {
|
|
16
|
+
// Magic number for Bitmap .bmp 24 bpp files (24/8 = 3 = rgb colors only)
|
|
17
|
+
static constexpr std::uint16_t BITMAP_BUFFER_MAGIC = 0x4D42;
|
|
18
|
+
|
|
19
|
+
#pragma pack(push, 1)
|
|
20
|
+
struct BitmapHeader {
|
|
21
|
+
/* Bitmap file header structure */
|
|
22
|
+
std::uint16_t magic; /* Magic number for file always BM which is 0x4D42 */
|
|
23
|
+
std::uint32_t file_size; /* Size of file */
|
|
24
|
+
std::uint16_t reserved1; /* Reserved */
|
|
25
|
+
std::uint16_t reserved2; /* Reserved */
|
|
26
|
+
std::uint32_t offset_bits; /* Offset to bitmap data */
|
|
27
|
+
/* Bitmap file info structure */
|
|
28
|
+
std::uint32_t size; /* Size of info header */
|
|
29
|
+
std::int32_t width; /* Width of image */
|
|
30
|
+
std::int32_t height; /* Height of image */
|
|
31
|
+
std::uint16_t planes; /* Number of color planes */
|
|
32
|
+
std::uint16_t bits_per_pixel; /* Number of bits per pixel */
|
|
33
|
+
std::uint32_t compression; /* Type of compression to use */
|
|
34
|
+
std::uint32_t size_image; /* Size of image data */
|
|
35
|
+
std::int32_t x_pixels_per_meter; /* X pixels per meter */
|
|
36
|
+
std::int32_t y_pixels_per_meter; /* Y pixels per meter */
|
|
37
|
+
std::uint32_t clr_used; /* Number of colors used */
|
|
38
|
+
std::uint32_t clr_important; /* Number of important colors */
|
|
39
|
+
};
|
|
40
|
+
// Sanity check
|
|
41
|
+
static_assert(sizeof(BitmapHeader) == 54, "Bitmap header size must be 54 bytes");
|
|
42
|
+
|
|
43
|
+
struct Pixel {
|
|
44
|
+
std::uint8_t r; /* Blue value */
|
|
45
|
+
std::uint8_t g; /* Green value */
|
|
46
|
+
std::uint8_t b; /* Red value */
|
|
47
|
+
|
|
48
|
+
constexpr Pixel() noexcept : r(0), g(0), b(0) {
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
explicit constexpr Pixel(const std::int32_t rgb) noexcept : r((rgb >> 16) & 0xff), g((rgb >> 8) & 0xff), b((rgb >> 0x0) & 0xff) {
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
constexpr Pixel(const std::uint8_t red, const std::uint8_t green, const std::uint8_t blue) noexcept : r(red), g(green), b(blue) {
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
constexpr bool operator==(const Pixel& other) const noexcept {
|
|
58
|
+
if (this == std::addressof(other))
|
|
59
|
+
return true;
|
|
60
|
+
return r == other.r && g == other.g && b == other.b;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
constexpr bool operator!=(const Pixel& other) const noexcept { return !((*this) == other); }
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
static_assert(sizeof(Pixel) == 3, "Bitmap Pixel size must be 3 bytes");
|
|
67
|
+
#pragma pack(pop)
|
|
68
|
+
|
|
69
|
+
static constexpr Pixel Aqua{ 0, 255, 255 };
|
|
70
|
+
static constexpr Pixel Beige{ 245, 245, 220 };
|
|
71
|
+
static constexpr Pixel Black{ 0, 0, 0 };
|
|
72
|
+
static constexpr Pixel Blue{ 0, 0, 255 };
|
|
73
|
+
static constexpr Pixel Brown{ 165, 42, 42 };
|
|
74
|
+
static constexpr Pixel Chocolate{ 210, 105, 30 };
|
|
75
|
+
static constexpr Pixel Coral{ 255, 127, 80 };
|
|
76
|
+
static constexpr Pixel Crimson{ 220, 20, 60 };
|
|
77
|
+
static constexpr Pixel Cyan{ 0, 255, 255 };
|
|
78
|
+
static constexpr Pixel Firebrick{ 178, 34, 34 };
|
|
79
|
+
static constexpr Pixel Gold{ 255, 215, 0 };
|
|
80
|
+
static constexpr Pixel Gray{ 128, 128, 128 };
|
|
81
|
+
static constexpr Pixel Green{ 0, 255, 0 };
|
|
82
|
+
static constexpr Pixel Indigo{ 75, 0, 130 };
|
|
83
|
+
static constexpr Pixel Lavender{ 230, 230, 250 };
|
|
84
|
+
static constexpr Pixel Lime{ 0, 255, 0 };
|
|
85
|
+
static constexpr Pixel Magenta{ 255, 0, 255 };
|
|
86
|
+
static constexpr Pixel Maroon{ 128, 0, 0 };
|
|
87
|
+
static constexpr Pixel Navy{ 0, 0, 128 };
|
|
88
|
+
static constexpr Pixel Olive{ 128, 128, 0 };
|
|
89
|
+
static constexpr Pixel Orange{ 255, 165, 0 };
|
|
90
|
+
static constexpr Pixel Pink{ 255, 192, 203 };
|
|
91
|
+
static constexpr Pixel Purple{ 128, 0, 128 };
|
|
92
|
+
static constexpr Pixel Red{ 255, 0, 0 };
|
|
93
|
+
static constexpr Pixel Salmon{ 250, 128, 114 };
|
|
94
|
+
static constexpr Pixel Silver{ 192, 192, 192 };
|
|
95
|
+
static constexpr Pixel Snow{ 255, 250, 250 };
|
|
96
|
+
static constexpr Pixel Teal{ 0, 128, 128 };
|
|
97
|
+
static constexpr Pixel Tomato{ 255, 99, 71 };
|
|
98
|
+
static constexpr Pixel Turquoise{ 64, 224, 208 };
|
|
99
|
+
static constexpr Pixel Violet{ 238, 130, 238 };
|
|
100
|
+
static constexpr Pixel White{ 255, 255, 255 };
|
|
101
|
+
static constexpr Pixel Wheat{ 245, 222, 179 };
|
|
102
|
+
static constexpr Pixel Yellow{ 255, 255, 0 };
|
|
103
|
+
|
|
104
|
+
class Exception : public std::runtime_error {
|
|
105
|
+
public:
|
|
106
|
+
explicit Exception(const std::string& message) : std::runtime_error(message) {
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
class Bitmap {
|
|
111
|
+
public:
|
|
112
|
+
Bitmap() noexcept : m_pixels(), m_width(0), m_height(0) {
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
explicit Bitmap(const std::string& filename) : m_pixels(), m_width(0), m_height(0) {
|
|
116
|
+
this->load(filename);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
Bitmap(const std::int32_t width, const std::int32_t height)
|
|
120
|
+
: m_pixels(static_cast<std::size_t>(width)* static_cast<std::size_t>(height)),
|
|
121
|
+
m_width(width),
|
|
122
|
+
m_height(height) {
|
|
123
|
+
if (width == 0 || height == 0)
|
|
124
|
+
throw Exception("Bitmap width and height must be > 0");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
Bitmap(const Bitmap& other) = default; // Copy Constructor
|
|
128
|
+
|
|
129
|
+
Bitmap(Bitmap&& other) noexcept
|
|
130
|
+
: m_pixels(std::move(other.m_pixels)),
|
|
131
|
+
m_width(std::exchange(other.m_width, 0)),
|
|
132
|
+
m_height(std::exchange(other.m_height, 0)) {
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
virtual ~Bitmap() noexcept = default;
|
|
136
|
+
|
|
137
|
+
public: /* Draw Primitives */
|
|
138
|
+
/**
|
|
139
|
+
* Draw a line form (x1, y1) to (x2, y2)
|
|
140
|
+
*/
|
|
141
|
+
void draw_line(std::int32_t x1, std::int32_t y1, std::int32_t x2, std::int32_t y2, const Pixel color) {
|
|
142
|
+
const std::int32_t dx = std::abs(x2 - x1);
|
|
143
|
+
const std::int32_t dy = std::abs(y2 - y1);
|
|
144
|
+
const std::int32_t sx = (x1 < x2) ? 1 : -1;
|
|
145
|
+
const std::int32_t sy = (y1 < y2) ? 1 : -1;
|
|
146
|
+
std::int32_t err = dx - dy;
|
|
147
|
+
while (true) {
|
|
148
|
+
m_pixels[IX(x1, y1)] = color;
|
|
149
|
+
|
|
150
|
+
if (x1 == x2 && y1 == y2) {
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
int e2 = 2 * err;
|
|
155
|
+
if (e2 > -dy) {
|
|
156
|
+
err -= dy;
|
|
157
|
+
x1 += sx;
|
|
158
|
+
}
|
|
159
|
+
if (e2 < dx) {
|
|
160
|
+
err += dx;
|
|
161
|
+
y1 += sy;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Draw a filled rect
|
|
168
|
+
*/
|
|
169
|
+
void fill_rect(const std::int32_t x, const std::int32_t y, const std::int32_t width, const std::int32_t height,
|
|
170
|
+
const Pixel color) {
|
|
171
|
+
if (!in_bounds(x, y) || !in_bounds(x + (width - 1), y + (height - 1)))
|
|
172
|
+
throw Exception(
|
|
173
|
+
"Bitmap::fill_rect(" + std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(width) + ", " +
|
|
174
|
+
std::to_string(height) + "): x,y,w or h out of bounds");
|
|
175
|
+
|
|
176
|
+
for (std::int32_t dx = x; dx < x + width; ++dx) {
|
|
177
|
+
for (std::int32_t dy = y; dy < y + height; ++dy) {
|
|
178
|
+
m_pixels[IX(dx, dy)] = color;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Draw a rect (not filled, border only)
|
|
185
|
+
*/
|
|
186
|
+
void draw_rect(const std::int32_t x, const std::int32_t y, const std::int32_t width, const std::int32_t height,
|
|
187
|
+
const Pixel color) {
|
|
188
|
+
if (!in_bounds(x, y) || !in_bounds(x + (width - 1), y + (height - 1)))
|
|
189
|
+
throw Exception(
|
|
190
|
+
"Bitmap::draw_rect(" + std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(width) + ", " +
|
|
191
|
+
std::to_string(height) + "): x,y,w or h out of bounds");
|
|
192
|
+
|
|
193
|
+
for (std::int32_t dx = x; dx < x + width; ++dx) {
|
|
194
|
+
m_pixels[IX(dx, y)] = color; // top
|
|
195
|
+
m_pixels[IX(dx, y + height - 1)] = color; // bottom
|
|
196
|
+
}
|
|
197
|
+
for (std::int32_t dy = y; dy < y + height; ++dy) {
|
|
198
|
+
m_pixels[IX(x, dy)] = color; // left
|
|
199
|
+
m_pixels[IX(x + width - 1, dy)] = color; // right
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Draw a triangle (not filled, border only)
|
|
206
|
+
*/
|
|
207
|
+
void draw_triangle(const std::int32_t x1, const std::int32_t y1,
|
|
208
|
+
const std::int32_t x2, const std::int32_t y2,
|
|
209
|
+
const std::int32_t x3, const std::int32_t y3,
|
|
210
|
+
const Pixel color) {
|
|
211
|
+
if (!in_bounds(x1, y1) || !in_bounds(x2, y2) || !in_bounds(x3, y3))
|
|
212
|
+
throw Exception("Bitmap::draw_triangle: One or more points are out of bounds");
|
|
213
|
+
|
|
214
|
+
draw_line(x1, y1, x2, y2, color);
|
|
215
|
+
draw_line(x2, y2, x3, y3, color);
|
|
216
|
+
draw_line(x3, y3, x1, y1, color);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Draw a filled triangle
|
|
221
|
+
*/
|
|
222
|
+
void fill_triangle(const std::int32_t x1, const std::int32_t y1,
|
|
223
|
+
const std::int32_t x2, const std::int32_t y2,
|
|
224
|
+
const std::int32_t x3, const std::int32_t y3,
|
|
225
|
+
const Pixel color) {
|
|
226
|
+
if (!in_bounds(x1, y1) || !in_bounds(x2, y2) || !in_bounds(x3, y3))
|
|
227
|
+
throw Exception("Bitmap::fill_triangle: One or more points are out of bounds");
|
|
228
|
+
|
|
229
|
+
// Sort the vertices by y-coordinate (top to bottom)
|
|
230
|
+
std::vector<std::pair<std::int32_t, std::int32_t> > vertices = {
|
|
231
|
+
{x1, y1},
|
|
232
|
+
{x2, y2},
|
|
233
|
+
{x3, y3} };
|
|
234
|
+
std::sort(vertices.begin(), vertices.end(), [](const auto& a, const auto& b) {
|
|
235
|
+
return a.second < b.second;
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
const auto [x_top, y_top] = vertices[0];
|
|
239
|
+
const auto [x_mid, y_mid] = vertices[1];
|
|
240
|
+
const auto [x_bot, y_bot] = vertices[2];
|
|
241
|
+
|
|
242
|
+
// Calculate the slopes of the left and right edges
|
|
243
|
+
const float slope_left = static_cast<float>(x_mid - x_top) / (y_mid - y_top);
|
|
244
|
+
const float slope_right = static_cast<float>(x_bot - x_top) / (y_bot - y_top);
|
|
245
|
+
|
|
246
|
+
// 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);
|
|
248
|
+
for (std::int32_t y = y_top; y <= y_mid; ++y) {
|
|
249
|
+
const auto x_start = static_cast<std::int32_t>(x_top + (y - y_top) * slope_left);
|
|
250
|
+
const auto x_end = static_cast<std::int32_t>(x_top + (y - y_top) * slope_right);
|
|
251
|
+
scanlines[y - y_top] = { x_start, x_end };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Fill the upper part of the triangle
|
|
255
|
+
for (std::int32_t y = y_top; y <= y_mid; ++y) {
|
|
256
|
+
const std::int32_t x_start = scanlines[y - y_top].first;
|
|
257
|
+
const std::int32_t x_end = scanlines[y - y_top].second;
|
|
258
|
+
draw_line(x_start, y, x_end, y, color);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Update the slope for the right edge of the triangle
|
|
262
|
+
const float new_slope_right = static_cast<float>(x_bot - x_mid) / (y_bot - y_mid);
|
|
263
|
+
|
|
264
|
+
// Update the x-coordinates for the scanlines in the lower part of the triangle
|
|
265
|
+
for (std::int32_t y = y_mid + 1; y <= y_bot; ++y) {
|
|
266
|
+
const auto x_start = static_cast<std::int32_t>(x_mid + (y - y_mid) * slope_left);
|
|
267
|
+
const auto x_end = static_cast<std::int32_t>(x_top + (y - y_top) * new_slope_right);
|
|
268
|
+
scanlines[y - y_top] = { x_start, x_end };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Fill the lower part of the triangle
|
|
272
|
+
for (std::int32_t y = y_mid + 1; y <= y_bot; ++y) {
|
|
273
|
+
const std::int32_t x_start = scanlines[y - y_top].first;
|
|
274
|
+
const std::int32_t x_end = scanlines[y - y_top].second;
|
|
275
|
+
draw_line(x_start, y, x_end, y, color);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Draw a circle with a given center and radius
|
|
281
|
+
*/
|
|
282
|
+
void draw_circle(const std::int32_t center_x, const std::int32_t center_y, const std::int32_t radius,
|
|
283
|
+
const Pixel color) {
|
|
284
|
+
if (!in_bounds(center_x - radius, center_y - radius) || !in_bounds(center_x + radius, center_y + radius))
|
|
285
|
+
throw Exception("Bitmap::draw_circle: Circle exceeds bounds");
|
|
286
|
+
|
|
287
|
+
std::int32_t x = radius;
|
|
288
|
+
std::int32_t y = 0;
|
|
289
|
+
std::int32_t err = 0;
|
|
290
|
+
|
|
291
|
+
while (x >= y) {
|
|
292
|
+
// Draw pixels in all octants
|
|
293
|
+
m_pixels[IX(center_x + x, center_y + y)] = color;
|
|
294
|
+
m_pixels[IX(center_x + y, center_y + x)] = color;
|
|
295
|
+
m_pixels[IX(center_x - y, center_y + x)] = color;
|
|
296
|
+
m_pixels[IX(center_x - x, center_y + y)] = color;
|
|
297
|
+
m_pixels[IX(center_x - x, center_y - y)] = color;
|
|
298
|
+
m_pixels[IX(center_x - y, center_y - x)] = color;
|
|
299
|
+
m_pixels[IX(center_x + y, center_y - x)] = color;
|
|
300
|
+
m_pixels[IX(center_x + x, center_y - y)] = color;
|
|
301
|
+
|
|
302
|
+
// Update error and y for the next pixel
|
|
303
|
+
if (err <= 0) {
|
|
304
|
+
y += 1;
|
|
305
|
+
err += 2 * y + 1;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Update error and x for the next pixel
|
|
309
|
+
if (err > 0) {
|
|
310
|
+
x -= 1;
|
|
311
|
+
err -= 2 * x + 1;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Fill a circle with a given center and radius
|
|
318
|
+
*/
|
|
319
|
+
void fill_circle(const std::int32_t center_x, const std::int32_t center_y, const std::int32_t radius,
|
|
320
|
+
const Pixel color) {
|
|
321
|
+
if (!in_bounds(center_x - radius, center_y - radius) || !in_bounds(center_x + radius, center_y + radius))
|
|
322
|
+
throw Exception("Bitmap::fill_circle: Circle exceeds bounds");
|
|
323
|
+
|
|
324
|
+
std::int32_t x = radius;
|
|
325
|
+
std::int32_t y = 0;
|
|
326
|
+
std::int32_t err = 0;
|
|
327
|
+
|
|
328
|
+
while (x >= y) {
|
|
329
|
+
// Fill scanlines in all octants
|
|
330
|
+
for (std::int32_t i = center_x - x; i <= center_x + x; ++i) {
|
|
331
|
+
m_pixels[IX(i, center_y + y)] = color;
|
|
332
|
+
m_pixels[IX(i, center_y - y)] = color;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
for (std::int32_t i = center_x - y; i <= center_x + y; ++i) {
|
|
336
|
+
m_pixels[IX(i, center_y + x)] = color;
|
|
337
|
+
m_pixels[IX(i, center_y - x)] = color;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Update error and y for the next pixel
|
|
341
|
+
if (err <= 0) {
|
|
342
|
+
y += 1;
|
|
343
|
+
err += 2 * y + 1;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Update error and x for the next pixel
|
|
347
|
+
if (err > 0) {
|
|
348
|
+
x -= 1;
|
|
349
|
+
err -= 2 * x + 1;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
public: /* Accessors */
|
|
355
|
+
/**
|
|
356
|
+
* Get pixel at position x,y
|
|
357
|
+
*/
|
|
358
|
+
Pixel& get(const std::int32_t x, const std::int32_t y) {
|
|
359
|
+
if (!in_bounds(x, y))
|
|
360
|
+
throw Exception("Bitmap::get(" + std::to_string(x) + ", " + std::to_string(y) + "): x,y out of bounds");
|
|
361
|
+
return m_pixels[IX(x, y)];
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Get const pixel at position x,y
|
|
366
|
+
*/
|
|
367
|
+
[[nodiscard]] const Pixel& get(const std::int32_t x, const std::int32_t y) const {
|
|
368
|
+
if (!in_bounds(x, y))
|
|
369
|
+
throw Exception("Bitmap::get(" + std::to_string(x) + ", " + std::to_string(y) + "): x,y out of bounds");
|
|
370
|
+
return m_pixels[IX(x, y)];
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Returns the width of the Bitmap image
|
|
375
|
+
*/
|
|
376
|
+
[[nodiscard]] std::int32_t width() const noexcept { return m_width; }
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Returns the height of the Bitmap image
|
|
380
|
+
*/
|
|
381
|
+
[[nodiscard]] std::int32_t height() const noexcept { return m_height; }
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Clears Bitmap pixels with an rgb color
|
|
385
|
+
*/
|
|
386
|
+
void clear(const Pixel pixel = Black) {
|
|
387
|
+
std::fill(m_pixels.begin(), m_pixels.end(), pixel);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
public: /* Operators */
|
|
391
|
+
const Pixel& operator[](const std::size_t i) const { return m_pixels[i]; }
|
|
392
|
+
|
|
393
|
+
Pixel& operator[](const std::size_t i) { return m_pixels[i]; }
|
|
394
|
+
|
|
395
|
+
bool operator!() const noexcept { return (m_pixels.empty()) || (m_width == 0) || (m_height == 0); }
|
|
396
|
+
|
|
397
|
+
explicit operator bool() const noexcept { return !(*this); }
|
|
398
|
+
|
|
399
|
+
bool operator==(const Bitmap& image) const {
|
|
400
|
+
if (this == std::addressof(image)) {
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
403
|
+
return (m_width == image.m_width) &&
|
|
404
|
+
(m_height == image.m_height) &&
|
|
405
|
+
(std::memcmp(m_pixels.data(), image.m_pixels.data(), sizeof(Pixel) * m_pixels.size()) == 0);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
bool operator!=(const Bitmap& image) const { return !(*this == image); }
|
|
409
|
+
|
|
410
|
+
Bitmap& operator=(const Bitmap& image) // Copy assignment operator
|
|
411
|
+
{
|
|
412
|
+
if (this != std::addressof(image)) {
|
|
413
|
+
m_width = image.m_width;
|
|
414
|
+
m_height = image.m_height;
|
|
415
|
+
m_pixels = image.m_pixels;
|
|
416
|
+
}
|
|
417
|
+
return *this;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
Bitmap& operator=(Bitmap&& image) noexcept {
|
|
421
|
+
if (this != std::addressof(image)) {
|
|
422
|
+
m_pixels = std::move(image.m_pixels);
|
|
423
|
+
m_width = std::exchange(image.m_width, 0);
|
|
424
|
+
m_height = std::exchange(image.m_height, 0);
|
|
425
|
+
}
|
|
426
|
+
return *this;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
public: /** foreach iterators access */
|
|
430
|
+
[[nodiscard]] std::vector<Pixel>::iterator begin() noexcept { return m_pixels.begin(); }
|
|
431
|
+
|
|
432
|
+
[[nodiscard]] std::vector<Pixel>::iterator end() noexcept { return m_pixels.end(); }
|
|
433
|
+
|
|
434
|
+
[[nodiscard]] std::vector<Pixel>::const_iterator cbegin() const noexcept { return m_pixels.cbegin(); }
|
|
435
|
+
|
|
436
|
+
[[nodiscard]] std::vector<Pixel>::const_iterator cend() const noexcept { return m_pixels.cend(); }
|
|
437
|
+
|
|
438
|
+
[[nodiscard]] std::vector<Pixel>::reverse_iterator rbegin() noexcept { return m_pixels.rbegin(); }
|
|
439
|
+
|
|
440
|
+
[[nodiscard]] std::vector<Pixel>::reverse_iterator rend() noexcept { return m_pixels.rend(); }
|
|
441
|
+
|
|
442
|
+
[[nodiscard]] std::vector<Pixel>::const_reverse_iterator crbegin() const noexcept { return m_pixels.crbegin(); }
|
|
443
|
+
|
|
444
|
+
[[nodiscard]] std::vector<Pixel>::const_reverse_iterator crend() const noexcept { return m_pixels.crend(); }
|
|
445
|
+
|
|
446
|
+
public: /* Modifiers */
|
|
447
|
+
/**
|
|
448
|
+
* Sets rgb color to pixel at position x,y
|
|
449
|
+
* @throws bmp::Exception on error
|
|
450
|
+
*/
|
|
451
|
+
void set(const std::int32_t x, const std::int32_t y, const Pixel color) {
|
|
452
|
+
if (!in_bounds(x, y)) {
|
|
453
|
+
throw Exception("Bitmap::set(" + std::to_string(x) + ", " + std::to_string(y) + "): x,y out of bounds");
|
|
454
|
+
}
|
|
455
|
+
m_pixels[IX(x, y)] = color;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Vertically flips the bitmap and returns the flipped version
|
|
461
|
+
*
|
|
462
|
+
*/
|
|
463
|
+
[[nodiscard("Bitmap::flip_v() is immutable")]]
|
|
464
|
+
Bitmap flip_v() const {
|
|
465
|
+
Bitmap finished(m_width, m_height);
|
|
466
|
+
for (std::int32_t x = 0; x < m_width; ++x) {
|
|
467
|
+
for (std::int32_t y = 0; y < m_height; ++y) {
|
|
468
|
+
// Calculate the reverse y-index
|
|
469
|
+
finished.m_pixels[IX(x, y)] = m_pixels[IX(x, m_height - 1 - y)];
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return finished;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Horizontally flips the bitmap and returns the flipped version
|
|
477
|
+
*
|
|
478
|
+
*/
|
|
479
|
+
[[nodiscard("Bitmap::flip_h() is immutable")]]
|
|
480
|
+
Bitmap flip_h() const {
|
|
481
|
+
Bitmap finished(m_width, m_height);
|
|
482
|
+
for (std::int32_t y = 0; y < m_height; ++y) {
|
|
483
|
+
for (std::int32_t x = 0; x < m_width; ++x) {
|
|
484
|
+
// Calculate the reverse x-index
|
|
485
|
+
finished.m_pixels[IX(x, y)] = m_pixels[IX(m_width - 1 - x, y)];
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
return finished;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Rotates the bitmap to the right and returns the rotated version
|
|
493
|
+
*
|
|
494
|
+
*/
|
|
495
|
+
[[nodiscard("Bitmap::rotate_90_left() is immutable")]]
|
|
496
|
+
Bitmap rotate_90_left() const {
|
|
497
|
+
Bitmap finished(m_height, m_width); // Swap dimensions
|
|
498
|
+
|
|
499
|
+
for (std::int32_t y = 0; y < m_height; ++y) {
|
|
500
|
+
const std::int32_t y_offset = y * m_width; // Precompute row start index
|
|
501
|
+
for (std::int32_t x = 0; x < m_width; ++x) {
|
|
502
|
+
// Original pixel at (x, y) moves to (y, m_width - 1 - x)
|
|
503
|
+
finished.m_pixels[(m_width - 1 - x) * m_height + y] = m_pixels[y_offset + x];
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
return finished;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Rotates the bitmap to the left and returns the rotated version
|
|
512
|
+
*
|
|
513
|
+
*/
|
|
514
|
+
[[nodiscard("Bitmap::rotate_90_right() is immutable")]]
|
|
515
|
+
Bitmap rotate_90_right() const {
|
|
516
|
+
Bitmap finished(m_height, m_width); // Swap dimensions
|
|
517
|
+
for (std::int32_t y = 0; y < m_height; ++y) {
|
|
518
|
+
const std::int32_t y_offset = y * m_width; // Precompute row start index
|
|
519
|
+
for (std::int32_t x = 0; x < m_width; ++x) {
|
|
520
|
+
finished.m_pixels[x * m_height + (m_height - 1 - y)] = m_pixels[y_offset + x];
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
return finished;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Saves Bitmap pixels into a file
|
|
529
|
+
* @throws bmp::Exception on error
|
|
530
|
+
*/
|
|
531
|
+
void save(const std::filesystem::path& filename) const {
|
|
532
|
+
// Calculate row and bitmap size
|
|
533
|
+
const std::int32_t row_size = m_width * 3 + m_width % 4;
|
|
534
|
+
const std::uint32_t bitmap_size = row_size * m_height;
|
|
535
|
+
|
|
536
|
+
// Construct bitmap header
|
|
537
|
+
BitmapHeader header{};
|
|
538
|
+
/* Bitmap file header structure */
|
|
539
|
+
header.magic = BITMAP_BUFFER_MAGIC;
|
|
540
|
+
header.file_size = bitmap_size + sizeof(BitmapHeader);
|
|
541
|
+
header.reserved1 = 0;
|
|
542
|
+
header.reserved2 = 0;
|
|
543
|
+
header.offset_bits = sizeof(BitmapHeader);
|
|
544
|
+
/* Bitmap file info structure */
|
|
545
|
+
header.size = 40;
|
|
546
|
+
header.width = m_width;
|
|
547
|
+
header.height = m_height;
|
|
548
|
+
header.planes = 1;
|
|
549
|
+
header.bits_per_pixel = sizeof(Pixel) * 8; // 24bpp
|
|
550
|
+
header.compression = 0;
|
|
551
|
+
header.size_image = bitmap_size;
|
|
552
|
+
header.x_pixels_per_meter = 0;
|
|
553
|
+
header.y_pixels_per_meter = 0;
|
|
554
|
+
header.clr_used = 0;
|
|
555
|
+
header.clr_important = 0;
|
|
556
|
+
|
|
557
|
+
// Save bitmap to output file
|
|
558
|
+
if (std::ofstream ofs{ filename, std::ios::binary }; ofs.good()) {
|
|
559
|
+
// Write Header
|
|
560
|
+
ofs.write(reinterpret_cast<const char*>(&header), sizeof(BitmapHeader));
|
|
561
|
+
if (!ofs.good()) {
|
|
562
|
+
throw Exception("Bitmap::save(\"" + filename.string() + "\"): Failed to write bitmap header to file.");
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// Write Pixels
|
|
566
|
+
std::vector<std::uint8_t> line(row_size);
|
|
567
|
+
for (std::int32_t y = m_height - 1; y >= 0; --y) {
|
|
568
|
+
std::size_t i = 0;
|
|
569
|
+
for (std::int32_t x = 0; x < m_width; ++x) {
|
|
570
|
+
const Pixel& color = m_pixels[IX(x, y)];
|
|
571
|
+
line[i++] = color.b;
|
|
572
|
+
line[i++] = color.g;
|
|
573
|
+
line[i++] = color.r;
|
|
574
|
+
}
|
|
575
|
+
ofs.write(reinterpret_cast<const char*>(line.data()), line.size());
|
|
576
|
+
if (!ofs.good()) {
|
|
577
|
+
throw Exception("Bitmap::save(\"" + filename.string() + "\"): Failed to write bitmap pixels to file.");
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
else
|
|
582
|
+
throw Exception("Bitmap::save(\"" + filename.string() + "\"): Failed to open file.");
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Loads Bitmap from file
|
|
587
|
+
* @throws bmp::Exception on error
|
|
588
|
+
*/
|
|
589
|
+
void load(const std::filesystem::path& filename) {
|
|
590
|
+
m_pixels.clear();
|
|
591
|
+
|
|
592
|
+
if (std::ifstream ifs{ filename, std::ios::binary }; ifs.good()) {
|
|
593
|
+
// Read Header
|
|
594
|
+
std::unique_ptr<BitmapHeader> header(new BitmapHeader());
|
|
595
|
+
ifs.read(reinterpret_cast<char*>(header.get()), sizeof(BitmapHeader));
|
|
596
|
+
|
|
597
|
+
// Check if Bitmap file is valid
|
|
598
|
+
if (header->magic != BITMAP_BUFFER_MAGIC) {
|
|
599
|
+
throw Exception("Bitmap::load(\"" + filename.string() + "\"): Unrecognized file format.");
|
|
600
|
+
}
|
|
601
|
+
// Check if the Bitmap file has 24 bits per pixel (for now supporting only 24bpp bitmaps)
|
|
602
|
+
if (header->bits_per_pixel != 24) {
|
|
603
|
+
throw Exception("Bitmap::load(\"" + filename.string() + "\"): Only 24 bits per pixel bitmaps supported.");
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// Seek the beginning of the pixels data
|
|
607
|
+
// Note: We can't just assume we're there right after we read the BitmapHeader
|
|
608
|
+
// Because some editors like Gimp might put extra information after the header.
|
|
609
|
+
// Thanks to @seeliger-ec
|
|
610
|
+
ifs.seekg(header->offset_bits);
|
|
611
|
+
|
|
612
|
+
// Set width & height
|
|
613
|
+
m_width = header->width;
|
|
614
|
+
m_height = header->height;
|
|
615
|
+
|
|
616
|
+
// Resize pixels size
|
|
617
|
+
m_pixels.resize(static_cast<std::size_t>(m_width) * static_cast<std::size_t>(m_height), Black);
|
|
618
|
+
|
|
619
|
+
// Read Bitmap pixels
|
|
620
|
+
const std::int32_t row_size = m_width * 3 + m_width % 4;
|
|
621
|
+
std::vector<std::uint8_t> line(row_size);
|
|
622
|
+
for (std::int32_t y = m_height - 1; y >= 0; --y) {
|
|
623
|
+
ifs.read(reinterpret_cast<char*>(line.data()), line.size());
|
|
624
|
+
if (!ifs.good())
|
|
625
|
+
throw Exception("Bitmap::load(\"" + filename.string() + "\"): Failed to read bitmap pixels from file.");
|
|
626
|
+
std::size_t i = 0;
|
|
627
|
+
for (std::int32_t x = 0; x < m_width; ++x) {
|
|
628
|
+
Pixel color{};
|
|
629
|
+
color.b = line[i++];
|
|
630
|
+
color.g = line[i++];
|
|
631
|
+
color.r = line[i++];
|
|
632
|
+
m_pixels[IX(x, y)] = color;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
else
|
|
637
|
+
throw Exception("Bitmap::load(\"" + filename.string() + "\"): Failed to load bitmap pixels from file.");
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
private: /* Utils */
|
|
641
|
+
/**
|
|
642
|
+
* Converts 2D x,y coords into 1D index
|
|
643
|
+
*/
|
|
644
|
+
[[nodiscard]] constexpr std::size_t IX(const std::int32_t x, const std::int32_t y) const noexcept {
|
|
645
|
+
return static_cast<std::size_t>(x) + static_cast<std::size_t>(m_width) * static_cast<std::size_t>(y);
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Returns true if x,y coords are within boundaries
|
|
649
|
+
*/
|
|
650
|
+
[[nodiscard]] constexpr bool in_bounds(const std::int32_t x, const std::int32_t y) const noexcept {
|
|
651
|
+
return (x >= 0) && (x < m_width) && (y >= 0) && (y < m_height);
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
private:
|
|
655
|
+
std::vector<Pixel> m_pixels;
|
|
656
|
+
std::int32_t m_width;
|
|
657
|
+
std::int32_t m_height;
|
|
658
|
+
};
|
|
659
|
+
}
|