aboutsummaryrefslogtreecommitdiffstats
path: root/src/blind-temporal-arithm.c
blob: f9f7b519f08090a7d1686ef388078834ab79bea0 (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
83
84
85
86
87
88
89
90
91
92
93
94
/* See LICENSE file for copyright and license details. */
#include "common.h"

USAGE("operation")

/* Because the syntax for a function returning a function pointer is disgusting. */
typedef void (*process_func)(struct stream *stream, void *image);

#define LIST_OPERATORS(PIXFMT, TYPE)\
	X(add, *img + *buf,     PIXFMT, TYPE)\
	X(mul, *img * *buf,     PIXFMT, TYPE)\
	X(min, MIN(*img, *buf), PIXFMT, TYPE)\
	X(max, MAX(*img, *buf), PIXFMT, TYPE)

#define X(NAME, ALGO, PIXFMT, TYPE)\
	static void\
	process_##PIXFMT##_##NAME(struct stream *stream, void *image)\
	{\
		TYPE *buf, *img = image;\
		size_t i, n, j = 0, m = stream->frame_size / sizeof(TYPE);\
		do {\
			n = stream->ptr / sizeof(TYPE);\
			buf = (TYPE *)(stream->buf);\
			for (i = 0; i < n; i++, buf++) {\
				*img = ALGO;\
				if (++j == m) {\
					j = 0;\
					img = image;\
				} else {\
					img++;\
				}\
			}\
			n *= sizeof(TYPE);\
			memmove(stream->buf, stream->buf + n, stream->ptr -= n);\
		} while (eread_stream(stream, SIZE_MAX));\
	}
LIST_OPERATORS(lf, double)
LIST_OPERATORS(f, float)
#undef X

static process_func
get_process_lf(const char *operation)
{
#define X(NAME, _ALGO, PIXFMT, TYPE)\
	if (!strcmp(operation, #NAME)) return process_##PIXFMT##_##NAME;
	LIST_OPERATORS(lf, double)
#undef X
	eprintf("algorithm not recognised: %s\n", operation);
	return NULL;
}

static process_func
get_process_f(const char *operation)
{
#define X(NAME, _ALGO, PIXFMT, TYPE)\
	if (!strcmp(operation, #NAME)) return process_##PIXFMT##_##NAME;
	LIST_OPERATORS(f, float)
#undef X
	eprintf("algorithm not recognised: %s\n", operation);
	return NULL;
}

int
main(int argc, char *argv[])
{
	struct stream stream;
	process_func process;
	char *img;

	UNOFLAGS(argc != 1);

	eopen_stream(&stream, NULL);

	if (stream.encoding == DOUBLE)
		process = get_process_lf(argv[0]);
	else
		process = get_process_f(argv[0]);

	echeck_dimensions(&stream, WIDTH | HEIGHT, NULL);
	img = emalloc(stream.frame_size);
	if (!eread_frame(&stream, img))
		eprintf("video has no frames\n");

	process(&stream, img);
	if (stream.ptr)
		eprintf("%s: incomplete frame\n", stream.file);

	stream.frames = 1;
	fprint_stream_head(stdout, &stream);
	efflush(stdout, "<stdout>");
	ewriteall(STDOUT_FILENO, img, stream.frame_size, "<stdout>");
	free(img);
	return 0;
}