aboutsummaryrefslogtreecommitdiffstats
path: root/src/blind-round-wave.c
blob: 00a30e1f59bc240686e0607239d06c5d1fd315f3 (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
/* See LICENSE file for copyright and license details. */
#include "common.h"

USAGE("[-e]")

static int equal = 0;


#define PROCESS(TYPE, SUFFIX)\
	static void\
	process_##SUFFIX(struct stream *stream)\
	{\
		size_t i, n;\
		TYPE x, y, z, a;\
		do {\
			n = stream->ptr / stream->pixel_size;\
			if (equal) {\
				for (i = 0; i < n; i++) {\
					a = ((TYPE *)(stream->buf))[4 * i + 3];\
					a = mod(a + 1, (TYPE)4) - 1;\
					a = a < 1 ? 1 - a * a / 2 : (a - 2) * (a - 2) / 2;\
					((TYPE *)(stream->buf))[4 * i + 0] = a;\
					((TYPE *)(stream->buf))[4 * i + 1] = a;\
					((TYPE *)(stream->buf))[4 * i + 2] = a;\
					((TYPE *)(stream->buf))[4 * i + 3] = a;\
				}\
			} else {\
				for (i = 0; i < n; i++) {\
					x = ((TYPE *)(stream->buf))[4 * i + 0];\
					y = ((TYPE *)(stream->buf))[4 * i + 1];\
					z = ((TYPE *)(stream->buf))[4 * i + 2];\
					a = ((TYPE *)(stream->buf))[4 * i + 3];\
					x = mod(x + 1, (TYPE)4) - 1;\
					y = mod(y + 1, (TYPE)4) - 1;\
					z = mod(z + 1, (TYPE)4) - 1;\
					a = mod(a + 1, (TYPE)4) - 1;\
					x = x < 1 ? 1 - x * x / 2 : (x - 2) * (x - 2) / 2;\
					y = y < 1 ? 1 - y * y / 2 : (y - 2) * (y - 2) / 2;\
					z = z < 1 ? 1 - z * z / 2 : (z - 2) * (z - 2) / 2;\
					a = a < 1 ? 1 - a * a / 2 : (a - 2) * (a - 2) / 2;\
					((TYPE *)(stream->buf))[4 * i + 0] = x;\
					((TYPE *)(stream->buf))[4 * i + 1] = y;\
					((TYPE *)(stream->buf))[4 * i + 2] = z;\
					((TYPE *)(stream->buf))[4 * i + 3] = a;\
				}\
			}\
			n *= stream->pixel_size;\
			ewriteall(STDOUT_FILENO, stream->buf, n, "<stdout>");\
			memmove(stream->buf, stream->buf + n, stream->ptr -= n);\
		} while (eread_stream(stream, SIZE_MAX));\
		if (stream->ptr)\
			eprintf("%s: incomplete frame\n", stream->file);\
	}

PROCESS(double, lf)
PROCESS(float, f)


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

	ARGBEGIN {
	case 'e':
		equal = 1;
		break;
	default:
		usage();
	} ARGEND;

	if (argc)
		usage();

	eopen_stream(&stream, NULL);

	if (!strcmp(stream.pixfmt, "xyza"))
		process = process_lf;
	else if (!strcmp(stream.pixfmt, "xyza f"))
		process = process_f;
	else
		eprintf("pixel format %s is not supported, try xyza\n", stream.pixfmt);

	fprint_stream_head(stdout, &stream);
	efflush(stdout, "<stdout>");
	process(&stream);
	return 0;
}