41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
"""CoCo PMODE 4: 256x192, 1 bit/pixel, black & white (buff on black).
|
|
|
|
The CoCo's high-resolution monochrome mode -- 256x192 is exactly 4:3, so square
|
|
pixels. Tone is carried by dithering, like Apple HGR mono.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
|
|
from ... import dither
|
|
from ...convert.base import Conversion, perceptual_error
|
|
from .. import palette as cpal
|
|
|
|
WIDTH, HEIGHT = 256, 192
|
|
PIXEL_ASPECT = 1.0
|
|
|
|
|
|
def convert(img_rgb, palette_name="mc6847", dither_mode="floyd",
|
|
intensive=False, base_color=None):
|
|
from ...palette import srgb_to_lab
|
|
plab = cpal.mono_lab()
|
|
L = srgb_to_lab(img_rgb)[..., 0]
|
|
img_mono = np.zeros((HEIGHT, WIDTH, 3))
|
|
img_mono[..., 0] = L
|
|
plab_mono = np.zeros((2, 3))
|
|
plab_mono[:, 0] = plab[:, 0]
|
|
|
|
allowed = np.tile(np.array([0, 1]), (HEIGHT, WIDTH, 1))
|
|
idx = dither.quantize(img_mono, allowed, plab_mono, dither_mode).astype(np.uint8)
|
|
|
|
data = cpal.pack_pmode4(idx) # 6144-byte video buffer
|
|
preview = cpal.MONO.astype(np.uint8)[idx]
|
|
|
|
return Conversion(
|
|
mode="pmode4", width=WIDTH, height=HEIGHT, pixel_aspect=PIXEL_ASPECT,
|
|
index_image=idx.astype(np.uint16), data=data, data_addr=0,
|
|
viewer="pmode4", preview_rgb=preview,
|
|
error=perceptual_error(idx, img_mono, plab_mono),
|
|
meta={"palette": "mc6847", "dither": dither_mode, "vdg": 0xF8},
|
|
)
|