Images

High-level image loading and management.

Overview

Typical use cases:

  • Loading an image from a file or memory buffer

  • Creating an empty image

  • Saving a texture to a PNG file

Header

<RaeptorCogs/IO/Images.hpp>

Metadata

Author

Estorc

Version

v1.0

Copyright

Copyright (c) 2025 Estorc MIT License.

Structs

Structures

Struct

Description

RaeptorCogs::Image

Image data structure.

struct Image

Image data structure.

Represents an image loaded into memory, including its pixel data, dimensions, and channel count.

RaeptorCogs::Image img = RaeptorCogs::LoadImageFromFile("example.png");
if (img.data) {
   std::cout << "Image loaded: " << img.width << "x" << img.height << " with " << img.channels << " channels." << std::endl;
}

Note

The pixel data is managed using a unique_ptr with a custom deleter to ensure proper memory management.

Public Functions

Image()

Default constructor for an empty image.

Initializes width, height, and channels to zero, and data to nullptr.

Image(unsigned char *data, size_t width, size_t height, size_t channels)

Parameterized constructor for an image.

Parameters:
  • data – Pointer to the pixel data.

  • width – Width of the image in pixels.

  • height – Height of the image in pixels.

  • channels – Number of color channels in the image.

~Image() = default

Destructor for the Image structure. Cleans up the pixel data using the custom deleter.

Image(const Image&) = delete

Deleted copy constructor and assignment operator to prevent copying. Images manage dynamic memory and should not be copied.

Image &operator=(const Image&) = delete
Image(Image&&) = default

Defaulted move constructor and assignment operator for efficient transfer of resources. Allows moving Image instances without copying the pixel data.

Image &operator=(Image&&) = default
inline bool isOpaque() const

Check if the image is fully opaque.

Note

An image is considered opaque if all alpha channel values are 255.

Returns:

true if all pixels are fully opaque, false otherwise.

Public Members

std::unique_ptr<unsigned char[], void (*)(void*)> data

Pixel data of the image.

Pointer to the raw pixel data stored as an array of unsigned char. The data is managed using a unique_ptr with a custom deleter to ensure proper memory management.

size_t width

Width of the image in pixels.

size_t height

Height of the image in pixels.

size_t channels

Number of color channels in the image.

Functions

Functions

Function

Description

RaeptorCogs::CreateImage(size_t width, size_t height)

Create an empty image with specified dimensions.

RaeptorCogs::LoadImageFromFile(const std::filesystem::path &filename, size_t s_width=0, size_t s_height=0)

Load an image from a file.

RaeptorCogs::LoadImageFromMemory(const FileData &data, size_t s_width=0, size_t s_height=0)

Load an image from memory.

RaeptorCogs::LoadImageFromURL(const std::filesystem::path &url)

Load an image from a URL.

RaeptorCogs::LoadTexture(const std::filesystem::path &filename)

Load a texture from an image file.

RaeptorCogs::saveTextureToPNG(GLuint textureID, size_t width, size_t height, const std::filesystem::path &filename)

No description.

RaeptorCogs::saveTextureToPNG(uint32_t textureID, size_t width, size_t height, const std::filesystem::path &filename)

Save an OpenGL texture to a PNG file.

Image RaeptorCogs::CreateImage(size_t width, size_t height)

Create an empty image with specified dimensions.

Note

The created image has 4 channels (RGBA) initialized to zero.

Parameters:
  • width – Width of the image in pixels.

  • height – Height of the image in pixels.

Returns:

Created Image object with allocated pixel data.

Image RaeptorCogs::LoadImageFromFile(const std::filesystem::path &filename, size_t s_width = 0, size_t s_height = 0)

Load an image from a file.

Note

Returns an empty Image if loading fails.

Parameters:
  • filename – Path to the image file.

  • s_width – Optional desired width to resize the image to. Set to 0 to keep original width.

  • s_height – Optional desired height to resize the image to. Set to 0 to keep original height.

Returns:

Loaded Image object.

Image RaeptorCogs::LoadImageFromMemory(const FileData &data, size_t s_width = 0, size_t s_height = 0)

Load an image from memory.

Note

Returns an empty Image if loading fails.

Parameters:
  • data – Byte data of the image file.

  • s_width – Optional desired width to resize the image to. Set to 0 to keep original width.

  • s_height – Optional desired height to resize the image to. Set to 0 to keep original height.

Returns:

Loaded Image object.

Image RaeptorCogs::LoadImageFromURL(const std::filesystem::path &url)

Load an image from a URL.

Note

Returns an empty Image if loading fails (and if EMSCRIPTEN is defined).

Parameters:

url – URL of the image.

Returns:

Loaded Image object.

uint32_t RaeptorCogs::LoadTexture(const std::filesystem::path &filename)

Load a texture from an image file.

Note

Returns 0 if loading fails.

Parameters:

filename – Path to the image file.

Returns:

OpenGL texture ID.

void RaeptorCogs::saveTextureToPNG(GLuint textureID, size_t width, size_t height, const std::filesystem::path &filename)
void RaeptorCogs::saveTextureToPNG(uint32_t textureID, size_t width, size_t height, const std::filesystem::path &filename)

Save an OpenGL texture to a PNG file.

Note

Overwrites the file if it already exists.

Parameters:
  • textureID – OpenGL texture ID.

  • width – Width of the texture in pixels.

  • height – Height of the texture in pixels.

  • filename – Path to save the PNG file.