This commit is contained in:
kris 2021-11-02 15:45:20 +00:00
parent e84dfb59f9
commit 9dbf413733
3 changed files with 26 additions and 7 deletions

View File

@ -1,6 +1,5 @@
"""Image transformation functions."""
import colour
import numpy as np
from PIL import Image

View File

@ -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]

View File

@ -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