raylib-cpp
C++ object-oriented wrapper library for raylib.
Image.hpp
1 /*
2 * LICENSE: zlib/libpng
3 *
4 * raylib-cpp is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
5 * BSD-like license that allows static linking with closed source software:
6 *
7 * Copyright (c) 2020 Rob Loach (@RobLoach)
8 *
9 * This software is provided "as-is", without any express or implied warranty. In no event
10 * will the authors be held liable for any damages arising from the use of this software.
11 *
12 * Permission is granted to anyone to use this software for any purpose, including commercial
13 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not claim that you
16 * wrote the original software. If you use this software in a product, an acknowledgment
17 * in the product documentation would be appreciated but is not required.
18 *
19 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
20 * as being the original software.
21 *
22 * 3. This notice may not be removed or altered from any source distribution.
23 */
24 
25 #ifndef RAYLIB_CPP_INCLUDE_IMAGE_HPP_
26 #define RAYLIB_CPP_INCLUDE_IMAGE_HPP_
27 
28 #include <string>
29 
30 #include "./raylib.hpp"
31 #include "./raylib-cpp-utils.hpp"
32 
33 namespace raylib {
34 class Image : public ::Image {
35  public:
36  Image() {}
37  Image(::Image image) {
38  set(image);
39  }
40  Image(const std::string& fileName) {
41  Load(fileName);
42  }
43  Image(const std::string& fileName, int width, int height, int format, int headerSize) {
44  LoadRaw(fileName, width, height, format, headerSize);
45  }
46  Image(const std::string& fileName, int* frames) {
47  LoadAnim(fileName, frames);
48  }
49  Image(const std::string& fileType, const unsigned char* fileData, int dataSize) {
50  LoadFromMemory(fileType, fileData, dataSize);
51  }
52  Image(::Texture2D texture) {
53  set(::GetTextureData(texture));
54  }
55  Image(int width, int height, Color color = WHITE) {
56  set(::GenImageColor(width, height, color));
57  }
58  Image(::Font font, const std::string& text, float fontSize, float spacing, ::Color tint) {
59  set(::ImageTextEx(font, text.c_str(), fontSize, spacing, tint));
60  }
61 
62  static Image Text(std::string text, int fontSize, Color color) {
63  return ::ImageText(text.c_str(), fontSize, color);
64  }
65 
66  static Image Text(::Font font, std::string text, float fontSize, float spacing, ::Color tint) {
67  return ::ImageTextEx(font, text.c_str(), fontSize, spacing, tint);
68  }
69 
73  static Image GetScreenData() {
74  return Image(::GetScreenData());
75  }
76 
80  static Image GenColor(int width, int height, Color color) {
81  return ::GenImageColor(width, height, color);
82  }
83 
87  static Image GenGradientV(int width, int height, Color top, Color bottom) {
88  return ::GenImageGradientV(width, height, top, bottom);
89  }
90 
94  static Image GenGradientH(int width, int height, Color left, Color right) {
95  return ::GenImageGradientH(width, height, left, right);
96  }
97 
101  static Image GenGradientRadial(int width, int height, float density,
102  Color inner, Color outer) {
103  return ::GenImageGradientRadial(width, height, density, inner, outer);
104  }
105 
109  static Image GenChecked(int width, int height, int checksX, int checksY,
110  Color col1, Color col2) {
111  return ::GenImageChecked(width, height, checksX, checksY, col1, col2);
112  }
113 
117  static Image GenWhiteNoise(int width, int height, float factor) {
118  return ::GenImageWhiteNoise(width, height, factor);
119  }
120 
124  static Image GenPerlinNoise(int width, int height, int offsetX, int offsetY, float scale) {
125  return ::GenImagePerlinNoise(width, height, offsetX, offsetY, scale);
126  }
127 
131  static Image GenCellular(int width, int height, int tileSize) {
132  return ::GenImageCellular(width, height, tileSize);
133  }
134 
135  ~Image() {
136  Unload();
137  }
138 
139  Image& operator=(const ::Image& image) {
140  set(image);
141  return *this;
142  }
143 
144  Image& operator=(const Image& image) {
145  set(image);
146  return *this;
147  }
148 
149  void Load(const std::string& fileName) {
150  set(::LoadImage(fileName.c_str()));
151  }
152 
153  void LoadRaw(const std::string& fileName, int width, int height, int format, int headerSize) {
154  set(::LoadImageRaw(fileName.c_str(), width, height, format, headerSize));
155  }
156 
157  void LoadAnim(const std::string& fileName, int* frames) {
158  set(::LoadImageAnim(fileName.c_str(), frames));
159  }
160 
161  void LoadFromMemory(const std::string& fileType, const unsigned char *fileData, int dataSize) {
162  set(::LoadImageFromMemory(fileType.c_str(), fileData, dataSize));
163  }
164 
165  inline void Unload() {
166  ::UnloadImage(*this);
167  }
168 
172  inline bool Export(const std::string& fileName) {
173  // TODO(RobLoach): Switch to an invalid loading exception on false.
174  return ::ExportImage(*this, fileName.c_str());
175  }
176 
180  inline bool ExportAsCode(const std::string& fileName) {
181  return ::ExportImageAsCode(*this, fileName.c_str());
182  }
183 
184  GETTERSETTER(void*, Data, data)
185  GETTERSETTER(int, Width, width)
186  GETTERSETTER(int, Height, height)
187  GETTERSETTER(int, Mipmaps, mipmaps)
188  GETTERSETTER(int, Format, format)
189 
193  inline Image Copy() {
194  return ::ImageCopy(*this);
195  }
196 
200  inline Image FromImage(::Rectangle rec) {
201  return ::ImageFromImage(*this, rec);
202  }
203 
207  inline Image& ToPOT(Color fillColor) {
208  ::ImageToPOT(this, fillColor);
209  return *this;
210  }
211 
215  inline Image& Format(int newFormat) {
216  ::ImageFormat(this, newFormat);
217  return *this;
218  }
219 
223  inline Image& AlphaMask(Image alphaMask) {
224  ::ImageAlphaMask(this, alphaMask);
225  return *this;
226  }
227 
231  inline Image& AlphaCrop(float threshold) {
232  ::ImageAlphaCrop(this, threshold);
233  return *this;
234  }
235 
240  ::ImageAlphaPremultiply(this);
241  return *this;
242  }
243 
247  inline Image& Crop(::Rectangle crop) {
248  ::ImageCrop(this, crop);
249  return *this;
250  }
251 
255  inline Image& Crop(int offsetX, int offsetY, int newWidth, int newHeight) {
256  ::Rectangle rect{
257  static_cast<float>(offsetX),
258  static_cast<float>(offsetY),
259  static_cast<float>(newWidth),
260  static_cast<float>(newHeight)
261  };
262  ::ImageCrop(this, rect);
263  return *this;
264  }
265 
269  inline Image& Resize(int newWidth, int newHeight) {
270  ::ImageResize(this, newWidth, newHeight);
271  return *this;
272  }
273 
277  inline Image& ResizeNN(int newWidth, int newHeight) {
278  ::ImageResizeNN(this, newWidth, newHeight);
279  return *this;
280  }
281 
285  inline Image& ResizeCanvas(int newWidth, int newHeight, int offsetX, int offsetY, Color color) {
286  ::ImageResizeCanvas(this, newWidth, newHeight, offsetX, offsetY, color);
287  return *this;
288  }
289 
293  inline Image& Mipmaps() {
294  ::ImageMipmaps(this);
295  return *this;
296  }
297 
301  inline Image& Dither(int rBpp, int gBpp, int bBpp, int aBpp) {
302  ::ImageDither(this, rBpp, gBpp, bBpp, aBpp);
303  return *this;
304  }
305 
309  inline Image& FlipVertical() {
310  ::ImageFlipVertical(this);
311  return *this;
312  }
313 
317  inline Image& FlipHorizontal() {
318  ::ImageFlipHorizontal(this);
319  return *this;
320  }
321 
325  inline Image& RotateCW() {
326  ::ImageRotateCW(this);
327  return *this;
328  }
329 
333  inline Image& RotateCCW() {
334  ::ImageRotateCCW(this);
335  return *this;
336  }
337 
341  inline Image& ColorTint(::Color color = WHITE) {
342  ::ImageColorTint(this, color);
343  return *this;
344  }
345 
349  inline Image& ColorInvert() {
350  ::ImageColorInvert(this);
351  return *this;
352  }
353 
357  inline Image& ColorGrayscale() {
358  ::ImageColorGrayscale(this);
359  return *this;
360  }
361 
367  inline Image& ColorContrast(float contrast) {
368  ::ImageColorContrast(this, contrast);
369  return *this;
370  }
371 
377  inline Image& ColorBrightness(int brightness) {
378  ::ImageColorBrightness(this, brightness);
379  return *this;
380  }
381 
385  inline Image& ColorReplace(::Color color, ::Color replace) {
386  ::ImageColorReplace(this, color, replace);
387  return *this;
388  }
389 
395  inline Rectangle GetAlphaBorder(float threshold) const {
396  return ::GetImageAlphaBorder(*this, threshold);
397  }
398 
402  inline Image& ClearBackground(::Color color = WHITE) {
403  ::ImageClearBackground(this, color);
404  return *this;
405  }
406 
410  inline Image& DrawPixel(int posX, int posY, ::Color color) {
411  ::ImageDrawPixel(this, posX, posY, color);
412  return *this;
413  }
414 
415  inline Image& DrawPixel(::Vector2 position, ::Color color) {
416  ::ImageDrawPixelV(this, position, color);
417  return *this;
418  }
419 
420  inline Image& DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, ::Color color) {
421  ::ImageDrawLine(this, startPosX, startPosY, endPosX, endPosY, color);
422  return *this;
423  }
424 
425  inline Image& DrawLine(::Vector2 start, ::Vector2 end, ::Color color) {
426  ::ImageDrawLineV(this, start, end, color);
427  return *this;
428  }
429 
430  inline Image& DrawCircle(int centerX, int centerY, int radius, ::Color color) {
431  ::ImageDrawCircle(this, centerX, centerY, radius, color);
432  return *this;
433  }
434 
435  inline Image& DrawCircle(::Vector2 center, int radius, ::Color color) {
436  ::ImageDrawCircleV(this, center, radius, color);
437  return *this;
438  }
439 
440  inline Image& DrawRectangle(int posX, int posY, int width, int height, ::Color color = WHITE) {
441  ::ImageDrawRectangle(this, posX, posY, width, height, color);
442  return *this;
443  }
444 
445  inline Image& DrawRectangle(Vector2 position, Vector2 size, ::Color color = WHITE) {
446  ::ImageDrawRectangleV(this, position, size, color);
447  return *this;
448  }
449 
450  inline Image& DrawRectangle(::Rectangle rec, ::Color color = WHITE) {
451  ::ImageDrawRectangleRec(this, rec, color);
452  return *this;
453  }
454 
455  inline Image& DrawRectangleLines(::Rectangle rec, int thick, ::Color color) {
456  ::ImageDrawRectangleLines(this, rec, thick, color);
457  return *this;
458  }
459 
460  inline Image& Draw(const ::Image& src, ::Rectangle srcRec, ::Rectangle dstRec,
461  ::Color tint = WHITE) {
462  ::ImageDraw(this, src, srcRec, dstRec, tint);
463  return *this;
464  }
465 
466  inline Image& DrawText(const std::string& text, ::Vector2 position, int fontSize,
467  ::Color color = WHITE) {
468  ::ImageDrawText(this,
469  text.c_str(),
470  static_cast<int>(position.x),
471  static_cast<int>(position.y),
472  fontSize,
473  color);
474  return *this;
475  }
476 
477  inline Image& DrawText(const std::string& text, int x, int y, int fontSize,
478  ::Color color = WHITE) {
479  ::ImageDrawText(this, text.c_str(), x, y, fontSize, color);
480  return *this;
481  }
482 
483  inline Image& DrawText(::Font font, const std::string& text, ::Vector2 position,
484  float fontSize, float spacing, ::Color tint = WHITE) {
485  ::ImageDrawTextEx(this, font, text.c_str(), position, fontSize, spacing, tint);
486  return *this;
487  }
488 
492  inline ::Color* LoadColors() {
493  return ::LoadImageColors(*this);
494  }
495 
499  inline ::Color* LoadPalette(int maxPaletteSize, int *colorsCount) {
500  return ::LoadImagePalette(*this, maxPaletteSize, colorsCount);
501  }
502 
506  inline ::Texture2D LoadTexture() {
507  return ::LoadTextureFromImage(*this);
508  }
509 
513  inline operator ::Texture2D() {
514  return LoadTexture();
515  }
516 
517  protected:
518  inline void set(::Image image) {
519  data = image.data;
520  width = image.width;
521  height = image.height;
522  mipmaps = image.mipmaps;
523  format = image.format;
524  }
525 };
526 } // namespace raylib
527 
528 #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:301
raylib
Provides all the classes associated with raylib-cpp.
Definition: AudioDevice.hpp:31
raylib::Image::LoadPalette
inline ::Color * LoadPalette(int maxPaletteSize, int *colorsCount)
Load colors palette from image as a Color array (RGBA - 32bit)
Definition: Image.hpp:499
raylib::Image::DrawRectangle
Image & DrawRectangle(Vector2 position, Vector2 size, ::Color color=WHITE)
Definition: Image.hpp:445
raylib::Image::ColorBrightness
Image & ColorBrightness(int brightness)
Modify image color: brightness.
Definition: Image.hpp:377
raylib::Image::set
void set(::Image image)
Definition: Image.hpp:518
raylib::Image::Image
Image(::Texture2D texture)
Definition: Image.hpp:52
raylib::Image::Image
Image(const std::string &fileType, const unsigned char *fileData, int dataSize)
Definition: Image.hpp:49
raylib::Image::~Image
~Image()
Definition: Image.hpp:135
raylib::Image::Format
Image & Format(int newFormat)
Convert image data to desired format.
Definition: Image.hpp:215
raylib::Image::GenGradientV
static Image GenGradientV(int width, int height, Color top, Color bottom)
Generate image: vertical gradient.
Definition: Image.hpp:87
raylib::Image::DrawText
Image & DrawText(const std::string &text, ::Vector2 position, int fontSize, ::Color color=WHITE)
Definition: Image.hpp:466
raylib::Image::ColorInvert
Image & ColorInvert()
Modify image color: invert.
Definition: Image.hpp:349
raylib::Image::ToPOT
Image & ToPOT(Color fillColor)
Convert image to POT (power-of-two)
Definition: Image.hpp:207
raylib::Image::ColorGrayscale
Image & ColorGrayscale()
Modify image color: grayscale.
Definition: Image.hpp:357
raylib::Image
Definition: Image.hpp:34
raylib::Image::Crop
Image & Crop(::Rectangle crop)
Crop an image to area defined by a rectangle.
Definition: Image.hpp:247
raylib::Image::Image
Image(const std::string &fileName)
Definition: Image.hpp:40
raylib::Image::Image
Image(::Image image)
Definition: Image.hpp:37
raylib::Image::GenGradientH
static Image GenGradientH(int width, int height, Color left, Color right)
Generate image: horizontal gradient.
Definition: Image.hpp:94
raylib::Image::GenWhiteNoise
static Image GenWhiteNoise(int width, int height, float factor)
Generate image: white noise.
Definition: Image.hpp:117
raylib::Image::RotateCCW
Image & RotateCCW()
Rotate image counter-clockwise 90deg.
Definition: Image.hpp:333
raylib::Image::DrawLine
Image & DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, ::Color color)
Definition: Image.hpp:420
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:180
raylib::Image::DrawPixel
Image & DrawPixel(int posX, int posY, ::Color color)
Draw pixel within an image.
Definition: Image.hpp:410
raylib::Image::DrawRectangle
Image & DrawRectangle(int posX, int posY, int width, int height, ::Color color=WHITE)
Definition: Image.hpp:440
raylib::Image::FlipVertical
Image & FlipVertical()
Flip image vertically.
Definition: Image.hpp:309
raylib::Image::ColorContrast
Image & ColorContrast(float contrast)
Modify image color: contrast.
Definition: Image.hpp:367
raylib::Image::DrawRectangleLines
Image & DrawRectangleLines(::Rectangle rec, int thick, ::Color color)
Definition: Image.hpp:455
raylib::Image::ClearBackground
Image & ClearBackground(::Color color=WHITE)
Clear image background with given color.
Definition: Image.hpp:402
raylib::Image::DrawCircle
Image & DrawCircle(::Vector2 center, int radius, ::Color color)
Definition: Image.hpp:435
raylib::Image::LoadColors
inline ::Color * LoadColors()
Load color data from image as a Color array (RGBA - 32bit)
Definition: Image.hpp:492
raylib::Image::LoadRaw
void LoadRaw(const std::string &fileName, int width, int height, int format, int headerSize)
Definition: Image.hpp:153
raylib::Image::operator=
Image & operator=(const ::Image &image)
Definition: Image.hpp:139
raylib::Image::Image
Image(::Font font, const std::string &text, float fontSize, float spacing, ::Color tint)
Definition: Image.hpp:58
raylib::Image::AlphaPremultiply
Image & AlphaPremultiply()
Premultiply alpha channel.
Definition: Image.hpp:239
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:255
raylib::Image::LoadFromMemory
void LoadFromMemory(const std::string &fileType, const unsigned char *fileData, int dataSize)
Definition: Image.hpp:161
raylib::Image::GetScreenData
static Image GetScreenData()
Get pixel data from screen buffer and return an Image (screenshot)
Definition: Image.hpp:73
raylib::Image::DrawText
Image & DrawText(::Font font, const std::string &text, ::Vector2 position, float fontSize, float spacing, ::Color tint=WHITE)
Definition: Image.hpp:483
raylib::Image::Mipmaps
Image & Mipmaps()
Generate all mipmap levels for a provided image.
Definition: Image.hpp:293
raylib::Image::Image
Image(const std::string &fileName, int width, int height, int format, int headerSize)
Definition: Image.hpp:43
raylib::Image::Export
bool Export(const std::string &fileName)
Export image data to file, returns true on success.
Definition: Image.hpp:172
raylib::Image::DrawPixel
Image & DrawPixel(::Vector2 position, ::Color color)
Definition: Image.hpp:415
raylib::Rectangle
Definition: Rectangle.hpp:33
raylib::Font
Definition: Font.hpp:34
raylib::Vector2
Definition: Vector2.hpp:37
raylib::Image::Text
static Image Text(std::string text, int fontSize, Color color)
Definition: Image.hpp:62
raylib::Image::GetAlphaBorder
Rectangle GetAlphaBorder(float threshold) const
Get image alpha border rectangle.
Definition: Image.hpp:395
raylib::Color
Definition: Color.hpp:35
raylib::Image::GenGradientRadial
static Image GenGradientRadial(int width, int height, float density, Color inner, Color outer)
Generate image: radial gradient.
Definition: Image.hpp:101
raylib::Image::GenPerlinNoise
static Image GenPerlinNoise(int width, int height, int offsetX, int offsetY, float scale)
Generate image: perlin noise.
Definition: Image.hpp:124
raylib::Image::LoadAnim
void LoadAnim(const std::string &fileName, int *frames)
Definition: Image.hpp:157
raylib::Image::AlphaMask
Image & AlphaMask(Image alphaMask)
Apply alpha mask to image.
Definition: Image.hpp:223
raylib::Image::Load
void Load(const std::string &fileName)
Definition: Image.hpp:149
raylib::Image::Image
Image(const std::string &fileName, int *frames)
Definition: Image.hpp:46
raylib::Image::DrawText
Image & DrawText(const std::string &text, int x, int y, int fontSize, ::Color color=WHITE)
Definition: Image.hpp:477
raylib::Image::ColorTint
Image & ColorTint(::Color color=WHITE)
Modify image color: tint.
Definition: Image.hpp:341
raylib::Image::AlphaCrop
Image & AlphaCrop(float threshold)
Crop image depending on alpha value.
Definition: Image.hpp:231
raylib::Image::Resize
Image & Resize(int newWidth, int newHeight)
Resize and image to new size.
Definition: Image.hpp:269
raylib::Image::GenCellular
static Image GenCellular(int width, int height, int tileSize)
Generate image: cellular algorithm.
Definition: Image.hpp:131
raylib::Image::Copy
Image Copy()
Create an image duplicate (useful for transformations)
Definition: Image.hpp:193
raylib::Image::Text
static Image Text(::Font font, std::string text, float fontSize, float spacing, ::Color tint)
Definition: Image.hpp:66
raylib::Image::Draw
Image & Draw(const ::Image &src, ::Rectangle srcRec, ::Rectangle dstRec, ::Color tint=WHITE)
Definition: Image.hpp:460
raylib::Image::ResizeCanvas
Image & ResizeCanvas(int newWidth, int newHeight, int offsetX, int offsetY, Color color)
Resize canvas and fill with color.
Definition: Image.hpp:285
raylib::Image::Unload
void Unload()
Definition: Image.hpp:165
raylib::Texture
Definition: Texture.hpp:36
raylib::Image::FlipHorizontal
Image & FlipHorizontal()
Flip image horizontally.
Definition: Image.hpp:317
raylib::Image::DrawCircle
Image & DrawCircle(int centerX, int centerY, int radius, ::Color color)
Definition: Image.hpp:430
raylib::Image::LoadTexture
inline ::Texture2D LoadTexture()
Load texture from image data.
Definition: Image.hpp:506
raylib::Image::ResizeNN
Image & ResizeNN(int newWidth, int newHeight)
Resize and image to new size using Nearest-Neighbor scaling algorithm.
Definition: Image.hpp:277
raylib::Image::Image
Image(int width, int height, Color color=WHITE)
Definition: Image.hpp:55
raylib::Image::operator=
Image & operator=(const Image &image)
Definition: Image.hpp:144
raylib::Image::GenChecked
static Image GenChecked(int width, int height, int checksX, int checksY, Color col1, Color col2)
Generate image: checked.
Definition: Image.hpp:109
raylib::Image::RotateCW
Image & RotateCW()
Rotate image clockwise 90deg.
Definition: Image.hpp:325
raylib::Image::Image
Image()
Definition: Image.hpp:36
raylib::Image::DrawLine
Image & DrawLine(::Vector2 start, ::Vector2 end, ::Color color)
Definition: Image.hpp:425
raylib::Image::ColorReplace
Image & ColorReplace(::Color color, ::Color replace)
Modify image color: replace color.
Definition: Image.hpp:385
raylib::Image::DrawRectangle
Image & DrawRectangle(::Rectangle rec, ::Color color=WHITE)
Definition: Image.hpp:450
raylib::Image::FromImage
Image FromImage(::Rectangle rec)
Create an image from another image piece.
Definition: Image.hpp:200
raylib::Image::GenColor
static Image GenColor(int width, int height, Color color)
Generate image: plain color.
Definition: Image.hpp:80