UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
ufo::Image< T > Class Template Reference

Image class for storing and manipulating 2D pixel data. More...

#include <lib/vision/include/ufo/vision/image.hpp>

Public Types

using const_iterator = T const *
 The type of a const iterator to an element.
 
using const_pointer = T const *
 The type of a const pointer to an element.
 
using const_reference = T const &
 The type of a const reference to an element.
 
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 The type of a const reverse iterator to an element.
 
using difference_type = std::ptrdiff_t
 The type of the difference between two sizes.
 
using iterator = T *
 The type of an iterator to an element.
 
using pointer = T *
 The type of a pointer to an element.
 
using reference = T &
 The type of a reference to an element.
 
using reverse_iterator = std::reverse_iterator< iterator >
 The type of a reverse iterator to an element.
 
using size_type = std::size_t
 The type of the size of the image.
 
using value_type = T
 The type of the elements stored in the image.
 

Public Member Functions

 Image ()=default
 Constructs an empty image with no pixels (rows = cols = 0).
 
 Image (Image &&)=default
 Move-constructs an image, transferring ownership of pixel data.
 
 Image (Image const &other)
 Copy-constructs an image, performing a deep copy of all pixel data.
 
 Image (size_type rows, size_type cols, T const &value=T{})
 Constructs an image with the given dimensions, filling every pixel with value.
 
auto & at (this auto &self, size_type row, size_type col)
 Returns a (const) reference to the pixel at (row, col) with bounds checking.
 
auto begin (this auto &self) noexcept
 Returns an iterator to the first pixel.
 
const_iterator cbegin () const noexcept
 Returns a const iterator to the first pixel.
 
const_iterator cend () const noexcept
 Returns a const iterator one past the last pixel.
 
void clear () noexcept
 Clears the image, releasing all memory.
 
constexpr size_type cols () const noexcept
 Returns the number of columns (width) in the image.
 
auto column (this auto &self, size_type c) noexcept
 Returns a lazy view over all pixels in the given column.
 
const_reverse_iterator crbegin () const noexcept
 Returns a const reverse iterator to the last pixel.
 
const_reverse_iterator crend () const noexcept
 Returns a const reverse iterator to one before the first pixel.
 
auto data (this auto &self) noexcept
 Returns a pointer to the underlying contiguous pixel array.
 
void downscale (float factor)
 Downscales the image by the given factor.
 
bool empty () const noexcept
 Returns true if the image has no pixels (i.e., size() == 0).
 
auto end (this auto &self) noexcept
 Returns an iterator one past the last pixel.
 
void fill (T const &value)
 Fills the image with the given value.
 
void flipHorizontal () noexcept
 Flips the image horizontally in-place.
 
void flipVertical () noexcept
 Flips the image vertically in-place.
 
constexpr size_type index (size_type row, size_type col) const noexcept
 Computes the flat row-major index for position (row, col).
 
Imageoperator= (Image &&)=default
 Move-assigns from another image, transferring ownership of pixel data.
 
Imageoperator= (Image const &rhs)
 Copy-assigns from another image, performing a deep copy of all pixel data.
 
auto & operator[] (this auto &self, size_type index)
 Returns a (const) reference to the pixel at the given flat index.
 
auto & operator[] (this auto &self, size_type row, size_type col)
 Returns a (const) reference to the pixel at (row, col).
 
auto rbegin (this auto &self) noexcept
 Returns a reverse iterator to the last pixel.
 
auto rend (this auto &self) noexcept
 Returns a reverse iterator to one before the first pixel.
 
void rescale (size_type rows, size_type cols)
 Rescales the image to the given dimensions using bilinear interpolation.
 
Image rescaled (size_type rows, size_type cols) const
 Returns a rescaled copy of the image using bilinear interpolation.
 
void resize (size_type rows, size_type cols)
 Resizes the image to the given dimensions.
 
Image rotate90 (bool clockwise=true) const
 Returns a rotated copy of the image.
 
