Support decoding arbitrary number of dots as nominal colour sequence.

This is needed for HGR support.
This commit is contained in:
kris 2019-07-07 21:13:28 +01:00
parent fc4a63fffe
commit 16c4faa66d
2 changed files with 21 additions and 29 deletions

View File

@ -82,41 +82,32 @@ class DHGRColours(NominalColours):
WHITE = 0b1111 WHITE = 0b1111
# @functools.lru_cache(None)
# def int28_to_nominal_colour_pixels2(int28):
# return tuple(
# HGRColours(
# (int28 & (0b1111 << (4 * i))) >> (4 * i)) for i in range(7)
# )
@functools.lru_cache(None) @functools.lru_cache(None)
def int34_to_nominal_colour_pixels( def dots_to_nominal_colour_pixels(
int34: int, num_bits: int,
dots: int,
colours: Type[NominalColours], colours: Type[NominalColours],
init_phase: int = 1 # Such that phase = 0 at start of 28-bit body init_phase: int = 1 # Such that phase = 0 at start of body
) -> Tuple[NominalColours]: ) -> Tuple[NominalColours]:
"""Produce sequence of 31 nominal colour pixels via sliding 4-bit window. """Sequence of num_bits nominal colour pixels via sliding 4-bit window.
Includes the 3-bit header that represents the trailing 3 bits of the Includes the 3-bit header that represents the trailing 3 bits of the
previous 28-bit tuple. i.e. storing a byte in aux even columns will also previous tuple body. i.e. storing a byte in aux even columns will also
influence the colours of the previous main odd column. influence the colours of the previous main odd column.
This naively models the NTSC colour artifacting. This naively models the NTSC colour artifacting.
TODO: Use a more careful colour composition model to produce effective TODO: Use a more careful analogue colour composition model to produce
pixel colours. effective pixel colours.
TODO: DHGR vs HGR colour differences can be modeled by changing init_phase TODO: DHGR vs HGR colour differences can be modeled by changing init_phase
""" """
res = [] res = []
shifted = int34 shifted = dots
phase = init_phase phase = init_phase
# Omit trailing 3 bits which are only there to provide a trailer for for i in range(num_bits):
# bits 28..31
for i in range(31):
colour = rol(shifted & 0b1111, phase) colour = rol(shifted & 0b1111, phase)
res.append(colours(colour)) res.append(colours(colour))
@ -129,12 +120,13 @@ def int34_to_nominal_colour_pixels(
@functools.lru_cache(None) @functools.lru_cache(None)
def int34_to_nominal_colour_pixel_values( def dots_to_nominal_colour_pixel_values(
int34: int, num_bits: int,
dots: int,
colours: Type[NominalColours], colours: Type[NominalColours],
init_phase: int = 1 # Such that phase = 0 at start of 28-bit body init_phase: int = 1 # Such that phase = 0 at start of body
) -> Tuple[int]: ) -> Tuple[int]:
return tuple(p.value for p in int34_to_nominal_colour_pixels( return tuple(p.value for p in dots_to_nominal_colour_pixels(
int34, colours, init_phase num_bits, dots, colours, init_phase
)) ))

View File

@ -7,7 +7,7 @@ HGRColours = colours.HGRColours
class TestColours(unittest.TestCase): class TestColours(unittest.TestCase):
def test_int34_to_pixels(self): def test_dots_to_pixels(self):
self.assertEqual( self.assertEqual(
( (
HGRColours.BLACK, HGRColours.BLACK,
@ -42,8 +42,8 @@ class TestColours(unittest.TestCase):
HGRColours.BLACK, HGRColours.BLACK,
HGRColours.BLACK HGRColours.BLACK
), ),
colours.int34_to_nominal_colour_pixels( colours.dots_to_nominal_colour_pixels(
0b00000000000000000000111000000000, HGRColours, init_phase=0 31, 0b00000000000000000000111000000000, HGRColours, init_phase=0
) )
) )
@ -81,8 +81,8 @@ class TestColours(unittest.TestCase):
HGRColours.BLACK, HGRColours.BLACK,
HGRColours.BLACK HGRColours.BLACK
), ),
colours.int34_to_nominal_colour_pixels( colours.dots_to_nominal_colour_pixels(
0b0000111100001111000011110000, HGRColours, init_phase=0 31, 0b0000111100001111000011110000, HGRColours, init_phase=0
) )
) )