-
Notifications
You must be signed in to change notification settings - Fork 0
/
zbropen.py
278 lines (225 loc) · 8.71 KB
/
zbropen.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
import os, os.path, struct
from PIL import Image
def makeString(data) -> str:
return data[:data.find(0)].decode("cp1252")
class ZbrLoader:
def __init__(self, path):
with open(path, "rb") as f:
self.data = f.read()
self.filetype = makeString(self.data[0x4C:0x4C+4])[::-1]
self.version = makeString(self.data[0x54:0x5B])
self.type = struct.unpack_from("<I", self.data, 0x4C)
self.pos = 0x5C
def openstruct(self):
tag, type, length = struct.unpack_from("<IHI", self.data, self.pos)
if tag != 0x00018003:
raise Exception("open tag expected, got %8.8x" % tag)
self.pos += 10
return type,self.pos,length
def skipstruct(self, st):
self.pos = st[1] - 10 + st[2] - 4
def closestruct(self):
tag, = struct.unpack_from("<I", self.data, self.pos)
if tag != 0x00008803:
raise Exception("close tag expected, got %8.8x" % tag)
self.pos += 4
def read(self, count : int):
data = self.data[self.pos:self.pos+count]
self.pos += count
return data
def read_rle(self, count : int):
data = []
end = self.pos + count
while self.pos < end:
cmd = self.data[self.pos]
self.pos += 1
if cmd >= 128:
run_len = (255 - cmd) + 1
for i in range(run_len):
val = self.data[self.pos+i]
data.append(val)
self.pos += run_len
else:
run_len = cmd
val = self.data[self.pos]
self.pos += 1
for i in range(run_len):
data.append(val)
return data
def getname(self) -> str:
st = self.openstruct()
if st[0] == 0x0c or st[0] == 0x0e:
s = makeString(self.data[st[1]+2:st[1] - 10 +st[2] - 4])
else:
raise "unknown string type %4.4x" % st[0]
self.skipstruct(st)
self.closestruct()
return s
def getunknown(self) -> bytes:
st = self.openstruct()
data = self.data[st[1]:st[1] - 10 + st[2] - 4]
self.pos = st[1] - 10 + st[2] - 4
self.closestruct()
return data
class ZbrThumbnail:
def __init__(self):
pass
def getimage(self) -> Image:
width,height = int(self.header[2]), int(self.header[3])
im = Image.new("RGB", (width, height))
data = self.data[1][1]
size = width * height
im.putdata(list(zip(data[size*2:size*3], data[0:size], data[size:size*2])))
return im
@staticmethod
def getheader(loader: ZbrLoader):
st = loader.openstruct()
if st[0] == 0x01:
data = struct.unpack("<ffffffffH", loader.read(34))
else:
raise Exception("Unexpected header version %4.4x" % st[0])
loader.closestruct()
return data
@staticmethod
def getcompresseddata(loader):
st = loader.openstruct()
if st[0] == 0x06:
header = struct.unpack("<IIIIIIII", loader.read(32))
uncompressed_size = header[0]
compressed_size = header[5]
data = loader.read_rle(compressed_size - 8)
endmarker = struct.unpack("<I", loader.read(4))
print ("compressed: ", header, "%x" % loader.pos, len(data), st[2])
if len(data) != uncompressed_size:
raise Exception("uncompressed size doesn't match %i != %i" % (len(data), uncompressed_size))
data = (header, data, endmarker)
else:
raise Exception("Unexpected header version %4.4x" % st[0])
loader.closestruct()
return data
@staticmethod
def getdata(loader : ZbrLoader):
st = loader.openstruct()
if st[0] == 0x0A:
header = struct.unpack("<IIBBBBBBBBBBBBBBBBffIH", loader.read(0x26))
compressed = ZbrThumbnail.getcompresseddata(loader)
unknown = loader.read(1)[0]
data = (header, compressed, unknown)
else:
raise Exception("Unexpected header version %4.4x" % st[0])
loader.closestruct()
return (data)
@staticmethod
def create(loader : ZbrLoader):
thumbnail = ZbrThumbnail()
thumbnail.name = loader.getname()
thumbnail.header = ZbrThumbnail.getheader(loader)
thumbnail.data = ZbrThumbnail.getdata(loader)
# print (thumbnail.header)
return thumbnail
class ZbrCanvas:
def __init__(self):
pass
@staticmethod
def getheader(loader : ZbrLoader):
st = loader.openstruct()
if st[0] == 0x01:
data = struct.unpack("<ffffffffH", loader.read(34))
else:
raise Exception("Unexpected header version %4.4x" % st[0])
loader.closestruct()
return data
@staticmethod
def getheader2(loader : ZbrLoader):
st = loader.openstruct()
if st[0] == 0x01:
data = struct.unpack("<ffffffffH", loader.read(34))
else:
raise Exception("Unexpected header version %4.4x" % st[0])
loader.closestruct()
return data
@staticmethod
def getcompresseddata(loader):
st = loader.openstruct()
if st[0] == 0x06:
header = struct.unpack("<IHHIIIIII", loader.read(32))
print ("compressed: ", header, st[2])
uncompressed_size = header[0]
compressed_size = header[6]
if False:
data = loader.read_rle(compressed_size - 8) # this seems to be a 214 pixel wide image with 4 channels following each other
data2 = loader.read_rle(header[6] - 4) # another 214 pixel
data3 = loader.read_rle(header[7]) # another 214 pixel
print (len(data), len(data2), len(data3))
with open ("data.bin", "wb") as f:
f.write(bytes(data))
with open ("data2.bin", "wb") as f:
f.write(bytes(data2))
with open ("data3.bin", "wb") as f:
f.write(bytes(data3))
else:
compressed = st[2] - 32 - 8
data = loader.read_rle(compressed) # 214*2383 pixel of graphics data (separate channels)
print (len(data), compressed) # closer to correct decompressed size, but still missing a bit...
with open ("data.bin", "wb") as f:
f.write(bytes(data))
endmarker = struct.unpack("<I", loader.read(4))
# not sure about this one...
if False and len(data) != uncompressed_size:
raise Exception("uncompressed size doesn't match %i != %i" % (len(data), uncompressed_size))
loader.skipstruct(st)
data = (header, data, endmarker)
else:
raise Exception("Unexpected header version %4.4x" % st[0])
loader.closestruct()
return data
@staticmethod
def getdata2(loader : ZbrLoader):
st = loader.openstruct()
if st[0] == 0x0A:
header = struct.unpack("<IIBBBBBBBBBBBBBBBBffIH", loader.read(0x26))
print (header)
compressed = ZbrCanvas.getcompresseddata(loader)
unknown = loader.read(1)[0]
data = (header, compressed, unknown)
else:
raise Exception("Unexpected header version %4.4x" % st[0])
loader.closestruct()
return (data)
@staticmethod
def getdata(loader : ZbrLoader):
st = loader.openstruct()
if st[0] == 0x1C:
unknown = loader.read(0x50) # some copyright string?
header = struct.unpack("<fIBBBBIfffB", loader.read(29))
print (header)
name = loader.getname()
header2 = ZbrCanvas.getheader2(loader)
print (header2)
data2 = ZbrCanvas.getdata2(loader)
# image = ZbrThumbnail.create(loader)
data = (header,)
else:
raise Exception("Unexpected header version %4.4x" % st[0])
# loader.closestruct()
return data
@staticmethod
def create(loader : ZbrLoader):
canvas = ZbrCanvas()
canvas.thumbnail = ZbrThumbnail.create(loader)
print ("-" * 80)
canvas.unknown1 = loader.getunknown()
canvas.header = ZbrCanvas.getheader(loader)
print (canvas.header)
canvas.data = ZbrCanvas.getdata(loader)
return canvas
def ZbrLoad(path : str):
loader = ZbrLoader(path)
if loader.filetype == "ZBR":
return ZbrCanvas.create(loader)
def main():
doc = ZbrLoad("data/ZBrush Document.ZBR")
im = doc.thumbnail.getimage()
im.save("data/ZBrush Document.thumbnail.png")
if __name__ == "__main__":
main()