From cea17fc25505cdaeb297cdd64ecae397aefb9866 Mon Sep 17 00:00:00 2001 From: hkc Date: Wed, 20 Dec 2023 19:59:28 +0300 Subject: [PATCH] Fix reading past EOF + .nfp viewer --- ccpi.lua | 7 +++++- nfpview.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 nfpview.py diff --git a/ccpi.lua b/ccpi.lua index 9f66d95..aa480d3 100644 --- a/ccpi.lua +++ b/ccpi.lua @@ -41,7 +41,12 @@ local function load(path) local line = { s = "", bg = "", fg = "" } for x = 1, image.w do line.s = line.s .. fp:read(1) - local color = string.byte(fp:read(1)) + local char = fp:read(1) + if char == nil then + printError("Fatal: failed to read color data for y=" .. y .. " x=" .. x) + break + end + local color = string.byte(char) line.bg = line.bg .. string.format("%x", bit32.band(0xF, color)) line.fg = line.fg .. string.format("%x", bit32.band(0xF, bit32.rshift(color, 4))) end diff --git a/nfpview.py b/nfpview.py new file mode 100644 index 0000000..468f64f --- /dev/null +++ b/nfpview.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +from argparse import ArgumentParser + +colors = { + "0": (240, 240, 240), + "1": (242, 178, 51), + "2": (229, 127, 216), + "3": (153, 178, 242), + "4": (222, 222, 108), + "5": (127, 204, 25), + "6": (242, 178, 204), + "7": ( 76, 76, 76), + "8": (153, 153, 153), + "9": ( 76, 153, 178), + "a": (178, 102, 229), + "b": ( 51, 102, 204), + "c": (127, 102, 76), + "d": ( 87, 166, 78), + "e": (204, 76, 76), + "f": ( 17, 17, 17), +} + +color_names = { + "0": "white", + "1": "orange", + "2": "magenta", + "3": "lightBlue", + "4": "yellow", + "5": "lime", + "6": "pink", + "7": "gray", + "8": "lightGray", + "9": "cyan", + "a": "purple", + "b": "blue", + "c": "brown", + "d": "green", + "e": "red", + "f": "black", +} + +parser = ArgumentParser(description="CC default image viewer") +parser.add_argument("filename") + +def color(v: str): + return tuple(bytes.fromhex(v.lstrip('#'))) + + +for k, (r, g, b) in colors.items(): + parser.add_argument(f"-{k}", type=color, default=(r, g, b), + help="Set color %s" % color_names[k]) + +args = parser.parse_args() + +with open(args.filename, "r") as fp: + for line in fp: + for char in line.rstrip(): + if char in colors: + print("\x1b[48;2;%d;%d;%dm \x1b[0m" % colors[char], end="") + else: + print(" ", end="") + print()