summaryrefslogtreecommitdiff
path: root/ff2pam.c
blob: aca15cb274dc5e16b013bd58c944a00408253d2b (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* See LICENSE file for copyright and license details. */
#include <arpa/inet.h>

#include <errno.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "util.h"

int
main(int argc, char *argv[])
{
	uint32_t width, height;
	char buf[BUFSIZ];
	size_t n, t;

	argv0 = argv[0], argc--, argv++;

	if (argc) {
		fprintf(stderr, "usage: %s\n", argv0);
		return 1;
	}

	read_ff_header(&width, &height);

	/* write PAM header */
	printf("P7\n"
	       "WIDTH %" PRIu32 "\n"
	       "HEIGHT %" PRIu32 "\n"
	       "DEPTH 4\n" /* number of channels */
	       "MAXVAL 65535\n"
	       "TUPLTYPE RGB_ALPHA\n"
	       "ENDHDR\n",
	       width, height);

	/* write image */
	t = (size_t)width * (size_t)height * sizeof(uint16_t) * (sizeof("RGBA") - 1);
	for (; (n = fread(buf, 1, sizeof(buf) <= t ? sizeof(buf) : t, stdin)); ) {
		t -= n;
		fwrite(buf, 1, n, stdout);

		if (feof(stdin)) {
			break;
		}
		if (ferror(stdin)) {
			fprintf(stderr, "%s: read: %s\n", argv0, strerror(errno));
			return 1;
		}
		if (ferror(stdout)) {
			fprintf(stderr, "%s: write: %s\n", argv0, strerror(errno));
			return 1;
		}
	}

	if (t > 0) {
		fprintf(stderr, "%s: file too short\n", argv0);
		return 1;
	}

	return 0;
}