From b60d8026e0a8c8d9d47a1096aef42abc36968b72 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Tue, 6 Feb 2018 21:34:44 -0500 Subject: Add gfxx palette loading and dumping --- bin/gfxx.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/bin/gfxx.c b/bin/gfxx.c index 8a124b3a..d824d276 100644 --- a/bin/gfxx.c +++ b/bin/gfxx.c @@ -55,10 +55,11 @@ static size_t size; static uint8_t *data; extern int init(int argc, char *argv[]) { + const char *pal = NULL; const char *path = NULL; int opt; - while (0 < (opt = getopt(argc, argv, "c:b:e:E:n:fmw:z:"))) { + while (0 < (opt = getopt(argc, argv, "c:p:b:e:E:n:fmw:z:"))) { switch (opt) { case 'c': switch (optarg[0]) { case 'i': space = COLOR_INDEXED; break; @@ -66,6 +67,7 @@ extern int init(int argc, char *argv[]) { case 'r': space = COLOR_RGB; break; default: return EX_USAGE; } break; + case 'p': pal = optarg; break; case 'e': switch (optarg[0]) { case 'l': byteOrder = ENDIAN_LITTLE; break; case 'b': byteOrder = ENDIAN_BIG; break; @@ -95,6 +97,15 @@ extern int init(int argc, char *argv[]) { if (argc > optind) path = argv[optind]; if (!width || !scale) return EX_USAGE; + if (pal) { + FILE *file = fopen(pal, "r"); + if (!file) err(EX_NOINPUT, "%s", pal); + fread(palette, 4, 256, file); + if (ferror(file)) err(EX_IOERR, "%s", pal); + int error = fclose(file); + if (error) err(EX_IOERR, "%s", pal); + } + if (path) { int fd = open(path, O_RDONLY); if (fd < 0) err(EX_NOINPUT, "%s", path); @@ -120,7 +131,7 @@ extern int init(int argc, char *argv[]) { static char options[128]; -extern const char *title(void) { +static void formatOptions(void) { snprintf( options, sizeof(options), @@ -135,6 +146,28 @@ extern const char *title(void) { width, scale ); +} + +static void formatName(const char *ext) { + snprintf( + options, + sizeof(options), + "%c%c%c%c%c%c%c-%08zx-%c%c%02zu-%zu.%s", + "igr"[space], + "lb"[byteOrder], + "lb"[bitOrder], + bits[0] + '0', bits[1] + '0', bits[2] + '0', bits[3] + '0', + offset, + "xf"[flip], + "xm"[mirror], + width, + scale, + ext + ); +} + +extern const char *title(void) { + formatOptions(); return options; } @@ -270,13 +303,23 @@ extern void draw(uint32_t *buf, size_t xres, size_t yres) { } } -static void samplePalette(void) { +static void palSample(void) { size_t temp = scale; scale = 1; draw(palette, 256, 1); scale = temp; } +static void palDump(void) { + formatName("dat"); + FILE *file = fopen(options, "w"); + if (!file) { warn("%s", options); return; } + size_t count = fwrite(palette, 4, 256, file); + if (count != 256) { warn("%s", options); return; } + int error = fclose(file); + if (error) { warn("%s", options); return; } +} + static const uint8_t PRESETS[][4] = { { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, @@ -300,11 +343,12 @@ extern void input(char in) { size_t pixel = (BITS_TOTAL + 7) / 8; size_t row = width * pixel; switch (in) { - case 'q': title(); printf("%s\n", options); exit(EX_OK); - break; case 'o': title(); printf("%s\n", options); + case 'q': formatOptions(); printf("%s\n", options); exit(EX_OK); + break; case 'o': formatOptions(); printf("%s\n", options); break; case '[': if (!space--) space = COLOR__MAX - 1; break; case ']': if (++space == COLOR__MAX) space = 0; - break; case 'p': samplePalette(); + break; case 'p': palSample(); + break; case 'P': palDump(); break; case '{': if (!preset--) preset = PRESETS_LEN - 1; setPreset(); break; case '}': if (++preset == PRESETS_LEN) preset = 0; setPreset(); break; case 'e': byteOrder ^= ENDIAN_BIG; -- cgit 1.4.1 '/src/commit/home/.config/cash/env.sh?id=f2acb66193e1ee662280565b5a3cb75321e69bd5&follow=1'>Add !! alias in cashJune McEnroe 2019-01-15Add shell and unshell targetsJune McEnroe 2019-01-15Use \$ in cash PS1 and add # to RPS1June McEnroe 2019-01-15Use flock(2) when loading and saving historyJune McEnroe 2019-01-15Add sans8x16.psfJune McEnroe 2019-01-14Add guides to psfedJune McEnroe 2019-01-14Check for NULL copy or undo buffers in psfedJune McEnroe 2019-01-14Add cash.7 READMEJune McEnroe 2019-01-14Document old=new argument of fc -sJune McEnroe 2019-01-14Allow replacing empty string with fc old=newJune McEnroe 2019-01-13Enable warnings in libeditJune McEnroe 2019-01-13Show full path in RPS1June McEnroe 2019-01-13Shorten $HOME to ~ in prompt expansionJune McEnroe 2019-01-13Document PSlitJune McEnroe 2019-01-13Document PS0June McEnroe 2019-01-13Set PS0 in cashJune McEnroe 2019-01-13Add PS0June McEnroe 2019-01-13Change default ENV from cashrc to env.shJune McEnroe 2019-01-13Use colours in cash promptsJune McEnroe 2019-01-12Set PSlit like NetBSD shJune McEnroe 2019-01-12Install gnupg2 from pkgsrc and symlink gpgJune McEnroe 2019-01-12Reference cash builtin man pages in cash.1 SEE ALSOJune McEnroe 2019-01-12Restore cash builtin man page datesJune McEnroe 2019-01-12Use local libeditJune McEnroe 2019-01-12Replace libedit MakefileJune McEnroe 2019-01-11Import /usr/src/lib/libedit from NetBSD 8.0June McEnroe 2019-01-11Add PSlit for prompt escapesJune McEnroe 2019-01-11Don't make depend automaticallyJune McEnroe