2021-01-25 23:16:46 +00:00
|
|
|
"""Image converter to Apple II Double Hi-Res format."""
|
|
|
|
|
2020-12-29 18:24:29 +00:00
|
|
|
import argparse
|
2021-11-09 15:13:07 +00:00
|
|
|
import array
|
2021-01-08 22:44:28 +00:00
|
|
|
import os.path
|
2021-01-12 10:00:56 +00:00
|
|
|
import time
|
2021-11-10 00:34:17 +00:00
|
|
|
import collections
|
|
|
|
import random
|
2020-12-29 18:24:29 +00:00
|
|
|
|
2021-07-15 13:25:32 +00:00
|
|
|
import colour
|
2021-01-03 22:32:04 +00:00
|
|
|
from PIL import Image
|
2020-12-29 18:24:29 +00:00
|
|
|
import numpy as np
|
2021-11-13 17:18:34 +00:00
|
|
|
from pyclustering.cluster.kmedians import kmedians
|
|
|
|
from pyclustering.cluster.center_initializer import kmeans_plusplus_initializer
|
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
|
2021-01-03 23:23:15 +00:00
|
|
|
|
2021-01-11 20:21:00 +00:00
|
|
|
|
2020-12-29 21:03:17 +00:00
|
|
|
# TODO:
|
2021-01-15 22:34:03 +00:00
|
|
|
# - support LR/DLR
|
|
|
|
# - support HGR
|
2021-01-03 22:32:04 +00:00
|
|
|
|
2021-11-09 16:14:37 +00:00
|
|
|
def cluster_palette(image: Image):
|
2021-11-10 00:34:17 +00:00
|
|
|
line_to_palette = {}
|
2021-11-10 18:30:39 +00:00
|
|
|
|
2021-11-13 17:18:34 +00:00
|
|
|
# shuffle_lines = liprint(st(range(200))
|
|
|
|
# random.shuffle(shuffle_lines)
|
|
|
|
# for idx, line in enumerate(shuffle_lines):
|
2021-11-10 18:30:39 +00:00
|
|
|
# line_to_palette[line] = idx % 16
|
|
|
|
|
|
|
|
# for line in range(200):
|
|
|
|
# if line % 3 == 0:
|
|
|
|
# line_to_palette[line] = int(line / (200 / 16))
|
|
|
|
# elif line % 3 == 1:
|
|
|
|
# line_to_palette[line] = np.clip(int(line / (200 / 16)) + 1, 0, 15)
|
|
|
|
# else:
|
|
|
|
# line_to_palette[line] = np.clip(int(line / (200 / 16)) + 2, 0, 15)
|
|
|
|
|
|
|
|
for line in range(200):
|
|
|
|
if line % 3 == 0:
|
|
|
|
line_to_palette[line] = int(line / (200 / 16))
|
|
|
|
elif line % 3 == 1:
|
|
|
|
line_to_palette[line] = np.clip(int(line / (200 / 16)) + 1, 0, 15)
|
|
|
|
else:
|
|
|
|
line_to_palette[line] = np.clip(int(line / (200 / 16)) + 2, 0, 15)
|
2021-11-10 00:34:17 +00:00
|
|
|
|
2021-11-09 15:13:07 +00:00
|
|
|
colours_rgb = np.asarray(image).reshape((-1, 3))
|
|
|
|
with colour.utilities.suppress_warnings(colour_usage_warnings=True):
|
2021-11-09 16:14:37 +00:00
|
|
|
colours_cam = colour.convert(colours_rgb, "RGB",
|
2021-11-09 15:13:07 +00:00
|
|
|
"CAM16UCS").astype(np.float32)
|
2021-11-09 22:42:27 +00:00
|
|
|
palettes_rgb = {}
|
2021-11-11 16:10:03 +00:00
|
|
|
palettes_cam = {}
|
|
|
|
for palette_idx in range(16):
|
2021-11-13 17:18:34 +00:00
|
|
|
p_lower = max(palette_idx - 2, 0)
|
|
|
|
p_upper = min(palette_idx + 2, 16)
|
2021-11-11 16:10:03 +00:00
|
|
|
palette_pixels = colours_cam[
|
2021-11-13 17:18:34 +00:00
|
|
|
int(p_lower * (200 / 16)) * 320:int(p_upper * (
|
|
|
|
200 / 16)) * 320, :]
|
2021-11-10 00:34:17 +00:00
|
|
|
|
2021-11-11 16:10:03 +00:00
|
|
|
# kmeans = KMeans(n_clusters=16, max_iter=10000)
|
|
|
|
# kmeans.fit_predict(palette_pixels)
|
|
|
|
# palettes_cam[palette_idx] = kmeans.cluster_centers_
|
2021-11-10 18:30:39 +00:00
|
|
|
|
2021-11-13 17:18:34 +00:00
|
|
|
# fixed_centroids = None
|
2021-11-10 18:30:39 +00:00
|
|
|
# print(np.array(line_colours), fixed_centroids)
|
2021-11-13 17:18:34 +00:00
|
|
|
# palettes_cam[palette_idx] = dither_pyx.k_means_with_fixed_centroids(
|
|
|
|
# 16, palette_pixels, fixed_centroids=fixed_centroids,
|
|
|
|
# tolerance=1e-6)
|
|
|
|
|
|
|
|
initial_centers = kmeans_plusplus_initializer(
|
|
|
|
palette_pixels, 16).initialize()
|
|
|
|
kmedians_instance = kmedians(palette_pixels, initial_centers)
|
|
|
|
kmedians_instance.process()
|
|
|
|
palettes_cam[palette_idx] = np.array(
|
|
|
|
kmedians_instance.get_medians()).astype(np.float32)
|
2021-11-11 16:10:03 +00:00
|
|
|
|
|
|
|
# palette_colours = collections.defaultdict(list)
|
2021-11-13 17:18:34 +00:00
|
|
|
# for line in range(200):
|
|
|
|
# palette = line_to_palette[line]
|
|
|
|
# palette_colours[palette].extend(
|
|
|
|
# colours_cam[line * 320:(line + 1) * 320])
|
|
|
|
|
|
|
|
# For each line grouping, find big palette entries with minimal total
|
|
|
|
# distance
|
|
|
|
|
|
|
|
# palette_cam = None
|
|
|
|
# for palette_idx in range(16):
|
|
|
|
# line_colours = palette_colours[palette_idx]
|
|
|
|
# #if palette_idx < 15:
|
|
|
|
# # line_colours += palette_colours[palette_idx + 1]
|
|
|
|
# # if palette_idx < 14:
|
|
|
|
# # line_colours += palette_colours[palette_idx + 2]
|
|
|
|
# # if palette_idx > 0:
|
|
|
|
# # fixed_centroids = palette_cam[:8, :]
|
|
|
|
# # else:
|
|
|
|
# fixed_centroids = None
|
|
|
|
# # print(np.array(line_colours), fixed_centroids)
|
|
|
|
# palette_cam = dither_pyx.k_means_with_fixed_centroids(16, np.array(
|
|
|
|
# line_colours), fixed_centroids=fixed_centroids, tolerance=1e-6)
|
|
|
|
|
|
|
|
# kmeans = KMeans(n_clusters=16, max_iter=10000)
|
|
|
|
# kmeans.fit_predict(line_colours)
|
|
|
|
# palette_cam = kmeans.cluster_centers_
|
2021-11-11 11:10:22 +00:00
|
|
|
|
2021-11-09 22:42:27 +00:00
|
|
|
with colour.utilities.suppress_warnings(colour_usage_warnings=True):
|
2021-11-13 17:18:34 +00:00
|
|
|
palette_rgb = colour.convert(palettes_cam[palette_idx], "CAM16UCS",
|
|
|
|
"RGB")
|
2021-11-09 22:42:27 +00:00
|
|
|
# SHR colour palette only uses 4-bit values
|
|
|
|
palette_rgb = np.round(palette_rgb * 15) / 15
|
2021-11-10 18:30:39 +00:00
|
|
|
palettes_rgb[palette_idx] = palette_rgb.astype(np.float32)
|
|
|
|
# print(palettes_rgb)
|
2021-11-11 16:10:03 +00:00
|
|
|
|
|
|
|
# For each line, pick the palette with lowest total distance
|
|
|
|
# best_palette = 15
|
|
|
|
# for line in range(200):
|
|
|
|
# line_pixels = colours_cam[line*320:(line+1)*320]
|
|
|
|
# best_palette = dither_pyx.best_palette_for_line(
|
|
|
|
# line_pixels, palettes_cam, best_palette)
|
|
|
|
# line_to_palette[line] = best_palette
|
|
|
|
# print(line, line_to_palette[line])
|
|
|
|
return palettes_cam, palettes_rgb, line_to_palette
|
2021-11-09 11:23:25 +00:00
|
|
|
|
2020-12-30 10:27:33 +00:00
|
|
|
|
2020-12-29 18:24:29 +00:00
|
|
|
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(
|
2021-03-15 17:55:21 +00:00
|
|
|
"--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 "
|
2021-03-15 17:55:21 +00:00
|
|
|
"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(
|
2021-03-15 17:21:22 +00:00
|
|
|
'--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(
|
2021-03-15 17:21:22 +00:00
|
|
|
'--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-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 + ")")
|
2021-02-14 23:34:25 +00:00
|
|
|
parser.add_argument(
|
2021-03-15 17:21:22 +00:00
|
|
|
'--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)")
|
2021-03-15 15:01:21 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--verbose', action=argparse.BooleanOptionalAction,
|
|
|
|
default=False, help="Show progress during conversion")
|
2021-07-19 08:57:26 +00:00
|
|
|
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-11-02 15:26:43 +00:00
|
|
|
if args.lookahead < 1:
|
|
|
|
parser.error('--lookahead must be at least 1')
|
2021-01-08 22:44:28 +00:00
|
|
|
|
2021-11-09 11:23:25 +00:00
|
|
|
# palette = palette_py.PALETTES[args.palette]()
|
|
|
|
screen = screen_py.SHR320Screen()
|
2020-12-30 10:27:33 +00:00
|
|
|
|
2021-07-19 17:35:44 +00:00
|
|
|
# Conversion matrix from RGB to CAM16UCS colour values. Indexed by
|
|
|
|
# 24-bit RGB value
|
|
|
|
rgb_to_cam16 = np.load("data/rgb_to_cam16ucs.npy")
|
|
|
|
|
2021-01-25 23:16:46 +00:00
|
|
|
# Open and resize source image
|
2021-01-16 17:57:21 +00:00
|
|
|
image = image_py.open(args.input)
|
2021-01-15 22:34:03 +00:00
|
|
|
if args.show_input:
|
2021-11-09 11:23:25 +00:00
|
|
|
image_py.resize(image, screen.X_RES, screen.Y_RES,
|
2021-11-09 22:26:34 +00:00
|
|
|
srgb_output=False).show()
|
2021-07-19 17:35:44 +00:00
|
|
|
rgb = np.array(
|
|
|
|
image_py.resize(image, screen.X_RES, screen.Y_RES,
|
2021-11-09 22:26:34 +00:00
|
|
|
gamma=args.gamma_correct, srgb_output=True)).astype(
|
|
|
|
np.float32) / 255
|
2021-07-19 16:54:46 +00:00
|
|
|
|
2021-11-11 16:10:03 +00:00
|
|
|
palettes_cam, palettes_rgb, line_to_palette = cluster_palette(rgb)
|
2021-11-09 22:26:34 +00:00
|
|
|
# print(palette_rgb)
|
|
|
|
# screen.set_palette(0, (image_py.linear_to_srgb_array(palette_rgb) *
|
|
|
|
# 15).astype(np.uint8))
|
2021-11-09 22:42:27 +00:00
|
|
|
for i, p in palettes_rgb.items():
|
|
|
|
screen.set_palette(i, (np.round(p * 15)).astype(np.uint8))
|
2021-11-09 22:26:34 +00:00
|
|
|
|
2021-11-11 16:10:03 +00:00
|
|
|
output_4bit, line_to_palette = dither_pyx.dither_shr(
|
|
|
|
rgb, palettes_cam, palettes_rgb, rgb_to_cam16)
|
2021-11-09 22:26:34 +00:00
|
|
|
screen.set_pixels(output_4bit)
|
2021-11-09 22:42:27 +00:00
|
|
|
output_rgb = np.zeros((200, 320, 3), dtype=np.uint8)
|
2021-11-10 00:34:17 +00:00
|
|
|
for i in range(200):
|
|
|
|
screen.line_palette[i] = line_to_palette[i]
|
|
|
|
output_rgb[i, :, :] = (
|
2021-11-10 18:30:39 +00:00
|
|
|
palettes_rgb[line_to_palette[i]][
|
|
|
|
output_4bit[i, :]] * 255).astype(np.uint8)
|
2021-11-09 11:23:25 +00:00
|
|
|
output_srgb = image_py.linear_to_srgb(output_rgb).astype(np.uint8)
|
|
|
|
|
|
|
|
# dither = dither_pattern.PATTERNS[args.dither]()
|
|
|
|
# bitmap = dither_pyx.dither_image(
|
|
|
|
# screen, rgb, dither, args.lookahead, args.verbose, rgb_to_cam16)
|
2021-01-16 18:11:21 +00:00
|
|
|
|
2021-02-14 23:34:25 +00:00
|
|
|
# Show output image by rendering in target palette
|
2021-11-09 11:23:25 +00:00
|
|
|
# output_palette_name = args.show_palette or args.palette
|
|
|
|
# output_palette = palette_py.PALETTES[output_palette_name]()
|
|
|
|
# output_screen = screen_py.DHGRScreen(output_palette)
|
|
|
|
# if output_palette_name == "ntsc":
|
|
|
|
# output_srgb = output_screen.bitmap_to_image_ntsc(bitmap)
|
|
|
|
# else:
|
|
|
|
# output_srgb = image_py.linear_to_srgb(
|
|
|
|
# output_screen.bitmap_to_image_rgb(bitmap)).astype(np.uint8)
|
2021-11-02 23:28:58 +00:00
|
|
|
out_image = image_py.resize(
|
2021-11-09 11:23:25 +00:00
|
|
|
Image.fromarray(output_srgb), screen.X_RES, screen.Y_RES,
|
2021-11-09 22:26:34 +00:00
|
|
|
srgb_output=False) # XXX true
|
2021-02-14 23:34:25 +00:00
|
|
|
|
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-04 21:08:29 +00:00
|
|
|
|
2021-01-25 23:16:46 +00:00
|
|
|
# Save Double hi-res image
|
2021-11-09 22:26:34 +00:00
|
|
|
outfile = os.path.join(os.path.splitext(args.output)[0] + "-preview.png")
|
|
|
|
out_image.save(outfile, "PNG")
|
|
|
|
screen.pack()
|
2021-11-09 11:23:25 +00:00
|
|
|
# with open(args.output, "wb") as f:
|
|
|
|
# f.write(bytes(screen.aux))
|
|
|
|
# f.write(bytes(screen.main))
|
2021-11-09 22:26:34 +00:00
|
|
|
with open(args.output, "wb") as f:
|
|
|
|
f.write(bytes(screen.memory))
|
2020-12-29 18:24:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-01-09 18:05:36 +00:00
|
|
|
main()
|