gempyre 1.8.1
Loading...
Searching...
No Matches
gempyre_bitmap.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <algorithm>
5#include <cstring> // memcpy (windows)
6#include <gempyre_types.h>
7#include <array>
8#include <optional>
9#include <string_view>
10#include <vector>
11#include <type_traits>
12#include <algorithm>
13
24#ifdef WINDOWS_EXPORT
25 #ifndef GEMPYRE_EX
26 #define GGEMPYRE_EX __declspec( dllexport )
27 //#else
28 // #define GEMPYRE_EX
29 #endif
30#endif
31
32#define gempyre_graphics_assert(b, x) ((b) || GempyreUtils::do_fatal(x, nullptr, __FILE__, __LINE__));
33
34
35namespace Gempyre {
36 class CanvasElement;
37
39 namespace Color {
41 using type = Gempyre::dataT;
42
43 [[nodiscard]]
45 static constexpr inline type rgba_clamped(type r, type g, type b, type a = 0xFF) {
46 const type FF = 0xFF;
47 return std::min(FF, r) | (std::min(FF, g) << 8) | (std::min(FF, b) << 16) | (std::min(FF, a) << 24);
48 }
49
50 [[nodiscard]]
52 static constexpr inline type rgba(type r, type g, type b, type a = 0xFF) {
53 return r | (g << 8) | (b << 16) | (a << 24);
54 }
55
56 [[nodiscard]]
58 static constexpr inline type rgb(type r, type g, type b) {
59 return r | (g << 8) | (b << 16) | (static_cast<type>(0xFF) << 24);
60 }
61
62 [[nodiscard]]
64 static constexpr inline type r(type pixel) {
65 return pixel & static_cast<type>(0xFF);
66 }
67 [[nodiscard]]
69 static constexpr inline type g(type pixel) {
70 return (pixel & static_cast<type>(0xFF00)) >> 8;
71 }
72
73 [[nodiscard]]
75 static constexpr inline type b(type pixel) {
76 return (pixel & static_cast<type>(0xFF0000)) >> 16;
77 }
78
79 [[nodiscard]]
81 static constexpr inline type alpha(type pixel) {
82 return (pixel & 0xFF000000) >> 24;
83 }
84
85 [[nodiscard]]
87 static inline std::string rgba(type pixel) {
88 constexpr auto c = "0123456789ABCDEF";
89 std::string v("#RRGGBBAA");
90 v[1] = c[r(pixel) >> 4];
91 v[2] = c[r(pixel) & 0xF];
92 v[3] = c[g(pixel) >> 4];
93 v[4] = c[g(pixel) & 0xF];
94 v[5] = c[b(pixel) >> 4];
95 v[6] = c[b(pixel) & 0xF];
96 v[7] = c[alpha(pixel) >> 4];
97 v[8] = c[alpha(pixel) & 0xF];
98 return v;
99 }
100
101 [[nodiscard]]
103 static inline std::string rgb(type pixel) {
104 constexpr auto c = "0123456789ABCDEF";
105 std::string v("#RRGGBB");
106 v[1] = c[r(pixel) >> 4];
107 v[2] = c[r(pixel) & 0xF];
108 v[3] = c[g(pixel) >> 4];
109 v[4] = c[g(pixel) & 0xF];
110 v[5] = c[b(pixel) >> 4];
111 v[6] = c[b(pixel) & 0xF];
112 return v;
113 }
114
115 [[nodiscard]]
117 static inline std::string to_string(type r, type g, type b, type a = 0xFF) {
118 return a == 0xFF ? Gempyre::Color::rgb(Gempyre::Color::rgb(r, g, b)) : Gempyre::Color::rgba(Gempyre::Color::rgba(r, g, b, a));
119 }
120
121 [[nodiscard]]
123 static inline std::string to_string(Gempyre::Color::type color) {
124 return Gempyre::Color::to_string(
125 Gempyre::Color::r(color),
126 Gempyre::Color::g(color),
127 Gempyre::Color::b(color),
128 Gempyre::Color::alpha(color));
129 }
130
131 [[nodiscard]]
133 static inline auto set_alpha(Gempyre::Color::type col, uint8_t alpha_val) {
134 return Gempyre::Color::rgba(Gempyre::Color::r(col), Gempyre::Color::g(col), Gempyre::Color::b(col), alpha_val);
135 }
136
137
138 [[nodiscard]]
140 static inline auto rgb_value(uint32_t col) {
141 return Gempyre::Color::rgba(0xFF & (col >> 16), 0xFF & (col >> 8), 0xFF & col, 0xFF);
142 }
143
144 [[nodiscard]]
146 static inline auto rgba_value(uint32_t col) {
147 return Gempyre::Color::rgba(0xFF & (col >> 24), 0xFF & (col >> 16), 0xFF & (col >> 8), 0xFF & col);
148 }
149
150
152 static constexpr Color::type Black = Color::rgba(0, 0, 0, 0xFF);
154 static constexpr Color::type White = Color::rgba(0xFF, 0xFF, 0xFF, 0xFF);
156 static constexpr Color::type Red = Color::rgba(0xFF, 0, 0, 0xFF);
158 static constexpr Color::type Green = Color::rgba(0, 0xFF, 0, 0xFF);
160 static constexpr Color::type Blue = Color::rgba(0, 0, 0xFF, 0xFF);
162 static constexpr Color::type Cyan = Color::rgba(0, 0xFF, 0xFF, 0xFF);
164 static constexpr Color::type Magenta = Color::rgba(0xFF, 0, 0xFF, 0xFF);
166 static constexpr Color::type Yellow = Color::rgba(0xFF, 0xFF, 0, 0xFF);
168 static constexpr Color::type Aqua = Cyan;
170 static constexpr Color::type Fuchsia = Magenta;
172 static constexpr Color::type Lime = Green;
174 static constexpr Color::type Transparent = Color::rgba(0, 0, 0, 0);
175
177 constexpr std::array<std::pair<std::string_view, type>, 147> html_colors = {{
178 {"AliceBlue", 0xF0F8FF}, {"AntiqueWhite", 0xFAEBD7}, {"Aqua", 0x00FFFF},
179 {"Aquamarine", 0x7FFFD4}, {"Azure", 0xF0FFFF}, {"Beige", 0xF5F5DC},
180 {"Bisque", 0xFFE4C4}, {"Black", 0x000000}, {"BlanchedAlmond", 0xFFEBCD},
181 {"Blue", 0x0000FF}, {"BlueViolet", 0x8A2BE2}, {"Brown", 0xA52A2A},
182 {"BurlyWood", 0xDEB887}, {"CadetBlue", 0x5F9EA0}, {"Chartreuse", 0x7FFF00},
183 {"Chocolate", 0xD2691E}, {"Coral", 0xFF7F50}, {"CornflowerBlue", 0x6495ED},
184 {"Cornsilk", 0xFFF8DC}, {"Crimson", 0xDC143C}, {"Cyan", 0x00FFFF},
185 {"DarkBlue", 0x00008B}, {"DarkCyan", 0x008B8B}, {"DarkGoldenRod", 0xB8860B},
186 {"DarkGray", 0xA9A9A9}, {"DarkGreen", 0x006400}, {"DarkKhaki", 0xBDB76B},
187 {"DarkMagenta", 0x8B008B}, {"DarkOliveGreen", 0x556B2F}, {"DarkOrange", 0xFF8C00},
188 {"DarkOrchid", 0x9932CC}, {"DarkRed", 0x8B0000}, {"DarkSalmon", 0xE9967A},
189 {"DarkSeaGreen", 0x8FBC8F}, {"DarkSlateBlue", 0x483D8B}, {"DarkSlateGray", 0x2F4F4F},
190 {"DarkTurquoise", 0x00CED1}, {"DarkViolet", 0x9400D3}, {"DeepPink", 0xFF1493},
191 {"DeepSkyBlue", 0x00BFFF}, {"DimGray", 0x696969}, {"DodgerBlue", 0x1E90FF},
192 {"FireBrick", 0xB22222}, {"FloralWhite", 0xFFFAF0}, {"ForestGreen", 0x228B22},
193 {"Fuchsia", 0xFF00FF}, {"Gainsboro", 0xDCDCDC}, {"GhostWhite", 0xF8F8FF},
194 {"Gold", 0xFFD700}, {"GoldenRod", 0xDAA520}, {"Gray", 0x808080},
195 {"Green", 0x008000}, {"GreenYellow", 0xADFF2F}, {"HoneyDew", 0xF0FFF0},
196 {"HotPink", 0xFF69B4}, {"IndianRed", 0xCD5C5C}, {"Indigo", 0x4B0082},
197 {"Ivory", 0xFFFFF0}, {"Khaki", 0xF0E68C}, {"Lavender", 0xE6E6FA},
198 {"LavenderBlush", 0xFFF0F5}, {"LawnGreen", 0x7CFC00}, {"LemonChiffon", 0xFFFACD},
199 {"LightBlue", 0xADD8E6}, {"LightCoral", 0xF08080}, {"LightCyan", 0xE0FFFF},
200 {"LightGoldenRodYellow", 0xFAFAD2}, {"LightGray", 0xD3D3D3}, {"LightGreen", 0x90EE90},
201 {"LightPink", 0xFFB6C1}, {"LightSalmon", 0xFFA07A}, {"LightSeaGreen", 0x20B2AA},
202 {"LightSkyBlue", 0x87CEFA}, {"LightSlateGray", 0x778899}, {"LightSteelBlue", 0xB0C4DE},
203 {"LightYellow", 0xFFFFE0}, {"Lime", 0x00FF00}, {"LimeGreen", 0x32CD32},
204 {"Linen", 0xFAF0E6}, {"Magenta", 0xFF00FF}, {"Maroon", 0x800000},
205 {"MediumAquaMarine", 0x66CDAA}, {"MediumBlue", 0x0000CD}, {"MediumOrchid", 0xBA55D3},
206 {"MediumPurple", 0x9370DB}, {"MediumSeaGreen", 0x3CB371}, {"MediumSlateBlue", 0x7B68EE},
207 {"MediumSpringGreen", 0x00FA9A}, {"MediumTurquoise", 0x48D1CC}, {"MediumVioletRed", 0xC71585},
208 {"MidnightBlue", 0x191970}, {"MintCream", 0xF5FFFA}, {"MistyRose", 0xFFE4E1},
209 {"Moccasin", 0xFFE4B5}, {"NavajoWhite", 0xFFDEAD}, {"Navy", 0x000080},
210 {"OldLace", 0xFDF5E6}, {"Olive", 0x808000}, {"OliveDrab", 0x6B8E23},
211 {"Orange", 0xFFA500}, {"OrangeRed", 0xFF4500}, {"Orchid", 0xDA70D6},
212 {"PaleGoldenRod", 0xEEE8AA}, {"PaleGreen", 0x98FB98}, {"PaleTurquoise", 0xAFEEEE},
213 {"PaleVioletRed", 0xDB7093}, {"PapayaWhip", 0xFFEFD5}, {"PeachPuff", 0xFFDAB9},
214 {"Peru", 0xCD853F}, {"Pink", 0xFFC0CB}, {"Plum", 0xDDA0DD},
215 {"PowderBlue", 0xB0E0E6}, {"Purple", 0x800080}, {"RebeccaPurple", 0x663399},
216 {"Red", 0xFF0000}, {"RosyBrown", 0xBC8F8F}, {"RoyalBlue", 0x4169E1},
217 {"SaddleBrown", 0x8B4513}, {"Salmon", 0xFA8072}, {"SandyBrown", 0xF4A460},
218 {"SeaGreen", 0x2E8B57}, {"SeaShell", 0xFFF5EE}, {"Sienna", 0xA0522D},
219 {"Silver", 0xC0C0C0}, {"SkyBlue", 0x87CEEB}, {"SlateBlue", 0x6A5ACD},
220 {"SlateGray", 0x708090}, {"Snow", 0xFFFAFA}, {"SpringGreen", 0x00FF7F},
221 {"SteelBlue", 0x4682B4}, {"Tan", 0xD2B48C}, {"Teal", 0x008080},
222 {"Thistle", 0xD8BFD8}, {"Tomato", 0xFF6347}, {"Turquoise", 0x40E0D0},
223 {"Violet", 0xEE82EE}, {"Wheat", 0xF5DEB3}, {"White", 0xFFFFFF},
224 {"WhiteSmoke", 0xF5F5F5}, {"Yellow", 0xFFFF00}, {"YellowGreen", 0x9ACD32}
225 }};
226
227 [[nodiscard]] inline bool is_equal(type a, type b) {
228 return (a & 0xFFFFFF) == (b & 0xFFFFFF);
229 }
230
231 [[nodiscard]]
233 std::optional<type> from_html_name(std::string_view name);
234
235 [[nodiscard]]
237 std::optional<type> get_color(std::string_view color);
238
239 }
240
242 class GEMPYRE_EX Bitmap {
243 public:
247 Bitmap(int width, int height);
248
253 Bitmap(int width, int height, Gempyre::Color::type color);
254
257
259 Bitmap(Bitmap&& other) = default;
260
262 Bitmap(const Bitmap& other) = default;
263
266
274 Bitmap(const std::vector<uint8_t>& image_data);
275
278 std::vector<uint8_t> png_image() const;
279
281 Bitmap& operator=(const Bitmap& other) = default;
282
284 Bitmap& operator=(Bitmap&& other) = default;
285
289 void create(int width, int height);
290
292 Bitmap clone() const;
293
295 static constexpr Color::type pix(Color::type r, Color::type g, Color::type b, Color::type a = 0xFF) {return Color::rgba(r, g, b, a);}
296
298 void set_pixel(int x, int y, Color::type color);
299
301 void set_alpha(int x, int y, Color::type alpha);
302
304 Color::type pixel(int x, int y) const;
305
307 [[nodiscard]] int width() const;
308
310 [[nodiscard]] int height() const;
311
313 void swap(Bitmap& other);
314
316 void draw_rect(const Gempyre::Rect& rect, Color::type color);
317
319 void merge(int x, int y, const Bitmap& other);
320
322 void merge(const Bitmap& other) {merge(0, 0, other);}
323
325 void tile(int x, int y, const Bitmap& other);
326
328 void tile(int x, int y, const Bitmap& other, int width, int height);
329
331 void tile(int x, int y, const Bitmap& other, int other_x, int other_y, int width, int height);
332
334 Bitmap clip(const Gempyre::Rect& rect) const;
335
337 bool empty() const;
338
340 const uint8_t* const_data() const;
341
346 template<class T, std::enable_if_t<std::is_same_v<typename T::value_type, Color::type>, int> = 0>
347 bool set_data(const T& bytes, size_t offset = 0) {
348 if(bytes.size() + offset > size())
349 return false;
350 std::memcpy(inner_data() + offset * sizeof(Color::type), bytes.data(), sizeof(Color::type) * bytes.size());
351 return true;
352 }
353
354 protected:
356 void copy_from(const Bitmap& other);
357 Color::type* inner_data();
358 std::size_t size() const;
360 private:
361 friend class Gempyre::CanvasElement;
362 Gempyre::CanvasDataPtr m_canvas{};
363 };
364
365}
Bitmap for Gempyre Graphics.
Definition gempyre_bitmap.h:242
bool empty() const
return true if there is not data
void create(int width, int height)
Create bitmap bytes.
void tile(int x, int y, const Bitmap &other, int other_x, int other_y, int width, int height)
Draw a Bitmap withing extents on this bitmap - replace area.
Bitmap clone() const
Deep copy bitmap bytes.
Bitmap clip(const Gempyre::Rect &rect) const
Create a new bitmap from part of bitmap.
std::vector< uint8_t > png_image() const
Convert a bitmap to PNG.
Bitmap(int width, int height)
Constructor - uninitialized data.
int width() const
Get width.
const uint8_t * const_data() const
underlaying data
Color::type pixel(int x, int y) const
Get a single pixel.
Bitmap & operator=(Bitmap &&other)=default
Move operator.
int height() const
Get height.
void swap(Bitmap &other)
Swap bitmap data with another.
void tile(int x, int y, const Bitmap &other)
Draw a Bitmap on this bitmap - replace area.
void draw_rect(const Gempyre::Rect &rect, Color::type color)
Draw a rect with a color in bitmap.
void merge(int x, int y, const Bitmap &other)
Draw a Bitmap on this bitmap - merge alpha.
Bitmap & operator=(const Bitmap &other)=default
Copy operator does only shallow copy, for deep copy.
bool set_data(const T &bytes, size_t offset=0)
Copy pixels into bitmap.
Definition gempyre_bitmap.h:347
~Bitmap()
Destructor.
Bitmap(int width, int height, Gempyre::Color::type color)
Constructor - with a single color data.
Bitmap(Bitmap &&other)=default
Move constructor.
void set_pixel(int x, int y, Color::type color)
Set a single pixel.
Bitmap()
Constructor - zero size, use.
static constexpr Color::type pix(Color::type r, Color::type g, Color::type b, Color::type a=0xFF)
Components to pixel type.
Definition gempyre_bitmap.h:295
void tile(int x, int y, const Bitmap &other, int width, int height)
Draw a Bitmap withing extents on this bitmap - replace area.
void merge(const Bitmap &other)
Draw a Bitmap on this bitmap - merge alpha.
Definition gempyre_bitmap.h:322
Bitmap(const Bitmap &other)=default
Copy constructor - does not copy the data, for deep copy.
Bitmap(const std::vector< uint8_t > &image_data)
Create bitmap from byte array.
void set_alpha(int x, int y, Color::type alpha)
Set a singe pixel's alpha value.
Graphics element.
Definition gempyre_graphics.h:41
constexpr std::array< std::pair< std::string_view, type >, 147 > html_colors
HTML colors.
Definition gempyre_bitmap.h:177
std::optional< type > get_color(std::string_view color)
get color from string, where string is a HTML color name, #RRGGBB, #RRGGBBAA, 0xRRGGBB or 0xRRGGBBAA
Gempyre::dataT type
pixel type
Definition gempyre_bitmap.h:41
std::optional< type > from_html_name(std::string_view name)
find a HTML color as Color::type
Rect.
Definition gempyre_types.h:24