raylib-cpp
C++ object-oriented wrapper library for raylib.
Image.hpp
1 #ifndef RAYLIB_CPP_IMAGE_HPP_
2 #define RAYLIB_CPP_IMAGE_HPP_
3 
4 #include <string>
5 #ifdef __cplusplus
6 extern "C"{
7 #endif
8 #include "raylib.h"
9 #ifdef __cplusplus
10 }
11 #endif
12 
13 #include "utils.hpp"
14 
15 namespace raylib {
16  class Image : public ::Image {
17  public:
18  Image() {};
19  Image(::Image image) {
20  set(image);
21  }
22  Image(const std::string& fileName) {
23  Load(fileName);
24  }
25  Image(::Color* pixels, int width, int height) {
26  LoadEx(pixels, width, height);
27  }
28  Image(void* data, int width, int height, int format) {
29  LoadPro(data, width, height, format);
30  }
31  Image(const std::string& fileName, int width, int height, int format, int headerSize) {
32  LoadRaw(fileName, width, height, format, headerSize);
33  }
34  Image(::Texture2D texture) {
35  set(::GetTextureData(texture));
36  }
37  Image(int width, int height, Color color = raylib::Color::RayWhite) {
38  set(::GenImageColor(width, height, color));
39  }
40 
41  static Image Text(std::string text, int fontSize, Color color) {
42  return ::ImageText(text.c_str(), fontSize, color);
43  }
44 
45  static Image Text(Font font, std::string text, float fontSize, float spacing, Color tint) {
46  return ::ImageTextEx(font, text.c_str(), fontSize, spacing, tint);
47  }
48 
49  static Image GetScreenData() {
50  return Image(::GetScreenData());
51  }
52 
53  static Image GenColor(int width, int height, Color color) {
54  return ::GenImageColor(width, height, color);
55  }
56 
57  static Image GenGradientV(int width, int height, Color top, Color bottom) {
58  return ::GenImageGradientV(width, height, top, bottom);
59  }
60 
61  static Image GenGradientH(int width, int height, Color left, Color right) {
62  return ::GenImageGradientH(width, height, left, right);
63  }
64 
65  static Image GenGradientRadial(int width, int height, float density, Color inner, Color outer) {
66  return ::GenImageGradientRadial(width, height, density, inner, outer);
67  }
68 
69  static Image GenChecked(int width, int height, int checksX, int checksY, Color col1, Color col2) {
70  return ::GenImageChecked(width, height, checksX, checksY, col1, col2);
71  }
72 
73  static Image GenWhiteNoise(int width, int height, float factor) {
74  return ::GenImageWhiteNoise(width, height, factor);
75  }
76 
77  static Image GenPerlinNoise(int width, int height, int offsetX, int offsetY, float scale) {
78  return ::GenImagePerlinNoise(width, height, offsetX, offsetY, scale);
79  }
80 
81  static Image GenCellular(int width, int height, int tileSize) {
82  return ::GenImageCellular(width, height, tileSize);
83  }
84 
85  ~Image() {
86  Unload();
87  };
88 
89  Image& operator=(const ::Image& image) {
90  set(image);
91  return *this;
92  }
93 
94  Image& operator=(const Image& image) {
95  set(image);
96  return *this;
97  }
98 
99  inline void set(::Image image) {
100  data = image.data;
101  width = image.width;
102  height = image.height;
103  mipmaps = image.mipmaps;
104  format = image.format;
105  }
106 
107  void Load(const std::string& fileName) {
108  set(::LoadImage(fileName.c_str()));
109  }
110 
111  void LoadEx(::Color* pixels, int width, int height) {
112  set(::LoadImageEx(pixels, width, height));
113  }
114 
115  void LoadPro(void* data, int width, int height, int format) {
116  set(::LoadImagePro(data, width, height, format));
117  }
118 
119  void LoadRaw(const std::string& fileName, int width, int height, int format, int headerSize) {
120  set(::LoadImageRaw(fileName.c_str(), width, height, format, headerSize));
121  }
122 
123  inline void Unload() {
124  ::UnloadImage(*this);
125  };
126 
127  inline Image& Export(const std::string& fileName) {
128  ::ExportImage(*this, fileName.c_str());
129  return *this;
130  }
131  inline Image& ExportAsCode(const std::string& fileName) {
132  ::ExportImageAsCode(*this, fileName.c_str());
133  return *this;
134  }
135 
136  GETTERSETTER(void*,Data,data)
137  GETTERSETTER(int,Width,width)
138  GETTERSETTER(int,Height,height)
139  GETTERSETTER(int,Mipmaps,mipmaps)
140  GETTERSETTER(int,Format,format)
141 
142  inline Image Copy() {
143  return ::ImageCopy(*this);
144  }
145  inline Image FromImage(::Rectangle rec) {
146  return ::ImageFromImage(*this, rec);
147  }
148 
149  inline Image& ToPOT(Color fillColor) {
150  ::ImageToPOT(this, fillColor);
151  return *this;
152  }
153 
154  inline Image& Format(int newFormat) {
155  ::ImageFormat(this, newFormat);
156  return *this;
157  }
158 
159  inline Image& AlphaMask(Image alphaMask) {
160  ::ImageAlphaMask(this, alphaMask);
161  return *this;
162  }
163 
164  inline Image& AlphaCrop(float threshold) {
165  ::ImageAlphaCrop(this, threshold);
166  return *this;
167  }
168 
170  ::ImageAlphaPremultiply(this);
171  return *this;
172  }
173 
174  inline Image& Crop(::Rectangle crop) {
175  ::ImageCrop(this, crop);
176  return *this;
177  }
178  inline Image& Resize(int newWidth, int newHeight) {
179  ::ImageResize(this, newWidth, newHeight);
180  return *this;
181  }
182  inline Image& ResizeNN(int newWidth, int newHeight) {
183  ::ImageResizeNN(this, newWidth, newHeight);
184  return *this;
185  }
186  inline Image& ResizeCanvas(int newWidth, int newHeight, int offsetX, int offsetY, Color color) {
187  ::ImageResizeCanvas(this, newWidth, newHeight, offsetX, offsetY, color);
188  return *this;
189  }
190  inline Image& Mipmaps() {
191  ::ImageMipmaps(this);
192  return *this;
193  }
194  inline Image& Dither(int rBpp, int gBpp, int bBpp, int aBpp) {
195  ::ImageDither(this, rBpp, gBpp, bBpp, aBpp);
196  return *this;
197  }
198 
199  inline Image& FlipVertical() {
200  ::ImageFlipVertical(this);
201  return *this;
202  }
203  inline Image& FlipHorizontal() {
204  ::ImageFlipHorizontal(this);
205  return *this;
206  }
207 
208  inline Image& RotateCW() {
209  ::ImageRotateCW(this);
210  return *this;
211  }
212  inline Image& RotateCCW() {
213  ::ImageRotateCCW(this);
214  return *this;
215  }
216  inline Image& ColorTint(::Color color = WHITE) {
217  ::ImageColorTint(this, color);
218  return *this;
219  }
220  inline Image& ColorInvert() {
221  ::ImageColorInvert(this);
222  return *this;
223  }
224  inline Image& ColorGrayscale() {
225  ::ImageColorGrayscale(this);
226  return *this;
227  }
228  inline Image& ColorContrast(float contrast) {
229  ::ImageColorContrast(this, contrast);
230  return *this;
231  }
232  inline Image& ColorBrightness(int brightness) {
233  ::ImageColorBrightness(this, brightness);
234  return *this;
235  }
236  inline Image& ColorReplace(::Color color, ::Color replace) {
237  ::ImageColorReplace(this, color, replace);
238  return *this;
239  }
240 
241  inline ::Color* ExtractPalette(int maxPaletteSize, int *extractCount) {
242  return ::ImageExtractPalette(*this, maxPaletteSize, extractCount);
243  }
244 
245  inline Rectangle GetAlphaBorder(float threshold) {
246  return ::GetImageAlphaBorder(*this, threshold);
247  }
248 
249  inline Image& ClearBackground(::Color color = WHITE) {
250  ::ImageClearBackground(this, color);
251  return *this;
252  }
253 
254  inline Image& DrawPixel(int posX, int posY, ::Color color) {
255  ::ImageDrawPixel(this, posX, posY, color);
256  return *this;
257  }
258 
259  inline Image& DrawPixel(::Vector2 position, ::Color color) {
260  ::ImageDrawPixelV(this, position, color);
261  return *this;
262  }
263 
264  inline Image& DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, ::Color color) {
265  ::ImageDrawLine(this, startPosX, startPosY, endPosX, endPosY, color);
266  return *this;
267  }
268 
269  inline Image& DrawLine(::Vector2 start, ::Vector2 end, ::Color color) {
270  ::ImageDrawLineV(this, start, end, color);
271  return *this;
272  }
273 
274  inline Image& DrawCircle(int centerX, int centerY, int radius, ::Color color) {
275  ::ImageDrawCircle(this, centerX, centerY, radius, color);
276  return *this;
277  }
278 
279  inline Image& DrawCircle(::Vector2 center, int radius, ::Color color) {
280  ::ImageDrawCircleV(this, center, radius, color);
281  return *this;
282  }
283  inline Image& DrawRectangle(int posX, int posY, int width, int height, ::Color color = WHITE) {
284  ::ImageDrawRectangle(this, posX, posY, width, height, color);
285  return *this;
286  }
287  inline Image& DrawRectangle(Vector2 position, Vector2 size, ::Color color = WHITE) {
288  ::ImageDrawRectangleV(this, position, size, color);
289  return *this;
290  }
291  inline Image& DrawRectangle(::Rectangle rec, ::Color color = WHITE) {
292  ::ImageDrawRectangleRec(this, rec, color);
293  return *this;
294  }
295  inline Image& DrawRectangleLines(::Rectangle rec, int thick, ::Color color) {
296  ::ImageDrawRectangleLines(this, rec, thick, color);
297  return *this;
298  }
299  inline Image& Draw(::Image& src, ::Rectangle srcRec, ::Rectangle dstRec, ::Color tint = WHITE) {
300  ::ImageDraw(this, src, srcRec, dstRec, tint);
301  return *this;
302  }
303 
304  inline Image& DrawText(::Vector2 position, const std::string& text, int fontSize, ::Color color = WHITE){
305  ::ImageDrawText(this, position, text.c_str(), fontSize, color);
306  return *this;
307  }
308  inline Image& DrawText(::Vector2 position, ::Font font, const std::string& text, float fontSize, float spacing, ::Color color = WHITE){
309  ::ImageDrawTextEx(this, position, font, text.c_str(), fontSize, spacing, color);
310  return *this;
311  }
312 
313  inline ::Color* GetImageData() {
314  return ::GetImageData(*this);
315  }
316 
317  inline ::Vector4* GetImageDataNormalized() {
318  return ::GetImageDataNormalized(*this);
319  }
320  inline operator Texture2D() {
321  return ::LoadTextureFromImage(*this);
322  }
323  };
324 }
325 
326 #endif
Image & DrawPixel(::Vector2 position, ::Color color)
Definition: Image.hpp:259
Image & ColorContrast(float contrast)
Definition: Image.hpp:228
Image(const std::string &fileName, int width, int height, int format, int headerSize)
Definition: Image.hpp:31
Image & operator=(const ::Image &image)
Definition: Image.hpp:89
Image & ColorTint(::Color color=WHITE)
Definition: Image.hpp:216
static Image GenGradientRadial(int width, int height, float density, Color inner, Color outer)
Definition: Image.hpp:65
static Image GetScreenData()
Definition: Image.hpp:49
static Image Text(std::string text, int fontSize, Color color)
Definition: Image.hpp:41
Image Copy()
Definition: Image.hpp:142
static Image GenPerlinNoise(int width, int height, int offsetX, int offsetY, float scale)
Definition: Image.hpp:77
Image & ResizeCanvas(int newWidth, int newHeight, int offsetX, int offsetY, Color color)
Definition: Image.hpp:186
void Load(const std::string &fileName)
Definition: Image.hpp:107
Image & operator=(const Image &image)
Definition: Image.hpp:94
Image & DrawText(::Vector2 position, ::Font font, const std::string &text, float fontSize, float spacing, ::Color color=WHITE)
Definition: Image.hpp:308
Image & DrawCircle(int centerX, int centerY, int radius, ::Color color)
Definition: Image.hpp:274
static Image Text(Font font, std::string text, float fontSize, float spacing, Color tint)
Definition: Image.hpp:45
Image & Resize(int newWidth, int newHeight)
Definition: Image.hpp:178
Image & DrawLine(::Vector2 start, ::Vector2 end, ::Color color)
Definition: Image.hpp:269
Image & DrawRectangle(::Rectangle rec, ::Color color=WHITE)
Definition: Image.hpp:291
Image & ColorBrightness(int brightness)
Definition: Image.hpp:232
Image & AlphaMask(Image alphaMask)
Definition: Image.hpp:159
Image & Export(const std::string &fileName)
Definition: Image.hpp:127
static Image GenChecked(int width, int height, int checksX, int checksY, Color col1, Color col2)
Definition: Image.hpp:69
Image(::Color *pixels, int width, int height)
Definition: Image.hpp:25
Image & FlipHorizontal()
Definition: Image.hpp:203
Rectangle GetAlphaBorder(float threshold)
Definition: Image.hpp:245
Image & AlphaCrop(float threshold)
Definition: Image.hpp:164
Image & Draw(::Image &src, ::Rectangle srcRec, ::Rectangle dstRec, ::Color tint=WHITE)
Definition: Image.hpp:299
static Image GenCellular(int width, int height, int tileSize)
Definition: Image.hpp:81
Image & ResizeNN(int newWidth, int newHeight)
Definition: Image.hpp:182
void Unload()
Definition: Image.hpp:123
Image(int width, int height, Color color=raylib::Color::RayWhite)
Definition: Image.hpp:37
Image & RotateCW()
Definition: Image.hpp:208
static Image GenGradientV(int width, int height, Color top, Color bottom)
Definition: Image.hpp:57
Image(::Texture2D texture)
Definition: Image.hpp:34
static Image GenColor(int width, int height, Color color)
Definition: Image.hpp:53
Image & ColorReplace(::Color color, ::Color replace)
Definition: Image.hpp:236
Image FromImage(::Rectangle rec)
Definition: Image.hpp:145
Image & ToPOT(Color fillColor)
Definition: Image.hpp:149
Image(void *data, int width, int height, int format)
Definition: Image.hpp:28
Image & DrawRectangle(Vector2 position, Vector2 size, ::Color color=WHITE)
Definition: Image.hpp:287
Image & Dither(int rBpp, int gBpp, int bBpp, int aBpp)
Definition: Image.hpp:194
Image & ExportAsCode(const std::string &fileName)
Definition: Image.hpp:131
inline ::Color * ExtractPalette(int maxPaletteSize, int *extractCount)
Definition: Image.hpp:241
static Color RayWhite
Definition: Color.hpp:87
Image(::Image image)
Definition: Image.hpp:19
static Image GenGradientH(int width, int height, Color left, Color right)
Definition: Image.hpp:61
Image & ColorGrayscale()
Definition: Image.hpp:224
void LoadEx(::Color *pixels, int width, int height)
Definition: Image.hpp:111
inline ::Vector4 * GetImageDataNormalized()
Definition: Image.hpp:317
void LoadPro(void *data, int width, int height, int format)
Definition: Image.hpp:115
Image & FlipVertical()
Definition: Image.hpp:199
static Image GenWhiteNoise(int width, int height, float factor)
Definition: Image.hpp:73
Provides all the classes associated with raylib-cpp.
Definition: AudioDevice.hpp:14
inline ::Color * GetImageData()
Definition: Image.hpp:313
Image & DrawText(::Vector2 position, const std::string &text, int fontSize, ::Color color=WHITE)
Definition: Image.hpp:304
Image & Format(int newFormat)
Definition: Image.hpp:154
Image & RotateCCW()
Definition: Image.hpp:212
void LoadRaw(const std::string &fileName, int width, int height, int format, int headerSize)
Definition: Image.hpp:119
Image & ColorInvert()
Definition: Image.hpp:220
Image & DrawRectangle(int posX, int posY, int width, int height, ::Color color=WHITE)
Definition: Image.hpp:283
Image & Crop(::Rectangle crop)
Definition: Image.hpp:174
Image(const std::string &fileName)
Definition: Image.hpp:22
Image & DrawRectangleLines(::Rectangle rec, int thick, ::Color color)
Definition: Image.hpp:295
Image & Mipmaps()
Definition: Image.hpp:190
Image & DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, ::Color color)
Definition: Image.hpp:264
Image & AlphaPremultiply()
Definition: Image.hpp:169
Image & DrawPixel(int posX, int posY, ::Color color)
Definition: Image.hpp:254
Image & ClearBackground(::Color color=WHITE)
Definition: Image.hpp:249
Image & DrawCircle(::Vector2 center, int radius, ::Color color)
Definition: Image.hpp:279