blind

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

blind-sinc-wave.c (3022B)


      1 /* See LICENSE file for copyright and license details. */
      2 #ifndef TYPE
      3 #include "common.h"
      4 
      5 USAGE("[-e] [theta0-stream]")
      6 
      7 static int equal = 0;
      8 
      9 #define FILE "blind-sinc-wave.c"
     10 #include "define-functions.h"
     11 
     12 int
     13 main(int argc, char *argv[])
     14 {
     15 	struct stream stream, theta0;
     16 	int have_theta0;
     17 	void (*process)(struct stream *grad, struct stream *theta0);
     18 
     19 	ARGBEGIN {
     20 	case 'e':
     21 		equal = 1;
     22 		break;
     23 	default:
     24 		usage();
     25 	} ARGEND;
     26 
     27 	if (argc > 1)
     28 		usage();
     29 
     30 	eopen_stream(&stream, NULL);
     31 	if ((have_theta0 = argc == 1)) {
     32 		eopen_stream(&theta0, argv[0]);
     33 		if (theta0.width != 1 || theta0.height != 1)
     34 			eprintf("theta0-stream must be of dimension 1x1\n");
     35 	}
     36 
     37 	SELECT_PROCESS_FUNCTION(&stream);
     38 	CHECK_CHANS(&stream, == 3, == 1);
     39 	CHECK_N_CHAN(&stream, 4, 4);
     40 
     41 	if (have_theta0 && strcmp(stream.pixfmt, theta0.pixfmt))
     42 		eprintf("videos use incompatible pixel formats\n");
     43 
     44 	echeck_dimensions(&stream, WIDTH | HEIGHT, NULL);
     45 
     46 	fprint_stream_head(stdout, &stream);
     47 	efflush(stdout, "<stdout>");
     48 	process(&stream, have_theta0 ? &theta0 : NULL);
     49 	return 0;
     50 }
     51 
     52 #else
     53 
     54 static void
     55 PROCESS(struct stream *grad, struct stream *theta0)
     56 {
     57 	size_t i, n, m = 0;
     58 	TYPE *theta0xyza;
     59 	TYPE x, theta0x = 0;
     60 	TYPE y, theta0y = 0;
     61 	TYPE z, theta0z = 0;
     62 	TYPE a, theta0a = 0;
     63 	do {
     64 		if (!m) {
     65 			m = grad->frame_size;
     66 			if (theta0) {
     67 				while (theta0->ptr < theta0->frame_size)
     68 					if (!eread_stream(theta0, theta0->frame_size - theta0->ptr))
     69 						return;
     70 				theta0xyza = (TYPE *)theta0->buf;
     71 				theta0x = (theta0xyza)[0];
     72 				theta0y = (theta0xyza)[1];
     73 				theta0z = (theta0xyza)[2];
     74 				theta0a = (theta0xyza)[3];
     75 				memmove(theta0->buf, theta0->buf + theta0->frame_size,
     76 					theta0->ptr -= theta0->frame_size);
     77 			}
     78 		}
     79 		n = MIN(grad->ptr, m) / grad->pixel_size;
     80 		if (equal) {
     81 			for (i = 0; i < n; i++) {
     82 				a = ((TYPE *)(grad->buf))[4 * i + 3];
     83 				a = (a ? sin(a + theta0y) / a : sin(a + theta0y)) / 2 + (TYPE)0.5;
     84 				((TYPE *)(grad->buf))[4 * i + 0] = a;
     85 				((TYPE *)(grad->buf))[4 * i + 1] = a;
     86 				((TYPE *)(grad->buf))[4 * i + 2] = a;
     87 				((TYPE *)(grad->buf))[4 * i + 3] = a;
     88 			}
     89 		} else {
     90 			for (i = 0; i < n; i++) {
     91 				x = ((TYPE *)(grad->buf))[4 * i + 0];
     92 				y = ((TYPE *)(grad->buf))[4 * i + 1];
     93 				z = ((TYPE *)(grad->buf))[4 * i + 2];
     94 				a = ((TYPE *)(grad->buf))[4 * i + 3];
     95 				x = (x ? sin(x + theta0x) / x : sin(x + theta0x)) / 2 + (TYPE)0.5;
     96 				y = (y ? sin(y + theta0y) / y : sin(y + theta0y)) / 2 + (TYPE)0.5;
     97 				z = (z ? sin(z + theta0z) / z : sin(z + theta0z)) / 2 + (TYPE)0.5;
     98 				a = (a ? sin(a + theta0a) / a : sin(a + theta0a)) / 2 + (TYPE)0.5;
     99 				((TYPE *)(grad->buf))[4 * i + 0] = x;
    100 				((TYPE *)(grad->buf))[4 * i + 1] = y;
    101 				((TYPE *)(grad->buf))[4 * i + 2] = z;
    102 				((TYPE *)(grad->buf))[4 * i + 3] = a;
    103 			}
    104 		}
    105 		n *= grad->pixel_size;
    106 		m -= n;
    107 		ewriteall(STDOUT_FILENO, grad->buf, n, "<stdout>");
    108 		memmove(grad->buf, grad->buf + n, grad->ptr -= n);
    109 	} while (eread_stream(grad, SIZE_MAX));
    110 	if (grad->ptr)
    111 		eprintf("%s: incomplete frame\n", grad->file);
    112 }
    113 
    114 #endif