First public commit.
This commit is contained in:
parent
2a48f52979
commit
4bac9d83ed
288 changed files with 18417 additions and 1076 deletions
19
lenser/coco3/convert/__init__.py
Normal file
19
lenser/coco3/convert/__init__.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
"""Tandy CoCo 3 (GIME) conversion dispatch."""
|
||||
from __future__ import annotations
|
||||
|
||||
from ... import imageprep
|
||||
from . import gr16, gr4, mono
|
||||
|
||||
_MODULES = {"gr16": gr16, "gr4": gr4, "mono": mono}
|
||||
MODES = list(_MODULES.keys())
|
||||
|
||||
|
||||
def convert_image(path_or_img, mode="gr16", palette_name="gime",
|
||||
dither_mode="floyd", intensive=False, prep_opt=None,
|
||||
base_color=None):
|
||||
prep_opt = prep_opt or imageprep.PrepOptions()
|
||||
module = _MODULES.get(mode, gr16)
|
||||
img_rgb = imageprep.prepare(path_or_img, module.WIDTH, module.HEIGHT,
|
||||
module.PIXEL_ASPECT, prep_opt, border_rgb=(0, 0, 0))
|
||||
return module.convert(img_rgb, palette_name, dither_mode, intensive,
|
||||
base_color=base_color)
|
||||
85
lenser/coco3/convert/_common.py
Normal file
85
lenser/coco3/convert/_common.py
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
"""Shared CoCo 3 GIME encoder helpers: global palette selection + linear packing.
|
||||
|
||||
GIME native graphics modes have no per-cell colour limit: a flat palette of pens
|
||||
(16/4/2), each pen any of the 64 colours. Pick the best N-colour sub-palette,
|
||||
dither to it, then pack pens into the LINEAR 80-byte x 192-row screen.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
|
||||
from ... import dither, palette as c64pal
|
||||
from .. import palette as g
|
||||
|
||||
BYTES_PER_ROW, ROWS = 80, 192
|
||||
|
||||
|
||||
def choose_inks(img_lab, plab, n):
|
||||
"""Greedy forward selection of the ``n`` palette colours (0-63) minimising
|
||||
nearest-colour error over the image."""
|
||||
flat = img_lab.reshape(-1, 3)
|
||||
d = np.sum((flat[:, None, :] - plab[None, :, :]) ** 2, axis=-1)
|
||||
chosen, best = [], np.full(flat.shape[0], np.inf)
|
||||
for _ in range(n):
|
||||
cand = np.minimum(best[:, None], d).sum(0)
|
||||
for c in chosen:
|
||||
cand[c] = np.inf
|
||||
c = int(cand.argmin())
|
||||
chosen.append(c)
|
||||
best = np.minimum(best, d[:, c])
|
||||
return sorted(chosen)
|
||||
|
||||
|
||||
def render(img_rgb, n, dither_mode, ramp=None):
|
||||
"""Return (pen (H,W) 0..n-1, inks (n 6-bit colours), idx (H,W) palette
|
||||
indices, img_lab, plab, prgb)."""
|
||||
plab = g.palette_lab()
|
||||
prgb = g.get_palette().astype(np.uint8)
|
||||
img_lab = c64pal.srgb_to_lab(img_rgb)
|
||||
|
||||
pal_idx = list(ramp) if ramp is not None else choose_inks(img_lab, plab, n)
|
||||
allowed = np.tile(np.array(pal_idx), (*img_lab.shape[:2], 1))
|
||||
idx = dither.quantize(img_lab, allowed, plab, dither_mode).astype(np.int64)
|
||||
lut = {p: k for k, p in enumerate(pal_idx)}
|
||||
pen = np.vectorize(lut.get)(idx).astype(np.uint8)
|
||||
return pen, list(pal_idx), idx.astype(np.uint16), img_lab, plab, prgb
|
||||
|
||||
|
||||
def _pack_16(pen):
|
||||
"""160x192 pens (0-15) -> 15360 bytes. 2 px/byte, high nibble = left."""
|
||||
scr = bytearray(BYTES_PER_ROW * ROWS)
|
||||
for y in range(ROWS):
|
||||
row = pen[y]
|
||||
base = y * BYTES_PER_ROW
|
||||
for bx in range(BYTES_PER_ROW):
|
||||
scr[base + bx] = (int(row[bx * 2]) << 4) | (int(row[bx * 2 + 1]) & 0x0F)
|
||||
return bytes(scr)
|
||||
|
||||
|
||||
def _pack_4(pen):
|
||||
"""320x192 pens (0-3) -> 15360 bytes. 4 px/byte, MSB = left."""
|
||||
scr = bytearray(BYTES_PER_ROW * ROWS)
|
||||
for y in range(ROWS):
|
||||
row = pen[y]
|
||||
base = y * BYTES_PER_ROW
|
||||
for bx in range(BYTES_PER_ROW):
|
||||
scr[base + bx] = ((int(row[bx * 4]) << 6) | (int(row[bx * 4 + 1]) << 4) |
|
||||
(int(row[bx * 4 + 2]) << 2) | int(row[bx * 4 + 3]))
|
||||
return bytes(scr)
|
||||
|
||||
|
||||
def _pack_2(pen):
|
||||
"""640x192 pens (0-1) -> 15360 bytes. 8 px/byte, MSB = left."""
|
||||
scr = bytearray(BYTES_PER_ROW * ROWS)
|
||||
for y in range(ROWS):
|
||||
row = pen[y]
|
||||
base = y * BYTES_PER_ROW
|
||||
for bx in range(BYTES_PER_ROW):
|
||||
byte = 0
|
||||
for k in range(8):
|
||||
byte |= (int(row[bx * 8 + k]) & 1) << (7 - k)
|
||||
scr[base + bx] = byte
|
||||
return bytes(scr)
|
||||
|
||||
|
||||
PACK = {0: _pack_2, 1: _pack_4, 2: _pack_16} # keyed by CRES
|
||||
24
lenser/coco3/convert/gr16.py
Normal file
24
lenser/coco3/convert/gr16.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
"""CoCo 3 GIME 160x192, 16 colours from the 64-colour palette (flagship photo
|
||||
mode -- wide pixels but the most colour)."""
|
||||
from __future__ import annotations
|
||||
|
||||
from ...convert.base import Conversion, perceptual_error
|
||||
from . import _common
|
||||
|
||||
WIDTH, HEIGHT = 160, 192
|
||||
PIXEL_ASPECT = 2.0 # 160 wide on a 4:3 screen -> pixels twice as wide
|
||||
NCOL = 16
|
||||
CRES = 2
|
||||
|
||||
|
||||
def convert(img_rgb, palette_name="gime", dither_mode="floyd",
|
||||
intensive=False, base_color=None):
|
||||
pen, inks, idx, img_lab, plab, prgb = _common.render(img_rgb, NCOL, dither_mode)
|
||||
screen = _common.PACK[CRES](pen)
|
||||
return Conversion(
|
||||
mode="gr16", width=WIDTH, height=HEIGHT, pixel_aspect=PIXEL_ASPECT,
|
||||
index_image=idx, data=screen, data_addr=0x4000, viewer="coco3",
|
||||
preview_rgb=prgb[idx], error=perceptual_error(idx, img_lab, plab),
|
||||
meta={"palette": "gime", "dither": dither_mode, "cres": CRES,
|
||||
"inks": inks, "border": inks[0] if inks else 0},
|
||||
)
|
||||
24
lenser/coco3/convert/gr4.py
Normal file
24
lenser/coco3/convert/gr4.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
"""CoCo 3 GIME 320x192, 4 colours from the 64-colour palette (more resolution,
|
||||
fewer colours than gr16)."""
|
||||
from __future__ import annotations
|
||||
|
||||
from ...convert.base import Conversion, perceptual_error
|
||||
from . import _common
|
||||
|
||||
WIDTH, HEIGHT = 320, 192
|
||||
PIXEL_ASPECT = 1.0
|
||||
NCOL = 4
|
||||
CRES = 1
|
||||
|
||||
|
||||
def convert(img_rgb, palette_name="gime", dither_mode="floyd",
|
||||
intensive=False, base_color=None):
|
||||
pen, inks, idx, img_lab, plab, prgb = _common.render(img_rgb, NCOL, dither_mode)
|
||||
screen = _common.PACK[CRES](pen)
|
||||
return Conversion(
|
||||
mode="gr4", width=WIDTH, height=HEIGHT, pixel_aspect=PIXEL_ASPECT,
|
||||
index_image=idx, data=screen, data_addr=0x4000, viewer="coco3",
|
||||
preview_rgb=prgb[idx], error=perceptual_error(idx, img_lab, plab),
|
||||
meta={"palette": "gime", "dither": dither_mode, "cres": CRES,
|
||||
"inks": inks, "border": inks[0] if inks else 0},
|
||||
)
|
||||
45
lenser/coco3/convert/mono.py
Normal file
45
lenser/coco3/convert/mono.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
"""CoCo 3 GIME monochrome: 640x192, 2 colours (black + white) -- the highest-
|
||||
resolution CoCo 3 mode, tone carried by dithering. ``--mono-base`` tints the
|
||||
second tone."""
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
|
||||
from ... import dither, palette as c64pal
|
||||
from ...convert.base import Conversion, perceptual_error
|
||||
from .. import palette as g
|
||||
from . import _common
|
||||
|
||||
WIDTH, HEIGHT = 640, 192
|
||||
PIXEL_ASPECT = 0.5
|
||||
CRES = 0
|
||||
|
||||
|
||||
def convert(img_rgb, palette_name="gime", dither_mode="floyd",
|
||||
intensive=False, base_color=None):
|
||||
plab = g.palette_lab()
|
||||
black = min(g.GREYS, key=lambda i: plab[i, 0])
|
||||
if base_color in range(64) and base_color != black:
|
||||
ramp = sorted({black, int(base_color)}, key=lambda i: plab[i, 0])
|
||||
else:
|
||||
ramp = sorted(g.GREYS, key=lambda i: plab[i, 0])
|
||||
ramp = [ramp[0], ramp[-1]] # black + white
|
||||
|
||||
img_lab = c64pal.srgb_to_lab(img_rgb)
|
||||
mono_lab = np.zeros_like(img_lab); mono_lab[..., 0] = img_lab[..., 0]
|
||||
plab_mono = np.zeros_like(plab); plab_mono[:, 0] = plab[:, 0]
|
||||
allowed = np.tile(np.array(ramp), (*mono_lab.shape[:2], 1))
|
||||
idx = dither.quantize(mono_lab, allowed, plab_mono, dither_mode).astype(np.int64)
|
||||
lut = {p: k for k, p in enumerate(ramp)}
|
||||
pen = np.vectorize(lut.get)(idx).astype(np.uint8)
|
||||
|
||||
screen = _common.PACK[CRES](pen)
|
||||
prgb = g.get_palette().astype(np.uint8)
|
||||
return Conversion(
|
||||
mode="mono", width=WIDTH, height=HEIGHT, pixel_aspect=PIXEL_ASPECT,
|
||||
index_image=idx.astype(np.uint16), data=screen, data_addr=0x4000,
|
||||
viewer="coco3", preview_rgb=prgb[idx.astype(np.uint16)],
|
||||
error=perceptual_error(idx, mono_lab, plab_mono),
|
||||
meta={"palette": "gime", "dither": dither_mode, "cres": CRES,
|
||||
"inks": ramp, "border": ramp[0]},
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue