This repository was archived by the owner on Dec 10, 2022. It is now read-only.
forked from ataulien/ZenLib
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathztex2dds.cpp
471 lines (431 loc) · 17.9 KB
/
ztex2dds.cpp
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/**
* File modified for openZE. Based on NicoDEs ztextools.
*/
#include "ztex2dds.h"
#include <algorithm>
#include <string>
#include "ztex.h"
#include <assert.h>
#include <inttypes.h>
#include <math.h>
#include <string.h>
#include "utils/logger.h"
#define ZTEX2DDS_ERROR_NONE 0 /* No Error */
#define ZTEX2DDS_ERROR_ARGS 1 /* Invalid Params / Syntax */
#define ZTEX2DDS_ERROR_OPEN 2 /* Failed to Open Input */
#define ZTEX2DDS_ERROR_READ 4 /* Failed to Read Input */
#define ZTEX2DDS_ERROR_CREATE 5 /* Failed to Create Output */
#define ZTEX2DDS_ERROR_WRITE 6 /* Failed to Write Output */
#define ZTEX2DDS_ERROR_FORMAT 7 /* Invalid Input File Format */
namespace ZenLoad
{
/**
* @brief Returns whether x is a power of two
*/
static bool IsPowerOfTwo(unsigned long x)
{
if (x == 0)
return false;
while ((x & 1) == 0)
x >>= 1;
return (1 == x);
}
/*FIXME: Scanline Alignment */
static uint32_t GetMipmapSize(unsigned long format, unsigned long width, unsigned long height, int level)
{
unsigned long x;
unsigned long y;
int i;
x = std::max(1ul, width);
y = std::max(1ul, height);
for (i = 0; i < level; i++)
{
if (x > 1)
x >>= 1;
if (y > 1)
y >>= 1;
}
switch (format)
{
case ZTEXFMT_B8G8R8A8:
case ZTEXFMT_R8G8B8A8:
case ZTEXFMT_A8B8G8R8:
case ZTEXFMT_A8R8G8B8:
return x * y * 4;
case ZTEXFMT_B8G8R8:
case ZTEXFMT_R8G8B8:
return x * y * 3;
case ZTEXFMT_A4R4G4B4:
case ZTEXFMT_A1R5G5B5:
case ZTEXFMT_R5G6B5:
return x * y * 2;
case ZTEXFMT_P8:
return x * y;
case ZTEXFMT_DXT1:
return std::max(1ul, x / 4) * std::max(1ul, y / 4) * 8;
case ZTEXFMT_DXT2:
case ZTEXFMT_DXT3:
case ZTEXFMT_DXT4:
case ZTEXFMT_DXT5:
return std::max(1ul, x / 4) * std::max(1ul, y / 4) * 16;
default:
return 0;
}
}
/**
* @brief Utility function to read data from a vector safely
*/
static bool readVectorData(void* target, size_t start, size_t size, const std::vector<uint8_t>& data)
{
if (start + size > data.size())
return false;
memcpy(target, &data[start], size);
return true;
}
/**
* @brief Utility function to write to the end of a vector
*/
static bool writeVectorData(const void* data, size_t size, std::vector<uint8_t>& target)
{
size_t sz=target.size();
target.resize(target.size()+size);
memcpy(&target[sz],data,size);
return true;
}
/**
* @brief Modified ZTEX to DDS conversion
*/
int convertZTEX2DDS(const std::vector<uint8_t>& ztexData, std::vector<uint8_t>& ddsData, bool optionForceARGB, int* pOutWidth, int* pOutHeight)
{
ZTEX_FILE_HEADER ZTexHeader;
uint32_t BytesRead = 0;
uint32_t DdsMagic;
DDSURFACEDESC2 DdsHeader;
int MipmapCount;
int MipmapLevel;
int i;
ozRGBQUAD palentry;
uint32_t BufferSize;
uint32_t MipmapSize;
ozRGBQUAD* Pixel32;
ozRGBTRIPLE* Pixel24;
uint8_t ColorTemp;
// Read header
if (!readVectorData(&ZTexHeader, 0, sizeof(ZTexHeader), ztexData))
return ZTEX2DDS_ERROR_READ;
if (pOutWidth)
*pOutWidth = ZTexHeader.TexInfo.Width;
if (pOutHeight)
*pOutHeight = ZTexHeader.TexInfo.Height;
BytesRead += sizeof(ZTexHeader);
/* 'ZTEX' */
if (ZTexHeader.Signature != ZTEX_FILE_SIGNATURE ||
ZTexHeader.Version != ZTEX_FILE_VERSION_0 ||
ZTexHeader.TexInfo.Format > ZTEXFMT_DXT5)
{
return ZTEX2DDS_ERROR_FORMAT;
}
/* 'DDS ' */
DdsMagic = MAKEFOURCC('D', 'D', 'S', ' ');
if (!writeVectorData(&DdsMagic, sizeof(DdsMagic), ddsData))
{
return ZTEX2DDS_ERROR_WRITE;
}
/* DDSURFACEDESC2 */
memset(&DdsHeader, 0, sizeof(DdsHeader));
DdsHeader.dwSize = sizeof(DDSURFACEDESC2);
DdsHeader.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
DdsHeader.ddsCaps.dwCaps1 = DDSCAPS_TEXTURE;
DdsHeader.dwHeight = ZTexHeader.TexInfo.Height;
DdsHeader.dwWidth = ZTexHeader.TexInfo.Width;
if (ZTexHeader.TexInfo.Format <= ZTEXFMT_P8)
{
DdsHeader.dwFlags |= DDSD_PITCH;
DdsHeader.dwPitchOrLinearSize =
GetMipmapSize(ZTexHeader.TexInfo.Format, DdsHeader.dwWidth, 1, 0);
}
else
{
DdsHeader.dwFlags |= DDSD_LINEARSIZE;
DdsHeader.dwPitchOrLinearSize =
GetMipmapSize(ZTexHeader.TexInfo.Format, DdsHeader.dwWidth, DdsHeader.dwHeight, 0);
}
if (ZTexHeader.TexInfo.MipMaps > 1)
{
DdsHeader.dwFlags |= DDSD_MIPMAPCOUNT;
DdsHeader.ddsCaps.dwCaps1 |= DDSCAPS_MIPMAP | DDSCAPS_COMPLEX;
DdsHeader.dwMipMapCount = ZTexHeader.TexInfo.MipMaps;
}
DdsHeader.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
switch (ZTexHeader.TexInfo.Format)
{
case ZTEXFMT_B8G8R8A8:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
DdsHeader.ddpfPixelFormat.dwRGBBitCount = 32;
DdsHeader.ddpfPixelFormat.dwRBitMask = 0x0000FF00;
DdsHeader.ddpfPixelFormat.dwGBitMask = 0x00FF0000;
DdsHeader.ddpfPixelFormat.dwBBitMask = 0xFF000000;
DdsHeader.ddpfPixelFormat.dwRGBAlphaBitMask = 0x000000FF;
break;
case ZTEXFMT_R8G8B8A8:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
DdsHeader.ddpfPixelFormat.dwRGBBitCount = 32;
DdsHeader.ddpfPixelFormat.dwRBitMask = 0xFF000000;
DdsHeader.ddpfPixelFormat.dwGBitMask = 0x00FF0000;
DdsHeader.ddpfPixelFormat.dwBBitMask = 0x0000FF00;
DdsHeader.ddpfPixelFormat.dwRGBAlphaBitMask = 0x000000FF;
break;
case ZTEXFMT_A8B8G8R8:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
// DdsHeader.ddpfPixelFormat.dwFourCC = D3DFMT_A8B8G8R8; /* 32 */
DdsHeader.ddpfPixelFormat.dwRGBBitCount = 32;
DdsHeader.ddpfPixelFormat.dwRBitMask = 0x000000FF;
DdsHeader.ddpfPixelFormat.dwGBitMask = 0x0000FF00;
DdsHeader.ddpfPixelFormat.dwBBitMask = 0x00FF0000;
DdsHeader.ddpfPixelFormat.dwRGBAlphaBitMask = 0xFF000000;
break;
case ZTEXFMT_A8R8G8B8:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
// DdsHeader.ddpfPixelFormat.dwFourCC = D3DFMT_A8R8G8B8; /* 21 */
DdsHeader.ddpfPixelFormat.dwRGBBitCount = 32;
DdsHeader.ddpfPixelFormat.dwRBitMask = 0x00FF0000;
DdsHeader.ddpfPixelFormat.dwGBitMask = 0x0000FF00;
DdsHeader.ddpfPixelFormat.dwBBitMask = 0x000000FF;
DdsHeader.ddpfPixelFormat.dwRGBAlphaBitMask = 0xFF000000;
break;
case ZTEXFMT_B8G8R8:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_RGB;
DdsHeader.ddpfPixelFormat.dwRGBBitCount = 24;
DdsHeader.ddpfPixelFormat.dwRBitMask = 0x000000FF;
DdsHeader.ddpfPixelFormat.dwGBitMask = 0x0000FF00;
DdsHeader.ddpfPixelFormat.dwBBitMask = 0x00FF0000;
break;
case ZTEXFMT_R8G8B8:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_RGB;
// DdsHeader.ddpfPixelFormat.dwFourCC = D3DFMT_R8G8B8; /* 20 */
DdsHeader.ddpfPixelFormat.dwRGBBitCount = 24;
DdsHeader.ddpfPixelFormat.dwRBitMask = 0x00FF0000;
DdsHeader.ddpfPixelFormat.dwGBitMask = 0x0000FF00;
DdsHeader.ddpfPixelFormat.dwBBitMask = 0x000000FF;
break;
case ZTEXFMT_A4R4G4B4:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
// DdsHeader.ddpfPixelFormat.dwFourCC = D3DFMT_A4R4G4B4; /* 26 */
DdsHeader.ddpfPixelFormat.dwRGBBitCount = 16;
DdsHeader.ddpfPixelFormat.dwRBitMask = 0x00000F00;
DdsHeader.ddpfPixelFormat.dwGBitMask = 0x000000F0;
DdsHeader.ddpfPixelFormat.dwBBitMask = 0x0000000F;
DdsHeader.ddpfPixelFormat.dwRGBAlphaBitMask = 0x0000F000;
break;
case ZTEXFMT_A1R5G5B5:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
// DdsHeader.ddpfPixelFormat.dwFourCC = D3DFMT_A1R5G5B5; /* 25 */
DdsHeader.ddpfPixelFormat.dwRGBBitCount = 16;
DdsHeader.ddpfPixelFormat.dwRBitMask = 0x00007C00;
DdsHeader.ddpfPixelFormat.dwGBitMask = 0x000003E0;
DdsHeader.ddpfPixelFormat.dwBBitMask = 0x0000001F;
DdsHeader.ddpfPixelFormat.dwRGBAlphaBitMask = 0x00008000;
break;
case ZTEXFMT_R5G6B5:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_RGB;
// DdsHeader.ddpfPixelFormat.dwFourCC = D3DFMT_R5G6B5; /* 23 */
DdsHeader.ddpfPixelFormat.dwRGBBitCount = 16;
DdsHeader.ddpfPixelFormat.dwRBitMask = 0x0000F800;
DdsHeader.ddpfPixelFormat.dwGBitMask = 0x000007E0;
DdsHeader.ddpfPixelFormat.dwBBitMask = 0x0000001F;
break;
case ZTEXFMT_P8:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8;
// DdsHeader.ddpfPixelFormat.dwFourCC = D3DFMT_P8; /* 41 */
break;
case ZTEXFMT_DXT1:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
DdsHeader.ddpfPixelFormat.dwFourCC = MAKEFOURCC('D', 'X', 'T', '1');
break;
case ZTEXFMT_DXT2:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
DdsHeader.ddpfPixelFormat.dwFourCC = MAKEFOURCC('D', 'X', 'T', '2');
break;
case ZTEXFMT_DXT3:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
DdsHeader.ddpfPixelFormat.dwFourCC = MAKEFOURCC('D', 'X', 'T', '3');
break;
case ZTEXFMT_DXT4:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
DdsHeader.ddpfPixelFormat.dwFourCC = MAKEFOURCC('D', 'X', 'T', '4');
break;
case ZTEXFMT_DXT5:
DdsHeader.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
DdsHeader.ddpfPixelFormat.dwFourCC = MAKEFOURCC('D', 'X', 'T', '5');
break;
}
if (optionForceARGB)
{
switch (ZTexHeader.TexInfo.Format)
{
case ZTEXFMT_B8G8R8A8:
case ZTEXFMT_R8G8B8A8:
case ZTEXFMT_A8B8G8R8:
DdsHeader.ddpfPixelFormat.dwRBitMask = 0x00FF0000;
DdsHeader.ddpfPixelFormat.dwGBitMask = 0x0000FF00;
DdsHeader.ddpfPixelFormat.dwBBitMask = 0x000000FF;
DdsHeader.ddpfPixelFormat.dwRGBAlphaBitMask = 0xFF000000;
break;
case ZTEXFMT_B8G8R8:
DdsHeader.ddpfPixelFormat.dwRBitMask = 0x00FF0000;
DdsHeader.ddpfPixelFormat.dwGBitMask = 0x0000FF00;
DdsHeader.ddpfPixelFormat.dwBBitMask = 0x000000FF;
break;
default:
break;
}
}
if (!writeVectorData(&DdsHeader, sizeof(DdsHeader), ddsData))
{
return ZTEX2DDS_ERROR_WRITE;
}
/* Palette */
if (ZTEXFMT_P8 == ZTexHeader.TexInfo.Format)
{
for (i = 0; i < ZTEX_PAL_ENTRIES; i++)
{
palentry.rgbReserved = 0x00;
if (!readVectorData(&palentry, BytesRead, sizeof(palentry), ztexData))
{
return ZTEX2DDS_ERROR_READ;
}
BytesRead += sizeof(palentry);
if (!writeVectorData(&palentry, sizeof(ozRGBQUAD), ddsData))
{
return ZTEX2DDS_ERROR_WRITE;
}
}
}
/* Mipmaps */
MipmapCount = std::max(1, static_cast<int>(ZTexHeader.TexInfo.MipMaps));
BufferSize = 0;
for (MipmapLevel = 0; MipmapLevel < MipmapCount; MipmapLevel++)
BufferSize += GetMipmapSize(ZTexHeader.TexInfo.Format,
ZTexHeader.TexInfo.Width, ZTexHeader.TexInfo.Height, MipmapLevel);
if(BytesRead+BufferSize>ztexData.size())
return ZTEX2DDS_ERROR_READ;
const uint8_t* Buffer = reinterpret_cast<const uint8_t*>(&ztexData[BytesRead]);
BytesRead += BufferSize;
const uint8_t* Mipmap = Buffer + BufferSize;
for (MipmapLevel = 0; MipmapLevel < MipmapCount; MipmapLevel++)
{
MipmapSize = GetMipmapSize(ZTexHeader.TexInfo.Format,
ZTexHeader.TexInfo.Width, ZTexHeader.TexInfo.Height, MipmapLevel);
Mipmap -= MipmapSize;
if (optionForceARGB)
{
Pixel32 = (ozRGBQUAD*)Mipmap;
Pixel24 = (ozRGBTRIPLE*)Mipmap;
switch (ZTexHeader.TexInfo.Format)
{
case ZTEXFMT_B8G8R8A8:
for (i = 0; i < (int)MipmapSize / 4; i++)
{
ColorTemp = Pixel32->rgbReserved;
Pixel32->rgbReserved = Pixel32->rgbBlue;
Pixel32->rgbBlue = ColorTemp;
ColorTemp = Pixel32->rgbRed;
Pixel32->rgbRed = Pixel32->rgbGreen;
Pixel32->rgbGreen = ColorTemp;
Pixel32++;
}
break;
case ZTEXFMT_R8G8B8A8:
for (i = 0; i < (int)MipmapSize / 4; i++)
{
ColorTemp = Pixel32->rgbBlue;
Pixel32->rgbBlue = Pixel32->rgbGreen;
Pixel32->rgbGreen = Pixel32->rgbRed;
Pixel32->rgbRed = Pixel32->rgbReserved;
Pixel32->rgbReserved = ColorTemp;
Pixel32++;
}
break;
case ZTEXFMT_A8B8G8R8:
for (i = 0; i < (int)MipmapSize / 4; i++)
{
ColorTemp = Pixel32->rgbBlue;
Pixel32->rgbBlue = Pixel32->rgbRed;
Pixel32->rgbRed = ColorTemp;
Pixel32++;
}
break;
case ZTEXFMT_B8G8R8:
for (i = 0; i < (int)MipmapSize / 3; i++)
{
ColorTemp = Pixel24->rgbtBlue;
Pixel24->rgbtBlue = Pixel24->rgbtRed;
Pixel24->rgbtRed = ColorTemp;
Pixel24++;
}
break;
default:
break;
}
}
if (!writeVectorData(Mipmap, MipmapSize, ddsData))
{
return ZTEX2DDS_ERROR_WRITE;
}
}
return ZTEX2DDS_ERROR_NONE;
}
static uint32_t ComputeSizeInBytes(int mip, int resolutionX, int resolutionY, bool dxt1)
{
int px = (int)std::max(1.0f, (float)floor(resolutionX / pow(2.0f, mip)));
int py = (int)std::max(1.0f, (float)floor(resolutionY / pow(2.0f, mip)));
//int px = TextureSize.x;
//int py = TextureSize.y;
// compute the storage requirements
int blockcount = ((px + 3) / 4) * ((py + 3) / 4);
int blocksize = dxt1 ? 8 : 16;
return blockcount * blocksize;
}
DXTLevel getDXTLevelFromDDS(const std::vector<uint8_t>& ddsData)
{
DDSURFACEDESC2 desc = getSurfaceDesc(ddsData);
switch (desc.ddpfPixelFormat.dwFourCC)
{
case MAKEFOURCC('D', 'X', 'T', '1'):
return DXTLevel::DXT1;
break;
case MAKEFOURCC('D', 'X', 'T', '3'):
return DXTLevel::DXT3;
break;
case MAKEFOURCC('D', 'X', 'T', '5'):
return DXTLevel::DXT5;
break;
default:
return DXTLevel::Unknown;
}
}
size_t getMipFileOffsetFromDDS(const std::vector<uint8_t>& ddsData, int mip)
{
size_t seek = 0;
seek += sizeof(uint32_t); // Skip magic number
seek += sizeof(DDSURFACEDESC2); // Skip header
DDSURFACEDESC2 desc = getSurfaceDesc(ddsData);
DXTLevel dxt = getDXTLevelFromDDS(ddsData);
for (int i = 0; i < mip; i++)
{
seek += ComputeSizeInBytes(i, desc.dwWidth, desc.dwHeight, dxt == DXTLevel::DXT1);
}
return seek;
}
/**
* @param ddsData Loaded dds
* @return Pointer to the surface info of the given dds
*/
DDSURFACEDESC2 getSurfaceDesc(const std::vector<uint8_t>& ddsData)
{
DDSURFACEDESC2 desc;
memcpy(&desc, &ddsData[sizeof(uint32_t)], sizeof(DDSURFACEDESC2));
return desc;
}
} // namespace ZenLoad
/* THE END */