aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2017-07-07 20:58:16 +0200
committerMattias Andrée <maandree@kth.se>2017-07-07 20:58:16 +0200
commitd1a5dfdf162d1e1200e7ee1060435d76a028d4c4 (patch)
tree46f1518b9277c9dafe0006f1daf62a3f4ae4ae63 /src
parentAdd blind-dual-key (diff)
downloadblind-d1a5dfdf162d1e1200e7ee1060435d76a028d4c4.tar.gz
blind-d1a5dfdf162d1e1200e7ee1060435d76a028d4c4.tar.bz2
blind-d1a5dfdf162d1e1200e7ee1060435d76a028d4c4.tar.xz
Add blind-chroma-key
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src')
-rw-r--r--src/blind-chroma-key.c92
-rw-r--r--src/blind-sinc-wave.c3
2 files changed, 94 insertions, 1 deletions
diff --git a/src/blind-chroma-key.c b/src/blind-chroma-key.c
new file mode 100644
index 0000000..e7b1448
--- /dev/null
+++ b/src/blind-chroma-key.c
@@ -0,0 +1,92 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+USAGE("key-stream")
+
+
+#define PROCESS(TYPE, SUFFIX)\
+ static void\
+ process_##SUFFIX(struct stream *stream, struct stream *key)\
+ {\
+ size_t i, n, m = 0;\
+ TYPE x1, y1, z1, a1, a2, variance2, *keyxyza;\
+ TYPE x, y, z, d;\
+ do {\
+ if (!m) {\
+ m = stream->frame_size;\
+ while (key->ptr < key->frame_size)\
+ if (!eread_stream(key, key->frame_size - key->ptr))\
+ return;\
+ keyxyza = (TYPE *)(key->buf);\
+ x1 = keyxyza[0];\
+ y1 = keyxyza[1];\
+ z1 = keyxyza[2];\
+ a1 = keyxyza[3];\
+ x = x1 - keyxyza[4];\
+ y = y1 - keyxyza[5];\
+ z = z1 - keyxyza[6];\
+ a2 = keyxyza[7];\
+ variance2 = x * x + y * y + z * z;\
+ if (a2 > a1) {\
+ a1 = 1 - a1;\
+ a2 = 1 - a2;\
+ }\
+ memmove(key->buf, key->buf + key->frame_size,\
+ key->ptr -= key->frame_size);\
+ }\
+ n = MIN(stream->ptr, m) / stream->pixel_size;\
+ for (i = 0; i < n; i++) {\
+ x = ((TYPE *)(stream->buf))[4 * i + 0] - x1;\
+ y = ((TYPE *)(stream->buf))[4 * i + 1] - y1;\
+ z = ((TYPE *)(stream->buf))[4 * i + 2] - z1;\
+ d = x * x + y * y + z * z;\
+ if (d <= variance2) {\
+ if (a1 == a2)\
+ d = 0;\
+ else\
+ d = sqrt(d / variance2) * (a1 - a2) + a2;\
+ ((TYPE *)(stream->buf))[4 * i + 3] *= d;\
+ }\
+ }\
+ n *= stream->pixel_size;\
+ m -= n;\
+ ewriteall(STDOUT_FILENO, stream->buf, n, "<stdout>");\
+ memmove(stream->buf, stream->buf + n, stream->ptr -= n);\
+ } while (eread_stream(stream, SIZE_MAX));\
+ if (stream->ptr)\
+ eprintf("%s: incomplete frame\n", stream->file);\
+ }
+
+PROCESS(double, lf)
+PROCESS(float, f)
+
+
+int
+main(int argc, char *argv[])
+{
+ struct stream stream, key;
+ void (*process)(struct stream *stream, struct stream *key);
+
+ UNOFLAGS(argc != 1);
+
+ eopen_stream(&stream, NULL);
+ eopen_stream(&key, argv[0]);
+
+ if (!strcmp(stream.pixfmt, "xyza"))
+ process = process_lf;
+ else if (!strcmp(stream.pixfmt, "xyza f"))
+ process = process_f;
+ else
+ eprintf("pixel format %s is not supported, try xyza\n", stream.pixfmt);
+
+ if (strcmp(stream.pixfmt, key.pixfmt))
+ eprintf("videos use incompatible pixel formats\n");
+
+ if (key.width > 2 || key.height > 2 || key.width * key.height != 2)
+ eprintf("%s: each frame must contain exactly 2 pixels\n", key.file);
+
+ fprint_stream_head(stdout, &stream);
+ efflush(stdout, "<stdout>");
+ process(&stream, &key);
+ return 0;
+}
diff --git a/src/blind-sinc-wave.c b/src/blind-sinc-wave.c
index 02c053d..aa91a8b 100644
--- a/src/blind-sinc-wave.c
+++ b/src/blind-sinc-wave.c
@@ -16,7 +16,6 @@ static int equal = 0;
TYPE y, theta0y = 0;\
TYPE z, theta0z = 0;\
TYPE a, theta0a = 0;\
- echeck_dimensions(grad, WIDTH | HEIGHT, NULL);\
do {\
if (!m) {\
m = grad->frame_size;\
@@ -107,6 +106,8 @@ main(int argc, char *argv[])
if (have_theta0 && strcmp(stream.pixfmt, theta0.pixfmt))
eprintf("videos use incompatible pixel formats\n");
+ echeck_dimensions(&stream, WIDTH | HEIGHT, NULL);
+
fprint_stream_head(stdout, &stream);
efflush(stdout, "<stdout>");
process(&stream, have_theta0 ? &theta0 : NULL);