Image

template<IsImagePixel T>
struct ImagePixelTraits

Type traits for image pixel types.

Provides compile-time information about pixel types including the underlying scalar type and the number of channels.

Template Parameters:

T – The pixel type (must satisfy IsImagePixel concept)

Public Types

using Scalar = T

Public Static Attributes

static int channels = 1
enum class huira::WrapMode

Specifies how texture coordinates outside [0,1] are handled during sampling.

Values:

enumerator Clamp

Clamp coordinates to [0,1].

enumerator Repeat

Repeat texture by wrapping coordinates.

enumerator Mirror

Mirror texture at boundaries.

template<IsImagePixel PixelT>
class Image

A 2D image container with templated pixel types.

The Image class provides a flexible container for 2D image data with support for various pixel types including scalar values, Vec3 for RGB/color data, and SpectralBins for spectral imaging. It offers both checked and unchecked access methods, as well as sampling operations with different wrap modes.

Memory is stored in row-major order, with the origin at the top-left corner. Pixel coordinates (x, y) map to image space where x increases to the right and y increases downward.

Template Parameters:

PixelT – The type of pixel stored (must satisfy IsImagePixel concept)

Public Types

using PixelType = PixelT
using Traits = ImagePixelTraits<PixelT>
using Scalar = typename Traits::Scalar

Public Functions

Image()

Default constructor creating an empty image.

Image(Resolution resolution)

Constructs an image with the specified resolution.

Pixels are default-initialized.

Parameters:

resolution – The width and height of the image

Image(Resolution resolution, const PixelT &fill_value)

Constructs an image with the specified resolution and fill value.

All pixels are initialized to the specified fill value.

Parameters:
  • resolution – The width and height of the image

  • fill_value – The value to initialize all pixels with

Image(int width, int height)

Constructs an image with the specified width and height.

Pixels are default-initialized.

Parameters:
  • width – The width of the image in pixels

  • height – The height of the image in pixels

Image(int width, int height, const PixelT &fill_value)

Constructs an image with the specified width, height, and fill value.

All pixels are initialized to the specified fill value.

Parameters:
  • width – The width of the image in pixels

  • height – The height of the image in pixels

  • fill_value – The value to initialize all pixels with

Image(const Image&) = default
Image(Image&&) noexcept = default
Image &operator=(const Image&) = default
Image &operator=(Image&&) noexcept = default
~Image() = default
bool empty() const noexcept

Checks if the image has no pixels.

Returns:

true if the image is empty (zero width or height), false otherwise

explicit operator bool() const noexcept

Checks if the image contains any pixels.

Returns:

true if the image is not empty, false otherwise

Resolution resolution() const noexcept

Gets the resolution (width and height) of the image.

Returns:

The image resolution

int width() const noexcept

Gets the width of the image in pixels.

Returns:

The image width

int height() const noexcept

Gets the height of the image in pixels.

Returns:

The image height

std::size_t size() const noexcept

Gets the total number of pixels in the image.

Returns:

The number of pixels (width × height)

PixelT &operator[](std::size_t index)

Provides unchecked access to a pixel by linear index.

This operator does not perform bounds checking in release builds. In debug builds, an assertion will fail if the index is out of bounds.

Parameters:

index – Linear index into the image data (0 to size()-1)

Returns:

Reference to the pixel at the specified index

const PixelT &operator[](std::size_t index) const

Provides unchecked read-only access to a pixel by linear index.

This operator does not perform bounds checking in release builds. In debug builds, an assertion will fail if the index is out of bounds.

Parameters:

index – Linear index into the image data (0 to size()-1)

Returns:

Const reference to the pixel at the specified index

PixelT &operator()(int x, int y)

Provides unchecked access to a pixel by 2D coordinates.

This operator does not perform bounds checking in release builds. In debug builds, an assertion will fail if the coordinates are out of bounds.

Parameters:
  • x – The x-coordinate (column) of the pixel

  • y – The y-coordinate (row) of the pixel

Returns:

Reference to the pixel at (x, y)

const PixelT &operator()(int x, int y) const

Provides unchecked read-only access to a pixel by 2D coordinates.

This operator does not perform bounds checking in release builds. In debug builds, an assertion will fail if the coordinates are out of bounds.

Parameters:
  • x – The x-coordinate (column) of the pixel

  • y – The y-coordinate (row) of the pixel

Returns:

Const reference to the pixel at (x, y)

PixelT &operator()(const Pixel &pixel)

Provides unchecked access to a pixel by Pixel coordinates.

The Pixel coordinates are converted to integers by truncation. This operator does not perform bounds checking in release builds. In debug builds, an assertion will fail if the coordinates are out of bounds.

