Skip to content

Engine → Core → image.h

image

Syntax

class CONJURE_API image

Constructors

AccessSignatureDescription
publicimage();
publicimage( u32 width, u32 height);A zero-filled image (transparent black). A zero width or height yields the empty image. Dimensions above MAX_DIMENSION, or whose pixel data would exceed MAX_PIXEL_BYTES, are a programmer error and yield the empty image too.
publicimage( const image &) = default;
publicimage( image &&) noexcept = default;

Destructors

AccessSignatureDescription
public~image();

Variables

AccessNameTypeDefaultDescription
publicBYTES_PER_PIXELstatic constexpr u324Size of one pixel in bytes.
publicMAX_DIMENSIONstatic constexpr u321u << 24Largest width or height accepted; the PNG codec rejects anything larger.
publicMAX_ENCODED_BYTESstatic constexpr usize0x7FFFFFFFLargest value of width * height * BYTES_PER_PIXEL + height that encode_png() accepts (the PNG encoder works in 32-bit signed arithmetic).
publicMAX_PIXEL_BYTESstatic constexpr usizeMAX_ENCODED_BYTESLargest pixel buffer (width * height * BYTES_PER_PIXEL) the constructor accepts; the PNG codecs work in 32-bit signed sizes.
privatemHeightu320
privatemPixelsstd::vector<std::byte>{}
privatemWidthu320

Functions

Public

compare

[[nodiscard]] f64 compare(
    const image &other) const noexcept;

Mean absolute difference over every channel of every pixel, normalised to 0 (identical) .. 1 (every byte differs by 255). Images of different dimensions compare as 1. Two empty images compare as 0. Used by golden-image tests.

encode_png

[[nodiscard]] result<std::vector<std::byte>> encode_png() const;

Encodes the image as PNG. Fails with INVALID_STATE for an empty image and UNSUPPORTED for one whose pixel data (plus one filter byte per row) exceeds MAX_ENCODED_BYTES, the limit of the encoder's 32-bit arithmetic.

fill

void fill(
    rgba8 color) noexcept;

Sets every pixel to one colour.

flip_vertical

void flip_vertical() noexcept;

Reverses the order of the rows in place (top becomes bottom).

height

[[nodiscard]] u32 height() const noexcept;

Height in pixels; 0 for the empty image.

is_empty

[[nodiscard]] bool is_empty() const noexcept;

True when the image has no pixels.

load_file

[[nodiscard]] static result<image> load_file(
    const std::filesystem::path &path);

Reads a PNG file. Fails with NOT_FOUND when the file does not exist, IO_FAILURE when it cannot be read and PARSE_FAILURE when it is not a PNG.

load_png

[[nodiscard]] static result<image> load_png(
    std::span<const std::byte> data);

Decodes a PNG held in memory. Grey, grey + alpha and RGB files are expanded to RGBA8 (alpha 255 when absent). Fails with INVALID_ARGUMENT for empty data and PARSE_FAILURE for anything that is not a well-formed PNG.

pixel

[[nodiscard]] rgba8 pixel(
    u32 x,
    u32 y) const noexcept;

The pixel at (x, y). Out-of-range coordinates assert and return a default rgba8.

pixels

[[nodiscard]] std::span<std::byte> pixels() noexcept;

The pixel bytes, width * height * 4 of them: for pixel (x, y) the red byte is at index (y * width + x) * 4, followed by green, blue and alpha.

[[nodiscard]] std::span<const std::byte> pixels() const noexcept;

save_png

[[nodiscard]] result<void> save_png(
    const std::filesystem::path &path) const;

Writes the image as a PNG file, replacing any existing file. Same failures as encode_png() plus IO_FAILURE when the file cannot be written.

set_pixel

void set_pixel(
    u32 x,
    u32 y,
    rgba8 color) noexcept;

Sets the pixel at (x, y). Out-of-range coordinates assert and change nothing.

width

[[nodiscard]] u32 width() const noexcept;

Width in pixels; 0 for the empty image.

Operator Overloads

Public

operator=

image &operator=(
    const image &) = default;
image &operator=(
    image &&) noexcept = default;

operator==

[[nodiscard]] bool operator==(
    const image &other) const noexcept;

True when dimensions and every pixel byte are equal.