template<UnsignedInt dimensions, class T>
Magnum::ImageView class

Image view.

Non-owning view on multi-dimensional image data together with layout and pixel format description. Unlike Image, this class doesn't take ownership of the data, so it is targeted for wrapping data that is either stored in stack/constant memory (and shouldn't be deleted) or is managed by something else.

This class can act as drop-in replacement for Image or Trade::ImageData, these two are additionally implicitly convertible to it. Particular graphics API wrappers provide additional image classes, for example GL::BufferImage. See also CompressedImageView for equivalent functionality targeted on compressed image formats.

Basic usage

Usually, the view is created on some pre-existing data array in order to describe its layout, with pixel format being one of the values from the generic PixelFormat:

ImageView2D image{PixelFormat::RGBA8Unorm, {512, 256}, data};

On construction, the image view internally calculates pixel size corresponding to given pixel format using pixelSize(). This value is needed to check that the passed data array is large enough and is also required by most image manipulation operations.

It's also possible to create an empty view and assign the memory later. That is useful for example in case of multi-buffered video streaming, where each frame has the same properties but a different memory location:

ImageView2D frame{PixelFormat::RGBA8Unorm, {512, 256}};

frame.setData(evenFrameData);
// Use even frame data ...

frame.setData(oddFrameData);
// Use odd frame data ...

It's possible to have views on image sub-rectangles, 3D texture slices or images with over-aligned rows by passing a particular PixelStorage as first parameter. In the following snippet, the view is the center 25x25 sub-rectangle of a 75x75 8-bit RGB image , with rows aligned to four bytes:

ImageView2D image{
    PixelStorage{}
        .setRowLength(75)
        .setAlignment(4)
        .setSkip({25, 25, 0}),
    PixelFormat::RGBA8Unorm, {25, 25}, data};

Data mutability

When using types derived from BasicImageView (e.g. ImageView2D), the viewed data are immutable. This is the most common use case. In order to be able to mutate the underlying data (for example in order to read into a pre-allocated memory), use BasicMutableImageView (e.g. MutableImageView2D) instead. Image and Trade::ImageData are convertible to either of these. Similarly to Corrade::Containers::ArrayView etc., a mutable view is also implicitly convertible to a const one.

Implementation-specific formats

For known graphics APIs, there's a set of utility functions converting from PixelFormat to implementation-specific format identifiers and such conversion is done implicitly when passing the view to a particular API. See the enum documentation and documentation of its values for more information.

In some cases, for example when there's no corresponding generic format available, it's desirable to specify the pixel format using implementation-specific identifiers directly. In case of OpenGL that would be the GL::PixelFormat and GL::PixelType pair:

ImageView2D image{GL::PixelFormat::DepthComponent,
                  GL::PixelType::UnsignedInt, {512, 256}, data};

In such cases, pixel size is calculated using either pixelSize(T, U) or pixelSize(T) that is found using ADL, with T and U corresponding to types of passed arguments. The implementation-specific format is wrapped in PixelFormat using pixelFormatWrap() and format() returns the wrapped value. In order to distinguish if the format is wrapped, use isPixelFormatImplementationSpecific() and then extract the implementation-specific identifier using pixelFormatUnwrap(). For APIs that have an additional format specifier (such as OpenGL), the second value is stored verbatim in formatExtra():

auto format = pixelFormatUnwrap<GLenum>(image.format());
auto type = GLenum(image.formatExtra());

As a final fallback, types for which the pixelSize() overload is not available can be specified directly together with pixel size. In particular, pixel size of 0 will cause the image to be treated as fully opaque data, disabling all slicing operations. The following shows a image view using Metal-specific format identifier:

/* Default pixel storage, 8-bit sRGB + alpha, four bytes per pixel */
ImageView2D view{{}, MTLPixelFormatRGBA8Unorm_sRGB, {}, 4, {256, 256}, data};

Public types

