ii-pix/image.py
KrisKennaway 3aa29f2d2c
Add support for hi-res conversions (#11)
Hi-Res is essentially a more constrained version of Double Hi-Res, in which only about half of the 560 horizontal screen pixels can be independently addressed.

In particular an 8 bit byte in screen memory controls 14 or 15 screen pixels.  Bits 0-7 are doubled, and bit 8 shifts these 14 dots to the right if enabled.  In this case bit 7 of the previous byte is repeated a third time.

This means that we have to optimize all 8 bits at once and move forward in increments of 14 screen pixels.

There's also a timing difference that results in a phase shift of the NTSC colour signal, which means the mappings from dot patterns to effective colours are rotated.

Error diffusion seems to give best results if we only distribute about 2/3 of the quantization error according to the dither pattern.
2023-02-03 00:40:32 +00:00

52 lines
1.6 KiB
Python

"""Image transformation functions."""
import numpy as np
from PIL import Image
def srgb_to_linear_array(a: np.ndarray, gamma=2.4) -> np.ndarray:
return np.where(a <= 0.04045, a / 12.92, ((a + 0.055) / 1.055) ** gamma)
def linear_to_srgb_array(a: np.ndarray, gamma=2.4) -> np.ndarray:
return np.where(a <= 0.0031308, a * 12.92, 1.055 * a ** (1.0 / gamma) -
0.055)
def srgb_to_linear(im: np.ndarray, gamma=2.4) -> np.ndarray:
rgb_linear = srgb_to_linear_array(im / 255.0, gamma=gamma)
return (np.clip(rgb_linear, 0.0, 1.0) * 255).astype(np.float32)
def linear_to_srgb(im: np.ndarray, gamma=2.4) -> np.ndarray:
srgb = linear_to_srgb_array(im / 255.0, gamma=gamma)
return (np.clip(srgb, 0.0, 1.0) * 255).astype(np.float32)
def open(filename: str) -> np.ndarray:
im = Image.open(filename)
# TODO: convert to sRGB colour profile explicitly, in case it has some other
# profile already.
if im.mode != "RGB":
im = im.convert("RGB")
return im
def resize(
image: Image, x_res, y_res, gamma: float = 2.4,
srgb_output: bool = False) -> Image:
# Convert to linear RGB before rescaling so that colour interpolation is
# in linear space
linear = srgb_to_linear(np.asarray(image), gamma=gamma).astype(np.uint8)
res = Image.fromarray(linear).resize((x_res, y_res), Image.LANCZOS)
if srgb_output:
return Image.fromarray(
linear_to_srgb(np.array(res, dtype=np.float32), gamma=gamma).astype(
np.uint8))
else:
return res
def resize_mono(image: Image, x_res, y_res) -> Image:
return image.resize((x_res, y_res), Image.LANCZOS)