-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexThumbnailProvider.cpp
More file actions
340 lines (276 loc) · 9 KB
/
TexThumbnailProvider.cpp
File metadata and controls
340 lines (276 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <shlwapi.h>
#include <thumbcache.h> // For IThumbnailProvider.
#include <wincodec.h> // Windows Imaging Codecs
#include <msxml6.h>
#include <new>
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "windowscodecs.lib")
#pragma comment(lib, "Crypt32.lib")
#include <memory> // Smart Pointers >w<
#include "s3tc.h"
#if defined(_MSC_VER)
#include <intrin.h> // _BitScanReverse64
static inline unsigned int LEADING_ZEROS(unsigned int x) {
unsigned long idx;
if (_BitScanReverse64(&idx, x))
return 31 - idx;
else
return 32;
}
#endif
class CTexThumbProvider : public IInitializeWithStream,
public IThumbnailProvider
{
public:
CTexThumbProvider() : _cRef(1), _pStream(NULL)
{
}
virtual ~CTexThumbProvider()
{
if (_pStream)
{
_pStream->Release();
}
}
// IUnknown
IFACEMETHODIMP QueryInterface(REFIID riid, void **ppv)
{
static const QITAB qit[] =
{
QITABENT(CTexThumbProvider, IInitializeWithStream),
QITABENT(CTexThumbProvider, IThumbnailProvider),
{ 0 },
};
return QISearch(this, qit, riid, ppv);
}
IFACEMETHODIMP_(ULONG) AddRef()
{
return InterlockedIncrement(&_cRef);
}
IFACEMETHODIMP_(ULONG) Release()
{
ULONG cRef = InterlockedDecrement(&_cRef);
if (!cRef)
{
delete this;
}
return cRef;
}
// IInitializeWithStream
IFACEMETHODIMP Initialize(IStream *pStream, DWORD grfMode);
// IThumbnailProvider
IFACEMETHODIMP GetThumbnail(UINT cx, HBITMAP *phbmp, WTS_ALPHATYPE *pdwAlpha);
private:
long _cRef;
IStream *_pStream; // provided during initialization.
};
HRESULT CTexThumbProvider_CreateInstance(REFIID riid, void **ppv)
{
CTexThumbProvider *pNew = new (std::nothrow) CTexThumbProvider();
HRESULT hr = pNew ? S_OK : E_OUTOFMEMORY;
if (SUCCEEDED(hr))
{
hr = pNew->QueryInterface(riid, ppv);
pNew->Release();
}
return hr;
}
// IInitializeWithStream
IFACEMETHODIMP CTexThumbProvider::Initialize(IStream *pStream, DWORD)
{
HRESULT hr = E_UNEXPECTED; // can only be inited once
if (_pStream == NULL)
{
// take a reference to the stream if we have not been inited yet
hr = pStream->QueryInterface(&_pStream);
}
return hr;
}
enum tex_format {
tex_format_etc1 = 0x1,
tex_format_etc2_eac = 0x2,
tex_format_etc2 = 0x3,
tex_format_dxt1 = 0xA,
tex_format_dxt5 = 0xC,
tex_format_bgra8 = 0x14
};
#define tex_magic "TEX"
typedef struct {
uint8_t magic[4];
uint16_t image_width;
uint16_t image_height;
uint8_t unk1;
uint8_t tex_format;
uint8_t unk2;
bool has_mipmaps;
} TEX_HEADER;
void swapUInt8(uint8_t* a, uint8_t* b) {
uint8_t temp = *a;
*a = *b;
*b = temp;
}
int get_num_mipmaps(int width, int height) {
int num = 0;
while (width > 1 || height > 1) {
if (width > 1) width >>= 1;
if (height > 1) height >>= 1;
++num;
}
return num;
}
HRESULT CreateHBitmapFromTex(IStream* pTexStream, HBITMAP* phbmp, WTS_ALPHATYPE* pdwAlpha)
{
*phbmp = NULL;
*pdwAlpha = WTSAT_UNKNOWN;
TEX_HEADER header;
ULONG bytesRead;
HRESULT hr = pTexStream->Read(&header, sizeof(header), &bytesRead);
if (FAILED(hr) || bytesRead != sizeof(header))
return E_FAIL;
if (memcmp(header.magic, "TEX", 3) != 0)
return E_INVALIDARG;
UINT width = header.image_width;
UINT height = header.image_height;
if (header.tex_format == tex_format_bgra8)
{
UINT stride = width * 4;
const UINT bytesPerBlock = 4;
if (header.has_mipmaps) {
unsigned int mipMapCount = get_num_mipmaps(header.image_width, header.image_height);
UINT skip = 0;
// block_size = 1 for bgra8
// Note: don't need to peform the weird calcs bcs bgra8 is not compressed UwU
for (auto x = mipMapCount; x > 0; x--) {
auto blockWidth = max(width / (1 << x), 1);
auto blockHeight = max(height / (1 << x), 1);
skip += bytesPerBlock * blockWidth * blockHeight;
}
LARGE_INTEGER liSkip;
liSkip.QuadPart = skip;
hr = pTexStream->Seek(liSkip, STREAM_SEEK_CUR, NULL);
if (FAILED(hr)) {
return hr;
}
}
const UINT imageSize = stride * height;
BYTE* pixelData = (BYTE*)malloc(imageSize);
if (!pixelData)
return E_OUTOFMEMORY;
hr = pTexStream->Read(pixelData, imageSize, &bytesRead);
if (FAILED(hr) || bytesRead != imageSize)
{
free(pixelData);
return E_FAIL;
}
BITMAPINFO bmi = {};
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = -((LONG)height);
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
void* bits;
HBITMAP hBitmap = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, &bits, NULL, 0);
if (!hBitmap)
{
free(pixelData);
return E_OUTOFMEMORY;
}
memcpy(bits, pixelData, imageSize);
*phbmp = hBitmap;
*pdwAlpha = WTSAT_ARGB;
free(pixelData);
return S_OK;
}
else if (header.tex_format == tex_format_dxt5 || header.tex_format == tex_format_dxt1)
{
const UINT bytesPerBlock = header.tex_format == tex_format_dxt5 ? 16 : 8;
UINT dataSize;
if (header.has_mipmaps) {
unsigned int mipMapCount = get_num_mipmaps(header.image_width, header.image_height);
UINT skip = 0;
// block_size = 4 for dxt5 and dxt1
// Note (+ block_size - 1) simplified to +3 bcs 4 - 1
for (auto x = mipMapCount; x > 0; x--) {
auto curr_width = max(width / (1 << x), 1);
auto curr_height = max(height / (1 << x), 1);
auto blockWidth = (curr_width + 3) / 4;
auto blockHeight = (curr_height + 3) / 4;
skip += bytesPerBlock * blockWidth * blockHeight;
}
LARGE_INTEGER liSkip;
liSkip.QuadPart = skip;
hr = pTexStream->Seek(liSkip, STREAM_SEEK_CUR, NULL);
if (FAILED(hr)) {
return hr;
}
}
const UINT blockWidth = (width + 3) / 4;
const UINT blockHeight = (height + 3) / 4;
dataSize = blockWidth * blockHeight * bytesPerBlock ;
UINT stride = width * 4;
auto dxtData = std::make_unique<uint8_t*>((uint8_t *) malloc(dataSize));
if (!dxtData)
return E_OUTOFMEMORY;
hr = pTexStream->Read(*dxtData, dataSize, &bytesRead);
if (FAILED(hr) || bytesRead != dataSize)
{
return E_FAIL;
}
auto imageData = std::make_unique<unsigned long*>((unsigned long*) malloc(stride * height));
if (!imageData)
{
return E_OUTOFMEMORY;
}
if (header.tex_format == tex_format_dxt5) {
BlockDecompressImageDXT5(width, height, *dxtData, *imageData);
}
else {
BlockDecompressImageDXT1(width, height, *dxtData, *imageData);
}
BITMAPINFO bmi = {};
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = -((LONG)height); // top-down
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
void* bits;
auto finalImage = std::make_unique<unsigned long*>((unsigned long*) malloc(stride * height));
if (!finalImage)
{
return E_OUTOFMEMORY;
}
// RGBA -> ARGB
for (size_t i = 0; i < width * height; ++i)
{
unsigned long rgba = (*imageData)[i];
unsigned char r = (rgba >> 24) & 0xFF;
unsigned char g = (rgba >> 16) & 0xFF;
unsigned char b = (rgba >> 8) & 0xFF;
unsigned char a = rgba & 0xFF;
(*finalImage)[i] = (
(unsigned long)a << 24) |
((unsigned long)r << 16) |
((unsigned long)g << 8) |
((unsigned long)b);
}
imageData = std::move(finalImage);
HBITMAP hBitmap = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, &bits, NULL, 0);
if (!hBitmap)
{
return E_OUTOFMEMORY;
}
memcpy(bits, *imageData, stride * height);
*phbmp = hBitmap;
*pdwAlpha = WTSAT_ARGB;
return S_OK;
}
return E_NOTIMPL;
}
// IThumbnailProvider
IFACEMETHODIMP CTexThumbProvider::GetThumbnail(UINT /* cx */, HBITMAP* phbmp, WTS_ALPHATYPE* pdwAlpha)
{
HRESULT hr = CreateHBitmapFromTex(this->_pStream, phbmp, pdwAlpha);
return hr;
}