blind-matrix-reflect.c - blind - suckless command-line video editing utility
(HTM) git clone git://git.suckless.org/blind
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
blind-matrix-reflect.c (2008B)
---
1 /* See LICENSE file for copyright and license details. */
2 #ifndef TYPE
3 #include "common.h"
4
5 USAGE("[-c]")
6
7 static int per_channel = 0;
8
9 #define FILE "blind-matrix-reflect.c"
10 #include "define-functions.h"
11
12 int
13 main(int argc, char *argv[])
14 {
15 struct stream stream;
16 void (*process)(struct stream *stream);
17
18 ARGBEGIN {
19 case 'c':
20 per_channel = 1;
21 break;
22 default:
23 usage();
24 } ARGEND;
25
26 if (argc)
27 usage();
28
29 eopen_stream(&stream, NULL);
30
31 SELECT_PROCESS_FUNCTION(&stream);
32 CHECK_CHANS(&stream, == 3, == 1);
33
34 if (stream.width > 2 || stream.height > 2 || stream.width * stream.height != 2)
35 eprintf("<stdin>: each frame must contain exactly 2 pixels\n");
36
37 stream.width = 3;
38 stream.height = 3;
39 fprint_stream_head(stdout, &stream);
40 efflush(stdout, "<stdout>");
41
42 process(&stream);
43 return 0;
44 }
45
46 #else
47
48 static void
49 PROCESS(struct stream *stream)
50 {
51 typedef TYPE pixel_t[4];
52 pixel_t matrix[9];
53 pixel_t buf[2];
54 TYPE x2, y2, norm2;
55 size_t i;
56
57 for (i = 0; i < stream->n_chan; i++) {
58 matrix[0][i] = 1, matrix[1][i] = 0, matrix[2][i] = 0;
59 matrix[3][i] = 0, matrix[4][i] = 1, matrix[5][i] = 0;
60 matrix[6][i] = 0, matrix[7][i] = 0, matrix[8][i] = 1;
61 }
62
63 while (eread_frame(stream, buf)) {
64 if (per_channel) {
65 for (i = 0; i < stream->n_chan; i++) {
66 x2 = buf[0][i] * buf[0][i];
67 y2 = buf[1][i] * buf[1][i];
68 norm2 = x2 + y2;
69 matrix[4][i] = -(matrix[0][i] = (x2 - y2) / norm2);
70 matrix[3][i] = matrix[1][i] = 2 * buf[0][i] * buf[1][i] / norm2;
71 }
72 } else {
73 buf[0][1] *= buf[0][3];
74 buf[1][1] *= buf[1][3];
75 x2 = buf[0][1] * buf[0][1];
76 y2 = buf[1][1] * buf[1][1];
77 norm2 = x2 + y2;
78 matrix[4][0] = -(matrix[0][0] = (x2 - y2) / norm2);
79 matrix[3][0] = matrix[1][0] = 2 * buf[0][1] * buf[1][1] / norm2;
80 for (i = 1; i < stream->n_chan; i++) {
81 matrix[0][i] = matrix[0][0];
82 matrix[1][i] = matrix[1][0];
83 matrix[3][i] = matrix[3][0];
84 matrix[4][i] = matrix[4][0];
85 }
86 }
87 ewriteall(STDOUT_FILENO, matrix, sizeof(matrix), "<stdout>");
88 }
89 }
90
91 #endif