From 9dbf41373313ca6b4e2f952d0152191cc75b8340 Mon Sep 17 00:00:00 2001 From: kris Date: Tue, 2 Nov 2021 15:45:20 +0000 Subject: [PATCH] Tidy --- image.py | 1 - palette.py | 24 ++++++++++++++++++++++-- screen.py | 8 ++++---- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/image.py b/image.py index 9f38c2b..41680ea 100644 --- a/image.py +++ b/image.py @@ -1,6 +1,5 @@ """Image transformation functions.""" -import colour import numpy as np from PIL import Image diff --git a/palette.py b/palette.py index 17d55a0..0f32de3 100644 --- a/palette.py +++ b/palette.py @@ -25,11 +25,31 @@ class Palette: self.CAM16UCS[k] = colour.convert( v / 255, "sRGB", "CAM16UCS").astype(np.float32) - def pixels_to_idx(self, pixels: np.array) -> int: + def _pixel_phase_shifts(self, phase_0_rgb): + rgb_phases = {} + for pixels, rgb in phase_0_rgb.items(): + rgb_phases[pixels, 0] = rgb + for phase in range(1, 4): + msb = pixels & (1 << (self.PALETTE_DEPTH - 1)) + pixels <<= 1 | (msb >> (self.PALETTE_DEPTH - 1)) + rgb_phases[pixels, phase] = rgb + return rgb_phases + + def bitmap_to_idx(self, pixels: np.array) -> int: + """Converts a bitmap of pixels into integer representation. + + Args: + pixels: 1-D array of booleans, representing a window of pixels from + L to R. Must be of size <= 8 + + Returns: + 8-bit integer representation of pixels, suitable for use as an + index into palette arrays + """ return np.packbits( # numpy uses big-endian representation which is the opposite # order to screen representation (i.e. LSB is the left-most - # screen pixel) + # screen pixel), so we need to flip the order np.flip(pixels, axis=0) )[0] diff --git a/screen.py b/screen.py index 58322d5..b029b19 100644 --- a/screen.py +++ b/screen.py @@ -59,13 +59,13 @@ class DHGRScreen: """ image_rgb = np.empty((self.Y_RES, self.X_RES, 3), dtype=np.uint8) for y in range(self.Y_RES): - pixels = [False] * self.palette.PALETTE_DEPTH + bitmap_window = [False] * self.palette.PALETTE_DEPTH for x in range(self.X_RES): # Maintain a sliding window of pixels of width PALETTE_DEPTH - pixels = pixels[1:] + [bitmap[y, x]] + bitmap_window = bitmap_window[1:] + [bitmap[y, x]] image_rgb[y, x, :] = self.palette.RGB[ - self.palette.pixels_to_idx( - np.array(pixels, dtype=bool)), x % 4] + self.palette.bitmap_to_idx( + np.array(bitmap_window, dtype=bool)), x % 4] return image_rgb @staticmethod