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  return *this;
154  }
155 
156  Image& operator=(Image&& other) {
157  if (this == &other) {
158  return *this;
159  }
160 
161  Unload();
162  set(other);
163 
164  other.data = nullptr;
165  other.width = 0;
166  other.height = 0;
167  other.mipmaps = 0;
168  other.format = 0;
169 
170  return *this;
171  }
172 
176  void Load(const std::string& fileName) {
177  set(::LoadImage(fileName.c_str()));
178  }
179 
183  void LoadRaw(const std::string& fileName, int width, int height, int format, int headerSize) {
184  set(::LoadImageRaw(fileName.c_str(), width, height, format, headerSize));
185  }
186 
190  void LoadAnim(const std::string& fileName, int* frames) {
191  set(::LoadImageAnim(fileName.c_str(), frames));
192  }
193 
198  const std::string& fileType,
199  const unsigned char *fileData,
200  int dataSize) {
201  set(::LoadImageFromMemory(fileType.c_str(), fileData, dataSize));
202  }
203 
207  inline void Unload() {
208  if (data != nullptr) {
209  ::UnloadImage(*this);
210  data = nullptr;
211  }
212  }
213 
217  inline bool Export(const std::string& fileName) const {
218  // TODO(RobLoach): Switch to an invalid loading exception on false.
219  return ::ExportImage(*this, fileName.c_str());
220  }
221 
225  inline bool ExportAsCode(const std::string& fileName) const {
226  return ::ExportImageAsCode(*this, fileName.c_str());
227  }
228 
229  GETTERSETTER(void*, Data, data)
230  GETTERSETTER(int, Width, width)
231  GETTERSETTER(int, Height, height)
232  GETTERSETTER(int, Mipmaps, mipmaps)
233  GETTERSETTER(int, Format, format)
234 
238  inline ::Vector2 GetSize() const {
239  return {static_cast<float>(width), static_cast<float>(height)};
240  }
241 
245  inline ::Image Copy() const {
246  return ::ImageCopy(*this);
247  }
248 
252  inline ::Image FromImage(::Rectangle rec) const {
253  return ::ImageFromImage(*this, rec);
254  }
255 
259  inline Image& ToPOT(::Color fillColor) {
260  ::ImageToPOT(this, fillColor);
261  return *this;
262  }
263 
267  inline Image& Format(int newFormat) {
268  ::ImageFormat(this, newFormat);
269  return *this;
270  }
271 
275  inline Image& AlphaCrop(float threshold) {
276  ::ImageAlphaCrop(this, threshold);
277  return *this;
278  }
279 
283  inline Image& AlphaClear(::Color color, float threshold) {
284  ::ImageAlphaClear(this, color, threshold);
285  return *this;
286  }
287 
291  inline Image& AlphaMask(const ::Image& alphaMask) {
292  ::ImageAlphaMask(this, alphaMask);
293  return *this;
294  }
295 
300  ::ImageAlphaPremultiply(this);
301  return *this;
302  }
303 
307  inline Image& Crop(::Rectangle crop) {
308  ::ImageCrop(this, crop);
309  return *this;
310  }
311 
315  inline Image& Crop(::Vector2 size) {
316  return Crop(0, 0, static_cast<int>(size.x), static_cast<int>(size.y));
317  }
318 
322  inline Image& Crop(int offsetX, int offsetY, int newWidth, int newHeight) {
323  ::Rectangle rect{
324  static_cast<float>(offsetX),
325  static_cast<float>(offsetY),
326  static_cast<float>(newWidth),
327  static_cast<float>(newHeight)
328  };
329  ::ImageCrop(this, rect);
330  return *this;
331  }
332 
336  inline Image& Resize(int newWidth, int newHeight) {
337  ::ImageResize(this, newWidth, newHeight);
338  return *this;
339  }
340 
344  inline Image& ResizeNN(int newWidth, int newHeight) {
345  ::ImageResizeNN(this, newWidth, newHeight);
346  return *this;
347  }
348 
352  inline Image& ResizeCanvas(int newWidth, int newHeight, int offsetX, int offsetY,
353  ::Color color = {255, 255, 255, 255}) {
354  ::ImageResizeCanvas(this, newWidth, newHeight, offsetX, offsetY, color);
355  return *this;
356  }
357 
361  inline Image& Mipmaps() {
362  ::ImageMipmaps(this);
363  return *this;
364  }
365 
369  inline Image& Dither(int rBpp, int gBpp, int bBpp, int aBpp) {
370  ::ImageDither(this, rBpp, gBpp, bBpp, aBpp);
371  return *this;
372  }
373 
377  inline Image& FlipVertical() {
378  ::ImageFlipVertical(this);
379  return *this;
380  }
381 
385  inline Image& FlipHorizontal() {
386  ::ImageFlipHorizontal(this);
387  return *this;
388  }
389 
393  inline Image& RotateCW() {
394  ::ImageRotateCW(this);
395  return *this;
396  }
397 
401  inline Image& RotateCCW() {
402  ::ImageRotateCCW(this);
403  return *this;
404  }
405 
409  inline Image& ColorTint(::Color color = {255, 255, 255, 255}) {
410  ::ImageColorTint(this, color);
411  return *this;
412  }
413 
417  inline Image& ColorInvert() {
418  ::ImageColorInvert(this);
419  return *this;
420  }
421 
425  inline Image& ColorGrayscale() {
426  ::ImageColorGrayscale(this);
427  return *this;
428  }
429 
435  inline Image& ColorContrast(float contrast) {
436  ::ImageColorContrast(this, contrast);
437  return *this;
438  }
439 
445  inline Image& ColorBrightness(int brightness) {
446  ::ImageColorBrightness(this, brightness);
447  return *this;
448  }
449 
453  inline Image& ColorReplace(::Color color, ::Color replace) {
454  ::ImageColorReplace(this, color, replace);
455  return *this;
456  }
457 
463  inline Rectangle GetAlphaBorder(float threshold) const {
464  return ::GetImageAlphaBorder(*this, threshold);
465  }
466 
470  inline Image& ClearBackground(::Color color = {0, 0, 0, 255}) {
471  ::ImageClearBackground(this, color);
472  return *this;
473  }
474 
478  inline Image& DrawPixel(int posX, int posY, ::Color color = {255, 255, 255, 255}) {
479  ::ImageDrawPixel(this, posX, posY, color);
480  return *this;
481  }
482 
483  inline Image& DrawPixel(::Vector2 position, ::Color color = {255, 255, 255, 255}) {
484  ::ImageDrawPixelV(this, position, color);
485  return *this;
486  }
487 
488  inline Image& DrawLine(int startPosX, int startPosY, int endPosX, int endPosY,
489  ::Color color = {255, 255, 255, 255}) {
490  ::ImageDrawLine(this, startPosX, startPosY, endPosX, endPosY, color);
491  return *this;
492  }
493 
494  inline Image& DrawLine(::Vector2 start, ::Vector2 end, ::Color color = {255, 255, 255, 255}) {
495  ::ImageDrawLineV(this, start, end, color);
496  return *this;
497  }
498 
499  inline Image& DrawCircle(int centerX, int centerY, int radius,
500  ::Color color = {255, 255, 255, 255}) {
501  ::ImageDrawCircle(this, centerX, centerY, radius, color);
502  return *this;
503  }
504 
505  inline Image& DrawCircle(::Vector2 center, int radius,
506  ::Color color = {255, 255, 255, 255}) {
507  ::ImageDrawCircleV(this, center, radius, color);
508  return *this;
509  }
510 
511  inline Image& DrawRectangle(int posX, int posY, int width, int height,
512  ::Color color = {255, 255, 255, 255}) {
513  ::ImageDrawRectangle(this, posX, posY, width, height, color);
514  return *this;
515  }
516 
517  inline Image& DrawRectangle(Vector2 position, Vector2 size,
518  ::Color color = {255, 255, 255, 255}) {
519  ::ImageDrawRectangleV(this, position, size, color);
520  return *this;
521  }
522 
523  inline Image& DrawRectangle(::Rectangle rec, ::Color color = {255, 255, 255, 255}) {
524  ::ImageDrawRectangleRec(this, rec, color);
525  return *this;
526  }
527 
528  inline Image& DrawRectangleLines(::Rectangle rec, int thick = 1,
529  ::Color color = {255, 255, 255, 255}) {
530  ::ImageDrawRectangleLines(this, rec, thick, color);
531  return *this;
532  }
533 
534  inline Image& Draw(const ::Image& src, ::Rectangle srcRec, ::Rectangle dstRec,
535  ::Color tint = {255, 255, 255, 255}) {
536  ::ImageDraw(this, src, srcRec, dstRec, tint);
537  return *this;
538  }
539 
540  inline Image& DrawText(const std::string& text, ::Vector2 position, int fontSize,
541  ::Color color = {255, 255, 255, 255}) {
542  ::ImageDrawText(this,
543  text.c_str(),
544  static_cast<int>(position.x),
545  static_cast<int>(position.y),
546  fontSize,
547  color);
548  return *this;
549  }
550 
551  inline Image& DrawText(const std::string& text, int x, int y, int fontSize,
552  ::Color color = {255, 255, 255, 255}) {
553  ::ImageDrawText(this, text.c_str(), x, y, fontSize, color);
554  return *this;
555  }
556 
557  inline Image& DrawText(const ::Font& font, const std::string& text, ::Vector2 position,
558  float fontSize, float spacing, ::Color tint = {255, 255, 255, 255}) {
559  ::ImageDrawTextEx(this, font, text.c_str(), position, fontSize, spacing, tint);
560  return *this;
561  }
562 
566  inline ::Color* LoadColors() const {
567  return ::LoadImageColors(*this);
568  }
569 
573  inline ::Color* LoadPalette(int maxPaletteSize, int *colorsCount) const {
574  return ::LoadImagePalette(*this, maxPaletteSize, colorsCount);
575  }
576 
580  inline void UnloadColors(::Color* colors) const {
581  ::UnloadImageColors(colors);
582  }
583 
587  inline void UnloadPalette(::Color* colors) const {
588  ::UnloadImagePalette(colors);
589  }
590 
594  inline ::Texture2D LoadTexture() const {
595  return ::LoadTextureFromImage(*this);
596  }
597 
601  inline operator ::Texture2D() {
602  return LoadTexture();
603  }
604 
608  static int GetPixelDataSize(int width, int height, int format = PIXELFORMAT_UNCOMPRESSED_R32G32B32A32) {
609  return ::GetPixelDataSize(width, height, format);
610  }
611 
617  int GetPixelDataSize() const {
618  return ::GetPixelDataSize(width, height, format);
619  }
620 
626  inline bool IsLoaded() const {
627  return data != nullptr;
628  }
629 
630  private:
631  inline void set(const ::Image& image) {
632  data = image.data;
633  width = image.width;
634  height = image.height;
635  mipmaps = image.mipmaps;
636  format = image.format;
637  }
638 };
639 } // namespace raylib
640 
641 #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:369
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:445
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:259
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:267
raylib::Image::Crop
Image & Crop(::Vector2 size)
Crop an image to a new given width and height based on a vector.
Definition: Image.hpp:315
raylib::Image::ColorInvert
Image & ColorInvert()
Modify image color: invert.
Definition: Image.hpp:417
raylib::Image::ColorGrayscale
Image & ColorGrayscale()
Modify image color: grayscale.
Definition: Image.hpp:425
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:252
raylib::Image::Crop
Image & Crop(::Rectangle crop)
Crop an image to area defined by a rectangle.
Definition: Image.hpp:307
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:225
raylib::Image::RotateCCW
Image & RotateCCW()
Rotate image counter-clockwise 90deg.
Definition: Image.hpp:401
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:573
raylib::Image::FlipVertical
Image & FlipVertical()
Flip image vertically.
Definition: Image.hpp:377
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:217
raylib::Image::ColorContrast
Image & ColorContrast(float contrast)
Modify image color: contrast.
Definition: Image.hpp:435
raylib::Image::AlphaClear
Image & AlphaClear(::Color color, float threshold)
Clear alpha channel to desired color.
Definition: Image.hpp:283
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:183
raylib::Image::LoadTexture
inline ::Texture2D LoadTexture() const
Load texture from image data.
Definition: Image.hpp:594
raylib::Image::AlphaPremultiply
Image & AlphaPremultiply()
Premultiply alpha channel.
Definition: Image.hpp:299
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:322
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:197
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:352
raylib::Image::DrawPixel
Image & DrawPixel(int posX, int posY, ::Color color={255, 255, 255, 255})
Draw pixel within an image.
Definition: Image.hpp:478
raylib::Image::Mipmaps
Image & Mipmaps()
Generate all mipmap levels for a provided image.
Definition: Image.hpp:361
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:409
raylib::Image::GetAlphaBorder
Rectangle GetAlphaBorder(float threshold) const
Get image alpha border rectangle.
Definition: Image.hpp:463
raylib::Image::UnloadPalette
void UnloadPalette(::Color *colors) const
Unload colors palette loaded with LoadImagePalette()
Definition: Image.hpp:587
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:566
raylib::Image::LoadAnim
void LoadAnim(const std::string &fileName, int *frames)
Load image sequence from file (frames appended to image.data).
Definition: Image.hpp:190
raylib::Image::Load
void Load(const std::string &fileName)
Load image from file into CPU memory (RAM)
Definition: Image.hpp:176
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:238
raylib::Image::GetPixelDataSize
int GetPixelDataSize() const
Returns the pixel data size of the image.
Definition: Image.hpp:617
raylib::Image::UnloadColors
void UnloadColors(::Color *colors) const
Unload color data loaded with LoadImageColors()
Definition: Image.hpp:580
raylib::Image::AlphaCrop
Image & AlphaCrop(float threshold)
Crop image depending on alpha value.
Definition: Image.hpp:275
raylib::Image::Resize
Image & Resize(int newWidth, int newHeight)
Resize and image to new size.
Definition: Image.hpp:336
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:608
raylib::Image::Unload
void Unload()
Unload image from CPU memory (RAM)
Definition: Image.hpp:207
raylib::Image::FlipHorizontal
Image & FlipHorizontal()
Flip image horizontally.
Definition: Image.hpp:385
raylib::Image::ResizeNN
Image & ResizeNN(int newWidth, int newHeight)
Resize and image to new size using Nearest-Neighbor scaling algorithm.
Definition: Image.hpp:344
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:470
raylib::Image::RotateCW
Image & RotateCW()
Rotate image clockwise 90deg.
Definition: Image.hpp:393
raylib::Image::IsLoaded
bool IsLoaded() const
Retrieve whether or not the Image has been loaded.
Definition: Image.hpp:626
raylib::Image::AlphaMask
Image & AlphaMask(const ::Image &alphaMask)
Apply alpha mask to image.
Definition: Image.hpp:291
raylib::Image::ColorReplace
Image & ColorReplace(::Color color, ::Color replace)
Modify image color: replace color.
Definition: Image.hpp:453
raylib::Image::Copy
inline ::Image Copy() const
Create an image duplicate (useful for transformations)
Definition: Image.hpp:245