gempyre  1.7.1
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 
19 #ifdef WINDOWS_EXPORT
20  #ifndef GEMPYRE_EX
21  #define GGEMPYRE_EX __declspec( dllexport )
22  //#else
23  // #define GEMPYRE_EX
24  #endif
25 #endif
26 
27 #define gempyre_graphics_assert(b, x) ((b) || GempyreUtils::do_fatal(x, nullptr, __FILE__, __LINE__));
28 
29 
30 namespace Gempyre {
31  class CanvasElement;
32 
34  namespace Color {
36  using type = Gempyre::dataT;
37 
38  [[nodiscard]]
40  static constexpr inline type rgba_clamped(type r, type g, type b, type a = 0xFF) {
41  const type FF = 0xFF;
42  return std::min(FF, r) | (std::min(FF, g) << 8) | (std::min(FF, b) << 16) | (std::min(FF, a) << 24);
43  }
44 
45  [[nodiscard]]
47  static constexpr inline type rgba(type r, type g, type b, type a = 0xFF) {
48  return r | (g << 8) | (b << 16) | (a << 24);
49  }
50 
51  [[nodiscard]]
53  static constexpr inline type rgb(type r, type g, type b) {
54  return r | (g << 8) | (b << 16) | (static_cast<type>(0xFF) << 24);
55  }
56 
57  [[nodiscard]]
59  static constexpr inline type r(type pixel) {
60  return pixel & static_cast<type>(0xFF);
61  }
62  [[nodiscard]]
64  static constexpr inline type g(type pixel) {
65  return (pixel & static_cast<type>(0xFF00)) >> 8;
66  }
67 
68  [[nodiscard]]
70  static constexpr inline type b(type pixel) {
71  return (pixel & static_cast<type>(0xFF0000)) >> 16;
72  }
73 
74  [[nodiscard]]
76  static constexpr inline type alpha(type pixel) {
77  return (pixel & 0xFF000000) >> 24;
78  }
79 
80  [[nodiscard]]
82  static inline std::string rgba(type pixel) {
83  constexpr auto c = "0123456789ABCDEF";
84  std::string v("#RRGGBBAA");
85  v[1] = c[r(pixel) >> 4];
86  v[2] = c[r(pixel) & 0xF];
87  v[3] = c[g(pixel) >> 4];
88  v[4] = c[g(pixel) & 0xF];
89  v[5] = c[b(pixel) >> 4];
90  v[6] = c[b(pixel) & 0xF];
91  v[7] = c[alpha(pixel) >> 4];
92  v[8] = c[alpha(pixel) & 0xF];
93  return v;
94  }
95 
96  [[nodiscard]]
98  static inline std::string rgb(type pixel) {
99  constexpr auto c = "0123456789ABCDEF";
100  std::string v("#RRGGBB");
101  v[1] = c[r(pixel) >> 4];
102  v[2] = c[r(pixel) & 0xF];
103  v[3] = c[g(pixel) >> 4];
104  v[4] = c[g(pixel) & 0xF];
105  v[5] = c[b(pixel) >> 4];
106  v[6] = c[b(pixel) & 0xF];
107  return v;
108  }
109 
110  [[nodiscard]]
112  static inline std::string to_string(type r, type g, type b, type a = 0xFF) {
113  return a == 0xFF ? Gempyre::Color::rgb(Gempyre::Color::rgb(r, g, b)) : Gempyre::Color::rgba(Gempyre::Color::rgba(r, g, b, a));
114  }
115 
116  [[nodiscard]]
118  static inline std::string to_string(Gempyre::Color::type color) {
119  return Gempyre::Color::to_string(
120  Gempyre::Color::r(color),
121  Gempyre::Color::g(color),
122  Gempyre::Color::b(color),
123  Gempyre::Color::alpha(color));
124  }
125 
127  static constexpr Color::type Black = Color::rgba(0, 0, 0, 0xFF);
129  static constexpr Color::type White = Color::rgba(0xFF, 0xFF, 0xFF, 0xFF);
131  static constexpr Color::type Red = Color::rgba(0xFF, 0, 0, 0xFF);
133  static constexpr Color::type Green = Color::rgba(0, 0xFF, 0, 0xFF);
135  static constexpr Color::type Blue = Color::rgba(0, 0, 0xFF, 0xFF);
137  static constexpr Color::type Cyan = Color::rgba(0, 0xFF, 0xFF, 0xFF);
139  static constexpr Color::type Magenta = Color::rgba(0xFF, 0, 0xFF, 0xFF);
141  static constexpr Color::type Yellow = Color::rgba(0xFF, 0xFF, 0, 0xFF);
143  static constexpr Color::type Aqua = Cyan;
145  static constexpr Color::type Fuchsia = Magenta;
147  static constexpr Color::type Lime = Green;
149  static constexpr Color::type Transparent = Color::rgba(0, 0, 0, 0);
150  }
151 
152 
154  class GEMPYRE_EX Bitmap {
155  public:
159  Bitmap(int width, int height);
160 
165  Bitmap(int width, int height, Gempyre::Color::type color);
166 
169 
171  Bitmap(Bitmap&& other) = default;
172 
174  Bitmap(const Bitmap& other) = default;
175 
178 
186  Bitmap(const std::vector<uint8_t>& image_data);
187 
190  std::vector<uint8_t> png_image() const;
191 
193  Bitmap& operator=(const Bitmap& other) = default;
194 
196  Bitmap& operator=(Bitmap&& other) = default;
197 
201  void create(int width, int height);
202 
204  Bitmap clone() const;
205 
207  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);}
208 
210  void set_pixel(int x, int y, Color::type color);
211 
213  void set_alpha(int x, int y, Color::type alpha);
214 
216  Color::type pixel(int x, int y) const;
217 
219  [[nodiscard]] int width() const;
220 
222  [[nodiscard]] int height() const;
223 
225  void swap(Bitmap& other);
226 
228  void draw_rect(const Gempyre::Rect& rect, Color::type color);
229 
231  void merge(int x, int y, const Bitmap& other);
232 
234  void merge(const Bitmap& other) {merge(0, 0, other);}
235 
237  void tile(int x, int y, const Bitmap& other);
238 
240  void tile(int x, int y, const Bitmap& other, int width, int height);
241 
243  void tile(int x, int y, const Bitmap& other, int other_x, int other_y, int width, int height);
244 
246  Bitmap clip(const Gempyre::Rect& rect) const;
247 
249  bool empty() const;
250 
252  const uint8_t* const_data() const;
253 
258  template<class T, std::enable_if_t<std::is_same_v<typename T::value_type, Color::type>, int> = 0>
259  bool set_data(const T& bytes, size_t offset = 0) {
260  if(bytes.size() + offset > size())
261  return false;
262  std::memcpy(inner_data() + offset * sizeof(Color::type), bytes.data(), sizeof(Color::type) * bytes.size());
263  return true;
264  }
265 
266  protected:
268  void copy_from(const Bitmap& other);
269  Color::type* inner_data();
270  std::size_t size() const;
272  private:
273  friend class Gempyre::CanvasElement;
274  Gempyre::CanvasDataPtr m_canvas{};
275  };
276 
277 }
Bitmap for Gempyre Graphics.
Definition: gempyre_bitmap.h:154
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.
Color::type pixel(int x, int y) const
Get a single pixel.
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.
Bitmap & operator=(const Bitmap &other)=default
Copy operator does only shallow copy, for deep copy.
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=(Bitmap &&other)=default
Move operator.
const uint8_t * const_data() const
underlaying data
bool set_data(const T &bytes, size_t offset=0)
Copy pixels into bitmap.
Definition: gempyre_bitmap.h:259
~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:207
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:234
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:39
Gempyre::dataT type
pixel type
Definition: gempyre_bitmap.h:36
Rect.
Definition: gempyre_types.h:24