From c097064eb30ea2a669ed3dc121b859d00d2c1516 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Fri, 2 Feb 2018 23:08:17 -0500 Subject: Add gfxx palette loading --- bin/gfxx.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/bin/gfxx.c b/bin/gfxx.c index 3d743803..f0b8aa9a 100644 --- a/bin/gfxx.c +++ b/bin/gfxx.c @@ -152,9 +152,10 @@ static uint8_t get(size_t i) { static /**/ int init(int argc, char *argv[]) { const char *path = NULL; + const char *palPath = NULL; int opt; - while (0 < (opt = getopt(argc, argv, "c:b:ern:mw:z:"))) { + while (0 < (opt = getopt(argc, argv, "c:p:b:ern:mw:z:"))) { switch (opt) { case 'c': switch (optarg[0]) { case 'g': space = COLOR_GRAYSCALE; break; @@ -162,6 +163,7 @@ static /**/ int init(int argc, char *argv[]) { case 'r': space = COLOR_RGB; break; default: return EX_USAGE; } break; + case 'p': palPath = optarg; break; case 'b': bits = strtoul(optarg, NULL, 0); break; case 'e': endian ^= true; break; case 'r': reverse ^= true; break; @@ -175,6 +177,16 @@ static /**/ int init(int argc, char *argv[]) { if (argc > optind) path = argv[optind]; if (!bits || !width || !scale) return EX_USAGE; + if (palPath) { + FILE *pal = fopen(palPath, "r"); + if (!pal) err(EX_NOINPUT, "%s", palPath); + + fread(palette, 1, sizeof(palette), pal); + if (ferror(pal)) err(EX_IOERR, "%s", palPath); + + fclose(pal); + } + FILE *file = path ? fopen(path, "r") : stdin; if (!file) err(EX_NOINPUT, "%s", path); @@ -191,6 +203,8 @@ static /**/ int init(int argc, char *argv[]) { size = fread(data, 1, size, file); if (ferror(file)) err(EX_IOERR, "%s", path); + fclose(file); + return EX_OK; } @@ -345,7 +359,7 @@ static /**/ void input(char in) { break; case '[': if (!space--) space = COLOR__MAX - 1; break; case ']': if (++space == COLOR__MAX) space = 0; break; case '{': if (bits > 16) bits -= 8; else bits = (bits + 1) / 2; - break; case '}': if (bits < 16) bits *= 2; else bits += 8; + break; case '}': if (bits < 16) bits *= 2; else if (bits < 32) bits += 8; break; case 'e': endian ^= true; break; case 'r': reverse ^= true; break; case 'h': if (offset) offset--; -- cgit 1.4.1