]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/commitdiff
Bug fixes and almost completely correct
authorMelissa LeBlanc-Williams <melissa@adafruit.com>
Sat, 30 Sep 2023 20:58:21 +0000 (13:58 -0700)
committerMelissa LeBlanc-Williams <melissa@adafruit.com>
Sat, 30 Sep 2023 20:58:21 +0000 (13:58 -0700)
displayio/_colorconverter.py
displayio/_displaycore.py
displayio/_ondiskbitmap.py

index 4f50417f103056cafca970514ecfe53414a9618a..69c1f49d9eb9648197f9e47c19f21df4e7736876 100644 (file)
@@ -85,7 +85,7 @@ class ColorConverter:
         red8 = color_rgb888 >> 16
         grn8 = (color_rgb888 >> 8) & 0xFF
         blu8 = color_rgb888 & 0xFF
-        return (red8 * 19 + grn8 * 182 + blu8 + 54) // 255
+        return (red8 * 19 + grn8 * 182 + blu8 * 54) // 255
 
     @staticmethod
     def _compute_chroma(color_rgb888: int):
@@ -144,9 +144,7 @@ class ColorConverter:
         return 0x0  # Black
 
     @staticmethod
-    def _compute_tricolor(
-        colorspace: ColorspaceStruct, pixel_hue: int, color: int
-    ) -> int:
+    def _compute_tricolor(colorspace: ColorspaceStruct, pixel_hue: int) -> int:
         hue_diff = colorspace.tricolor_hue - pixel_hue
         if -10 <= hue_diff <= 10 or hue_diff <= -220 or hue_diff >= 220:
             if colorspace.grayscale:
@@ -169,7 +167,7 @@ class ColorConverter:
         else:
             raise ValueError("Color must be an integer or 3 or 4 value tuple")
 
-        input_pixel = InputPixelStruct(color)
+        input_pixel = InputPixelStruct(pixel=color)
         output_pixel = OutputPixelStruct()
 
         self._convert(self._output_colorspace, input_pixel, output_pixel)
@@ -251,9 +249,15 @@ class ColorConverter:
         # pylint: disable=too-many-return-statements, too-many-branches, too-many-statements
         pixel = input_pixel.pixel
         if dither:
-            rand_red = ColorConverter._dither_noise_2(input_pixel.x, input_pixel.y)
-            rand_grn = ColorConverter._dither_noise_2(input_pixel.x + 33, input_pixel.y)
-            rand_blu = ColorConverter._dither_noise_2(input_pixel.x, input_pixel.y + 33)
+            rand_red = ColorConverter._dither_noise_2(
+                input_pixel.tile_x, input_pixel.tile_y
+            )
+            rand_grn = ColorConverter._dither_noise_2(
+                input_pixel.tile_x + 33, input_pixel.tile_y
+            )
+            rand_blu = ColorConverter._dither_noise_2(
+                input_pixel.tile_x, input_pixel.tile_y + 33
+            )
 
             red8 = pixel >> 16
             grn8 = (pixel >> 8) & 0xFF
@@ -287,9 +291,7 @@ class ColorConverter:
                 output_color.opaque = True
                 return
             pixel_hue = ColorConverter._compute_hue(pixel)
-            output_color.pixel = ColorConverter._compute_tricolor(
-                colorspace, pixel_hue, output_color.pixel
-            )
+            output_color.pixel = ColorConverter._compute_tricolor(colorspace, pixel_hue)
             return
         if colorspace.grayscale and colorspace.depth <= 8:
             bitmask = (1 << colorspace.depth) - 1
index 9e1ba4bdfb8bdad015ac7ecd5c4e06b68ea9a9cd..207f37a058dcb30584cceba469289e27d28fc559 100644 (file)
@@ -283,7 +283,6 @@ class _DisplayCore:
             else:
                 region_y1 //= pixels_per_byte * self.colorspace.bytes_per_cell
                 region_y2 //= pixels_per_byte * self.colorspace.bytes_per_cell
-        print(region_y2)
         region_x2 -= 1
         region_y2 -= 1
 
index 4a638a9b6eb6060e1543d10916e8b3b4b63e82ea..67c7e08289cfa8156a3ceecd97b9299a99940423 100644 (file)
@@ -234,27 +234,30 @@ class OnDiskBitmap:
 
         self._file.seek(location)
 
-        pixel_data = memoryview(self._file.read(4)).cast(
-            "I"
-        )  # cast as unsigned 32-bit int
-        pixel_data = pixel_data[0]  # We only need a single 32-bit uint
-        if bytes_per_pixel == 1:
-            offset = (x % pixels_per_byte) * self._bits_per_pixel
-            mask = (1 << self._bits_per_pixel) - 1
-            return (pixel_data >> ((8 - self._bits_per_pixel) - offset)) & mask
-        if bytes_per_pixel == 2:
-            if self._g_bitmask == 0x07E0:  # 565
-                red = (pixel_data & self._r_bitmask) >> 11
-                green = (pixel_data & self._g_bitmask) >> 5
-                blue = pixel_data & self._b_bitmask
-            else:  # 555
-                red = (pixel_data & self._r_bitmask) >> 10
-                green = (pixel_data & self._g_bitmask) >> 4
-                blue = pixel_data & self._b_bitmask
-            return red << 19 | green << 10 | blue << 3
-        if bytes_per_pixel == 4 and self._bitfield_compressed:
-            return pixel_data & 0x00FFFFFF
-        return pixel_data
+        pixel_data = self._file.read(bytes_per_pixel)
+        if len(pixel_data) == bytes_per_pixel:
+            if bytes_per_pixel == 1:
+                offset = (x % pixels_per_byte) * self._bits_per_pixel
+                mask = (1 << self._bits_per_pixel) - 1
+                return (pixel_data[0] >> ((8 - self._bits_per_pixel) - offset)) & mask
+            if bytes_per_pixel == 2:
+                pixel_data = pixel_data[0] | pixel_data[1] << 8
+                if self._g_bitmask == 0x07E0:  # 565
+                    red = (pixel_data & self._r_bitmask) >> 11
+                    green = (pixel_data & self._g_bitmask) >> 5
+                    blue = pixel_data & self._b_bitmask
+                else:  # 555
+                    red = (pixel_data & self._r_bitmask) >> 10
+                    green = (pixel_data & self._g_bitmask) >> 4
+                    blue = pixel_data & self._b_bitmask
+                return red << 19 | green << 10 | blue << 3
+            if bytes_per_pixel == 4 and self._bitfield_compressed:
+                return pixel_data[0] | pixel_data[1] << 8 | pixel_data[2] << 16
+            pixel = pixel_data[0] | pixel_data[1] << 8 | pixel_data[2] << 16
+            if bytes_per_pixel == 4:
+                pixel |= pixel_data[3] << 24
+            return pixel
+        return 0
 
     def _finish_refresh(self) -> None:
         pass