Browse Source

Use png.h and config.mk

June 1 week ago
parent
commit
ecc97ef1d4
Signed by: Curtis McEnroe <june@causal.agency> GPG Key ID: CEA2F97ADCFCD77C
5 changed files with 148 additions and 70 deletions
  1. 1
    0
      .gitignore
  2. 2
    0
      Darwin.mk
  3. 31
    29
      Makefile
  4. 6
    41
      image.c
  5. 108
    0
      png.h

+ 1
- 0
.gitignore View File

@@ -1,6 +1,7 @@
1 1
 *.o
2 2
 chroot.tar
3 3
 client
4
+config.mk
4 5
 image
5 6
 merge
6 7
 meta

+ 2
- 0
Darwin.mk View File

@@ -0,0 +1,2 @@
1
+LDFLAGS =
2
+LDLIBS = -lcurses

+ 31
- 29
Makefile View File

@@ -1,55 +1,57 @@
1 1
 CHROOT_USER = torus
2 2
 CHROOT_GROUP = $(CHROOT_USER)
3 3
 
4
-CFLAGS += -Wall -Wextra -Wpedantic
5
-LDFLAGS += -static
6
-LDLIBS = -lcursesw -lutil -lz
7
-BINS = server client image meta merge
4
+CFLAGS += -std=c11 -Wall -Wextra -Wpedantic
5
+LDFLAGS = -static
6
+LDLIBS = -lcursesw -lutil
7
+
8
+-include config.mk
9
+
10
+BINS = client image merge meta server
8 11
 OBJS = $(BINS:%=%.o)
9 12
 
10 13
 all: tags $(BINS)
11 14
 
12
-.o:
13
-	$(CC) $(LDFLAGS) $< $(LDLIBS) -o $@
14
-
15 15
 $(OBJS): torus.h
16 16
 
17 17
 client.o: help.h
18 18
 
19
-help.h:
20
-	head -c 4096 torus.dat \
21
-		| file2c -s -x 'static const uint8_t HelpData[] = {' '};' \
22
-		> help.h
23
-	echo 'static const struct Tile *Help = (const struct Tile *)HelpData;' \
24
-		>> help.h
19
+image.o: png.h
20
+
21
+.o:
22
+	$(CC) $(LDFLAGS) $< $(LDLIBS) -o $@
25 23
 
26 24
 tags: *.h *.c
27 25
 	ctags -w *.h *.c
28 26
 
29
-chroot.tar: server client
30
-	mkdir -p root
27
+chroot.tar: client server
31 28
 	install -d -o root -g wheel \
29
+		root \
32 30
 		root/bin \
33 31
 		root/home \
34
-		root/usr \
35
-		root/usr/share \
36 32
 		root/usr/share/misc \
37
-		root/var \
38 33
 		root/var/run
39 34
 	install -d -o $(CHROOT_USER) -g $(CHROOT_GROUP) root/home/$(CHROOT_USER)
40 35
 	install -d -o $(CHROOT_USER) -g $(CHROOT_GROUP) root/var/run/torus
41
-	cp -a -f /usr/share/locale root/usr/share
42
-	cp -p -f /usr/share/misc/termcap.db root/usr/share/misc
43
-	cp -p -f /rescue/sh root/bin
44
-	install -o root -g wheel -m 555 server client root/bin
45
-	tar -c -f chroot.tar -C root bin home usr var
36
+	cp -af /usr/share/locale root/usr/share
37
+	cp -fp /usr/share/misc/termcap.db root/usr/share/misc
38
+	cp -fp /rescue/sh root/bin
39
+	install client server root/bin
40
+	tar -cf chroot.tar -C root bin home usr var
41
+
42
+install: chroot.tar rc.torus
43
+	tar -xf chroot.tar -C /home/$(CHROOT_USER)
44
+	install rc.torus /usr/local/etc/rc.d/torus
46 45
 
47 46
 clean:
48
-	rm -f tags $(OBJS) $(BINS) chroot.tar
47
+	rm -f $(OBJS) $(BINS) tags root chroot.tar
49 48
 
50
-README: torus.1
51
-	mandoc torus.1 | col -b -x > README
49
+help.h:
50
+	head -c 4096 torus.dat \
51
+		| file2c -sx 'static const uint8_t HelpData[] = {' '};' \
52
+		> help.h
53
+	echo 'static const struct Tile *Help = (const struct Tile *)HelpData;' \
54
+		>> help.h
52 55
 
53
-install: chroot.tar rc.torus
54
-	tar -x -f chroot.tar -C /home/$(CHROOT_USER)
55
-	install rc.torus /usr/local/etc/rc.d/torus
56
+README: torus.1
57
+	mandoc torus.1 | col -bx > README

+ 6
- 41
image.c View File

@@ -14,7 +14,6 @@
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 <fcntl.h>
20 19
 #include <stdint.h>
@@ -25,8 +24,8 @@
25 24
 #include <sys/stat.h>
26 25
 #include <sysexits.h>
27 26
 #include <unistd.h>
28
-#include <zlib.h>
29 27
 
28
+#include "png.h"
30 29
 #include "torus.h"
31 30
 
