blind

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

blind-transition.c (2753B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include "common.h"
      3 
      4 USAGE("[-ir] [-s softness-stream]")
      5 
      6 static size_t fm;
      7 static double fm_double;
      8 static float fm_float;
      9 static int reverse = 0;
     10 static int invert = 0;
     11 static const char *softness_file = NULL;
     12 static struct stream softness;
     13 
     14 #define PROCESS(TYPE)\
     15 	do {\
     16 		size_t i, j = 0;\
     17 		TYPE a, s, t = fm ? (TYPE)(reverse ? fm - f : f) / fm_##TYPE : (TYPE)0.5;\
     18 		if (!softness_file) {\
     19 			for (i = 0; i < n; i += stream->pixel_size) {\
     20 				a = ((TYPE *)(stream->buf + i))[1];\
     21 				if (invert)\
     22 					a = (TYPE)1 - a;\
     23 				a = (TYPE)(a >= t);\
     24 				((TYPE *)(stream->buf + i))[0] = a * (TYPE)D65_XYZ_X;\
     25 				((TYPE *)(stream->buf + i))[1] = a;\
     26 				((TYPE *)(stream->buf + i))[2] = a * (TYPE)D65_XYZ_Z;\
     27 				((TYPE *)(stream->buf + i))[3] = 1;\
     28 			}\
     29 		} else {\
     30 			for (i = 0; i < n; i += stream->pixel_size, j += softness.pixel_size) {\
     31 				while (j + softness.pixel_size > softness.ptr) {\
     32 					memmove(softness.buf, softness.buf + j, softness.ptr -= j);\
     33 					j = 0;\
     34 					if (!eread_stream(&softness, SIZE_MAX))\
     35 						return;\
     36 				}\
     37 				s  = ((TYPE *)(softness.buf + j))[1];\
     38 				s *= ((TYPE *)(softness.buf + j))[3];\
     39 				a  = ((TYPE *)(stream->buf + i))[1];\
     40 				if (invert)\
     41 					a = (TYPE)1 - a;\
     42 				a = (a / (1 + 2 * s) + s - t) / s;\
     43 				a = a < 0 ? (TYPE)0 : a > 1 ? (TYPE)1 : a;\
     44 				((TYPE *)(stream->buf + i))[0] = a * (TYPE)D65_XYZ_X;\
     45 				((TYPE *)(stream->buf + i))[1] = a;\
     46 				((TYPE *)(stream->buf + i))[2] = a * (TYPE)D65_XYZ_Z;\
     47 				((TYPE *)(stream->buf + i))[3] = 1;\
     48 			}\
     49 		}\
     50 	} while (0)
     51 
     52 static void process_lf(struct stream *stream, size_t n, size_t f) {PROCESS(double);}
     53 static void process_f (struct stream *stream, size_t n, size_t f) {PROCESS(float);}
     54 
     55 int
     56 main(int argc, char *argv[])
     57 {
     58 	struct stream stream;
     59 	void (*process)(struct stream *stream, size_t n, size_t f);
     60 
     61 	ARGBEGIN {
     62 	case 'i':
     63 		invert = 1;
     64 		break;
     65 	case 'r':
     66 		reverse = 1;
     67 		break;
     68 	case 's':
     69 		softness_file = UARGF();
     70 		break;
     71 	default:
     72 		usage();
     73 	} ARGEND;
     74 
     75 	if (argc)
     76 		usage();
     77 
     78 	eopen_stream(&stream, NULL);
     79 	if (softness_file) {
     80 		eopen_stream(&softness, softness_file);
     81 		echeck_compat(&stream, &softness);
     82 	}
     83 
     84 	CHECK_ALPHA(&stream);
     85 	CHECK_COLOUR_SPACE(&stream, CIEXYZ);
     86 	if (stream.encoding == DOUBLE)
     87 		process = process_lf;
     88 	else if (stream.encoding == FLOAT)
     89 		process = process_f;
     90 	else
     91 		eprintf("pixel format %s is not supported, try xyza\n", stream.pixfmt);
     92 
     93 	if (!stream.frames)
     94 		eprintf("video's length is not recorded");
     95 
     96 	fprint_stream_head(stdout, &stream);
     97 	efflush(stdout, "<stdout>");
     98 	fm_double = (double)(fm = stream.frames - 1);
     99 	fm_float = (float)fm_double;
    100 	process_each_frame_segmented(&stream, STDOUT_FILENO, "<stdout>", process);
    101 	return 0;
    102 }