-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_acorn_graphics.py
executable file
·185 lines (157 loc) · 5.98 KB
/
render_acorn_graphics.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# render_acorn_graphics.py
#
# The Python script in this file renders a graphical representation of files
# containing Acorn screen memory.
#
# Copyright (C) 2022-2024 Dominic Ford <https://dcford.org.uk/>
#
# This code 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; either version 3 of the License, or (at your option) any later
# version.
#
# You should have received a copy of the GNU General Public License along with
# this file; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA
# ----------------------------------------------------------------------------
"""
Render a graphical representation of files containing Acorn screen memory.
"""
import argparse
import logging
import sys
from math import ceil, floor
from PIL import Image
from typing import Dict, Iterable, List, Tuple
# List of the BBC Micro's screen modes
acorn_screen_modes: Dict[int, Dict] = {
0: {
'width': 640,
'bit_depth': 1
},
1: {
'width': 320,
'bit_depth': 2
},
2: {
'width': 160,
'bit_depth': 4
},
4: {
'width': 320,
'bit_depth': 1
},
5: {
'width': 160,
'bit_depth': 2
}
}
# Color palettes to use when drawing graphics in each mode
palettes: Dict[int, List[Tuple[int]]] = {
1: [(0, 0, 0), (255, 255, 255)],
2: [(0, 0, 0), (255, 0, 0), (255, 255, 0), (255, 255, 255)],
4: [(0, 0, 0), (255, 0, 0), (0, 255, 0), (255, 255, 0), (0, 0, 255), (255, 0, 255), (0, 255, 255), (255, 255, 255),
(0, 0, 0), (255, 0, 0), (0, 255, 0), (255, 255, 0), (0, 0, 255), (255, 0, 255), (0, 255, 255), (255, 255, 255)]
}
def render_acorn_graphics(filename: str, output: str, screen_mode: int, offset: int = 0):
"""
Render Acorn screen memory
:param filename:
The filename of the binary input file
:param output:
The filename of the graphical output
:param screen_mode:
The Acorn screen mode to simulate
:param offset:
The offset within the binary file to start rendering
:return:
None
"""
assert screen_mode in acorn_screen_modes
# Read input file
with open(filename, "rb") as f:
input_bytes = f.read()
# Produce graphical output
create_image_from_bytes(byte_list=input_bytes, output=output, screen_mode=screen_mode, offset=offset)
def create_image_from_bytes(byte_list: Iterable, output: str, screen_mode: int, offset: int = 0):
"""
Create a listing of a input file
:param byte_list:
The bytes of the input file
:param output:
The filename of the graphical output
:param screen_mode:
The Acorn screen mode to simulate
:param offset:
The offset within the binary file to start rendering
:return:
None
"""
byte_list = byte_list[offset:]
assert screen_mode in acorn_screen_modes
mode_info = acorn_screen_modes[screen_mode]
palette = palettes[mode_info['bit_depth']]
y_scaling = 2
output_size_x = 640
bytes_per_line = mode_info['width'] * mode_info['bit_depth']
line_count = ceil(len(byte_list) / bytes_per_line)
pixel_width = int(output_size_x / mode_info['width'])
output_size_y = line_count * 8 * y_scaling
# Create image
map_image = Image.new('RGB', (output_size_x, output_size_y), (0, 0, 0))
# Loop over input bytes
for pos, byte in enumerate(byte_list):
pos_y = floor(pos / bytes_per_line) * 8 + (pos % 8)
pos_x_row = floor((pos % bytes_per_line) / 8)
pixels_per_byte = int(8 / mode_info['bit_depth'])
pos_x = int(pos_x_row * pixels_per_byte)
accumulators = [0] * pixels_per_byte
input_bits = int(byte)
for bit_number in range(8):
bit_value = input_bits % 2
input_bits = floor(input_bits / 2)
accumulators[bit_number % pixels_per_byte] += bit_value * pow(2, floor(bit_number / pixels_per_byte))
for x_pixel in range(pixels_per_byte):
for y_offset in range(y_scaling):
for x_offset in range(pixel_width):
map_image.putpixel(((pos_x + x_pixel) * pixel_width + x_offset,
pos_y * y_scaling + y_offset),
palette[accumulators[pixels_per_byte - 1 - x_pixel]])
# Save the final image
map_image.save(output, "PNG")
# Do it right away if we're run as a script
if __name__ == "__main__":
# Set up a logging object
logging.basicConfig(level=logging.INFO,
stream=sys.stdout,
format='[%(asctime)s] %(levelname)s:%(filename)s:%(message)s',
datefmt='%d/%m/%Y %H:%M:%S')
logger = logging.getLogger(__name__)
logger.debug(__doc__.strip())
# Read input parameters
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--input',
required=True,
type=str,
dest="input",
help="The input binary file")
parser.add_argument('--output',
required=True,
type=str,
dest="output",
help="The output graphical file")
parser.add_argument('--mode',
required=True,
type=int,
dest="mode",
help="The Acorn screen mode to simulate")
parser.add_argument('--offset',
default=0,
type=int,
dest="offset",
help="The offset position within the file to start rendering")
args = parser.parse_args()
# Render graphics
render_acorn_graphics(filename=args.input, output=args.output, screen_mode=args.mode, offset=args.offset)