From 7a6d92169a8a2f829322ec768ee5cfdf11deb7a1 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Thu, 15 Nov 2018 14:44:54 -0500 Subject: Use png.h in gfxx.c --- gfx/Makefile | 4 ++- gfx/gfxx.c | 58 +++++++------------------------- gfx/png.h | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 47 deletions(-) create mode 100644 gfx/png.h (limited to 'gfx') diff --git a/gfx/Makefile b/gfx/Makefile index 22198835..20ff5ecd 100644 --- a/gfx/Makefile +++ b/gfx/Makefile @@ -5,7 +5,7 @@ BINS = brot gfxx MAN1 = $(BINS:%=man/%.1) CFLAGS += -Wall -Wextra -Wpedantic -LDLIBS = -lm -lz +LDLIBS = -lm LDLIBS_cocoa = -framework Cocoa LDLIBS_x11 = -lX11 @@ -18,6 +18,8 @@ brot.o gfxx.o: gfx.h cocoa.o fb.o x11.o: gfx.h +gfxx.o: png.h + brot: brot.o $(GFX).o $(CC) $(LDFLAGS) brot.o $(GFX).o $(LDLIBS) $(LDLIBS_$(GFX)) -o $@ diff --git a/gfx/gfxx.c b/gfx/gfxx.c index edeb14f5..0e2a2f2a 100644 --- a/gfx/gfxx.c +++ b/gfx/gfxx.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2018, June McEnroe +/* Copyright (C) 2018 June McEnroe * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -14,7 +14,6 @@ * along with this program. If not, see . */ -#include #include #include #include @@ -27,9 +26,9 @@ #include #include #include -#include #include "gfx.h" +#include "png.h" #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MASK(b) ((1 << (b)) - 1) @@ -329,22 +328,6 @@ static void outOpen(const char *ext) { } } -static uint32_t crc; -static void pngWrite(const void *ptr, size_t size) { - fwrite(ptr, size, 1, out.file); - if (ferror(out.file)) err(EX_IOERR, "%s", out.path); - crc = crc32(crc, ptr, size); -} -static void pngUint(uint32_t host) { - uint32_t net = htonl(host); - pngWrite(&net, 4); -} -static void pngChunk(const char *type, uint32_t size) { - pngUint(size); - crc = crc32(0, Z_NULL, 0); - pngWrite(type, 4); -} - static void pngDump(uint32_t *src, size_t srcWidth, size_t srcHeight) { int error; @@ -360,43 +343,26 @@ static void pngDump(uint32_t *src, size_t srcWidth, size_t srcHeight) { } } - uLong deflateSize = compressBound(sizeof(data)); - uint8_t deflate[deflateSize]; - error = compress(deflate, &deflateSize, data, sizeof(data)); - if (error != Z_OK) errx(EX_SOFTWARE, "compress: %d", error); - outOpen("png"); if (!out.file) return; - const uint8_t Signature[8] = "\x89PNG\r\n\x1A\n"; - const uint8_t Header[] = { 8, 2, 0, 0, 0 }; // 8-bit truecolor const char Software[] = "Software"; formatOptions(); uint8_t sbit[3] = { MAX(bits[R], 1), MAX(bits[G], 1), MAX(bits[B], 1) }; - pngWrite(Signature, sizeof(Signature)); - - pngChunk("IHDR", 4 + 4 + sizeof(Header)); - pngUint(srcWidth); - pngUint(srcHeight); - pngWrite(Header, sizeof(Header)); - pngUint(crc); - - pngChunk("tEXt", sizeof(Software) + strlen(options)); - pngWrite(Software, sizeof(Software)); - pngWrite(options, strlen(options)); - pngUint(crc); + pngHead(out.file, srcWidth, srcHeight, 8, PNGTruecolor); - pngChunk("sBIT", sizeof(sbit)); - pngWrite(sbit, sizeof(sbit)); - pngUint(crc); + pngChunk(out.file, "tEXt", sizeof(Software) + strlen(options)); + pngWrite(out.file, (uint8_t *)Software, sizeof(Software)); + pngWrite(out.file, (uint8_t *)options, strlen(options)); + pngInt32(out.file, ~pngCRC); - pngChunk("IDAT", deflateSize); - pngWrite(deflate, deflateSize); - pngUint(crc); + pngChunk(out.file, "sBIT", sizeof(sbit)); + pngWrite(out.file, sbit, sizeof(sbit)); + pngInt32(out.file, ~pngCRC); - pngChunk("IEND", 0); - pngUint(crc); + pngData(out.file, data, sizeof(data)); + pngTail(out.file); error = fclose(out.file); if (error) err(EX_IOERR, "%s", out.path); diff --git a/gfx/png.h b/gfx/png.h new file mode 100644 index 00000000..0df4699b --- /dev/null +++ b/gfx/png.h @@ -0,0 +1,108 @@ +/* Copyright (C) 2018 June McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +static inline uint32_t pngCRCTable(uint8_t n) { + static uint32_t table[256]; + if (table[1]) return table[n]; + for (int i = 0; i < 256; ++i) { + table[i] = i; + for (int j = 0; j < 8; ++j) { + table[i] = (table[i] >> 1) ^ (table[i] & 1 ? 0xEDB88320 : 0); + } + } + return table[n]; +} + +static uint32_t pngCRC; + +static inline void pngWrite(FILE *file, const uint8_t *ptr, uint32_t len) { + if (!fwrite(ptr, len, 1, file)) err(EX_IOERR, "pngWrite"); + for (uint32_t i = 0; i < len; ++i) { + pngCRC = pngCRCTable(pngCRC ^ ptr[i]) ^ (pngCRC >> 8); + } +} +static inline void pngInt32(FILE *file, uint32_t n) { + pngWrite(file, (uint8_t []) { n >> 24, n >> 16, n >> 8, n }, 4); +} +static inline void pngChunk(FILE *file, char type[static 4], uint32_t len) { + pngInt32(file, len); + pngCRC = ~0; + pngWrite(file, (uint8_t *)type, 4); +} + +enum { + PNGGrayscale, + PNGTruecolor = 2, + PNGIndexed, + PNGAlpha, +}; + +static inline void pngHead( + FILE *file, uint32_t width, uint32_t height, uint8_t depth, uint8_t color +) { + pngWrite(file, (uint8_t *)"\x89PNG\r\n\x1A\n", 8); + pngChunk(file, "IHDR", 13); + pngInt32(file, width); + pngInt32(file, height); + pngWrite(file, &depth, 1); + pngWrite(file, &color, 1); + pngWrite(file, (uint8_t []) { 0, 0, 0 }, 3); + pngInt32(file, ~pngCRC); +} + +static inline void pngPalette(FILE *file, const uint8_t *pal, uint32_t len) { + pngChunk(file, "PLTE", len); + pngWrite(file, pal, len); + pngInt32(file, ~pngCRC); +} + +enum { + PNGNone, + PNGSub, + PNGUp, + PNGAverage, + PNGPaeth, +}; + +static inline void pngData(FILE *file, const uint8_t *data, uint32_t len) { + uint32_t adler1 = 1, adler2 = 0; + for (uint32_t i = 0; i < len; ++i) { + adler1 = (adler1 + data[i]) % 65521; + adler2 = (adler1 + adler2) % 65521; + } + uint32_t zlen = 2 + 5 * ((len + 0xFFFE) / 0xFFFF) + len + 4; + pngChunk(file, "IDAT", zlen); + pngWrite(file, (uint8_t []) { 0x08, 0x1D }, 2); + for (; len > 0xFFFF; data += 0xFFFF, len -= 0xFFFF) { + pngWrite(file, (uint8_t []) { 0x00, 0xFF, 0xFF, 0x00, 0x00 }, 5); + pngWrite(file, data, 0xFFFF); + } + pngWrite(file, (uint8_t []) { 0x01, len, len >> 8, ~len, ~len >> 8 }, 5); + pngWrite(file, data, len); + pngInt32(file, adler2 << 16 | adler1); + pngInt32(file, ~pngCRC); +} + +static inline void pngTail(FILE *file) { + pngChunk(file, "IEND", 0); + pngInt32(file, ~pngCRC); +} -- cgit 1.4.1