template<UnsignedInt dimensions>
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};

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 }

Constructors, destructors, conversion operators

ImageView(PixelStorage storage, PixelFormat format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> data) explicit noexcept
Constructor.
ImageView(PixelFormat format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> 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<const void> 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<const void> 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 T, class U>
ImageView(PixelStorage storage, T format, U formatExtra, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> data) explicit noexcept
Construct an image view with implementation-specific pixel format.
template<class T>
ImageView(PixelStorage storage, T format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> data) explicit noexcept
Construct an image view with implementation-specific pixel format.
template<class T, class U>
ImageView(T format, U formatExtra, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> data) explicit noexcept
Construct an image view with implementation-specific pixel format.
template<class T>
ImageView(T format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> data) explicit noexcept
Construct an image view with implementation-specific pixel format.
template<class T, class U>
ImageView(PixelStorage storage, T format, U formatExtra, const VectorTypeFor<dimensions, Int>& size) explicit noexcept
Construct an empty view with implementation-specific pixel format.
template<class T>
ImageView(PixelStorage storage, T format, const VectorTypeFor<dimensions, Int>& size) explicit noexcept
Construct an empty view with implementation-specific pixel format.
template<class T, class U>
ImageView(T format, U formatExtra, const VectorTypeFor<dimensions, Int>& size) explicit noexcept
Construct an empty view with implementation-specific pixel format.
template<class T>
ImageView(T format, const VectorTypeFor<dimensions, Int>& size) explicit noexcept
Construct an empty view with implementation-specific pixel format.

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 type() const -> GL::PixelType deprecated
Data type of pixel data.
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<const char>
Image data.
template<class T>
auto data() const -> const T*
void setData(Containers::ArrayView<const void> data)
Set image data.

Enum documentation

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

Enumerators
Dimensions

Image dimension count

Function documentation

template<UnsignedInt dimensions>
Magnum::ImageView<dimensions>::ImageView(PixelStorage storage, PixelFormat format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> 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>
Magnum::ImageView<dimensions>::ImageView(PixelFormat format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> 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<const void>) with default-constructed PixelStorage.

template<UnsignedInt dimensions>
Magnum::ImageView<dimensions>::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>
Magnum::ImageView<dimensions>::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>
Magnum::ImageView<dimensions>::ImageView(PixelStorage storage, UnsignedInt format, UnsignedInt formatExtra, UnsignedInt pixelSize, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> 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<const void>), 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>
Magnum::ImageView<dimensions>::ImageView(PixelStorage storage, PixelFormat format, UnsignedInt formatExtra, UnsignedInt pixelSize, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> 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>
Magnum::ImageView<dimensions>::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>
Magnum::ImageView<dimensions>::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> template<class T, class U>
Magnum::ImageView<dimensions>::ImageView(PixelStorage storage, T format, U formatExtra, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> 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<const void>) with calculated pixel size.

template<UnsignedInt dimensions> template<class T>
Magnum::ImageView<dimensions>::ImageView(PixelStorage storage, T format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> 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<const void>) with calculated pixel size and formatExtra set to 0.

template<UnsignedInt dimensions> template<class T, class U>
Magnum::ImageView<dimensions>::ImageView(T format, U formatExtra, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> 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, T, U, const VectorTypeFor<dimensions, Int>&, Containers::ArrayView<const void>) with default-constructed PixelStorage.

template<UnsignedInt dimensions> template<class T>
Magnum::ImageView<dimensions>::ImageView(T format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> 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, T, const VectorTypeFor<dimensions, Int>&, Containers::ArrayView<const void>) with default-constructed PixelStorage.

template<UnsignedInt dimensions> template<class T, class U>
Magnum::ImageView<dimensions>::ImageView(PixelStorage storage, T format, U 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> template<class T>
Magnum::ImageView<dimensions>::ImageView(PixelStorage storage, T 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> template<class T, class U>
Magnum::ImageView<dimensions>::ImageView(T format, U 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, T, U, const VectorTypeFor<dimensions, Int>&, Containers::ArrayView<const void>) with default-constructed PixelStorage.

template<UnsignedInt dimensions> template<class T>
Magnum::ImageView<dimensions>::ImageView(T 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, T, const VectorTypeFor<dimensions, Int>&) with default-constructed PixelStorage.

template<UnsignedInt dimensions>
PixelFormat Magnum::ImageView<dimensions>::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>
UnsignedInt Magnum::ImageView<dimensions>::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>
GL::PixelType Magnum::ImageView<dimensions>::type() const

Data type of pixel data.

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

Pixel size (in bytes)

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

Image data properties.

See PixelStorage::dataProperties() for more information.

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

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

template<UnsignedInt dimensions>
void Magnum::ImageView<dimensions>::setData(Containers::ArrayView<const void> data)

Set image data.

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