]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blobdiff - displayio/bitmap.py
Add pre-commit support.
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / bitmap.py
index 1bc90aa52331f478f36430b281ed060ab8ee0e7a..2f67937074cf3df11c37ea8dac3dff23cf8c4488 100644 (file)
@@ -1,24 +1,6 @@
-# The MIT License (MIT)
+# SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
 #
-# Copyright (c) 2020 Melissa LeBlanc-Williams for Adafruit Industries
-#
-# 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.
+# SPDX-License-Identifier: MIT
 
 """
 `displayio.bitmap`
@@ -36,6 +18,7 @@ displayio for Blinka
 """
 
 from recordclass import recordclass
+from PIL import Image
 
 __version__ = "0.0.0-auto.0"
 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
@@ -52,8 +35,8 @@ class Bitmap:
         share the underlying Bitmap. value_count is used to minimize the memory used to store
         the Bitmap.
         """
-        self._width = width
-        self._height = height
+        self._bmp_width = width
+        self._bmp_height = height
         self._read_only = False
 
         if value_count < 0:
@@ -75,7 +58,7 @@ class Bitmap:
         ):
             raise NotImplementedError("Invalid bits per value")
 
-        self._data = (width * height) * [0]
+        self._image = Image.new("P", (width, height), 0)
         self._dirty_area = Rectangle(0, 0, width, height)
 
     def __getitem__(self, index):
@@ -84,10 +67,16 @@ class Bitmap:
         an x,y tuple or an int equal to `y * width + x`.
         """
         if isinstance(index, (tuple, list)):
-            index = (index[1] * self._width) + index[0]
-        if index >= len(self._data):
+            x, y = index
+        elif isinstance(index, int):
+            x = index % self._bmp_width
+            y = index // self._bmp_width
+        else:
+            raise TypeError("Index is not an int, list, or tuple")
+
+        if x > self._image.width or y > self._image.height:
             raise ValueError("Index {} is out of range".format(index))
-        return self._data[index]
+        return self._image.getpixel((x, y))
 
     def __setitem__(self, index, value):
         """
@@ -99,11 +88,11 @@ class Bitmap:
         if isinstance(index, (tuple, list)):
             x = index[0]
             y = index[1]
-            index = y * self._width + x
+            index = y * self._bmp_width + x
         elif isinstance(index, int):
-            x = index % self._width
-            y = index // self._width
-        self._data[index] = value
+            x = index % self._bmp_width
+            y = index // self._bmp_width
+        self._image.putpixel((x, y), value)
         if self._dirty_area.x1 == self._dirty_area.x2:
             self._dirty_area.x1 = x
             self._dirty_area.x2 = x + 1
@@ -125,15 +114,15 @@ class Bitmap:
 
     def fill(self, value):
         """Fills the bitmap with the supplied palette index value."""
-        self._data = (self._width * self._height) * [value]
-        self._dirty_area = Rectangle(0, 0, self._width, self._height)
+        self._image = Image.new("P", (self._bmp_width, self._bmp_height), value)
+        self._dirty_area = Rectangle(0, 0, self._bmp_width, self._bmp_height)
 
     @property
     def width(self):
         """Width of the bitmap. (read only)"""
-        return self._width
+        return self._bmp_width
 
     @property
     def height(self):
         """Height of the bitmap. (read only)"""
-        return self._height
+        return self._bmp_height