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"
18 Image(
void* data =
nullptr,
22 int format = 0) :
::Image{data, width, height, mipmaps, format} {
26 Image(const ::Image& image) {
37 Image(
const std::string& fileName) {
38 if (!
Load(fileName)) {
39 throw RaylibException(TextFormat(
"Failed to load Image from file: %s", fileName.c_str()));
50 Image(
const std::string& fileName,
int width,
int height,
int format,
int headerSize) {
51 if (!
Load(fileName, width, height, format, headerSize)) {
52 throw RaylibException(TextFormat(
"Failed to load Image from file: %s", fileName.c_str()));
63 Image(
const std::string& fileName,
int* frames) {
64 if (!
Load(fileName, frames)) {
65 throw RaylibException(TextFormat(
"Failed to load Image from animation: %s", fileName.c_str()));
74 Image(
const std::string& fileType,
const unsigned char* fileData,
int dataSize) {
75 if (!
Load(fileType, fileData, dataSize)) {
85 Image(const ::Texture2D& texture) {
91 Image(
int width,
int height, ::
Color color = {255, 255, 255, 255}) {
92 set(::GenImageColor(width, height, color));
95 Image(
const std::string& text,
int fontSize, ::
Color color = {255, 255, 255, 255}) {
96 set(::ImageText(text.c_str(), fontSize, color));
99 Image(const ::Font& font,
const std::string& text,
float fontSize,
float spacing,
100 ::
Color tint = {255, 255, 255, 255}) {
101 set(::ImageTextEx(font, text.c_str(), fontSize, spacing, tint));
104 Image(
const Image& other) {
108 Image(Image&& other) {
111 other.data =
nullptr;
118 static ::Image Text(
const std::string& text,
int fontSize,
119 ::
Color color = {255, 255, 255, 255}) {
120 return ::ImageText(text.c_str(), fontSize, color);
123 static ::Image Text(const ::Font& font,
const std::string& text,
float fontSize,
float spacing,
124 ::
Color tint = {255, 255, 255, 255}) {
125 return ::ImageTextEx(font, text.c_str(), fontSize, spacing, tint);
132 return ::LoadImageFromScreen();
138 static ::Image
Color(
int width,
int height, ::
Color color = {255, 255, 255, 255}) {
139 return ::GenImageColor(width, height, color);
146 return ::GenImageGradientV(width, height, top, bottom);
153 return ::GenImageGradientH(width, height, left, right);
161 return ::GenImageGradientRadial(width, height, density, inner, outer);
167 static ::Image
Checked(
int width,
int height,
int checksX,
int checksY,
168 ::
Color col1 = {255, 255, 255, 255},
::Color col2 = {0, 0, 0, 255}) {
169 return ::GenImageChecked(width, height, checksX, checksY, col1, col2);
175 static ::Image
WhiteNoise(
int width,
int height,
float factor) {
176 return ::GenImageWhiteNoise(width, height, factor);
182 static ::Image
Cellular(
int width,
int height,
int tileSize) {
183 return ::GenImageCellular(width, height, tileSize);
190 Image& operator=(const ::Image& image) {
195 Image& operator=(
const Image& other) {
196 if (
this == &other) {
206 Image& operator=(Image&& other) {
207 if (
this == &other) {
214 other.data =
nullptr;
230 bool Load(
const std::string& fileName) {
242 bool Load(
const std::string& fileName,
int width,
int height,
int format,
int headerSize) {
243 set(::
LoadImageRaw(fileName.c_str(), width, height, format, headerSize));
254 bool Load(
const std::string& fileName,
int* frames) {
267 const std::string& fileType,
268 const unsigned char *fileData,
281 bool Load(const ::Texture2D& texture) {
282 set(::LoadImageFromTexture(texture));
290 if (data !=
nullptr) {
291 ::UnloadImage(*
this);
299 inline bool Export(
const std::string& fileName)
const {
301 return ::ExportImage(*
this, fileName.c_str());
308 return ::ExportImageAsCode(*
this, fileName.c_str());
311 GETTERSETTER(
void*, Data, data)
312 GETTERSETTER(
int, Width, width)
313 GETTERSETTER(
int, Height, height)
321 return {
static_cast<float>(width),
static_cast<float>(height)};
328 return ::ImageCopy(*
this);
335 return ::ImageFromImage(*
this, rec);
342 ::ImageToPOT(
this, fillColor);
350 ::ImageFormat(
this, newFormat);
358 ::ImageAlphaCrop(
this, threshold);
366 ::ImageAlphaClear(
this, color, threshold);
374 ::ImageAlphaMask(
this, alphaMask);
382 ::ImageAlphaPremultiply(
this);
390 ::ImageCrop(
this, crop);
398 return Crop(0, 0, newWidth, newHeight);
405 return Crop(0, 0,
static_cast<int>(size.x),
static_cast<int>(size.y));
411 inline Image&
Crop(
int offsetX,
int offsetY,
int newWidth,
int newHeight) {
413 static_cast<float>(offsetX),
414 static_cast<float>(offsetY),
415 static_cast<float>(newWidth),
416 static_cast<float>(newHeight)
418 ::ImageCrop(
this, rect);
426 ::ImageResize(
this, newWidth, newHeight);
434 ::ImageResizeNN(
this, newWidth, newHeight);
442 ::
Color color = {255, 255, 255, 255}) {
443 ::ImageResizeCanvas(
this, newWidth, newHeight, offsetX, offsetY, color);
451 ::ImageMipmaps(
this);
459 ::ImageDither(
this, rBpp, gBpp, bBpp, aBpp);
467 ::ImageFlipVertical(
this);
475 ::ImageFlipHorizontal(
this);
483 ::ImageRotateCW(
this);
491 ::ImageRotateCCW(
this);
499 ::ImageColorTint(
this, color);
507 ::ImageColorInvert(
this);
515 ::ImageColorGrayscale(
this);
525 ::ImageColorContrast(
this, contrast);
535 ::ImageColorBrightness(
this, brightness);
543 ::ImageColorReplace(
this, color, replace);
553 return ::GetImageAlphaBorder(*
this, threshold);
560 ::ImageClearBackground(
this, color);
568 ::ImageDrawPixel(
this, posX, posY, color);
572 inline Image&
DrawPixel(::Vector2 position, ::
Color color = {255, 255, 255, 255}) {
573 ::ImageDrawPixelV(
this, position, color);
577 inline Image& DrawLine(
int startPosX,
int startPosY,
int endPosX,
int endPosY,
578 ::
Color color = {255, 255, 255, 255}) {
579 ::ImageDrawLine(
this, startPosX, startPosY, endPosX, endPosY, color);
583 inline Image& DrawLine(::Vector2 start, ::Vector2 end, ::
Color color = {255, 255, 255, 255}) {
584 ::ImageDrawLineV(
this, start, end, color);
588 inline Image& DrawCircle(
int centerX,
int centerY,
int radius,
589 ::
Color color = {255, 255, 255, 255}) {
590 ::ImageDrawCircle(
this, centerX, centerY, radius, color);
594 inline Image& DrawCircle(::Vector2 center,
int radius,
595 ::
Color color = {255, 255, 255, 255}) {
596 ::ImageDrawCircleV(
this, center, radius, color);
600 inline Image& DrawRectangle(
int posX,
int posY,
int width,
int height,
601 ::
Color color = {255, 255, 255, 255}) {
602 ::ImageDrawRectangle(
this, posX, posY, width, height, color);
606 inline Image& DrawRectangle(Vector2 position, Vector2 size,
607 ::
Color color = {255, 255, 255, 255}) {
608 ::ImageDrawRectangleV(
this, position, size, color);
612 inline Image& DrawRectangle(::Rectangle rec, ::
Color color = {255, 255, 255, 255}) {
613 ::ImageDrawRectangleRec(
this, rec, color);
617 inline Image& DrawRectangleLines(::Rectangle rec,
int thick = 1,
618 ::
Color color = {255, 255, 255, 255}) {
619 ::ImageDrawRectangleLines(
this, rec, thick, color);
623 inline Image& Draw(const ::Image& src, ::Rectangle srcRec, ::Rectangle dstRec,
624 ::
Color tint = {255, 255, 255, 255}) {
625 ::ImageDraw(
this, src, srcRec, dstRec, tint);
629 inline Image& DrawText(
const std::string& text, ::Vector2 position,
int fontSize,
630 ::
Color color = {255, 255, 255, 255}) {
631 ::ImageDrawText(
this,
633 static_cast<int>(position.x),
634 static_cast<int>(position.y),
640 inline Image& DrawText(
const std::string& text,
int x,
int y,
int fontSize,
641 ::
Color color = {255, 255, 255, 255}) {
642 ::ImageDrawText(
this, text.c_str(), x, y, fontSize, color);
646 inline Image& DrawText(const ::Font& font,
const std::string& text, ::Vector2 position,
647 float fontSize,
float spacing, ::
Color tint = {255, 255, 255, 255}) {
648 ::ImageDrawTextEx(
this, font, text.c_str(), position, fontSize, spacing, tint);
656 return ::LoadImageColors(*
this);
662 inline ::Color*
LoadPalette(
int maxPaletteSize,
int *colorsCount)
const {
663 return ::LoadImagePalette(*
this, maxPaletteSize, colorsCount);
670 ::UnloadImageColors(colors);
677 ::UnloadImagePalette(colors);
684 return ::LoadTextureFromImage(*
this);
692 inline operator ::Texture2D() {
699 static int GetPixelDataSize(
int width,
int height,
int format = PIXELFORMAT_UNCOMPRESSED_R32G32B32A32) {
700 return ::GetPixelDataSize(width, height, format);
709 return ::GetPixelDataSize(width, height, format);
718 return data !=
nullptr;
722 inline void set(const ::Image& image) {
725 height = image.height;
726 mipmaps = image.mipmaps;
727 format = image.format;
732 #endif // RAYLIB_CPP_INCLUDE_IMAGE_HPP_