commit af50be4d25c2e20eb12440aea13f222545a9793e
parent 6a0fbbf686b592065bccf2a319f6d450df5f91f1
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sun, 5 Nov 2017 13:41:33 +0100
ff.c: farbeld support
detect PNG or farbfeld depending on the first byte:
ff first byte is 'f'
PNG first byte is 137
Diffstat:
5 files changed, 56 insertions(+), 4 deletions(-)
diff --git a/Makefile b/Makefile
@@ -6,7 +6,7 @@ MANPREFIX = $(PREFIX)/man
CPPFLAGS = -I/usr/local/include
CFLAGS = -Wall -O3
LDFLAGS = -L/usr/local/lib -lpng
-OBJ = colors.o png.o
+OBJ = colors.o ff.o png.o
BIN = colors
all: $(BIN)
@@ -15,6 +15,7 @@ $(BIN): $(OBJ)
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $(OBJ) $(LDFLAGS)
colors.o: arg.h colors.h tree.h
+ff.o: colors.h
png.o: colors.h
install: all
diff --git a/colors.c b/colors.c
@@ -319,6 +319,7 @@ int
main(int argc, char *argv[])
{
char *e;
+ int c;
ARGBEGIN {
case 'e':
@@ -351,8 +352,12 @@ main(int argc, char *argv[])
if (argc != 0)
usage();
+ if ((c = getc(stdin)) == EOF || ungetc(c, stdin) == EOF)
+ return 1;
+
RB_INIT(&pointhead);
- parseimg(stdin, fillpoints);
+
+ (c == 'f' ? parseimg_ff : parseimg_png)(stdin, fillpoints);
initcluster = initcluster_greyscale;
initspace = 256;
diff --git a/colors.h b/colors.h
@@ -1,2 +1,3 @@
/* See LICENSE file for copyright and license details. */
-void parseimg(FILE *, void (*)(int, int, int));
+void parseimg_ff(FILE *, void (*)(int, int, int));
+void parseimg_png(FILE *, void (*)(int, int, int));
diff --git a/ff.c b/ff.c
@@ -0,0 +1,45 @@
+/* See LICENSE file for copyright and license details. */
+#include <arpa/inet.h>
+#include <sys/types.h>
+
+#include <err.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "colors.h"
+
+void
+parseimg_ff(FILE *fp, void (*fn)(int, int, int))
+{
+ uint32_t hdr[4], width, height;
+ uint16_t *row;
+ size_t rowlen, i, j;
+
+ if (fread(hdr, sizeof(*hdr), 4, fp) != 4)
+ err(1, "fread");
+ if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1))
+ errx(1, "invalid magic value");
+
+ width = ntohl(hdr[2]);
+ height = ntohl(hdr[3]);
+
+ if (!(row = reallocarray(NULL, width, (sizeof("RGBA") - 1) * sizeof(uint16_t))))
+ err(1, "reallocarray");
+ rowlen = width * (sizeof("RGBA") - 1);
+
+ for (i = 0; i < height; ++i) {
+ if (fread(row, sizeof(uint16_t), rowlen, fp) != rowlen) {
+ if (ferror(fp))
+ err(1, "fread");
+ else
+ errx(1, "unexpected end of file");
+ }
+ for (j = 0; j < rowlen; j += 4) {
+ if (!row[j + 3])
+ continue;
+ fn(row[j] / 257, row[j+1] / 257, row[j+2] / 257);
+ }
+ }
+}
diff --git a/png.c b/png.c
@@ -6,7 +6,7 @@
#include "colors.h"
void
-parseimg(FILE *fp, void (*fn)(int, int, int))
+parseimg_png(FILE *fp, void (*fn)(int, int, int))
{
png_structp png_struct_p;
png_infop png_info_p;