summaryrefslogtreecommitdiff
path: root/bin/ff-catwalk.c
blob: 69b827e14c9ba3968caa9168e83d2da79a1cf391 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <endian.h>

int
main(int argc, char *argv[])
{
	if (argc != 5) {
		fprintf(stderr, "Usage: %s LATTE FRAPPE MACCHIATO MOCHA\n", argv[0]);
		return 1;
	}
	FILE *images[4];
	uint32_t headers[16];
	for (int i = 0; i < 4; i++) {
		images[i] = fopen(argv[i+1], "r");
		if (!images[i]) {
			perror(argv[i+1]);
			return 1;
		}
		if (fread(&headers + i*16, 16, 1, images[i]) != 1 || memcmp(&headers, &headers + i*16, 16)) {
			fprintf(stderr, "error: header mismatch");
			return 1;
		}
	}
	uint32_t width = be32toh(headers[2]);
	uint32_t height = be32toh(headers[3]);
	char *buf = calloc(width, 8);

	fwrite(&headers, 16, 1, stdout);
	for (uint32_t row = 0; row < height; row++) {
		for (int i = 0; i < 4; i++) {
			if (fread(buf, 8, width, images[i]) != width) {
				if (ferror(images[i])) {
					perror("fread");
				} else {
					perror("fread: unexpected EOF");
				}
				return 1;
			}
			uint32_t start = width / 4 * i + width / 8 - width / 4 * row / height;
			uint32_t end = start + width / 4;
			if (i == 0) start = 0;
			if (i == 3) end = width;
			fwrite(buf + 8 * start, 8, end - start, stdout);
		}
	}
	return 0;
}