forked from adafruit/Adafruit_CircuitPython_ImageLoad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplayio_shared_bindings.py
More file actions
213 lines (187 loc) · 7.83 KB
/
displayio_shared_bindings.py
File metadata and controls
213 lines (187 loc) · 7.83 KB
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
# The MIT License (MIT)
#
# Copyright (c) 2019 Matt Land
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_imageload.tests.displayio_shared_bindings`
====================================================
The classes in this file are designed to emulate Circuitpython's displayio classes
for Bitmap and Palette. These mimic classes should have the same methods and interface as the real
interface, but with extra validation checks, warnings, and messages to facilitate debugging.
Code that can be run successfully against these classes will have a good chance of
working correctly on hardware running Circuitpython, but without needing to upload code to a board
after each attempt.
* Author(s): Matt Land
"""
from typing import Union
class Bitmap_C_Interface:
"""
A class to simulate the displayio.Bitmap class for testing, based on
https://circuitpython.readthedocs.io/en/latest/shared-bindings/displayio/Bitmap.html
In case of discrepancy, the C implementation takes precedence.
"""
def __init__(self, width: int, height: int, colors: int) -> None:
self.width = width
self.height = height
self.colors = colors
self.data = {}
bits = 1
while (colors - 1) >> bits:
if bits < 8:
bits = bits << 1
else:
bits += 8
self._bits_per_value = bits
def _abs_pos(self, width: int, height: int) -> int:
if height >= self.height:
raise ValueError("height > max")
if width >= self.width:
raise ValueError("width > max")
return width + (height * self.width)
def _decode(self, position: int) -> tuple:
return position % self.width, position // self.width
def __setitem__(self, key: Union[tuple, int], value: int) -> None:
"""
Set using x, y coordinates, or absolution position
bitmap[0] = 1
bitmap[2,1] = 5
"""
if isinstance(key, tuple):
# order is X, Y from the docs
# https://github.com/adafruit/circuitpython/blob/main/shared-bindings/displayio/Bitmap.c
self.__setitem__(self._abs_pos(key[0], key[1]), value)
return
if not isinstance(value, (int)):
raise RuntimeError(f"set value as int, not {type(value)}")
if value > 255:
raise ValueError(f"pixel value {value} too large")
if self.data.get(key):
raise ValueError(f"pixel {self._decode(key)}/{key} already set, cannot set again")
self.data[key] = value
def __getitem__(self, item: Union[tuple, int]) -> bytearray:
if isinstance(item, tuple):
return self.__getitem__(self._abs_pos(item[0], item[1]))
if item > self.height * self.width:
raise RuntimeError(f"get position out of range {item}")
try:
return self.data[item]
except KeyError as err:
raise RuntimeError(f"no data at {self._decode(item)} [{item}]") from err
def validate(self, detect_empty_image=True) -> None:
"""
method to to make sure all pixels allocated in the Bitmap
were set with a value
"""
seen_colors = set()
if not self.data:
raise ValueError("no rows were set / no data in memory")
for y in range(self.height):
for x in range(self.width):
try:
seen_colors.add(self[x, y])
except KeyError as err:
raise ValueError(f"missing data at {x},{y}") from err
if detect_empty_image and len(seen_colors) < 2:
raise ValueError(
"image detected as only one color. set detect_empty_image=False to ignore"
)
def __str__(self) -> str:
"""
method to dump the contents of the Bitmap to a terminal,
for debugging purposes
Example:
--------
bitmap = Bitmap(5, 4, 4)
... # assign bitmap values
print(str(bitmap))
"""
out = "\n"
for y in range(self.height):
for x in range(self.width):
data = self[x, y]
out += f"{data:>4}"
out += "\n"
return out
class Palette_C_Interface:
"""
A class to simulates the displayio.Palette class for testing, based on
https://circuitpython.readthedocs.io/en/latest/shared-bindings/displayio/Palette.html
In case of discrepancy, the C implementation takes precedence.
"""
def __init__(self, num_colors: int) -> None:
self.num_colors = num_colors
self.colors = {}
def __setitem__(self, key: int, value: Union[bytes, int, bytearray]) -> None:
"""
Set using zero indexed color value
palette = Palette(1)
palette[0] = 0xFFFFFF
"""
if key >= self.num_colors:
raise ValueError(
f"palette index {key} is greater than allowed by num_colors {self.num_colors}"
)
if not isinstance(value, (bytes, int, bytearray)):
raise ValueError(f"palette color should be bytes, not {type(value)}")
if isinstance(value, int) and value > 0xFFFFFF:
raise ValueError(f"palette color int {value} is too large")
if self.colors.get(key):
raise ValueError(f"palette color {key} was already set, should not reassign")
self.colors[key] = value
def __getitem__(self, item: int) -> Union[bytes, int, bytearray]:
"""
Warning: this method is not supported in the actual C interface.
It is provided here for debugging purposes.
"""
if item >= self.num_colors:
raise ValueError(f"palette index {item} should be less than {self.num_colors}")
if not self.colors.get(item):
raise ValueError(f"palette index {item} is not set")
return self.colors[item]
def validate(self):
"""
method to make sure all colors allocated in Palette were set to a value
"""
if not self.colors:
raise IndexError("no palette colors were set")
if len(self.colors) != self.num_colors:
raise IndexError(
f"palette was initialized for {self.num_colors} "
f"colors, but only {len(self.colors)} were inserted"
)
for i in range(self.num_colors):
try:
self.colors
except IndexError as err:
raise ValueError(f"missing color `{i}` in palette color list") from err
def __str__(self):
"""
method to dump the contents of the Palette to a terminal,
for debugging purposes
Example:
--------
palette = Palette(1)
palette[0] = 0xFFFFFF
print(str(palette))
"""
out = "\nPalette:\n"
for i, color in enumerate(self.colors):
out += f" [{i}] {color}\n"
return out