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 #include "./RaylibException.hpp"
9 #include "./Color.hpp"
10 
11 namespace raylib {
17 class Image : public ::Image {
18  public:
19  Image(void* data = nullptr,
20  int width = 0,
21  int height = 0,
22  int mipmaps = 1,
23  int format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8) : ::Image{data, width, height, mipmaps, format} {
24  // Nothing.
25  }
26 
27  Image(const ::Image& image) {
28  set(image);
29  }
30 
38  Image(const std::string& fileName) {
39  Load(fileName);
40  }
41 
49  Image(const std::string& fileName, int width, int height, int format, int headerSize = 0) {
50  Load(fileName, width, height, format, headerSize);
51  }
52 
60  Image(const std::string& fileName, int* frames) {
61  Load(fileName, frames);
62  }
63 
69  Image(const std::string& fileType, const unsigned char* fileData, int dataSize) {
70  Load(fileType, fileData, dataSize);
71  }
72 
78  Image(const ::Texture2D& texture) {
79  Load(texture);
80  }
81 
82  Image(int width, int height, ::Color color = {255, 255, 255, 255}) {
83  set(::GenImageColor(width, height, color));
84  }
85 
86  Image(const std::string& text, int fontSize, ::Color color = {255, 255, 255, 255}) {
87  set(::ImageText(text.c_str(), fontSize, color));
88  }
89 
90  Image(const ::Font& font, const std::string& text, float fontSize, float spacing,
91  ::Color tint = {255, 255, 255, 255}) {
92  set(::ImageTextEx(font, text.c_str(), fontSize, spacing, tint));
93  }
94 
95  Image(const Image& other) {
96  set(other.Copy());
97  }
98 
99  Image(Image&& other) {
100  set(other);
101 
102  other.data = nullptr;
103  other.width = 0;
104  other.height = 0;
105  other.mipmaps = 0;
106  other.format = 0;
107  }
108 
109  static ::Image Text(const std::string& text, int fontSize,
110  ::Color color = {255, 255, 255, 255}) {
111  return ::ImageText(text.c_str(), fontSize, color);
112  }
113 
114  static ::Image Text(const ::Font& font, const std::string& text, float fontSize, float spacing,
115  ::Color tint = {255, 255, 255, 255}) {
116  return ::ImageTextEx(font, text.c_str(), fontSize, spacing, tint);
117  }
118 
122  static ::Image LoadFromScreen() {
123  return ::LoadImageFromScreen();
124  }
125 
129  static ::Image Color(int width, int height, ::Color color = {255, 255, 255, 255}) {
130  return ::GenImageColor(width, height, color);
131  }
132 
136  static ::Image GradientV(int width, int height, ::Color top, ::Color bottom) {
137  return ::GenImageGradientV(width, height, top, bottom);
138  }
139 
143  static ::Image GradientH(int width, int height, ::Color left, ::Color right) {
144  return ::GenImageGradientH(width, height, left, right);
145  }
146 
150  static ::Image GradientRadial(int width, int height, float density,
151  ::Color inner, ::Color outer) {
152  return ::GenImageGradientRadial(width, height, density, inner, outer);
153  }
154 
158  static ::Image Checked(int width, int height, int checksX, int checksY,
159  ::Color col1 = {255, 255, 255, 255}, ::Color col2 = {0, 0, 0, 255}) {
160  return ::GenImageChecked(width, height, checksX, checksY, col1, col2);
161  }
162 
166  static ::Image WhiteNoise(int width, int height, float factor) {
167  return ::GenImageWhiteNoise(width, height, factor);
168  }
169 
173  static ::Image Cellular(int width, int height, int tileSize) {
174  return ::GenImageCellular(width, height, tileSize);
175  }
176 
177  ~Image() {
178  Unload();
179  }
180 
181  Image& operator=(const ::Image& image) {
182  set(image);
183  return *this;
184  }
185 
186  Image& operator=(const Image& other) {
187  if (this == &other) {
188  return *this;
189  }
190 
191  Unload();
192  set(other.Copy());
193 
194  return *this;
195  }
196 
197  Image& operator=(Image&& other) noexcept {
198  if (this == &other) {
199  return *this;
200  }
201 
202  Unload();
203  set(other);
204 
205  other.data = nullptr;
206  other.width = 0;
207  other.height = 0;
208  other.mipmaps = 0;
209  other.format = 0;
210 
211  return *this;
212  }
213 
221  void Load(const std::string& fileName) {
222  set(::LoadImage(fileName.c_str()));
223  if (!IsReady()) {
224  throw RaylibException("Failed to load Image from file: " + fileName);
225  }
226  }
227 
235  void Load(const std::string& fileName, int width, int height, int format, int headerSize) {
236  set(::LoadImageRaw(fileName.c_str(), width, height, format, headerSize));
237  if (!IsReady()) {
238  throw RaylibException("Failed to load Image from file: " + fileName);
239  }
240  }
241 
249  void Load(const std::string& fileName, int* frames) {
250  set(::LoadImageAnim(fileName.c_str(), frames));
251  if (!IsReady()) {
252  throw RaylibException("Failed to load Image from file: " + fileName);
253  }
254  }
255 
263  void Load(
264  const std::string& fileType,
265  const unsigned char *fileData,
266  int dataSize) {
267  set(::LoadImageFromMemory(fileType.c_str(), fileData, dataSize));
268  if (!IsReady()) {
269  throw RaylibException("Failed to load Image data with file type: " + fileType);
270  }
271  }
272 
280  void Load(const ::Texture2D& texture) {
281  set(::LoadImageFromTexture(texture));
282  if (!IsReady()) {
283  throw RaylibException("Failed to load Image from texture.");
284  }
285  }
286 
290  inline void Unload() {
291  if (data != nullptr) {
292  ::UnloadImage(*this);
293  data = nullptr;
294  }
295  }
296 
302  inline void Export(const std::string& fileName) const {
303  if (!::ExportImage(*this, fileName.c_str())) {
304  throw RaylibException(TextFormat("Failed to export Image to file: %s", fileName.c_str()));
305  }
306  }
307 
313  inline void ExportAsCode(const std::string& fileName) const {
314  if (!::ExportImageAsCode(*this, fileName.c_str())) {
315  throw RaylibException(TextFormat("Failed to export Image code to file: %s", fileName.c_str()));
316  }
317  }
318 
319  GETTERSETTER(void*, Data, data)
320  GETTERSETTER(int, Width, width)
321  GETTERSETTER(int, Height, height)
322  GETTERSETTER(int, Mipmaps, mipmaps)
323  GETTERSETTER(int, Format, format)
324 
328  inline ::Vector2 GetSize() const {
329  return {static_cast<float>(width), static_cast<float>(height)};
330  }
331 
335  inline ::Image Copy() const {
336  return ::ImageCopy(*this);
337  }
338 
342  inline ::Image FromImage(::Rectangle rec) const {
343  return ::ImageFromImage(*this, rec);
344  }
345 
349  inline Image& Format(int newFormat) {
350  ::ImageFormat(this, newFormat);
351  return *this;
352  }
353 
357  inline Image& ToPOT(::Color fillColor) {
358  ::ImageToPOT(this, fillColor);
359  return *this;
360  }
361 
365  inline Image& Crop(::Rectangle crop) {
366  ::ImageCrop(this, crop);
367  return *this;
368  }
369 
373  inline Image& AlphaCrop(float threshold) {
374  ::ImageAlphaCrop(this, threshold);
375  return *this;
376  }
377 
381  inline Image& AlphaClear(::Color color, float threshold) {
382  ::ImageAlphaClear(this, color, threshold);
383  return *this;
384  }
385 
389  inline Image& AlphaMask(const ::Image& alphaMask) {
390  ::ImageAlphaMask(this, alphaMask);
391  return *this;
392  }
393 
398  ::ImageAlphaPremultiply(this);
399  return *this;
400  }
401 
405  inline Image& Crop(int newWidth, int newHeight) {
406  return Crop(0, 0, newWidth, newHeight);
407  }
408 
412  inline Image& Crop(::Vector2 size) {
413  return Crop(0, 0, static_cast<int>(size.x), static_cast<int>(size.y));
414  }
415 
419  inline Image& Crop(int offsetX, int offsetY, int newWidth, int newHeight) {
420  ::Rectangle rect{
421  static_cast<float>(offsetX),
422  static_cast<float>(offsetY),
423  static_cast<float>(newWidth),
424  static_cast<float>(newHeight)
425  };
426  ::ImageCrop(this, rect);
427  return *this;
428  }
429 
433  inline Image& Resize(int newWidth, int newHeight) {
434  ::ImageResize(this, newWidth, newHeight);
435  return *this;
436  }
437 
441  inline Image& ResizeNN(int newWidth, int newHeight) {
442  ::ImageResizeNN(this, newWidth, newHeight);
443  return *this;
444  }
445 
449  inline Image& ResizeCanvas(int newWidth, int newHeight, int offsetX = 0, int offsetY = 0,
450  ::Color color = {255, 255, 255, 255}) {
451  ::ImageResizeCanvas(this, newWidth, newHeight, offsetX, offsetY, color);
452  return *this;
453  }
454 
458  inline Image& Mipmaps() {
459  ::ImageMipmaps(this);
460  return *this;
461  }
462 
466  inline Image& Dither(int rBpp, int gBpp, int bBpp, int aBpp) {
467  ::ImageDither(this, rBpp, gBpp, bBpp, aBpp);
468  return *this;
469  }
470 
474  inline Image& FlipVertical() {
475  ::ImageFlipVertical(this);
476  return *this;
477  }
478 
482  inline Image& FlipHorizontal() {
483  ::ImageFlipHorizontal(this);
484  return *this;
485  }
486 
490  inline Image& RotateCW() {
491  ::ImageRotateCW(this);
492  return *this;
493  }
494 
498  inline Image& RotateCCW() {
499  ::ImageRotateCCW(this);
500  return *this;
501  }
502 
506  inline Image& ColorTint(::Color color = {255, 255, 255, 255}) {
507  ::ImageColorTint(this, color);
508  return *this;
509  }
510 
514  inline Image& ColorInvert() {
515  ::ImageColorInvert(this);
516  return *this;
517  }
518 
522  inline Image& ColorGrayscale() {
523  ::ImageColorGrayscale(this);
524  return *this;
525  }
526 
532  inline Image& ColorContrast(float contrast) {
533  ::ImageColorContrast(this, contrast);
534  return *this;
535  }
536 
542  inline Image& ColorBrightness(int brightness) {
543  ::ImageColorBrightness(this, brightness);
544  return *this;
545  }
546 
550  inline Image& ColorReplace(::Color color, ::Color replace) {
551  ::ImageColorReplace(this, color, replace);
552  return *this;
553  }
554 
560  inline Rectangle GetAlphaBorder(float threshold) const {
561  return ::GetImageAlphaBorder(*this, threshold);
562  }
563 
567  inline raylib::Color GetColor(int x = 0, int y = 0) const {
568  return ::GetImageColor(*this, x, y);
569  }
570 
574  inline raylib::Color GetColor(::Vector2 position) const {
575  return ::GetImageColor(*this, static_cast<int>(position.x), static_cast<int>(position.y));
576  }
577 
581  inline Image& ClearBackground(::Color color = {0, 0, 0, 255}) {
582  ::ImageClearBackground(this, color);
583  return *this;
584  }
585 
589  inline void DrawPixel(int posX, int posY, ::Color color = {255, 255, 255, 255}) {
590  ::ImageDrawPixel(this, posX, posY, color);
591  }
592 
593  inline void DrawPixel(::Vector2 position, ::Color color = {255, 255, 255, 255}) {
594  ::ImageDrawPixelV(this, position, color);
595  }
596 
597  inline void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY,
598  ::Color color = {255, 255, 255, 255}) {
599  ::ImageDrawLine(this, startPosX, startPosY, endPosX, endPosY, color);
600  }
601 
602  inline void DrawLine(::Vector2 start, ::Vector2 end, ::Color color = {255, 255, 255, 255}) {
603  ::ImageDrawLineV(this, start, end, color);
604  }
605 
606  inline void DrawCircle(int centerX, int centerY, int radius,
607  ::Color color = {255, 255, 255, 255}) {
608  ::ImageDrawCircle(this, centerX, centerY, radius, color);
609  }
610 
611  inline void DrawCircle(::Vector2 center, int radius,
612  ::Color color = {255, 255, 255, 255}) {
613  ::ImageDrawCircleV(this, center, radius, color);
614  }
615 
616  inline void DrawRectangle(int posX, int posY, int width, int height,
617  ::Color color = {255, 255, 255, 255}) {
618  ::ImageDrawRectangle(this, posX, posY, width, height, color);
619  }
620 
621  inline void DrawRectangle(Vector2 position, Vector2 size,
622  ::Color color = {255, 255, 255, 255}) {
623  ::ImageDrawRectangleV(this, position, size, color);
624  }
625 
626  inline void DrawRectangle(::Rectangle rec, ::Color color = {255, 255, 255, 255}) {
627  ::ImageDrawRectangleRec(this, rec, color);
628  }
629 
630  inline void DrawRectangleLines(::Rectangle rec, int thick = 1,
631  ::Color color = {255, 255, 255, 255}) {
632  ::ImageDrawRectangleLines(this, rec, thick, color);
633  }
634 
635  inline void Draw(const ::Image& src, ::Rectangle srcRec, ::Rectangle dstRec,
636  ::Color tint = {255, 255, 255, 255}) {
637  ::ImageDraw(this, src, srcRec, dstRec, tint);
638  }
639 
640  inline void DrawText(const std::string& text, ::Vector2 position, int fontSize,
641  ::Color color = {255, 255, 255, 255}) {
642  ::ImageDrawText(this,
643  text.c_str(),
644  static_cast<int>(position.x),
645  static_cast<int>(position.y),
646  fontSize,
647  color);
648  }
649 
650  inline void DrawText(const std::string& text, int x, int y, int fontSize,
651  ::Color color = {255, 255, 255, 255}) {
652  ::ImageDrawText(this, text.c_str(), x, y, fontSize, color);
653  }
654 
655  inline void DrawText(const ::Font& font, const std::string& text, ::Vector2 position,
656  float fontSize, float spacing, ::Color tint = {255, 255, 255, 255}) {
657  ::ImageDrawTextEx(this, font, text.c_str(), position, fontSize, spacing, tint);
658  }
659 
663  inline ::Color* LoadColors() const {
664  return ::LoadImageColors(*this);
665  }
666 
670  inline ::Color* LoadPalette(int maxPaletteSize, int *colorsCount) const {
671  return ::LoadImagePalette(*this, maxPaletteSize, colorsCount);
672  }
673 
677  inline void UnloadColors(::Color* colors) const {
678  ::UnloadImageColors(colors);
679  }
680 
684  inline void UnloadPalette(::Color* colors) const {
685  ::UnloadImagePalette(colors);
686  }
687 
691  inline ::Texture2D LoadTexture() const {
692  return ::LoadTextureFromImage(*this);
693  }
694 
700  inline operator ::Texture2D() {
701  return LoadTexture();
702  }
703 
707  static int GetPixelDataSize(int width, int height, int format = PIXELFORMAT_UNCOMPRESSED_R32G32B32A32) {
708  return ::GetPixelDataSize(width, height, format);
709  }
710 
716  int GetPixelDataSize() const {
717  return ::GetPixelDataSize(width, height, format);
718  }
719 
725  inline bool IsReady() const {
726  return data != nullptr;
727  }
728 
729  private:
730  void set(const ::Image& image) {
731  data = image.data;
732  width = image.width;
733  height = image.height;
734  mipmaps = image.mipmaps;
735  format = image.format;
736  }
737 };
738 } // namespace raylib
739 
740 using RImage = raylib::Image;
741 
742 #endif // RAYLIB_CPP_INCLUDE_IMAGE_HPP_
Color type, RGBA (32bit)
Definition: Color.hpp:14
Image type, bpp always RGBA (32bit)
Definition: Image.hpp:17
Image & ResizeCanvas(int newWidth, int newHeight, int offsetX=0, int offsetY=0, ::Color color={255, 255, 255, 255})
Resize canvas and fill with color.
Definition: Image.hpp:449
Image & Format(int newFormat)
Convert image data to desired format.
Definition: Image.hpp:349
Image & ColorTint(::Color color={255, 255, 255, 255})
Modify image color: tint.
Definition: Image.hpp:506
void Load(const std::string &fileName, int *frames)
Load image sequence from file (frames appended to image.data).
Definition: Image.hpp:249
Image & Dither(int rBpp, int gBpp, int bBpp, int aBpp)
Dither image data to 16bpp or lower (Floyd-Steinberg dithering)
Definition: Image.hpp:466
Image & FlipVertical()
Flip image vertically.
Definition: Image.hpp:474
::Image WhiteNoise(int width, int height, float factor)
Generate image: white noise.
Definition: Image.hpp:166
Image & ResizeNN(int newWidth, int newHeight)
Resize and image to new size using Nearest-Neighbor scaling algorithm.
Definition: Image.hpp:441
::Image GradientH(int width, int height, ::Color left, ::Color right)
Generate image: horizontal gradient.
Definition: Image.hpp:143
Image(const std::string &fileName, int width, int height, int format, int headerSize=0)
Load a raw image from the given file, with the provided width, height, and formats.
Definition: Image.hpp:49
Image & Crop(int newWidth, int newHeight)
Crop an image to a new given width and height.
Definition: Image.hpp:405
void Load(const std::string &fileName, int width, int height, int format, int headerSize)
Load image from RAW file data.
Definition: Image.hpp:235
Image & Crop(int offsetX, int offsetY, int newWidth, int newHeight)
Crop an image to area defined by a rectangle.
Definition: Image.hpp:419
Image & ColorBrightness(int brightness)
Modify image color: brightness.
Definition: Image.hpp:542
Image & ColorGrayscale()
Modify image color: grayscale.
Definition: Image.hpp:522
Image & Crop(::Vector2 size)
Crop an image to a new given width and height based on a vector.
Definition: Image.hpp:412
::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:158
void Load(const ::Texture2D &texture)
Load an image from the given file.
Definition: Image.hpp:280
::Image Cellular(int width, int height, int tileSize)
Generate image: cellular algorithm.
Definition: Image.hpp:173
Image(const std::string &fileType, const unsigned char *fileData, int dataSize)
Load an image from the given file.
Definition: Image.hpp:69
void Load(const std::string &fileName)
Load image from file into CPU memory (RAM)
Definition: Image.hpp:221
Image & AlphaClear(::Color color, float threshold)
Clear alpha channel to desired color.
Definition: Image.hpp:381
Image & AlphaMask(const ::Image &alphaMask)
Apply alpha mask to image.
Definition: Image.hpp:389
Image(const ::Texture2D &texture)
Load an image from the given file.
Definition: Image.hpp:78
Rectangle GetAlphaBorder(float threshold) const
Get image alpha border rectangle.
Definition: Image.hpp:560
inline ::Image Copy() const
Create an image duplicate (useful for transformations)
Definition: Image.hpp:335
int GetPixelDataSize() const
Returns the pixel data size based on the current image.
Definition: Image.hpp:716
Image & Crop(::Rectangle crop)
Crop an image to area defined by a rectangle.
Definition: Image.hpp:365
::Image GradientV(int width, int height, ::Color top, ::Color bottom)
Generate image: vertical gradient.
Definition: Image.hpp:136
Image & AlphaCrop(float threshold)
Crop image depending on alpha value.
Definition: Image.hpp:373
raylib::Color GetColor(::Vector2 position) const
Get image pixel color at vector position.
Definition: Image.hpp:574
bool IsReady() const
Retrieve whether or not the Image has been loaded.
Definition: Image.hpp:725
Image & FlipHorizontal()
Flip image horizontally.
Definition: Image.hpp:482
inline ::Image FromImage(::Rectangle rec) const
Create an image from another image piece.
Definition: Image.hpp:342
Image & Resize(int newWidth, int newHeight)
Resize and image to new size.
Definition: Image.hpp:433
inline ::Color * LoadColors() const
Load color data from image as a Color array (RGBA - 32bit)
Definition: Image.hpp:663
Image(const std::string &fileName, int *frames)
Load an animation image from the given file.
Definition: Image.hpp:60
Image(const std::string &fileName)
Load an image from the given file.
Definition: Image.hpp:38
inline ::Color * LoadPalette(int maxPaletteSize, int *colorsCount) const
Load colors palette from image as a Color array (RGBA - 32bit)
Definition: Image.hpp:670
::Image Color(int width, int height, ::Color color={255, 255, 255, 255})
Generate image: plain color.
Definition: Image.hpp:129
raylib::Color GetColor(int x=0, int y=0) const
Get image pixel color at (x, y) position.
Definition: Image.hpp:567
void Load(const std::string &fileType, const unsigned char *fileData, int dataSize)
Load image from memory buffer, fileType refers to extension: i.e.
Definition: Image.hpp:263
Image & RotateCCW()
Rotate image counter-clockwise 90deg.
Definition: Image.hpp:498
inline ::Texture2D LoadTexture() const
Load texture from image data.
Definition: Image.hpp:691
static int GetPixelDataSize(int width, int height, int format=PIXELFORMAT_UNCOMPRESSED_R32G32B32A32)
Get pixel data size in bytes for certain format.
Definition: Image.hpp:707
::Image GradientRadial(int width, int height, float density, ::Color inner, ::Color outer)
Generate image: radial gradient.
Definition: Image.hpp:150
Image & Mipmaps()
Generate all mipmap levels for a provided image.
Definition: Image.hpp:458
::Image LoadFromScreen()
Get pixel data from screen buffer and return an Image (screenshot)
Definition: Image.hpp:122
inline ::Vector2 GetSize() const
Retrieve the width and height of the image.
Definition: Image.hpp:328
void Unload()
Unload image from CPU memory (RAM)
Definition: Image.hpp:290
void UnloadColors(::Color *colors) const
Unload color data loaded with LoadImageColors()
Definition: Image.hpp:677
void Export(const std::string &fileName) const
Export image data to file, returns true on success.
Definition: Image.hpp:302
Image & AlphaPremultiply()
Premultiply alpha channel.
Definition: Image.hpp:397
void ExportAsCode(const std::string &fileName) const
Export image as code file defining an array of bytes, returns true on success.
Definition: Image.hpp:313
void DrawPixel(int posX, int posY, ::Color color={255, 255, 255, 255})
Draw pixel within an image.
Definition: Image.hpp:589
void UnloadPalette(::Color *colors) const
Unload colors palette loaded with LoadImagePalette()
Definition: Image.hpp:684
Image & ToPOT(::Color fillColor)
Convert image to POT (power-of-two)
Definition: Image.hpp:357
Image & RotateCW()
Rotate image clockwise 90deg.
Definition: Image.hpp:490
Image & ClearBackground(::Color color={0, 0, 0, 255})
Clear image background with given color.
Definition: Image.hpp:581
Image & ColorContrast(float contrast)
Modify image color: contrast.
Definition: Image.hpp:532
Image & ColorInvert()
Modify image color: invert.
Definition: Image.hpp:514
Image & ColorReplace(::Color color, ::Color replace)
Modify image color: replace color.
Definition: Image.hpp:550
Exception used for most raylib-related exceptions.
Rectangle type.
Definition: Rectangle.hpp:12
Vector2 type.
Definition: Vector2.hpp:16
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
static bool ExportImageAsCode(const Image &image, const std::string &fileName)
Export image as code file (.h) defining an array of bytes.
Definition: Functions.hpp:244
static inline ::Image LoadImage(const std::string &fileName)
Load an image.
Definition: Functions.hpp:205
static bool ExportImage(const Image &image, const std::string &fileName)
Export image data to file.
Definition: Functions.hpp:237
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:228
static inline ::Image LoadImageAnim(const std::string &fileName, int *frames)
Load animated image data.
Definition: Functions.hpp:221
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:212