diff options
| author | Mattias Andrée <maandree@kth.se> | 2017-07-20 20:03:52 +0200 |
|---|---|---|
| committer | Mattias Andrée <maandree@kth.se> | 2017-07-20 20:03:52 +0200 |
| commit | fa20f84e2d12bf9998591ce4e464180586d957ff (patch) | |
| tree | a8fd23c22c3f798d3e985246339e69e8567400d5 /src | |
| parent | Add blind-mosaic-corners and fix enset_pixel_format (diff) | |
| download | blind-fa20f84e2d12bf9998591ce4e464180586d957ff.tar.gz blind-fa20f84e2d12bf9998591ce4e464180586d957ff.tar.bz2 blind-fa20f84e2d12bf9998591ce4e464180586d957ff.tar.xz | |
Add blind-affine-colour and blind-split-chans
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
| -rw-r--r-- | src/blind-affine-colour.c | 118 | ||||
| -rw-r--r-- | src/blind-split-chans.c | 65 |
2 files changed, 183 insertions, 0 deletions
diff --git a/src/blind-affine-colour.c b/src/blind-affine-colour.c new file mode 100644 index 0000000..88fd76a --- /dev/null +++ b/src/blind-affine-colour.c @@ -0,0 +1,118 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef TYPE +#include "common.h" + +USAGE("[-alp] matrix-stream") + +static int skip_alpha = 0; +static int linear = 0; +static int per_pixel = 0; +static size_t dim; + +#define FILE "blind-affine-colour.c" +#include "define-functions.h" + +int +main(int argc, char *argv[]) +{ + struct stream colour, matrix; + void (*process)(struct stream *colour, struct stream *matrix); + size_t h; + + ARGBEGIN { + case 'a': + skip_alpha = 1; + break; + case 'l': + linear = 1; + break; + case 'p': + per_pixel = 1; + break; + default: + usage(); + } ARGEND; + + if (argc != 1) + usage(); + + eopen_stream(&colour, NULL); + eopen_stream(&matrix, argv[0]); + + SELECT_PROCESS_FUNCTION(&colour); + + if (strcmp(colour.pixfmt, matrix.pixfmt)) + eprintf("videos use incompatible pixel formats\n"); + + dim = colour.n_chan - (size_t)skip_alpha + (size_t)!linear; + h = matrix.height, matrix.height = dim; + echeck_dimensions(&matrix, WIDTH | HEIGHT, "matrix"); + matrix.height = h; + + if (per_pixel) { + if (matrix.height != dim * colour.height || matrix.width != dim * colour.width) + eprintf("the matrice should have the size %zux%zu, but are %zux%zu", + dim * colour.height, dim * colour.width, matrix.height, matrix.width); + } else { + if (matrix.height != dim || matrix.width != dim) + eprintf("the matrice should have the size %zux%zu, but are %zux%zu", + dim, dim, matrix.height, matrix.width); + } + + fprint_stream_head(stdout, &colour); + efflush(stdout, "<stdout>"); + process(&colour, &matrix); + return 0; +} + +#else + +static void +PROCESS(struct stream *colour, struct stream *matrix) +{ + char *mbuf; + TYPE *mat, *pixel, V[5], M[ELEMENTSOF(V)][ELEMENTSOF(V)]; + size_t ptr, i, j, w, x = 0, y = 0, cn; + + mbuf = emalloc2(dim, matrix->row_size); + mat = (TYPE *)mbuf; + w = matrix->width * matrix->n_chan; + cn = colour->n_chan - (size_t)skip_alpha; + + memset(M, 0, sizeof(M)); + for (i = 0; i < ELEMENTSOF(V); i++) + M[i][i] = V[i] = 1; + + do { + for (ptr = 0; ptr + colour->pixel_size <= colour->ptr; x = (x + 1) % colour->width, ptr += colour->pixel_size) { + if (!x) { + if (!y && !eread_segment(matrix, mbuf, dim * matrix->row_size)) + break; + if (!per_pixel) + y = (y + 1) % colour->height; + } + if (per_pixel) { + mat = (TYPE *)(mbuf + x * dim * matrix->pixel_size); + for (i = 0; i < dim; i++, mat += w) + for (j = 0; j < dim; j++) + M[i][j] = mat[j * matrix->n_chan + 1] * mat[(j + 1) * matrix->n_chan - 1]; + } + pixel = (TYPE *)(colour->buf + ptr); + for (i = 0; i < dim; i++) { + V[i] = 0; + for (j = 0; j < cn; j++) + V[i] += M[i][j] * pixel[j]; + for (; j < dim; j++) + V[i] += M[i][j]; + } + for (i = 0; i < cn; i++) + pixel[i] = V[i] / V[cn]; + } + ewriteall(STDOUT_FILENO, colour->buf, ptr, "<stdout>"); + memmove(colour->buf, colour->buf + ptr, colour->ptr -= ptr); + } while (eread_stream(colour, SIZE_MAX)); + if (colour->ptr) + eprintf("%s: incomplete frame\n", colour->file); +} + +#endif diff --git a/src/blind-split-chans.c b/src/blind-split-chans.c new file mode 100644 index 0000000..523fb55 --- /dev/null +++ b/src/blind-split-chans.c @@ -0,0 +1,65 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + +USAGE("X-file Y-file Z-file [alpha-file]") + +int +main(int argc, char *argv[]) +{ + struct stream stream; + char xbuf[BUFSIZ], ybuf[BUFSIZ], zbuf[BUFSIZ], abuf[BUFSIZ]; + int xfd, yfd, zfd, afd = -1; + size_t i, n, ptr; + + UNOFLAGS(argc != 3 && argc != 4); + + eopen_stream(&stream, NULL); + + xfd = eopen(argv[0], O_WRONLY | O_CREAT | O_TRUNC, 0666); + yfd = eopen(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0666); + zfd = eopen(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666); + if (argc == 4) + afd = eopen(argv[3], O_WRONLY | O_CREAT | O_TRUNC, 0666); + + if (DPRINTF_HEAD(xfd, stream.frames, stream.width, stream.height, stream.pixfmt) < 0) + eprintf("dprintf %s:", argv[0]); + if (DPRINTF_HEAD(yfd, stream.frames, stream.width, stream.height, stream.pixfmt) < 0) + eprintf("dprintf %s:", argv[1]); + if (DPRINTF_HEAD(zfd, stream.frames, stream.width, stream.height, stream.pixfmt) < 0) + eprintf("dprintf %s:", argv[2]); + if (afd >= 0 && DPRINTF_HEAD(afd, stream.frames, stream.width, stream.height, stream.pixfmt) < 0) + eprintf("dprintf %s:", argv[3]); + + n = (stream.n_chan - (afd < 0)) * stream.chan_size; + do { + for (ptr = 0; ptr + stream.pixel_size <= stream.ptr; ptr += stream.pixel_size) { + for (i = 0; i < n; i += stream.chan_size) { + memcpy(xbuf + ptr + i, stream.buf + ptr + 0 * stream.chan_size, stream.chan_size); + memcpy(ybuf + ptr + i, stream.buf + ptr + 1 * stream.chan_size, stream.chan_size); + memcpy(zbuf + ptr + i, stream.buf + ptr + 2 * stream.chan_size, stream.chan_size); + if (afd >= 0) + memcpy(abuf + ptr + i, stream.buf + ptr + 3 * stream.chan_size, stream.chan_size); + } + if (afd < 0) { + memcpy(xbuf + ptr + n, stream.buf + ptr + 3 * stream.chan_size, stream.chan_size); + memcpy(ybuf + ptr + n, stream.buf + ptr + 3 * stream.chan_size, stream.chan_size); + memcpy(zbuf + ptr + n, stream.buf + ptr + 3 * stream.chan_size, stream.chan_size); + } + } + ewriteall(xfd, xbuf, ptr, argv[0]); + ewriteall(yfd, ybuf, ptr, argv[1]); + ewriteall(zfd, zbuf, ptr, argv[2]); + if (afd >= 0) + ewriteall(afd, abuf, ptr, argv[3]); + memmove(stream.buf, stream.buf + ptr, stream.ptr -= ptr); + } while (eread_stream(&stream, SIZE_MAX)); + if (stream.ptr) + eprintf("%s: incomplete frame\n", stream.file); + + close(xfd); + close(yfd); + close(zfd); + if (afd >= 0) + close(afd); + return 0; +} |
