Class image_t

Class Documentation

class image_t

Pixel-interleaved, row-major image buffer with shared ownership.

Copying an Image shares the underlying buffer via shared_ptr (shallow copy, analogous to cv::Mat reference counting). Use clone() for an independent deep copy.

The element type is expressed as a runtime tag (Type::U8 or Type::F32).

Public Types

enum class Type : uint8_t

Values:

enumerator U8

uint8_t elements — 8 bits per channel

enumerator F32

float elements — 32 bits per channel

Public Functions

image_t() = default

Creates an empty image with no allocated pixel buffer.

Returns:

Empty image_t instance.

inline image_t(size_t rows_, size_t cols_, size_t channels_, Type type_ = Type::U8)

Creates an image and allocates a zero-initialized pixel buffer.

Parameters:
  • rows_ – Image height in pixels.

  • cols_ – Image width in pixels.

  • channels_ – Number of interleaved channels per pixel (for example 1 for grayscale or 3 for RGB/BGR).

  • type_ – Element datatype used per channel.

Returns:

Initialized image_t with owned storage.

image_t(const image_t &other_) = default

Copy-constructs an image with shared underlying storage.

Parameters:

other_ – Source image to copy.

Returns:

New image_t sharing the same buffer.

image_t(image_t &&other_) = default

Move-constructs an image by transferring ownership metadata.

Parameters:

other_ – Source image to move from.

Returns:

New image_t that takes ownership of the source state.

image_t &operator=(const image_t &other_) = default

Copy-assigns this image with shared underlying storage.

Parameters:

other_ – Source image to copy.

Returns:

Reference to this image.

image_t &operator=(image_t &&other_) = default

Move-assigns this image by transferring ownership metadata.

Parameters:

other_ – Source image to move from.

Returns:

Reference to this image.

inline image_t clone() const

Creates a deep copy with an independent pixel buffer.

Returns:

Deep-copied image.

inline bool empty() const noexcept

Checks whether the image has a valid buffer and dimensions.

Returns:

true when the image has no usable pixel data.

inline size_t rows() const noexcept

Returns the image height.

Returns:

Number of rows (pixels).

inline size_t cols() const noexcept

Returns the image width.

Returns:

Number of columns (pixels).

inline size_t channels() const noexcept

Returns channel count per pixel.

Returns:

Number of interleaved channels.

inline Type type() const noexcept

Returns the per-channel element datatype.

Returns:

Runtime datatype tag.

inline size_t total() const noexcept

Returns the number of pixels in the image plane.

Returns:

rows() * cols().

inline size_t elem_size() const noexcept

Returns the number of bytes used by one pixel.

Returns:

Channel count multiplied by bytes per channel element.

inline size_t size_bytes() const noexcept

Returns total allocated payload size in bytes.

Returns:

total() * elem_size().

inline uint8_t *data() noexcept

Returns mutable pointer to the first byte of image storage.

Returns:

Raw mutable byte pointer.

inline const uint8_t *data() const noexcept

Returns read-only pointer to the first byte of image storage.

Returns:

Raw const byte pointer.

template<typename T = uint8_t>
inline T *ptr(size_t row_ = 0) noexcept

Returns a typed mutable pointer to the first element of a row.

Parameters:

row_ – Row index from which pointer arithmetic starts.

Returns:

Pointer typed as T* into the internal buffer.

template<typename T = uint8_t>
inline const T *ptr(size_t row_ = 0) const noexcept

Returns a typed read-only pointer to the first element of a row.

Parameters:

row_ – Row index from which pointer arithmetic starts.

Returns:

Const pointer typed as const T* into the internal buffer.

inline const uint8_t *pixel(const size_t &row_, const size_t &col_) const noexcept

Returns pointer to the first channel byte of a pixel.

Parameters:
  • row_ – Pixel row index.

  • col_ – Pixel column index.

Returns:

Pointer to pixel start (channel 0) in interleaved storage.

inline image_t convert_to(Type new_type_) const

Converts this image to another per-channel element datatype.

Parameters:

new_type_ – Target element datatype.

Returns:

Converted image with same geometry and channel count.

inline image_t bgr2rgb() const

Swaps channel 0 and 2 for each pixel.

Returns:

Deep copy with BGR<->RGB channel order swap applied when possible.

inline image_t to_grayscale() const

Converts this image to single-channel grayscale.

Returns:

Deep-copied grayscale image.

inline image_t resize(double scaleFactor_) const

Resizes this image by uniform scale using bilinear interpolation.

Parameters:

scaleFactor_ – Positive scale factor applied to width and height.

Throws:

std::runtime_error – If image is empty or scale is non-positive.

Returns:

Resized image preserving channel count and datatype.

Public Static Functions

static inline image_t zeros(size_t rows_, size_t cols_, size_t channels_, Type type_ = Type::U8)

Creates a zero-filled image with the requested geometry.

Parameters:
  • rows_ – Image height in pixels.

  • cols_ – Image width in pixels.

  • channels_ – Number of channels per pixel.

  • type_ – Element datatype used per channel.

Returns:

Zero-initialized image_t.

static inline image_t read_image(const std::string &path_, bool grayscale_ = false)

Loads an image from disk.

Loads an image file (.png, .jpg, .jpeg) into image_t.

Parameters:
  • path_ – Input file path (.png, .jpg, or .jpeg).

  • grayscale_true to force single-channel grayscale loading.

  • path_ – Input file path.

  • grayscale_true to request grayscale output, false for color.

Throws:
  • std::runtime_error – If decoding fails or format is unsupported.

  • std::runtime_error – If format is unsupported or decoding fails.

Returns:

Decoded image in Type::U8.

Returns:

Decoded image_t in Type::U8.

static inline image_t read_depth_image(const std::string &path_)

Loads a 16-bit grayscale depth PNG into Type::F32.

Loads a 16-bit grayscale depth PNG into image_t::Type::F32.

Parameters:
  • path_ – Input file path (.png).

  • path_ – Input depth PNG file path.

Throws:
  • std::runtime_error – If the file is not a 16-bit grayscale PNG or decoding fails.

  • std::runtime_error – If the file is not a 16-bit grayscale PNG or decoding fails.

Returns:

Decoded depth image in Type::F32 with 1 channel.

Returns:

Decoded depth image in Type::F32 with 1 channel (height × width × 1).

Private Members

size_t _rows = 0

Number of image rows (height in pixels).

size_t _cols = 0

Number of image columns (width in pixels).

size_t _channels = 0

Number of interleaved channels per pixel.

Type _type = Type::U8

Runtime channel datatype.

std::shared_ptr<uint8_t[]> _data

Shared pixel buffer stored as raw bytes.

size_t _step = 0

Stride in bytes between consecutive rows.

Private Static Functions

static inline constexpr size_t bytes_per_elem(Type type_) noexcept

Returns byte size of one channel element for a datatype.

Parameters:

type_ – Channel datatype tag.

Returns:

Number of bytes per channel element.