#include #include #include #include #include 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; }