]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/_bitmap.py
Update to pyproject.toml
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / _bitmap.py
1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4
5 """
6 `displayio.bitmap`
7 ================================================================================
8
9 displayio for Blinka
10
11 **Software and Dependencies:**
12
13 * Adafruit Blinka:
14   https://github.com/adafruit/Adafruit_Blinka/releases
15
16 * Author(s): Melissa LeBlanc-Williams
17
18 """
19
20 from __future__ import annotations
21 from typing import Union, Tuple
22 from PIL import Image
23 from ._structs import RectangleStruct
24
25 __version__ = "0.0.0+auto.0"
26 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
27
28
29 class Bitmap:
30     """Stores values of a certain size in a 2D array"""
31
32     def __init__(self, width: int, height: int, value_count: int):
33         """Create a Bitmap object with the given fixed size. Each pixel stores a value that is
34         used to index into a corresponding palette. This enables differently colored sprites to
35         share the underlying Bitmap. value_count is used to minimize the memory used to store
36         the Bitmap.
37         """
38         self._bmp_width = width
39         self._bmp_height = height
40         self._read_only = False
41
42         if value_count < 0:
43             raise ValueError("value_count must be > 0")
44
45         bits = 1
46         while (value_count - 1) >> bits:
47             if bits < 8:
48                 bits = bits << 1
49             else:
50                 bits += 8
51
52         self._bits_per_value = bits
53
54         if (
55             self._bits_per_value > 8
56             and self._bits_per_value != 16
57             and self._bits_per_value != 32
58         ):
59             raise NotImplementedError("Invalid bits per value")
60
61         self._image = Image.new("P", (width, height), 0)
62         self._dirty_area = RectangleStruct(0, 0, width, height)
63
64     def __getitem__(self, index: Union[Tuple[int, int], int]) -> int:
65         """
66         Returns the value at the given index. The index can either be
67         an x,y tuple or an int equal to `y * width + x`.
68         """
69         if isinstance(index, (tuple, list)):
70             x, y = index
71         elif isinstance(index, int):
72             x = index % self._bmp_width
73             y = index // self._bmp_width
74         else:
75             raise TypeError("Index is not an int, list, or tuple")
76
77         if x > self._image.width or y > self._image.height:
78             raise ValueError(f"Index {index} is out of range")
79         return self._image.getpixel((x, y))
80
81     def __setitem__(self, index: Union[Tuple[int, int], int], value: int) -> None:
82         """
83         Sets the value at the given index. The index can either be
84         an x,y tuple or an int equal to `y * width + x`.
85         """
86         if self._read_only:
87             raise RuntimeError("Read-only object")
88         if isinstance(index, (tuple, list)):
89             x = index[0]
90             y = index[1]
91             index = y * self._bmp_width + x
92         elif isinstance(index, int):
93             x = index % self._bmp_width
94             y = index // self._bmp_width
95         self._image.putpixel((x, y), value)
96         if self._dirty_area.x1 == self._dirty_area.x2:
97             self._dirty_area.x1 = x
98             self._dirty_area.x2 = x + 1
99             self._dirty_area.y1 = y
100             self._dirty_area.y2 = y + 1
101         else:
102             if x < self._dirty_area.x1:
103                 self._dirty_area.x1 = x
104             elif x >= self._dirty_area.x2:
105                 self._dirty_area.x2 = x + 1
106             if y < self._dirty_area.y1:
107                 self._dirty_area.y1 = y
108             elif y >= self._dirty_area.y2:
109                 self._dirty_area.y2 = y + 1
110
111     def _finish_refresh(self):
112         self._dirty_area.x1 = 0
113         self._dirty_area.x2 = 0
114
115     def fill(self, value: int) -> None:
116         """Fills the bitmap with the supplied palette index value."""
117         self._image = Image.new("P", (self._bmp_width, self._bmp_height), value)
118         self._dirty_area = RectangleStruct(0, 0, self._bmp_width, self._bmp_height)
119
120     def blit(
121         self,
122         x: int,
123         y: int,
124         source_bitmap: Bitmap,
125         *,
126         x1: int,
127         y1: int,
128         x2: int,
129         y2: int,
130         skip_index: int,
131     ) -> None:
132         # pylint: disable=unnecessary-pass, invalid-name
133         """Inserts the source_bitmap region defined by rectangular boundaries"""
134         pass
135
136     def dirty(self, x1: int = 0, y1: int = 0, x2: int = -1, y2: int = -1) -> None:
137         # pylint: disable=unnecessary-pass, invalid-name
138         """Inform displayio of bitmap updates done via the buffer protocol."""
139         pass
140
141     @property
142     def width(self) -> int:
143         """Width of the bitmap. (read only)"""
144         return self._bmp_width
145
146     @property
147     def height(self) -> int:
148         """Height of the bitmap. (read only)"""
149         return self._bmp_height