4#ifndef GREM_RESOURCE_IMAGE_HPP
5#define GREM_RESOURCE_IMAGE_HPP
138 , mipLevelCount(mipLevelCount) {
204 return Extent2D{.width = size3D.width, .height = size3D.height};
227 [[nodiscard]]
constexpr uint32_t
getWidth() const noexcept {
236 [[nodiscard]]
constexpr uint32_t
getHeight() const noexcept {
237 return size3D.height;
245 [[nodiscard]]
constexpr uint32_t
getDepth() const noexcept {
255 return mipLevelCount;
281 [[nodiscard]]
constexpr const byte*
data() const noexcept {
282 return contents.data();
294 [[nodiscard]]
constexpr size_t size() const noexcept {
295 return contents.size();
315 template <
typename Pixel>
316 [[nodiscard]]
constexpr Pixel
readPixel(
Offset3D position, uint32_t mipLevel = 0)
const;
348 [[nodiscard]]
constexpr ImageView getLayer(uint32_t layer, uint32_t mipLevel = 0)
const;
416 [[nodiscard]] GREM_API(
resource)
Image getPadded(uint32_t paddingLeft, uint32_t paddingRight, uint32_t paddingTop, uint32_t paddingBottom)
const;
441 Extent3D size3D{.width = 0, .height = 0, .depth = 0};
442 uint32_t mipLevelCount = 0;
481 , mipLevelCount(mipLevelCount) {
586 return Extent2D{.width = size3D.width, .height = size3D.height};
609 [[nodiscard]]
constexpr uint32_t
getWidth() const noexcept {
618 [[nodiscard]]
constexpr uint32_t
getHeight() const noexcept {
619 return size3D.height;
627 [[nodiscard]]
constexpr uint32_t
getDepth() const noexcept {
637 return mipLevelCount;
663 [[nodiscard]]
constexpr byte*
data() const noexcept {
664 return contents.data();
676 [[nodiscard]]
constexpr size_t size() const noexcept {
677 return contents.size();
697 template <
typename Pixel>
698 [[nodiscard]]
constexpr Pixel
readPixel(
Offset3D position, uint32_t mipLevel = 0)
const;
796 [[nodiscard]]
Image getPadded(uint32_t paddingLeft, uint32_t paddingRight, uint32_t paddingTop, uint32_t paddingBottom)
const;
821 Extent3D size3D{.width = 0, .height = 0, .depth = 0};
822 uint32_t mipLevelCount = 0;
1567 max(size3D.width >> mipLevel, uint32_t{1}),
1568 max(size3D.height >> mipLevel, uint32_t{1}),
1587 max(size2D.
width >> mipLevel, uint32_t{1}),
1588 max(size2D.
height >> mipLevel, uint32_t{1}),
1610 throw std::length_error{
"Image size overflow."};
1612 return pixelStride *
static_cast<size_t>(width);
1629 if (blockSize.
width == 0 || blockSize.
height == 0) {
1632 const size_t blockCountX =
static_cast<size_t>((size2D.
width + blockSize.
width - 1) / blockSize.
width);
1633 const size_t blockCountY =
static_cast<size_t>((size2D.
height + blockSize.
height - 1) / blockSize.
height);
1636 throw std::length_error{
"Image size overflow."};
1638 const size_t rowStride = blockStride * blockCountX;
1640 throw std::length_error{
"Image size overflow."};
1642 return rowStride * blockCountY;
1658 if (size3D.depth == 0) {
1661 const size_t layerStride =
getLayerStride(format,
Extent2D{.width = size3D.width, .height = size3D.height});
1663 throw std::length_error{
"Image size overflow."};
1665 return layerStride *
static_cast<size_t>(size3D.depth);
1683 for (uint32_t mipLevel = 0; mipLevel < mipLevelCount; ++mipLevel) {
1686 throw std::length_error{
"Image size overflow."};
1688 result += mipLevelStride;
1706 template <
typename OutputComponent,
typename InputComponent>
1711 return static_cast<OutputComponent
>(
static_cast<float>(value));
1738 template <
typename OutputComponent,
size_t OutputComponentCount,
typename InputComponent,
size_t InputComponentCount>
1740 static_assert(OutputComponentCount >= 1 && OutputComponentCount <= 4);
1741 static_assert(InputComponentCount >= 1 && InputComponentCount <= 4);
1746 constexpr size_t COMMON_COMPONENT_COUNT =
min(InputComponentCount, OutputComponentCount);
1747 for (
size_t i = 0; i < COMMON_COMPONENT_COUNT; ++i) {
1748 InputComponent value{};
1749 memcpy(&value, input,
sizeof(InputComponent));
1750 input +=
sizeof(InputComponent);
1752 memcpy(output, &convertedValue,
sizeof(OutputComponent));
1753 output +=
sizeof(OutputComponent);
1755 if constexpr (OutputComponentCount >= 2 && InputComponentCount < 2) {
1756 memset(output, 0,
sizeof(OutputComponent));
1757 output +=
sizeof(OutputComponent);
1759 if constexpr (OutputComponentCount >= 3 && InputComponentCount < 3) {
1760 memset(output, 0,
sizeof(OutputComponent));
1761 output +=
sizeof(OutputComponent);
1763 if constexpr (OutputComponentCount >= 4 && InputComponentCount < 4) {
1765 constexpr OutputComponent ONE = 1.0f;
1766 memcpy(output, &ONE,
sizeof(OutputComponent));
1769 memcpy(output, &MAX,
sizeof(OutputComponent));
1771 output +=
sizeof(OutputComponent);
1811 template <
typename OutputComponent,
size_t OutputComponentCount,
typename InputComponent,
size_t InputComponentCount>
1814 GREM_ASSERT(outputOffset.
x >= 0 &&
static_cast<uint32_t
>(outputOffset.
x) <= outputSize.
width);
1815 GREM_ASSERT(outputOffset.
y >= 0 &&
static_cast<uint32_t
>(outputOffset.
y) <= outputSize.
height);
1816 GREM_ASSERT(outputOffset.
z >= 0 &&
static_cast<uint32_t
>(outputOffset.
z) <= outputSize.
depth);
1828 const size_t outOffsetX =
static_cast<size_t>(outputOffset.
x);
1829 const size_t inOffsetX =
static_cast<size_t>(inputRegion.
offset.x);
1830 const size_t outOffsetY =
static_cast<size_t>(outputOffset.
y);
1831 const size_t inOffsetY =
static_cast<size_t>(inputRegion.
offset.y);
1832 const size_t outOffsetZ =
static_cast<size_t>(outputOffset.
z);
1833 const size_t inOffsetZ =
static_cast<size_t>(inputRegion.
offset.z);
1834 const size_t countX =
static_cast<size_t>(inputRegion.
size.width);
1835 const size_t countY =
static_cast<size_t>(inputRegion.
size.height);
1836 const size_t countZ =
static_cast<size_t>(inputRegion.
size.depth);
1837 const size_t outStrideX = 1;
1838 const size_t inStrideX = 1;
1839 const size_t outStrideY =
static_cast<size_t>(outputSize.
width);
1840 const size_t inStrideY =
static_cast<size_t>(inputSize.
width);
1841 const size_t outStrideZ =
static_cast<size_t>(outputSize.
width) *
static_cast<size_t>(outputSize.
height);
1842 const size_t inStrideZ =
static_cast<size_t>(inputSize.
width) *
static_cast<size_t>(inputSize.
height);
1843 const size_t outPixelStride = OutputComponentCount *
sizeof(OutputComponent);
1844 const size_t inPixelStride = InputComponentCount *
sizeof(InputComponent);
1846 for (
size_t z = 0; z < countZ; ++z) {
1847 const size_t outZ = outOffsetZ + z;
1848 const size_t inZ = inOffsetZ + z;
1849 for (
size_t y = 0; y < countY; ++y) {
1850 const size_t outY = outOffsetY + y;
1851 const size_t inY = inOffsetY + y;
1852 for (
size_t x = 0; x < countX; ++x) {
1853 const size_t outX = outOffsetX + x;
1854 const size_t inX = inOffsetX + x;
1855 const size_t outIndex = outZ * outStrideZ + outY * outStrideY + outX * outStrideX;
1856 const size_t inIndex = inZ * inStrideZ + inY * inStrideY + inX * inStrideX;
1857 byte*
const outputPixel = output + outIndex * outPixelStride;
1858 const byte*
const inputPixel = input + inIndex * inPixelStride;
1893 template <
typename OutputComponent,
size_t OutputComponentCount,
typename InputComponent,
size_t InputComponentCount>
1899 const size_t outPixelStride = OutputComponentCount *
sizeof(OutputComponent);
1900 const size_t inPixelStride = InputComponentCount *
sizeof(InputComponent);
1902 for (uint32_t mipLevel = 0; mipLevel < mipLevelCount; ++mipLevel) {
1906 const size_t mipLevelPixelCount =
static_cast<size_t>(mipLevelSize.
width) *
static_cast<size_t>(mipLevelSize.
height) *
static_cast<size_t>(mipLevelSize.
depth);
1907 output += outPixelStride * mipLevelPixelCount;
1908 input += inPixelStride * mipLevelPixelCount;
1942 GREM_ASSERT(outputOffset.
x >= 0 &&
static_cast<uint32_t
>(outputOffset.
x) <= outputSize.
width);
1943 GREM_ASSERT(outputOffset.
y >= 0 &&
static_cast<uint32_t
>(outputOffset.
y) <= outputSize.
height);
1944 GREM_ASSERT(outputOffset.
z >= 0 &&
static_cast<uint32_t
>(outputOffset.
z) <= outputSize.
depth);
1957 const size_t outOffsetX =
static_cast<size_t>(outputOffset.
x);
1958 const size_t inOffsetX =
static_cast<size_t>(inputRegion.
offset.x);
1959 const size_t outOffsetY =
static_cast<size_t>(outputOffset.
y);
1960 const size_t inOffsetY =
static_cast<size_t>(inputRegion.
offset.y);
1961 const size_t outOffsetZ =
static_cast<size_t>(outputOffset.
z);
1962 const size_t inOffsetZ =
static_cast<size_t>(inputRegion.
offset.z);
1963 const size_t countX =
static_cast<size_t>(inputRegion.
size.width);
1964 const size_t countY =
static_cast<size_t>(inputRegion.
size.height);
1965 const size_t countZ =
static_cast<size_t>(inputRegion.
size.depth);
1966 const size_t outStrideX = 1;
1967 const size_t inStrideX = 1;
1968 const size_t outStrideY =
static_cast<size_t>(outputSize.
width);
1969 const size_t inStrideY =
static_cast<size_t>(inputSize.
width);
1970 const size_t outStrideZ =
static_cast<size_t>(outputSize.
width) *
static_cast<size_t>(outputSize.
height);
1971 const size_t inStrideZ =
static_cast<size_t>(inputSize.
width) *
static_cast<size_t>(inputSize.
height);
1973 for (
size_t z = 0; z < countZ; ++z) {
1974 const size_t outZ = outOffsetZ + z;
1975 const size_t inZ = inOffsetZ + z;
1976 for (
size_t y = 0; y < countY; ++y) {
1977 const size_t outY = outOffsetY + y;
1978 const size_t inY = inOffsetY + y;
1979 for (
size_t x = 0; x < countX; ++x) {
1980 const size_t outX = outOffsetX + x;
1981 const size_t inX = inOffsetX + x;
1982 const size_t outIndex = outZ * outStrideZ + outY * outStrideY + outX * outStrideX;
1983 const size_t inIndex = inZ * inStrideZ + inY * inStrideY + inX * inStrideX;
1984 byte*
const outputPixel = output + outIndex * pixelStride;
1985 const byte*
const inputPixel = input + inIndex * pixelStride;
1986 memcpy(outputPixel, inputPixel, pixelStride);
2012 static void copyPixels(
Extent3D size3D, uint32_t mipLevelCount,
byte* output,
const byte* input,
size_t pixelStride) {
2018 size_t sizeInBytes = 0;
2019 for (uint32_t mipLevel = 0; mipLevel < mipLevelCount; ++mipLevel) {
2021 const size_t mipLevelPixelCount =
static_cast<size_t>(mipLevelSize.
width) *
static_cast<size_t>(mipLevelSize.
height) *
static_cast<size_t>(mipLevelSize.
depth);
2022 sizeInBytes += pixelStride * mipLevelPixelCount;
2024 memcpy(output, input, sizeInBytes);
2047 const size_t offsetX =
static_cast<size_t>(region.
offset.x);
2048 const size_t offsetY =
static_cast<size_t>(region.
offset.y);
2049 const size_t offsetZ =
static_cast<size_t>(region.
offset.z);
2050 const size_t countX =
static_cast<size_t>(region.
size.width);
2051 const size_t countY =
static_cast<size_t>(region.
size.height);
2052 const size_t countZ =
static_cast<size_t>(region.
size.depth);
2053 const size_t strideX = 1;
2054 const size_t strideY =
static_cast<size_t>(
size.width);
2055 const size_t strideZ =
static_cast<size_t>(
size.width) *
static_cast<size_t>(
size.height);
2057 for (
size_t z = 0; z < countZ; ++z) {
2058 const size_t outZ = offsetZ + z;
2059 for (
size_t y = 0; y < countY; ++y) {
2060 const size_t outY = offsetY + y;
2061 for (
size_t x = 0; x < countX; ++x) {
2062 const size_t outX = offsetX + x;
2063 const size_t index = outZ * strideZ + outY * strideY + outX * strideX;
2064 byte*
const pixel = pixels + index * pixelStride;
2092 for (uint32_t mipLevel = 0; mipLevel < mipLevelCount; ++mipLevel) {
2094 const size_t mipLevelPixelCount =
static_cast<size_t>(mipLevelSize.
width) *
static_cast<size_t>(mipLevelSize.
height) *
static_cast<size_t>(mipLevelSize.
depth);
2096 pixels += pixelStride * mipLevelPixelCount;
2121 transformPixels(outputSize, output, outputRegion, pixelStride, [&](
byte* pixel) ->
void { memcpy(pixel, inputPixel, pixelStride); });
2142 static void fillPixels(
Extent3D size3D, uint32_t mipLevelCount,
byte* output,
size_t pixelStride,
const byte* inputPixel) {
2143 transformPixels(size3D, mipLevelCount, output, pixelStride, [&](
byte* pixel) ->
void { memcpy(pixel, inputPixel, pixelStride); });
2170 template <
typename OutputComponent,
size_t OutputComponentCount,
size_t ComponentIndex>
2173 static_assert(OutputComponentCount >= 1 && OutputComponentCount <= 4);
2174 static_assert(ComponentIndex < OutputComponentCount);
2176 transformPixels(outputSize, output, outputRegion,
sizeof(OutputComponent) * OutputComponentCount,
2177 [&](
byte* pixel) ->
void { memcpy(pixel +
sizeof(OutputComponent) * ComponentIndex, &value,
sizeof(OutputComponent)); });
2201 template <
typename OutputComponent,
size_t OutputComponentCount,
size_t ComponentIndex>
2204 static_assert(OutputComponentCount >= 1 && OutputComponentCount <= 4);
2205 static_assert(ComponentIndex < OutputComponentCount);
2207 transformPixels(size3D, mipLevelCount, output,
sizeof(OutputComponent) * OutputComponentCount,
2208 [&](
byte* pixel) ->
void { memcpy(pixel +
sizeof(OutputComponent) * ComponentIndex, &value,
sizeof(OutputComponent)); });
2228 template <
typename PixelComponent, Color::TransferFunction TransferFunction>
2232 transformPixels(size3D, mipLevelCount, pixels,
sizeof(PixelComponent) * 4, [&](
byte* pixel) ->
void {
2234 memcpy(&pixelValue, pixel,
sizeof(pixelValue));
2240 linearPixel =
vec4{pixelValue};
2249 memcpy(pixel, &pixelValue,
sizeof(pixelValue));
2270 template <
typename PixelComponent, Color::TransferFunction TransferFunction>
2274 transformPixels(size3D, mipLevelCount, pixels,
sizeof(PixelComponent) * 4, [&](
byte* pixel) ->
void {
2276 memcpy(&pixelValue, pixel,
sizeof(pixelValue));
2282 linearPixel =
vec4{pixelValue};
2291 memcpy(pixel, &pixelValue,
sizeof(pixelValue));
2314 throw std::length_error{
"Image size overflow."};
2316 const size_t rowStride = pixelStride *
static_cast<size_t>(size2D.
width);
2317 if (rowStride > 0) {
2319 const size_t height =
static_cast<size_t>(size2D.
height);
2320 const size_t halfHeight = height / 2;
2321 for (
size_t y = 0; y < halfHeight; ++y) {
2322 byte*
const rowA = pixels + y * rowStride;
2323 byte*
const rowB = pixels + (height - y - 1) * rowStride;
2324 memcpy(temporaryRow.
data(), rowA, rowStride);
2325 memcpy(rowA, rowB, rowStride);
2326 memcpy(rowB, temporaryRow.
data(), rowStride);
2757 return Extent2D{.width = size3D.width, .height = size3D.height};
2780 return size3D.width;
2789 return size3D.height;
2798 return size3D.depth;
2807 return mipLevelCount;
2851 [[nodiscard]]
byte*
data() noexcept {
2852 return contents.data();
2862 [[nodiscard]]
const byte*
data() const noexcept {
2863 return contents.data();
2875 [[nodiscard]]
size_t size() const noexcept {
2876 return contents.size();
2896 template <
typename Pixel>
2897 [[nodiscard]]
constexpr Pixel
readPixel(
Offset3D position, uint32_t mipLevel = 0)
const;
3076 [[nodiscard]]
Image getPadded(uint32_t paddingLeft, uint32_t paddingRight, uint32_t paddingBottom, uint32_t paddingTop)
const {
3077 return ImageView{*
this}.
getPadded(paddingLeft, paddingRight, paddingBottom, paddingTop);
3105 Extent3D size3D{.width = 0, .height = 0, .depth = 0};
3106 uint32_t mipLevelCount;
3109template <
typename Pixel>
3111 return ImageReference{type, format, size3D, mipLevelCount,
Span{
const_cast<byte*
>(contents.data()), contents.size()}}.
readPixel<Pixel>(position, mipLevel);
3115 return ImageReference{type, format, size3D, mipLevelCount,
Span{
const_cast<byte*
>(contents.data()), contents.size()}}.
getPixel(position, mipLevel);
3119 return ImageReference{type, format, size3D, mipLevelCount,
Span{
const_cast<byte*
>(contents.data()), contents.size()}}.
getLayer(layer, mipLevel);
3123 return ImageReference{type, format, size3D, mipLevelCount,
Span{
const_cast<byte*
>(contents.data()), contents.size()}}.
getMipLevel(mipLevel);
3127 return ImageReference{type, format, size3D, mipLevelCount,
Span{
const_cast<byte*
>(contents.data()), contents.size()}}.
getMipLevels(firstMipLevel, mipLevels);
3131 return ImageReference{type, format, size3D, mipLevelCount,
Span{
const_cast<byte*
>(contents.data()), contents.size()}}.
getMipLevels(firstMipLevel);
3134template <
typename Pixel>
3140 memcpy(&result, pixel.
data(),
sizeof(Pixel));
3150 if (pixelStride == 0) {
3151 throw std::invalid_argument{
"Invalid image format."};
3153 const size_t pixelIndex =
static_cast<size_t>(position.
y) *
static_cast<size_t>(
getWidth()) +
static_cast<size_t>(position.
x);
3160 if (layerStride == 0) {
3161 throw std::invalid_argument{
"Invalid image format."};
3168 if (blockSize.
width == 0 || blockSize.
height == 0) {
3169 throw std::invalid_argument{
"Invalid image format."};
3172 for (uint32_t level = 0; level < mipLevel; ++level) {
3186 if (blockSize.
width == 0 || blockSize.
height == 0) {
3187 throw std::invalid_argument{
"Invalid image format."};
3190 for (uint32_t level = 0; level < firstMipLevel; ++level) {
3196 const uint32_t mipLevelsEnd = firstMipLevel + mipLevels;
3197 for (uint32_t level = firstMipLevel; level < mipLevelsEnd; ++level) {
3200 size += mipLevelStride;
3211 return getPadded(padding, padding, padding, padding);
3215 return ImageView{*
this}.
getPadded(paddingLeft, paddingRight, paddingTop, paddingBottom);
3222template <
typename Pixel>
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition Allocation.hpp:25
constexpr pointer data() noexcept
Definition Allocation.hpp:425
static constexpr vec4 convertStraightToPremultipliedAlpha(vec4 rgba)
Convert a linear color from straight to pre-multiplied alpha.
Definition Color.hpp:312
TransferFunction
Color transfer function.
Definition Color.hpp:65
@ SRGB
sRGB transfer function.
Definition Color.hpp:67
@ LINEAR
Linear transfer function.
Definition Color.hpp:66
static constexpr float convertLinearToSRGB(float x)
Convert a color component in linear space to the sRGB color space.
Definition Color.hpp:227
static constexpr vec4 convertPremultipliedToStraightAlpha(vec4 rgba)
Convert a linear color from pre-multiplied to straight alpha.
Definition Color.hpp:323
static constexpr float convertSRGBToLinear(float x)
Convert a color component in the sRGB color space to linear space.
Definition Color.hpp:269
Base interface for a filesystem.
Definition Filesystem.hpp:25
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
constexpr detail::subspan_type_t< T, EXTENT, Offset, N > subspan() const
Definition Span.hpp:284
Non-owning view over a 2D image.
Definition Image.hpp:451
constexpr Pixel readPixel(Offset3D position, uint32_t mipLevel=0) const
Read a specific pixel of the image.
Definition Image.hpp:3135
constexpr ImageReference getMipLevels(uint32_t firstMipLevel, uint32_t mipLevels) const
Get a view over all layers in a specific range of mip levels of the image.
Definition Image.hpp:3182
constexpr size_t size() const noexcept
Get the size of the image data.
Definition Image.hpp:676
void transcodeTo(byte *output, ImageFormat outputFormat) const
Transcode the image data to a specific format.
Definition Image.hpp:521
constexpr ImageType getType() const noexcept
Get the type of the image referenced by this view.
Definition Image.hpp:558
Image getPadded(uint32_t paddingLeft, uint32_t paddingRight, uint32_t paddingTop, uint32_t paddingBottom) const
Get a padded version of the image.
Definition Image.hpp:3214
constexpr byte * data() const noexcept
Get a pointer to the image data referenced by this view.
Definition Image.hpp:663
constexpr ImageFormat getFormat() const noexcept
Get the format of the image referenced by this view.
Definition Image.hpp:567
constexpr ImageReference getPixel(Offset3D position, uint32_t mipLevel=0) const
Get a view over a specific pixel of the image.
Definition Image.hpp:3144
constexpr ImageReference getLayer(uint32_t layer, uint32_t mipLevel=0) const
Get a view over a specific layer of a specific mip level of the image.
Definition Image.hpp:3157
void transformFromPremultipliedToStraightAlpha(Color::TransferFunction transferFunction)
Transform the RGBA image pixel colors from pre-multiplied to straight alpha.
constexpr uint32_t getWidth() const noexcept
Get the width of the image referenced by this view.
Definition Image.hpp:609
constexpr uint32_t getMipLevelCount() const noexcept
Get the number of mip levels in the image referenced by this view.
Definition Image.hpp:636
void transformFromStraightToPremultipliedAlpha(Color::TransferFunction transferFunction)
Transform the RGBA image pixel colors from straight to pre-multiplied alpha.
constexpr uint32_t getHeight() const noexcept
Get the height of the image referenced by this view.
Definition Image.hpp:618
constexpr Extent2D getSize2D() const noexcept
Get the size, in pixels, of the image layers referenced by this view.
Definition Image.hpp:585
constexpr ImageReference getMipLevel(uint32_t mipLevel) const
Get a view over all layers in a specific mip level of the image.
Definition Image.hpp:3166
constexpr uint32_t getDepth() const noexcept
Get the depth of the image referenced by this view.
Definition Image.hpp:627
constexpr Span< byte > getContents() const noexcept
Get the image data referenced by this view.
Definition Image.hpp:652
constexpr ImageReference() noexcept=default
Construct a view that does not reference an image.
constexpr Extent3D getSize3D() const noexcept
Get the size, in pixels, and the depth, in layers, of the image referenced by this view.
Definition Image.hpp:600
Read-only non-owning view over a 2D image.
Definition Image.hpp:108
constexpr const byte * data() const noexcept
Get a pointer to the image data referenced by this view.
Definition Image.hpp:281
constexpr ImageView getMipLevel(uint32_t mipLevel) const
Get a view over all layers in a specific mip level of the image.
Definition Image.hpp:3122
constexpr ImageView() noexcept=default
Construct a view that does not reference an image.
constexpr Span< const byte > getContents() const noexcept
Get the image data referenced by this view.
Definition Image.hpp:270
constexpr ImageType getType() const noexcept
Get the type of the image referenced by this view.
Definition Image.hpp:176
constexpr uint32_t getDepth() const noexcept
Get the depth of the image referenced by this view.
Definition Image.hpp:245
Image getPadded(uint32_t paddingLeft, uint32_t paddingRight, uint32_t paddingTop, uint32_t paddingBottom) const
Get a padded version of the image.
constexpr ImageView getMipLevels(uint32_t firstMipLevel, uint32_t mipLevels) const
Get a view over all layers in a specific range of mip levels of the image.
Definition Image.hpp:3126
constexpr ImageView getPixel(Offset3D position, uint32_t mipLevel=0) const
Get a view over a specific pixel of the image.
Definition Image.hpp:3114
constexpr Extent3D getSize3D() const noexcept
Get the size, in pixels, and the depth, in layers, of the image referenced by this view.
Definition Image.hpp:218
constexpr ImageFormat getFormat() const noexcept
Get the format of the image referenced by this view.
Definition Image.hpp:185
constexpr uint32_t getMipLevelCount() const noexcept
Get the number of mip levels in the image referenced by this view.
Definition Image.hpp:254
constexpr Pixel readPixel(Offset3D position, uint32_t mipLevel=0) const
Read a specific pixel of the image.
Definition Image.hpp:3110
constexpr uint32_t getHeight() const noexcept
Get the height of the image referenced by this view.
Definition Image.hpp:236
void transcodeTo(byte *output, ImageFormat outputFormat) const
Transcode the image data to a specific format.
constexpr size_t size() const noexcept
Get the size of the image data.
Definition Image.hpp:294
constexpr ImageView getLayer(uint32_t layer, uint32_t mipLevel=0) const
Get a view over a specific layer of a specific mip level of the image.
Definition Image.hpp:3118
constexpr uint32_t getWidth() const noexcept
Get the width of the image referenced by this view.
Definition Image.hpp:227
constexpr Extent2D getSize2D() const noexcept
Get the size, in pixels, of the image layers referenced by this view.
Definition Image.hpp:203
Container for a 2D image.
Definition Image.hpp:951
static constexpr size_t getLogicalChannelCount(ImageFormat format)
Get the number of logical pixel component channels in a specific image format (which doesn't necessar...
Definition Image.hpp:973
ImageView getPixel(Offset3D position, uint32_t mipLevel=0) const
Get a view over a specific pixel of the image.
Definition Image.hpp:3227
static void fillPixels(Extent3D outputSize, byte *output, Region3D outputRegion, size_t pixelStride, const byte *inputPixel)
Set the pixels in a region of a raw image to a specific pixel value.
Definition Image.hpp:2120
static void convertPixel(byte *output, const byte *input)
Convert a single pixel of a raw image to another raw pixel format.
Definition Image.hpp:1739
static Allocation< byte > saveJPEG(const ImageView &image, const ImageSaveJPEGOptions &options={})
Save an 8-bit-per-channel image to an in-memory JPEG file.
Image getPadded(uint32_t paddingLeft, uint32_t paddingRight, uint32_t paddingBottom, uint32_t paddingTop) const
Get a uniformly padded version of the image.
Definition Image.hpp:3076
static ImageFileType determineFileType(Span< const byte > fileContents)
Determine which image file type a file should be parsed as based on the contents at the beginning of ...
static constexpr size_t getMipLevelStride(ImageFormat format, Extent3D size3D)
Get the size in bytes of a single-mip-level image in a specific non-containerized image format.
Definition Image.hpp:1657
Extent2D getSize2D() const noexcept
Get the size, in pixels, of the image layers.
Definition Image.hpp:2756
Image getPadded(uint32_t padding) const
Get a uniformly padded version of the image.
Definition Image.hpp:3097
static constexpr ImageFormat getUncompressedFormat(ImageFormat format)
Get the uncompressed format corresponding to a specific image format.
Definition Image.hpp:1342
uint32_t getHeight() const noexcept
Get the height of the image.
Definition Image.hpp:2788
static constexpr ImageFormat getRawFormat(ImageFormat format)
Get the raw format corresponding to a specific image format.
Definition Image.hpp:1288
constexpr Pixel readPixel(Offset3D position, uint32_t mipLevel=0) const
Read a specific pixel of the image.
Definition Image.hpp:3223
static constexpr size_t getLayerStride(ImageFormat format, Extent2D size2D)
Get the stride in bytes of the layers in a specific non-containerized image format.
Definition Image.hpp:1627
Image(const Filesystem &filesystem, CStringView filepath, const ImageOptions &options={})
Load an image from a file.
ImageType getType() const noexcept
Get the type of the image.
Definition Image.hpp:2729
static constexpr bool isCompressedFormat(ImageFormat format)
Check if a specific image format is a (potentially containerized) compressed format.
Definition Image.hpp:1186
Span< byte > getContents() noexcept
Get the data of this image.
Definition Image.hpp:2823
void transformFromPremultipliedToStraightAlpha(Color::TransferFunction transferFunction)
Transform the RGBA image pixel colors from pre-multiplied to straight alpha.
Definition Image.hpp:2720
uint32_t getWidth() const noexcept
Get the width of the image.
Definition Image.hpp:2779
static Allocation< byte > savePNG(const ImageView &image, const ImageSavePNGOptions &options={})
Save an 8-bit-per-channel image to an in-memory PNG file.
void reset() noexcept
Remove the value from this image and reset it to an empty image.
Definition Image.hpp:2662
static void saveJPEG(const ImageView &image, Filesystem &filesystem, CStringView filepath, const ImageSaveJPEGOptions &options={})
Save an 8-bit-per-channel image to a JPEG file.
Image() noexcept=default
Construct an empty image without a value.
void transformFromStraightToPremultipliedAlpha(Color::TransferFunction transferFunction)
Transform the RGBA image pixel colors from straight to pre-multiplied alpha.
Definition Image.hpp:2704
static constexpr bool isRawFormat(ImageFormat format)
Check if a specific image format is a raw format.
Definition Image.hpp:1077
ImageView getMipLevel(uint32_t mipLevel) const
Get a view over all layers in a specific mip level of the image.
Definition Image.hpp:3243
static void transformPixels(Extent3D size, byte *pixels, Region3D region, size_t pixelStride, auto op)
Transform the pixels in a region of a raw image by a given operation.
Definition Image.hpp:2043
Span< const byte > getContents() const noexcept
Get the data of this image.
Definition Image.hpp:2840
static Allocation< byte > saveKTX2(const ImageView &image, const ImageSaveKTX2Options &options={})
Save an image to an in-memory Khronos Texture, version 2.0 file.
Image(Span< const byte > fileContents, const ImageOptions &options={})
Load an image from an in-memory file.
static void saveKTX2(const ImageView &image, Filesystem &filesystem, CStringView filepath, const ImageSaveKTX2Options &options={})
Save an image to a Khronos TeXture, version 2.0 file.
ImageView getMipLevels(uint32_t firstMipLevel, uint32_t mipLevels) const
Get a view over all layers in a specific range of mip levels of the image.
Definition Image.hpp:3251
static void copyPixels(Extent3D size3D, uint32_t mipLevelCount, byte *output, const byte *input, size_t pixelStride)
Copy all pixels of a full raw image to another image in the same format.
Definition Image.hpp:2012
static constexpr size_t getRowStride(ImageFormat format, uint32_t width)
Get the stride in bytes of the rows in a specific uncompressed image format.
Definition Image.hpp:1604
uint32_t getMipLevelCount() const noexcept
Get the number of mip levels in the image referenced by this view.
Definition Image.hpp:2806
static void fillPixels(Extent3D size3D, uint32_t mipLevelCount, byte *output, size_t pixelStride, const byte *inputPixel)
Set all pixels of a full raw image to a specific pixel value.
Definition Image.hpp:2142
static void setPixelsComponent(Extent3D outputSize, byte *output, Region3D outputRegion, const OutputComponent &value)
Set a component of the pixels in a region of a raw image to a specific value.
Definition Image.hpp:2171
size_t size() const noexcept
Get the size of the image data.
Definition Image.hpp:2875
static void copyPixels(Extent3D outputSize, byte *output, Offset3D outputOffset, Extent3D inputSize, const byte *input, Region3D inputRegion, size_t pixelStride)
Copy the pixels in a region of a raw image to another image region in the same format.
Definition Image.hpp:1940
static constexpr bool isRGBAColorFormat(ImageFormat format)
Check if a specific image format is an RGBA color format with an alpha channel.
Definition Image.hpp:1026
static void savePNG(const ImageView &image, Filesystem &filesystem, CStringView filepath, const ImageSavePNGOptions &options={})
Save an 8-bit-per-channel image to a PNG file.
static void flipVertically(Extent2D size2D, byte *pixels, size_t pixelStride)
Flip a layer of a raw image on the Y axis.
Definition Image.hpp:2309
static void transformPixels(Extent3D size3D, uint32_t mipLevelCount, byte *pixels, size_t pixelStride, auto op)
Transform all pixels of a full raw image by a given operation.
Definition Image.hpp:2087
const byte * data() const noexcept
Get a pointer to the data of this image.
Definition Image.hpp:2862
Image(ImageType type, ImageFormat format, Extent3D size3D, uint32_t mipLevelCount, Allocation< byte > contents)
Construct an image.
static void save(const ImageView &image, Filesystem &filesystem, CStringView filepath, const ImageSaveOptions &options={})
Save an image to a file.
ImageFormat getFormat() const noexcept
Get the format of the image.
Definition Image.hpp:2738
Extent3D getSize3D() const noexcept
Get the size, in pixels, and the depth, in layers, of the image.
Definition Image.hpp:2770
static constexpr size_t getSizeInBytes(ImageFormat format, Extent3D size3D, uint32_t mipLevelCount)
Get the size in bytes of an image in a specific non-containerized image format.
Definition Image.hpp:1681
static void setPixelsComponent(Extent3D size3D, uint32_t mipLevelCount, byte *output, const OutputComponent &value)
Set a component of all pixels of a full raw image to a specific value.
Definition Image.hpp:2202
void transcodeTo(byte *output, ImageFormat outputFormat) const
Transcode the image data to a specific format.
Definition Image.hpp:2688
static constexpr Extent2D getMipLevelSize2D(Extent2D size2D, uint32_t mipLevel)
Get the 2D size, in pixels, of a single layer in a specific mip level of an image.
Definition Image.hpp:1584
static constexpr size_t getPixelStride(ImageFormat format)
Get the stride in bytes of the pixels in a specific uncompressed image format.
Definition Image.hpp:1395
static Allocation< byte > saveHDR(const ImageView &image, const ImageSaveHDROptions &options={})
Save a floating-point 32-bit-per-channel image to an in-memory RGBE/Radiance HDR file.
static constexpr bool isPackedFormat(ImageFormat format)
Check if a specific image format is a bit-packed format.
Definition Image.hpp:1131
Image(const ImageView &image)
Construct an image copied from an image view.
uint32_t getDepth() const noexcept
Get the depth of the image.
Definition Image.hpp:2797
byte * data() noexcept
Get a pointer to the data of this image.
Definition Image.hpp:2851
static constexpr Extent2D getBlockSize2D(ImageFormat format)
Get the raw uncompressed size of the 2D blocks in a specific non-containerized image format,...
Definition Image.hpp:1452
static void convertPixels(Extent3D outputSize, byte *output, Offset3D outputOffset, Extent3D inputSize, const byte *input, Region3D inputRegion)
Convert the pixels in a region of a raw image to another raw pixel format.
Definition Image.hpp:1812
static constexpr bool isContainerizedFormat(ImageFormat format)
Check if a specific image format is a containerized format.
Definition Image.hpp:1237
static constexpr Extent3D getMipLevelSize3D(Extent3D size3D, uint32_t mipLevel)
Get the 3D size, in pixels, of all layers in a specific mip level of an image.
Definition Image.hpp:1564
ImageView getLayer(uint32_t layer, uint32_t mipLevel=0) const
Get a view over a specific layer of a specific mip level of the image.
Definition Image.hpp:3235
static constexpr size_t getBlockStride(ImageFormat format)
Get the stride in bytes of the blocks in a specific non-containerized image format.
Definition Image.hpp:1508
static void transformRGBAPixelsFromPremultipliedToStraightAlpha(Extent3D size3D, uint32_t mipLevelCount, byte *pixels)
Transform all pixels of a full raw RGBA image from pre-multiplied to straight alpha.
Definition Image.hpp:2271
static constexpr uint32_t getMaxMipLevelCount(Extent2D size2D) noexcept
Get the maximum number of mip levels for a specific 2D image size.
Definition Image.hpp:960
static void transformRGBAPixelsFromStraightToPremultipliedAlpha(Extent3D size3D, uint32_t mipLevelCount, byte *pixels)
Transform all pixels of a full raw RGBA image from straight to pre-multiplied alpha.
Definition Image.hpp:2229
static OutputComponent convertPixelComponent(const InputComponent &value) noexcept
Convert a single pixel component of a raw image to another pixel component type.
Definition Image.hpp:1707
static void convertPixels(Extent3D size3D, uint32_t mipLevelCount, byte *output, const byte *input)
Convert all pixels of a full raw image to another raw pixel format.
Definition Image.hpp:1894
static void saveHDR(const ImageView &image, Filesystem &filesystem, CStringView filepath, const ImageSaveHDROptions &options={})
Save a floating-point 32-bit-per-channel image to an RGBE/Radiance HDR file.
Definition concepts.hpp:106
Definition concepts.hpp:35
Definition concepts.hpp:187
ImageFormat
Description of the format of an Image, including the number of component channels,...
Definition Image.hpp:53
@ R32G32_FLOAT
Raw bitmap where each pixel comprises 2 32-bit floating-point components: red, green.
Definition Image.hpp:60
@ A1R5G5B5_UINT_PACK16
Packed bitmap with the following 16-bit unsigned integer RGBA pixel format: ARRRRRGGGGGBBBBB.
Definition Image.hpp:68
@ BC4_R_UINT_BLOCK
S3TC BC4 (RGTC1) block-compressed single-channel image using 64 bits per 4x4 block.
Definition Image.hpp:74
@ B10G11R11_UFLOAT_PACK32
Packed bitmap with the following 32-bit unsigned floating-point RGB pixel format: BBBBBBBBBBGGGGGGGGG...
Definition Image.hpp:69
@ R16_FLOAT
Raw bitmap where each pixel comprises 1 16-bit floating-point component: red.
Definition Image.hpp:56
@ KTX2_ETC1S_RGBA_UINT_BLOCK
KTX2 container of an ETC1-compressed ETC1S-encoded image with 2 DFD channels: RGB and AAA.
Definition Image.hpp:87
@ R32_FLOAT
Raw bitmap where each pixel comprises 1 32-bit floating-point component: red.
Definition Image.hpp:57
@ R8G8B8_UINT
Raw bitmap where each pixel comprises 3 8-bit unsigned integer components: red, green,...
Definition Image.hpp:61
@ R16G16B16_FLOAT
Raw bitmap where each pixel comprises 3 16-bit floating-point components: red, green,...
Definition Image.hpp:62
@ EAC_R11G11_UINT_BLOCK
ETC2 EAC block-compressed double-channel image using 128 bits per 4x4 block.
Definition Image.hpp:82
@ R16G16_FLOAT
Raw bitmap where each pixel comprises 2 16-bit floating-point components: red, green.
Definition Image.hpp:59
@ KTX2_ETC1S_R_UINT_BLOCK
KTX2 container of an ETC1-compressed ETC1S-encoded image with 1 DFD channel: RRR.
Definition Image.hpp:84
@ BC6H_RGB_UFLOAT_BLOCK
S3TC BC6H (BPTC) block-compressed unsigned floating-point RGB image using 128 bits per 4x4 block.
Definition Image.hpp:76
@ ETC2_R8G8B8A8_UINT_BLOCK
ETC2 block-compressed RGBA image using 128 bits per 4x4 block.
Definition Image.hpp:80
@ BC5_RG_UINT_BLOCK
S3TC BC5 (RGTC2) block-compressed double-channel image using 128 bits per 4x4 block.
Definition Image.hpp:75
@ R8G8B8A8_UINT
Raw bitmap where each pixel comprises 4 8-bit unsigned integer components: red, green,...
Definition Image.hpp:64
@ R8G8_UINT
Raw bitmap where each pixel comprises 2 8-bit unsigned integer components: red, green.
Definition Image.hpp:58
@ UNKNOWN
Unknown image format.
Definition Image.hpp:54
@ KTX2_UASTC_R_UINT_BLOCK
KTX2 container of an ASTC 4x4-compressed UASTC LDR-encoded image with 1 DFD channel: RRR.
Definition Image.hpp:88
@ ETC2_R8G8B8_UINT_BLOCK
ETC2 block-compressed RGB image using 64 bits per 4x4 block.
Definition Image.hpp:79
@ BC6H_RGB_FLOAT_BLOCK
S3TC BC6H (BPTC) block-compressed signed floating-point RGB image using 128 bits per 4x4 block.
Definition Image.hpp:77
@ R32G32B32A32_FLOAT
Raw bitmap where each pixel comprises 4 32-bit floating-point components: red, green,...
Definition Image.hpp:66
@ R16G16B16A16_FLOAT
Raw bitmap where each pixel comprises 4 16-bit floating-point components: red, green,...
Definition Image.hpp:65
@ ASTC_4x4_RGBA_UINT_BLOCK
ASTC block-compressed RGBA image using 128 bits per 4x4 block.
Definition Image.hpp:71
@ KTX2_UASTC_RGB_UINT_BLOCK
KTX2 container of an ASTC 4x4-compressed UASTC LDR-encoded image with 1 DFD channel: RGB.
Definition Image.hpp:90
@ R5G6B5_UINT_PACK16
Packed bitmap with the following 16-bit unsigned integer RGB pixel format: RRRRRGGGGGGBBBBB.
Definition Image.hpp:67
@ EAC_R11_UINT_BLOCK
ETC2 EAC block-compressed single-channel image using 64 bits per 4x4 block.
Definition Image.hpp:81
@ BC3_RGBA_UINT_BLOCK
S3TC BC3 (DXT5) block-compressed RGBA image using 128 bits per 4x4 block.
Definition Image.hpp:73
@ R8_UINT
Raw bitmap where each pixel comprises 1 8-bit unsigned integer component: red.
Definition Image.hpp:55
@ BC1_RGB_UINT_BLOCK
S3TC BC1 (DXT1) block-compressed RGB image using 64 bits per 4x4 block.
Definition Image.hpp:72
@ KTX2_ETC1S_RGB_UINT_BLOCK
KTX2 container of an ETC1-compressed ETC1S-encoded image with 1 DFD channel: RGB.
Definition Image.hpp:86
@ A2B10G10R10_UINT_PACK32
Packed bitmap with the following 32-bit unsigned integer RGBA pixel format: AABBBBBBBBBBGGGGGGGGGGRRR...
Definition Image.hpp:70
@ KTX2_UASTC_RG_UINT_BLOCK
KTX2 container of an ASTC 4x4-compressed UASTC LDR-encoded image with 1 DFD channel: RG.
Definition Image.hpp:89
@ KTX2_ETC1S_RG_UINT_BLOCK
KTX2 container of an ETC1-compressed ETC1S-encoded image with 2 DFD channels: RRR and GGG.
Definition Image.hpp:85
@ PVRTC1_4BPP_RGBA_UINT_BLOCK
PVRTC1 block-compressed RGBA image using 64 bits per 4x4 block.
Definition Image.hpp:83
@ R32G32B32_FLOAT
Raw bitmap where each pixel comprises 3 32-bit floating-point components: red, green,...
Definition Image.hpp:63
@ BC7_RGBA_UINT_BLOCK
S3TC BC7 (BPTC) block-compressed RGBA image using 128 bits per 4x4 block.
Definition Image.hpp:78
@ KTX2_UASTC_RGBA_UINT_BLOCK
KTX2 container of an ASTC 4x4-compressed UASTC LDR-encoded image with 1 DFD channel: RGBA.
Definition Image.hpp:91
ImageFileType
File type of an Image.
Definition Image.hpp:30
@ KTX2
Khronos TeXture, version 2.0 (.ktx2).
Definition Image.hpp:35
@ PNG
PNG (.png).
Definition Image.hpp:33
@ UNKNOWN
Unknown file type.
Definition Image.hpp:31
@ HDR
RGBE/Radiance HDR (.hdr).
Definition Image.hpp:34
@ JPEG
JPEG (.jpg/.jpeg).
Definition Image.hpp:32
ImageType
Type of an Image.
Definition Image.hpp:41
@ IMAGE_CUBE
A cube of 6 sides of square 2D images.
Definition Image.hpp:45
@ IMAGE_CUBE_ARRAY
An array of cubes of 6 sides of square 2D images.
Definition Image.hpp:46
@ EMPTY
Empty image without a value.
Definition Image.hpp:42
@ IMAGE_2D_ARRAY
An array of 2D images.
Definition Image.hpp:44
@ IMAGE_2D
A single 2D image.
Definition Image.hpp:43
vec< 4, float > vec4
Definition math.hpp:440
GREM_ALWAYS_INLINE constexpr int getRequiredBitCount(UnsignedInteger value) noexcept
Get the minimum number of bits required to represent a given value.
Definition fundamentals.hpp:750
Region< 3 > Region3D
Rectangular region of 3D data.
Definition extents.hpp:820
Extent< 2 > Extent2D
Size of a 2D data region.
Definition extents.hpp:31
void unreachable()
Definition assertions.hpp:125
GREM_ALWAYS_INLINE constexpr decltype(auto) max(A &&a, B &&b)
Get the greater of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:598
Offset< 3 > Offset3D
Offset in a 3D data region.
Definition extents.hpp:22
Extent< 3 > Extent3D
Size of a 3D data region.
Definition extents.hpp:32
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
uint32_t width
Horizontal extent of the region.
Definition extents.hpp:537
uint32_t height
Vertical extent of the region.
Definition extents.hpp:538
uint32_t height
Vertical extent.
Definition extents.hpp:239
uint32_t width
Horizontal extent.
Definition extents.hpp:238
uint32_t depth
Number of layers.
Definition extents.hpp:240
static constexpr T MAX
Definition fundamentals.hpp:29
int32_t x
Horizontal offset.
Definition extents.hpp:111
int32_t z
Depth offset.
Definition extents.hpp:113
int32_t y
Vertical offset.
Definition extents.hpp:112
Extent< N > size
Size of the region.
Definition extents.hpp:780
Offset< N > offset
Offset of the region.
Definition extents.hpp:779
Options for loading an image.
Definition Image.hpp:911
constexpr bool operator==(const ImageOptions &other) const =default
Compare these options to another set of options for equality.
size_t maxImageSizeInBytes
Maximum size in bytes of the loaded image data.
Definition Image.hpp:934
Extent3D maxImageDimensions
Maximum width, height and depth of the loaded image.
Definition Image.hpp:925
Optional< ImageFormat > requiredFormat
If set, require the loaded image to have or be converted to this format.
Definition Image.hpp:920
Optional< ImageType > requiredType
If set, require the loaded image to have this type.
Definition Image.hpp:915
Options for saving an image in RGBE/Radiance HDR format.
Definition Image.hpp:888
ImageSubresource subresource
Subresource of the image to save.
Definition Image.hpp:892
Options for saving an image in JPEG format.
Definition Image.hpp:867
ImageSubresource subresource
Subresource of the image to save.
Definition Image.hpp:871
int quality
JPEG quality.
Definition Image.hpp:882
Options for saving an image in Khronos TeXture, version 2.0 format.
Definition Image.hpp:901
Options for saving an image in any format.
Definition Image.hpp:906
Options for saving an image in PNG format.
Definition Image.hpp:828
Optional< FilterType > filterTypeOverride
Specific PNG filter type to use instead of choosing automatically for each scanline.
Definition Image.hpp:861
FilterType
Definition Image.hpp:829
@ AVERAGE
Definition Image.hpp:833
@ SUB
Definition Image.hpp:831
@ PAETH
Definition Image.hpp:834
@ NONE
Definition Image.hpp:830
@ UP
Definition Image.hpp:832
size_t compressionLevel
Deflate compression level.
Definition Image.hpp:855
ImageSubresource subresource
Subresource of the image to save.
Definition Image.hpp:840
Subresource of an image.
Definition Image.hpp:97
uint32_t mipLevel
Mip level index in the image.
Definition Image.hpp:99
uint32_t layer
Layer index in the image.
Definition Image.hpp:98