Parameters:

pixel – The pixel coordinates (floating point values are truncated)

Returns:

Reference to the pixel at the converted coordinates

const PixelT &operator()(const Pixel &pixel) const

Provides unchecked read-only access to a pixel by Pixel coordinates.

The Pixel coordinates are converted to integers by truncation. This operator does not perform bounds checking in release builds. In debug builds, an assertion will fail if the coordinates are out of bounds.

Parameters:

pixel – The pixel coordinates (floating point values are truncated)

Returns:

Const reference to the pixel at the converted coordinates

PixelT &at(std::size_t index)

Provides bounds-checked access to a pixel by linear index.

Parameters:

index – Linear index into the image data (0 to size()-1)

Throws:

std::out_of_range – if the index is out of bounds

Returns:

Reference to the pixel at the specified index

const PixelT &at(std::size_t index) const

Provides bounds-checked read-only access to a pixel by linear index.

Parameters:

index – Linear index into the image data (0 to size()-1)

Throws:

std::out_of_range – if the index is out of bounds

Returns:

Const reference to the pixel at the specified index

PixelT &at(int x, int y)

Provides bounds-checked access to a pixel by 2D coordinates.

Parameters:
  • x – The x-coordinate (column) of the pixel

  • y – The y-coordinate (row) of the pixel

Throws:

std::out_of_range – if the coordinates are out of bounds

Returns:

Reference to the pixel at (x, y)

const PixelT &at(int x, int y) const

Provides bounds-checked read-only access to a pixel by 2D coordinates.

Parameters:
  • x – The x-coordinate (column) of the pixel

  • y – The y-coordinate (row) of the pixel

Throws:

std::out_of_range – if the coordinates are out of bounds

Returns:

Const reference to the pixel at (x, y)

PixelT &at(const Pixel &pixel)

Provides bounds-checked access to a pixel by Pixel coordinates.

The Pixel coordinates are converted to integers by truncation.

Parameters:

pixel – The pixel coordinates (floating point values are truncated)

Throws:

std::out_of_range – if the coordinates are out of bounds

Returns:

Reference to the pixel at the converted coordinates

const PixelT &at(const Pixel &pixel) const

Provides bounds-checked read-only access to a pixel by Pixel coordinates.

The Pixel coordinates are converted to integers by truncation.

Parameters:

pixel – The pixel coordinates (floating point values are truncated)

Throws:

std::out_of_range – if the coordinates are out of bounds

Returns:

Const reference to the pixel at the converted coordinates

template<WrapMode W = WrapMode::Clamp>
PixelT sample_nearest_neighbor(float u, float v) const

Samples the image using nearest-neighbor interpolation.

UV coordinates are expected in the range [0, 1], where (0, 0) is the top-left corner and (1, 1) is the bottom-right corner. Coordinates outside this range are handled according to the specified wrap mode.

Template Parameters:

W – The wrap mode for handling coordinates outside [0, 1]

Parameters:
  • u – The horizontal texture coordinate (0 to 1)

  • v – The vertical texture coordinate (0 to 1)

Returns:

The sampled pixel value, or a default-constructed pixel if the image is empty

template<WrapMode W = WrapMode::Clamp>
PixelT sample_bilinear(float u, float v) const

Samples the image using bilinear interpolation.

UV coordinates are expected in the range [0, 1], where (0, 0) is the top-left corner and (1, 1) is the bottom-right corner. Coordinates outside this range are handled according to the specified wrap mode.

Bilinear interpolation is supported for arithmetic types, Vec3 types, and SpectralBins. For other pixel types, the function falls back to nearest-neighbor sampling.

Template Parameters:

W – The wrap mode for handling coordinates outside [0, 1]

Parameters:
  • u – The horizontal texture coordinate (0 to 1)

  • v – The vertical texture coordinate (0 to 1)

Returns:

The interpolated pixel value, or a default-constructed pixel if the image is empty

PixelT *data() noexcept

Gets a pointer to the underlying pixel data.

Returns:

Pointer to the first pixel in the image data

const PixelT *data() const noexcept

Gets a const pointer to the underlying pixel data.

Returns:

Const pointer to the first pixel in the image data

inline int sensor_bit_depth() const noexcept
inline void set_sensor_bit_depth(int bits) noexcept
void clear()

Clears the image, removing all pixel data.

After calling this method, the image will be empty with zero width and height.

void fill(const PixelT &value)

Fills all pixels in the image with the specified value.

Parameters:

value – The value to set for all pixels

inline void reset(const PixelT &value = PixelT{0})