ii-pix/convert.py

143 lines
5.7 KiB
Python
Raw Normal View History

2021-01-25 23:16:46 +00:00
"""Image converter to Apple II Double Hi-Res format."""
import argparse
2021-01-08 22:44:28 +00:00
import os.path
2021-01-12 10:00:56 +00:00
import time
2021-07-15 13:25:32 +00:00
import colour
from PIL import Image
import numpy as np
2021-01-15 22:18:25 +00:00
2021-01-15 22:20:28 +00:00
import dither as dither_pyx
2021-01-15 22:18:25 +00:00
import dither_pattern
import image as image_py
import palette as palette_py
import screen as screen_py
# TODO:
2021-01-15 22:34:03 +00:00
# - support LR/DLR
# - support HGR
def main():
parser = argparse.ArgumentParser()
2021-01-15 22:28:44 +00:00
parser.add_argument("input", type=str, help="Input image file to process.")
parser.add_argument("output", type=str, help="Output file for converted "
"Apple II image.")
2021-01-08 22:44:28 +00:00
parser.add_argument(
"--lookahead", type=int, default=8,
2021-01-08 22:44:28 +00:00
help=("How many pixels to look ahead to compensate for NTSC colour "
"artifacts (default: 8)"))
2021-01-15 22:28:44 +00:00
parser.add_argument(
'--dither', type=str, choices=list(dither_pattern.PATTERNS.keys()),
default=dither_pattern.DEFAULT_PATTERN,
2021-03-15 10:45:33 +00:00
help="Error distribution pattern to apply when dithering (default: "
+ dither_pattern.DEFAULT_PATTERN + ")")
2021-01-15 22:34:03 +00:00
parser.add_argument(
'--show-input', action=argparse.BooleanOptionalAction, default=False,
2021-03-15 10:45:33 +00:00
help="Whether to show the input image before conversion.")
2021-01-15 22:34:03 +00:00
parser.add_argument(
'--show-output', action=argparse.BooleanOptionalAction, default=True,
2021-03-15 10:45:33 +00:00
help="Whether to show the output image after conversion.")
2021-01-15 22:58:01 +00:00
parser.add_argument(
2021-03-15 10:45:33 +00:00
'--resolution', type=str, choices=("140", "560"), default="560",
help=("Effective double hi-res resolution to target. '140' treats "
"pixels in groups of 4, with 16 colours that are chosen "
"independently, and ignores NTSC fringing. This is mostly only "
"useful for comparison to other 140px converters. '560' treats "
"each pixel individually, with choice of 2 colours (depending on "
"NTSC colour phase), and looking ahead over next --lookahead "
2021-03-15 10:45:33 +00:00
"pixels to optimize the colour sequence (default: 560)")
2021-01-15 22:58:01 +00:00
)
2021-01-25 22:28:00 +00:00
parser.add_argument(
2021-03-15 10:45:33 +00:00
'--palette', type=str, choices=list(set(palette_py.PALETTES.keys())),
2021-01-25 22:28:00 +00:00
default=palette_py.DEFAULT_PALETTE,
2021-03-15 10:45:33 +00:00
help='RGB colour palette to dither to. "ntsc" blends colours over 8 '
'pixels and gives better image quality on targets that '
'use/emulate NTSC, but can be substantially slower. Other '
'palettes determine colours based on 4 pixel sequences '
'(default: ' + palette_py.DEFAULT_PALETTE + ")")
parser.add_argument(
'--show-palette', type=str, choices=list(palette_py.PALETTES.keys()),
2021-03-15 10:45:33 +00:00
help="RGB colour palette to use when --show_output (default: "
"value of --palette)")
parser.add_argument(
'--verbose', action=argparse.BooleanOptionalAction,
default=False, help="Show progress during conversion")
parser.add_argument(
'--gamma_correct', type=float, default=2.4,
help='Gamma-correct image by this value (default: 2.4)'
)
2021-01-08 22:44:28 +00:00
args = parser.parse_args()
2021-03-15 10:45:33 +00:00
palette = palette_py.PALETTES[args.palette]()
if args.resolution == "140":
if args.palette == "ntsc":
raise argparse.ArgumentError(
"--resolution=140 cannot be combined with --palette=ntsc")
screen = screen_py.DHGR140Screen(palette)
lookahead = 0
2021-01-15 22:58:01 +00:00
else:
2021-03-15 10:45:33 +00:00
if args.palette == "ntsc":
# TODO: palette depth should be controlled by Palette not Screen
screen = screen_py.DHGR560NTSCScreen(palette)
else:
2021-03-15 10:45:33 +00:00
screen = screen_py.DHGR560Screen(palette)
lookahead = args.lookahead
2021-01-25 23:16:46 +00:00
# Open and resize source image
image = image_py.open(args.input)
2021-01-15 22:34:03 +00:00
if args.show_input:
2021-03-15 10:45:33 +00:00
image_py.resize(image, screen.NATIVE_X_RES, screen.NATIVE_Y_RES * 2,
srgb_output=True).show()
2021-07-19 17:13:43 +00:00
rgb = np.array(image_py.resize(image, screen.X_RES,
screen.Y_RES,
gamma=args.gamma_correct) / 255).astype(
np.float32)
2021-07-19 17:13:43 +00:00
# bits24 = np.arange(2 ** 24).reshape(-1, 1)
# all_rgb = (np.concatenate(
# [bits24 >> 16 & 0xff, bits24 >> 8 & 0xff, bits24 & 0xff],
# axis=1) / 255).astype(np.float32)
# all_cam16 = colour.convert(all_rgb, "RGB", "CAM16UCS").astype(np.float32)
# f = np.memmap("rgb_to_cam16ucs.data", mode="w+", dtype=np.float32,
# shape=all_cam16.shape)
# f[:] = all_cam16
# if True:
# return
all_cam16 = np.memmap("rgb_to_cam16ucs.data", mode="r+", dtype=np.float32,
shape=(2 ** 24, 3))
dither = dither_pattern.PATTERNS[args.dither]()
2021-03-15 10:45:33 +00:00
output_nbit, _ = dither_pyx.dither_image(
2021-07-19 17:13:43 +00:00
screen, rgb, dither, lookahead, args.verbose, all_cam16)
2021-03-15 10:45:33 +00:00
bitmap = screen.pack(output_nbit)
# Show output image by rendering in target palette
2021-03-15 10:45:33 +00:00
output_palette_name = args.show_palette or args.palette
output_palette = palette_py.PALETTES[output_palette_name]()
if output_palette_name == "ntsc":
output_screen = screen_py.DHGR560NTSCScreen(output_palette)
else:
output_screen = screen_py.DHGR560Screen(output_palette)
output_rgb = output_screen.bitmap_to_image_rgb(bitmap)
2021-01-15 22:18:25 +00:00
out_image = Image.fromarray(image_py.linear_to_srgb(output_rgb).astype(
np.uint8))
2021-03-15 10:45:33 +00:00
out_image = image_py.resize(out_image, screen.NATIVE_X_RES,
screen.NATIVE_Y_RES * 2, srgb_output=True)
2021-01-15 22:34:03 +00:00
if args.show_output:
2021-01-25 22:28:00 +00:00
out_image.show()
2021-01-25 23:16:46 +00:00
# Save Double hi-res image
2021-01-25 22:28:00 +00:00
outfile = os.path.join(os.path.splitext(args.output)[0] + "-preview.png")
out_image.save(outfile, "PNG")
with open(args.output, "wb") as f:
f.write(bytes(screen.aux))
f.write(bytes(screen.main))
if __name__ == "__main__":
2021-01-09 18:05:36 +00:00
main()