32 31
 static const uint8_t Palette[16][3] = {
@@ -106,40 +105,15 @@ static void tilesMap(const char *path) {
106 105
 	if (error) err(EX_OSERR, "madvise");
107 106
 }
108 107
 
109
-static uint32_t crc;
110
-static void pngWrite(const void *ptr, size_t size) {
111
-	fwrite(ptr, size, 1, stdout);
112
-	if (ferror(stdout)) err(EX_IOERR, "(stdout)");
113
-	crc = crc32(crc, ptr, size);
114
-}
115
-static void pngInt(uint32_t host) {
116
-	uint32_t net = htonl(host);
117
-	pngWrite(&net, 4);
118
-}
119
-static void pngChunk(char type[static 4], uint32_t size) {
120
-	pngInt(size);
121
-	crc = crc32(0, Z_NULL, 0);
122
-	pngWrite(type, 4);
123
-}
124
-
125 108
 static void render(uint32_t tileX, uint32_t tileY) {
126 109
 	uint32_t width = CellCols * font.glyph.width;
127 110
 	uint32_t height = CellRows * font.glyph.height;
128 111
 
129
-	pngWrite("\x89PNG\r\n\x1A\n", 8);
130
-
131
-	pngChunk("IHDR", 13);
132
-	pngInt(width);
133
-	pngInt(height);
134
-	pngWrite("\x08\x03\x00\x00\x00", 5);
135
-	pngInt(crc);
136
-
137
-	pngChunk("PLTE", sizeof(Palette));
138
-	pngWrite(Palette, sizeof(Palette));
139
-	pngInt(crc);
112
+	pngHead(stdout, width, height, 8, PNGIndexed);
113
+	pngPalette(stdout, (uint8_t *)Palette, sizeof(Palette));
140 114
 
141 115
 	uint8_t data[height][1 + width];
142
-	memset(data, 0, sizeof(data));
116
+	memset(data, PNGNone, sizeof(data));
143 117
 
144 118
 	uint32_t widthBytes = (font.glyph.width + 7) / 8;
145 119
 	uint8_t (*bits)[font.glyph.len][font.glyph.height][widthBytes];
@@ -163,17 +137,8 @@ static void render(uint32_t tileX, uint32_t tileY) {
163 137
 		}
164 138
 	}
165 139
 
166
-	uLong size = compressBound(sizeof(data));
167
-	uint8_t deflate[size];
168
-	int error = compress(deflate, &size, (Byte *)data, sizeof(data));
169
-	if (error != Z_OK) errx(EX_SOFTWARE, "compress: %d", error);
170
-
171
-	pngChunk("IDAT", size);
172
-	pngWrite(deflate, size);
173
-	pngInt(crc);
174
-
175
-	pngChunk("IEND", 0);
176
-	pngInt(crc);
140
+	pngData(stdout, (uint8_t *)data, sizeof(data));
141
+	pngTail(stdout);
177 142
 }
178 143
 
179 144
 int main(int argc, char *argv[]) {

+ 108
- 0
png.h View File

@@ -0,0 +1,108 @@
1
+/* Copyright (C) 2018  Curtis McEnroe <june@causal.agency>
2
+ *
3
+ * This program is free software: you can redistribute it and/or modify
4
+ * it under the terms of the GNU Affero General Public License as published by
5
+ * the Free Software Foundation, either version 3 of the License, or
6
+ * (at your option) any later version.
7
+ *
8
+ * This program is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+ * GNU Affero General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Affero General Public License
14
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
+ */
16
+
17
+#include <err.h>
18
+#include <stdint.h>
19
+#include <stdio.h>
20
+#include <stdlib.h>
21
+#include <sysexits.h>
22
+
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
+}
34
+
35
+static uint32_t pngCRC;
36
+
37
+static inline void pngWrite(FILE *file, const uint8_t *ptr, uint32_t len) {
38
+	if (!fwrite(ptr, len, 1, file)) err(EX_IOERR, "pngWrite");
39
+	for (uint32_t i = 0; i < len; ++i) {
40
+		pngCRC = pngCRCTable(pngCRC ^ ptr[i]) ^ (pngCRC >> 8);
41
+	}
42
+}
43
+static inline void pngInt32(FILE *file, uint32_t n) {
44
+	pngWrite(file, (uint8_t []) { n >> 24, n >> 16, n >> 8, n }, 4);
45
+}
46
+static inline void pngChunk(FILE *file, char type[static 4], uint32_t len) {
47
+	pngInt32(file, len);
48
+	pngCRC = ~0;
49
+	pngWrite(file, (uint8_t *)type, 4);
50
+}
51
+
52
+enum {
53
+	PNGGrayscale,
54
+	PNGTruecolor = 2,
55
+	PNGIndexed,
56
+	PNGAlpha,
57
+};
58
+
59
+static inline void pngHead(
60
+	FILE *file, uint32_t width, uint32_t height, uint8_t depth, uint8_t color
61
+) {
62
+	pngWrite(file, (uint8_t *)"\x89PNG\r\n\x1A\n", 8);
63
+	pngChunk(file, "IHDR", 13);
64
+	pngInt32(file, width);
65
+	pngInt32(file, height);
66
+	pngWrite(file, &depth, 1);
67
+	pngWrite(file, &color, 1);
68
+	pngWrite(file, (uint8_t []) { 0, 0, 0 }, 3);
69
+	pngInt32(file, ~pngCRC);
70
+}
71
+
72
+static inline void pngPalette(FILE *file, const uint8_t *pal, uint32_t len) {
73
+	pngChunk(file, "PLTE", len);
74
+	pngWrite(file, pal, len);
75
+	pngInt32(file, ~pngCRC);
76
+}
77
+
78
+enum {
79
+	PNGNone,
80
+	PNGSub,
81
+	PNGUp,
82
+	PNGAverage,
83
+	PNGPaeth,
84
+};
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;
93
+	pngChunk(file, "IDAT", zlen);
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);
103
+}
104
+
105
+static inline void pngTail(FILE *file) {
106
+	pngChunk(file, "IEND", 0);
107
+	pngInt32(file, ~pngCRC);
108
+}

Loading…
Cancel
Save