aboutsummaryrefslogtreecommitdiffstats
path: root/src/blind-next-frame.c
blob: bd47f1d15d7cc147f6c6add34001665ad457e37b (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
68
69
70
71
72
73
/* See LICENSE file for copyright and license details. */
#include "stream.h"
#include "util.h"

#include <inttypes.h>
#include <string.h>
#include <unistd.h>

USAGE("[-f frames] width height pixel-format ...")

int
main(int argc, char *argv[])
{
	struct stream stream;
	size_t n, w, h = 0;
	int i, anything = 0;
	char *p;

	stream.frames = 1;

	ARGBEGIN {
	case 'f':
		stream.frames = etozu_flag('f', UARGF(), 1, SIZE_MAX);
		break;
	default:
		usage();
	} ARGEND;

	if (argc < 3)
		usage();

	stream.fd = STDIN_FILENO;
	stream.file = "<stdin>";
	stream.pixfmt[0] = '\0';

	stream.width  = entozu_arg(2, "the width",  argv[0], 1, SIZE_MAX);
	stream.height = entozu_arg(2, "the height", argv[1], 1, SIZE_MAX);
	argv += 2, argc -= 2;

	n = (size_t)argc - 1;
	for (i = 0; i < argc; i++)
		n += strlen(argv[i]);
	if (n < sizeof(stream.pixfmt)) {
		p = stpcpy(stream.pixfmt, argv[0]);
		for (i = 1; i < argc; i++) {
			*p++ = ' ';
			p = stpcpy(p, argv[i]);
		}
	}

	enset_pixel_size(2, &stream);

	fprint_stream_head(stdout, &stream);
	enfflush(2, stdout, "<stdout>");

	w = stream.width * stream.pixel_size;
	for (; stream.frames; stream.frames--) {
		for (h = stream.height; h; h--) {
			for (n = w; n; n -= stream.ptr, stream.ptr = 0) {
				if (!stream.ptr && !enread_stream(2, &stream, n))
					goto done;
				anything = 1;
				enwriteall(2, STDOUT_FILENO, stream.buf, stream.ptr, "<stdout>");
			}
		}
	}
done:

	if (anything && stream.frames)
		enprintf(2, "%s: is shorted than expected\n", stream.file);

	return !anything;
}