summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-02-14 22:47:46 -0500
committerJune McEnroe <june@causal.agency>2018-02-14 22:47:46 -0500
commit3ad29de9fe35e8b4ee22f0a3c329bdec295aea7a (patch)
treeb7d7258bd3a48758913418cc120fadfb98e5ff79 /bin
parentAvoid accessing past last byte in gfxx (diff)
downloadsrc-3ad29de9fe35e8b4ee22f0a3c329bdec295aea7a.tar.gz
src-3ad29de9fe35e8b4ee22f0a3c329bdec295aea7a.zip
Encode grayscale PNGs in gfxx
Diffstat (limited to '')
-rw-r--r--bin/gfxx.c47
1 files changed, 28 insertions, 19 deletions
diff --git a/bin/gfxx.c b/bin/gfxx.c
index 72fe64b8..5d89055b 100644
--- a/bin/gfxx.c
+++ b/bin/gfxx.c
@@ -314,16 +314,19 @@ static void pngUint32(uint32_t data) {
 static void pngDump(uint32_t *src, size_t srcWidth, size_t srcHeight) {
     int error;
 
-    size_t scanline = 1 + 3 * srcWidth;
+    size_t scanline = 1 + (space == COLOR_GRAYSCALE ? 1 : 3) * srcWidth;
     uint8_t filt[scanline * srcHeight];
     for (size_t y = 0; y < srcHeight; ++y) {
         filt[y * scanline] = 0; // None
         for (size_t x = 0; x < srcWidth; ++x) {
-            uint32_t srcPixel = src[y * srcWidth + x];
-            uint8_t *filtPixel = &filt[y * scanline + 1 + 3 * x];
-            filtPixel[0] = srcPixel >> 16;
-            filtPixel[1] = srcPixel >> 8;
-            filtPixel[2] = srcPixel;
+            if (space == COLOR_GRAYSCALE) {
+                filt[y * scanline + 1 + x] = src[y * srcWidth + x];
+            } else {
+                uint8_t *filtPixel = &filt[y * scanline + 1 + 3 * x];
+                filtPixel[0] = src[y * srcWidth + x] >> 16;
+                filtPixel[1] = src[y * srcWidth + x] >> 8;
+                filtPixel[2] = src[y * srcWidth + x];
+            }
         }
     }
 
@@ -334,28 +337,34 @@ static void pngDump(uint32_t *src, size_t srcWidth, size_t srcHeight) {
 
     snprintf(pngPath, sizeof(pngPath), "%s%04u.png", prefix, counter++);
     png = fopen(pngPath, "wx");
-    if (!png) err(EX_CANTCREAT, "%s", pngPath);
+    if (!png) { warn("%s", pngPath); return; }
     printf("%s\n", pngPath);
 
-    const uint8_t SIGNATURE[] = { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n' };
-    const uint8_t HEADER[] = { 8, 2, 0, 0, 0 }; // 8-bit RGB
-    const char SOFTWARE[] = "Software";
-
+    uint8_t signature[] = { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n' };
+    uint8_t header[] = { 8, (space == COLOR_GRAYSCALE ? 0 : 2), 0, 0, 0 };
+    char software[] = "Software";
     formatOptions();
 
-    pngWrite(SIGNATURE, sizeof(SIGNATURE));
-    PNG_CHUNK("IHDR", 4 + 4 + sizeof(HEADER)) {
+    pngWrite(signature, sizeof(signature));
+    PNG_CHUNK("IHDR", 4 + 4 + sizeof(header)) {
         pngUint32(srcWidth);
         pngUint32(srcHeight);
-        pngWrite(HEADER, sizeof(HEADER));
+        pngWrite(header, sizeof(header));
     }
-    PNG_CHUNK("tEXt", sizeof(SOFTWARE) + strlen(options)) {
-        pngWrite(SOFTWARE, sizeof(SOFTWARE));
+    PNG_CHUNK("tEXt", sizeof(software) + strlen(options)) {
+        pngWrite(software, sizeof(software));
         pngWrite(options, strlen(options));
     }
-    PNG_CHUNK("sBIT", 3) {
-        uint8_t sbit[3] = { MAX(bits[R], 1), MAX(bits[G], 1), MAX(bits[B], 1) };
-        pngWrite(sbit, sizeof(sbit));
+    if (space == COLOR_GRAYSCALE) {
+        PNG_CHUNK("sBIT", 1) {
+            uint8_t sbit = BITS_COLOR;
+            pngWrite(&sbit, 1);
+        }
+    } else {
+        PNG_CHUNK("sBIT", 3) {
+            uint8_t sbit[] = { MAX(bits[R], 1), MAX(bits[G], 1), MAX(bits[B], 1) };
+            pngWrite(sbit, sizeof(sbit));
+        }
     }
     PNG_CHUNK("IDAT", dataSize) {
         pngWrite(data, dataSize);
s='nohover-highlight'> 2020-02-11Don't insert color codes for non-mentionsJune McEnroe 2020-02-11Take first two words in colorMentionsJune McEnroe This lets phrases like "hi june" get colored, but still doesn't get carried away. 2020-02-11Use time_t for save signatureJune McEnroe It's actually more likely to be 64-bit than size_t anyway, and it eliminates some helper functions. Also don't error when reading an empty save file. 2020-02-11Set self.nick to * initiallyJune McEnroe Allows removing a bunch of checks that self.nick is set, and it's what the server usually calls you before registration. Never highlight notices as mentions. 2020-02-11Define ColorCap instead of hardcoding 100June McEnroe 2020-02-11Move hash to top of chat.hJune McEnroe 2020-02-11Move base64 out of chat.hJune McEnroe 2020-02-11Move XDG_SUBDIR out of chat.hJune McEnroe 2020-02-11Fix whois idle unit calculationJune McEnroe Rookie mistake. 2020-02-11Cast towupper to wchar_tJune McEnroe For some reason it takes and returns wint_t... 2020-02-11Cast set but unused variables to voidJune McEnroe 2020-02-11Declare strlcatJune McEnroe 2020-02-11Check if VDSUSP existsJune McEnroe 2020-02-11Fix completeReplace iterationJune McEnroe 2020-02-11Use pkg(8) to configure on FreeBSDJune McEnroe 2020-02-11Remove legacy codeJune McEnroe 2020-02-11Add INSTALLING section to READMEJune McEnroe