Browse Source

Remove zlib dependency from png.h

June 4 weeks ago
parent
commit
ca1aea390b
Signed by: Curtis McEnroe <june@causal.agency> GPG Key ID: CEA2F97ADCFCD77C
1 changed files with 54 additions and 38 deletions
  1. 54
    38
      bin/png.h

+ 54
- 38
bin/png.h View File

@@ -14,79 +14,95 @@
14 14
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 15
  */
16 16
 
17
-#include <arpa/inet.h>
18 17
 #include <err.h>
19 18
 #include <stdint.h>
20 19
 #include <stdio.h>
21 20
 #include <stdlib.h>
22 21
 #include <sysexits.h>
23
-#include <zlib.h>
24 22
 
25
-enum {
26
-	PNGGrayscale,
27
-	PNGTruecolor = 2,
28
-	PNGIndexed,
29
-	PNGAlpha,
30
-};
31
-
32
-enum {
33
-	PNGNone,
34
-	PNGSub,
35
-	PNGUp,
36
-	PNGAverage,
37
-	PNGPaeth,
38
-};
23
+static inline uint32_t pngCRCTable(uint8_t n) {
24
+	static uint32_t table[256];
25
+	if (table[1]) return table[n];
26
+	for (int i = 0; i < 256; ++i) {
27
+		table[i] = i;
28
+		for (int j = 0; j < 8; ++j) {
29
+			table[i] = (table[i] >> 1) ^ (table[i] & 1 ? 0xEDB88320 : 0);
30
+		}
31
+	}
32
+	return table[n];
33
+}
39 34
 
40 35
 static uint32_t pngCRC;
41
-static inline void pngWrite(FILE *file, const void *ptr, size_t len) {
36
+
37
+static inline void pngWrite(FILE *file, const uint8_t *ptr, uint32_t len) {
42 38
 	if (!fwrite(ptr, len, 1, file)) err(EX_IOERR, "pngWrite");
43
-	pngCRC = crc32(pngCRC, ptr, len);
39
+	for (uint32_t i = 0; i < len; ++i) {
40
+		pngCRC = pngCRCTable(pngCRC ^ ptr[i]) ^ (pngCRC >> 8);
41
+	}
44 42
 }
45 43
 static inline void pngInt32(FILE *file, uint32_t n) {
46
-	n = htonl(n);
47
-	pngWrite(file, &n, 4);
44
+	pngWrite(file, (uint8_t []) { n >> 24, n >> 16, n >> 8, n }, 4);
48 45
 }
49 46
 static inline void pngChunk(FILE *file, char type[static 4], uint32_t len) {
50 47
 	pngInt32(file, len);
51
-	pngCRC = crc32(0, Z_NULL, 0);
52
-	pngWrite(file, type, 4);
48
+	pngCRC = ~0;
49
+	pngWrite(file, (uint8_t *)type, 4);
53 50
 }
54 51
 
52
+enum {
53
+	PNGGrayscale,
54
+	PNGTruecolor = 2,
55
+	PNGIndexed,
56
+	PNGAlpha,
57
+};
58
+
55 59
 static inline void pngHead(
56 60
 	FILE *file, uint32_t width, uint32_t height, uint8_t depth, uint8_t color
57 61
 ) {
58
-	pngWrite(file, "\x89PNG\r\n\x1A\n", 8);
62
+	pngWrite(file, (uint8_t *)"\x89PNG\r\n\x1A\n", 8);
59 63
 	pngChunk(file, "IHDR", 13);
60 64
 	pngInt32(file, width);
61 65
 	pngInt32(file, height);
62 66
 	pngWrite(file, &depth, 1);
63 67
 	pngWrite(file, &color, 1);
64
-	pngWrite(file, "\0\0\0", 3);
65
-	pngInt32(file, pngCRC);
68
+	pngWrite(file, (uint8_t []) { 0, 0, 0 }, 3);
69
+	pngInt32(file, ~pngCRC);
66 70
 }
67 71
 
68 72
 static inline void pngPalette(FILE *file, const uint8_t *pal, uint32_t len) {
69 73
 	pngChunk(file, "PLTE", len);
70 74
 	pngWrite(file, pal, len);
71
-	pngInt32(file, pngCRC);
75
+	pngInt32(file, ~pngCRC);
72 76
 }
73 77
 
74
-static inline void pngData(FILE *file, const uint8_t *data, uint32_t len) {
75
-	uLong zlen = compressBound(len);
76
-	uint8_t *zdata = malloc(zlen);
77
-	if (!zlen) err(EX_OSERR, "malloc");
78
-
79
-	int error = compress(zdata, &zlen, data, len);
80
-	if (error != Z_OK) errx(EX_SOFTWARE, "compress: %d", error);
78
+enum {
79
+	PNGNone,
80
+	PNGSub,
81
+	PNGUp,
82
+	PNGAverage,
83
+	PNGPaeth,
84
+};
81 85
 
86
+static inline void pngData(FILE *file, const uint8_t *data, uint32_t len) {
87
+	uint32_t adler1 = 1, adler2 = 0;
88
+	for (uint32_t i = 0; i < len; ++i) {
89
+		adler1 = (adler1 + data[i]) % 65521;
90
+		adler2 = (adler1 + adler2) % 65521;
91
+	}
92
+	uint32_t zlen = 2 + 5 * ((len + 0xFFFE) / 0xFFFF) + len + 4;
82 93
 	pngChunk(file, "IDAT", zlen);
83
-	pngWrite(file, zdata, zlen);
84
-	pngInt32(file, pngCRC);
85
-
86
-	free(zdata);
94
+	pngWrite(file, (uint8_t []) { 0x08, 0x1D }, 2);
95
+	for (; len > 0xFFFF; data += 0xFFFF, len -= 0xFFFF) {
96
+		pngWrite(file, (uint8_t []) { 0x00, 0xFF, 0xFF, 0x00, 0x00 }, 5);
97
+		pngWrite(file, data, 0xFFFF);
98
+	}
99
+	pngWrite(file, (uint8_t []) { 0x01, len, len >> 8, ~len, ~len >> 8 }, 5);
100
+	pngWrite(file, data, len);
101
+	pngInt32(file, adler2 << 16 | adler1);
102
+	pngInt32(file, ~pngCRC);
87 103
 }
88 104
 
89 105
 static inline void pngTail(FILE *file) {
90 106
 	pngChunk(file, "IEND", 0);
91
-	pngInt32(file, pngCRC);
107
+	pngInt32(file, ~pngCRC);
92 108
 }

Loading…
Cancel
Save