-
Notifications
You must be signed in to change notification settings - Fork 2
/
EncoderImage.py
225 lines (201 loc) · 6.59 KB
/
EncoderImage.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
# wadcode - WAD compiler/decompiler for WAD resource files
# Copyright (C) 2019-2019 Johannes Bauer
#
# This file is part of wadcode.
#
# wadcode is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; this program is ONLY licensed under
# version 3 of the License, later versions are explicitly excluded.
#
# wadcode is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Johannes Bauer <JohannesBauer@gmx.de>
import io
import png
import json
import math
from NamedStruct import NamedStruct
class Palette():
def __init__(self, colors):
self._colors = colors
self._index_by_color = { rgb: index for (index, rgb) in enumerate(self._colors) }
@staticmethod
def rgb_diff(rgb1, rgb2):
return math.sqrt((rgb1[0] - rgb2[0]) ** 2 + (rgb1[1] - rgb2[1]) ** 2 + (rgb1[2] - rgb2[2]) ** 2)
def _lookup_closest(self, rgb):
best_index = 0
best_error = self.rgb_diff(rgb, self._colors[best_index])
for (index, pixel) in enumerate(self._colors[1:], 1):
error = self.rgb_diff(rgb, pixel)
if error < best_error:
best_error = error
best_index = index
return best_index
def lookup(self, rgb):
if rgb in self._index_by_color:
return self._index_by_color[rgb]
else:
return self._lookup_closest(rgb)
def write_gimp_palette(self, filename):
with open(filename, "w") as f:
print("GIMP Palette", file = f)
print("Name: Doom", file = f)
print("Columns: 8", file = f)
print("#", file = f)
for color in self._colors:
print("%3d %3d %3d Unknown" % (color[0], color[1], color[2]), file = f)
@classmethod
def load_from_json(cls, filename):
with open(filename) as f:
colors = json.load(f)
colors = [ (int(color[0 : 2], 16), int(color[2 : 4], 16), int(color[4 : 6], 16)) for color in colors ]
return cls(colors)
def __getitem__(self, index):
return self._colors[index]
class EncoderImage():
name = "image"
_HEADER = NamedStruct((
("h", "width"),
("h", "height"),
("h", "offsetx"),
("h", "offsety"),
))
_POINTER = NamedStruct((
("l", "offset"),
))
_SPANHDR = NamedStruct((
("B", "yoffset"),
("B", "pixel_cnt"),
("B", "dummy"),
))
_Palette = None
@classmethod
def _generate_palette(cls):
if cls._Palette is not None:
return
cls._Palette = Palette.load_from_json("palette.json")
@classmethod
def decode(cls, encoded_data):
cls._generate_palette()
decoded_data = encoded_data
header = cls._HEADER.unpack_head(encoded_data)
metadata = {
"offsetx": header.offsetx,
"offsety": header.offsety,
}
start_ptrs = [ ]
offset = cls._HEADER.size
for x in range(header.width):
start_ptr = cls._POINTER.unpack_head(encoded_data[offset : ])
start_ptrs.append(start_ptr)
offset += cls._POINTER.size
(img_width, img_height) = (header.width, header.height)
raw_pixel_data = bytearray(4 * img_width * img_height)
for (x, start_ptr) in enumerate(start_ptrs):
offset = start_ptr.offset
ybase = 0
while True:
if encoded_data[offset] == 255:
break
span_hdr = cls._SPANHDR.unpack_head(encoded_data[offset : ])
offset += cls._SPANHDR.size
for y in range(span_hdr.pixel_cnt):
pixel_index = encoded_data[offset + y]
pixel_offset = 4 * (x + ((y + span_hdr.yoffset) * header.width))
pixel_value = cls._Palette[pixel_index]
for i in range(3):
raw_pixel_data[pixel_offset + i] = pixel_value[i]
raw_pixel_data[pixel_offset + 3] = 0xff
offset += span_hdr.pixel_cnt + 1
row_data = [ raw_pixel_data[x : x + (4 * img_width)] for x in range(0, len(raw_pixel_data), 4 * img_width) ]
iobuf = io.BytesIO()
png_image = png.from_array(row_data, mode = "RGBA", info = { "width": header.width, "height": header.height })
png_image.write(iobuf)
decoded_data = iobuf.getvalue()
return (decoded_data, metadata)
@classmethod
def encode(cls, decoded_data, metadata = None):
if metadata is None:
metadata = { }
def encode_column(column):
def emit(start_offset, values):
return cls._SPANHDR.pack({
"yoffset": start_offset,
"pixel_cnt": len(values),
"dummy": 0,
}) + bytes(values) + b"\x00"
encoded_column = bytearray()
start_offset = None
chunk_data = [ ]
for (index, value) in enumerate(column):
if value == -1:
if start_offset is not None:
# No more pixels, but we've seen some before
encoded_column += emit(start_offset, chunk_data)
start_offset = None
chunk_data = [ ]
else:
# There's a pixel there
if start_offset is None:
# First pixel
start_offset = index
chunk_data = [ value ]
else:
# Subsequent pixel
chunk_data.append(value)
if len(chunk_data) >= 128:
encoded_column += emit(start_offset, chunk_data)
start_offset = None
chunk_data = [ ]
if start_offset is not None:
encoded_column += emit(start_offset, chunk_data)
return encoded_column + b"\xff"
cls._generate_palette()
iobuf = io.BytesIO(decoded_data)
(width, height, pixels, info) = png.Reader(file = iobuf).read_flat()
encoded_data = bytearray()
encoded_data += cls._HEADER.pack({
"width": width,
"height": height,
"offsetx": metadata.get("offsetx", 0),
"offsety": metadata.get("offsety", 0),
})
column_offset = len(encoded_data) + (width * cls._POINTER.size)
column_data = bytearray()
pixels = bytes(pixels)
if (info["planes"] == 3) and (info["alpha"] == False) and (len(pixels) == 3 * width * height):
def rgb_to_rgba(data):
for idx in range(0, len(data), 3):
yield data[idx + 0]
yield data[idx + 1]
yield data[idx + 2]
yield 0xff
pixels = bytes(rgb_to_rgba(pixels))
assert(len(pixels) == 4 * width * height)
for x in range(width):
# First get the color indices of each column
col_data = [ ]
for y in range(height):
pixel_offset = 4 * (x + (y * width))
(rgb, a) = (tuple(pixels[pixel_offset + 0 : pixel_offset + 3]), pixels[pixel_offset + 3])
if a == 255:
pixel_index = cls._Palette.lookup(rgb)
else:
pixel_index = -1
col_data.append(pixel_index)
# Then encode them
encoded_col_data = encode_column(col_data)
column_data += encoded_col_data
encoded_data += cls._POINTER.pack({
"offset": column_offset,
})
column_offset += len(encoded_col_data)
encoded_data += column_data
return encoded_data