aboutsummaryrefslogtreecommitdiffstats
path: root/src/vu-frame-to-image.c
blob: e4466cffeae604792e8e49a89d28c37df51537d4 (plain) (blame)
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
65
66
67
/* See LICENSE file for copyright and license details. */
#include "arg.h"
#include "util.h"

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static void
usage(void)
{
	eprintf("usage: %s -w width -h height\n", argv0);
}

int
main(int argc, char *argv[])
{
	size_t width = 0, height = 0;
	size_t p, n;
	ssize_t r;
	char buf[8096];

	ARGBEGIN {
	case 'w':
		if (tozu(EARGF(usage()), 1, SIZE_MAX, &width))
			eprintf("argument of -w must be an integer in [1, %zu]\n", SIZE_MAX);
		break;
	case 'h':
		if (tozu(EARGF(usage()), 1, SIZE_MAX, &height))
			eprintf("argument of -h must be an integer in [1, %zu]\n", SIZE_MAX);
		break;
	default:
		usage();
	} ARGEND;

	if (!width || !height || argc)
		usage();

	printf("P7\n"
	       "WIDTH %zu\n"
	       "HEIGHT %zu\n"
	       "DEPTH 4\n"
	       "MAXVAL 255\n"
	       "TUPLTYPE RGB_ALPHA\n"
	       "ENDHDR\n", width, height);
	fflush(stdout);
	if (ferror(stdout))
		eprintf("<stdout>:");

	for (n = 0;;) {
		for (p = 0; p < n;) {
			r = write(STDOUT_FILENO, buf + p, n - p);
			if (r < 0)
				eprintf("write <stdin>:");
			p += (size_t)r;
		}
		r = read(STDIN_FILENO, buf, sizeof(buf));
		if (r < 0)
			 eprintf("read <stdin>:");
		if (r == 0)
			break;
		n = (size_t)r;
	}

	return 0;
}