aboutsummaryrefslogtreecommitdiffstats
path: root/src/blind-skip-pattern.c
blob: 8fdf57015e0a29c4dbd2665578fdcf8180c1f8e1 (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
74
75
76
77
78
79
80
81
82
/* See LICENSE file for copyright and license details. */
#include "stream.h"
#include "util.h"

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

USAGE("(skipped-frames | +included-frames) ...")

static int
process_frame(struct stream *stream, int include, size_t rown)
{
	size_t h, n;
	int anything = 0;

	for (h = stream->height; h; h--) {
		for (n = rown; n; n -= stream->ptr) {
			stream->ptr = 0;
			if (!eread_stream(stream, n))
				goto done;
			anything = 1;
			if (include)
				ewriteall(STDOUT_FILENO, stream->buf, stream->ptr, "<stdout>");
		}
	}
done:

	if (anything && h)
		eprintf("%s: is shorted than expected\n", stream->file);

	return !anything;
}

int
main(int argc, char *argv[])
{
	struct stream stream;
	int i, include;
	size_t f, n, rown, total = 0;
	char *includes;
	size_t *ns;

	UNOFLAGS(!argc);

	eopen_stream(&stream, NULL);

	includes = emalloc((size_t)argc);
	ns = ecalloc((size_t)argc, sizeof(*ns));

	for (i = 0; i < argc; i++) {
		include = argv[i][0] == '+';
		n = etozu_arg(include ? "included frame count" : "skipped frame count",
			      argv[i] + include, 0, SIZE_MAX);
		ns[i] = n;
		includes[i] = (char)include;
	}
	if (!total)
		eprintf("null pattern is not allowed");

	for (i = 0, total = 0, f = stream.frames;; i = (i + 1) % argc) {
		include = (int)includes[i];
		for (n = ns[i]; n-- && f--;)
			total += (size_t)include;
	}

	echeck_frame_size(stream.width, 1, stream.pixel_size, 0, stream.file);
	rown = stream.width * stream.pixel_size;
	stream.frames = total;
	fprint_stream_head(stdout, &stream);
	efflush(stdout, "<stdout>");

	for (i = 0;; i = (i + 1) % argc) {
		include = (int)includes[i];
		n = ns[i];
		while (n-- && process_frame(&stream, include, rown));
	}

	free(includes);
	free(ns);
	return 0;
}