GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Image.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_RESOURCE_IMAGE_HPP
5#define GREM_RESOURCE_IMAGE_HPP
6
8
16#include <GREM/core/extents.hpp>
18#include <GREM/core/math.hpp>
20
21#include <stdexcept> // std::length_error
22
23namespace grem::resource {
24
25class Image; // Forward declaration.
26
30enum class ImageFileType : uint8_t {
36};
37
48
93
98 uint32_t layer = 0;
99 uint32_t mipLevel = 0;
100};
101
109public:
113 constexpr ImageView() noexcept = default;
114
133 constexpr ImageView(ImageType type, ImageFormat format, Extent3D size3D, uint32_t mipLevelCount, Span<const byte> contents)
134 : contents(contents)
135 , type(type)
136 , format(format)
137 , size3D(size3D)
138 , mipLevelCount(mipLevelCount) {
139 GREM_ASSERT(type != ImageType::IMAGE_2D || size3D.depth == 1);
140 GREM_ASSERT((type != ImageType::IMAGE_CUBE && type != ImageType::IMAGE_CUBE_ARRAY) || size3D.width == size3D.height);
141 GREM_ASSERT(type != ImageType::IMAGE_CUBE || size3D.depth == 6);
142 GREM_ASSERT(type != ImageType::IMAGE_CUBE_ARRAY || size3D.depth % 6 == 0);
143 GREM_ASSERT(mipLevelCount <= static_cast<uint32_t>(getRequiredBitCount(max(size3D.width, size3D.height))));
144 }
145
169 GREM_API(resource) void transcodeTo(byte* output, ImageFormat outputFormat) const;
170
176 [[nodiscard]] constexpr ImageType getType() const noexcept {
177 return type;
178 }
179
185 [[nodiscard]] constexpr ImageFormat getFormat() const noexcept {
186 return format;
187 }
188
203 [[nodiscard]] constexpr Extent2D getSize2D() const noexcept {
204 return Extent2D{.width = size3D.width, .height = size3D.height};
205 }
206
218 [[nodiscard]] constexpr Extent3D getSize3D() const noexcept {
219 return size3D;
220 }
221
227 [[nodiscard]] constexpr uint32_t getWidth() const noexcept {
228 return size3D.width;
229 }
230
236 [[nodiscard]] constexpr uint32_t getHeight() const noexcept {
237 return size3D.height;
238 }
239
245 [[nodiscard]] constexpr uint32_t getDepth() const noexcept {
246 return size3D.depth;
247 }
248
254 [[nodiscard]] constexpr uint32_t getMipLevelCount() const noexcept {
255 return mipLevelCount;
256 }
257
270 [[nodiscard]] constexpr Span<const byte> getContents() const noexcept {
271 return contents;
272 }
273
281 [[nodiscard]] constexpr const byte* data() const noexcept {
282 return contents.data();
283 }
284
294 [[nodiscard]] constexpr size_t size() const noexcept {
295 return contents.size();
296 }
297
315 template <typename Pixel>
316 [[nodiscard]] constexpr Pixel readPixel(Offset3D position, uint32_t mipLevel = 0) const;
317
332 [[nodiscard]] constexpr ImageView getPixel(Offset3D position, uint32_t mipLevel = 0) const;
333
348 [[nodiscard]] constexpr ImageView getLayer(uint32_t layer, uint32_t mipLevel = 0) const;
349
362 [[nodiscard]] constexpr ImageView getMipLevel(uint32_t mipLevel) const;
363
379 [[nodiscard]] constexpr ImageView getMipLevels(uint32_t firstMipLevel, uint32_t mipLevels) const;
380
394 [[nodiscard]] constexpr ImageView getMipLevels(uint32_t firstMipLevel) const;
395
416 [[nodiscard]] GREM_API(resource) Image getPadded(uint32_t paddingLeft, uint32_t paddingRight, uint32_t paddingTop, uint32_t paddingBottom) const;
417
435 [[nodiscard]] Image getPadded(uint32_t padding) const;
436
437private:
438 Span<const byte> contents{};
441 Extent3D size3D{.width = 0, .height = 0, .depth = 0};
442 uint32_t mipLevelCount = 0;
443};
444
452public:
456 constexpr ImageReference() noexcept = default;
457
476 constexpr ImageReference(ImageType type, ImageFormat format, Extent3D size3D, uint32_t mipLevelCount, Span<byte> contents)
477 : contents(contents)
478 , type(type)
479 , format(format)
480 , size3D(size3D)
481 , mipLevelCount(mipLevelCount) {
482 GREM_ASSERT(type != ImageType::IMAGE_2D || size3D.depth == 1);
483 GREM_ASSERT((type != ImageType::IMAGE_CUBE && type != ImageType::IMAGE_CUBE_ARRAY) || size3D.width == size3D.height);
484 GREM_ASSERT(type != ImageType::IMAGE_CUBE || size3D.depth == 6);
485 GREM_ASSERT(type != ImageType::IMAGE_CUBE_ARRAY || size3D.depth % 6 == 0);
486 GREM_ASSERT(mipLevelCount <= static_cast<uint32_t>(getRequiredBitCount(max(size3D.width, size3D.height))));
487 }
488
494 constexpr operator ImageView() const noexcept {
496 }
497
521 void transcodeTo(byte* output, ImageFormat outputFormat) const {
522 ImageView{*this}.transcodeTo(output, outputFormat);
523 }
524
538
552
558 [[nodiscard]] constexpr ImageType getType() const noexcept {
559 return type;
560 }
561
567 [[nodiscard]] constexpr ImageFormat getFormat() const noexcept {
568 return format;
569 }
570
585 [[nodiscard]] constexpr Extent2D getSize2D() const noexcept {
586 return Extent2D{.width = size3D.width, .height = size3D.height};
587 }
588
600 [[nodiscard]] constexpr Extent3D getSize3D() const noexcept {
601 return size3D;
602 }
603
609 [[nodiscard]] constexpr uint32_t getWidth() const noexcept {
610 return size3D.width;
611 }
612
618 [[nodiscard]] constexpr uint32_t getHeight() const noexcept {
619 return size3D.height;
620 }
621
627 [[nodiscard]] constexpr uint32_t getDepth() const noexcept {
628 return size3D.depth;
629 }
630
636 [[nodiscard]] constexpr uint32_t getMipLevelCount() const noexcept {
637 return mipLevelCount;
638 }
639
652 [[nodiscard]] constexpr Span<byte> getContents() const noexcept {
653 return contents;
654 }
655
663 [[nodiscard]] constexpr byte* data() const noexcept {
664 return contents.data();
665 }
666
676 [[nodiscard]] constexpr size_t size() const noexcept {
677 return contents.size();
678 }
679
697 template <typename Pixel>
698 [[nodiscard]] constexpr Pixel readPixel(Offset3D position, uint32_t mipLevel = 0) const;
699
714 [[nodiscard]] constexpr ImageReference getPixel(Offset3D position, uint32_t mipLevel = 0) const;
715
730 [[nodiscard]] constexpr ImageReference getLayer(uint32_t layer, uint32_t mipLevel = 0) const;
731
743 [[nodiscard]] constexpr ImageReference getMipLevel(uint32_t mipLevel) const;
744
759 [[nodiscard]] constexpr ImageReference getMipLevels(uint32_t firstMipLevel, uint32_t mipLevels) const;
760
774 [[nodiscard]] constexpr ImageReference getMipLevels(uint32_t firstMipLevel) const;
775
796 [[nodiscard]] Image getPadded(uint32_t paddingLeft, uint32_t paddingRight, uint32_t paddingTop, uint32_t paddingBottom) const;
797
815 [[nodiscard]] Image getPadded(uint32_t padding) const;
816
817private:
818 Span<byte> contents{};
821 Extent3D size3D{.width = 0, .height = 0, .depth = 0};
822 uint32_t mipLevelCount = 0;
823};
824
829 enum class FilterType : uint8_t {
830 NONE = 0,
831 SUB = 1,
832 UP = 2,
834 PAETH = 4,
835 };
836
841 .layer = 0,
842 .mipLevel = 0,
843 };
844
856
862};
863
872 .layer = 0,
873 .mipLevel = 0,
874 };
875
882 int quality = 90;
883};
884
893 .layer = 0,
894 .mipLevel = 0,
895 };
896};
897
902
907
916
921
926 .width = 16384,
927 .height = 16384,
928 .depth = 2048,
929 };
930
934 size_t maxImageSizeInBytes = (uint64_t{8'589'934'592ull} <= uint64_t{Limits<size_t>::MAX}) ? static_cast<size_t>(uint64_t{8'589'934'592ull}) : size_t{2'147'483'648ull};
935
943 [[nodiscard]] constexpr bool operator==(const ImageOptions& other) const = default;
944};
945
951class Image {
952public:
960 [[nodiscard]] static constexpr uint32_t getMaxMipLevelCount(Extent2D size2D) noexcept {
961 return static_cast<uint32_t>(getRequiredBitCount(max(size2D.width, size2D.height)));
962 }
963
973 [[nodiscard]] static constexpr size_t getLogicalChannelCount(ImageFormat format) {
974 switch (format) {
975 case ImageFormat::UNKNOWN: return 0;
976 case ImageFormat::R8_UINT: [[fallthrough]];
977 case ImageFormat::R16_FLOAT: [[fallthrough]];
978 case ImageFormat::R32_FLOAT: [[fallthrough]];
979 case ImageFormat::BC4_R_UINT_BLOCK: [[fallthrough]];
980 case ImageFormat::EAC_R11_UINT_BLOCK: [[fallthrough]];
981 case ImageFormat::KTX2_ETC1S_R_UINT_BLOCK: [[fallthrough]];
983 case ImageFormat::R8G8_UINT: [[fallthrough]];
984 case ImageFormat::R16G16_FLOAT: [[fallthrough]];
985 case ImageFormat::R32G32_FLOAT: [[fallthrough]];
986 case ImageFormat::BC5_RG_UINT_BLOCK: [[fallthrough]];
987 case ImageFormat::EAC_R11G11_UINT_BLOCK: [[fallthrough]];
988 case ImageFormat::KTX2_ETC1S_RG_UINT_BLOCK: [[fallthrough]];
990 case ImageFormat::R8G8B8_UINT: [[fallthrough]];
991 case ImageFormat::R16G16B16_FLOAT: [[fallthrough]];
992 case ImageFormat::R32G32B32_FLOAT: [[fallthrough]];
993 case ImageFormat::R5G6B5_UINT_PACK16: [[fallthrough]];
994 case ImageFormat::B10G11R11_UFLOAT_PACK32: [[fallthrough]];
995 case ImageFormat::BC1_RGB_UINT_BLOCK: [[fallthrough]];
996 case ImageFormat::BC6H_RGB_UFLOAT_BLOCK: [[fallthrough]];
997 case ImageFormat::BC6H_RGB_FLOAT_BLOCK: [[fallthrough]];
998 case ImageFormat::ETC2_R8G8B8_UINT_BLOCK: [[fallthrough]];
999 case ImageFormat::KTX2_ETC1S_RGB_UINT_BLOCK: [[fallthrough]];
1001 case ImageFormat::R8G8B8A8_UINT: [[fallthrough]];
1002 case ImageFormat::R16G16B16A16_FLOAT: [[fallthrough]];
1003 case ImageFormat::R32G32B32A32_FLOAT: [[fallthrough]];
1004 case ImageFormat::A1R5G5B5_UINT_PACK16: [[fallthrough]];
1005 case ImageFormat::A2B10G10R10_UINT_PACK32: [[fallthrough]];
1006 case ImageFormat::ASTC_4x4_RGBA_UINT_BLOCK: [[fallthrough]];
1007 case ImageFormat::BC3_RGBA_UINT_BLOCK: [[fallthrough]];
1008 case ImageFormat::BC7_RGBA_UINT_BLOCK: [[fallthrough]];
1009 case ImageFormat::ETC2_R8G8B8A8_UINT_BLOCK: [[fallthrough]];
1010 case ImageFormat::PVRTC1_4BPP_RGBA_UINT_BLOCK: [[fallthrough]];
1011 case ImageFormat::KTX2_ETC1S_RGBA_UINT_BLOCK: [[fallthrough]];
1013 }
1014 unreachable();
1015 }
1016
1026 [[nodiscard]] static constexpr bool isRGBAColorFormat(ImageFormat format) {
1027 switch (format) {
1028 case ImageFormat::UNKNOWN: [[fallthrough]];
1029 case ImageFormat::R8_UINT: [[fallthrough]];
1030 case ImageFormat::R16_FLOAT: [[fallthrough]];
1031 case ImageFormat::R32_FLOAT: [[fallthrough]];
1032 case ImageFormat::BC4_R_UINT_BLOCK: [[fallthrough]];
1033 case ImageFormat::EAC_R11_UINT_BLOCK: [[fallthrough]];
1034 case ImageFormat::KTX2_ETC1S_R_UINT_BLOCK: [[fallthrough]];
1035 case ImageFormat::KTX2_UASTC_R_UINT_BLOCK: [[fallthrough]];
1036 case ImageFormat::R8G8_UINT: [[fallthrough]];
1037 case ImageFormat::R16G16_FLOAT: [[fallthrough]];
1038 case ImageFormat::R32G32_FLOAT: [[fallthrough]];
1039 case ImageFormat::BC5_RG_UINT_BLOCK: [[fallthrough]];
1040 case ImageFormat::EAC_R11G11_UINT_BLOCK: [[fallthrough]];
1041 case ImageFormat::KTX2_ETC1S_RG_UINT_BLOCK: [[fallthrough]];
1042 case ImageFormat::KTX2_UASTC_RG_UINT_BLOCK: [[fallthrough]];
1043 case ImageFormat::R8G8B8_UINT: [[fallthrough]];
1044 case ImageFormat::R16G16B16_FLOAT: [[fallthrough]];
1045 case ImageFormat::R32G32B32_FLOAT: [[fallthrough]];
1046 case ImageFormat::R5G6B5_UINT_PACK16: [[fallthrough]];
1047 case ImageFormat::B10G11R11_UFLOAT_PACK32: [[fallthrough]];
1048 case ImageFormat::BC1_RGB_UINT_BLOCK: [[fallthrough]];
1049 case ImageFormat::BC6H_RGB_UFLOAT_BLOCK: [[fallthrough]];
1050 case ImageFormat::BC6H_RGB_FLOAT_BLOCK: [[fallthrough]];
1051 case ImageFormat::ETC2_R8G8B8_UINT_BLOCK: [[fallthrough]];
1052 case ImageFormat::KTX2_ETC1S_RGB_UINT_BLOCK: [[fallthrough]];
1053 case ImageFormat::KTX2_UASTC_RGB_UINT_BLOCK: return false;
1054 case ImageFormat::R8G8B8A8_UINT: [[fallthrough]];
1055 case ImageFormat::R16G16B16A16_FLOAT: [[fallthrough]];
1056 case ImageFormat::R32G32B32A32_FLOAT: [[fallthrough]];
1057 case ImageFormat::A1R5G5B5_UINT_PACK16: [[fallthrough]];
1058 case ImageFormat::A2B10G10R10_UINT_PACK32: [[fallthrough]];
1059 case ImageFormat::ASTC_4x4_RGBA_UINT_BLOCK: [[fallthrough]];
1060 case ImageFormat::BC3_RGBA_UINT_BLOCK: [[fallthrough]];
1061 case ImageFormat::BC7_RGBA_UINT_BLOCK: [[fallthrough]];
1062 case ImageFormat::ETC2_R8G8B8A8_UINT_BLOCK: [[fallthrough]];
1063 case ImageFormat::PVRTC1_4BPP_RGBA_UINT_BLOCK: [[fallthrough]];
1064 case ImageFormat::KTX2_ETC1S_RGBA_UINT_BLOCK: [[fallthrough]];
1066 }
1067 unreachable();
1068 }
1069
1077 [[nodiscard]] static constexpr bool isRawFormat(ImageFormat format) {
1078 switch (format) {
1079 case ImageFormat::UNKNOWN: [[fallthrough]];
1080 case ImageFormat::R5G6B5_UINT_PACK16: [[fallthrough]];
1081 case ImageFormat::A1R5G5B5_UINT_PACK16: [[fallthrough]];
1082 case ImageFormat::B10G11R11_UFLOAT_PACK32: [[fallthrough]];
1083 case ImageFormat::A2B10G10R10_UINT_PACK32: [[fallthrough]];
1084 case ImageFormat::ASTC_4x4_RGBA_UINT_BLOCK: [[fallthrough]];
1085 case ImageFormat::BC1_RGB_UINT_BLOCK: [[fallthrough]];
1086 case ImageFormat::BC3_RGBA_UINT_BLOCK: [[fallthrough]];
1087 case ImageFormat::BC4_R_UINT_BLOCK: [[fallthrough]];
1088 case ImageFormat::BC5_RG_UINT_BLOCK: [[fallthrough]];
1089 case ImageFormat::BC6H_RGB_UFLOAT_BLOCK: [[fallthrough]];
1090 case ImageFormat::BC6H_RGB_FLOAT_BLOCK: [[fallthrough]];
1091 case ImageFormat::BC7_RGBA_UINT_BLOCK: [[fallthrough]];
1092 case ImageFormat::ETC2_R8G8B8_UINT_BLOCK: [[fallthrough]];
1093 case ImageFormat::ETC2_R8G8B8A8_UINT_BLOCK: [[fallthrough]];
1094 case ImageFormat::EAC_R11_UINT_BLOCK: [[fallthrough]];
1095 case ImageFormat::EAC_R11G11_UINT_BLOCK: [[fallthrough]];
1096 case ImageFormat::PVRTC1_4BPP_RGBA_UINT_BLOCK: [[fallthrough]];
1097 case ImageFormat::KTX2_ETC1S_R_UINT_BLOCK: [[fallthrough]];
1098 case ImageFormat::KTX2_ETC1S_RG_UINT_BLOCK: [[fallthrough]];
1099 case ImageFormat::KTX2_ETC1S_RGB_UINT_BLOCK: [[fallthrough]];
1100 case ImageFormat::KTX2_ETC1S_RGBA_UINT_BLOCK: [[fallthrough]];
1101 case ImageFormat::KTX2_UASTC_R_UINT_BLOCK: [[fallthrough]];
1102 case ImageFormat::KTX2_UASTC_RG_UINT_BLOCK: [[fallthrough]];
1103 case ImageFormat::KTX2_UASTC_RGB_UINT_BLOCK: [[fallthrough]];
1104 case ImageFormat::KTX2_UASTC_RGBA_UINT_BLOCK: return false;
1105 case ImageFormat::R8_UINT: [[fallthrough]];
1106 case ImageFormat::R16_FLOAT: [[fallthrough]];
1107 case ImageFormat::R32_FLOAT: [[fallthrough]];
1108 case ImageFormat::R8G8_UINT: [[fallthrough]];
1109 case ImageFormat::R16G16_FLOAT: [[fallthrough]];
1110 case ImageFormat::R32G32_FLOAT: [[fallthrough]];
1111 case ImageFormat::R8G8B8_UINT: [[fallthrough]];
1112 case ImageFormat::R16G16B16_FLOAT: [[fallthrough]];
1113 case ImageFormat::R32G32B32_FLOAT: [[fallthrough]];
1114 case ImageFormat::R8G8B8A8_UINT: [[fallthrough]];
1115 case ImageFormat::R16G16B16A16_FLOAT: [[fallthrough]];
1116 case ImageFormat::R32G32B32A32_FLOAT: return true;
1117 }
1118 unreachable();
1119 }
1120
1131 [[nodiscard]] static constexpr bool isPackedFormat(ImageFormat format) {
1132 switch (format) {
1133 case ImageFormat::UNKNOWN: [[fallthrough]];
1134 case ImageFormat::R8_UINT: [[fallthrough]];
1135 case ImageFormat::R16_FLOAT: [[fallthrough]];
1136 case ImageFormat::R32_FLOAT: [[fallthrough]];
1137 case ImageFormat::R8G8_UINT: [[fallthrough]];
1138 case ImageFormat::R16G16_FLOAT: [[fallthrough]];
1139 case ImageFormat::R32G32_FLOAT: [[fallthrough]];
1140 case ImageFormat::R8G8B8_UINT: [[fallthrough]];
1141 case ImageFormat::R16G16B16_FLOAT: [[fallthrough]];
1142 case ImageFormat::R32G32B32_FLOAT: [[fallthrough]];
1143 case ImageFormat::R8G8B8A8_UINT: [[fallthrough]];
1144 case ImageFormat::R16G16B16A16_FLOAT: [[fallthrough]];
1145 case ImageFormat::R32G32B32A32_FLOAT: [[fallthrough]];
1146 case ImageFormat::ASTC_4x4_RGBA_UINT_BLOCK: [[fallthrough]];
1147 case ImageFormat::BC1_RGB_UINT_BLOCK: [[fallthrough]];
1148 case ImageFormat::BC3_RGBA_UINT_BLOCK: [[fallthrough]];
1149 case ImageFormat::BC4_R_UINT_BLOCK: [[fallthrough]];
1150 case ImageFormat::BC5_RG_UINT_BLOCK: [[fallthrough]];
1151 case ImageFormat::BC6H_RGB_UFLOAT_BLOCK: [[fallthrough]];
1152 case ImageFormat::BC6H_RGB_FLOAT_BLOCK: [[fallthrough]];
1153 case ImageFormat::BC7_RGBA_UINT_BLOCK: [[fallthrough]];
1154 case ImageFormat::ETC2_R8G8B8_UINT_BLOCK: [[fallthrough]];
1155 case ImageFormat::ETC2_R8G8B8A8_UINT_BLOCK: [[fallthrough]];
1156 case ImageFormat::EAC_R11_UINT_BLOCK: [[fallthrough]];
1157 case ImageFormat::EAC_R11G11_UINT_BLOCK: [[fallthrough]];
1158 case ImageFormat::PVRTC1_4BPP_RGBA_UINT_BLOCK: [[fallthrough]];
1159 case ImageFormat::KTX2_ETC1S_R_UINT_BLOCK: [[fallthrough]];
1160 case ImageFormat::KTX2_ETC1S_RG_UINT_BLOCK: [[fallthrough]];
1161 case ImageFormat::KTX2_ETC1S_RGB_UINT_BLOCK: [[fallthrough]];
1162 case ImageFormat::KTX2_ETC1S_RGBA_UINT_BLOCK: [[fallthrough]];
1163 case ImageFormat::KTX2_UASTC_R_UINT_BLOCK: [[fallthrough]];
1164 case ImageFormat::KTX2_UASTC_RG_UINT_BLOCK: [[fallthrough]];
1165 case ImageFormat::KTX2_UASTC_RGB_UINT_BLOCK: [[fallthrough]];
1166 case ImageFormat::KTX2_UASTC_RGBA_UINT_BLOCK: return false;
1167 case ImageFormat::R5G6B5_UINT_PACK16: [[fallthrough]];
1168 case ImageFormat::A1R5G5B5_UINT_PACK16: [[fallthrough]];
1169 case ImageFormat::B10G11R11_UFLOAT_PACK32: [[fallthrough]];
1170 case ImageFormat::A2B10G10R10_UINT_PACK32: return true;
1171 }
1172 unreachable();
1173 }
1174
1186 [[nodiscard]] static constexpr bool isCompressedFormat(ImageFormat format) {
1187 switch (format) {
1188 case ImageFormat::UNKNOWN: [[fallthrough]];
1189 case ImageFormat::R8_UINT: [[fallthrough]];
1190 case ImageFormat::R16_FLOAT: [[fallthrough]];
1191 case ImageFormat::R32_FLOAT: [[fallthrough]];
1192 case ImageFormat::R8G8_UINT: [[fallthrough]];
1193 case ImageFormat::R16G16_FLOAT: [[fallthrough]];
1194 case ImageFormat::R32G32_FLOAT: [[fallthrough]];
1195 case ImageFormat::R8G8B8_UINT: [[fallthrough]];
1196 case ImageFormat::R16G16B16_FLOAT: [[fallthrough]];
1197 case ImageFormat::R32G32B32_FLOAT: [[fallthrough]];
1198 case ImageFormat::R8G8B8A8_UINT: [[fallthrough]];
1199 case ImageFormat::R16G16B16A16_FLOAT: [[fallthrough]];
1200 case ImageFormat::R32G32B32A32_FLOAT: [[fallthrough]];
1201 case ImageFormat::R5G6B5_UINT_PACK16: [[fallthrough]];
1202 case ImageFormat::A1R5G5B5_UINT_PACK16: [[fallthrough]];
1203 case ImageFormat::B10G11R11_UFLOAT_PACK32: [[fallthrough]];
1204 case ImageFormat::A2B10G10R10_UINT_PACK32: return false;
1205 case ImageFormat::ASTC_4x4_RGBA_UINT_BLOCK: [[fallthrough]];
1206 case ImageFormat::BC1_RGB_UINT_BLOCK: [[fallthrough]];
1207 case ImageFormat::BC3_RGBA_UINT_BLOCK: [[fallthrough]];
1208 case ImageFormat::BC4_R_UINT_BLOCK: [[fallthrough]];
1209 case ImageFormat::BC5_RG_UINT_BLOCK: [[fallthrough]];
1210 case ImageFormat::BC6H_RGB_UFLOAT_BLOCK: [[fallthrough]];
1211 case ImageFormat::BC6H_RGB_FLOAT_BLOCK: [[fallthrough]];
1212 case ImageFormat::BC7_RGBA_UINT_BLOCK: [[fallthrough]];
1213 case ImageFormat::ETC2_R8G8B8_UINT_BLOCK: [[fallthrough]];
1214 case ImageFormat::ETC2_R8G8B8A8_UINT_BLOCK: [[fallthrough]];
1215 case ImageFormat::EAC_R11_UINT_BLOCK: [[fallthrough]];
1216 case ImageFormat::EAC_R11G11_UINT_BLOCK: [[fallthrough]];
1217 case ImageFormat::PVRTC1_4BPP_RGBA_UINT_BLOCK: [[fallthrough]];
1218 case ImageFormat::KTX2_ETC1S_R_UINT_BLOCK: [[fallthrough]];
1219 case ImageFormat::KTX2_ETC1S_RG_UINT_BLOCK: [[fallthrough]];
1220 case ImageFormat::KTX2_ETC1S_RGB_UINT_BLOCK: [[fallthrough]];
1221 case ImageFormat::KTX2_ETC1S_RGBA_UINT_BLOCK: [[fallthrough]];
1222 case ImageFormat::KTX2_UASTC_R_UINT_BLOCK: [[fallthrough]];
1223 case ImageFormat::KTX2_UASTC_RG_UINT_BLOCK: [[fallthrough]];
1224 case ImageFormat::KTX2_UASTC_RGB_UINT_BLOCK: [[fallthrough]];
1226 }
1227 unreachable();
1228 }
1229
1237 [[nodiscard]] static constexpr bool isContainerizedFormat(ImageFormat format) {
1238 switch (format) {
1239 case ImageFormat::UNKNOWN: [[fallthrough]];
1240 case ImageFormat::R8_UINT: [[fallthrough]];
1241 case ImageFormat::R16_FLOAT: [[fallthrough]];
1242 case ImageFormat::R32_FLOAT: [[fallthrough]];
1243 case ImageFormat::R8G8_UINT: [[fallthrough]];
1244 case ImageFormat::R16G16_FLOAT: [[fallthrough]];
1245 case ImageFormat::R32G32_FLOAT: [[fallthrough]];
1246 case ImageFormat::R8G8B8_UINT: [[fallthrough]];
1247 case ImageFormat::R16G16B16_FLOAT: [[fallthrough]];
1248 case ImageFormat::R32G32B32_FLOAT: [[fallthrough]];
1249 case ImageFormat::R8G8B8A8_UINT: [[fallthrough]];
1250 case ImageFormat::R16G16B16A16_FLOAT: [[fallthrough]];
1251 case ImageFormat::R32G32B32A32_FLOAT: [[fallthrough]];
1252 case ImageFormat::R5G6B5_UINT_PACK16: [[fallthrough]];
1253 case ImageFormat::A1R5G5B5_UINT_PACK16: [[fallthrough]];
1254 case ImageFormat::B10G11R11_UFLOAT_PACK32: [[fallthrough]];
1255 case ImageFormat::A2B10G10R10_UINT_PACK32: [[fallthrough]];
1256 case ImageFormat::ASTC_4x4_RGBA_UINT_BLOCK: [[fallthrough]];
1257 case ImageFormat::BC1_RGB_UINT_BLOCK: [[fallthrough]];
1258 case ImageFormat::BC3_RGBA_UINT_BLOCK: [[fallthrough]];
1259 case ImageFormat::BC4_R_UINT_BLOCK: [[fallthrough]];
1260 case ImageFormat::BC5_RG_UINT_BLOCK: [[fallthrough]];
1261 case ImageFormat::BC6H_RGB_UFLOAT_BLOCK: [[fallthrough]];
1262 case ImageFormat::BC6H_RGB_FLOAT_BLOCK: [[fallthrough]];
1263 case ImageFormat::BC7_RGBA_UINT_BLOCK: [[fallthrough]];
1264 case ImageFormat::ETC2_R8G8B8_UINT_BLOCK: [[fallthrough]];
1265 case ImageFormat::ETC2_R8G8B8A8_UINT_BLOCK: [[fallthrough]];
1266 case ImageFormat::EAC_R11_UINT_BLOCK: [[fallthrough]];
1267 case ImageFormat::EAC_R11G11_UINT_BLOCK: [[fallthrough]];
1269 case ImageFormat::KTX2_ETC1S_R_UINT_BLOCK: [[fallthrough]];
1270 case ImageFormat::KTX2_ETC1S_RG_UINT_BLOCK: [[fallthrough]];
1271 case ImageFormat::KTX2_ETC1S_RGB_UINT_BLOCK: [[fallthrough]];
1272 case ImageFormat::KTX2_ETC1S_RGBA_UINT_BLOCK: [[fallthrough]];
1273 case ImageFormat::KTX2_UASTC_R_UINT_BLOCK: [[fallthrough]];
1274 case ImageFormat::KTX2_UASTC_RG_UINT_BLOCK: [[fallthrough]];
1275 case ImageFormat::KTX2_UASTC_RGB_UINT_BLOCK: [[fallthrough]];
1277 }
1278 unreachable();
1279 }
1280
1288 [[nodiscard]] static constexpr ImageFormat getRawFormat(ImageFormat format) {
1289 switch (format) {
1328 }
1329 unreachable();
1330 }
1331
1342 [[nodiscard]] static constexpr ImageFormat getUncompressedFormat(ImageFormat format) {
1343 switch (format) {
1382 }
1383 unreachable();
1384 }
1385
1395 [[nodiscard]] static constexpr size_t getPixelStride(ImageFormat format) {
1396 switch (format) {
1397 case ImageFormat::UNKNOWN: return 0;
1398 case ImageFormat::R8_UINT: return 1;
1399 case ImageFormat::R16_FLOAT: return 2;
1400 case ImageFormat::R32_FLOAT: return 4;
1401 case ImageFormat::R8G8_UINT: return 2;
1402 case ImageFormat::R16G16_FLOAT: return 4;
1403 case ImageFormat::R32G32_FLOAT: return 8;
1404 case ImageFormat::R8G8B8_UINT: return 3;
1405 case ImageFormat::R16G16B16_FLOAT: return 6;
1406 case ImageFormat::R32G32B32_FLOAT: return 12;
1407 case ImageFormat::R8G8B8A8_UINT: return 4;
1408 case ImageFormat::R16G16B16A16_FLOAT: return 8;
1409 case ImageFormat::R32G32B32A32_FLOAT: return 16;
1410 case ImageFormat::R5G6B5_UINT_PACK16: return 2;
1411 case ImageFormat::A1R5G5B5_UINT_PACK16: return 2;
1414 case ImageFormat::ASTC_4x4_RGBA_UINT_BLOCK: [[fallthrough]];
1415 case ImageFormat::BC1_RGB_UINT_BLOCK: [[fallthrough]];
1416 case ImageFormat::BC3_RGBA_UINT_BLOCK: [[fallthrough]];
1417 case ImageFormat::BC4_R_UINT_BLOCK: [[fallthrough]];
1418 case ImageFormat::BC5_RG_UINT_BLOCK: [[fallthrough]];
1419 case ImageFormat::BC6H_RGB_UFLOAT_BLOCK: [[fallthrough]];
1420 case ImageFormat::BC6H_RGB_FLOAT_BLOCK: [[fallthrough]];
1421 case ImageFormat::BC7_RGBA_UINT_BLOCK: [[fallthrough]];
1422 case ImageFormat::ETC2_R8G8B8_UINT_BLOCK: [[fallthrough]];
1423 case ImageFormat::ETC2_R8G8B8A8_UINT_BLOCK: [[fallthrough]];
1424 case ImageFormat::EAC_R11_UINT_BLOCK: [[fallthrough]];
1425 case ImageFormat::EAC_R11G11_UINT_BLOCK: [[fallthrough]];
1426 case ImageFormat::PVRTC1_4BPP_RGBA_UINT_BLOCK: [[fallthrough]];
1427 case ImageFormat::KTX2_ETC1S_R_UINT_BLOCK: [[fallthrough]];
1428 case ImageFormat::KTX2_ETC1S_RG_UINT_BLOCK: [[fallthrough]];
1429 case ImageFormat::KTX2_ETC1S_RGB_UINT_BLOCK: [[fallthrough]];
1430 case ImageFormat::KTX2_ETC1S_RGBA_UINT_BLOCK: [[fallthrough]];
1431 case ImageFormat::KTX2_UASTC_R_UINT_BLOCK: [[fallthrough]];
1432 case ImageFormat::KTX2_UASTC_RG_UINT_BLOCK: [[fallthrough]];
1433 case ImageFormat::KTX2_UASTC_RGB_UINT_BLOCK: [[fallthrough]];
1435 }
1436 unreachable();
1437 }
1438
1452 [[nodiscard]] static constexpr Extent2D getBlockSize2D(ImageFormat format) {
1453 switch (format) {
1454 case ImageFormat::UNKNOWN: return Extent2D{};
1455 case ImageFormat::R8_UINT: [[fallthrough]];
1456 case ImageFormat::R16_FLOAT: [[fallthrough]];
1457 case ImageFormat::R32_FLOAT: [[fallthrough]];
1458 case ImageFormat::R8G8_UINT: [[fallthrough]];
1459 case ImageFormat::R16G16_FLOAT: [[fallthrough]];
1460 case ImageFormat::R32G32_FLOAT: [[fallthrough]];
1461 case ImageFormat::R8G8B8_UINT: [[fallthrough]];
1462 case ImageFormat::R16G16B16_FLOAT: [[fallthrough]];
1463 case ImageFormat::R32G32B32_FLOAT: [[fallthrough]];
1464 case ImageFormat::R8G8B8A8_UINT: [[fallthrough]];
1465 case ImageFormat::R16G16B16A16_FLOAT: [[fallthrough]];
1466 case ImageFormat::R32G32B32A32_FLOAT: [[fallthrough]];
1467 case ImageFormat::R5G6B5_UINT_PACK16: [[fallthrough]];
1468 case ImageFormat::A1R5G5B5_UINT_PACK16: [[fallthrough]];
1469 case ImageFormat::B10G11R11_UFLOAT_PACK32: [[fallthrough]];
1471 case ImageFormat::ASTC_4x4_RGBA_UINT_BLOCK: [[fallthrough]];
1472 case ImageFormat::BC1_RGB_UINT_BLOCK: [[fallthrough]];
1473 case ImageFormat::BC3_RGBA_UINT_BLOCK: [[fallthrough]];
1474 case ImageFormat::BC4_R_UINT_BLOCK: [[fallthrough]];
1475 case ImageFormat::BC5_RG_UINT_BLOCK: [[fallthrough]];
1476 case ImageFormat::BC6H_RGB_UFLOAT_BLOCK: [[fallthrough]];
1477 case ImageFormat::BC6H_RGB_FLOAT_BLOCK: [[fallthrough]];
1478 case ImageFormat::BC7_RGBA_UINT_BLOCK: [[fallthrough]];
1479 case ImageFormat::ETC2_R8G8B8_UINT_BLOCK: [[fallthrough]];
1480 case ImageFormat::ETC2_R8G8B8A8_UINT_BLOCK: [[fallthrough]];
1481 case ImageFormat::EAC_R11_UINT_BLOCK: [[fallthrough]];
1482 case ImageFormat::EAC_R11G11_UINT_BLOCK: [[fallthrough]];
1484 case ImageFormat::KTX2_ETC1S_R_UINT_BLOCK: [[fallthrough]];
1485 case ImageFormat::KTX2_ETC1S_RG_UINT_BLOCK: [[fallthrough]];
1486 case ImageFormat::KTX2_ETC1S_RGB_UINT_BLOCK: [[fallthrough]];
1487 case ImageFormat::KTX2_ETC1S_RGBA_UINT_BLOCK: [[fallthrough]];
1488 case ImageFormat::KTX2_UASTC_R_UINT_BLOCK: [[fallthrough]];
1489 case ImageFormat::KTX2_UASTC_RG_UINT_BLOCK: [[fallthrough]];
1490 case ImageFormat::KTX2_UASTC_RGB_UINT_BLOCK: [[fallthrough]];
1492 }
1493 unreachable();
1494 }
1495
1508 [[nodiscard]] static constexpr size_t getBlockStride(ImageFormat format) {
1509 switch (format) {
1510 case ImageFormat::UNKNOWN: return 0;
1511 case ImageFormat::R8_UINT: return 1;
1512 case ImageFormat::R16_FLOAT: return 2;
1513 case ImageFormat::R32_FLOAT: return 4;
1514 case ImageFormat::R8G8_UINT: return 2;
1515 case ImageFormat::R16G16_FLOAT: return 4;
1516 case ImageFormat::R32G32_FLOAT: return 8;
1517 case ImageFormat::R8G8B8_UINT: return 3;
1518 case ImageFormat::R16G16B16_FLOAT: return 6;
1519 case ImageFormat::R32G32B32_FLOAT: return 12;
1520 case ImageFormat::R8G8B8A8_UINT: return 4;
1521 case ImageFormat::R16G16B16A16_FLOAT: return 8;
1522 case ImageFormat::R32G32B32A32_FLOAT: return 16;
1523 case ImageFormat::R5G6B5_UINT_PACK16: return 2;
1524 case ImageFormat::A1R5G5B5_UINT_PACK16: return 2;
1528 case ImageFormat::BC1_RGB_UINT_BLOCK: return 8;
1529 case ImageFormat::BC3_RGBA_UINT_BLOCK: return 16;
1530 case ImageFormat::BC4_R_UINT_BLOCK: return 8;
1531 case ImageFormat::BC5_RG_UINT_BLOCK: return 16;
1532 case ImageFormat::BC6H_RGB_UFLOAT_BLOCK: return 16;
1533 case ImageFormat::BC6H_RGB_FLOAT_BLOCK: return 16;
1534 case ImageFormat::BC7_RGBA_UINT_BLOCK: return 16;
1537 case ImageFormat::EAC_R11_UINT_BLOCK: return 8;
1538 case ImageFormat::EAC_R11G11_UINT_BLOCK: return 16;
1540 case ImageFormat::KTX2_ETC1S_R_UINT_BLOCK: [[fallthrough]];
1541 case ImageFormat::KTX2_ETC1S_RG_UINT_BLOCK: [[fallthrough]];
1542 case ImageFormat::KTX2_ETC1S_RGB_UINT_BLOCK: [[fallthrough]];
1543 case ImageFormat::KTX2_ETC1S_RGBA_UINT_BLOCK: [[fallthrough]];
1544 case ImageFormat::KTX2_UASTC_R_UINT_BLOCK: [[fallthrough]];
1545 case ImageFormat::KTX2_UASTC_RG_UINT_BLOCK: [[fallthrough]];
1546 case ImageFormat::KTX2_UASTC_RGB_UINT_BLOCK: [[fallthrough]];
1548 }
1549 unreachable();
1550 }
1551
1564 [[nodiscard]] static constexpr Extent3D getMipLevelSize3D(Extent3D size3D, uint32_t mipLevel) {
1565 GREM_ASSERT(mipLevel < getMaxMipLevelCount(Extent2D{size3D.width, size3D.height}));
1566 return Extent3D{
1567 max(size3D.width >> mipLevel, uint32_t{1}),
1568 max(size3D.height >> mipLevel, uint32_t{1}),
1569 size3D.depth,
1570 };
1571 }
1572
1584 [[nodiscard]] static constexpr Extent2D getMipLevelSize2D(Extent2D size2D, uint32_t mipLevel) {
1585 GREM_ASSERT(mipLevel < getMaxMipLevelCount(size2D));
1586 return Extent2D{
1587 max(size2D.width >> mipLevel, uint32_t{1}),
1588 max(size2D.height >> mipLevel, uint32_t{1}),
1589 };
1590 }
1591
1604 [[nodiscard]] static constexpr size_t getRowStride(ImageFormat format, uint32_t width) {
1605 if (width == 0) {
1606 return 0;
1607 }
1608 const size_t pixelStride = getPixelStride(format);
1609 if (pixelStride > Limits<size_t>::MAX / static_cast<size_t>(width)) {
1610 throw std::length_error{"Image size overflow."};
1611 }
1612 return pixelStride * static_cast<size_t>(width);
1613 }
1614
1627 [[nodiscard]] static constexpr size_t getLayerStride(ImageFormat format, Extent2D size2D) {
1628 const Extent2D blockSize = getBlockSize2D(format);
1629 if (blockSize.width == 0 || blockSize.height == 0) {
1630 return 0;
1631 }
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);
1634 const size_t blockStride = getBlockStride(format);
1635 if (blockStride > Limits<size_t>::MAX / blockCountX) {
1636 throw std::length_error{"Image size overflow."};
1637 }
1638 const size_t rowStride = blockStride * blockCountX;
1639 if (rowStride > Limits<size_t>::MAX / blockCountY) {
1640 throw std::length_error{"Image size overflow."};
1641 }
1642 return rowStride * blockCountY;
1643 }
1644
1657 [[nodiscard]] static constexpr size_t getMipLevelStride(ImageFormat format, Extent3D size3D) {
1658 if (size3D.depth == 0) {
1659 return 0;
1660 }
1661 const size_t layerStride = getLayerStride(format, Extent2D{.width = size3D.width, .height = size3D.height});
1662 if (Limits<size_t>::MAX / static_cast<size_t>(size3D.depth) < layerStride) {
1663 throw std::length_error{"Image size overflow."};
1664 }
1665 return layerStride * static_cast<size_t>(size3D.depth);
1666 }
1667
1681 [[nodiscard]] static constexpr size_t getSizeInBytes(ImageFormat format, Extent3D size3D, uint32_t mipLevelCount) {
1682 size_t result = 0;
1683 for (uint32_t mipLevel = 0; mipLevel < mipLevelCount; ++mipLevel) {
1684 const size_t mipLevelStride = getMipLevelStride(format, getMipLevelSize3D(size3D, mipLevel));
1685 if (result > Limits<size_t>::MAX - mipLevelStride) {
1686 throw std::length_error{"Image size overflow."};
1687 }
1688 result += mipLevelStride;
1689 }
1690 return result;
1691 }
1692
1706 template <typename OutputComponent, typename InputComponent>
1707 [[nodiscard]] static OutputComponent convertPixelComponent(const InputComponent& value) noexcept {
1709 return value;
1710 } else {
1711 return static_cast<OutputComponent>(static_cast<float>(value));
1712 }
1713 }
1714
1738 template <typename OutputComponent, size_t OutputComponentCount, typename InputComponent, size_t InputComponentCount>
1739 static void convertPixel(byte* output, const byte* input) {
1740 static_assert(OutputComponentCount >= 1 && OutputComponentCount <= 4);
1741 static_assert(InputComponentCount >= 1 && InputComponentCount <= 4);
1742
1743 GREM_ASSERT(output);
1744 GREM_ASSERT(input);
1745
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);
1751 const OutputComponent convertedValue = convertPixelComponent<OutputComponent>(value);
1752 memcpy(output, &convertedValue, sizeof(OutputComponent));
1753 output += sizeof(OutputComponent);
1754 }
1755 if constexpr (OutputComponentCount >= 2 && InputComponentCount < 2) {
1756 memset(output, 0, sizeof(OutputComponent));
1757 output += sizeof(OutputComponent);
1758 }
1759 if constexpr (OutputComponentCount >= 3 && InputComponentCount < 3) {
1760 memset(output, 0, sizeof(OutputComponent));
1761 output += sizeof(OutputComponent);
1762 }
1763 if constexpr (OutputComponentCount >= 4 && InputComponentCount < 4) {
1765 constexpr OutputComponent ONE = 1.0f;
1766 memcpy(output, &ONE, sizeof(OutputComponent));
1767 } else {
1768 constexpr OutputComponent MAX = Limits<OutputComponent>::MAX;
1769 memcpy(output, &MAX, sizeof(OutputComponent));
1770 }
1771 output += sizeof(OutputComponent);
1772 }
1773 }
1774
1811 template <typename OutputComponent, size_t OutputComponentCount, typename InputComponent, size_t InputComponentCount>
1812 static void convertPixels(Extent3D outputSize, byte* output, Offset3D outputOffset, Extent3D inputSize, const byte* input, Region3D inputRegion) {
1813 GREM_ASSERT(output);
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);
1817 GREM_ASSERT(inputRegion.size.width <= outputSize.width - static_cast<uint32_t>(outputOffset.x));
1818 GREM_ASSERT(inputRegion.size.height <= outputSize.height - static_cast<uint32_t>(outputOffset.y));
1819 GREM_ASSERT(inputRegion.size.depth <= outputSize.depth - static_cast<uint32_t>(outputOffset.z));
1820 GREM_ASSERT(input);
1821 GREM_ASSERT(inputRegion.offset.x >= 0 && static_cast<uint32_t>(inputRegion.offset.x) <= inputSize.width);
1822 GREM_ASSERT(inputRegion.offset.y >= 0 && static_cast<uint32_t>(inputRegion.offset.y) <= inputSize.height);
1823 GREM_ASSERT(inputRegion.offset.z >= 0 && static_cast<uint32_t>(inputRegion.offset.z) <= inputSize.depth);
1824 GREM_ASSERT(inputRegion.size.width <= inputSize.width - static_cast<uint32_t>(inputRegion.offset.x));
1825 GREM_ASSERT(inputRegion.size.height <= inputSize.height - static_cast<uint32_t>(inputRegion.offset.y));
1826 GREM_ASSERT(inputRegion.size.depth <= inputSize.depth - static_cast<uint32_t>(inputRegion.offset.z));
1827
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);
1845
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;
1860 }
1861 }
1862 }
1863 }
1864
1893 template <typename OutputComponent, size_t OutputComponentCount, typename InputComponent, size_t InputComponentCount>
1894 static void convertPixels(Extent3D size3D, uint32_t mipLevelCount, byte* output, const byte* input) {
1895 GREM_ASSERT(output);
1896 GREM_ASSERT(input);
1897 GREM_ASSERT(mipLevelCount <= getMaxMipLevelCount(Extent2D{size3D.width, size3D.height}));
1898
1899 const size_t outPixelStride = OutputComponentCount * sizeof(OutputComponent);
1900 const size_t inPixelStride = InputComponentCount * sizeof(InputComponent);
1901
1902 for (uint32_t mipLevel = 0; mipLevel < mipLevelCount; ++mipLevel) {
1903 const Extent3D mipLevelSize = getMipLevelSize3D(size3D, mipLevel);
1905 Region3D{.size = mipLevelSize});
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;
1909 }
1910 }
1911
1940 static void copyPixels(Extent3D outputSize, byte* output, Offset3D outputOffset, Extent3D inputSize, const byte* input, Region3D inputRegion, size_t pixelStride) {
1941 GREM_ASSERT(output);
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);
1945 GREM_ASSERT(inputRegion.size.width <= outputSize.width - static_cast<uint32_t>(outputOffset.x));
1946 GREM_ASSERT(inputRegion.size.height <= outputSize.height - static_cast<uint32_t>(outputOffset.y));
1947 GREM_ASSERT(inputRegion.size.depth <= outputSize.depth - static_cast<uint32_t>(outputOffset.z));
1948 GREM_ASSERT(input);
1949 GREM_ASSERT(inputRegion.offset.x >= 0 && static_cast<uint32_t>(inputRegion.offset.x) <= inputSize.width);
1950 GREM_ASSERT(inputRegion.offset.y >= 0 && static_cast<uint32_t>(inputRegion.offset.y) <= inputSize.height);
1951 GREM_ASSERT(inputRegion.offset.z >= 0 && static_cast<uint32_t>(inputRegion.offset.z) <= inputSize.depth);
1952 GREM_ASSERT(inputRegion.size.width <= inputSize.width - static_cast<uint32_t>(inputRegion.offset.x));
1953 GREM_ASSERT(inputRegion.size.height <= inputSize.height - static_cast<uint32_t>(inputRegion.offset.y));
1954 GREM_ASSERT(inputRegion.size.depth <= inputSize.depth - static_cast<uint32_t>(inputRegion.offset.z));
1955 GREM_ASSERT(pixelStride > 0);
1956
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);
1972
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);
1987 }
1988 }
1989 }
1990 }
1991
2012 static void copyPixels(Extent3D size3D, uint32_t mipLevelCount, byte* output, const byte* input, size_t pixelStride) {
2013 GREM_ASSERT(output);
2014 GREM_ASSERT(input);
2015 GREM_ASSERT(pixelStride > 0);
2016 GREM_ASSERT(mipLevelCount <= getMaxMipLevelCount(Extent2D{size3D.width, size3D.height}));
2017
2018 size_t sizeInBytes = 0;
2019 for (uint32_t mipLevel = 0; mipLevel < mipLevelCount; ++mipLevel) {
2020 const Extent3D mipLevelSize = getMipLevelSize3D(size3D, 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;
2023 }
2024 memcpy(output, input, sizeInBytes);
2025 }
2026
2043 static void transformPixels(Extent3D size, byte* pixels, Region3D region, size_t pixelStride, auto op) {
2044 GREM_ASSERT(pixels);
2045 GREM_ASSERT(pixelStride > 0);
2046
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);
2056
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;
2065 op(pixel);
2066 }
2067 }
2068 }
2069 }
2070
2087 static void transformPixels(Extent3D size3D, uint32_t mipLevelCount, byte* pixels, size_t pixelStride, auto op) {
2088 GREM_ASSERT(pixels);
2089 GREM_ASSERT(pixelStride > 0);
2090 GREM_ASSERT(mipLevelCount <= getMaxMipLevelCount(Extent2D{size3D.width, size3D.height}));
2091
2092 for (uint32_t mipLevel = 0; mipLevel < mipLevelCount; ++mipLevel) {
2093 const Extent3D mipLevelSize = getMipLevelSize3D(size3D, mipLevel);
2094 const size_t mipLevelPixelCount = static_cast<size_t>(mipLevelSize.width) * static_cast<size_t>(mipLevelSize.height) * static_cast<size_t>(mipLevelSize.depth);
2095 transformPixels(mipLevelSize, pixels, Region3D{.size = mipLevelSize}, pixelStride, op);
2096 pixels += pixelStride * mipLevelPixelCount;
2097 }
2098 }
2099
2120 static void fillPixels(Extent3D outputSize, byte* output, Region3D outputRegion, size_t pixelStride, const byte* inputPixel) {
2121 transformPixels(outputSize, output, outputRegion, pixelStride, [&](byte* pixel) -> void { memcpy(pixel, inputPixel, pixelStride); });
2122 }
2123
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); });
2144 }
2145
2170 template <typename OutputComponent, size_t OutputComponentCount, size_t ComponentIndex>
2171 static void setPixelsComponent(Extent3D outputSize, byte* output, Region3D outputRegion, const OutputComponent& value) {
2173 static_assert(OutputComponentCount >= 1 && OutputComponentCount <= 4);
2174 static_assert(ComponentIndex < OutputComponentCount);
2175
2176 transformPixels(outputSize, output, outputRegion, sizeof(OutputComponent) * OutputComponentCount,
2177 [&](byte* pixel) -> void { memcpy(pixel + sizeof(OutputComponent) * ComponentIndex, &value, sizeof(OutputComponent)); });
2178 }
2179
2201 template <typename OutputComponent, size_t OutputComponentCount, size_t ComponentIndex>
2202 static void setPixelsComponent(Extent3D size3D, uint32_t mipLevelCount, byte* output, const OutputComponent& value) {
2204 static_assert(OutputComponentCount >= 1 && OutputComponentCount <= 4);
2205 static_assert(ComponentIndex < OutputComponentCount);
2206
2207 transformPixels(size3D, mipLevelCount, output, sizeof(OutputComponent) * OutputComponentCount,
2208 [&](byte* pixel) -> void { memcpy(pixel + sizeof(OutputComponent) * ComponentIndex, &value, sizeof(OutputComponent)); });
2209 }
2210
2228 template <typename PixelComponent, Color::TransferFunction TransferFunction>
2229 static void transformRGBAPixelsFromStraightToPremultipliedAlpha(Extent3D size3D, uint32_t mipLevelCount, byte* pixels) {
2231
2232 transformPixels(size3D, mipLevelCount, pixels, sizeof(PixelComponent) * 4, [&](byte* pixel) -> void {
2233 vec<4, PixelComponent> pixelValue{};
2234 memcpy(&pixelValue, pixel, sizeof(pixelValue));
2235 vec4 linearPixel{};
2236 if constexpr (TransferFunction == Color::TransferFunction::SRGB) {
2237 linearPixel = Color::convertSRGBToLinear(vec4{pixelValue});
2238 } else {
2239 static_assert(TransferFunction == Color::TransferFunction::LINEAR);
2240 linearPixel = vec4{pixelValue};
2241 }
2242 linearPixel = Color::convertStraightToPremultipliedAlpha(linearPixel);
2243 if constexpr (TransferFunction == Color::TransferFunction::SRGB) {
2244 pixelValue = vec<4, PixelComponent>{Color::convertLinearToSRGB(linearPixel)};
2245 } else {
2246 static_assert(TransferFunction == Color::TransferFunction::LINEAR);
2247 pixelValue = vec<4, PixelComponent>{linearPixel};
2248 }
2249 memcpy(pixel, &pixelValue, sizeof(pixelValue));
2250 });
2251 }
2252
2270 template <typename PixelComponent, Color::TransferFunction TransferFunction>
2271 static void transformRGBAPixelsFromPremultipliedToStraightAlpha(Extent3D size3D, uint32_t mipLevelCount, byte* pixels) {
2273
2274 transformPixels(size3D, mipLevelCount, pixels, sizeof(PixelComponent) * 4, [&](byte* pixel) -> void {
2275 vec<4, PixelComponent> pixelValue{};
2276 memcpy(&pixelValue, pixel, sizeof(pixelValue));
2277 vec4 linearPixel{};
2278 if constexpr (TransferFunction == Color::TransferFunction::SRGB) {
2279 linearPixel = Color::convertSRGBToLinear(vec4{pixelValue});
2280 } else {
2281 static_assert(TransferFunction == Color::TransferFunction::LINEAR);
2282 linearPixel = vec4{pixelValue};
2283 }
2284 linearPixel = Color::convertPremultipliedToStraightAlpha(linearPixel);
2285 if constexpr (TransferFunction == Color::TransferFunction::SRGB) {
2286 pixelValue = vec<4, PixelComponent>{Color::convertLinearToSRGB(linearPixel)};
2287 } else {
2288 static_assert(TransferFunction == Color::TransferFunction::LINEAR);
2289 pixelValue = vec<4, PixelComponent>{linearPixel};
2290 }
2291 memcpy(pixel, &pixelValue, sizeof(pixelValue));
2292 });
2293 }
2294
2309 static void flipVertically(Extent2D size2D, byte* pixels, size_t pixelStride) {
2310 GREM_ASSERT(pixels);
2311 GREM_ASSERT(pixelStride > 0);
2312
2313 if (Limits<size_t>::MAX / static_cast<size_t>(size2D.width) < pixelStride) {
2314 throw std::length_error{"Image size overflow."};
2315 }
2316 const size_t rowStride = pixelStride * static_cast<size_t>(size2D.width);
2317 if (rowStride > 0) {
2318 Allocation<byte> temporaryRow(rowStride);
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);
2327 }
2328 }
2329 }
2330
2340 [[nodiscard]] GREM_API(resource) static ImageFileType determineFileType(Span<const byte> fileContents);
2341
2361 GREM_API(resource) static void savePNG(const ImageView& image, Filesystem& filesystem, CStringView filepath, const ImageSavePNGOptions& options = {});
2362
2378 [[nodiscard]] GREM_API(resource) static Allocation<byte> savePNG(const ImageView& image, const ImageSavePNGOptions& options = {});
2379
2399 GREM_API(resource) static void saveJPEG(const ImageView& image, Filesystem& filesystem, CStringView filepath, const ImageSaveJPEGOptions& options = {});
2400
2416 [[nodiscard]] GREM_API(resource) static Allocation<byte> saveJPEG(const ImageView& image, const ImageSaveJPEGOptions& options = {});
2417
2438 GREM_API(resource) static void saveHDR(const ImageView& image, Filesystem& filesystem, CStringView filepath, const ImageSaveHDROptions& options = {});
2439
2456 [[nodiscard]] GREM_API(resource) static Allocation<byte> saveHDR(const ImageView& image, const ImageSaveHDROptions& options = {});
2457
2474 GREM_API(resource) static void saveKTX2(const ImageView& image, Filesystem& filesystem, CStringView filepath, const ImageSaveKTX2Options& options = {});
2475
2488 [[nodiscard]] GREM_API(resource) static Allocation<byte> saveKTX2(const ImageView& image, const ImageSaveKTX2Options& options = {});
2489
2520 GREM_API(resource) static void save(const ImageView& image, Filesystem& filesystem, CStringView filepath, const ImageSaveOptions& options = {});
2521
2525 Image() noexcept = default;
2526
2548 GREM_API(resource) Image(ImageType type, ImageFormat format, Extent3D size3D, uint32_t mipLevelCount, Span<const byte> contents = {});
2549
2571 GREM_API(resource) Image(ImageType type, ImageFormat format, Extent3D size3D, uint32_t mipLevelCount, Allocation<byte> contents);
2572
2581 GREM_API(resource) explicit Image(const ImageView& image);
2582
2610 GREM_API(resource) Image(const Filesystem& filesystem, CStringView filepath, const ImageOptions& options = {});
2611
2636 GREM_API(resource) explicit Image(Span<const byte> fileContents, const ImageOptions& options = {});
2637
2645 operator ImageView() const noexcept {
2647 }
2648
2655 operator ImageReference() noexcept {
2657 }
2658
2662 void reset() noexcept {
2663 *this = Image{};
2664 }
2665
2688 void transcodeTo(byte* output, ImageFormat outputFormat) const {
2689 ImageView{*this}.transcodeTo(output, outputFormat);
2690 }
2691
2707
2723
2729 [[nodiscard]] ImageType getType() const noexcept {
2730 return type;
2731 }
2732
2738 [[nodiscard]] ImageFormat getFormat() const noexcept {
2739 return format;
2740 }
2741
2756 [[nodiscard]] Extent2D getSize2D() const noexcept {
2757 return Extent2D{.width = size3D.width, .height = size3D.height};
2758 }
2759
2770 [[nodiscard]] Extent3D getSize3D() const noexcept {
2771 return size3D;
2772 }
2773
2779 [[nodiscard]] uint32_t getWidth() const noexcept {
2780 return size3D.width;
2781 }
2782
2788 [[nodiscard]] uint32_t getHeight() const noexcept {
2789 return size3D.height;
2790 }
2791
2797 [[nodiscard]] uint32_t getDepth() const noexcept {
2798 return size3D.depth;
2799 }
2800
2806 [[nodiscard]] uint32_t getMipLevelCount() const noexcept {
2807 return mipLevelCount;
2808 }
2809
2823 [[nodiscard]] Span<byte> getContents() noexcept {
2824 return contents;
2825 }
2826
2840 [[nodiscard]] Span<const byte> getContents() const noexcept {
2841 return contents;
2842 }
2843
2851 [[nodiscard]] byte* data() noexcept {
2852 return contents.data();
2853 }
2854
2862 [[nodiscard]] const byte* data() const noexcept {
2863 return contents.data();
2864 }
2865
2875 [[nodiscard]] size_t size() const noexcept {
2876 return contents.size();
2877 }
2878
2896 template <typename Pixel>
2897 [[nodiscard]] constexpr Pixel readPixel(Offset3D position, uint32_t mipLevel = 0) const;
2898
2913 [[nodiscard]] ImageView getPixel(Offset3D position, uint32_t mipLevel = 0) const;
2914
2929 [[nodiscard]] ImageReference getPixel(Offset3D position, uint32_t mipLevel = 0);
2930
2945 [[nodiscard]] ImageView getLayer(uint32_t layer, uint32_t mipLevel = 0) const;
2946
2961 [[nodiscard]] ImageReference getLayer(uint32_t layer, uint32_t mipLevel = 0);
2962
2975 [[nodiscard]] ImageView getMipLevel(uint32_t mipLevel) const;
2976
2988 [[nodiscard]] ImageReference getMipLevel(uint32_t mipLevel);
2989
3005 [[nodiscard]] ImageView getMipLevels(uint32_t firstMipLevel, uint32_t mipLevels) const;
3006
3021 [[nodiscard]] ImageReference getMipLevels(uint32_t firstMipLevel, uint32_t mipLevels);
3022
3036 [[nodiscard]] ImageView getMipLevels(uint32_t firstMipLevel) const;
3037
3051 [[nodiscard]] ImageReference getMipLevels(uint32_t firstMipLevel);
3052
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);
3078 }
3079
3097 [[nodiscard]] Image getPadded(uint32_t padding) const {
3098 return ImageView{*this}.getPadded(padding);
3099 }
3100
3101private:
3102 Allocation<byte> contents{};
3105 Extent3D size3D{.width = 0, .height = 0, .depth = 0};
3106 uint32_t mipLevelCount;
3107};
3108
3109template <typename Pixel>
3110constexpr Pixel ImageView::readPixel(Offset3D position, uint32_t mipLevel) const {
3111 return ImageReference{type, format, size3D, mipLevelCount, Span{const_cast<byte*>(contents.data()), contents.size()}}.readPixel<Pixel>(position, mipLevel);
3112}
3113
3114constexpr ImageView ImageView::getPixel(Offset3D position, uint32_t mipLevel) const {
3115 return ImageReference{type, format, size3D, mipLevelCount, Span{const_cast<byte*>(contents.data()), contents.size()}}.getPixel(position, mipLevel);
3116}
3117
3118constexpr ImageView ImageView::getLayer(uint32_t layer, uint32_t mipLevel) const {
3119 return ImageReference{type, format, size3D, mipLevelCount, Span{const_cast<byte*>(contents.data()), contents.size()}}.getLayer(layer, mipLevel);
3120}
3121
3122constexpr ImageView ImageView::getMipLevel(uint32_t mipLevel) const {
3123 return ImageReference{type, format, size3D, mipLevelCount, Span{const_cast<byte*>(contents.data()), contents.size()}}.getMipLevel(mipLevel);
3124}
3125
3126constexpr ImageView ImageView::getMipLevels(uint32_t firstMipLevel, uint32_t mipLevels) const {
3127 return ImageReference{type, format, size3D, mipLevelCount, Span{const_cast<byte*>(contents.data()), contents.size()}}.getMipLevels(firstMipLevel, mipLevels);
3128}
3129
3130constexpr ImageView ImageView::getMipLevels(uint32_t firstMipLevel) const {
3131 return ImageReference{type, format, size3D, mipLevelCount, Span{const_cast<byte*>(contents.data()), contents.size()}}.getMipLevels(firstMipLevel);
3132}
3133
3134template <typename Pixel>
3135constexpr Pixel ImageReference::readPixel(Offset3D position, uint32_t mipLevel) const {
3136 static_assert(trivially_copyable<Pixel>);
3137 const ImageView pixel = getPixel(position, mipLevel);
3138 GREM_ASSERT(pixel.size() == sizeof(Pixel));
3139 Pixel result;
3140 memcpy(&result, pixel.data(), sizeof(Pixel));
3141 return result;
3142}
3143
3144constexpr ImageReference ImageReference::getPixel(Offset3D position, uint32_t mipLevel) const {
3145 GREM_ASSERT(position.x >= 0 && static_cast<uint32_t>(position.x) < getWidth());
3146 GREM_ASSERT(position.y >= 0 && static_cast<uint32_t>(position.y) < getHeight());
3147 GREM_ASSERT(position.z >= 0 && static_cast<uint32_t>(position.z) < getDepth());
3148 const ImageReference layer = getLayer(static_cast<uint32_t>(position.z), mipLevel);
3149 const size_t pixelStride = Image::getPixelStride(format);
3150 if (pixelStride == 0) {
3151 throw std::invalid_argument{"Invalid image format."};
3152 }
3153 const size_t pixelIndex = static_cast<size_t>(position.y) * static_cast<size_t>(getWidth()) + static_cast<size_t>(position.x);
3154 return ImageReference{ImageType::IMAGE_2D, format, Extent2D{1, 1}, 1, layer.getContents().subspan(pixelIndex * pixelStride, pixelStride)};
3155}
3156
3157constexpr ImageReference ImageReference::getLayer(uint32_t layer, uint32_t mipLevel) const {
3158 const ImageReference layers = getMipLevel(mipLevel);
3159 const size_t layerStride = Image::getLayerStride(format, layers.getSize2D());
3160 if (layerStride == 0) {
3161 throw std::invalid_argument{"Invalid image format."};
3162 }
3163 return ImageReference{ImageType::IMAGE_2D, format, layers.getSize2D(), 1, layers.getContents().subspan(static_cast<size_t>(layer) * layerStride, layerStride)};
3164}
3165
3166constexpr ImageReference ImageReference::getMipLevel(uint32_t mipLevel) const {
3167 const Extent2D blockSize = Image::getBlockSize2D(format);
3168 if (blockSize.width == 0 || blockSize.height == 0) {
3169 throw std::invalid_argument{"Invalid image format."};
3170 }
3171 byte* pointer = data();
3172 for (uint32_t level = 0; level < mipLevel; ++level) {
3173 const Extent3D mipLevelSize3D = Image::getMipLevelSize3D(size3D, level);
3174 const size_t mipLevelStride = Image::getMipLevelStride(format, mipLevelSize3D);
3175 pointer += mipLevelStride;
3176 }
3177 const Extent3D mipLevelSize3D = Image::getMipLevelSize3D(size3D, mipLevel);
3178 const size_t mipLevelStride = Image::getMipLevelStride(format, mipLevelSize3D);
3179 return ImageReference{type, format, mipLevelSize3D, 1, Span{pointer, mipLevelStride}};
3180}
3181
3182constexpr ImageReference ImageReference::getMipLevels(uint32_t firstMipLevel, uint32_t mipLevels) const {
3183 GREM_ASSERT(firstMipLevel <= getMipLevelCount());
3184 GREM_ASSERT(mipLevels <= getMipLevelCount() - firstMipLevel);
3185 const Extent2D blockSize = Image::getBlockSize2D(format);
3186 if (blockSize.width == 0 || blockSize.height == 0) {
3187 throw std::invalid_argument{"Invalid image format."};
3188 }
3189 byte* pointer = data();
3190 for (uint32_t level = 0; level < firstMipLevel; ++level) {
3191 const Extent3D mipLevelSize3D = Image::getMipLevelSize3D(size3D, level);
3192 const size_t mipLevelStride = Image::getMipLevelStride(format, mipLevelSize3D);
3193 pointer += mipLevelStride;
3194 }
3195 size_t size = 0;
3196 const uint32_t mipLevelsEnd = firstMipLevel + mipLevels;
3197 for (uint32_t level = firstMipLevel; level < mipLevelsEnd; ++level) {
3198 const Extent3D mipLevelSize3D = Image::getMipLevelSize3D(size3D, level);
3199 const size_t mipLevelStride = Image::getMipLevelStride(format, mipLevelSize3D);
3200 size += mipLevelStride;
3201 }
3202 return ImageReference{type, format, Image::getMipLevelSize3D(size3D, firstMipLevel), mipLevels, Span{pointer, size}};
3203}
3204
3205constexpr ImageReference ImageReference::getMipLevels(uint32_t firstMipLevel) const {
3206 GREM_ASSERT(firstMipLevel <= getMipLevelCount());
3207 return getMipLevels(firstMipLevel, getMipLevelCount() - firstMipLevel);
3208}
3209
3210inline Image ImageView::getPadded(uint32_t padding) const {
3211 return getPadded(padding, padding, padding, padding);
3212}
3213
3214inline Image ImageReference::getPadded(uint32_t paddingLeft, uint32_t paddingRight, uint32_t paddingTop, uint32_t paddingBottom) const {
3215 return ImageView{*this}.getPadded(paddingLeft, paddingRight, paddingTop, paddingBottom);
3216}
3217
3218inline Image ImageReference::getPadded(uint32_t padding) const {
3219 return ImageView{*this}.getPadded(padding);
3220}
3221
3222template <typename Pixel>
3223constexpr Pixel Image::readPixel(Offset3D position, uint32_t mipLevel) const {
3224 return ImageView{*this}.readPixel<Pixel>(position, mipLevel);
3225}
3226
3227inline ImageView Image::getPixel(Offset3D position, uint32_t mipLevel) const {
3228 return ImageView{*this}.getPixel(position, mipLevel);
3229}
3230
3231inline ImageReference Image::getPixel(Offset3D position, uint32_t mipLevel) {
3232 return ImageReference{*this}.getPixel(position, mipLevel);
3233}
3234
3235inline ImageView Image::getLayer(uint32_t layer, uint32_t mipLevel) const {
3236 return ImageView{*this}.getLayer(layer, mipLevel);
3237}
3238
3239inline ImageReference Image::getLayer(uint32_t layer, uint32_t mipLevel) {
3240 return ImageReference{*this}.getLayer(layer, mipLevel);
3241}
3242
3243inline ImageView Image::getMipLevel(uint32_t mipLevel) const {
3244 return ImageView{*this}.getMipLevel(mipLevel);
3245}
3246
3247inline ImageReference Image::getMipLevel(uint32_t mipLevel) {
3248 return ImageReference{*this}.getMipLevel(mipLevel);
3249}
3250
3251inline ImageView Image::getMipLevels(uint32_t firstMipLevel, uint32_t mipLevels) const {
3252 return ImageView{*this}.getMipLevels(firstMipLevel, mipLevels);
3253}
3254
3255inline ImageReference Image::getMipLevels(uint32_t firstMipLevel, uint32_t mipLevels) {
3256 return ImageReference{*this}.getMipLevels(firstMipLevel, mipLevels);
3257}
3258
3259inline ImageView Image::getMipLevels(uint32_t firstMipLevel) const {
3260 return ImageView{*this}.getMipLevels(firstMipLevel);
3261}
3262
3263inline ImageReference Image::getMipLevels(uint32_t firstMipLevel) {
3264 return ImageReference{*this}.getMipLevels(firstMipLevel);
3265}
3266
3267} // namespace grem::resource
3268
3269#endif
#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
Definition Span.hpp:146
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
Definition Window.hpp:15
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
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
Definition math.hpp:58