blind

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

blind-hexagon-tessellation.c (3144B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include "common.h"
      3 
      4 USAGE("[-F pixel-format] block-diameter")
      5 
      6 #define SET_XYZA(TYPE)\
      7 	(colours = alloca(4 * stream.pixel_size),\
      8 	 ((TYPE *)colours)[ 0] = (TYPE)0.412457445582367600,\
      9 	 ((TYPE *)colours)[ 1] = (TYPE)0.212673370378408280,\
     10 	 ((TYPE *)colours)[ 2] = (TYPE)0.019333942761673460,\
     11 	 ((TYPE *)colours)[ 3] = (TYPE)1,\
     12 	 ((TYPE *)colours)[ 4] = (TYPE)0.770033310827883400,\
     13 	 ((TYPE *)colours)[ 5] = (TYPE)0.927825100869440000,\
     14 	 ((TYPE *)colours)[ 6] = (TYPE)0.138525897843512050,\
     15 	 ((TYPE *)colours)[ 7] = (TYPE)1,\
     16 	 ((TYPE *)colours)[ 8] = (TYPE)0.357575865245515900,\
     17 	 ((TYPE *)colours)[ 9] = (TYPE)0.715151730491031800,\
     18 	 ((TYPE *)colours)[10] = (TYPE)0.119191955081838600,\
     19 	 ((TYPE *)colours)[11] = (TYPE)1,\
     20 	 ((TYPE *)colours)[12] = (TYPE)D65_XYZ_X,\
     21 	 ((TYPE *)colours)[13] = (TYPE)1.0000,\
     22 	 ((TYPE *)colours)[14] = (TYPE)D65_XYZ_Z,\
     23 	 ((TYPE *)colours)[15] = (TYPE)1)
     24 
     25 static struct stream stream = { .width = 0, .height = 0, .frames = 1 };
     26 
     27 int
     28 main(int argc, char *argv[])
     29 {
     30 	size_t diameter;
     31 	const char *pixfmt = "xyza";
     32 	char *colours;
     33 	size_t x, y, y2;
     34 	int k;
     35 
     36 	ARGBEGIN {
     37 	case 'F':
     38 		pixfmt = UARGF();
     39 		break;
     40 	default:
     41 		usage();
     42 	} ARGEND;
     43 
     44 	if (argc != 1)
     45 		usage();
     46 
     47 	diameter = etozu_arg("block-diameter", argv[0], 1, SIZE_MAX);
     48 
     49 	eset_pixel_format(&stream, pixfmt);
     50 	CHECK_N_CHAN(&stream, 4, 4);
     51 	if (stream.encoding == DOUBLE)
     52 		SET_XYZA(double);
     53 	else if (stream.encoding == FLOAT)
     54 		SET_XYZA(float);
     55 	else
     56 		eprintf("pixel format %s is not supported, try xyza\n", stream.pixfmt);
     57 
     58 	stream.width  = (size_t)((double)diameter * sqrt(3.));
     59 	stream.height = diameter * 3 / 2;
     60 	fprint_stream_head(stdout, &stream);
     61 	efflush(stdout, "<stdout>");
     62 
     63 	for (y = 0; y < stream.height; y++) {
     64 		for (x = 0; x < stream.width; x++) {
     65 			if (y * 4 < diameter) {
     66 				switch (x * 4 / stream.width) {
     67 				case 0:
     68 					k = 2 * (4 * x * diameter < stream.width * diameter - 4 * y * stream.width);
     69 					break;
     70 				case 1:
     71 					k = 3 * (4 * x * diameter - stream.width * diameter > 4 * y * stream.width);
     72 					break;
     73 				case 2:
     74 					k = 1 + 2 * (3 * diameter * stream.width - 4 * x * diameter > 4 * y * stream.width);
     75 					break;
     76 				default:
     77 					k = 1 + (4 * x * diameter - 3 * stream.width * diameter > 4 * y * stream.width);
     78 					break;
     79 				}
     80 			} else if (y * 4 < diameter * 3) {
     81 				k = (x * 2 >= stream.width);
     82 			} else if (y < diameter) {
     83 				y2 = diameter - y;
     84 				switch (x * 4 / stream.width) {
     85 				case 0:
     86 					k = 2 * (4 * x * diameter < stream.width * diameter - 4 * y2 * stream.width);
     87 					break;
     88 				case 1:
     89 					k = 3 * (4 * x * diameter - stream.width * diameter > 4 * y2 * stream.width);
     90 					break;
     91 				case 2:
     92 					k = 1 + 2 * (3 * diameter * stream.width - 4 * x * diameter > 4 * y2 * stream.width);
     93 					break;
     94 				default:
     95 					k = 1 + (4 * x * diameter - 3 * stream.width * diameter > 4 * y2 * stream.width);
     96 					break;
     97 				}
     98 			} else {
     99 				k = (stream.width <= x * 4 && x * 4 < stream.width * 3) + 2;
    100 			}
    101 			ewriteall(STDOUT_FILENO, colours + (size_t)k * stream.pixel_size, stream.pixel_size, "<stdout>");
    102 		}
    103 	}
    104 
    105 	return 0;
    106 }