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(::LoadImageFromTexture(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 std::string& text, int fontSize, ::Color color = {255, 255, 255, 255}) {
46  set(::ImageText(text.c_str(), fontSize, color));
47  }
48 
49  Image(const ::Font& font, const std::string& text, float fontSize, float spacing,
50  ::Color tint = {255, 255, 255, 255}) {
51  set(::ImageTextEx(font, text.c_str(), fontSize, spacing, tint));
52  }
53 
54  Image(const Image& other) {
55  set(other.Copy());
56  }
57 
58  Image(Image&& other) {
59  set(other);
60 
61  other.data = nullptr;
62  other.width = 0;
63  other.height = 0;
64  other.mipmaps = 0;
65  other.format = 0;
66  }
67 
68  static ::Image Text(const std::string& text, int fontSize,
69  ::Color color = {255, 255, 255, 255}) {
70  return ::ImageText(text.c_str(), fontSize, color);
71  }
72 
73  static ::Image Text(const ::Font& font, const std::string& text, float fontSize, float spacing,
74  ::Color tint = {255, 255, 255, 255}) {
75  return ::ImageTextEx(font, text.c_str(), fontSize, spacing, tint);
76  }
77 
81  static ::Image LoadFromScreen() {
82  return ::LoadImageFromScreen();
83  }
84 
88  static ::Image Color(int width, int height, ::Color color = {255, 255, 255, 255}) {
89  return ::GenImageColor(width, height, color);
90  }
91 
95  static ::Image GradientV(int width, int height, ::Color top, ::Color bottom) {
96  return ::GenImageGradientV(width, height, top, bottom);
97  }
98 
102  static ::Image GradientH(int width, int height, ::Color left, ::Color right) {
103  return ::GenImageGradientH(width, height, left, right);
104  }
105 
109  static ::Image GradientRadial(int width, int height, float density,
110  ::Color inner, ::Color outer) {
111  return ::GenImageGradientRadial(width, height, density, inner, outer);
112  }
113 
117  static ::Image Checked(int width, int height, int checksX, int checksY,
118  ::Color col1 = {255, 255, 255, 255}, ::Color col2 = {0, 0, 0, 255}) {
119  return ::GenImageChecked(width, height, checksX, checksY, col1, col2);
120  }
121 
125  static ::Image WhiteNoise(int width, int height, float factor) {
126  return ::GenImageWhiteNoise(width, height, factor);
127  }
128 
132  static ::Image Cellular(int width, int height, int tileSize) {
133  return ::GenImageCellular(width, height, tileSize);
134  }
135 
136  ~Image() {
137  Unload();
138  }
139 
140  Image& operator=(const ::Image& image) {
141  set(image);
142  return *this;
143  }
144 
145  Image& operator=(const Image& other) {
146  if (this == &other) {
147  return *this;
148  }
149 
150  Unload();
151  set(other.Copy());
152  }
153 
154  Image& operator=(Image&& other) {
155  if (this == &other) {
156  return *this;
157  }
158 
159  Unload();
160  set(other);
161 
162  other.data = nullptr;
163  other.width = 0;
164  other.height = 0;
165  other.mipmaps = 0;
166  other.format = 0;
167 
168  return *this;
169  }
170 
174  void Load(const std::string& fileName) {
175  set(::LoadImage(fileName.c_str()));
176  }
177 
181  void LoadRaw(const std::string& fileName, int width, int height, int format, int headerSize) {
182  set(::LoadImageRaw(fileName.c_str(), width, height, format, headerSize));
183  }
184 
188  void LoadAnim(const std::string& fileName, int* frames) {
189  set(::LoadImageAnim(fileName.c_str(), frames));
190  }
191 
196  const std::string& fileType,
197  const unsigned char *fileData,
198  int dataSize) {
199  set(::LoadImageFromMemory(fileType.c_str(), fileData, dataSize));
200  }
201 
205  inline void Unload() {
206  if (data != nullptr) {
207  ::UnloadImage(*this);
208  data = nullptr;
209  }
210  }
211 
215  inline bool Export(const std::string& fileName) const {
216  // TODO(RobLoach): Switch to an invalid loading exception on false.
217  return ::ExportImage(*this, fileName.c_str());
218  }
219 
223  inline bool ExportAsCode(const std::string& fileName) const {
224  return ::ExportImageAsCode(*this, fileName.c_str());
225  }
226 
227  GETTERSETTER(void*, Data, data)
228  GETTERSETTER(int, Width, width)
229  GETTERSETTER(int, Height, height)
230  GETTERSETTER(int, Mipmaps, mipmaps)
231  GETTERSETTER(int, Format, format)
232 
236  inline ::Vector2 GetSize() const {
237  return {static_cast<float>(width), static_cast<float>(height)};
238  }
239 
243  inline ::Image Copy() const {
244  return ::ImageCopy(*this);
245  }
246 
250  inline ::Image FromImage(::Rectangle rec) const {
251  return ::ImageFromImage(*this, rec);
252  }
253 
257  inline Image& ToPOT(::Color fillColor) {
258  ::ImageToPOT(this, fillColor);
259  return *this;
260  }
261 
265  inline Image& Format(int newFormat) {
266  ::ImageFormat(this, newFormat);
267  return *this;
268  }
269 
273  inline Image& AlphaCrop(float threshold) {
274  ::ImageAlphaCrop(this, threshold);
275  return *this;
276  }
277 
281  inline Image& AlphaClear(::Color color, float threshold) {
282  ::ImageAlphaClear(this, color, threshold);
283  return *this;
284  }
285 
289  inline Image& AlphaMask(const ::Image& alphaMask) {
290  ::ImageAlphaMask(this, alphaMask);
291  return *this;
292  }
293 
298  ::ImageAlphaPremultiply(this);
299  return *this;
300  }
301 
305  inline Image& Crop(::Rectangle crop) {
306  ::ImageCrop(this, crop);
307  return *this;
308  }
309 
313  inline Image& Crop(::Vector2 size) {
314  return Crop(0, 0, static_cast<int>(size.x), static_cast<int>(size.y));
315  }
316 
320  inline Image& Crop(int offsetX, int offsetY, int newWidth, int newHeight) {
321  ::Rectangle rect{
322  static_cast<float>(offsetX),
323  static_cast<float>(offsetY),
324  static_cast<float>(newWidth),
325  static_cast<float>(newHeight)
326  };
327  ::ImageCrop(this, rect);
328  return *this;
329  }
330 
334  inline Image& Resize(int newWidth, int newHeight) {
335  ::ImageResize(this, newWidth, newHeight);
336  return *this;
337  }
338 
342  inline Image& ResizeNN(int newWidth, int newHeight) {
343  ::ImageResizeNN(this, newWidth, newHeight);
344  return *this;
345  }
346 
350  inline Image& ResizeCanvas(int newWidth, int newHeight, int offsetX, int offsetY,
351  ::Color color = {255, 255, 255, 255}) {
352  ::ImageResizeCanvas(this, newWidth, newHeight, offsetX, offsetY, color);
353  return *this;
354  }
355 
359  inline Image& Mipmaps() {
360  ::ImageMipmaps(this);
361  return *this;
362  }
363 
367  inline Image& Dither(int rBpp, int gBpp, int bBpp, int aBpp) {
368  ::ImageDither(this, rBpp, gBpp, bBpp, aBpp);
369  return *this;
370  }
371 
375  inline Image& FlipVertical() {
376  ::ImageFlipVertical(this);
377  return *this;
378  }
379 
383  inline Image& FlipHorizontal() {
384  ::ImageFlipHorizontal(this);
385  return *this;
386  }
387 
391  inline Image& RotateCW() {
392  ::ImageRotateCW(this);
393  return *this;
394  }
395 
399  inline Image& RotateCCW() {
400  ::ImageRotateCCW(this);
401  return *this;
402  }
403 
407  inline Image& ColorTint(::Color color = {255, 255, 255, 255}) {
408  ::ImageColorTint(this, color);
409  return *this;
410  }
411 
415  inline Image& ColorInvert() {
416  ::ImageColorInvert(this);
417  return *this;
418  }
419 
423  inline Image& ColorGrayscale() {
424  ::ImageColorGrayscale(this);
425  return *this;
426  }
427 
433  inline Image& ColorContrast(float contrast) {
434  ::ImageColorContrast(this, contrast);
435  return *this;
436  }
437 
443  inline Image& ColorBrightness(int brightness) {
444  ::ImageColorBrightness(this, brightness);
445  return *this;
446  }
447 
451  inline Image& ColorReplace(::Color color, ::Color replace) {
452  ::ImageColorReplace(this, color, replace);
453  return *this;
454  }
455 
461  inline Rectangle GetAlphaBorder(float threshold) const {
462  return ::GetImageAlphaBorder(*this, threshold);
463  }
464 
468  inline Image& ClearBackground(::Color color = {0, 0, 0, 255}) {
469  ::ImageClearBackground(this, color);
470  return *this;
471  }
472 
476  inline Image& DrawPixel(int posX, int posY, ::Color color = {255, 255, 255, 255}) {
477  ::ImageDrawPixel(this, posX, posY, color);
478  return *this;
479  }
480 
481  inline Image& DrawPixel(::Vector2 position, ::Color color = {255, 255, 255, 255}) {
482  ::ImageDrawPixelV(this, position, color);
483  return *this;
484  }
485 
486  inline Image& DrawLine(int startPosX, int startPosY, int endPosX, int endPosY,
487  ::Color color = {255, 255, 255, 255}) {
488  ::ImageDrawLine(this, startPosX, startPosY, endPosX, endPosY, color);
489  return *this;
490  }
491 
492  inline Image& DrawLine(::Vector2 start, ::Vector2 end, ::Color color = {255, 255, 255, 255}) {
493  ::ImageDrawLineV(this, start, end, color);
494  return *this;
495  }
496 
497  inline Image& DrawCircle(int centerX, int centerY, int radius,
498  ::Color color = {255, 255, 255, 255}) {
499  ::ImageDrawCircle(this, centerX, centerY, radius, color);
500  return *this;
501  }
502 
503  inline Image& DrawCircle(::Vector2 center, int radius,
504  ::Color color = {255, 255, 255, 255}) {
505  ::ImageDrawCircleV(this, center, radius, color);
506  return *this;
507  }
508 
509  inline Image& DrawRectangle(int posX, int posY, int width, int height,
510  ::Color color = {255, 255, 255, 255}) {
511  ::ImageDrawRectangle(this, posX, posY, width, height, color);
512  return *this;
513  }
514 
515  inline Image& DrawRectangle(Vector2 position, Vector2 size,
516  ::Color color = {255, 255, 255, 255}) {
517  ::ImageDrawRectangleV(this, position, size, color);
518  return *this;
519  }
520 
521  inline Image& DrawRectangle(::Rectangle rec, ::Color color = {255, 255, 255, 255}) {
522  ::ImageDrawRectangleRec(this, rec, color);
523  return *this;
524  }
525 
526  inline Image& DrawRectangleLines(::Rectangle rec, int thick = 1,
527  ::Color color = {255, 255, 255, 255}) {
528  ::ImageDrawRectangleLines(this, rec, thick, color);
529  return *this;
530  }
531 
532  inline Image& Draw(const ::Image& src, ::Rectangle srcRec, ::Rectangle dstRec,
533  ::Color tint = {255, 255, 255, 255}) {
534  ::ImageDraw(this, src, srcRec, dstRec, tint);
535  return *this;
536  }
537 
538  inline Image& DrawText(const std::string& text, ::Vector2 position, int fontSize,
539  ::Color color = {255, 255, 255, 255}) {
540  ::ImageDrawText(this,
541  text.c_str(),
542  static_cast<int>(position.x),
543  static_cast<int>(position.y),
544  fontSize,
545  color);
546  return *this;
547  }
548 
549  inline Image& DrawText(const std::string& text, int x, int y, int fontSize,
550  ::Color color = {255, 255, 255, 255}) {
551  ::ImageDrawText(this, text.c_str(), x, y, fontSize, color);
552  return *this;
553  }
554 
555  inline Image& DrawText(const ::Font& font, const std::string& text, ::Vector2 position,
556  float fontSize, float spacing, ::Color tint = {255, 255, 255, 255}) {
557  ::ImageDrawTextEx(this, font, text.c_str(), position, fontSize, spacing, tint);
558  return *this;
559  }
560 
564  inline ::Color* LoadColors() const {
565  return ::LoadImageColors(*this);
566  }
567 
571  inline ::Color* LoadPalette(int maxPaletteSize, int *colorsCount) const {
572  return ::LoadImagePalette(*this, maxPaletteSize, colorsCount);
573  }
574 
578  inline void UnloadColors(::Color* colors) const {
579  ::UnloadImageColors(colors);
580  }
581 
585  inline void UnloadPalette(::Color* colors) const {
586  ::UnloadImagePalette(colors);
587  }
588 
592  inline ::Texture2D LoadTexture() const {
593  return ::LoadTextureFromImage(*this);
594  }
595 
599  inline operator ::Texture2D() {
600  return LoadTexture();
601  }
602 
606  static int GetPixelDataSize(int width, int height, int format = PIXELFORMAT_UNCOMPRESSED_R32G32B32A32) {
607  return ::GetPixelDataSize(width, height, format);
608  }
609 
615  int GetPixelDataSize() const {
616  return ::GetPixelDataSize(width, height, format);
617  }
618 
619  private:
620  inline void set(const ::Image& image) {
621  data = image.data;
622  width = image.width;
623  height = image.height;
624  mipmaps = image.mipmaps;
625  format = image.format;
626  }
627 };
628 } // namespace raylib
629 
630 #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:367
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:7
raylib::Image::ColorBrightness
Image & ColorBrightness(int brightness)
Modify image color: brightness.
Definition: Image.hpp:443
raylib::Image::WhiteNoise
::Image WhiteNoise(int width, int height, float factor)
Generate image: white noise.
Definition: Image.hpp:125
raylib::LoadImage
static inline ::Image LoadImage(const std::string &fileName)
Load an image.
Definition: Functions.hpp:203
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:210
raylib::Image::GradientRadial
::Image GradientRadial(int width, int height, float density, ::Color inner, ::Color outer)
Generate image: radial gradient.
Definition: Image.hpp:109
raylib::Image::ToPOT
Image & ToPOT(::Color fillColor)
Convert image to POT (power-of-two)
Definition: Image.hpp:257
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:226
raylib::Image::Format
Image & Format(int newFormat)
Convert image data to desired format.
Definition: Image.hpp:265
raylib::Image::Crop
Image & Crop(::Vector2 size)
Crop an image to a new given width and height based on a vector.
Definition: Image.hpp:313
raylib::Image::ColorInvert
Image & ColorInvert()
Modify image color: invert.
Definition: Image.hpp:415
raylib::Image::ColorGrayscale
Image & ColorGrayscale()
Modify image color: grayscale.
Definition: Image.hpp:423
raylib::Image
Image type, bpp always RGBA (32bit)
Definition: Image.hpp:15
raylib::Image::FromImage
inline ::Image FromImage(::Rectangle rec) const
Create an image from another image piece.
Definition: Image.hpp:250
raylib::Image::Crop
Image & Crop(::Rectangle crop)
Crop an image to area defined by a rectangle.
Definition: Image.hpp:305
raylib::LoadImageAnim
static inline ::Image LoadImageAnim(const std::string &fileName, int *frames)
Load animated image data.
Definition: Functions.hpp:219
raylib::Image::ExportAsCode
bool ExportAsCode(const std::string &fileName) const
Export image as code file defining an array of bytes, returns true on success.
Definition: Image.hpp:223
raylib::Image::RotateCCW
Image & RotateCCW()
Rotate image counter-clockwise 90deg.
Definition: Image.hpp:399
raylib::Image::LoadPalette
inline ::Color * LoadPalette(int maxPaletteSize, int *colorsCount) const
Load colors palette from image as a Color array (RGBA - 32bit)
Definition: Image.hpp:571
raylib::Image::FlipVertical
Image & FlipVertical()
Flip image vertically.
Definition: Image.hpp:375
raylib::Image::LoadFromScreen
::Image LoadFromScreen()
Get pixel data from screen buffer and return an Image (screenshot)
Definition: Image.hpp:81
raylib::Image::Export
bool Export(const std::string &fileName) const
Export image data to file, returns true on success.
Definition: Image.hpp:215
raylib::Image::ColorContrast
Image & ColorContrast(float contrast)
Modify image color: contrast.
Definition: Image.hpp:433
raylib::Image::AlphaClear
Image & AlphaClear(::Color color, float threshold)
Clear alpha channel to desired color.
Definition: Image.hpp:281
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:181
raylib::Image::LoadTexture
inline ::Texture2D LoadTexture() const
Load texture from image data.
Definition: Image.hpp:592
raylib::Image::AlphaPremultiply
Image & AlphaPremultiply()
Premultiply alpha channel.
Definition: Image.hpp:297
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:320
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:195
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:350
raylib::Image::DrawPixel
Image & DrawPixel(int posX, int posY, ::Color color={255, 255, 255, 255})
Draw pixel within an image.
Definition: Image.hpp:476
raylib::Image::Mipmaps
Image & Mipmaps()
Generate all mipmap levels for a provided image.
Definition: Image.hpp:359
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:95
raylib::Image::ColorTint
Image & ColorTint(::Color color={255, 255, 255, 255})
Modify image color: tint.
Definition: Image.hpp:407
raylib::Image::GetAlphaBorder
Rectangle GetAlphaBorder(float threshold) const
Get image alpha border rectangle.
Definition: Image.hpp:461
raylib::Image::UnloadPalette
void UnloadPalette(::Color *colors) const
Unload colors palette loaded with LoadImagePalette()
Definition: Image.hpp:585
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:102
raylib::Image::LoadColors
inline ::Color * LoadColors() const
Load color data from image as a Color array (RGBA - 32bit)
Definition: Image.hpp:564
raylib::Image::LoadAnim
void LoadAnim(const std::string &fileName, int *frames)
Load image sequence from file (frames appended to image.data).
Definition: Image.hpp:188
raylib::Image::Load
void Load(const std::string &fileName)
Load image from file into CPU memory (RAM)
Definition: Image.hpp:174
raylib::Image::Color
::Image Color(int width, int height, ::Color color={255, 255, 255, 255})
Generate image: plain color.
Definition: Image.hpp:88
raylib::Image::GetSize
inline ::Vector2 GetSize() const
Retrieve the width and height of the image.
Definition: Image.hpp:236
raylib::Image::GetPixelDataSize
int GetPixelDataSize() const
Returns the pixel data size of the image.
Definition: Image.hpp:615
raylib::Image::UnloadColors
void UnloadColors(::Color *colors) const
Unload color data loaded with LoadImageColors()
Definition: Image.hpp:578
raylib::Image::AlphaCrop
Image & AlphaCrop(float threshold)
Crop image depending on alpha value.
Definition: Image.hpp:273
raylib::Image::Resize
Image & Resize(int newWidth, int newHeight)
Resize and image to new size.
Definition: Image.hpp:334
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:117
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:606
raylib::Image::Unload
void Unload()
Unload image from CPU memory (RAM)
Definition: Image.hpp:205
raylib::Image::FlipHorizontal
Image & FlipHorizontal()
Flip image horizontally.
Definition: Image.hpp:383
raylib::Image::ResizeNN
Image & ResizeNN(int newWidth, int newHeight)
Resize and image to new size using Nearest-Neighbor scaling algorithm.
Definition: Image.hpp:342
raylib::Image::Cellular
::Image Cellular(int width, int height, int tileSize)
Generate image: cellular algorithm.
Definition: Image.hpp:132
raylib::Image::ClearBackground
Image & ClearBackground(::Color color={0, 0, 0, 255})
Clear image background with given color.
Definition: Image.hpp:468
raylib::Image::RotateCW
Image & RotateCW()
Rotate image clockwise 90deg.
Definition: Image.hpp:391
raylib::Image::AlphaMask
Image & AlphaMask(const ::Image &alphaMask)
Apply alpha mask to image.
Definition: Image.hpp:289
raylib::Image::ColorReplace
Image & ColorReplace(::Color color, ::Color replace)
Modify image color: replace color.
Definition: Image.hpp:451
raylib::Image::Copy
inline ::Image Copy() const
Create an image duplicate (useful for transformations)
Definition: Image.hpp:243