blind

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

commit a21ebe8bea569e7d8961b9711d430d21c92f769b
parent b7a82c980fe7e0c1f9029b55be97422428d65d5a
Author: Mattias Andrée <maandree@kth.se>
Date:   Wed, 11 Jan 2017 09:30:33 +0100

Reuse code

Signed-off-by: Mattias Andrée <maandree@kth.se>

Diffstat:
Msrc/stream.c | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/stream.h | 21+++++----------------
Msrc/vu-arithm.c | 50++------------------------------------------------
Msrc/vu-dissolve.c | 2+-
Msrc/vu-invert-luma.c | 47+----------------------------------------------
Msrc/vu-set-alpha.c | 47+----------------------------------------------
Msrc/vu-set-luma.c | 47+----------------------------------------------
Msrc/vu-set-saturation.c | 47+----------------------------------------------
8 files changed, 87 insertions(+), 249 deletions(-)

diff --git a/src/stream.c b/src/stream.c @@ -191,3 +191,78 @@ enread_frame(int status, struct stream *stream, void *buf, size_t n) stream->ptr = 0; return 1; } + + +void +process_each_frame_segmented(struct stream *stream, int output_fd, const char* output_fname, + void (*process)(struct stream *stream, size_t n, size_t frame)) +{ + size_t frame_size, frame, r, n; + + echeck_frame_size(stream->width, stream->height, stream->pixel_size, 0, stream->file); + frame_size = stream->height * stream->width * stream->pixel_size; + + for (frame = 0; frame < stream->frames; frame++) { + for (n = frame_size; n; n -= r) { + if (!eread_stream(stream, n)) + eprintf("%s: file is shorter than expected\n", stream->file); + r = stream->ptr - (stream->ptr % stream->pixel_size); + (process)(stream, r, frame); + ewriteall(output_fd, stream->buf, r, output_fname); + memmove(stream->buf, stream->buf + r, stream->ptr -= r); + } + } +} + + +void +process_two_streams(struct stream *left, struct stream *right, int output_fd, const char* output_fname, + void (*process)(struct stream *left, struct stream *right, size_t n)) +{ + size_t n; + + echeck_compat(left, right); + + for (;;) { + if (left->ptr < sizeof(left->buf) && !eread_stream(left, SIZE_MAX)) { + close(left->fd); + left->fd = -1; + break; + } + if (right->ptr < sizeof(right->buf) && !eread_stream(right, SIZE_MAX)) { + close(right->fd); + right->fd = -1; + break; + } + + n = left->ptr < right->ptr ? left->ptr : right->ptr; + n -= n % left->pixel_size; + left->ptr -= n; + right->ptr -= n; + + process(left, right, n); + + ewriteall(output_fd, left->buf, n, output_fname); + if ((n & 3) || left->ptr != right->ptr) { + memmove(left->buf, left->buf + n, left->ptr); + memmove(right->buf, right->buf + n, right->ptr); + } + } + + if (right->fd >= 0) + close(right->fd); + + ewriteall(output_fd, left->buf, left->ptr, output_fname); + + if (left->fd >= 0) { + for (;;) { + left->ptr = 0; + if (!eread_stream(left, SIZE_MAX)) { + close(left->fd); + left->fd = -1; + break; + } + ewriteall(output_fd, left->buf, left->ptr, output_fname); + } + } +} diff --git a/src/stream.h b/src/stream.h @@ -37,19 +37,8 @@ void encheck_frame_size(int status, size_t width, size_t height, size_t pixel_si void encheck_compat(int status, const struct stream *a, const struct stream *b); int enread_frame(int status, struct stream *stream, void *buf, size_t n); -#define EACH_FRAME_SEGMENTED(stream, process)\ - do {\ - size_t size__, f__, r__, n__;\ - echeck_frame_size((stream)->width, (stream)->height, (stream)->pixel_size, 0, (stream)->file);\ - size__ = (stream)->height * (stream)->width * (stream)->pixel_size;\ - for (f__ = 0; f__ < (stream)->frames; f__++) {\ - for (n__ = size__; n__; n__ -= r__) {\ - if (!eread_stream((stream), n__))\ - eprintf("%s: file is shorter than expected\n", (stream)->file);\ - r__ = (stream)->ptr - ((stream)->ptr % (stream)->pixel_size);\ - (process)((stream), r__, f__);\ - ewriteall(STDOUT_FILENO, (stream)->buf, r__, "<stdout>");\ - memmove((stream)->buf, (stream)->buf + r__, (stream)->ptr -= r__);\ - }\ - }\ - } while (0) +void process_each_frame_segmented(struct stream *stream, int output_fd, const char* output_fname, + void (*process)(struct stream *stream, size_t n, size_t frame)); + +void process_two_streams(struct stream *left, struct stream *right, int output_fd, const char* output_fname, + void (*process)(struct stream *left, struct stream *right, size_t n)); diff --git a/src/vu-arithm.c b/src/vu-arithm.c @@ -57,9 +57,7 @@ LIST_OPERATORS int main(int argc, char *argv[]) { - struct stream left; - struct stream right; - size_t n; + struct stream left, right; process_func process = NULL; ENOFLAGS(argc != 2); @@ -72,55 +70,11 @@ main(int argc, char *argv[]) right.fd = eopen(right.file, O_RDONLY); einit_stream(&right); - echeck_compat(&left, &right); - if (!strcmp(left.pixfmt, "xyza")) process = get_lf_process(argv[0]); else eprintf("pixel format %s is not supported, try xyza\n", left.pixfmt); - for (;;) { - if (left.ptr < sizeof(left.buf) && !eread_stream(&left, SIZE_MAX)) { - close(left.fd); - left.fd = -1; - break; - } - if (right.ptr < sizeof(right.buf) && !eread_stream(&right, SIZE_MAX)) { - close(right.fd); - right.fd = -1; - break; - } - - n = left.ptr < right.ptr ? left.ptr : right.ptr; - n -= n % left.pixel_size; - left.ptr -= n; - right.ptr -= n; - - process(&left, &right, n); - - ewriteall(STDOUT_FILENO, left.buf, n, "<stdout>"); - if ((n & 3) || left.ptr != right.ptr) { - memmove(left.buf, left.buf + n, left.ptr); - memmove(right.buf, right.buf + n, right.ptr); - } - } - - if (right.fd >= 0) - close(right.fd); - - ewriteall(STDOUT_FILENO, left.buf, left.ptr, "<stdout>"); - - if (left.fd >= 0) { - for (;;) { - left.ptr = 0; - if (!eread_stream(&left, SIZE_MAX)) { - close(left.fd); - left.fd = -1; - break; - } - ewriteall(STDOUT_FILENO, left.buf, left.ptr, "<stdout>"); - } - } - + process_two_streams(&left, &right, STDOUT_FILENO, "<stdout>", process); return 0; } diff --git a/src/vu-dissolve.c b/src/vu-dissolve.c @@ -64,7 +64,7 @@ main(int argc, char *argv[]) eprintf("pixel format %s is not supported, try xyza\n", stream.pixfmt); fmd = fm = stream.frames - 1; - EACH_FRAME_SEGMENTED(&stream, process); + process_each_frame_segmented(&stream, STDOUT_FILENO, "<stdout>", process); return 0; } diff --git a/src/vu-invert-luma.c b/src/vu-invert-luma.c @@ -42,7 +42,6 @@ main(int argc, char *argv[]) { int invert = 0; struct stream colour, mask; - size_t n; void (*process)(struct stream *colour, struct stream *mask, size_t n) = NULL; ARGBEGIN { @@ -64,55 +63,11 @@ main(int argc, char *argv[]) mask.fd = eopen(mask.file, O_RDONLY); einit_stream(&mask); - echeck_compat(&colour, &mask); - if (!strcmp(colour.pixfmt, "xyza")) process = invert ? process_xyza_i : process_xyza; else eprintf("pixel format %s is not supported, try xyza\n", colour.pixfmt); - for (;;) { - if (colour.ptr < sizeof(colour.buf) && !eread_stream(&colour, SIZE_MAX)) { - close(colour.fd); - colour.fd = -1; - break; - } - if (mask.ptr < sizeof(mask.buf) && !eread_stream(&mask, SIZE_MAX)) { - close(mask.fd); - mask.fd = -1; - break; - } - - n = colour.ptr < mask.ptr ? colour.ptr : mask.ptr; - n -= n % colour.pixel_size; - colour.ptr -= n; - mask.ptr -= n; - - process(&colour, &mask, n); - - ewriteall(STDOUT_FILENO, colour.buf, n, "<stdout>"); - if ((n & 3) || colour.ptr != mask.ptr) { - memmove(colour.buf, colour.buf + n, colour.ptr); - memmove(mask.buf, mask.buf + n, mask.ptr); - } - } - - if (mask.fd >= 0) - close(mask.fd); - - ewriteall(STDOUT_FILENO, colour.buf, colour.ptr, "<stdout>"); - - if (colour.fd >= 0) { - for (;;) { - colour.ptr = 0; - if (!eread_stream(&colour, SIZE_MAX)) { - close(colour.fd); - colour.fd = -1; - break; - } - ewriteall(STDOUT_FILENO, colour.buf, colour.ptr, "<stdout>"); - } - } - + process_two_streams(&colour, &mask, STDOUT_FILENO, "<stdout>", process); return 0; } diff --git a/src/vu-set-alpha.c b/src/vu-set-alpha.c @@ -38,7 +38,6 @@ main(int argc, char *argv[]) { int invert = 0; struct stream colour, alpha; - size_t n; void (*process)(struct stream *colour, struct stream *alpha, size_t n) = NULL; ARGBEGIN { @@ -60,55 +59,11 @@ main(int argc, char *argv[]) alpha.fd = eopen(alpha.file, O_RDONLY); einit_stream(&alpha); - echeck_compat(&colour, &alpha); - if (!strcmp(colour.pixfmt, "xyza")) process = invert ? process_xyza_i : process_xyza; else eprintf("pixel format %s is not supported, try xyza\n", colour.pixfmt); - for (;;) { - if (colour.ptr < sizeof(colour.buf) && !eread_stream(&colour, SIZE_MAX)) { - close(colour.fd); - colour.fd = -1; - break; - } - if (alpha.ptr < sizeof(alpha.buf) && !eread_stream(&alpha, SIZE_MAX)) { - close(alpha.fd); - alpha.fd = -1; - break; - } - - n = colour.ptr < alpha.ptr ? colour.ptr : alpha.ptr; - n -= n % colour.pixel_size; - colour.ptr -= n; - alpha.ptr -= n; - - process(&colour, &alpha, n); - - ewriteall(STDOUT_FILENO, colour.buf, n, "<stdout>"); - if ((n & 3) || colour.ptr != alpha.ptr) { - memmove(colour.buf, colour.buf + n, colour.ptr); - memmove(alpha.buf, alpha.buf + n, alpha.ptr); - } - } - - if (alpha.fd >= 0) - close(alpha.fd); - - ewriteall(STDOUT_FILENO, colour.buf, colour.ptr, "<stdout>"); - - if (colour.fd >= 0) { - for (;;) { - colour.ptr = 0; - if (!eread_stream(&colour, SIZE_MAX)) { - close(colour.fd); - colour.fd = -1; - break; - } - ewriteall(STDOUT_FILENO, colour.buf, colour.ptr, "<stdout>"); - } - } - + process_two_streams(&colour, &alpha, STDOUT_FILENO, "<stdout>", process); return 0; } diff --git a/src/vu-set-luma.c b/src/vu-set-luma.c @@ -25,7 +25,6 @@ int main(int argc, char *argv[]) { struct stream colour, luma; - size_t n; void (*process)(struct stream *colour, struct stream *luma, size_t n); ENOFLAGS(argc != 1); @@ -38,55 +37,11 @@ main(int argc, char *argv[]) luma.fd = eopen(luma.file, O_RDONLY); einit_stream(&luma); - echeck_compat(&colour, &luma); - if (!strcmp(colour.pixfmt, "xyza")) process = process_xyza; else eprintf("pixel format %s is not supported, try xyza\n", colour.pixfmt); - for (;;) { - if (colour.ptr < sizeof(colour.buf) && !eread_stream(&colour, SIZE_MAX)) { - close(colour.fd); - colour.fd = -1; - break; - } - if (luma.ptr < sizeof(luma.buf) && !eread_stream(&luma, SIZE_MAX)) { - close(luma.fd); - luma.fd = -1; - break; - } - - n = colour.ptr < luma.ptr ? colour.ptr : luma.ptr; - n -= n % colour.pixel_size; - colour.ptr -= n; - luma.ptr -= n; - - process(&colour, &luma, n); - - ewriteall(STDOUT_FILENO, colour.buf, n, "<stdout>"); - if ((n & 3) || colour.ptr != luma.ptr) { - memmove(colour.buf, colour.buf + n, colour.ptr); - memmove(luma.buf, luma.buf + n, luma.ptr); - } - } - - if (luma.fd >= 0) - close(luma.fd); - - ewriteall(STDOUT_FILENO, colour.buf, colour.ptr, "<stdout>"); - - if (colour.fd >= 0) { - for (;;) { - colour.ptr = 0; - if (!eread_stream(&colour, SIZE_MAX)) { - close(colour.fd); - colour.fd = -1; - break; - } - ewriteall(STDOUT_FILENO, colour.buf, colour.ptr, "<stdout>"); - } - } - + process_two_streams(&colour, &luma, STDOUT_FILENO, "<stdout>", process); return 0; } diff --git a/src/vu-set-saturation.c b/src/vu-set-saturation.c @@ -48,7 +48,6 @@ main(int argc, char *argv[]) { struct stream colour, satur; int whitepoint = 0; - size_t n; void (*process)(struct stream *colour, struct stream *satur, size_t n) = NULL; ARGBEGIN { @@ -70,55 +69,11 @@ main(int argc, char *argv[]) satur.fd = eopen(satur.file, O_RDONLY); einit_stream(&satur); - echeck_compat(&colour, &satur); - if (!strcmp(colour.pixfmt, "xyza")) process = whitepoint ? process_xyza_w : process_xyza; else eprintf("pixel format %s is not supported, try xyza\n", colour.pixfmt); - for (;;) { - if (colour.ptr < sizeof(colour.buf) && !eread_stream(&colour, SIZE_MAX)) { - close(colour.fd); - colour.fd = -1; - break; - } - if (satur.ptr < sizeof(satur.buf) && !eread_stream(&satur, SIZE_MAX)) { - close(satur.fd); - satur.fd = -1; - break; - } - - n = colour.ptr < satur.ptr ? colour.ptr : satur.ptr; - n -= n % colour.pixel_size; - colour.ptr -= n; - satur.ptr -= n; - - process(&colour, &satur, n); - - ewriteall(STDOUT_FILENO, colour.buf, n, "<stdout>"); - if ((n & 3) || colour.ptr != satur.ptr) { - memmove(colour.buf, colour.buf + n, colour.ptr); - memmove(satur.buf, satur.buf + n, satur.ptr); - } - } - - if (satur.fd >= 0) - close(satur.fd); - - ewriteall(STDOUT_FILENO, colour.buf, colour.ptr, "<stdout>"); - - if (colour.fd >= 0) { - for (;;) { - colour.ptr = 0; - if (!eread_stream(&colour, SIZE_MAX)) { - close(colour.fd); - colour.fd = -1; - break; - } - ewriteall(STDOUT_FILENO, colour.buf, colour.ptr, "<stdout>"); - } - } - + process_two_streams(&colour, &satur, STDOUT_FILENO, "<stdout>", process); return 0; }