Simplify image_to_rgb and remove the need for DOTS and DOTS_TO_INDEX

This commit is contained in:
kris 2021-11-02 15:14:22 +00:00
parent 809b975e6e
commit 75a3c6bc48
2 changed files with 28 additions and 18 deletions

View File

@ -7,11 +7,10 @@ import palette_ntsc
class Palette:
RGB = {}
SRGB = None
RGB = {}
CAM16UCS = {}
DOTS = {}
DOTS_TO_INDEX = {}
# DOTS_TO_INDEX = {}
# How many successive screen pixels are used to compute output pixel
# palette index.
@ -19,6 +18,7 @@ class Palette:
def __init__(self):
self.RGB = {}
# XXX RGB and CAM16UCS should be indexed by (pixels_nbit, phase)
for k, v in self.SRGB.items():
self.RGB[k] = (np.clip(image.srgb_to_linear_array(v / 255), 0.0,
1.0) * 255).astype(np.uint8)
@ -26,16 +26,25 @@ class Palette:
self.CAM16UCS[k] = colour.convert(
v / 255, "sRGB", "CAM16UCS").astype(np.float32)
# Maps palette values to screen dots. Note that these are the same as
# the binary index values in reverse order.
for i in range(1 << self.PALETTE_DEPTH):
self.DOTS[i] = tuple(
bool(i & (1 << j)) for j in range(self.PALETTE_DEPTH))
# # Maps palette values to screen dots. Note that these are the same as
# # the binary index values in reverse order.
# _DOTS = {}
# for i in range(1 << self.PALETTE_DEPTH):
# _DOTS[i] = tuple(
# bool(i & (1 << j)) for j in range(self.PALETTE_DEPTH))
#
# # Reverse mapping from screen dots to palette index.
# self.DOTS_TO_INDEX = {}
# for k, v in _DOTS.items():
# self.DOTS_TO_INDEX[v] = k
# Reverse mapping from screen dots to palette index.
self.DOTS_TO_INDEX = {}
for k, v in self.DOTS.items():
self.DOTS_TO_INDEX[v] = k
def pixels_to_idx(self, pixels: np.array) -> int:
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)
np.flip(pixels, axis=0)
)[0]
class ToHgrPalette(Palette):

View File

@ -79,14 +79,15 @@ class DHGR560NTSCScreen(DHGRScreen):
window indexed by x % 4, which gives the index into our 256-colour RGB
palette.
"""
image_rgb = np.empty((self.Y_RES, self.X_RES, 3),
dtype=np.uint8)
image_rgb = np.empty((self.Y_RES, self.X_RES, 3), dtype=np.uint8)
for y in range(self.Y_RES):
pixel = [False, False, False, False, False, False, False, False]
pixels = [False] * self.palette.PALETTE_DEPTH
for x in range(self.X_RES):
pixel = pixel[1:] + [bitmap[y, x]]
dots = self.palette.DOTS_TO_INDEX[tuple(pixel)]
image_rgb[y, x, :] = self.palette.RGB[dots, x % 4]
pixels = pixels[1:] + [bitmap[y, x]]
# dots = self.palette.DOTS_TO_INDEX[tuple(pixel)]
image_rgb[y, x, :] = self.palette.RGB[
self.palette.pixels_to_idx(
np.array(pixels, dtype=bool)), x % 4]
return image_rgb
@staticmethod