8bitlenser/lenser/c16/convert/mono.py
2026-07-03 19:35:35 -07:00

35 lines
1.3 KiB
Python

"""C16 TED monochrome: 320x200, restricted to the TED's neutral grey ramp (8
luminance levels of hue 1, plus black) for smooth greyscale. ``--mono-base``
tints it toward one hue (black -> hue -> white). Two greys per 8x8 cell, packed
into the same TED hires layout as the colour mode."""
from __future__ import annotations
import numpy as np
from ...convert import base
from .. import palette as tedpal
from . import hires
WIDTH, HEIGHT = hires.WIDTH, hires.HEIGHT
CELL_W, CELL_H = hires.CELL_W, hires.CELL_H
PIXEL_ASPECT = hires.PIXEL_ASPECT
def convert(img_rgb, palette_name="ted", dither_mode="floyd",
intensive=False, base_color=None):
plab = tedpal.palette_lab()
prgb = tedpal.get_palette().astype(np.uint8)
ramp = base.luminance_ramp(plab, tedpal.GREYS, base_color)
idx, sets, rows, cols, err = base.mono_render(
img_rgb, plab, ramp, WIDTH, HEIGHT, CELL_W, CELL_H, dither_mode, n_free=2)
bitmap, attr, ch = hires._encode(idx, sets, rows, cols)
payload = bytes(bitmap) + bytes(attr) + bytes(ch)
return base.Conversion(
mode="mono", width=WIDTH, height=HEIGHT, pixel_aspect=PIXEL_ASPECT,
index_image=idx.astype(np.uint16), data=payload, viewer="c16",
preview_rgb=prgb[idx], error=err,
meta={"palette": "ted", "dither": dither_mode, "border": 0},
)