1 #ifndef RAYLIB_CPP_INCLUDE_IMAGE_HPP_
2 #define RAYLIB_CPP_INCLUDE_IMAGE_HPP_
6 #include "./raylib.hpp"
7 #include "./raylib-cpp-utils.hpp"
8 #include "./RaylibException.hpp"
19 Image(
void* data =
nullptr,
23 int format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8) :
::Image{data, width, height, mipmaps, format} {
27 Image(const ::Image& image) {
38 Image(
const std::string& fileName) {
49 Image(
const std::string& fileName,
int width,
int height,
int format,
int headerSize = 0) {
50 Load(fileName, width, height, format, headerSize);
60 Image(
const std::string& fileName,
int* frames) {
61 Load(fileName, frames);
69 Image(
const std::string& fileType,
const unsigned char* fileData,
int dataSize) {
70 Load(fileType, fileData, dataSize);
78 Image(const ::Texture2D& texture) {
82 Image(
int width,
int height, ::
Color color = {255, 255, 255, 255}) {
83 set(::GenImageColor(width, height, color));
86 Image(
const std::string& text,
int fontSize, ::
Color color = {255, 255, 255, 255}) {
87 set(::ImageText(text.c_str(), fontSize, color));
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));
95 Image(
const Image& other) {
99 Image(Image&& other) {
102 other.data =
nullptr;
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);
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);
123 return ::LoadImageFromScreen();
129 static ::Image
Color(
int width,
int height, ::
Color color = {255, 255, 255, 255}) {
130 return ::GenImageColor(width, height, color);
137 return ::GenImageGradientV(width, height, top, bottom);
144 return ::GenImageGradientH(width, height, left, right);
152 return ::GenImageGradientRadial(width, height, density, inner, outer);
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);
166 static ::Image
WhiteNoise(
int width,
int height,
float factor) {
167 return ::GenImageWhiteNoise(width, height, factor);
173 static ::Image
Cellular(
int width,
int height,
int tileSize) {
174 return ::GenImageCellular(width, height, tileSize);
181 Image& operator=(const ::Image& image) {
186 Image& operator=(
const Image& other) {
187 if (
this == &other) {
197 Image& operator=(Image&& other) noexcept {
198 if (
this == &other) {
205 other.data =
nullptr;
221 void Load(
const std::string& fileName) {
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));
249 void Load(
const std::string& fileName,
int* frames) {
264 const std::string& fileType,
265 const unsigned char *fileData,
269 throw RaylibException(
"Failed to load Image data with file type: " + fileType);
280 void Load(const ::Texture2D& texture) {
281 set(::LoadImageFromTexture(texture));
291 if (data !=
nullptr) {
292 ::UnloadImage(*
this);
302 inline void Export(
const std::string& fileName)
const {
304 throw RaylibException(TextFormat(
"Failed to export Image to file: %s", fileName.c_str()));
315 throw RaylibException(TextFormat(
"Failed to export Image code to file: %s", fileName.c_str()));
319 GETTERSETTER(
void*, Data, data)
320 GETTERSETTER(
int, Width, width)
321 GETTERSETTER(
int, Height, height)
329 return {
static_cast<float>(width),
static_cast<float>(height)};
336 return ::ImageCopy(*
this);
343 return ::ImageFromImage(*
this, rec);
350 ::ImageFormat(
this, newFormat);
358 ::ImageToPOT(
this, fillColor);
366 ::ImageCrop(
this, crop);
374 ::ImageAlphaCrop(
this, threshold);
382 ::ImageAlphaClear(
this, color, threshold);
390 ::ImageAlphaMask(
this, alphaMask);
398 ::ImageAlphaPremultiply(
this);
406 return Crop(0, 0, newWidth, newHeight);
413 return Crop(0, 0,
static_cast<int>(size.x),
static_cast<int>(size.y));
419 inline Image&
Crop(
int offsetX,
int offsetY,
int newWidth,
int newHeight) {
421 static_cast<float>(offsetX),
422 static_cast<float>(offsetY),
423 static_cast<float>(newWidth),
424 static_cast<float>(newHeight)
426 ::ImageCrop(
this, rect);
434 ::ImageResize(
this, newWidth, newHeight);
442 ::ImageResizeNN(
this, newWidth, newHeight);
450 ::
Color color = {255, 255, 255, 255}) {
451 ::ImageResizeCanvas(
this, newWidth, newHeight, offsetX, offsetY, color);
459 ::ImageMipmaps(
this);
467 ::ImageDither(
this, rBpp, gBpp, bBpp, aBpp);
475 ::ImageFlipVertical(
this);
483 ::ImageFlipHorizontal(
this);
491 ::ImageRotateCW(
this);
499 ::ImageRotateCCW(
this);
507 ::ImageColorTint(
this, color);
515 ::ImageColorInvert(
this);
523 ::ImageColorGrayscale(
this);
533 ::ImageColorContrast(
this, contrast);
543 ::ImageColorBrightness(
this, brightness);
551 ::ImageColorReplace(
this, color, replace);
561 return ::GetImageAlphaBorder(*
this, threshold);
568 return ::GetImageColor(*
this, x, y);
575 return ::GetImageColor(*
this,
static_cast<int>(position.x),
static_cast<int>(position.y));
582 ::ImageClearBackground(
this, color);
589 inline void DrawPixel(
int posX,
int posY, ::
Color color = {255, 255, 255, 255}) {
590 ::ImageDrawPixel(
this, posX, posY, color);
593 inline void DrawPixel(::Vector2 position, ::
Color color = {255, 255, 255, 255}) {
594 ::ImageDrawPixelV(
this, position, color);
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);
602 inline void DrawLine(::Vector2 start, ::Vector2 end, ::
Color color = {255, 255, 255, 255}) {
603 ::ImageDrawLineV(
this, start, end, color);
606 inline void DrawCircle(
int centerX,
int centerY,
int radius,
607 ::
Color color = {255, 255, 255, 255}) {
608 ::ImageDrawCircle(
this, centerX, centerY, radius, color);
611 inline void DrawCircle(::Vector2 center,
int radius,
612 ::
Color color = {255, 255, 255, 255}) {
613 ::ImageDrawCircleV(
this, center, radius, color);
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);
621 inline void DrawRectangle(Vector2 position, Vector2 size,
622 ::
Color color = {255, 255, 255, 255}) {
623 ::ImageDrawRectangleV(
this, position, size, color);
626 inline void DrawRectangle(::Rectangle rec, ::
Color color = {255, 255, 255, 255}) {
627 ::ImageDrawRectangleRec(
this, rec, color);
630 inline void DrawRectangleLines(::Rectangle rec,
int thick = 1,
631 ::
Color color = {255, 255, 255, 255}) {
632 ::ImageDrawRectangleLines(
this, rec, thick, color);
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);
640 inline void DrawText(
const std::string& text, ::Vector2 position,
int fontSize,
641 ::
Color color = {255, 255, 255, 255}) {
642 ::ImageDrawText(
this,
644 static_cast<int>(position.x),
645 static_cast<int>(position.y),
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);
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);
664 return ::LoadImageColors(*
this);
670 inline ::Color*
LoadPalette(
int maxPaletteSize,
int *colorsCount)
const {
671 return ::LoadImagePalette(*
this, maxPaletteSize, colorsCount);
678 ::UnloadImageColors(colors);
685 ::UnloadImagePalette(colors);
692 return ::LoadTextureFromImage(*
this);
700 inline operator ::Texture2D() {
707 static int GetPixelDataSize(
int width,
int height,
int format = PIXELFORMAT_UNCOMPRESSED_R32G32B32A32) {
708 return ::GetPixelDataSize(width, height, format);
717 return ::GetPixelDataSize(width, height, format);
726 return data !=
nullptr;
730 void set(const ::Image& image) {
733 height = image.height;
734 mipmaps = image.mipmaps;
735 format = image.format;
Image type, bpp always RGBA (32bit)
Image & ResizeCanvas(int newWidth, int newHeight, int offsetX=0, int offsetY=0, ::Color color={255, 255, 255, 255})
Resize canvas and fill with color.
Image & Format(int newFormat)
Convert image data to desired format.
Image & ColorTint(::Color color={255, 255, 255, 255})
Modify image color: tint.
void Load(const std::string &fileName, int *frames)
Load image sequence from file (frames appended to image.data).
Image & Dither(int rBpp, int gBpp, int bBpp, int aBpp)
Dither image data to 16bpp or lower (Floyd-Steinberg dithering)
Image & FlipVertical()
Flip image vertically.
::Image WhiteNoise(int width, int height, float factor)
Generate image: white noise.
Image & ResizeNN(int newWidth, int newHeight)
Resize and image to new size using Nearest-Neighbor scaling algorithm.
::Image GradientH(int width, int height, ::Color left, ::Color right)
Generate image: horizontal gradient.
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.
Image & Crop(int newWidth, int newHeight)
Crop an image to a new given width and height.
void Load(const std::string &fileName, int width, int height, int format, int headerSize)
Load image from RAW file data.
Image & Crop(int offsetX, int offsetY, int newWidth, int newHeight)
Crop an image to area defined by a rectangle.
Image & ColorBrightness(int brightness)
Modify image color: brightness.
Image & ColorGrayscale()
Modify image color: grayscale.
Image & Crop(::Vector2 size)
Crop an image to a new given width and height based on a vector.
::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.
void Load(const ::Texture2D &texture)
Load an image from the given file.
::Image Cellular(int width, int height, int tileSize)
Generate image: cellular algorithm.
Image(const std::string &fileType, const unsigned char *fileData, int dataSize)
Load an image from the given file.
void Load(const std::string &fileName)
Load image from file into CPU memory (RAM)
Image & AlphaClear(::Color color, float threshold)
Clear alpha channel to desired color.
Image & AlphaMask(const ::Image &alphaMask)
Apply alpha mask to image.
Image(const ::Texture2D &texture)
Load an image from the given file.
Rectangle GetAlphaBorder(float threshold) const
Get image alpha border rectangle.
inline ::Image Copy() const
Create an image duplicate (useful for transformations)
int GetPixelDataSize() const
Returns the pixel data size based on the current image.
Image & Crop(::Rectangle crop)
Crop an image to area defined by a rectangle.
::Image GradientV(int width, int height, ::Color top, ::Color bottom)
Generate image: vertical gradient.
Image & AlphaCrop(float threshold)
Crop image depending on alpha value.
raylib::Color GetColor(::Vector2 position) const
Get image pixel color at vector position.
bool IsReady() const
Retrieve whether or not the Image has been loaded.
Image & FlipHorizontal()
Flip image horizontally.
inline ::Image FromImage(::Rectangle rec) const
Create an image from another image piece.
Image & Resize(int newWidth, int newHeight)
Resize and image to new size.
inline ::Color * LoadColors() const
Load color data from image as a Color array (RGBA - 32bit)
Image(const std::string &fileName, int *frames)
Load an animation image from the given file.
Image(const std::string &fileName)
Load an image from the given file.
inline ::Color * LoadPalette(int maxPaletteSize, int *colorsCount) const
Load colors palette from image as a Color array (RGBA - 32bit)
::Image Color(int width, int height, ::Color color={255, 255, 255, 255})
Generate image: plain color.
raylib::Color GetColor(int x=0, int y=0) const
Get image pixel color at (x, y) position.
void Load(const std::string &fileType, const unsigned char *fileData, int dataSize)
Load image from memory buffer, fileType refers to extension: i.e.
Image & RotateCCW()
Rotate image counter-clockwise 90deg.
inline ::Texture2D LoadTexture() const
Load texture from image data.
static int GetPixelDataSize(int width, int height, int format=PIXELFORMAT_UNCOMPRESSED_R32G32B32A32)
Get pixel data size in bytes for certain format.
::Image GradientRadial(int width, int height, float density, ::Color inner, ::Color outer)
Generate image: radial gradient.
Image & Mipmaps()
Generate all mipmap levels for a provided image.
::Image LoadFromScreen()
Get pixel data from screen buffer and return an Image (screenshot)
inline ::Vector2 GetSize() const
Retrieve the width and height of the image.
void Unload()
Unload image from CPU memory (RAM)
void UnloadColors(::Color *colors) const
Unload color data loaded with LoadImageColors()
void Export(const std::string &fileName) const
Export image data to file, returns true on success.
Image & AlphaPremultiply()
Premultiply alpha channel.
void ExportAsCode(const std::string &fileName) const
Export image as code file defining an array of bytes, returns true on success.
void DrawPixel(int posX, int posY, ::Color color={255, 255, 255, 255})
Draw pixel within an image.
void UnloadPalette(::Color *colors) const
Unload colors palette loaded with LoadImagePalette()
Image & ToPOT(::Color fillColor)
Convert image to POT (power-of-two)
Image & RotateCW()
Rotate image clockwise 90deg.
Image & ClearBackground(::Color color={0, 0, 0, 255})
Clear image background with given color.
Image & ColorContrast(float contrast)
Modify image color: contrast.
Image & ColorInvert()
Modify image color: invert.
Image & ColorReplace(::Color color, ::Color replace)
Modify image color: replace color.
Exception used for most raylib-related exceptions.
All raylib-cpp classes and functions appear in the raylib namespace.
static bool ExportImageAsCode(const Image &image, const std::string &fileName)
Export image as code file (.h) defining an array of bytes.
static inline ::Image LoadImage(const std::string &fileName)
Load an image.
static bool ExportImage(const Image &image, const std::string &fileName)
Export image data to file.
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".
static inline ::Image LoadImageAnim(const std::string &fileName, int *frames)
Load animated image data.
static inline ::Image LoadImageRaw(const std::string &fileName, int width, int height, int format, int headerSize)
Load an image from RAW file data.