-
Notifications
You must be signed in to change notification settings - Fork 5
/
mcc.py
executable file
·229 lines (155 loc) · 4.23 KB
/
mcc.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
""" Copyright 2015-2017 sta256+mpskit at gmail.com
This file is part of mpskit.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See LICENSE file for more details.
"""
from common import *
from PIL import Image, ImageDraw
from palette import attach_palette
import sys, struct, io
pal = [
[0x0,0x0,0x0],
[0x7d,0x7d,0x7d],
[0x41,0x41,0x41],
[0xeb,0xcf,0x9e],
[0xff,0xff,0xff],
[0x82,0x61,0x0],
[0xff,0x0,0x0],
[0x0,0x8,0x28],
[0x4,0x20,0x3c],
[0xc,0x2c,0x49],
[0x0,0x10,0x28],
[0x14,0x34,0x55],
[0x1c,0x41,0x61],
[0xb6,0xb6,0xb6],
[0x5d,0x5d,0x5d],
[0x1c,0x1c,0x1c],
]
def make_sprite(name):
h = Record()
h.name = name
h.bitmap_offset = None
h.unk0 = None
h.width = None
h.height = None
h.bitdepth = None
h.unk2 = None
h.unk3 = None
h.bitmap = None
return h
def get_byte_width(width, bitdepth):
assert bitdepth in (4,8)
k = 8 / bitdepth
return (width + k - 1) // k
def get_sprite_path(s, mcc_name):
opath = mcc_name + '.dir/' + s.name.lower().strip('.').replace('\\', '/') + '.png'
return opath
def save_sprite(s, mcc_name):
assert s.name
assert s.bitmap
opath = get_sprite_path(s, mcc_name)
odir,oname = os.path.split(opath)
os.makedirs(odir, exist_ok=True)
s.bitmap.save(opath)
print(opath)
def read_sprite_header(f, index, s):
f.seek(index * 4)
header_offset = read_uint32(f)
f.seek(header_offset)
s.bitmap_offset = read_uint32(f)
s.unk0 = read_int16(f)
s.width = read_int16(f)
s.height = read_int16(f)
s.bitdepth = read_int16(f)
s.unk2 = read_int16(f)
s.unk3 = read_int16(f)
assert f.tell() == header_offset + 16
return s
def iter_mcc(f):
while 1:
x = f.read(1)
if len(x) == 0 or x[0] == 0xFF:
return
bs = x + f.read(29)
yield bs.decode('ascii')
# skip newline
f.read(2)
def read_mcc(mcc_name):
wmcc_name = 'W{}.MCC'.format(mcc_name)
with open(mcc_name, 'rb') as mcc:
with open(wmcc_name, 'rb') as wmcc:
for i,line in enumerate(iter_mcc(mcc)):
s = make_sprite(line)
read_sprite_header(wmcc, i, s)
if s.bitdepth == 4:
read_sprite_bitmap(wmcc, s)
save_sprite(s, mcc_name)
else:
# not implemented
pass
def write_mcc(mcc_name):
wmcc_name = 'W{}.MCC'.format(mcc_name)
ss = []
with open(mcc_name, 'rb') as mcc:
with open(wmcc_name, 'rb') as wmcc:
for i,line in enumerate(iter_mcc(mcc)):
s = make_sprite(line)
read_sprite_header(wmcc, i, s)
if s.bitdepth == 4:
load_sprite_bitmap(s, mcc_name)
ss.append(s)
else:
# not implemented
pass
with open(wmcc_name, 'r+b') as wmcc:
for s in ss:
write_sprite_bitmap(wmcc, s)
print(wmcc_name)
def load_sprite_bitmap(s, mcc_name):
path = get_sprite_path(s, mcc_name)
s.bitmap = Image.open(path)
def write_sprite_bitmap(f, s):
pix = s.bitmap.load()
f.seek(s.bitmap_offset + 2)
assert f.tell() == s.bitmap_offset + 2
mask = 0xFF >> (8 - s.bitdepth)
for y in range(s.height):
byte = 0x00
shift = 8
for x in range(s.width):
shift -= s.bitdepth
c = pix[x,y]
byte |= (c << shift)
if shift == 0:
write_uint8(f, byte)
shift = 8
byte = 0x00
if shift != 8:
write_uint8(f, byte)
byte = 0x00
byte_size = get_byte_width(s.width, s.bitdepth) * s.height
assert f.tell() - s.bitmap_offset - 2 == byte_size, (f.tell() - s.bitmap_offset - 2, byte_size)
def read_sprite_bitmap(f, s):
dim = (s.width, s.height)
img = Image.new('P', dim)
pix = img.load()
attach_palette(img, pal)
f.seek(s.bitmap_offset + 2)
mask = 0xFF >> (8 - s.bitdepth)
for y in range(s.height):
shift = 0
for x in range(s.width):
if shift == 0:
byte = f.read(1)[0]
shift = 8
shift -= s.bitdepth
color = (byte >> shift) & mask
pix[x,y] = color
s.bitmap = img
byte_size = get_byte_width(s.width, s.bitdepth) * s.height
assert f.tell() == s.bitmap_offset + 2 + byte_size