From d89d1a6c8b0a2d4b867e4e2ad868b6e60651a603 Mon Sep 17 00:00:00 2001 From: June 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 184699b8..92a2267f 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 2020-03-09Import /usr/src/bin/sh from FreeBSD 12.1-RELEASEJune McEnroe 2020-03-09Remove 1sh sourcesJune McEnroe I'm going to recreate it from fresh sh sources for clean git history. 2020-03-08Add The Stone SkyJune McEnroe 2020-03-08Publish "How I Relay Chat"June McEnroe 2020-03-03Don't use $ inside $(())June McEnroe 2020-03-03Remove setoptJune McEnroe 2020-03-03Use getopts in shell scriptsJune McEnroe WTF why did no one tell me about this? 2020-02-27Style %T outside of Rs in italicJune McEnroe 2020-02-26Add Fierce Femmes and Notorious LiarsJune McEnroe 2020-02-23Add This Is How You Lose the Time WarJune McEnroe 2020-02-22Add See Ya LaterJune McEnroe 2020-02-20Remove wiki scriptJune McEnroe Wikipedia seems to have removed the one-sentence extracts from the opensearch results. Too bad. It's not a wiki script, what we need is a command that fetches single-sentence summaries of articles on Wikipedia. 2020-02-19Add The Obelisk GateJune McEnroe 2020-02-17Add Four Tet — HandsJune McEnroe One from the cafe that caught my attention. 2020-02-12Simplify macOS notify-sendJune McEnroe 2020-02-12Add imbox and notemap to pageJune McEnroe 2020-02-12Collapse simple linksJune McEnroe 2020-02-12Move catgirl up the pageJune McEnroe 2020-02-12Update catgirl pty grabJune McEnroe 2020-02-12Link to cgit /about pages where appropriateJune McEnroe 2020-02-11Separate LINKS from BINS for html to workJune McEnroe 2020-02-11Add margin to Bl-bullet itemsJune McEnroe 2020-02-10Match URLs inside parens or with paired parens insideJune McEnroe 2020-02-10Duplicate effective URL before passing it back to curlJune McEnroe Apparently sometimes it didn't like receiving its own internal storage to parse again. Understandable. 2020-02-09Add To Be Taught, If FortunateJune McEnroe 2020-02-04Add The Future of Another TimelineJune McEnroe Wow. One of the best I've read. 2020-01-31Reorganize the Makefile for the umpteenth timeJune McEnroe Broke out LDLIBS for each bin, and made everything more uniform. 2020-01-28Change scout sensitivity to 1.4June McEnroe idk it seems to work. 2020-01-28Import shows.txtJune McEnroe