enum (anonymous): UnsignedInt { Dimensions = dimensions }
using Type = T
Raw data type.
using ErasedType = std::conditional<std::is_const<T>::value, const void, void>::type
Erased data type.

Constructors, destructors, conversion operators

ImageView(PixelStorage storage, PixelFormat format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept
Constructor.
ImageView(PixelFormat format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept
Constructor.
ImageView(PixelStorage storage, PixelFormat format, const VectorTypeFor<dimensions, Int>& size) explicit noexcept
Construct an empty view.
ImageView(PixelFormat format, const VectorTypeFor<dimensions, Int>& size) explicit noexcept
Construct an empty view.
ImageView(PixelStorage storage, UnsignedInt format, UnsignedInt formatExtra, UnsignedInt pixelSize, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept
Construct an image view with implementation-specific pixel format.
ImageView(PixelStorage storage, PixelFormat format, UnsignedInt formatExtra, UnsignedInt pixelSize, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept
ImageView(PixelStorage storage, UnsignedInt format, UnsignedInt formatExtra, UnsignedInt pixelSize, const VectorTypeFor<dimensions, Int>& size) explicit noexcept
Construct an empty view with implementation-specific pixel format.
ImageView(PixelStorage storage, PixelFormat format, UnsignedInt formatExtra, UnsignedInt pixelSize, const VectorTypeFor<dimensions, Int>& size) explicit noexcept
template<class U, class V>
ImageView(PixelStorage storage, U format, V formatExtra, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept
Construct an image view with implementation-specific pixel format.
template<class U>
ImageView(PixelStorage storage, U format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept
Construct an image view with implementation-specific pixel format.
template<class U, class V>
ImageView(U format, V formatExtra, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept
Construct an image view with implementation-specific pixel format.
template<class U>
ImageView(U format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept
Construct an image view with implementation-specific pixel format.
template<class U, class V>
ImageView(PixelStorage storage, U format, V formatExtra, const VectorTypeFor<dimensions, Int>& size) explicit noexcept
Construct an empty view with implementation-specific pixel format.
template<class U>
ImageView(PixelStorage storage, U format, const VectorTypeFor<dimensions, Int>& size) explicit noexcept
Construct an empty view with implementation-specific pixel format.
template<class U, class V>
ImageView(U format, V formatExtra, const VectorTypeFor<dimensions, Int>& size) explicit noexcept
Construct an empty view with implementation-specific pixel format.
template<class U>
ImageView(U format, const VectorTypeFor<dimensions, Int>& size) explicit noexcept
Construct an empty view with implementation-specific pixel format.
template<UnsignedInt otherDimensions>
ImageView(const ImageView<otherDimensions, T>& other) noexcept new in 2019.10
Construct from a view of lower dimension count.
template<class U, class = typename std::enable_if<std::is_const<T>::value&& !std::is_const<U>::value>::type>
ImageView(const ImageView<dimensions, U>& other) noexcept new in 2019.10
Convert a mutable view to a const one.

Public functions

auto storage() const -> PixelStorage
Storage of pixel data.
auto format() const -> PixelFormat
Format of pixel data.
auto formatExtra() const -> UnsignedInt
Additional pixel format specifier.
auto pixelSize() const -> UnsignedInt
Pixel size (in bytes)
auto size() const -> VectorTypeFor<dimensions, Int> constexpr
Image size.
auto dataProperties() const -> std::pair<VectorTypeFor<dimensions, std::size_t>, VectorTypeFor<dimensions, std::size_t>>
Image data properties.
auto data() const -> Containers::ArrayView<Type>
Image data.
template<class U>
auto data() const -> const U* deprecated in 2019.10
Image data in a particular type.
void setData(Containers::ArrayView<ErasedType> data)
Set image data.
auto pixels() const -> Containers::StridedArrayView<dimensions+1, Type> new in 2019.10
View on pixel data.
template<class U>
auto pixels() const -> Containers::StridedArrayView<dimensions, typename std::conditional<std::is_const<Type>::value, typename std::add_const<U>::type, U>::type> new in 2019.10
View on pixel data with a concrete pixel type.

Enum documentation

template<UnsignedInt dimensions, class T>
enum Magnum::ImageView<dimensions, T>::(anonymous): UnsignedInt

Enumerators
Dimensions

Image dimension count

Typedef documentation

template<UnsignedInt dimensions, class T>
typedef T Magnum::ImageView<dimensions, T>::Type

Raw data type.

const char for BasicImageView and char for BasicMutableImageView. See also ErasedType.

template<UnsignedInt dimensions, class T>
typedef std::conditional<std::is_const<T>::value, const void, void>::type Magnum::ImageView<dimensions, T>::ErasedType

Erased data type.

const void for BasicImageView and const void for BasicMutableImageView. See also Type.

Function documentation

template<UnsignedInt dimensions, class T>
Magnum::ImageView<dimensions, T>::ImageView(PixelStorage storage, PixelFormat format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept

Constructor.

Parameters
storage Storage of pixel data
format Format of pixel data
size Image size
data Image data

The data array is expected to be of proper size for given parameters.

template<UnsignedInt dimensions, class T>
Magnum::ImageView<dimensions, T>::ImageView(PixelFormat format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept

Constructor.

Parameters
format Format of pixel data
size Image size
data Image data

Equivalent to calling ImageView(PixelStorage, PixelFormat, const VectorTypeFor<dimensions, Int>&, Containers::ArrayView<ErasedType>) with default-constructed PixelStorage.

template<UnsignedInt dimensions, class T>
Magnum::ImageView<dimensions, T>::ImageView(PixelStorage storage, PixelFormat format, const VectorTypeFor<dimensions, Int>& size) explicit noexcept

Construct an empty view.

Parameters
storage Storage of pixel data
format Format of pixel data
size Image size

Data pointer is set to nullptr, call setData() to assign a memory view to the image.

template<UnsignedInt dimensions, class T>
Magnum::ImageView<dimensions, T>::ImageView(PixelFormat format, const VectorTypeFor<dimensions, Int>& size) explicit noexcept

Construct an empty view.

Parameters
format Format of pixel data
size Image size

Equivalent to calling ImageView(PixelStorage, PixelFormat, const VectorTypeFor<dimensions, Int>&) with default-constructed PixelStorage.

template<UnsignedInt dimensions, class T>
Magnum::ImageView<dimensions, T>::ImageView(PixelStorage storage, UnsignedInt format, UnsignedInt formatExtra, UnsignedInt pixelSize, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept

Construct an image view with implementation-specific pixel format.

Parameters
storage Storage of pixel data
format Format of pixel data
formatExtra Additional pixel format specifier
pixelSize Size of a pixel in given format
size Image size
data Image data

Unlike with ImageView(PixelStorage, PixelFormat, const VectorTypeFor<dimensions, Int>&, Containers::ArrayView<ErasedType>), where pixel size is calculated automatically using pixelSize(PixelFormat), this allows you to specify an implementation-specific pixel format and pixel size directly. Uses pixelFormatWrap() internally to wrap format in PixelFormat.

The data array is expected to be of proper size for given parameters.

template<UnsignedInt dimensions, class T>
Magnum::ImageView<dimensions, T>::ImageView(PixelStorage storage, PixelFormat format, UnsignedInt formatExtra, UnsignedInt pixelSize, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Equivalent to the above for format already wrapped with pixelFormatWrap().

template<UnsignedInt dimensions, class T>
Magnum::ImageView<dimensions, T>::ImageView(PixelStorage storage, UnsignedInt format, UnsignedInt formatExtra, UnsignedInt pixelSize, const VectorTypeFor<dimensions, Int>& size) explicit noexcept

Construct an empty view with implementation-specific pixel format.

Parameters
storage Storage of pixel data
format Format of pixel data
formatExtra Additional pixel format specifier
pixelSize Size of a pixel in given format
size Image size

Unlike with ImageView(PixelStorage, PixelFormat, const VectorTypeFor<dimensions, Int>&), where pixel size is calculated automatically using pixelSize(PixelFormat), this allows you to specify an implementation-specific pixel format and pixel size directly. Uses pixelFormatWrap() internally to wrap format in PixelFormat.

Data pointer is set to nullptr, call setData() to assign a memory view to the image.

template<UnsignedInt dimensions, class T>
Magnum::ImageView<dimensions, T>::ImageView(PixelStorage storage, PixelFormat format, UnsignedInt formatExtra, UnsignedInt pixelSize, const VectorTypeFor<dimensions, Int>& size) explicit noexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Equivalent to the above for format already wrapped with pixelFormatWrap().

template<UnsignedInt dimensions, class T> template<class U, class V>
Magnum::ImageView<dimensions, T>::ImageView(PixelStorage storage, U format, V formatExtra, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept

Construct an image view with implementation-specific pixel format.

Parameters
storage Storage of pixel data
format Format of pixel data
formatExtra Additional pixel format specifier
size Image size
data Image data

Uses ADL to find a corresponding pixelSize(T, U) overload, then calls ImageView(PixelStorage, UnsignedInt, UnsignedInt, UnsignedInt, const VectorTypeFor<dimensions, Int>&, Containers::ArrayView<ErasedType>) with calculated pixel size.

template<UnsignedInt dimensions, class T> template<class U>
Magnum::ImageView<dimensions, T>::ImageView(PixelStorage storage, U format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept

Construct an image view with implementation-specific pixel format.

Parameters
storage Storage of pixel data
format Format of pixel data
size Image size
data Image data

Uses ADL to find a corresponding pixelSize(T) overload, then calls ImageView(PixelStorage, UnsignedInt, UnsignedInt, UnsignedInt, const VectorTypeFor<dimensions, Int>&, Containers::ArrayView<ErasedType>) with calculated pixel size and formatExtra set to 0.

template<UnsignedInt dimensions, class T> template<class U, class V>
Magnum::ImageView<dimensions, T>::ImageView(U format, V formatExtra, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept

Construct an image view with implementation-specific pixel format.

Parameters
format Format of pixel data
formatExtra Additional pixel format specifier
size Image size
data Image data

Equivalent to calling ImageView(PixelStorage, U, V, const VectorTypeFor<dimensions, Int>&, Containers::ArrayView<ErasedType>) with default-constructed PixelStorage.

template<UnsignedInt dimensions, class T> template<class U>
Magnum::ImageView<dimensions, T>::ImageView(U format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<ErasedType> data) explicit noexcept

Construct an image view with implementation-specific pixel format.

Parameters
format Format of pixel data
size Image size
data Image data

Equivalent to calling ImageView(PixelStorage, U, const VectorTypeFor<dimensions, Int>&, Containers::ArrayView<ErasedType>) with default-constructed PixelStorage.

template<UnsignedInt dimensions, class T> template<class U, class V>
Magnum::ImageView<dimensions, T>::ImageView(PixelStorage storage, U format, V formatExtra, const VectorTypeFor<dimensions, Int>& size) explicit noexcept

Construct an empty view with implementation-specific pixel format.

Parameters
storage Storage of pixel data
format Format of pixel data
formatExtra Additional pixel format specifier
size Image size

Uses ADL to find a corresponding pixelSize(T, U) overload, then calls ImageView(PixelStorage, UnsignedInt, UnsignedInt, UnsignedInt, const VectorTypeFor<dimensions, Int>&) with calculated pixel size.

Data pointer is set to nullptr, call setData() to assign a memory view to the image.

template<UnsignedInt dimensions, class T> template<class U>
Magnum::ImageView<dimensions, T>::ImageView(PixelStorage storage, U format, const VectorTypeFor<dimensions, Int>& size) explicit noexcept

Construct an empty view with implementation-specific pixel format.

Parameters
storage Storage of pixel data
format Format of pixel data
size Image size

Uses ADL to find a corresponding pixelSize(T) overload, then calls ImageView(PixelStorage, UnsignedInt, UnsignedInt, UnsignedInt, const VectorTypeFor<dimensions, Int>&) with calculated pixel size and formatExtra set to 0.

Data pointer is set to nullptr, call setData() to assign a memory view to the image.

template<UnsignedInt dimensions, class T> template<class U, class V>
Magnum::ImageView<dimensions, T>::ImageView(U format, V formatExtra, const VectorTypeFor<dimensions, Int>& size) explicit noexcept

Construct an empty view with implementation-specific pixel format.

Parameters
format Format of pixel data
formatExtra Additional pixel format specifier
size Image size

Equivalent to calling ImageView(PixelStorage, U, V, const VectorTypeFor<dimensions, Int>&, Containers::ArrayView<ErasedType>) with default-constructed PixelStorage.

template<UnsignedInt dimensions, class T> template<class U>
Magnum::ImageView<dimensions, T>::ImageView(U format, const VectorTypeFor<dimensions, Int>& size) explicit noexcept

Construct an empty view with implementation-specific pixel format.

Parameters
format Format of pixel data
size Image size

Equivalent to calling ImageView(PixelStorage, U, const VectorTypeFor<dimensions, Int>&) with default-constructed PixelStorage.

template<UnsignedInt dimensions, class T>
PixelFormat Magnum::ImageView<dimensions, T>::format() const

Format of pixel data.

Returns either a defined value from the PixelFormat enum or a wrapped implementation-specific value. Use isPixelFormatImplementationSpecific() to distinguish the case and pixelFormatUnwrap() to extract an implementation-specific value, if needed.

template<UnsignedInt dimensions, class T>
UnsignedInt Magnum::ImageView<dimensions, T>::formatExtra() const

Additional pixel format specifier.

Some implementations (such as OpenGL) define a pixel format using two values. This field contains the second implementation-specific value verbatim, if any. See format() for more information.

template<UnsignedInt dimensions, class T>
UnsignedInt Magnum::ImageView<dimensions, T>::pixelSize() const

Pixel size (in bytes)

template<UnsignedInt dimensions, class T>
std::pair<VectorTypeFor<dimensions, std::size_t>, VectorTypeFor<dimensions, std::size_t>> Magnum::ImageView<dimensions, T>::dataProperties() const

Image data properties.

See PixelStorage::dataProperties() for more information.

template<UnsignedInt dimensions, class T>
Containers::ArrayView<Type> Magnum::ImageView<dimensions, T>::data() const

Image data.

template<UnsignedInt dimensions, class T> template<class U>
const U* Magnum::ImageView<dimensions, T>::data() const

Image data in a particular type.

template<UnsignedInt dimensions, class T>
void Magnum::ImageView<dimensions, T>::setData(Containers::ArrayView<ErasedType> data)

Set image data.

The data array is expected to be of proper size for parameters specified in the constructor.

template<UnsignedInt dimensions, class T>
Containers::StridedArrayView<dimensions+1, Type> Magnum::ImageView<dimensions, T>::pixels() const new in 2019.10

View on pixel data.

Provides direct and easy-to-use access to image pixels. See Obtaining a view on pixel data for more information. If the view is empty (with data() being nullptr), returns nullptr as well.

template<UnsignedInt dimensions, class T> template<class U>
Containers::StridedArrayView<dimensions, typename std::conditional<std::is_const<Type>::value, typename std::add_const<U>::type, U>::type> Magnum::ImageView<dimensions, T>::pixels() const new in 2019.10

View on pixel data with a concrete pixel type.

Compared to non-templated pixels() in addition casts the pixel data to a specified type. The user is responsible for choosing correct type for given format() — checking it on the library side is not possible for the general case. If the view is empty (with data() being nullptr), returns nullptr as well.