-
Notifications
You must be signed in to change notification settings - Fork 7
/
dds.py
443 lines (317 loc) · 12.7 KB
/
dds.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright © 2016-2018 AboodXD
# Supported formats:
# -RGBA8
# -RGB10A2
# -RGB565
# -RGB5A1
# -RGBA4
# -L8
# -L8A8
# -L4A4
# -ETC1
# -BC1
# -BC2
# -BC3
# -BC4U
# -BC4S
# -BC5U
# -BC5S
# Feel free to include this in your own program if you want, just give credits. :)
"""dds.py: DDS reader and header generator."""
import struct
try:
import pyximport
pyximport.install()
import form_conv_cy as form_conv
except ImportError:
import form_conv
dx10_formats = ["BC4U", "BC4S", "BC5U", "BC5S"]
def readDDS(f, SRGB):
with open(f, "rb") as inf:
inb = inf.read()
if len(inb) < 0x80 or inb[:4] != b'DDS ':
print("")
print(f + " is not a valid DDS file!")
return 0, 0, 0, b'', 0, [], 0, []
width = struct.unpack("<I", inb[16:20])[0]
height = struct.unpack("<I", inb[12:16])[0]
fourcc = inb[84:88]
pflags = struct.unpack("<I", inb[80:84])[0]
bpp = struct.unpack("<I", inb[88:92])[0] >> 3
channel0 = struct.unpack("<I", inb[92:96])[0]
channel1 = struct.unpack("<I", inb[96:100])[0]
channel2 = struct.unpack("<I", inb[100:104])[0]
channel3 = struct.unpack("<I", inb[104:108])[0]
caps = struct.unpack("<I", inb[108:112])[0]
if caps not in [0x1000, 0x401008]:
print("")
print("Invalid texture.")
return 0, 0, 0, b'', 0, [], 0, []
abgr8_masks = {0xff: 0, 0xff00: 1, 0xff0000: 2, 0xff000000: 3, 0: 5}
bgr8_masks = {0xff: 0, 0xff00: 1, 0xff0000: 2, 0: 5}
a2rgb10_masks = {0x3ff00000: 0, 0xffc00: 1, 0x3ff: 2, 0xc0000000: 3, 0: 5}
bgr565_masks = {0x1f: 0, 0x7e0: 1, 0xf800: 2, 0: 5}
a1bgr5_masks = {0x1f: 0, 0x3e0: 1, 0x7c00: 2, 0x8000: 3, 0: 5}
abgr4_masks = {0xf: 0, 0xf0: 1, 0xf00: 2, 0xf000: 3, 0: 5}
l8_masks = {0xff: 0, 0: 5}
a8l8_masks = {0xff: 0, 0xff00: 1, 0: 5}
a4l4_masks = {0xf: 0, 0xf0: 1, 0: 5}
compressed = False
luminance = False
rgb = False
has_alpha = False
if pflags == 4:
compressed = True
elif pflags == 0x20000 or pflags == 2:
luminance = True
elif pflags == 0x20001:
luminance = True
has_alpha = True
elif pflags == 0x40:
rgb = True
elif pflags == 0x41:
rgb = True
has_alpha = True
else:
print("")
print("Invalid texture.")
return 0, 0, 0, b'', 0, [], 0, []
format_ = 0
compSel = [0, 1, 2, 3]
if fourcc == b'DX10':
if not compressed:
print("")
print("Uncompressed DX10 DDS files are not supported.")
return 0, 0, 0, b'', 0, [], 0, []
headSize = 0x94
else:
headSize = 0x80
if compressed:
if fourcc == b'ETC1':
format_ = 0x31
bpp = 8
elif fourcc == b'DXT1':
format_ = 0x431 if SRGB else 0x31
bpp = 8
elif fourcc == b'DXT3':
format_ = 0x432 if SRGB else 0x32
bpp = 16
elif fourcc == b'DXT5':
format_ = 0x433 if SRGB else 0x33
bpp = 16
elif fourcc in [b'BC4U', b'ATI1']:
format_ = 0x34
bpp = 8
elif fourcc == b'BC4S':
format_ = 0x234
bpp = 8
elif fourcc in [b'BC5U', b'ATI2']:
format_ = 0x35
bpp = 16
elif fourcc == b'BC5S':
format_ = 0x235
bpp = 16
elif fourcc == b'DX10':
if inb[128:148] == b"\x50\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00":
format_ = 0x34
bpp = 8
elif inb[128:148] == b"\x51\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00":
format_ = 0x234
bpp = 8
elif inb[128:148] == b"\x53\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00":
format_ = 0x35
bpp = 16
elif inb[128:148] == b"\x54\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00":
format_ = 0x235
bpp = 16
size = ((width + 3) >> 2) * ((height + 3) >> 2) * bpp
else:
if luminance:
if has_alpha:
if channel0 in a8l8_masks and channel1 in a8l8_masks and channel2 in a8l8_masks and channel3 in a8l8_masks and bpp == 2:
format_ = 7
compSel = [a8l8_masks[channel0], a8l8_masks[channel1], a8l8_masks[channel2], a8l8_masks[channel3]]
elif channel0 in a4l4_masks and channel1 in a4l4_masks and channel2 in a4l4_masks and channel3 in a4l4_masks and bpp == 1:
format_ = 2
compSel = [a4l4_masks[channel0], a4l4_masks[channel1], a4l4_masks[channel2], a4l4_masks[channel3]]
else:
if channel0 in l8_masks and channel1 in l8_masks and channel2 in l8_masks and channel3 in l8_masks and bpp == 1:
format_ = 1
compSel = [l8_masks[channel0], l8_masks[channel1], l8_masks[channel2], l8_masks[channel3]]
elif rgb:
if has_alpha:
if bpp == 4:
if channel0 in abgr8_masks and channel1 in abgr8_masks and channel2 in abgr8_masks and channel3 in abgr8_masks:
format_ = 0x41a if SRGB else 0x1a
compSel = [abgr8_masks[channel0], abgr8_masks[channel1], abgr8_masks[channel2],
abgr8_masks[channel3]]
elif channel0 in a2rgb10_masks and channel1 in a2rgb10_masks and channel2 in a2rgb10_masks and channel3 in a2rgb10_masks:
format_ = 0x19
compSel = [a2rgb10_masks[channel0], a2rgb10_masks[channel1], a2rgb10_masks[channel2],
a2rgb10_masks[channel3]]
elif bpp == 2:
if channel0 in a1bgr5_masks and channel1 in a1bgr5_masks and channel2 in a1bgr5_masks and channel3 in a1bgr5_masks:
format_ = 0xa
compSel = [a1bgr5_masks[channel0], a1bgr5_masks[channel1], a1bgr5_masks[channel2],
a1bgr5_masks[channel3]]
elif channel0 in abgr4_masks and channel1 in abgr4_masks and channel2 in abgr4_masks and channel3 in abgr4_masks:
format_ = 0xb
compSel = [abgr4_masks[channel0], abgr4_masks[channel1], abgr4_masks[channel2],
abgr4_masks[channel3]]
else:
if channel0 in bgr8_masks and channel1 in bgr8_masks and channel2 in bgr8_masks and channel3 == 0 and bpp == 3: # Kinda not looking good if you ask me
format_ = 0x41a if SRGB else 0x1a
compSel = [bgr8_masks[channel0], bgr8_masks[channel1], bgr8_masks[channel2], 3]
if channel0 in bgr565_masks and channel1 in bgr565_masks and channel2 in bgr565_masks and channel3 in bgr565_masks and bpp == 2:
format_ = 8
compSel = [bgr565_masks[channel0], bgr565_masks[channel1], bgr565_masks[channel2],
bgr565_masks[channel3]]
size = width * height * bpp
if caps == 0x401008:
numMips = struct.unpack("<I", inb[28:32])[0] - 1
mipSize = get_mipSize(width, height, bpp, numMips, compressed)
else:
numMips = 0
mipSize = 0
if len(inb) < headSize + size + mipSize:
print("")
print(f + " is not a valid DDS file!")
return 0, 0, 0, b'', 0, [], 0, []
if format_ == 0:
print("")
print("Unsupported DDS format!")
return 0, 0, 0, b'', 0, [], 0, []
data = bytearray(inb[headSize:headSize + size + mipSize])
if format_ in [0x1a, 0x41a] and bpp == 3:
data = form_conv.rgb8torgbx8(data)
bpp += 1
size = width * height * bpp
return width, height, format_, fourcc, size, compSel, numMips, bytes(data)
def get_mipSize(width, height, bpp, numMips, compressed):
size = 0
for i in range(numMips):
level = i + 1
if compressed:
size += ((max(1, width >> level) + 3) >> 2) * ((max(1, height >> level) + 3) >> 2) * bpp
else:
size += max(1, width >> level) * max(1, height >> level) * bpp
return size
def generateHeader(num_mipmaps, w, h, format_, compSel, size, compressed):
hdr = bytearray(128)
luminance = False
RGB = False
compSels = {}
fmtbpp = 0
fourcc = b''
has_alpha = True
if format_ == 28: # ABGR8
RGB = True
compSels = {0: 0x000000ff, 1: 0x0000ff00, 2: 0x00ff0000, 3: 0xff000000, 5: 0}
fmtbpp = 4
elif format_ == 24: # A2RGB10
RGB = True
compSels = {0: 0x3ff00000, 1: 0x000ffc00, 2: 0x000003ff, 3: 0xc0000000, 5: 0}
fmtbpp = 4
elif format_ == 85: # BGR565
RGB = True
compSels = {0: 0x0000001f, 1: 0x000007e0, 2: 0x0000f800, 3: 0, 5: 0}
fmtbpp = 2
has_alpha = False
elif format_ == 86: # A1BGR5
RGB = True
compSels = {0: 0x0000001f, 1: 0x000003e0, 2: 0x00007c00, 3: 0x00008000, 5: 0}
fmtbpp = 2
elif format_ == 115: # ABGR4
RGB = True
compSels = {0: 0x0000000f, 1: 0x000000f0, 2: 0x00000f00, 3: 0x0000f000, 5: 0}
fmtbpp = 2
elif format_ == 61: # L8
luminance = True
compSels = {0: 0x000000ff, 1: 0, 2: 0, 3: 0, 5: 0}
fmtbpp = 1
if compSel[3] != 0:
has_alpha = False
elif format_ == 49: # A8L8
luminance = True
compSels = {0: 0x000000ff, 1: 0x0000ff00, 2: 0, 3: 0, 5: 0}
fmtbpp = 2
elif format_ == 112: # A4L4
luminance = True
compSels = {0: 0x0000000f, 1: 0x000000f0, 2: 0, 3: 0, 5: 0}
fmtbpp = 1
flags = 0x00000001 | 0x00001000 | 0x00000004 | 0x00000002
caps = 0x00001000
if num_mipmaps == 0:
num_mipmaps = 1
elif num_mipmaps != 1:
flags |= 0x00020000
caps |= 0x00000008 | 0x00400000
if not compressed:
flags |= 0x00000008
a = False
if compSel[0] != 0 and compSel[1] != 0 and compSel[2] != 0 and compSel[3] == 0: # ALPHA
a = True
pflags = 0x00000002
elif luminance: # LUMINANCE
pflags = 0x00020000
elif RGB: # RGB
pflags = 0x00000040
else: # Not possible...
return b''
if has_alpha and not a:
pflags |= 0x00000001
size = w * fmtbpp
else:
flags |= 0x00080000
pflags = 0x00000004
if format_ == "ETC1":
fourcc = b'ETC1'
elif format_ == "BC1":
fourcc = b'DXT1'
elif format_ == "BC2":
fourcc = b'DXT3'
elif format_ == "BC3":
fourcc = b'DXT5'
elif format_ in dx10_formats:
fourcc = b'DX10'
hdr[0:0 + 4] = b'DDS '
hdr[4:4 + 4] = 124 .to_bytes(4, 'little')
hdr[8:8 + 4] = flags.to_bytes(4, 'little')
hdr[12:12 + 4] = h.to_bytes(4, 'little')
hdr[16:16 + 4] = w.to_bytes(4, 'little')
hdr[20:20 + 4] = size.to_bytes(4, 'little')
hdr[28:28 + 4] = num_mipmaps.to_bytes(4, 'little')
hdr[76:76 + 4] = 32 .to_bytes(4, 'little')
hdr[80:80 + 4] = pflags.to_bytes(4, 'little')
if compressed:
hdr[84:84 + 4] = fourcc
else:
hdr[88:88 + 4] = (fmtbpp << 3).to_bytes(4, 'little')
if compSel[0] in compSels:
hdr[92:92 + 4] = compSels[compSel[0]].to_bytes(4, 'little')
else:
hdr[92:92 + 4] = compSels[0].to_bytes(4, 'little')
if compSel[1] in compSels:
hdr[96:96 + 4] = compSels[compSel[1]].to_bytes(4, 'little')
else:
hdr[96:96 + 4] = compSels[1].to_bytes(4, 'little')
if compSel[2] in compSels:
hdr[100:100 + 4] = compSels[compSel[2]].to_bytes(4, 'little')
else:
hdr[100:100 + 4] = compSels[2].to_bytes(4, 'little')
if compSel[3] in compSels:
hdr[104:104 + 4] = compSels[compSel[3]].to_bytes(4, 'little')
else:
hdr[104:104 + 4] = compSels[3].to_bytes(4, 'little')
hdr[108:108 + 4] = caps.to_bytes(4, 'little')
if format_ == "BC4U":
hdr += bytearray(b"\x50\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00")
elif format_ == "BC4S":
hdr += bytearray(b"\x51\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00")
elif format_ == "BC5U":
hdr += bytearray(b"\x53\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00")
elif format_ == "BC5S":
hdr += bytearray(b"\x54\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00")
return hdr