aboutsummaryrefslogtreecommitdiffstats
path: root/src/blind-spatial-arithm.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2017-07-25 22:12:23 +0200
committerMattias Andrée <maandree@kth.se>2017-07-25 22:12:23 +0200
commit835df7bd1e81852062dd70ce1a054fc9b510e50f (patch)
treede418467fc318adfcf5e3fdd8e75ea2a055386c9 /src/blind-spatial-arithm.c
parentAdd ability to choose korn shell implementation (diff)
downloadblind-835df7bd1e81852062dd70ce1a054fc9b510e50f.tar.gz
blind-835df7bd1e81852062dd70ce1a054fc9b510e50f.tar.bz2
blind-835df7bd1e81852062dd70ce1a054fc9b510e50f.tar.xz
Fix blind-kernel and blind-temporal-mean,d add blind-{spatial,temporal}-arithm and blind-spatial-mean, and add support for multiple streams in blind-arithm
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src/blind-spatial-arithm.c')
-rw-r--r--src/blind-spatial-arithm.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/blind-spatial-arithm.c b/src/blind-spatial-arithm.c
new file mode 100644
index 0000000..b63dc89
--- /dev/null
+++ b/src/blind-spatial-arithm.c
@@ -0,0 +1,94 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+USAGE("operation")
+
+/* Because the syntax for a function returning a function pointer is disgusting. */
+typedef void (*process_func)(struct stream *stream);
+
+#define LIST_OPERATORS(PIXFMT, TYPE)\
+ X(add, img[j & 3] + *buf, PIXFMT, TYPE)\
+ X(mul, img[j & 3] * *buf, PIXFMT, TYPE)\
+ X(min, MIN(img[j & 3], *buf), PIXFMT, TYPE)\
+ X(max, MAX(img[j & 3], *buf), PIXFMT, TYPE)
+
+#define X(NAME, ALGO, PIXFMT, TYPE)\
+ static void\
+ process_##PIXFMT##_##NAME(struct stream *stream)\
+ {\
+ TYPE img[4], *buf;\
+ size_t i, n, j = 0, m = stream->frame_size / sizeof(*img);\
+ int first = 1;\
+ do {\
+ n = stream->ptr / stream->pixel_size * stream->n_chan;\
+ buf = (TYPE *)(stream->buf);\
+ for (i = 0; i < n; i++, buf++, j++, j %= m) {\
+ if (!j) {\
+ if (!first)\
+ ewriteall(STDOUT_FILENO, img, sizeof(img), "<stdout>");\
+ first = 0;\
+ img[0] = *buf++;\
+ img[1] = *buf++;\
+ img[2] = *buf++;\
+ img[3] = *buf;\
+ i += 3;\
+ j = 3;\
+ } else {\
+ img[j & 3] = ALGO;\
+ }\
+ }\
+ n *= sizeof(TYPE);\
+ memmove(stream->buf, stream->buf + n, stream->ptr -= n);\
+ } while (eread_stream(stream, SIZE_MAX));\
+ if (!first)\
+ ewriteall(STDOUT_FILENO, img, sizeof(img), "<stdout>");\
+ }
+LIST_OPERATORS(lf, double)
+LIST_OPERATORS(f, float)
+#undef X
+
+static process_func
+get_process_lf(const char *operation)
+{
+#define X(NAME, _ALGO, PIXFMT, TYPE)\
+ if (!strcmp(operation, #NAME)) return process_##PIXFMT##_##NAME;
+ LIST_OPERATORS(lf, double)
+#undef X
+ eprintf("algorithm not recognised: %s\n", operation);
+ return NULL;
+}
+
+static process_func
+get_process_f(const char *operation)
+{
+#define X(NAME, _ALGO, PIXFMT, TYPE)\
+ if (!strcmp(operation, #NAME)) return process_##PIXFMT##_##NAME;
+ LIST_OPERATORS(f, float)
+#undef X
+ eprintf("algorithm not recognised: %s\n", operation);
+ return NULL;
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct stream stream;
+ process_func process;
+
+ UNOFLAGS(argc != 1);
+
+ eopen_stream(&stream, NULL);
+ echeck_dimensions(&stream, WIDTH | HEIGHT, NULL);
+
+ if (stream.encoding == DOUBLE)
+ process = get_process_lf(argv[0]);
+ else
+ process = get_process_f(argv[0]);
+
+ if (DPRINTF_HEAD(STDOUT_FILENO, stream.frames, 1, 1, stream.pixfmt) < 0)
+ eprintf("dprintf:");
+ process(&stream);
+ if (stream.ptr)
+ eprintf("%s: incomplete frame\n", stream.file);
+ return 0;
+}