1#ifndef RAYLIB_CPP_INCLUDE_IMAGE_HPP_
2#define RAYLIB_CPP_INCLUDE_IMAGE_HPP_
9#include "./raylib-cpp-utils.hpp"
10#include "./RaylibException.hpp"
21 Image(
void* data =
nullptr,
25 int format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8) : ::Image{data, width, height, mipmaps, format} {
29 Image(const ::Image& image) {
40 Image(
const std::string_view fileName) {
51 Image(
const std::string_view fileName,
int width,
int height,
int format,
int headerSize = 0) {
52 Load(fileName, width, height, format, headerSize);
62 Image(
const std::string_view fileName,
int* frames) {
63 Load(fileName, frames);
73 Image(
const std::string_view fileName, std::span<int> frames) {
74 Load(fileName, frames.data());
82 Image(
const std::string_view fileType,
const unsigned char* fileData,
int dataSize) {
83 Load(fileType, fileData, dataSize);
91 Image(const ::Texture2D& texture) {
95 Image(
int width,
int height, ::Color color = {255, 255, 255, 255}) {
96 set(::GenImageColor(width, height, color));
99 Image(
const std::string_view text,
int fontSize, ::Color color = {255, 255, 255, 255}) {
100 set(::ImageText(text.data(), fontSize, color));
103 Image(const ::Font& font,
const std::string_view text,
float fontSize,
float spacing,
104 ::Color tint = {255, 255, 255, 255}) {
105 set(::ImageTextEx(font, text.data(), fontSize, spacing, tint));
108 Image(
const Image& other) {
112 Image(Image&& other) {
115 other.data =
nullptr;
122 static ::Image Text(
const std::string_view text,
int fontSize,
123 ::Color color = {255, 255, 255, 255}) {
124 return ::ImageText(text.data(), fontSize, color);
127 static ::Image Text(const ::Font& font,
const std::string_view text,
float fontSize,
float spacing,
128 ::Color tint = {255, 255, 255, 255}) {
129 return ::ImageTextEx(font, text.data(), fontSize, spacing, tint);
136 return ::LoadImageFromScreen();
142 static ::Image
Color(
int width,
int height, ::Color color = {255, 255, 255, 255}) {
143 return ::GenImageColor(width, height, color);
149 static ::Image
GradientLinear(
int width,
int height,
int direction, ::Color start, ::Color end) {
150 return ::GenImageGradientLinear(width, height, direction, start, end);
157 ::Color inner, ::Color outer) {
158 return ::GenImageGradientRadial(width, height, density, inner, outer);
164 static ::Image
Checked(
int width,
int height,
int checksX,
int checksY,
165 ::Color col1 = {255, 255, 255, 255}, ::Color col2 = {0, 0, 0, 255}) {
166 return ::GenImageChecked(width, height, checksX, checksY, col1, col2);
172 static ::Image
WhiteNoise(
int width,
int height,
float factor) {
173 return ::GenImageWhiteNoise(width, height, factor);
179 static ::Image
Cellular(
int width,
int height,
int tileSize) {
180 return ::GenImageCellular(width, height, tileSize);
187 Image& operator=(const ::Image& image) {
192 Image& operator=(
const Image& other) {
193 if (
this == &other) {
203 Image& operator=(Image&& other)
noexcept {
204 if (
this == &other) {
211 other.data =
nullptr;
227 void Load(
const std::string_view fileName) {
228 set(::LoadImage(fileName.data()));
230 throw RaylibException(
"Failed to load Image from file: " + std::string(fileName));
241 void Load(
const std::string_view fileName,
int width,
int height,
int format,
int headerSize) {
242 set(::LoadImageRaw(fileName.data(), width, height, format, headerSize));
244 throw RaylibException(
"Failed to load Image from file: " + std::string(fileName));
255 void Load(
const std::string_view fileName,
int* frames) {
256 set(::LoadImageAnim(fileName.data(), frames));
258 throw RaylibException(
"Failed to load Image from file: " + std::string(fileName));
269 void Load(
const std::string_view fileName, std::span<int> frames) {
270 set(::LoadImageAnim(fileName.data(), frames.data()));
272 throw RaylibException(
"Failed to load Image from file: " + std::string(fileName));
284 const std::string_view fileType,
285 const unsigned char *fileData,
287 set(::LoadImageFromMemory(fileType.data(), fileData, dataSize));
289 throw RaylibException(
"Failed to load Image data with file type: " + std::string(fileType));
301 const std::string_view fileType,
302 const std::span<unsigned char> fileData) {
303 set(::LoadImageFromMemory(fileType.data(), fileData.data(),
static_cast<int>(fileData.size())));
305 throw RaylibException(
"Failed to load Image data with file type: " + std::string(fileType));
316 void Load(const ::Texture2D& texture) {
317 set(::LoadImageFromTexture(texture));
327 if (data !=
nullptr) {
328 ::UnloadImage(*
this);
338 void Export(
const std::string_view fileName)
const {
339 if (!::ExportImage(*
this, fileName.data())) {
340 throw RaylibException(TextFormat(
"Failed to export Image to file: %s", fileName.data()));
348 return ::ExportImageToMemory(*
this, fileType, fileSize);
351 unsigned char*
ExportToMemory(
const std::string_view fileType, std::span<int> fileSize) {
352 return ::ExportImageToMemory(*
this, fileType.data(), fileSize.data());
361 if (!::ExportImageAsCode(*
this, fileName.data())) {
362 throw RaylibException(TextFormat(
"Failed to export Image code to file: %s", fileName.data()));
366 GETTER(
void*, Data, data)
367 GETTER(
int, Width, width)
368 GETTER(
int, Height, height)
377 void SetWidth(
int width,
int offsetX = 0,
int offsetY = 0, ::
Color fill = {255, 255, 255, 255}) {
386 void SetHeight(
int height,
int offsetX = 0,
int offsetY = 0, ::Color fill = {255, 255, 255, 255}) {
394 return {
static_cast<float>(width),
static_cast<float>(height)};
401 return ::ImageCopy(*
this);
408 return ::ImageFromImage(*
this, rec);
415 ::ImageFormat(
this, newFormat);
423 ::ImageToPOT(
this, fillColor);
431 ::ImageCrop(
this, crop);
439 ::ImageAlphaCrop(
this, threshold);
447 ::ImageAlphaClear(
this, color, threshold);
455 ::ImageAlphaMask(
this, alphaMask);
463 ::ImageAlphaPremultiply(
this);
471 return Crop(0, 0, newWidth, newHeight);
478 return Crop(0, 0,
static_cast<int>(size.x),
static_cast<int>(size.y));
484 Image&
Crop(
int offsetX,
int offsetY,
int newWidth,
int newHeight) {
486 static_cast<float>(offsetX),
487 static_cast<float>(offsetY),
488 static_cast<float>(newWidth),
489 static_cast<float>(newHeight)
491 ::ImageCrop(
this, rect);
499 ::ImageResize(
this, newWidth, newHeight);
507 ::ImageResizeNN(
this, newWidth, newHeight);
515 ::Color color = {255, 255, 255, 255}) {
516 ::ImageResizeCanvas(
this, newWidth, newHeight, offsetX, offsetY, color);
524 ::ImageMipmaps(
this);
532 ::ImageDither(
this, rBpp, gBpp, bBpp, aBpp);
540 ::ImageFlipVertical(
this);
548 ::ImageFlipHorizontal(
this);
558 ::ImageRotate(
this,
static_cast<int>(degrees));
566 ::ImageRotateCW(
this);
574 ::ImageRotateCCW(
this);
582 ::ImageColorTint(
this, color);
590 ::ImageColorInvert(
this);
598 ::ImageColorGrayscale(
this);
608 ::ImageColorContrast(
this, contrast);
618 ::ImageColorBrightness(
this, brightness);
626 ::ImageColorReplace(
this, color, replace);
636 return ::GetImageAlphaBorder(*
this, threshold);
643 return ::GetImageColor(*
this, x, y);
650 return ::GetImageColor(*
this,
static_cast<int>(position.x),
static_cast<int>(position.y));
657 ::ImageClearBackground(
this, color);
664 void DrawPixel(
int posX,
int posY, ::Color color = {255, 255, 255, 255}) {
665 ::ImageDrawPixel(
this, posX, posY, color);
668 void DrawPixel(::Vector2 position, ::Color color = {255, 255, 255, 255}) {
669 ::ImageDrawPixelV(
this, position, color);
672 void DrawLine(
int startPosX,
int startPosY,
int endPosX,
int endPosY,
673 ::Color color = {255, 255, 255, 255}) {
674 ::ImageDrawLine(
this, startPosX, startPosY, endPosX, endPosY, color);
677 void DrawLine(::Vector2 start, ::Vector2 end, ::Color color = {255, 255, 255, 255}) {
678 ::ImageDrawLineV(
this, start, end, color);
681 void DrawCircle(
int centerX,
int centerY,
int radius,
682 ::Color color = {255, 255, 255, 255}) {
683 ::ImageDrawCircle(
this, centerX, centerY, radius, color);
686 void DrawCircle(::Vector2 center,
int radius,
687 ::Color color = {255, 255, 255, 255}) {
688 ::ImageDrawCircleV(
this, center, radius, color);
691 void DrawRectangle(
int posX,
int posY,
int width,
int height,
692 ::Color color = {255, 255, 255, 255}) {
693 ::ImageDrawRectangle(
this, posX, posY, width, height, color);
696 void DrawRectangle(Vector2 position, Vector2 size,
697 ::Color color = {255, 255, 255, 255}) {
698 ::ImageDrawRectangleV(
this, position, size, color);
701 void DrawRectangle(::Rectangle rec, ::Color color = {255, 255, 255, 255}) {
702 ::ImageDrawRectangleRec(
this, rec, color);
705 void DrawRectangleLines(::Rectangle rec,
int thick = 1,
706 ::Color color = {255, 255, 255, 255}) {
707 ::ImageDrawRectangleLines(
this, rec, thick, color);
710 void Draw(const ::Image& src, ::Rectangle srcRec, ::Rectangle dstRec,
711 ::Color tint = {255, 255, 255, 255}) {
712 ::ImageDraw(
this, src, srcRec, dstRec, tint);
715 void DrawText(
const char* text, ::Vector2 position,
int fontSize,
716 ::Color color = {255, 255, 255, 255}) {
717 ::ImageDrawText(
this,
719 static_cast<int>(position.x),
720 static_cast<int>(position.y),
725 void DrawText(
const std::string_view text, ::Vector2 position,
int fontSize,
726 ::Color color = {255, 255, 255, 255}) {
727 ::ImageDrawText(
this,
729 static_cast<int>(position.x),
730 static_cast<int>(position.y),
735 void DrawText(
const std::string_view text,
int x,
int y,
int fontSize,
736 ::Color color = {255, 255, 255, 255}) {
737 ::ImageDrawText(
this, text.data(), x, y, fontSize, color);
740 void DrawText(
const char* text,
int x,
int y,
int fontSize,
741 ::Color color = {255, 255, 255, 255}) {
742 ::ImageDrawText(
this, text, x, y, fontSize, color);
745 void DrawText(const ::Font& font,
const std::string_view text, ::Vector2 position,
746 float fontSize,
float spacing, ::Color tint = {255, 255, 255, 255}) {
747 ::ImageDrawTextEx(
this, font, text.data(), position, fontSize, spacing, tint);
750 void DrawText(const ::Font& font,
const char* text, ::Vector2 position,
751 float fontSize,
float spacing, ::Color tint = {255, 255, 255, 255}) {
752 ::ImageDrawTextEx(
this, font, text, position, fontSize, spacing, tint);
759 return ::LoadImageColors(*
this);
765 ::Color*
LoadPalette(
int maxPaletteSize,
int *colorsCount)
const {
766 return ::LoadImagePalette(*
this, maxPaletteSize, colorsCount);
773 ::UnloadImageColors(colors);
780 ::UnloadImagePalette(colors);
787 return ::LoadTextureFromImage(*
this);
795 operator ::Texture2D() {
802 static int GetPixelDataSize(
int width,
int height,
int format = PIXELFORMAT_UNCOMPRESSED_R32G32B32A32) {
803 return ::GetPixelDataSize(width, height, format);
812 return ::GetPixelDataSize(width, height, format);
821 return ::IsImageReady(*
this);
825 void set(const ::Image& image) {
828 height = image.height;
829 mipmaps = image.mipmaps;
830 format = image.format;
Degree type (allows automatic worry free conversion between radians and degrees)
Image type, bpp always RGBA (32bit)
void Load(const std::string_view fileName, int width, int height, int format, int headerSize)
Load image from RAW file data.
void Load(const std::string_view fileType, const std::span< unsigned char > fileData)
Load image from memory buffer, fileType refers to extension: i.e.
Image(const std::string_view fileName, int *frames)
Load an animation image from the given file.
Image & ColorContrast(float contrast)
Modify image color: contrast.
Image & Dither(int rBpp, int gBpp, int bBpp, int aBpp)
Dither image data to 16bpp or lower (Floyd-Steinberg dithering)
::Image WhiteNoise(int width, int height, float factor)
Generate image: white noise.
Image & ColorTint(::Color color={255, 255, 255, 255})
Modify image color: tint.
Image & ResizeNN(int newWidth, int newHeight)
Resize and image to new size using Nearest-Neighbor scaling algorithm.
Image & Rotate(Degree degrees)
Rotate image by input angle in degrees (-359 to 359)
Image & ResizeCanvas(int newWidth, int newHeight, int offsetX=0, int offsetY=0, ::Color color={255, 255, 255, 255})
Resize canvas and fill with color.
::Color * LoadColors() const
Load color data from image as a Color array (RGBA - 32bit)
::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 ::Texture2D &texture)
Load an image from the given file.
Rectangle GetAlphaBorder(float threshold) const
Get image alpha border rectangle.
::Image FromImage(::Rectangle rec) const
Create an image from another image piece.
int GetPixelDataSize() const
Returns the pixel data size based on the current image.
Image(const std::string_view fileName, std::span< int > frames)
Load an animation image from the given file.
operator::Texture2D()
Loads a texture from the image data.
Image(const std::string_view fileName)
Load an image from the given file.
void Load(const std::string_view fileType, const unsigned char *fileData, int dataSize)
Load image from memory buffer, fileType refers to extension: i.e.
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.
void SetHeight(int height, int offsetX=0, int offsetY=0, ::Color fill={255, 255, 255, 255})
Set the height of the image canvas.
::Texture2D LoadTexture() const
Load texture from image data.
Image & ColorGrayscale()
Modify image color: grayscale.
void ExportAsCode(const std::string_view fileName) const
Export image as code file defining an array of bytes, returns true on success.
::Color * LoadPalette(int maxPaletteSize, int *colorsCount) const
Load colors palette from image as a Color array (RGBA - 32bit)
Image & ColorBrightness(int brightness)
Modify image color: brightness.
Image & RotateCW()
Rotate image clockwise 90deg.
Image & FlipHorizontal()
Flip image horizontally.
Image & AlphaPremultiply()
Premultiply alpha channel.
Image & ToPOT(::Color fillColor)
Convert image to POT (power-of-two)
Image & ClearBackground(::Color color={0, 0, 0, 255})
Clear image background with given color.
Image & Crop(int offsetX, int offsetY, int newWidth, int newHeight)
Crop an image to area defined by a rectangle.
void Load(const std::string_view fileName, int *frames)
Load image sequence from file (frames appended to image.data).
::Image Color(int width, int height, ::Color color={255, 255, 255, 255})
Generate image: plain color.
Image & FlipVertical()
Flip image vertically.
Image & RotateCCW()
Rotate image counter-clockwise 90deg.
void Load(const std::string_view fileName)
Load image from file into CPU memory (RAM)
::Image Copy() const
Create an image duplicate (useful for transformations)
raylib::Color GetColor(int x=0, int y=0) const
Get image pixel color at (x, y) position.
Image & Format(int newFormat)
Convert image data to desired format.
Image & AlphaCrop(float threshold)
Crop image depending on alpha value.
::Vector2 GetSize() const
Retrieve the width and height of the image.
static int GetPixelDataSize(int width, int height, int format=PIXELFORMAT_UNCOMPRESSED_R32G32B32A32)
Get pixel data size in bytes for certain format.
unsigned char * ExportToMemory(const char *fileType, int *fileSize)
Export image to memory buffer.
Image(const std::string_view 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 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 & AlphaMask(const ::Image &alphaMask)
Apply alpha mask to image.
::Image LoadFromScreen()
Get pixel data from screen buffer and return an Image (screenshot)
Image & ColorInvert()
Modify image color: invert.
void Unload()
Unload image from CPU memory (RAM)
Image & Crop(int newWidth, int newHeight)
Crop an image to a new given width and height.
Image(const std::string_view fileType, const unsigned char *fileData, int dataSize)
Load an image from the given file.
void Export(const std::string_view fileName) const
Export image data to file, returns true on success.
void UnloadColors(::Color *colors) const
Unload color data loaded with LoadImageColors()
void SetWidth(int width, int offsetX=0, int offsetY=0, ::Color fill={255, 255, 255, 255})
Set the width of the image canvas.
::Image GradientLinear(int width, int height, int direction, ::Color start, ::Color end)
Generate image: linear gradient.
Image & ColorReplace(::Color color, ::Color replace)
Modify image color: replace color.
Image & AlphaClear(::Color color, float threshold)
Clear alpha channel to desired color.
Image & Crop(::Rectangle crop)
Crop an image to area defined by a rectangle.
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 & Resize(int newWidth, int newHeight)
Resize and image to new size.
Image & Crop(::Vector2 size)
Crop an image to a new given width and height based on a vector.
void Load(const std::string_view fileName, std::span< int > frames)
Load image sequence from file (frames appended to image.data).
Exception used for most raylib-related exceptions.