1#ifndef RAYLIB_CPP_INCLUDE_IMAGE_HPP_
2#define RAYLIB_CPP_INCLUDE_IMAGE_HPP_
9#include "./RaylibException.hpp"
10#include "./raylib-cpp-utils.hpp"
11#include "./raylib.hpp"
26 int format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
27 : ::Image{data, width, height, mipmaps, format} {
31 Image(const ::Image& image) : ::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) {
Load(texture); }
93 Image(
int width,
int height, ::Color color = {255, 255, 255, 255}) { set(::GenImageColor(width, height, color)); }
95 Image(
const std::string_view text,
int fontSize, ::Color color = {255, 255, 255, 255}) {
96 set(::ImageText(text.data(), fontSize, color));
99 Image(const ::Font& font,
const std::string_view text,
float fontSize,
float spacing,
100 ::Color tint = {255, 255, 255, 255}) {
101 set(::ImageTextEx(font, text.data(), fontSize, spacing, tint));
104 Image(
const Image& other) { set(other.Copy()); }
106 Image(Image&& other)
noexcept {
109 other.data =
nullptr;
116 static ::Image Text(
const std::string_view text,
int fontSize,
117 ::Color color = {255, 255, 255, 255}) {
118 return ::ImageText(text.data(), fontSize, color);
121 static ::Image Text(const ::Font& font,
const std::string_view text,
float fontSize,
float spacing,
122 ::Color tint = {255, 255, 255, 255}) {
123 return ::ImageTextEx(font, text.data(), fontSize, spacing, tint);
134 static ::Image
Color(
int width,
int height, ::Color color = {255, 255, 255, 255}) {
135 return ::GenImageColor(width, height, color);
141 static ::Image
GradientLinear(
int width,
int height,
int direction, ::Color start, ::Color end) {
142 return ::GenImageGradientLinear(width, height, direction, start, end);
148 static ::Image
GradientRadial(
int width,
int height,
float density, ::Color inner, ::Color outer) {
149 return ::GenImageGradientRadial(width, height, density, inner, outer);
160 ::Color col1 = {255, 255, 255, 255},
161 ::Color col2 = {0, 0, 0, 255}) {
162 return ::GenImageChecked(width, height, checksX, checksY, col1, col2);
168 static ::Image
WhiteNoise(
int width,
int height,
float factor) {
169 return ::GenImageWhiteNoise(width, height, factor);
175 static ::Image
Cellular(
int width,
int height,
int tileSize) { return ::GenImageCellular(width, height, tileSize); }
184 Image& operator=(const ::Image& image) {
189 Image& operator=(
const Image& other) {
190 if (
this == &other) {
200 Image& operator=(Image&& other)
noexcept {
201 if (
this == &other) {
208 other.data =
nullptr;
224 void Load(
const std::string_view fileName) {
227 throw RaylibException(
"Failed to load Image from file: " + std::string(fileName));
238 void Load(
const std::string_view fileName,
int width,
int height,
int format,
int headerSize) {
239 set(
::LoadImageRaw(fileName.data(), width, height, format, headerSize));
241 throw RaylibException(
"Failed to load Image from file: " + std::string(fileName));
252 void Load(
const std::string_view fileName,
int* frames) {
255 throw RaylibException(
"Failed to load Image from file: " + std::string(fileName));
266 void Load(
const std::string_view fileName, std::span<int> frames) {
269 throw RaylibException(
"Failed to load Image from file: " + std::string(fileName));
281 const std::string_view fileType,
282 const unsigned char *fileData,
286 throw RaylibException(
"Failed to load Image data with file type: " + std::string(fileType));
298 const std::string_view fileType,
299 const std::span<unsigned char> fileData) {
302 throw RaylibException(
"Failed to load Image data with file type: " + std::string(fileType));
313 void Load(const ::Texture2D& texture) {
314 set(::LoadImageFromTexture(texture));
324 if (data !=
nullptr) {
325 ::UnloadImage(*
this);
335 void Export(
const std::string_view fileName)
const {
337 throw RaylibException(TextFormat(
"Failed to export Image to file: %s", fileName.data()));
345 return ::ExportImageToMemory(*
this, fileType, fileSize);
348 unsigned char*
ExportToMemory(
const std::string_view fileType, std::span<int> fileSize) {
349 return ::ExportImageToMemory(*
this, fileType.data(), fileSize.data());
359 throw RaylibException(TextFormat(
"Failed to export Image code to file: %s", fileName.data()));
363 GETTER(
void*, Data, data)
364 GETTER(
int, Width, width)
365 GETTER(
int, Height, height)
374 void SetWidth(
int width,
int offsetX = 0,
int offsetY = 0, ::
Color fill = {255, 255, 255, 255}) {
383 void SetHeight(
int height,
int offsetX = 0,
int offsetY = 0, ::Color fill = {255, 255, 255, 255}) {
390 [[nodiscard]] ::Vector2
GetSize()
const {
return {
static_cast<float>(width),
static_cast<float>(height)}; }
395 [[nodiscard]] ::Image
Copy()
const { return ::ImageCopy(*
this); }
400 [[nodiscard]] ::Image
FromImage(::Rectangle rec)
const { return ::ImageFromImage(*
this, rec); }
406 ::ImageFormat(
this, newFormat);
414 ::ImageToPOT(
this, fillColor);
422 ::ImageCrop(
this, crop);
430 ::ImageAlphaCrop(
this, threshold);
438 ::ImageAlphaClear(
this, color, threshold);
446 ::ImageAlphaMask(
this, alphaMask);
454 ::ImageAlphaPremultiply(
this);
461 Image&
Crop(
int newWidth,
int newHeight) {
return Crop(0, 0, newWidth, newHeight); }
466 Image&
Crop(::Vector2 size) {
return Crop(0, 0,
static_cast<int>(size.x),
static_cast<int>(size.y)); }
471 Image&
Crop(
int offsetX,
int offsetY,
int newWidth,
int newHeight) {
473 static_cast<float>(offsetX),
474 static_cast<float>(offsetY),
475 static_cast<float>(newWidth),
476 static_cast<float>(newHeight)};
477 ::ImageCrop(
this, rect);
485 ::ImageResize(
this, newWidth, newHeight);
493 ::ImageResizeNN(
this, newWidth, newHeight);
501 ResizeCanvas(
int newWidth,
int newHeight,
int offsetX = 0,
int offsetY = 0, ::Color color = {255, 255, 255, 255}) {
502 ::ImageResizeCanvas(
this, newWidth, newHeight, offsetX, offsetY, color);
510 ::ImageMipmaps(
this);
518 ::ImageDither(
this, rBpp, gBpp, bBpp, aBpp);
526 ::ImageFlipVertical(
this);
534 ::ImageFlipHorizontal(
this);
544 ::ImageRotate(
this,
static_cast<int>(degrees));
552 ::ImageRotateCW(
this);
560 ::ImageRotateCCW(
this);
568 ::ImageColorTint(
this, color);
576 ::ImageColorInvert(
this);
584 ::ImageColorGrayscale(
this);
594 ::ImageColorContrast(
this, contrast);
604 ::ImageColorBrightness(
this, brightness);
612 ::ImageColorReplace(
this, color, replace);
632 return ::GetImageColor(*
this,
static_cast<int>(position.x),
static_cast<int>(position.y));
639 ::ImageClearBackground(
this, color);
646 void DrawPixel(
int posX,
int posY, ::Color color = {255, 255, 255, 255}) {
647 ::ImageDrawPixel(
this, posX, posY, color);
650 void DrawPixel(::Vector2 position, ::Color color = {255, 255, 255, 255}) {
651 ::ImageDrawPixelV(
this, position, color);
654 void DrawLine(
int startPosX,
int startPosY,
int endPosX,
int endPosY, ::Color color = {255, 255, 255, 255}) {
655 ::ImageDrawLine(
this, startPosX, startPosY, endPosX, endPosY, color);
658 void DrawLine(::Vector2 start, ::Vector2 end, ::Color color = {255, 255, 255, 255}) {
659 ::ImageDrawLineV(
this, start, end, color);
665 void DrawLine(::Vector2 start, ::Vector2 end,
int thick, ::Color color = {255, 255, 255, 255}) {
666 ImageDrawLineEx(
this, start, end, thick, color);
669 void DrawCircle(
int centerX,
int centerY,
int radius, ::Color color = {255, 255, 255, 255}) {
670 ::ImageDrawCircle(
this, centerX, centerY, radius, color);
673 void DrawCircle(::Vector2 center,
int radius, ::Color color = {255, 255, 255, 255}) {
674 ::ImageDrawCircleV(
this, center, radius, color);
677 void DrawRectangle(
int posX,
int posY,
int width,
int height, ::Color color = {255, 255, 255, 255}) {
678 ::ImageDrawRectangle(
this, posX, posY, width, height, color);
681 void DrawRectangle(Vector2 position, Vector2 size, ::Color color = {255, 255, 255, 255}) {
682 ::ImageDrawRectangleV(
this, position, size, color);
685 void DrawRectangle(::Rectangle rec, ::Color color = {255, 255, 255, 255}) {
686 ::ImageDrawRectangleRec(
this, rec, color);
689 void DrawRectangleLines(::Rectangle rec,
int thick = 1, ::Color color = {255, 255, 255, 255}) {
690 ::ImageDrawRectangleLines(
this, rec, thick, color);
695 void Draw(const ::Image& src, ::Rectangle srcRec, ::Rectangle dstRec, ::Color tint = {255, 255, 255, 255}) {
696 ::ImageDraw(
this, src, srcRec, dstRec, tint);
699 void DrawText(
const char* text, ::Vector2 position,
int fontSize, ::Color color = {255, 255, 255, 255}) {
700 ::ImageDrawText(
this, text,
static_cast<int>(position.x),
static_cast<int>(position.y), fontSize, color);
703 void DrawText(
const std::string_view text, ::Vector2 position,
int fontSize,
704 ::Color color = {255, 255, 255, 255}) {
705 ::ImageDrawText(
this,
707 static_cast<int>(position.x),
708 static_cast<int>(position.y),
713 void DrawText(
const std::string_view text,
int x,
int y,
int fontSize,
714 ::Color color = {255, 255, 255, 255}) {
715 ::ImageDrawText(
this, text.data(), x, y, fontSize, color);
718 void DrawText(
const char* text,
int x,
int y,
int fontSize, ::Color color = {255, 255, 255, 255}) {
719 ::ImageDrawText(
this, text, x, y, fontSize, color);
722 void DrawText(const ::Font& font,
const std::string_view text, ::Vector2 position,
723 float fontSize,
float spacing, ::Color tint = {255, 255, 255, 255}) {
724 ::ImageDrawTextEx(
this, font, text.data(), position, fontSize, spacing, tint);
733 ::Color tint = {255, 255, 255, 255}) {
734 ::ImageDrawTextEx(
this, font, text, position, fontSize, spacing, tint);
740 [[nodiscard]] ::Color*
LoadColors()
const { return ::LoadImageColors(*
this); }
745 ::Color*
LoadPalette(
int maxPaletteSize,
int* colorsCount)
const {
746 return ::LoadImagePalette(*
this, maxPaletteSize, colorsCount);
752 void UnloadColors(::Color* colors)
const { ::UnloadImageColors(colors); }
774 static int GetPixelDataSize(
int width,
int height,
int format = PIXELFORMAT_UNCOMPRESSED_R32G32B32A32) {
775 return ::GetPixelDataSize(width, height, format);
783 [[nodiscard]]
int GetPixelDataSize()
const { return ::GetPixelDataSize(width, height, format); }
790 [[nodiscard]]
bool IsValid()
const { return ::IsImageValid(*
this); }
795 ::Image
Channel(
int selectedChannel) { return ::ImageFromChannel(*
this, selectedChannel); }
801 ::ImageKernelConvolution(
this, kernel, kernelSize);
804 void set(const ::Image& image) {
807 height = image.height;
808 mipmaps = image.mipmaps;
809 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.
void KernelConvolution(const float *kernel, int kernelSize)
Apply custom square convolution kernel to image.
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.
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.
void DrawLine(::Vector2 start, ::Vector2 end, int thick, ::Color color={255, 255, 255, 255})
Description: Draw a line defining thickness within an image.
::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.
bool IsValid() const
Retrieve whether or not the Image has been loaded.
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.
operator::Texture2D() const
Loads a texture from the image data.
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()
::Image Channel(int selectedChannel)
Create an image from a selected channel of another image (GRAYSCALE)
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.
::Image GetClipboard()
Get clipboard image content.
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.
All raylib-cpp classes and functions appear in the raylib namespace.
static inline ::Image LoadImage(const std::string_view fileName)
Load an image.
static inline ::Image LoadImageFromMemory(const std::string_view fileType, const unsigned char *fileData, int dataSize)
Load image from memory buffer, fileType refers to extension like "png".
static bool ExportImageAsCode(const Image &image, const std::string_view fileName)
Export image as code file (.h) defining an array of bytes.
static inline ::Image LoadImageAnim(const std::string_view fileName, int *frames)
Load animated image data.
static inline ::Image LoadImageRaw(const std::string_view fileName, int width, int height, int format, int headerSize)
Load an image from RAW file data.
static bool ExportImage(const Image &image, const std::string_view fileName)
Export image data to file.