std::span< T const > row (size_type r) const noexcept
 Returns a const span over all pixels in the given row.
 
std::span< T > row (size_type r) noexcept
 Returns a span over all pixels in the given row.
 
constexpr size_type rows () const noexcept
 Returns the number of rows (height) in the image.
 
sample (float r, float c) const
 Samples the image at (row, col) using bilinear interpolation.
 
samplePixel (float r, float c) const
 Samples the image at pixel (row, col) using bilinear interpolation.
 
constexpr size_type size () const noexcept
 Returns the total number of pixels (rows() * cols()).
 
void swap (Image &other) noexcept
 Swaps the contents of this image with other.
 
Image transposed () const
 Returns a transposed copy of the image.
 
void upscale (float factor)
 Upscales the image by the given factor.
 

Detailed Description

template<class T>
class ufo::Image< T >

Image class for storing and manipulating 2D pixel data.

Template Parameters
TThe pixel type. Usually a ufo::Color or scalar.

Image owns its pixel data and provides both flat (single-index) and two-dimensional (row, col) element access. The storage layout is contiguous and row-major, meaning that pixels in the same row are adjacent in memory.

The class satisfies std::ranges::contiguous_range, so it can be used directly with range algorithms and range-based for loops.

Definition at line 77 of file image.hpp.

Member Typedef Documentation

◆ const_iterator

template<class T >
using ufo::Image< T >::const_iterator = T const*

The type of a const iterator to an element.

Definition at line 115 of file image.hpp.

◆ const_pointer

template<class T >
using ufo::Image< T >::const_pointer = T const*

The type of a const pointer to an element.

Definition at line 107 of file image.hpp.

◆ const_reference

template<class T >
using ufo::Image< T >::const_reference = T const&

The type of a const reference to an element.

Definition at line 99 of file image.hpp.

◆ const_reverse_iterator

template<class T >
using ufo::Image< T >::const_reverse_iterator = std::reverse_iterator<const_iterator>

The type of a const reverse iterator to an element.

Definition at line 123 of file image.hpp.

◆ difference_type

template<class T >
using ufo::Image< T >::difference_type = std::ptrdiff_t

The type of the difference between two sizes.

Definition at line 91 of file image.hpp.

◆ iterator

template<class T >
using ufo::Image< T >::iterator = T*

The type of an iterator to an element.

Definition at line 111 of file image.hpp.

◆ pointer

template<class T >
using ufo::Image< T >::pointer = T*

The type of a pointer to an element.

Definition at line 103 of file image.hpp.

◆ reference

template<class T >
using ufo::Image< T >::reference = T&

The type of a reference to an element.

Definition at line 95 of file image.hpp.

◆ reverse_iterator

template<class T >
using ufo::Image< T >::reverse_iterator = std::reverse_iterator<iterator>

The type of a reverse iterator to an element.

Definition at line 119 of file image.hpp.

◆ size_type

template<class T >
using ufo::Image< T >::size_type = std::size_t

The type of the size of the image.

Definition at line 87 of file image.hpp.

◆ value_type

template<class T >
using ufo::Image< T >::value_type = T

The type of the elements stored in the image.

Definition at line 83 of file image.hpp.

Constructor & Destructor Documentation

◆ Image() [1/3]

template<class T >
ufo::Image< T >::Image ( Image< T > const &  other)
inline

Copy-constructs an image, performing a deep copy of all pixel data.

Parameters
[in]otherThe image to copy from.

Definition at line 134 of file image.hpp.

◆ Image() [2/3]

template<class T >
ufo::Image< T >::Image ( Image< T > &&  )
default

Move-constructs an image, transferring ownership of pixel data.

The moved-from image is left in a valid but empty state.

◆ Image() [3/3]

template<class T >
ufo::Image< T >::Image ( size_type  rows,
size_type  cols,
T const &  value = T{} 
)
inline

Constructs an image with the given dimensions, filling every pixel with value.

