Skip to content

Image class

TechnologicalTurtle edited this page Jul 15, 2026 · 5 revisions

Image

Image is class for handling images, it has functions for loading&saving to files, editing and rendering them.

Initialization

Blank image creation

You can create blank images using Image(Vec2i Size, Color color = Color::Black) constructor.

LibGui::Image MyImage({200, 200}, Color::White);

If you render it, it should look like this:

blank_image

Loading from file

You can also load images from files using Image(const std::string& path) constructor, but remember that image will be sqeezed to fit object you render on and to fix it you should update the objects scale.

LibGui::Image MyImage("cool_turtle_image.jpg");

// the scale fix (and part of rendering)
LibGui::Texture MyTexture(MyImage);

LibGui::Object MyObject;
MyObject.InitAsRectangle({250, 250}, {200, 200}, &MyTexture);

// set scale to have same ration as source image
MyObject.scale = MyImage.size * 0.5f;

If you render it (with the scale fix), it should look like this:

cool_turtle_app_image

source here

Important

If you want to just render an image file (without editing or re-saving), you can just use textures

Rendering

To render image, you have to convert it into a texture and apply that texture to object, then render that object regularly.

// create blank image
LibGui::Image MyImage({200, 200}, Color::Green);

// create an texture from MyImage
LibGui::Texture MyTexture(MyImage);

LibGui::Object MyObject;
MyObject.InitAsRectangle({250, 250}, {200, 200}, MyTexture);

while (!MyWindow.ShouldClose())
{
   MyWindow.Draw();

   MyObject.Render();

   MyWindow.PushDraw();
}

After creating texture from an image, you can safely discard MyImage, you only have to keep the MyTexture and MyObject.

Saving images

You can save your image to four different formats: PNG, JPG, TGA and BMP

Function Description
SavePNG(const std::string& path) saves image as .png, to path
SaveJPG(const std::string& path, int quality) saves image as .jpg, to path with quality (quality ranges from 1 to 100, 100 = best, but higher size)
SaveTGA(const std::string& path) saves image as .tga, to path
SaveBMP(const std::string& path) saves image as .bmp, to path

Get functions

LibGui::Pixel& GetPixel(Vec2i pos)

Returns reference to Pixel at pos, you can safely edit the pixel; {0, 0} of pos is in lower left corner of image.

LibGui::Color GetPixelColor(Vec2i pos)

Returns pixel color on given coordinates, wrapper for GetPixel.

LibGui::Pixel& GetPixel(size_t index)

Returns reference to Pixel at index, you can safely edit the pixel; index starts in lower left corner, then goes left by rows.

unsigned char* GetData()

Returns pointer to first pixel of image, used in texture loading.
Structure of pixels array for image sized 2x2: (datatype of one value is char):

r|g|b|a|r|g|b|a|
r|g|b|a|r|g|b|a

you can also read it in Pixel then the image would look like this:

{r, g, b, a}{r, g, b, a}
{r, g, b, a}{r, g, b, a}

In both options, pixels start at lower left corner and go up by rows.

std::vector GetDataCopy()

Returns copy of std::vectorLibGui::Pixel, starting in lower left corner of image.

Editing

void Clear(Color fillColor = Color::Black)

Clears whole image with fillColor

void Resize(Vec2i newSize, Color fillColor = Color::Black)

Resizes the whole image, then calls Clear(fillColor);

void SetPixelColor(Vec2i pos, Color color)

Sets pixel color on given coordinates, wrapper for GetPixel.

void ApplyEffect(void(*effect)(Pixel&, size_t))

Apply function effect to each pixel of image.

// darken the image
MyImage.ApplyEffect(
   [](LibGui::Pixel& pixel, size_t index)
   {
      pixel.A /= 2;
   });

void FlipXAxis()

Mirror image along X-axis.

void FlipYAxis()

Mirror image along Y-axis.

void RemoveCount(LibGui::Direction_ side, int count)

Remove count of pixels from side of image. Example: (remove left part of this image)

LibGui::Image Forest("forest.png");
Forest.RemoveCount(LibGui::Direction_Left, 300);

forest_example source here

void MergeImage(LibGui::Direction_ side, LibGui::Image& img, Color fill = Color::Transparent)

Merges image img to the side of this image, if the dimensions don't match fill blank space with fill.

LibGui::Image turtle1("turtle1.jpg");
LibGui::Image turtle2("turtle2.jpg");

turtle1.MergeImage(LibGui::Direction_Right, turtle2, Color::Blue);

void MergeCount(LibGui::Direction_ side, int count, Color color)

Adds count of columns/rows, filled with color, to the side of image.

Clone this wiki locally