Plasma Engine  2.0
Loading...
Searching...
No Matches
ImageHeader.h
1#pragma once
2
3#include <Foundation/Basics/Assert.h>
4#include <Foundation/Math/Color8UNorm.h>
5#include <Foundation/Math/Math.h>
6
7#include <Texture/Image/ImageFormat.h>
8#include <Texture/TextureDLL.h>
9
14class PL_TEXTURE_DLL plImageHeader
15{
16public:
18 plImageHeader() { Clear(); }
19
21 void Clear()
22 {
23 m_uiNumMipLevels = 1;
24 m_uiNumFaces = 1;
25 m_uiNumArrayIndices = 1;
26 m_uiWidth = 0;
27 m_uiHeight = 0;
28 m_uiDepth = 1;
29 m_Format = plImageFormat::UNKNOWN;
30 }
31
33 void SetImageFormat(const plImageFormat::Enum& format) { m_Format = format; }
34
36 plImageFormat::Enum GetImageFormat() const { return m_Format; }
37
39 void SetWidth(plUInt32 uiWidth) { m_uiWidth = uiWidth; }
40
42 plUInt32 GetWidth(plUInt32 uiMipLevel = 0) const
43 {
44 PL_ASSERT_DEV(uiMipLevel < m_uiNumMipLevels, "Invalid mip level");
45 return plMath::Max(m_uiWidth >> uiMipLevel, 1U);
46 }
47
49 void SetHeight(plUInt32 uiHeight) { m_uiHeight = uiHeight; }
50
52 plUInt32 GetHeight(plUInt32 uiMipLevel = 0) const
53 {
54 PL_ASSERT_DEV(uiMipLevel < m_uiNumMipLevels, "Invalid mip level");
55 return plMath::Max(m_uiHeight >> uiMipLevel, 1U);
56 }
57
59 void SetDepth(plUInt32 uiDepth) { m_uiDepth = uiDepth; }
60
62 plUInt32 GetDepth(plUInt32 uiMipLevel = 0) const
63 {
64 PL_ASSERT_DEV(uiMipLevel < m_uiNumMipLevels, "Invalid mip level");
65 return plMath::Max(m_uiDepth >> uiMipLevel, 1U);
66 }
67
71 void SetNumMipLevels(plUInt32 uiNumMipLevels) { m_uiNumMipLevels = uiNumMipLevels; }
72
74 plUInt32 GetNumMipLevels() const { return m_uiNumMipLevels; }
75
79 void SetNumFaces(plUInt32 uiNumFaces) { m_uiNumFaces = uiNumFaces; }
80
82 plUInt32 GetNumFaces() const { return m_uiNumFaces; }
83
87 void SetNumArrayIndices(plUInt32 uiNumArrayIndices) { m_uiNumArrayIndices = uiNumArrayIndices; }
88
90 plUInt32 GetNumArrayIndices() const { return m_uiNumArrayIndices; }
91
93 plUInt32 GetPlaneCount() const
94 {
95 return plImageFormat::GetPlaneCount(m_Format);
96 }
97
99 plUInt32 GetNumBlocksX(plUInt32 uiMipLevel = 0, plUInt32 uiPlaneIndex = 0) const
100 {
101 return plImageFormat::GetNumBlocksX(m_Format, GetWidth(uiMipLevel), uiPlaneIndex);
102 }
103
105 plUInt32 GetNumBlocksY(plUInt32 uiMipLevel = 0, plUInt32 uiPlaneIndex = 0) const
106 {
107 return plImageFormat::GetNumBlocksY(m_Format, GetHeight(uiMipLevel), uiPlaneIndex);
108 }
109
111 plUInt32 GetNumBlocksZ(plUInt32 uiMipLevel = 0, plUInt32 uiPlaneIndex = 0) const
112 {
113 return plImageFormat::GetNumBlocksZ(m_Format, GetDepth(uiMipLevel), uiPlaneIndex);
114 }
115
117 plUInt64 GetRowPitch(plUInt32 uiMipLevel = 0, plUInt32 uiPlaneIndex = 0) const
118 {
119 return plImageFormat::GetRowPitch(m_Format, GetWidth(uiMipLevel), uiPlaneIndex);
120 }
121
123 plUInt64 GetDepthPitch(plUInt32 uiMipLevel = 0, plUInt32 uiPlaneIndex = 0) const
124 {
125 return plImageFormat::GetDepthPitch(m_Format, GetWidth(uiMipLevel), GetHeight(uiMipLevel), uiPlaneIndex);
126 }
127
129 plUInt64 ComputeDataSize() const
130 {
131 plUInt64 uiDataSize = 0;
132
133 for (plUInt32 uiMipLevel = 0; uiMipLevel < GetNumMipLevels(); uiMipLevel++)
134 {
135 for (plUInt32 uiPlaneIndex = 0; uiPlaneIndex < GetPlaneCount(); ++uiPlaneIndex)
136 {
137 uiDataSize += GetDepthPitch(uiMipLevel, uiPlaneIndex) * static_cast<plUInt64>(GetDepth(uiMipLevel));
138 }
139 }
140
141 return plMath::SafeMultiply64(uiDataSize, plMath::SafeMultiply32(GetNumArrayIndices(), GetNumFaces()));
142 }
143
145 plUInt32 ComputeNumberOfMipMaps() const
146 {
147 plUInt32 numMipMaps = 1;
148 plUInt32 width = GetWidth();
149 plUInt32 height = GetHeight();
150 plUInt32 depth = GetDepth();
151
152 while (width > 1 || height > 1 || depth > 1)
153 {
154 width = plMath::Max(1u, width / 2);
155 height = plMath::Max(1u, height / 2);
156 depth = plMath::Max(1u, depth / 2);
157
158 numMipMaps++;
159 }
160
161 return numMipMaps;
162 }
163
164 bool operator==(const plImageHeader& other) const
165 {
166 return
167 m_uiNumMipLevels == other.m_uiNumMipLevels &&
168 m_uiNumFaces == other.m_uiNumFaces &&
169 m_uiNumArrayIndices == other.m_uiNumArrayIndices &&
170 m_uiWidth == other.m_uiWidth &&
171 m_uiHeight == other.m_uiHeight &&
172 m_uiDepth == other.m_uiDepth &&
173 m_Format == other.m_Format;
174 }
175
176 bool operator!=(const plImageHeader& other) const
177 {
178 return !operator==(other);
179 }
180
181protected:
182 plUInt32 m_uiNumMipLevels;
183 plUInt32 m_uiNumFaces;
184 plUInt32 m_uiNumArrayIndices;
185
186 plUInt32 m_uiWidth;
187 plUInt32 m_uiHeight;
188 plUInt32 m_uiDepth;
189
190 plImageFormat::Enum m_Format;
191};
A class containing image meta data, such as format and dimensions.
Definition ImageHeader.h:15
void SetNumMipLevels(plUInt32 uiNumMipLevels)
Sets the number of mip levels, including the full-size image.
Definition ImageHeader.h:71
plUInt32 GetNumBlocksZ(plUInt32 uiMipLevel=0, plUInt32 uiPlaneIndex=0) const
Returns the number of blocks contained in a given mip level in the depth direction.
Definition ImageHeader.h:111
void SetNumFaces(plUInt32 uiNumFaces)
Sets the number of cubemap faces. Use 1 for a non-cubemap.
Definition ImageHeader.h:79
plUInt64 ComputeDataSize() const
Computes the data size required for an image with the header's format and dimensions.
Definition ImageHeader.h:129
void SetHeight(plUInt32 uiHeight)
Sets the image height.
Definition ImageHeader.h:49
plImageHeader()
Constructs an image using an unknown format and zero size.
Definition ImageHeader.h:18
void SetWidth(plUInt32 uiWidth)
Sets the image width.
Definition ImageHeader.h:39
void SetImageFormat(const plImageFormat::Enum &format)
Sets the image format.
Definition ImageHeader.h:33
plUInt32 GetHeight(plUInt32 uiMipLevel=0) const
Returns the image height for a given mip level, clamped to 1.
Definition ImageHeader.h:52
plUInt32 GetPlaneCount() const
Returns the number of image planes.
Definition ImageHeader.h:93
plUInt32 GetNumArrayIndices() const
Returns the number of array indices.
Definition ImageHeader.h:90
plUInt32 GetNumFaces() const
Returns the number of cubemap faces, or 1 for a non-cubemap.
Definition ImageHeader.h:82
plUInt32 GetDepth(plUInt32 uiMipLevel=0) const
Returns the image depth for a given mip level, clamped to 1.
Definition ImageHeader.h:62
plUInt32 ComputeNumberOfMipMaps() const
Computes the number of mip maps in the full mip chain.
Definition ImageHeader.h:145
plUInt64 GetRowPitch(plUInt32 uiMipLevel=0, plUInt32 uiPlaneIndex=0) const
Returns the offset in bytes between two subsequent rows of the given mip level.
Definition ImageHeader.h:117
plImageFormat::Enum GetImageFormat() const
Returns the image format.
Definition ImageHeader.h:36
plUInt32 GetNumBlocksY(plUInt32 uiMipLevel=0, plUInt32 uiPlaneIndex=0) const
Returns the number of blocks contained in a given mip level in the horizontal direction.
Definition ImageHeader.h:105
plUInt32 GetNumBlocksX(plUInt32 uiMipLevel=0, plUInt32 uiPlaneIndex=0) const
Returns the number of blocks contained in a given mip level in the horizontal direction.
Definition ImageHeader.h:99
plUInt32 GetNumMipLevels() const
Returns the number of mip levels, including the full-size image.
Definition ImageHeader.h:74
plUInt32 GetWidth(plUInt32 uiMipLevel=0) const
Returns the image width for a given mip level, clamped to 1.
Definition ImageHeader.h:42
void SetNumArrayIndices(plUInt32 uiNumArrayIndices)
Sets the number of array indices.
Definition ImageHeader.h:87
plUInt64 GetDepthPitch(plUInt32 uiMipLevel=0, plUInt32 uiPlaneIndex=0) const
Returns the offset in bytes between two subsequent depth slices of the given mip level.
Definition ImageHeader.h:123
void SetDepth(plUInt32 uiDepth)
Sets the image depth. The default is 1.
Definition ImageHeader.h:59
void Clear()
Constructs an image using an unknown format and zero size.
Definition ImageHeader.h:21
PL_FOUNDATION_DLL plUInt64 SafeMultiply64(plUInt64 a, plUInt64 b, plUInt64 c=1, plUInt64 d=1)
returns a * b. If an overflow happens, the program is terminated.
Definition Math.cpp:200
constexpr PL_ALWAYS_INLINE T Max(T f1, T f2)
Returns the greater value, f1 or f2.
Definition Math_inl.h:39
PL_FOUNDATION_DLL plUInt32 SafeMultiply32(plUInt32 a, plUInt32 b, plUInt32 c=1, plUInt32 d=1)
returns a * b. If an overflow happens, the program is terminated.
Definition Math.cpp:132
static plUInt32 GetNumBlocksZ(Enum format, plUInt32 uiDepth, plUInt32 uiPlaneIndex=0)
Computes the number of blocks in Z direction (compressed) or pixels (if uncompressed) for a given hei...
Definition ImageFormat.cpp:760
static plUInt32 GetPlaneCount(Enum format)
Returns number of planes in the format, or 1 for non-planar formats.
Definition ImageFormat.cpp:665
static plUInt64 GetDepthPitch(Enum format, plUInt32 uiWidth, plUInt32 uiHeight, plUInt32 uiPlaneIndex=0)
Computes the size in bytes of a 2D slice of blocks (compressed) or pixels (if uncompressed) of the gi...
Definition ImageFormat.cpp:770
static plUInt64 GetRowPitch(Enum format, plUInt32 uiWidth, plUInt32 uiPlaneIndex=0)
Computes the size in bytes of a row of blocks (compressed) or pixels (if uncompressed) of the given w...
Definition ImageFormat.cpp:765
static plUInt32 GetNumBlocksX(Enum format, plUInt32 uiWidth, plUInt32 uiPlaneIndex=0)
Computes the number of blocks in X direction (compressed) or pixels (if uncompressed) for a given wid...
Definition ImageFormat.cpp:750
static plUInt32 GetNumBlocksY(Enum format, plUInt32 uiHeight, plUInt32 uiPlaneIndex=0)
Computes the number of blocks in Y direction (compressed) or pixels (if uncompressed) for a given hei...
Definition ImageFormat.cpp:755
Enum
Enum describing the encoding format of the pixels of an image.
Definition ImageFormat.h:68