Parameters
[in]rowsNumber of rows (height).
[in]colsNumber of columns (width).
[in]valueInitial value for every pixel. Defaults to a value-initialized T.

Definition at line 156 of file image.hpp.

Member Function Documentation

◆ at()

template<class T >
auto & ufo::Image< T >::at ( this auto &  self,
size_type  row,
size_type  col 
)
inline

Returns a (const) reference to the pixel at (row, col) with bounds checking.

Parameters
[in]selfThe image.
[in]rowRow index.
[in]colColumn index.
Return values
referenceif self is a mutable reference.
const_referenceif self is a const reference.
Exceptions
std::out_of_rangeif row >= rows() or col >= cols().

Definition at line 224 of file image.hpp.

◆ begin()

template<class T >
auto ufo::Image< T >::begin ( this auto &  self)
inlinenoexcept

Returns an iterator to the first pixel.

Parameters
[in]selfThe image.
Return values
iteratorif self is a mutable reference.
const_iteratorif self is a const reference

Definition at line 282 of file image.hpp.

◆ cbegin()

template<class T >
const_iterator ufo::Image< T >::cbegin ( ) const
inlinenoexcept

Returns a const iterator to the first pixel.

Returns
A const iterator to the first pixel.

Definition at line 288 of file image.hpp.

◆ cend()

template<class T >
const_iterator ufo::Image< T >::cend ( ) const
inlinenoexcept

Returns a const iterator one past the last pixel.

Returns
A const iterator one past the last pixel.

Definition at line 322 of file image.hpp.

◆ clear()

template<class T >
void ufo::Image< T >::clear ( )
inlinenoexcept

Clears the image, releasing all memory.

Definition at line 429 of file image.hpp.

◆ cols()

template<class T >
constexpr size_type ufo::Image< T >::cols ( ) const
inlineconstexprnoexcept

Returns the number of columns (width) in the image.

Returns
The number of columns.

Definition at line 368 of file image.hpp.

◆ column()

template<class T >
auto ufo::Image< T >::column ( this auto &  self,
size_type  c 
)
inlinenoexcept

Returns a lazy view over all pixels in the given column.

Parameters
[in]selfThe image.
[in]cColumn index in [0, cols()). Passing an out-of-range index is undefined behavior.
Returns
A range of rows() T (const) references, one per row, in top-to-bottom order.

Because storage is row-major, column pixels are not contiguous in memory (they are cols() elements apart). A view is returned rather than a span, yielding references directly into the image's storage with no copying.

Definition at line 269 of file image.hpp.

◆ crbegin()

template<class T >
const_reverse_iterator ufo::Image< T >::crbegin ( ) const
inlinenoexcept

Returns a const reverse iterator to the last pixel.

Returns
A const reverse iterator to the last pixel.

Definition at line 305 of file image.hpp.

◆ crend()

template<class T >
const_reverse_iterator ufo::Image< T >::crend ( ) const
inlinenoexcept

Returns a const reverse iterator to one before the first pixel.

Returns
A const reverse iterator to one before the first pixel.

Definition at line 339 of file image.hpp.

◆ data()

template<class T >
auto ufo::Image< T >::data ( this auto &  self)
inlinenoexcept

Returns a pointer to the underlying contiguous pixel array.

Parameters
[in]selfThe image.
Return values
pointerto the underlying contiguous pixel array if self is a mutable reference.
const_pointerto the underlying contiguous pixel array if self is a const reference

Definition at line 349 of file image.hpp.

◆ downscale()

template<class T >
void ufo::Image< T >::downscale ( float  factor)
inline

Downscales the image by the given factor.

Parameters
[in]factorDownscale factor (> 1).

Definition at line 602 of file image.hpp.

◆ empty()

template<class T >
bool ufo::Image< T >::empty ( ) const
inlinenoexcept

Returns true if the image has no pixels (i.e., size() == 0).

Return values
trueif the image has no pixels.
falseif the image has pixels.

Definition at line 356 of file image.hpp.

◆ end()

