ezEngine  Milestone 7
Image_inl.h
1 #pragma once
2 
4 {
5 
6 }
7 
8 ezUInt32 ezImage::GetNumBlocksX(ezUInt32 uiMipLevel) const
9 {
10  EZ_ASSERT_DEV(ezImageFormat::GetType(m_format) == ezImageFormatType::BLOCK_COMPRESSED,
11  "Number of blocks can only be retrieved for block compressed formats.");
12  ezUInt32 uiBlockSize = 4;
13  return (GetWidth(uiMipLevel) + uiBlockSize - 1) / uiBlockSize;
14 }
15 
16 ezUInt32 ezImage::GetNumBlocksY(ezUInt32 uiMipLevel) const
17 {
18  EZ_ASSERT_DEV(ezImageFormat::GetType(m_format) == ezImageFormatType::BLOCK_COMPRESSED,
19  "Number of blocks can only be retrieved for block compressed formats.");
20  ezUInt32 uiBlockSize = 4;
21  return (GetHeight(uiMipLevel) + uiBlockSize - 1) / uiBlockSize;
22 }
23 
24 ezUInt32 ezImage::GetDataSize() const
25 {
26  return ezMath::Max(static_cast<int>(m_data.GetCount()) - 16, 0);
27 }
28 
29 
30 template<typename T>
31 const T* ezImage::GetDataPointer() const
32 {
33  return reinterpret_cast<const T*>(&m_data[0]);
34 }
35 
36 template<typename T>
38 {
39  return const_cast<T*>(static_cast<const ezImage*>(this)->GetDataPointer<T>());
40 }
41 
42 template<typename T>
43 const T* ezImage::GetSubImagePointer(ezUInt32 uiMipLevel, ezUInt32 uiFace, ezUInt32 uiArrayIndex) const
44 {
45  return reinterpret_cast<const T*>(&m_data[GetSubImage(uiMipLevel, uiFace, uiArrayIndex).m_uiDataOffset]);
46 }
47 
48 template<typename T>
49 T* ezImage::GetSubImagePointer(ezUInt32 uiMipLevel, ezUInt32 uiFace, ezUInt32 uiArrayIndex)
50 {
51  return const_cast<T*>(static_cast<const ezImage*>(this)->GetSubImagePointer<T>(uiMipLevel, uiFace, uiArrayIndex));
52 }
53 
54 template<typename T>
55 const T* ezImage::GetPixelPointer(ezUInt32 uiMipLevel, ezUInt32 uiFace, ezUInt32 uiArrayIndex, ezUInt32 x, ezUInt32 y, ezUInt32 z) const
56 {
57  EZ_ASSERT_DEBUG(ezImageFormat::GetType(m_format) == ezImageFormatType::LINEAR, "Pixel pointer can only be retrieved for linear formats.");
58  EZ_ASSERT_DEV(x < this->m_uiWidth, "x out of bounds");
59  EZ_ASSERT_DEV(y < this->m_uiHeight, "y out of bounds");
60  EZ_ASSERT_DEV(z < this->m_uiDepth, "z out of bounds");
61 
62  const ezUInt8* pPointer = GetSubImagePointer<ezUInt8>(uiMipLevel, uiFace, uiArrayIndex);
63 
64  pPointer += z * GetDepthPitch(uiMipLevel);
65  pPointer += y * GetRowPitch(uiMipLevel);
66  pPointer += x * ezImageFormat::GetBitsPerPixel(m_format) / 8;
67 
68  return reinterpret_cast<const T*>(pPointer);
69 }
70 
71 template<typename T>
72 T* ezImage::GetPixelPointer(ezUInt32 uiMipLevel, ezUInt32 uiFace, ezUInt32 uiArrayIndex, ezUInt32 x, ezUInt32 y, ezUInt32 z)
73 {
74  return const_cast<T*>(static_cast<const ezImage*>(this)->GetPixelPointer<T>(uiMipLevel, uiFace, uiArrayIndex, x, y, z));
75 }
76 
77 
78 template<typename T>
79 const T* ezImage::GetBlockPointer(ezUInt32 uiMipLevel, ezUInt32 uiFace, ezUInt32 uiArrayIndex, ezUInt32 uiBlockX, ezUInt32 uiBlockY, ezUInt32 z) const
80 {
81  EZ_ASSERT_DEBUG(ezImageFormat::GetType(m_format) == ezImageFormatType::BLOCK_COMPRESSED,
82  "Block pointer can only be retrieved for block compressed formats.");
83 
84  const ezUInt8* basePointer = GetSubImagePointer<ezUInt8>(uiMipLevel, uiFace, uiArrayIndex);
85 
86  basePointer += z * GetDepthPitch(uiMipLevel);
87 
88  const ezUInt32 uiBlockSize = 4;
89 
90  const ezUInt32 uiNumBlocksX = GetWidth(uiMipLevel) / uiBlockSize;
91 
92  const ezUInt32 uiBlockIndex = uiBlockX + uiNumBlocksX * uiBlockY;
93 
94  basePointer += uiBlockIndex * uiBlockSize * uiBlockSize * ezImageFormat::GetBitsPerPixel(m_format) / 8;
95 
96  return reinterpret_cast<const T*>(basePointer);
97 }
98 
99 template<typename T>
100 T* ezImage::GetBlockPointer(ezUInt32 uiMipLevel, ezUInt32 uiFace, ezUInt32 uiArrayIndex, ezUInt32 uiBlockX, ezUInt32 uiBlockY, ezUInt32 z)
101 {
102  return const_cast<T*>(static_cast<const ezImage*>(this)->GetBlockPointer<T>(uiMipLevel, uiFace, uiArrayIndex, uiBlockX, uiBlockY, z));
103 }
104 
105 
106 const ezImage::SubImage& ezImage::GetSubImage(ezUInt32 uiMipLevel, ezUInt32 uiFace, ezUInt32 uiArrayIndex) const
107 {
108  ValidateSubImageIndices(uiMipLevel, uiFace, uiArrayIndex);
109  return m_subImages[uiMipLevel + m_uiNumMipLevels * (uiFace + m_uiNumFaces * uiArrayIndex)];
110 }
111 
112 ezImage::SubImage& ezImage::GetSubImage(ezUInt32 uiMipLevel, ezUInt32 uiFace, ezUInt32 uiArrayIndex)
113 {
114  ValidateSubImageIndices(uiMipLevel, uiFace, uiArrayIndex);
115  return m_subImages[uiMipLevel + m_uiNumMipLevels * (uiFace + m_uiNumFaces * uiArrayIndex)];
116 }
117 
118 void ezImage::ValidateSubImageIndices(ezUInt32 uiMipLevel, ezUInt32 uiFace, ezUInt32 uiArrayIndex) const
119 {
120  EZ_ASSERT_DEV(uiMipLevel < m_uiNumMipLevels, "Invalid mip level");
121  EZ_ASSERT_DEV(uiFace < m_uiNumFaces, "Invalid uiFace");
122  EZ_ASSERT_DEV(uiArrayIndex < m_uiNumArrayIndices, "Invalid array slice");
123 }
124 
125 ezUInt32 ezImage::GetRowPitch(ezUInt32 uiMipLevel) const
126 {
127  EZ_ASSERT_DEBUG(ezImageFormat::GetType(m_format) == ezImageFormatType::LINEAR, "Row pitch can only be retrieved for linear formats.");
128  return GetSubImage(uiMipLevel, 0, 0).m_uiRowPitch;
129 }
130 
131 ezUInt32 ezImage::GetDepthPitch(ezUInt32 uiMipLevel) const
132 {
133  return GetSubImage(uiMipLevel, 0, 0).m_uiDepthPitch;
134 }
135 
136 ezUInt32 ezImage::GetDataOffSet(ezUInt32 uiMipLevel, ezUInt32 uiFace, ezUInt32 uiArrayIndex) const
137 {
138  return GetSubImage(uiMipLevel, uiFace, uiArrayIndex).m_uiDataOffset;
139 }
140