aboutsummaryrefslogtreecommitdiffstats
path: root/src/blind-radial-gradient.c
blob: a97e474b3e38ed51f1400dc9ca657f3452668e29 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* See LICENSE file for copyright and license details. */
#ifndef TYPE
#include "common.h"

USAGE("-w width -h height")

static size_t width = 0;
static size_t height = 0;
static int with_params;

#define FILE "blind-radial-gradient.c"
#include "define-functions.h"

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

	ARGBEGIN {
	case 'w':
		width = etozu_flag('w', UARGF(), 1, SIZE_MAX);
		break;
	case 'h':
		height = etozu_flag('h', UARGF(), 1, SIZE_MAX);
		break;
	default:
		usage();
	} ARGEND;

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

	eopen_stream(&stream, NULL);

	SELECT_PROCESS_FUNCTION(&stream);

	if (stream.width > 3 || stream.height > 3 ||
	    stream.width * stream.height < 2 ||
	    stream.width * stream.height > 3)
		eprintf("<stdin>: each frame must contain exactly 2 pixels\n");

	with_params = stream.width * stream.height == 3;

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

#else

static void
PROCESS(struct stream *stream)
{
	typedef TYPE pixel_t[4];
	pixel_t buf[BUFSIZ / sizeof(pixel_t)];
	TYPE *params, x1, y1, x2, y2, norm, rd = 1, pe = 2, re = 2, e = 0.5;
	TYPE x, y, p, r, rx, ry;
	size_t i, ix, iy, ptr = 0;
	for (;;) {
		while (stream->ptr < stream->frame_size) {
			if (!eread_stream(stream, stream->frame_size - stream->ptr)) {
				ewriteall(STDOUT_FILENO, buf, ptr * sizeof(*buf), "<stdout>");
				return;
			}
		}
		params = (TYPE *)stream->buf;
		x1 = (params)[0];
		y1 = (params)[1];
		x2 = (params)[4];
		y2 = (params)[5];
		if (with_params) {
			pe = (params)[8];
			re = (params)[9];
			rd = (params)[10];
			e = 1 / sqrt(pe * re);
		}
		memmove(stream->buf, stream->buf + stream->frame_size,
			stream->ptr -= stream->frame_size);

		x2 -= x1;
		y2 -= y1;
		norm = sqrt(x2 * x2 + y2 * y2);

		if (!with_params) {
			for (iy = 0; iy < height; iy++) {
				y = (TYPE)iy - y1;
				y *= y;
				for (ix = 0; ix < width; ix++) {
					x = (TYPE)ix - x1;
					x = sqrt(x * x + y) / norm;
					for (i = 0; i < stream->n_chan; i++)
						buf[ptr][i] = x;
					if (++ptr == ELEMENTSOF(buf)) {
						ewriteall(STDOUT_FILENO, buf, sizeof(buf), "<stdout>");
						ptr = 0;
					}
				}
			}
		} else {
			for (iy = 0; iy < height; iy++) {
				y = (TYPE)iy - y1;
				for (ix = 0; ix < width; ix++) {
					x = (TYPE)ix - x1;
					p = (x * x2 + y * y2) / norm;
					rx = x - p * x2 / norm;
					ry = y - p * y2 / norm;
					r = sqrt(rx * rx + ry * ry) / rd;
					p = pow(abs(p / norm), pe);
					r = pow(abs(r / norm), re);
					x = pow(p + r, e);
					for (i = 0; i < stream->n_chan; i++)
						buf[ptr][i] = x;
					if (++ptr == ELEMENTSOF(buf)) {
						ewriteall(STDOUT_FILENO, buf, sizeof(buf), "<stdout>");
						ptr = 0;
					}
				}
			}
		}
	}
}

#endif