ii-pix/convert.py

173 lines
6.1 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
import numpy as np
2021-11-16 23:45:11 +00:00
import convert_hgr as convert_hgr_py
2021-11-26 13:15:57 +00:00
import convert_dhr as convert_dhr_py
import convert_shr as convert_shr_py
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
2023-01-31 21:28:40 +00:00
# TODO:
2022-07-16 21:00:14 +00:00
# - support additional graphics modes (easiest --> hardest):
# - LR/DLR
# - SHR 3200
# - SHR 640
# - HGR
2021-11-26 13:15:57 +00:00
def add_common_args(parser):
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-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-25 22:28:00 +00:00
parser.add_argument(
2021-11-26 13:15:57 +00:00
'--save-preview', action=argparse.BooleanOptionalAction, default=True,
help='Whether to save a .PNG rendering of the output image (default: '
'True)'
)
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)'
)
def add_dhr_hgr_args(parser):
parser.add_argument(
2021-11-26 13:15:57 +00:00
'--dither', type=str, choices=list(dither_pattern.PATTERNS.keys()),
default=dither_pattern.DEFAULT_PATTERN,
help="Error distribution pattern to apply when dithering (default: "
+ dither_pattern.DEFAULT_PATTERN + ")")
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)")
def validate_lookahead(arg: int) -> int:
try:
int_arg = int(arg)
except Exception:
raise argparse.ArgumentTypeError("--lookahead must be an integer")
if int_arg < 1:
raise argparse.ArgumentTypeError("--lookahead must be at least 1")
return int_arg
def main():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(required=True)
# Hi-res
hgr_parser = subparsers.add_parser("hgr")
add_common_args(hgr_parser)
add_dhr_hgr_args(hgr_parser)
hgr_parser.add_argument(
'--error_fraction', type=float, default = 0.7,
help="Fraction of quantization error to distribute to neighbouring "
"pixels according to dither pattern"
)
hgr_parser.set_defaults(func=convert_hgr)
# Double Hi-res
dhr_parser = subparsers.add_parser("dhr")
add_common_args(dhr_parser)
add_dhr_hgr_args(dhr_parser)
dhr_parser.add_argument(
"--lookahead", type=validate_lookahead, default=8,
help=("How many pixels to look ahead to compensate for NTSC colour "
"artifacts (default: 8)"))
2021-11-26 13:15:57 +00:00
dhr_parser.set_defaults(func=convert_dhr)
# Double Hi-Res mono
2023-01-21 17:30:27 +00:00
dhr_mono_parser = subparsers.add_parser("dhr_mono")
add_common_args(dhr_mono_parser)
dhr_mono_parser.set_defaults(func=convert_dhr_mono)
# Super Hi-Res 320x200
2021-11-26 13:15:57 +00:00
shr_parser = subparsers.add_parser("shr")
add_common_args(shr_parser)
shr_parser.add_argument(
'--fixed-colours', type=int, default=0,
2021-11-24 15:44:37 +00:00
help='How many colours to fix as identical across all 16 SHR palettes '
'(default: 0)'
)
2021-11-26 13:15:57 +00:00
shr_parser.add_argument(
2021-11-24 16:03:55 +00:00
'--show-final-score', action=argparse.BooleanOptionalAction,
default=False, help='Whether to output the final image quality score '
'(default: False)'
)
shr_parser.add_argument(
'--save-intermediate', action=argparse.BooleanOptionalAction,
default=False, help='Whether to save each intermediate iteration, '
'or just the final image (default: False)'
)
2021-11-26 13:15:57 +00:00
shr_parser.set_defaults(func=convert_shr)
2021-01-08 22:44:28 +00:00
args = parser.parse_args()
2021-11-26 13:15:57 +00:00
args.func(args)
2021-01-08 22:44:28 +00:00
2021-11-26 13:15:57 +00:00
def prepare_image(image_filename: str, show_input: bool, screen,
gamma_correct: float) -> np.ndarray:
2021-01-25 23:16:46 +00:00
# Open and resize source image
2021-11-26 13:15:57 +00:00
image = image_py.open(image_filename)
if show_input:
2023-01-21 17:30:27 +00:00
image_py.resize(image, screen.X_RES, screen.Y_RES * 2,
srgb_output=True).show()
return image_py.resize(image, screen.X_RES, screen.Y_RES,
gamma=gamma_correct)
2021-11-26 13:15:57 +00:00
2023-01-31 21:28:40 +00:00
def convert_hgr(args):
palette = palette_py.PALETTES[args.palette]()
screen = screen_py.HGRNTSCScreen(palette)
image = prepare_image(args.input, args.show_input, screen,
args.gamma_correct)
convert_hgr_py.convert(screen, image, args)
2021-11-26 13:15:57 +00:00
def convert_dhr(args):
palette = palette_py.PALETTES[args.palette]()
2023-01-21 17:30:27 +00:00
screen = screen_py.DHGRNTSCScreen(palette)
image = prepare_image(args.input, args.show_input, screen,
2023-01-31 21:28:40 +00:00
args.gamma_correct)
2023-01-21 17:30:27 +00:00
convert_dhr_py.convert(screen, image, args)
def convert_dhr_mono(args):
screen = screen_py.DHGRScreen()
2023-01-31 21:28:40 +00:00
image = prepare_image(args.input, args.show_input, screen,
args.gamma_correct)
2023-01-21 17:30:27 +00:00
convert_dhr_py.convert_mono(screen, image, args)
2021-11-26 13:15:57 +00:00
def convert_shr(args):
screen = screen_py.SHR320Screen()
2023-01-31 21:28:40 +00:00
image = prepare_image(args.input, args.show_input, screen,
args.gamma_correct)
2023-01-21 17:30:27 +00:00
convert_shr_py.convert(screen, image, args)
if __name__ == "__main__":
2021-01-09 18:05:36 +00:00
main()