From 021c78cccbeb286150bdc91dd1b7c4bd1aa727e2 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sat, 17 Feb 2018 12:05:38 -0500 Subject: Implement filter heuristic from PNG spec --- bin/pngo.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'bin') diff --git a/bin/pngo.c b/bin/pngo.c index bd39d6bd..c426b34e 100644 --- a/bin/pngo.c +++ b/bin/pngo.c @@ -97,13 +97,13 @@ struct PACKED Header { }; static uint8_t bytesPerPixel(struct Header header) { - assert(header.depth == 8); + assert(header.depth >= 8); switch (header.color) { - case GRAYSCALE: return 1; - case TRUECOLOR: return 3; - case INDEXED: return 1; - case GRAYSCALE_ALPHA: return 2; - case TRUECOLOR_ALPHA: return 4; + case GRAYSCALE: return 1 * header.depth / 8; + case TRUECOLOR: return 3 * header.depth / 8; + case INDEXED: return 1 * header.depth / 8; + case GRAYSCALE_ALPHA: return 2 * header.depth / 8; + case TRUECOLOR_ALPHA: return 4 * header.depth / 8; } } @@ -220,6 +220,7 @@ enum PACKED FilterType { FILT_AVERAGE, FILT_PAETH, }; +#define FILT__COUNT (FILT_PAETH + 1) struct FilterBytes { uint8_t x; @@ -230,9 +231,9 @@ struct FilterBytes { static uint8_t paethPredictor(struct FilterBytes f) { int32_t p = (int32_t)f.a + (int32_t)f.b - (int32_t)f.c; - int32_t pa = labs(p - (int32_t)f.a); - int32_t pb = labs(p - (int32_t)f.b); - int32_t pc = labs(p - (int32_t)f.c); + int32_t pa = abs(p - (int32_t)f.a); + int32_t pb = abs(p - (int32_t)f.b); + int32_t pc = abs(p - (int32_t)f.c); if (pa <= pb && pa <= pc) return f.a; if (pb <= pc) return f.b; return f.c; @@ -273,7 +274,7 @@ static struct Scanline *scanlines( for (uint32_t y = 0; y < header.height; ++y) { lines[y].type = &data[y * stride]; lines[y].data = &data[y * stride + 1]; - if (*lines[y].type > FILT_PAETH) { + if (*lines[y].type >= FILT__COUNT) { errx(EX_DATAERR, "%s: invalid filter type %hhu", path, *lines[y].type); } } @@ -307,11 +308,18 @@ static void reconData(struct Header header, const struct Scanline *lines) { static void filterData(struct Header header, const struct Scanline *lines) { uint8_t bpp = bytesPerPixel(header); for (uint32_t y = header.height - 1; y < header.height; --y) { - // TODO: Filter type heuristic. - *lines[y].type = FILT_PAETH; - for (uint32_t i = (bpp * header.width) - 1; i < bpp * header.width; --i) { - lines[y].data[i] = filt(*lines[y].type, filterBytes(lines, bpp, y, i)); + uint8_t filter[FILT__COUNT][bpp * header.width]; + uint32_t heuristic[FILT__COUNT] = { 0 }; + enum FilterType minType = FILT_NONE; + for (enum FilterType type = FILT_NONE; type < FILT__COUNT; ++type) { + for (uint32_t i = 0; i < bpp * header.width; ++i) { + filter[type][i] = filt(type, filterBytes(lines, bpp, y, i)); + heuristic[type] += abs((int8_t)filter[type][i]); + } + if (heuristic[type] < heuristic[minType]) minType = type; } + *lines[y].type = minType; + memcpy(lines[y].data, filter[minType], bpp * header.width); } } @@ -380,7 +388,7 @@ int main(int argc, char *argv[]) { path, header.interlace ); } - if (header.depth != 8) { + if (header.depth < 8) { errx(EX_CONFIG, "%s: unsupported bit depth %hhu", path, header.depth); } -- cgit 1.4.1 e Suffers a little bit from middle book but I really enjoyed it. Read it faster than the first one too, despite its length. 2022-06-25Bump xterm font size to 12June McEnroe 2022-06-10Handle subshells (and functions) inside substitutionsJune McEnroe 2022-06-10Switch to jorts Install scriptJune McEnroe 2022-06-08Indicate if still reading or no resultsJune McEnroe 2022-06-08Add Maiden, Mother, CroneJune McEnroe Mixed bag like most collections of short stories. Some of them are pretty good. The author of the worst written story also has the worst written bio. 2022-06-05FIRST SHOW IN 2.5 YEARS BABEY!!!June McEnroe 2022-06-03Set line number on File linesJune McEnroe 2022-06-03Stop polling stdin after EOFJune McEnroe 2022-06-02Set TABSIZE=4June McEnroe Absolutely indiscriminately. 2022-06-02Do basic match highlightingJune McEnroe 2022-06-02Clean up parsing a littleJune McEnroe 2022-06-02Don't duplicate path stringJune McEnroe 2022-06-02Use stderr instead of /dev/tty, realloc buffer if lines too longJune McEnroe For some reason I haven't been able to figure out, trying to poll /dev/tty returns POLLNVAL (and this was using 100% CPU looping), but using stderr instead works fine. 2022-06-02Add initial working version of qfJune McEnroe 2022-05-29Set prompt for okshJune McEnroe