forked from Marisa-Chan/RF2_rfl_rfc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeg_read.py
More file actions
200 lines (148 loc) · 3.47 KB
/
peg_read.py
File metadata and controls
200 lines (148 loc) · 3.47 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
#!/usr/bin/python3
import sys
import re
from rf2 import xyz, read4u, read4S, read2u, read1u, read1S, readNstr, readFixNstr, readCstr, readflt, readXYZ, readMt33
from PIL import Image as pimg
class Texx:
Name = ""
width = 0
height = 0
fmt = 0
fmt2 = 0
flgs = 0
frmCnt = 0
animDelay = 0
mipCount = 0
unk1 = 0
unk2 = 0
offset = 0
sz = 0
img = None
class Peg:
Texturies = None
def __init__(self):
self.Texturies = list()
def DecodeA1B5G5R5(tx, inp):
img = pimg.new('RGBA', (tx.width, tx.height))
pix = img.load()
ipos = 0
for y in range(tx.height):
for x in range(tx.width):
wrd = int.from_bytes(inp[ipos : ipos + 2], byteorder="little")
a = 255 * (wrd >> 15)
b = int(((wrd >> 10) & 0x1F) * 8.23)
g = int(((wrd >> 5) & 0x1F) * 8.23)
r = int((wrd & 0x1F) * 8.23)
pix[ x , y ] = (r, g, b, a)
ipos += 2
return img
def DecodeA8B8G8R8(tx, inp):
img = pimg.new('RGBA', (tx.width, tx.height))
pix = img.load()
ipos = 0
for y in range(tx.height):
for x in range(tx.width):
bts = inp[ipos : ipos + 4]
a = bts[3]
b = bts[2]
g = bts[1]
r = bts[0]
if (a != 0):
a = 255
pix[ x , y ] = (r, g, b, a)
ipos += 4
return img
def DecodeIndexed(tx, inp):
img = pimg.new('RGBA', (tx.width, tx.height))
pix = img.load()
palette = []
ipos = 0
for p in range(256):
if (tx.fmt2 == 1):
wrd = int.from_bytes(inp[ipos : ipos + 2], byteorder="little")
a = 255 * (wrd >> 15)
b = int(((wrd >> 10) & 0x1F) * 8.23)
g = int(((wrd >> 5) & 0x1F) * 8.23)
r = int((wrd & 0x1F) * 8.23)
palette.append( (r, g, b, a) )
ipos += 2
elif (tx.fmt2 == 2):
bts = inp[ipos : ipos + 4]
a = bts[3]
b = bts[2]
g = bts[1]
r = bts[0]
if (a != 0):
a = 255
palette.append( (r, g, b, a) )
ipos += 4
else:
print("Fmt2:", tx.fmt2)
for y in range(tx.height):
for x in range(tx.width):
idx = inp[ipos]
## PS2 CLUT index Swizzle
idx = idx & 0xE7 | (idx >> 1) & 8 | (idx << 1) & 0x10
pix[ x , y ] = palette[idx]
ipos += 1
return img
def ReadPeg(filename):
fl = open(filename, "rb")
fl.seek(0, 2)
SZ = fl.tell()
fl.seek(0, 0)
Sign = read4u(fl)
if (Sign != 0x564B4547):
return None
Version = read4u(fl)
if (Version != 6):
return None
pg = Peg()
hdrSz = read4u(fl)
datSz = read4u(fl)
texCount = read4u(fl)
unknown14 = read4u(fl)
frameCount = read4u(fl)
unknown1C = read4u(fl)
## Pre allocate
for i in range(texCount):
pg.Texturies.append(Texx())
## Read headers
for i in range(texCount):
tx = pg.Texturies[i]
tx.width = read2u(fl)
tx.height = read2u(fl)
tx.fmt = read1u(fl)
tx.fmt2 = read1u(fl)
tx.flgs = read1u(fl)
tx.frmCnt = read1u(fl)
tx.animDelay = read1u(fl)
tx.mipCount = read1u(fl)
tx.unk1 = read1u(fl)
tx.unk2 = read1u(fl)
tx.Name = readFixNstr(fl, 48)
tx.offset = read4u(fl)
if (i > 0):
pg.Texturies[i - 1].sz = tx.offset - pg.Texturies[i - 1].offset
if (i == texCount - 1):
tx.sz = SZ - tx.offset
# Decode
for tx in pg.Texturies:
fl.seek(tx.offset, 0)
pkd = fl.read(tx.sz)
if (tx.fmt == 3):
tx.img = DecodeA1B5G5R5(tx, pkd)
elif (tx.fmt == 4):
tx.img = DecodeIndexed(tx, pkd)
elif (tx.fmt == 7):
tx.img = DecodeA8B8G8R8(tx, pkd)
else:
print("Unknown format :", tx.fmt)
return pg
def main():
PEG = ReadPeg(sys.argv[1])
for tx in PEG.Texturies:
if (tx.img != None):
tx.img.save(tx.Name + ".png")
if __name__ == '__main__':
main()