template<class T >
auto ufo::Image< T >::end ( this auto &  self)
inlinenoexcept

Returns an iterator one past the last pixel.

Parameters
[in]selfThe image.
Return values
iteratorif self is a mutable reference.
const_iteratorif self is a const reference

Definition at line 313 of file image.hpp.

◆ fill()

template<class T >
void ufo::Image< T >::fill ( T const &  value)
inline

Fills the image with the given value.

Parameters
[in]valueThe value to fill the image with.

Definition at line 424 of file image.hpp.

◆ flipHorizontal()

template<class T >
void ufo::Image< T >::flipHorizontal ( )
inlinenoexcept

Flips the image horizontally in-place.

Definition at line 454 of file image.hpp.

◆ flipVertical()

template<class T >
void ufo::Image< T >::flipVertical ( )
inlinenoexcept

Flips the image vertically in-place.

Definition at line 465 of file image.hpp.

◆ index()

template<class T >
constexpr size_type ufo::Image< T >::index ( size_type  row,
size_type  col 
) const
inlineconstexprnoexcept

Computes the flat row-major index for position (row, col).

Parameters
[in]rowRow index.
[in]colColumn index.
Returns
Flat index into the pixel array.

Equivalent to row * cols() + col. No bounds checking is performed.

Definition at line 384 of file image.hpp.

◆ operator=() [1/2]

template<class T >
Image & ufo::Image< T >::operator= ( Image< T > &&  )
default

Move-assigns from another image, transferring ownership of pixel data.

Returns
Reference to this image.

The moved-from image is left in a valid but empty state.

◆ operator=() [2/2]

template<class T >
Image & ufo::Image< T >::operator= ( Image< T > const &  rhs)
inline

Copy-assigns from another image, performing a deep copy of all pixel data.

Parameters
[in]rhsThe image to copy from.
Returns
Reference to this image.

Self-assignment is a no-op.

Definition at line 169 of file image.hpp.

◆ operator[]() [1/2]

template<class T >
auto & ufo::Image< T >::operator[] ( this auto &  self,
size_type  index 
)
inline

Returns a (const) reference to the pixel at the given flat index.

Parameters
[in]selfThe image.
[in]indexFlat index in [0, size()).
Return values
referenceif self is a mutable reference.
const_referenceif self is a const reference.
Note
No bounds checking is performed.

The flat index corresponds to row-major storage: index = row * cols() + col.

Definition at line 196 of file image.hpp.

◆ operator[]() [2/2]

template<class T >
auto & ufo::Image< T >::operator[] ( this auto &  self,
size_type  row,
size_type  col 
)
inline

Returns a (const) reference to the pixel at (row, col).

Parameters
[in]selfThe image.
[in]rowRow index in [0, rows()).
[in]colColumn index in [0, cols()).
Return values
referenceif self is a mutable reference.
const_referenceif self is a const reference.
Note
No bounds checking is performed.

Definition at line 210 of file image.hpp.

◆ rbegin()

template<class T >
auto ufo::Image< T >::rbegin ( this auto &  self)
inlinenoexcept

Returns a reverse iterator to the last pixel.

Parameters
[in]selfThe image.
Return values
reverse_iteratorif self is a mutable reference.
const_reverse_iteratorif self is a const reference

Definition at line 296 of file image.hpp.

◆ rend()

template<class T >
auto ufo::Image< T >::rend ( this auto &  self)
inlinenoexcept

Returns a reverse iterator to one before the first pixel.

Parameters
[in]selfThe image.
Return values
reverse_iteratorif self is a mutable reference.
const_reverse_iteratorif self is a const reference

Definition at line 330 of file image.hpp.

◆ rescale()

template<class T >
void ufo::Image< T >::rescale ( size_type  rows,
size_type  cols 
)
inline

Rescales the image to the given dimensions using bilinear interpolation.

Parameters
[in]rowsNew number of rows.
[in]colsNew number of columns.

Definition at line 561 of file image.hpp.

◆ rescaled()

