summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--bin/pngo.c85
1 files changed, 49 insertions, 36 deletions
diff --git a/bin/pngo.c b/bin/pngo.c
index 5ee9ea91..59a4afaa 100644
--- a/bin/pngo.c
+++ b/bin/pngo.c
@@ -172,15 +172,11 @@ static void printHeader(void) {
 	);
 }
 
-static void readHeader(void) {
-	struct Chunk ihdr = readChunk();
-	if (0 != memcmp(ihdr.type, "IHDR", 4)) {
-		errx(EX_DATAERR, "%s: expected IHDR, found %s", path, typeStr(ihdr));
-	}
-	if (ihdr.size != sizeof(header)) {
+static void readHeader(struct Chunk chunk) {
+	if (chunk.size != sizeof(header)) {
 		errx(
 			EX_DATAERR, "%s: expected IHDR size %zu, found %u",
-			path, sizeof(header), ihdr.size
+			path, sizeof(header), chunk.size
 		);
 	}
 	readExpect(&header, sizeof(header), "header");
@@ -254,6 +250,12 @@ static struct {
 	uint8_t notAlpha[256];
 } trans;
 
+static void paletteClear(void) {
+	palette.len = 0;
+	trans.len = 0;
+	memset(trans.notAlpha, 0, sizeof(trans.notAlpha));
+}
+
 static uint32_t paletteIndex(bool alpha, const uint8_t *rgba) {
 	uint32_t i;
 	for (i = 0; i < palette.len; ++i) {
@@ -300,13 +302,7 @@ static void transCompact(void) {
 	trans.len = i;
 }
 
-static void readPalette(void) {
-	struct Chunk chunk;
-	for (;;) {
-		chunk = readChunk();
-		if (0 == memcmp(chunk.type, "PLTE", 4)) break;
-		skipChunk(chunk);
-	}
+static void readPalette(struct Chunk chunk) {
 	if (chunk.size % 3) {
 		errx(EX_DATAERR, "%s: PLTE size %u not divisible by 3", path, chunk.size);
 	}
@@ -360,7 +356,7 @@ static void allocData(void) {
 	if (!data) err(EX_OSERR, "malloc(%zu)", dataSize());
 }
 
-static void readData(void) {
+static void readData(struct Chunk chunk) {
 	if (verbose) fprintf(stderr, "%s: data size %zu\n", path, dataSize());
 
 	struct z_stream_s stream = { .next_out = data, .avail_out = dataSize() };
@@ -368,29 +364,27 @@ static void readData(void) {
 	if (error != Z_OK) errx(EX_SOFTWARE, "%s: inflateInit: %s", path, stream.msg);
 
 	for (;;) {
-		struct Chunk chunk = readChunk();
-		if (0 == memcmp(chunk.type, "IDAT", 4)) {
-			uint8_t *idat = malloc(chunk.size);
-			if (!idat) err(EX_OSERR, "malloc");
+		if (0 != memcmp(chunk.type, "IDAT", 4)) {
+			errx(EX_DATAERR, "%s: missing IDAT chunk", path);
+		}
 
-			readExpect(idat, chunk.size, "image data");
-			readCrc();
+		uint8_t *idat = malloc(chunk.size);
+		if (!idat) err(EX_OSERR, "malloc");
 
-			stream.next_in = idat;
-			stream.avail_in = chunk.size;
-			int error = inflate(&stream, Z_SYNC_FLUSH);
-			free(idat);
+		readExpect(idat, chunk.size, "image data");
+		readCrc();
 
-			if (error == Z_STREAM_END) break;
-			if (error != Z_OK) errx(EX_DATAERR, "%s: inflate: %s", path, stream.msg);
+		stream.next_in = idat;
+		stream.avail_in = chunk.size;
+		int error = inflate(&stream, Z_SYNC_FLUSH);
+		free(idat);
 
-		} else if (0 == memcmp(chunk.type, "tRNS", 4)) {
-			readTrans(chunk);
-		} else if (0 == memcmp(chunk.type, "IEND", 4)) {
-			errx(EX_DATAERR, "%s: missing IDAT chunk", path);
-		} else {
-			skipChunk(chunk);
+		if (error == Z_STREAM_END) break;
+		if (error != Z_OK) {
+			errx(EX_DATAERR, "%s: inflate: %s", path, stream.msg);
 		}
+
+		chunk = readChunk();
 	}
 
 	inflateEnd(&stream);
@@ -724,16 +718,35 @@ static void optimize(const char *inPath, const char *outPath) {
 	}
 
 	readSignature();
-	readHeader();
+	struct Chunk ihdr = readChunk();
+	if (0 != memcmp(ihdr.type, "IHDR", 4)) {
+		errx(EX_DATAERR, "%s: expected IHDR, found %s", path, typeStr(ihdr));
+	}
+	readHeader(ihdr);
 	if (header.interlace != Progressive) {
 		errx(
 			EX_CONFIG, "%s: unsupported interlace method %hhu",
 			path, header.interlace
 		);
 	}
-	if (header.color == Indexed) readPalette();
+
+	paletteClear();
 	allocData();
-	readData();
+	for (;;) {
+		struct Chunk chunk = readChunk();
+		if (0 == memcmp(chunk.type, "PLTE", 4)) {
+			readPalette(chunk);
+		} else if (0 == memcmp(chunk.type, "tRNS", 4)) {
+			readTrans(chunk);
+		} else if (0 == memcmp(chunk.type, "IDAT", 4)) {
+			readData(chunk);
+		} else if (0 != memcmp(chunk.type, "IEND", 4)) {
+			skipChunk(chunk);
+		} else {
+			break;
+		}
+	}
+
 	fclose(file);
 
 	allocLines();
0'>2021-01-12Don't output a pre in hilex by defaultJune McEnroe 2021-01-12Move hilex out of hilex directoryJune McEnroe 2021-01-12Consolidate hilex formatters into hilex.cJune McEnroe 2021-01-12Remove hacky tagging from hilexJune McEnroe God that makes the lexers so much simpler. 2021-01-12Add htagml -iJune McEnroe 2021-01-12Render tag index in HTMLJune McEnroe 2021-01-12Add htagml -xJune McEnroe 2021-01-12Prevent matching the same tag twiceJune McEnroe 2021-01-12Process htagml file line by lineJune McEnroe This simplifies some things, adds support for line number tag definitions, and should enable combining htagml with other preprocessors in the future. 2021-01-12Split fields by tab onlyJune McEnroe Also don't fail hard on non-forward-search definitions. 2021-01-12List both Makefile and html.sh under README.7June McEnroe 2021-01-12Add htagml exampleJune McEnroe 2021-01-12Use mandoc and htagml for bin htmlJune McEnroe 2021-01-12Add htagmlJune McEnroe 2021-01-12Replace causal.agency with a simple mdoc pageJune McEnroe 2021-01-11Publish "Using vi"June McEnroe 2021-01-11Enable diff.colorMovedJune McEnroe 2021-01-10Set less search case-insensitiveJune McEnroe 2021-01-10Set EXINITJune McEnroe neovim is laggy as hell in my OpenBSD VM, so I switched to vi so I could type without getting frustrated. 2021-01-09Add c -t flag to print expression typeJune McEnroe Also add missing float case. 2021-01-05Update taglineJune McEnroe