raylib-cpp
C++ object-oriented wrapper library for raylib.
Image.hpp
1 #ifndef RAYLIB_CPP_INCLUDE_IMAGE_HPP_
2 #define RAYLIB_CPP_INCLUDE_IMAGE_HPP_
3 
4 #include <string>
5 
6 #include "./raylib.hpp"
7 #include "./raylib-cpp-utils.hpp"
8 
9 namespace raylib {
15 class Image : public ::Image {
16  public:
17  Image(const ::Image& image) {
18  set(image);
19  }
20 
21  Image(const std::string& fileName) {
22  Load(fileName);
23  }
24 
25  Image(const std::string& fileName, int width, int height, int format, int headerSize) {
26  LoadRaw(fileName, width, height, format, headerSize);
27  }
28 
29  Image(const std::string& fileName, int* frames) {
30  LoadAnim(fileName, frames);
31  }
32 
33  Image(const std::string& fileType, const unsigned char* fileData, int dataSize) {
34  LoadFromMemory(fileType, fileData, dataSize);
35  }
36 
37  Image(const ::Texture2D& texture) {
38  set(::GetTextureData(texture));
39  }
40 
41  Image(int width, int height, ::Color color = {255, 255, 255, 255}) {
42  set(::GenImageColor(width, height, color));
43  }
44 
45  Image(const ::Font& font, const std::string& text, float fontSize, float spacing,
46  ::Color tint = {255, 255, 255, 255}) {
47  set(::ImageTextEx(font, text.c_str(), fontSize, spacing, tint));
48  }
49 
50  static ::Image Text(const std::string& text, int fontSize,
51  ::Color color = {255, 255, 255, 255}) {
52  return ::ImageText(text.c_str(), fontSize, color);
53  }
54 
55  static ::Image Text(const ::Font& font, const std::string& text, float fontSize, float spacing,
56  ::Color tint = {255, 255, 255, 255}) {
57  return ::ImageTextEx(font, text.c_str(), fontSize, spacing, tint);
58  }
59 
63  static ::Image GetScreenData() {
64  return ::GetScreenData();
65  }
66 
70  static ::Image Color(int width, int height, ::Color color = {255, 255, 255, 255}) {
71  return ::GenImageColor(width, height, color);
72  }
73 
77  static ::Image GradientV(int width, int height, ::Color top, ::Color bottom) {
78  return ::GenImageGradientV(width, height, top, bottom);
79  }
80 
84  static ::Image GradientH(int width, int height, ::Color left, ::Color right) {
85  return ::GenImageGradientH(width, height, left, right);
86  }
87 
91  static ::Image GradientRadial(int width, int height, float density,
92  ::Color inner, ::Color outer) {
93  return ::GenImageGradientRadial(width, height, density, inner, outer);
94  }
95 
99  static ::Image Checked(int width, int height, int checksX, int checksY,
100  ::Color col1 = {255, 255, 255, 255}, ::Color col2 = {0, 0, 0, 255}) {
101  return ::GenImageChecked(width, height, checksX, checksY, col1, col2);
102  }
103 
107  static ::Image WhiteNoise(int width, int height, float factor) {
108  return ::GenImageWhiteNoise(width, height, factor);
109  }
110 
114  static ::Image PerlinNoise(int width, int height, int offsetX, int offsetY,
115  float scale = 1.0f) {
116  return ::GenImagePerlinNoise(width, height, offsetX, offsetY, scale);
117  }
118 
122  static ::Image Cellular(int width, int height, int tileSize) {
123  return ::GenImageCellular(width, height, tileSize);
124  }
125 
126  ~Image() {
127  Unload();
128  }
129 
130  Image& operator=(const ::Image& image) {
131  set(image);
132  return *this;
133  }
134 
138  void Load(const std::string& fileName) {
139  set(::LoadImage(fileName.c_str()));
140  }
141 
145  void LoadRaw(const std::string& fileName, int width, int height, int format, int headerSize) {
146  set(::LoadImageRaw(fileName.c_str(), width, height, format, headerSize));
147  }
148 
152  void LoadAnim(const std::string& fileName, int* frames) {
153  set(::LoadImageAnim(fileName.c_str(), frames));
154  }
155 
160  const std::string& fileType,
161  const unsigned char *fileData,
162  int dataSize) {
163  set(::LoadImageFromMemory(fileType.c_str(), fileData, dataSize));
164  }
165 
169  inline void Unload() {
170  if (data != NULL) {
171  ::UnloadImage(*this);
172  data = NULL;
173  }
174  }
175 
179  inline bool Export(const std::string& fileName) {
180  // TODO(RobLoach): Switch to an invalid loading exception on false.
181  return ::ExportImage(*this, fileName.c_str());
182  }
183 
187  inline bool ExportAsCode(const std::string& fileName) {
188  return ::ExportImageAsCode(*this, fileName.c_str());
189  }
190 
191  GETTERSETTER(void*, Data, data)
192  GETTERSETTER(int, Width, width)
193  GETTERSETTER(int, Height, height)
194  GETTERSETTER(int, Mipmaps, mipmaps)
195  GETTERSETTER(int, Format, format)
196 
200  inline ::Vector2 GetSize() {
201  return {static_cast<float>(width), static_cast<float>(height)};
202  }
203 
207  inline ::Image Copy() {
208  return ::ImageCopy(*this);
209  }
210 
214  inline ::Image FromImage(::Rectangle rec) {
215  return ::ImageFromImage(*this, rec);
216  }
217 
221  inline Image& ToPOT(::Color fillColor) {
222  ::ImageToPOT(this, fillColor);
223  return *this;
224  }
225 
229  inline Image& Format(int newFormat) {
230  ::ImageFormat(this, newFormat);
231  return *this;
232  }
233 
237  inline Image& AlphaMask(const ::Image& alphaMask) {
238  ::ImageAlphaMask(this, alphaMask);
239  return *this;
240  }
241 
245  inline Image& AlphaCrop(float threshold) {
246  ::ImageAlphaCrop(this, threshold);
247  return *this;
248  }
249 
254  ::ImageAlphaPremultiply(this);
255  return *this;
256  }
257 
261  inline Image& Crop(::Rectangle crop) {
262  ::ImageCrop(this, crop);
263  return *this;
264  }
265 
269  inline Image& Crop(::Vector2 size) {
270  return Crop(0, 0, static_cast<int>(size.x), static_cast<int>(size.y));
271  }
272 
276  inline Image& Crop(int offsetX, int offsetY, int newWidth, int newHeight) {
277  ::Rectangle rect{
278  static_cast<float>(offsetX),
279  static_cast<float>(offsetY),
280  static_cast<float>(newWidth),
281  static_cast<float>(newHeight)
282  };
283  ::ImageCrop(this, rect);
284  return *this;
285  }
286 
290  inline Image& Resize(int newWidth, int newHeight) {
291  ::ImageResize(this, newWidth, newHeight);
292  return *this;
293  }
294 
298  inline Image& ResizeNN(int newWidth, int newHeight) {
299  ::ImageResizeNN(this, newWidth, newHeight);
300  return *this;
301  }
302 
306  inline Image& ResizeCanvas(int newWidth, int newHeight, int offsetX, int offsetY,
307  ::Color color = {255, 255, 255, 255}) {
308  ::ImageResizeCanvas(this, newWidth, newHeight, offsetX, offsetY, color);
309  return *this;
310  }
311 
315  inline Image& Mipmaps() {
316  ::ImageMipmaps(this);
317  return *this;
318  }
319 
323  inline Image& Dither(int rBpp, int gBpp, int bBpp, int aBpp) {
324  ::ImageDither(this, rBpp, gBpp, bBpp, aBpp);
325  return *this;
326  }
327 
331  inline Image& FlipVertical() {
332  ::ImageFlipVertical(this);
333  return *this;
334  }
335 
339  inline Image& FlipHorizontal() {
340  ::ImageFlipHorizontal(this);
341  return *this;
342  }
343 
347  inline Image& RotateCW() {
348  ::ImageRotateCW(this);
349  return *this;
350  }
351 
355  inline Image& RotateCCW() {
356  ::ImageRotateCCW(this);
357  return *this;
358  }
359 
363  inline Image& ColorTint(::Color color = {255, 255, 255, 255}) {
364  ::ImageColorTint(this, color);
365  return *this;
366  }
367 
371  inline Image& ColorInvert() {
372  ::ImageColorInvert(this);
373  return *this;
374  }
375 
379  inline Image& ColorGrayscale() {
380  ::ImageColorGrayscale(this);
381  return *this;
382  }
383 
389  inline Image& ColorContrast(float contrast) {
390  ::ImageColorContrast(this, contrast);
391  return *this;
392  }
393 
399  inline Image& ColorBrightness(int brightness) {
400  ::ImageColorBrightness(this, brightness);
401  return *this;
402  }
403 
407  inline Image& ColorReplace(::Color color, ::Color replace) {
408  ::ImageColorReplace(this, color, replace);
409  return *this;
410  }
411 
417  inline Rectangle GetAlphaBorder(float threshold) const {
418  return ::GetImageAlphaBorder(*this, threshold);
419  }
420 
424  inline Image& ClearBackground(::Color color = {0, 0, 0, 255}) {
425  ::ImageClearBackground(this, color);
426  return *this;
427  }
428 
432  inline Image& DrawPixel(int posX, int posY, ::Color color = {255, 255, 255, 255}) {
433  ::ImageDrawPixel(this, posX, posY, color);
434  return *this;
435  }
436 
437  inline Image& DrawPixel(::Vector2 position, ::Color color = {255, 255, 255, 255}) {
438  ::ImageDrawPixelV(this, position, color);
439  return *this;
440  }
441 
442  inline Image& DrawLine(int startPosX, int startPosY, int endPosX, int endPosY,
443  ::Color color = {255, 255, 255, 255}) {
444  ::ImageDrawLine(this, startPosX, startPosY, endPosX, endPosY, color);
445  return *this;
446  }
447 
448  inline Image& DrawLine(::Vector2 start, ::Vector2 end, ::Color color = {255, 255, 255, 255}) {
449  ::ImageDrawLineV(this, start, end, color);
450  return *this;
451  }
452 
453  inline Image& DrawCircle(int centerX, int centerY, int radius,
454  ::Color color = {255, 255, 255, 255}) {
455  ::ImageDrawCircle(this, centerX, centerY, radius, color);
456  return *this;
457  }
458 
459  inline Image& DrawCircle(::Vector2 center, int radius,
460  ::Color color = {255, 255, 255, 255}) {
461  ::ImageDrawCircleV(this, center, radius, color);
462  return *this;
463  }
464 
465  inline Image& DrawRectangle(int posX, int posY, int width, int height,
466  ::Color color = {255, 255, 255, 255}) {
467  ::ImageDrawRectangle(this, posX, posY, width, height, color);
468  return *this;
469  }
470 
471  inline Image& DrawRectangle(Vector2 position, Vector2 size,
472  ::Color color = {255, 255, 255, 255}) {
473  ::ImageDrawRectangleV(this, position, size, color);
474  return *this;
475  }
476 
477  inline Image& DrawRectangle(::Rectangle rec, ::Color color = {255, 255, 255, 255}) {
478  ::ImageDrawRectangleRec(this, rec, color);
479  return *this;
480  }
481 
482  inline Image& DrawRectangleLines(::Rectangle rec, int thick = 1,
483  ::Color color = {255, 255, 255, 255}) {
484  ::ImageDrawRectangleLines(this, rec, thick, color);
485  return *this;
486  }
487 
488  inline Image& Draw(const ::Image& src, ::Rectangle srcRec, ::Rectangle dstRec,
489  ::Color tint = {255, 255, 255, 255}) {
490  ::ImageDraw(this, src, srcRec, dstRec, tint);
491  return *this;
492  }
493 
494  inline Image& DrawText(const std::string& text, ::Vector2 position, int fontSize,
495  ::Color color = {255, 255, 255, 255}) {
496  ::ImageDrawText(this,
497  text.c_str(),
498  static_cast<int>(position.x),
499  static_cast<int>(position.y),
500  fontSize,
501  color);
502  return *this;
503  }
504 
505  inline Image& DrawText(const std::string& text, int x, int y, int fontSize,
506  ::Color color = {255, 255, 255, 255}) {
507  ::ImageDrawText(this, text.c_str(), x, y, fontSize, color);
508  return *this;
509  }
510 
511  inline Image& DrawText(const ::Font& font, const std::string& text, ::Vector2 position,
512  float fontSize, float spacing, ::Color tint = {255, 255, 255, 255}) {
513  ::ImageDrawTextEx(this, font, text.c_str(), position, fontSize, spacing, tint);
514  return *this;
515  }
516 
520  inline ::Color* LoadColors() {
521  return ::LoadImageColors(*this);
522  }
523 
527  inline ::Color* LoadPalette(int maxPaletteSize, int *colorsCount) {
528  return ::LoadImagePalette(*this, maxPaletteSize, colorsCount);
529  }
530 
534  inline void UnloadColors(::Color* colors) {
535  ::UnloadImageColors(colors);
536  }
537 
541  inline void UnloadPalette(::Color* colors) {
542  ::UnloadImagePalette(colors);
543  }
544 
548  inline ::Texture2D LoadTexture() {
549  return ::LoadTextureFromImage(*this);
550  }
551 
555  inline operator ::Texture2D() {
556  return LoadTexture();
557  }
558 
562  static int GetPixelDataSize(int width, int height, int format = PIXELFORMAT_UNCOMPRESSED_R32G32B32A32) {
563  return ::GetPixelDataSize(width, height, format);
564  }
565 
572  return ::GetPixelDataSize(width, height, format);
573  }
574 
575  private:
576  inline void set(const ::Image& image) {
577  data = image.data;
578  width = image.width;
579  height = image.height;
580  mipmaps = image.mipmaps;
581  format = image.format;
582  }
583 };
584 } // namespace raylib
585 
586 #endif // RAYLIB_CPP_INCLUDE_IMAGE_HPP_
raylib::Image::Dither
Image & Dither(int rBpp, int gBpp, int bBpp, int aBpp)
Dither image data to 16bpp or lower (Floyd-Steinberg dithering)
Definition: Image.hpp:323
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:7
raylib::Image::LoadPalette
inline ::Color * LoadPalette(int maxPaletteSize, int *colorsCount)
Load colors palette from image as a Color array (RGBA - 32bit)
Definition: Image.hpp:527
raylib::Image::ColorBrightness
Image & ColorBrightness(int brightness)
Modify image color: brightness.
Definition: Image.hpp:399
raylib::Image::WhiteNoise
::Image WhiteNoise(int width, int height, float factor)
Generate image: white noise.
Definition: Image.hpp:107
raylib::LoadImage
static inline ::Image LoadImage(const std::string &fileName)
Load an image.
Definition: Functions.hpp:204
raylib::LoadImageRaw
static inline ::Image LoadImageRaw(const std::string &fileName, int width, int height, int format, int headerSize)
Load an image from RAW file data.
Definition: Functions.hpp:211
raylib::Image::GradientRadial
::Image GradientRadial(int width, int height, float density, ::Color inner, ::Color outer)
Generate image: radial gradient.
Definition: Image.hpp:91
raylib::Image::ToPOT
Image & ToPOT(::Color fillColor)
Convert image to POT (power-of-two)
Definition: Image.hpp:221
raylib::LoadImageFromMemory
static inline ::Image LoadImageFromMemory(const std::string &fileType, const unsigned char *fileData, int dataSize)
Load image from memory buffer, fileType refers to extension like "png".
Definition: Functions.hpp:227
raylib::Image::Format
Image & Format(int newFormat)
Convert image data to desired format.
Definition: Image.hpp:229
raylib::Image::Copy
inline ::Image Copy()
Create an image duplicate (useful for transformations)
Definition: Image.hpp:207
raylib::Image::Crop
Image & Crop(::Vector2 size)
Crop an image to a new given width and height based on a vector.
Definition: Image.hpp:269
raylib::Image::ColorInvert
Image & ColorInvert()
Modify image color: invert.
Definition: Image.hpp:371
raylib::Image::ColorGrayscale
Image & ColorGrayscale()
Modify image color: grayscale.
Definition: Image.hpp:379
raylib::Image
Image type, bpp always RGBA (32bit)
Definition: Image.hpp:15
raylib::Image::Crop
Image & Crop(::Rectangle crop)
Crop an image to area defined by a rectangle.
Definition: Image.hpp:261
raylib::LoadImageAnim
static inline ::Image LoadImageAnim(const std::string &fileName, int *frames)
Load animated image data.
Definition: Functions.hpp:220
raylib::Image::UnloadPalette
void UnloadPalette(::Color *colors)
Unload colors palette loaded with LoadImagePalette()
Definition: Image.hpp:541
raylib::Image::GetSize
inline ::Vector2 GetSize()
Retrieve the width and height of the image.
Definition: Image.hpp:200
raylib::Image::RotateCCW
Image & RotateCCW()
Rotate image counter-clockwise 90deg.
Definition: Image.hpp:355
raylib::Image::ExportAsCode
bool ExportAsCode(const std::string &fileName)
Export image as code file defining an array of bytes, returns true on success.
Definition: Image.hpp:187
raylib::Image::FlipVertical
Image & FlipVertical()
Flip image vertically.
Definition: Image.hpp:331
raylib::Image::ColorContrast
Image & ColorContrast(float contrast)
Modify image color: contrast.
Definition: Image.hpp:389
raylib::Image::LoadColors
inline ::Color * LoadColors()
Load color data from image as a Color array (RGBA - 32bit)
Definition: Image.hpp:520
raylib::Image::GetScreenData
::Image GetScreenData()
Get pixel data from screen buffer and return an Image (screenshot)
Definition: Image.hpp:63
raylib::Image::PerlinNoise
::Image PerlinNoise(int width, int height, int offsetX, int offsetY, float scale=1.0f)
Generate image: perlin noise.
Definition: Image.hpp:114
raylib::Image::LoadRaw
void LoadRaw(const std::string &fileName, int width, int height, int format, int headerSize)
Load image from RAW file data.
Definition: Image.hpp:145
raylib::Image::AlphaPremultiply
Image & AlphaPremultiply()
Premultiply alpha channel.
Definition: Image.hpp:253
raylib::Image::Crop
Image & Crop(int offsetX, int offsetY, int newWidth, int newHeight)
Crop an image to area defined by a rectangle.
Definition: Image.hpp:276
raylib::Image::LoadFromMemory
void LoadFromMemory(const std::string &fileType, const unsigned char *fileData, int dataSize)
Load image from memory buffer, fileType refers to extension: i.e.
Definition: Image.hpp:159
raylib::Image::ResizeCanvas
Image & ResizeCanvas(int newWidth, int newHeight, int offsetX, int offsetY, ::Color color={255, 255, 255, 255})
Resize canvas and fill with color.
Definition: Image.hpp:306
raylib::Image::DrawPixel
Image & DrawPixel(int posX, int posY, ::Color color={255, 255, 255, 255})
Draw pixel within an image.
Definition: Image.hpp:432
raylib::Image::Mipmaps
Image & Mipmaps()
Generate all mipmap levels for a provided image.
Definition: Image.hpp:315
raylib::Image::Export
bool Export(const std::string &fileName)
Export image data to file, returns true on success.
Definition: Image.hpp:179
raylib::Rectangle
Rectangle type.
Definition: Rectangle.hpp:12
raylib::Vector2
Vector2 type.
Definition: Vector2.hpp:16
raylib::Image::GradientV
::Image GradientV(int width, int height, ::Color top, ::Color bottom)
Generate image: vertical gradient.
Definition: Image.hpp:77
raylib::Image::UnloadColors
void UnloadColors(::Color *colors)
Unload color data loaded with LoadImageColors()
Definition: Image.hpp:534
raylib::Image::ColorTint
Image & ColorTint(::Color color={255, 255, 255, 255})
Modify image color: tint.
Definition: Image.hpp:363
raylib::Image::GetAlphaBorder
Rectangle GetAlphaBorder(float threshold) const
Get image alpha border rectangle.
Definition: Image.hpp:417
raylib::Color
Color type, RGBA (32bit)
Definition: Color.hpp:14
raylib::Image::GradientH
::Image GradientH(int width, int height, ::Color left, ::Color right)
Generate image: horizontal gradient.
Definition: Image.hpp:84
raylib::Image::GetPixelDataSize
int GetPixelDataSize()
Returns the pixel data size of the image.
Definition: Image.hpp:571
raylib::Image::LoadAnim
void LoadAnim(const std::string &fileName, int *frames)
Load image sequence from file (frames appended to image.data).
Definition: Image.hpp:152
raylib::Image::Load
void Load(const std::string &fileName)
Load image from file into CPU memory (RAM)
Definition: Image.hpp:138
raylib::Image::Color
::Image Color(int width, int height, ::Color color={255, 255, 255, 255})
Generate image: plain color.
Definition: Image.hpp:70
raylib::Image::AlphaCrop
Image & AlphaCrop(float threshold)
Crop image depending on alpha value.
Definition: Image.hpp:245
raylib::Image::Resize
Image & Resize(int newWidth, int newHeight)
Resize and image to new size.
Definition: Image.hpp:290
raylib::Image::Checked
::Image Checked(int width, int height, int checksX, int checksY, ::Color col1={255, 255, 255, 255}, ::Color col2={0, 0, 0, 255})
Generate image: checked.
Definition: Image.hpp:99
raylib::Image::GetPixelDataSize
static int GetPixelDataSize(int width, int height, int format=PIXELFORMAT_UNCOMPRESSED_R32G32B32A32)
Get pixel data size in bytes for certain format.
Definition: Image.hpp:562
raylib::Image::Unload
void Unload()
Unload image from CPU memory (RAM)
Definition: Image.hpp:169
raylib::Image::FlipHorizontal
Image & FlipHorizontal()
Flip image horizontally.
Definition: Image.hpp:339
raylib::Image::FromImage
inline ::Image FromImage(::Rectangle rec)
Create an image from another image piece.
Definition: Image.hpp:214
raylib::Image::LoadTexture
inline ::Texture2D LoadTexture()
Load texture from image data.
Definition: Image.hpp:548
raylib::Image::ResizeNN
Image & ResizeNN(int newWidth, int newHeight)
Resize and image to new size using Nearest-Neighbor scaling algorithm.
Definition: Image.hpp:298
raylib::Image::Cellular
::Image Cellular(int width, int height, int tileSize)
Generate image: cellular algorithm.
Definition: Image.hpp:122
raylib::Image::ClearBackground
Image & ClearBackground(::Color color={0, 0, 0, 255})
Clear image background with given color.
Definition: Image.hpp:424
raylib::Image::RotateCW
Image & RotateCW()
Rotate image clockwise 90deg.
Definition: Image.hpp:347
raylib::Image::AlphaMask
Image & AlphaMask(const ::Image &alphaMask)
Apply alpha mask to image.
Definition: Image.hpp:237
raylib::Image::ColorReplace
Image & ColorReplace(::Color color, ::Color replace)
Modify image color: replace color.
Definition: Image.hpp:407