blind

suckless command-line video editing utility
git clone git://git.suckless.org/blind
Log | Files | Refs | README | LICENSE

blind-matrix-orthoproject.c (2016B)


      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-orthoproject.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[0][i] = x2 / norm2;
     70 				matrix[4][i] = y2 / norm2;
     71 				matrix[3][i] = matrix[1][i] = buf[0][i] * buf[1][i] / norm2;
     72 			}
     73 		} else {
     74 			buf[0][1] *= buf[0][3];
     75 			buf[1][1] *= buf[1][3];
     76 			x2 = buf[0][1] * buf[0][1];
     77 			y2 = buf[1][1] * buf[1][1];
     78 			norm2 = x2 + y2;
     79 			matrix[0][0] = x2 / norm2;
     80 			matrix[4][0] = y2 / norm2;
     81 			matrix[3][0] = matrix[1][0] = buf[0][1] * buf[1][1] / norm2;
     82 			for (i = 1; i < stream->n_chan; i++) {
     83 				matrix[0][i] = matrix[0][0];
     84 				matrix[1][i] = matrix[1][0];
     85 				matrix[3][i] = matrix[3][0];
     86 				matrix[4][i] = matrix[4][0];
     87 			}
     88 		}
     89 		ewriteall(STDOUT_FILENO, matrix, sizeof(matrix), "<stdout>");
     90 	}
     91 }
     92 
     93 #endif