template<class T >
Image ufo::Image< T >::rescaled ( size_type  rows,
size_type  cols 
) const
inline

Returns a rescaled copy of the image using bilinear interpolation.

Parameters
[in]rowsNew number of rows.
[in]colsNew number of columns.
Returns
A new rescaled image.

Definition at line 575 of file image.hpp.

◆ resize()

template<class T >
void ufo::Image< T >::resize ( size_type  rows,
size_type  cols 
)
inline

Resizes the image to the given dimensions.

Parameters
[in]rowsNew number of rows.
[in]colsNew number of columns.

Reallocates memory only if the total number of pixels changes. The contents of the image are undefined after a resize.

Definition at line 411 of file image.hpp.

◆ rotate90()

template<class T >
Image ufo::Image< T >::rotate90 ( bool  clockwise = true) const
inline

Returns a rotated copy of the image.

Parameters
[in]clockwiseIf true, rotate 90 degrees clockwise. Otherwise, counter-clockwise.
Returns
A new rotated image.

Definition at line 480 of file image.hpp.

◆ row() [1/2]

template<class T >
std::span< T const > ufo::Image< T >::row ( size_type  r) const
inlinenoexcept

Returns a const span over all pixels in the given row.

Parameters
rRow index in [0, rows()).
Returns
A std::span<T const> covering the cols() pixels in row r.

Definition at line 252 of file image.hpp.

◆ row() [2/2]

template<class T >
std::span< T > ufo::Image< T >::row ( size_type  r)
inlinenoexcept

Returns a span over all pixels in the given row.

Parameters
[in]rRow index in [0, rows()). Passing an out-of-range index is undefined behavior.
Returns
A std::span<T> covering the cols() pixels in row r.

The span refers directly into the image's storage; no copy is made.

Definition at line 242 of file image.hpp.

◆ rows()

template<class T >
constexpr size_type ufo::Image< T >::rows ( ) const
inlineconstexprnoexcept

Returns the number of rows (height) in the image.

Returns
The number of rows.

Definition at line 362 of file image.hpp.

◆ sample()

template<class T >
T ufo::Image< T >::sample ( float  r,
float  c 
) const
inline

Samples the image at (row, col) using bilinear interpolation.

Parameters
[in]rNormalized row index in [0, 1].
[in]cNormalized column index in [0, 1].
Returns
The sampled value.

Coordinates are clamped to [0, 1]. The normalized coordinates are mapped to pixel coordinates by multiplying by (rows - 1) and (cols - 1).

Definition at line 508 of file image.hpp.

◆ samplePixel()

template<class T >
T ufo::Image< T >::samplePixel ( float  r,
float  c 
) const
inline

Samples the image at pixel (row, col) using bilinear interpolation.

Parameters
[in]rFloating-point row index.
[in]cFloating-point column index.
Returns
The sampled value.

Coordinates are clamped to the image boundaries.

Definition at line 546 of file image.hpp.

◆ size()

template<class T >
constexpr size_type ufo::Image< T >::size ( ) const
inlineconstexprnoexcept

Returns the total number of pixels (rows() * cols()).

Returns
The total number of pixels.

Definition at line 374 of file image.hpp.

◆ swap()

template<class T >
void ufo::Image< T >::swap ( Image< T > &  other)
inlinenoexcept

Swaps the contents of this image with other.

Parameters
[in,out]otherThe image to swap with.

Runs in O(1); no pixel data is copied.

Definition at line 395 of file image.hpp.

◆ transposed()

template<class T >
Image ufo::Image< T >::transposed ( ) const
inline

Returns a transposed copy of the image.

Returns
A new image with rows and columns swapped.

Definition at line 440 of file image.hpp.

◆ upscale()

template<class T >
void ufo::Image< T >::upscale ( float  factor)
inline

Upscales the image by the given factor.

Parameters
[in]factorUpscale factor (> 1).

Definition at line 592 of file image.hpp.


The documentation for this class was generated from the following file: