Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
bitmap.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2021-2025 sfan5
4
5#include "irrlichttypes.h"
6#include <vector>
7#include <algorithm>
8#include <cassert>
9
14class Bitmap {
16 std::vector<u8> data;
17
18 static inline u32 bytepos(u32 index) { return index >> 3; }
19 static inline u8 bitpos(u32 index) { return index & 7; }
20
21 template<bool set, bool toggle, bool clear>
22 bool modify_(u32 x, u32 y)
23 {
24 u32 index = y * linesize + x;
25 u8 mask = 1 << bitpos(index);
26 u8 byte = data[bytepos(index)];
27 if constexpr (set)
28 byte |= mask;
29 else if constexpr (toggle)
30 byte ^= mask;
31 else if constexpr (clear)
32 byte &= ~mask;
33 data[bytepos(index)] = byte;
34 return byte & mask;
35 }
36
37public:
39 Bitmap() : linesize(0), lines(0) {}
40
43 {
45 }
46
47 inline u32 width() const { return linesize; }
48 inline u32 height() const { return lines; }
49
50 inline void resize(u32 width, u32 height, bool initial_value=false)
51 {
52 assert(width <= 65534 && height <= 65534); // index would overflow
54 lines = height;
55 data.clear(); // make sure to discard all data
56 if (width && height)
57 data.resize(bytepos(width * height) + 1, static_cast<u8>(initial_value ? 0xff : 0));
58 }
59
60 inline void reset(bool value)
61 {
62 std::fill(data.begin(), data.end(), value ? 0xff : 0);
63 }
64
65 inline bool get(u32 x, u32 y) const
66 {
67 u32 index = y * linesize + x;
68 return data[bytepos(index)] & (1 << bitpos(index));
69 }
70
71 inline void set(u32 x, u32 y) { modify_<1, 0, 0>(x, y); }
72 inline void unset(u32 x, u32 y) { modify_<0, 0, 1>(x, y); }
73 inline bool toggle(u32 x, u32 y) { return modify_<0, 1, 0>(x, y); }
74
76 inline bool all() const
77 {
78 if (!linesize || !lines)
79 return (assert(0), true);
80 for (u32 i = 0; i < data.size() - 1; i++) {
81 if (data[i] != 0xff)
82 return false;
83 }
84 u8 last_byte = data.back(); // not used entirely
85 for (u8 i = 0; i < bitpos(linesize * lines); i++) {
86 if (!(last_byte & (1 << i)))
87 return false;
88 }
89 return true;
90 }
91};
92
93// Note: a 3D class could be written based on the 2D one. Or maybe the other way?
Rudimentary header-only 2D bitmap class.
Definition bitmap.h:14
void set(u32 x, u32 y)
Definition bitmap.h:71
void reset(bool value)
Definition bitmap.h:60
u32 lines
Definition bitmap.h:15
void unset(u32 x, u32 y)
Definition bitmap.h:72
static u8 bitpos(u32 index)
Definition bitmap.h:19
bool all() const
Returns true if all bits in the bitmap are set.
Definition bitmap.h:76
u32 width() const
Definition bitmap.h:47
bool get(u32 x, u32 y) const
Definition bitmap.h:65
u32 linesize
Definition bitmap.h:15
u32 height() const
Definition bitmap.h:48
bool toggle(u32 x, u32 y)
Definition bitmap.h:73
Bitmap(u32 width, u32 height)
Create a new zero-filled bitmap.
Definition bitmap.h:42
Bitmap()
Create an empty bitmap.
Definition bitmap.h:39
static u32 bytepos(u32 index)
Definition bitmap.h:18
bool modify_(u32 x, u32 y)
Definition bitmap.h:22
void resize(u32 width, u32 height, bool initial_value=false)
Definition bitmap.h:50
std::vector< u8 > data
Definition bitmap.h:16
constexpr v3f x
Definition test_irr_matrix4.cpp:18
constexpr v3f y
Definition test_irr_matrix4.cpp:19