18
18
19
19
#include < algorithm>
20
20
#include < cassert>
21
+ #include < cstdint>
21
22
#include < memory>
22
23
#include < new>
23
24
@@ -53,82 +54,90 @@ using namespace DirectX;
53
54
//
54
55
// See DDS.h in the 'Texconv' sample and the 'DirectXTex' library
55
56
// --------------------------------------------------------------------------------------
56
- #pragma pack(push,1)
57
+ namespace
58
+ {
59
+ #pragma pack(push,1)
57
60
58
- constexpr uint32_t DDS_MAGIC = 0x20534444 ; // "DDS "
61
+ constexpr uint32_t DDS_MAGIC = 0x20534444 ; // "DDS "
59
62
60
- struct DDS_PIXELFORMAT
61
- {
62
- uint32_t size;
63
- uint32_t flags;
64
- uint32_t fourCC;
65
- uint32_t RGBBitCount;
66
- uint32_t RBitMask;
67
- uint32_t GBitMask;
68
- uint32_t BBitMask;
69
- uint32_t ABitMask;
70
- };
71
-
72
- #define DDS_FOURCC 0x00000004 // DDPF_FOURCC
73
- #define DDS_RGB 0x00000040 // DDPF_RGB
74
- #define DDS_LUMINANCE 0x00020000 // DDPF_LUMINANCE
75
- #define DDS_ALPHA 0x00000002 // DDPF_ALPHA
76
- #define DDS_BUMPDUDV 0x00080000 // DDPF_BUMPDUDV
77
-
78
- #define DDS_HEADER_FLAGS_VOLUME 0x00800000 // DDSD_DEPTH
79
-
80
- #define DDS_HEIGHT 0x00000002 // DDSD_HEIGHT
81
-
82
- #define DDS_CUBEMAP_POSITIVEX 0x00000600 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX
83
- #define DDS_CUBEMAP_NEGATIVEX 0x00000a00 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX
84
- #define DDS_CUBEMAP_POSITIVEY 0x00001200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY
85
- #define DDS_CUBEMAP_NEGATIVEY 0x00002200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY
86
- #define DDS_CUBEMAP_POSITIVEZ 0x00004200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ
87
- #define DDS_CUBEMAP_NEGATIVEZ 0x00008200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ
88
-
89
- #define DDS_CUBEMAP_ALLFACES ( DDS_CUBEMAP_POSITIVEX | DDS_CUBEMAP_NEGATIVEX |\
90
- DDS_CUBEMAP_POSITIVEY | DDS_CUBEMAP_NEGATIVEY |\
91
- DDS_CUBEMAP_POSITIVEZ | DDS_CUBEMAP_NEGATIVEZ )
92
-
93
- #define DDS_CUBEMAP 0x00000200 // DDSCAPS2_CUBEMAP
94
-
95
- enum DDS_MISC_FLAGS2
96
- {
97
- DDS_MISC_FLAGS2_ALPHA_MODE_MASK = 0x7L ,
98
- };
63
+ struct DDS_PIXELFORMAT
64
+ {
65
+ uint32_t size;
66
+ uint32_t flags;
67
+ uint32_t fourCC;
68
+ uint32_t RGBBitCount;
69
+ uint32_t RBitMask;
70
+ uint32_t GBitMask;
71
+ uint32_t BBitMask;
72
+ uint32_t ABitMask;
73
+ };
74
+
75
+ #define DDS_FOURCC 0x00000004 // DDPF_FOURCC
76
+ #define DDS_RGB 0x00000040 // DDPF_RGB
77
+ #define DDS_LUMINANCE 0x00020000 // DDPF_LUMINANCE
78
+ #define DDS_ALPHA 0x00000002 // DDPF_ALPHA
79
+ #define DDS_BUMPDUDV 0x00080000 // DDPF_BUMPDUDV
80
+
81
+ #define DDS_HEADER_FLAGS_VOLUME 0x00800000 // DDSD_DEPTH
82
+
83
+ #define DDS_HEIGHT 0x00000002 // DDSD_HEIGHT
84
+
85
+ #define DDS_CUBEMAP_POSITIVEX 0x00000600 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX
86
+ #define DDS_CUBEMAP_NEGATIVEX 0x00000a00 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX
87
+ #define DDS_CUBEMAP_POSITIVEY 0x00001200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY
88
+ #define DDS_CUBEMAP_NEGATIVEY 0x00002200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY
89
+ #define DDS_CUBEMAP_POSITIVEZ 0x00004200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ
90
+ #define DDS_CUBEMAP_NEGATIVEZ 0x00008200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ
91
+
92
+ #define DDS_CUBEMAP_ALLFACES ( DDS_CUBEMAP_POSITIVEX | DDS_CUBEMAP_NEGATIVEX |\
93
+ DDS_CUBEMAP_POSITIVEY | DDS_CUBEMAP_NEGATIVEY |\
94
+ DDS_CUBEMAP_POSITIVEZ | DDS_CUBEMAP_NEGATIVEZ )
95
+
96
+ #define DDS_CUBEMAP 0x00000200 // DDSCAPS2_CUBEMAP
97
+
98
+ enum DDS_MISC_FLAGS2
99
+ {
100
+ DDS_MISC_FLAGS2_ALPHA_MODE_MASK = 0x7L ,
101
+ };
99
102
100
- struct DDS_HEADER
101
- {
102
- uint32_t size;
103
- uint32_t flags;
104
- uint32_t height;
105
- uint32_t width;
106
- uint32_t pitchOrLinearSize;
107
- uint32_t depth; // only if DDS_HEADER_FLAGS_VOLUME is set in flags
108
- uint32_t mipMapCount;
109
- uint32_t reserved1[11 ];
110
- DDS_PIXELFORMAT ddspf;
111
- uint32_t caps;
112
- uint32_t caps2;
113
- uint32_t caps3;
114
- uint32_t caps4;
115
- uint32_t reserved2;
116
- };
117
-
118
- struct DDS_HEADER_DXT10
119
- {
120
- DXGI_FORMAT dxgiFormat;
121
- uint32_t resourceDimension;
122
- uint32_t miscFlag; // see D3D11_RESOURCE_MISC_FLAG
123
- uint32_t arraySize;
124
- uint32_t miscFlags2;
125
- };
103
+ struct DDS_HEADER
104
+ {
105
+ uint32_t size;
106
+ uint32_t flags;
107
+ uint32_t height;
108
+ uint32_t width;
109
+ uint32_t pitchOrLinearSize;
110
+ uint32_t depth; // only if DDS_HEADER_FLAGS_VOLUME is set in flags
111
+ uint32_t mipMapCount;
112
+ uint32_t reserved1[11 ];
113
+ DDS_PIXELFORMAT ddspf;
114
+ uint32_t caps;
115
+ uint32_t caps2;
116
+ uint32_t caps3;
117
+ uint32_t caps4;
118
+ uint32_t reserved2;
119
+ };
120
+
121
+ struct DDS_HEADER_DXT10
122
+ {
123
+ DXGI_FORMAT dxgiFormat;
124
+ uint32_t resourceDimension;
125
+ uint32_t miscFlag; // see D3D11_RESOURCE_MISC_FLAG
126
+ uint32_t arraySize;
127
+ uint32_t miscFlags2;
128
+ };
126
129
127
- #pragma pack(pop)
130
+ #pragma pack(pop)
128
131
129
- // --------------------------------------------------------------------------------------
130
- namespace
131
- {
132
+ static_assert (sizeof (DDS_PIXELFORMAT) == 32 , " DDS pixel format size mismatch" );
133
+ static_assert (sizeof (DDS_HEADER) == 124 , " DDS Header size mismatch" );
134
+ static_assert (sizeof (DDS_HEADER_DXT10) == 20 , " DDS DX10 Extended Header size mismatch" );
135
+
136
+ constexpr size_t DDS_MIN_HEADER_SIZE = sizeof (uint32_t ) + sizeof (DDS_HEADER);
137
+ constexpr size_t DDS_DX10_HEADER_SIZE = sizeof (uint32_t ) + sizeof (DDS_HEADER) + sizeof (DDS_HEADER_DXT10);
138
+ static_assert (DDS_DX10_HEADER_SIZE > DDS_MIN_HEADER_SIZE, " DDS DX10 Header should be larger than standard header" );
139
+
140
+ // --------------------------------------------------------------------------------------
132
141
struct handle_closer { void operator ()(HANDLE h) noexcept { if (h) CloseHandle (h); } };
133
142
134
143
using ScopedHandle = std::unique_ptr<void , handle_closer>;
@@ -168,7 +177,7 @@ namespace
168
177
return E_FAIL;
169
178
}
170
179
171
- if (ddsDataSize < ( sizeof ( uint32_t ) + sizeof (DDS_HEADER)) )
180
+ if (ddsDataSize < DDS_MIN_HEADER_SIZE )
172
181
{
173
182
return E_FAIL;
174
183
}
@@ -195,7 +204,7 @@ namespace
195
204
(MAKEFOURCC (' D' , ' X' , ' 1' , ' 0' ) == hdr->ddspf .fourCC ))
196
205
{
197
206
// Must be long enough for both headers and magic value
198
- if (ddsDataSize < ( sizeof ( uint32_t ) + sizeof (DDS_HEADER) + sizeof (DDS_HEADER_DXT10)) )
207
+ if (ddsDataSize < DDS_DX10_HEADER_SIZE )
199
208
{
200
209
return E_FAIL;
201
210
}
@@ -205,8 +214,7 @@ namespace
205
214
206
215
// setup the pointers in the process request
207
216
*header = hdr;
208
- auto offset = sizeof (uint32_t )
209
- + sizeof (DDS_HEADER)
217
+ auto offset = DDS_MIN_HEADER_SIZE
210
218
+ (bDXT10Header ? sizeof (DDS_HEADER_DXT10) : 0u );
211
219
*bitData = ddsData + offset;
212
220
*bitSize = ddsDataSize - offset;
@@ -264,7 +272,7 @@ namespace
264
272
}
265
273
266
274
// Need at least enough data to fill the header and magic number to be a valid DDS
267
- if (fileInfo.EndOfFile .LowPart < ( sizeof ( uint32_t ) + sizeof (DDS_HEADER)) )
275
+ if (fileInfo.EndOfFile .LowPart < DDS_MIN_HEADER_SIZE )
268
276
{
269
277
return E_FAIL;
270
278
}
@@ -319,7 +327,7 @@ namespace
319
327
(MAKEFOURCC (' D' , ' X' , ' 1' , ' 0' ) == hdr->ddspf .fourCC ))
320
328
{
321
329
// Must be long enough for both headers and magic value
322
- if (fileInfo.EndOfFile .LowPart < ( sizeof ( uint32_t ) + sizeof (DDS_HEADER) + sizeof (DDS_HEADER_DXT10)) )
330
+ if (fileInfo.EndOfFile .LowPart < DDS_DX10_HEADER_SIZE )
323
331
{
324
332
ddsData.reset ();
325
333
return E_FAIL;
@@ -330,7 +338,7 @@ namespace
330
338
331
339
// setup the pointers in the process request
332
340
*header = hdr;
333
- auto offset = sizeof ( uint32_t ) + sizeof (DDS_HEADER)
341
+ auto offset = DDS_MIN_HEADER_SIZE
334
342
+ (bDXT10Header ? sizeof (DDS_HEADER_DXT10) : 0u );
335
343
*bitData = ddsData.get () + offset;
336
344
*bitSize = fileInfo.EndOfFile .LowPart - offset;
0 commit comments