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

USAGE("[-cd]")

static int per_channel = 0;
static int in_degrees = 0;

#define PROCESS(TYPE)\
	do {\
		typedef TYPE pixel_t[4];\
		pixel_t matrix[9];\
		pixel_t buf;\
		size_t i;\
		\
		for (i = 0; i < stream->n_chan; i++) {\
			matrix[0][i] = 1, matrix[1][i] = 0, matrix[2][i] = 0;\
			matrix[3][i] = 0, matrix[4][i] = 1, matrix[5][i] = 0;\
			matrix[6][i] = 0, matrix[7][i] = 0, matrix[8][i] = 1;\
		}\
		\
		while (eread_frame(stream, buf)) {\
			if (per_channel && in_degrees) {\
				for (i = 0; i < stream->n_chan; i++) {\
					matrix[4][i] = matrix[0][i] = degcos(buf[i]);\
					matrix[3][i] = -(matrix[1][i] = degsin(buf[i]));\
				}\
			} else if (per_channel) {\
				for (i = 0; i < stream->n_chan; i++) {\
					matrix[4][i] = matrix[0][i] = cos(buf[i]);\
					matrix[3][i] = -(matrix[1][i] = sin(buf[i]));\
				}\
			} else {\
				buf[1] *= buf[3];\
				if (in_degrees) {\
					matrix[4][0] = matrix[0][0] = degcos(buf[1]);\
					matrix[3][0] = -(matrix[1][0] = degsin(buf[1]));\
				} else {\
					matrix[4][0] = matrix[0][0] = cos(buf[1]);\
					matrix[3][0] = -(matrix[1][0] = sin(buf[1]));\
				}\
				for (i = 0; i < stream->n_chan; i++) {\
					matrix[0][i] = matrix[0][0];\
					matrix[1][i] = matrix[1][0];\
					matrix[3][i] = matrix[3][0];\
					matrix[4][i] = matrix[4][0];\
				}\
			}\
			ewriteall(STDOUT_FILENO, matrix, sizeof(matrix), "<stdout>");\
		}\
	} while (0)

static void process_lf(struct stream *stream) { PROCESS(double); }
static void process_f(struct stream *stream) { PROCESS(float); }

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

	ARGBEGIN {
	case 'c':
		per_channel = 1;
		break;
	case 'd':
		in_degrees = 1;
		break;
	default:
		usage();
	} ARGEND;

	if (argc)
		usage();

	eopen_stream(&stream, NULL);

	if (stream.width != 1 || stream.height != 1)
		eprintf("<stdin>: each frame must contain exactly 1 pixels\n");

	stream.width  = 3;
	stream.height = 3;
	fprint_stream_head(stdout, &stream);
	efflush(stdout, "<stdout>");

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

	process(&stream);
	return 0;
}