junglejourney
changeset 57:c632214161bc
Experiment with image compression.
| author | David Boddie <david@boddie.org.uk> |
|---|---|
| date | Thu Aug 25 01:41:46 2011 +0200 |
| parents | 1a555d848009 |
| children | 76fff1782efe |
| files | tools/makesprites.py |
| diffstat | 1 files changed, 57 insertions(+), 6 deletions(-) [+] |
line diff
1.1 --- a/tools/makesprites.py Thu Aug 25 01:41:20 2011 +0200 1.2 +++ b/tools/makesprites.py Thu Aug 25 01:41:46 2011 +0200 1.3 @@ -170,30 +170,81 @@ 1.4 1.5 # Read 8 rows at a time. 1.6 for row in range(0, len(lines), 8): 1.7 - 1.8 + 1.9 # Read 4 columns at a time. 1.10 for column in range(0, len(lines[0]), 4): 1.11 - 1.12 + 1.13 # Read the rows. 1.14 for line in lines[row:row + 8]: 1.15 - 1.16 + 1.17 shift = 3 1.18 byte = 0 1.19 for pixel in line[column:column + 4]: 1.20 - 1.21 + 1.22 if pixel == "1": 1.23 byte = byte | (0x01 << shift) 1.24 elif pixel == "2": 1.25 byte = byte | (0x10 << shift) 1.26 elif pixel == "3": 1.27 byte = byte | (0x11 << shift) 1.28 - 1.29 + 1.30 shift -= 1 1.31 - 1.32 + 1.33 data += chr(byte) 1.34 1.35 return data 1.36 1.37 +def make_scanline_bytes(lines): 1.38 + 1.39 + data = [] 1.40 + for line in lines: 1.41 + 1.42 + line_data = "" 1.43 + # Read 4 columns at a time. 1.44 + for column in range(0, len(lines[0]), 4): 1.45 + 1.46 + shift = 3 1.47 + byte = 0 1.48 + for pixel in line[column:column + 4]: 1.49 + 1.50 + if pixel == "1": 1.51 + byte = byte | (0x01 << shift) 1.52 + elif pixel == "2": 1.53 + byte = byte | (0x10 << shift) 1.54 + elif pixel == "3": 1.55 + byte = byte | (0x11 << shift) 1.56 + 1.57 + shift -= 1 1.58 + 1.59 + line_data += chr(byte) 1.60 + 1.61 + data.append(line_data) 1.62 + 1.63 + return data 1.64 + 1.65 +def compress(data): 1.66 + 1.67 + output = [] 1.68 + current = None 1.69 + length = 0 1.70 + 1.71 + for byte in data: 1.72 + 1.73 + if current != byte: 1.74 + if length > 0: 1.75 + output.append(current + chr(length)) 1.76 + current = byte 1.77 + length = 1 1.78 + else: 1.79 + length += 1 1.80 + if length == 255: 1.81 + output.append(current + chr(length)) 1.82 + length = 0 1.83 + 1.84 + if length > 0: 1.85 + output.append(current + chr(length)) 1.86 + 1.87 + return "".join(output) 1.88 1.89 def read_sprites(sprites): 1.90
