ii-pix/ntsc_colours.py
kris ad9515dcf2 Implement NTSC emulation, using an 8 pixel window for chroma signal.
Use this to precompute a new ntsc palette with 256 entries (though
only 84 unique colours) that are available by appropriate pixel
sequences.  Unfortunately the precomputed distance matrix for this
palette is 4GB!

Optimize the precomputation to be less memory hungry, while also
making efficient use of the mmapped output file.

Add support for dithering images using this 8-bit palette depth,
i.e. to optimize for NTSC rendering.  This often gives better image
quality since more colours are available, especially when modulating
areas of similar colour.

Fix 140 pixel dithering and render the output including NTSC fringing
instead of the unrealistic 140px output that doesn't include it.

Add support for rendering output image using any target palette, which
is useful e.g. for comparing how an 8-pixel NTSC rendered image will
be displayed on an emulator using 4-pixel ntsc emulation (there is
usually some colour bias, because the 8 pixel chroma blending tends to
average away colours).

Switch the output binary format to write AUX memory first, which
matches the image format of other utilities.
2021-02-14 23:34:25 +00:00

42 lines
1.2 KiB
Python

"""Precomputes all possible colours available via NTSC emulation."""
import colour
import numpy as np
from PIL import Image
import screen
def main():
s = screen.DHGR560Screen(palette=None)
bitmap = np.zeros((1, 8), dtype=np.bool)
colours = {}
unique = set()
print("SRGB = {")
# For each sequence of 8 pixels, compute the RGB colour of the right-most
# pixel, using NTSC emulation.
for bits in range(256):
for i in range(8):
bitmap[0, i] = bits & (1 << i)
ntsc = s.bitmap_to_ntsc(bitmap)
last_colour = ntsc[0, -1, :]
colours[bits] = last_colour
unique.add(tuple(last_colour))
print("%d: np.array((%d, %d, %d))," % (
bits, last_colour[0], last_colour[1], last_colour[2]))
print("}")
print("# %d unique colours" % len(unique))
# Show spectrum of available colours sorted by HSV hue value
im = np.zeros((128, 256 * 16, 3), dtype=np.uint8)
for x, hsv in enumerate(sorted([tuple(colour.RGB_to_HSV(c / 256)) for c in
colours.values()])):
im[0:128, x * 16: (x + 1) * 16, :] = colour.HSV_to_RGB(hsv) * 256
Image.fromarray(im).show()
if __name__ == "__main__":
main()