blind

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

commit deec79ce61b8661d3ad76f3ecc10a80d1ce19cdd
parent 4a3ac9442a7e6fb810f3c56b4a00ffa120cae475
Author: Mattias Andrée <maandree@kth.se>
Date:   Sun,  9 Apr 2017 17:33:31 +0200

Clean up

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

Diffstat:
Msrc/blind-arithm.c | 29+++++++++--------------------
Msrc/blind-crop.c | 2+-
Msrc/blind-cut.c | 2+-
Msrc/blind-decompress.c | 6+++---
Msrc/blind-flip.c | 6+++---
Msrc/blind-next-frame.c | 10+++++-----
Msrc/blind-single-colour.c | 2+-
Msrc/blind-split.c | 2+-
Msrc/blind-to-image.c | 8++++----
Msrc/blind-to-video.c | 14+++++++-------
Msrc/blind-translate.c | 8++++----
Msrc/blind-transpose.c | 2+-
Msrc/stream.c | 9++++-----
Msrc/util.c | 2+-
Msrc/util.h | 3+++
15 files changed, 48 insertions(+), 57 deletions(-)

diff --git a/src/blind-arithm.c b/src/blind-arithm.c @@ -25,34 +25,23 @@ typedef void (*process_func)(struct stream *left, struct stream *right, size_t n X(div, *lh /= rh)\ X(exp, *lh = pow(*lh, rh))\ X(log, *lh = log(*lh) / log(rh))\ - X(min, *lh = *lh < rh ? *lh : rh)\ - X(max, *lh = *lh > rh ? *lh : rh)\ + X(min, *lh = MIN(*lh, rh))\ + X(max, *lh = MAX(*lh, rh))\ X(abs, *lh = fabs(*lh - rh) + rh) +#define C(CH, CHI, ALGO)\ + (!skip_##CH ? ((lh = ((double *)(left->buf + i)) + (CHI),\ + rh = ((double *)(right->buf + i))[CHI],\ + (ALGO)), 0) : 0) + #define X(NAME, ALGO)\ static void\ process_lf_##NAME(struct stream *left, struct stream *right, size_t n)\ {\ size_t i;\ double *lh, rh;\ - for (i = 0; i < n; i += 4 * sizeof(double)) {\ - if (!skip_x) {\ - lh = ((double *)(left->buf + i)) + 0, rh = ((double *)(right->buf + i))[0];\ - ALGO;\ - }\ - if (!skip_y) {\ - lh = ((double *)(left->buf + i)) + 1, rh = ((double *)(right->buf + i))[1];\ - ALGO;\ - }\ - if (!skip_z) {\ - lh = ((double *)(left->buf + i)) + 2, rh = ((double *)(right->buf + i))[2];\ - ALGO;\ - }\ - if (!skip_a) {\ - lh = ((double *)(left->buf + i)) + 3, rh = ((double *)(right->buf + i))[3];\ - ALGO;\ - }\ - }\ + for (i = 0; i < n; i += 4 * sizeof(double))\ + C(x, 0, ALGO), C(y, 1, ALGO), C(z, 2, ALGO), C(a, 3, ALGO);\ } LIST_OPERATORS #undef X diff --git a/src/blind-crop.c b/src/blind-crop.c @@ -63,7 +63,7 @@ main(int argc, char *argv[]) buf = emalloc(n); orown = width * stream.pixel_size; m = (tile || keepsize || keepsize_inv) ? n : height * orown; - image = (keepsize || keepsize_inv) ? buf : malloc(m); + image = (keepsize || keepsize_inv) ? buf : emalloc(m); left *= stream.pixel_size; if (!tile) { diff --git a/src/blind-cut.c b/src/blind-cut.c @@ -58,7 +58,7 @@ main(int argc, char *argv[]) #endif for (ptr = start; ptr < end; ptr += (size_t)r) { max = end - ptr; - max = max < sizeof(buf) ? max : sizeof(buf); + max = MIN(max, sizeof(buf)); r = pread(stream.fd, buf, max, ptr); if (r < 0) eprintf("pread %s:", stream.file); diff --git a/src/blind-decompress.c b/src/blind-decompress.c @@ -31,15 +31,15 @@ main(int argc, char *argv[]) sptr = 0; again: while (same) { - m = same < n - fptr ? same : n - fptr; + m = MIN(same, n - fptr); ewriteall(STDOUT_FILENO, buf + fptr, m, "<stdout>"); fptr = (fptr + m) % n; same -= m; } while (diff && sptr < stream.ptr) { - m = diff < n - fptr ? diff : n - fptr; - m = m < stream.ptr - sptr ? m : stream.ptr - sptr; + m = MIN(diff, n - fptr); + m = MIN(m, stream.ptr - sptr); memcpy(buf + fptr, stream.buf + sptr, m); ewriteall(STDOUT_FILENO, buf + fptr, m, "<stdout>"); fptr = (fptr + m) % n; diff --git a/src/blind-flip.c b/src/blind-flip.c @@ -11,7 +11,7 @@ int main(int argc, char *argv[]) { struct stream stream; - size_t n, ptr, row_size; + size_t n, rown, ptr; char *buf; UNOFLAGS(argc); @@ -23,12 +23,12 @@ main(int argc, char *argv[]) efflush(stdout, "<stdout>"); echeck_frame_size(stream.width, stream.height, stream.pixel_size, 0, stream.file); - n = stream.height * (row_size = stream.width * stream.pixel_size); + n = stream.height * (rown = stream.width * stream.pixel_size); buf = emalloc(n); while (eread_frame(&stream, buf, n)) for (ptr = n; ptr;) - ewriteall(STDOUT_FILENO, buf + (ptr -= row_size), row_size, "<stdout>"); + ewriteall(STDOUT_FILENO, buf + (ptr -= rown), rown, "<stdout>"); free(buf); return 0; diff --git a/src/blind-next-frame.c b/src/blind-next-frame.c @@ -22,6 +22,8 @@ main(int argc, char *argv[]) case 'f': stream.frames = etozu_flag('f', UARGF(), 1, SIZE_MAX); break; + default: + usage(); } ARGEND; if (argc < 3) @@ -52,10 +54,8 @@ main(int argc, char *argv[]) enfflush(2, stdout, "<stdout>"); w = stream.width * stream.pixel_size; - while (stream.frames) { - stream.frames--; - for (h = stream.height; h;) { - h--; + for (; stream.frames; stream.frames--) { + for (h = stream.height; h; h--) { for (n = w; n; n -= stream.ptr) { stream.ptr = 0; if (!enread_stream(2, &stream, n)) @@ -67,7 +67,7 @@ main(int argc, char *argv[]) } done: - if (anything && (h || n || stream.frames)) + if (anything && stream.frames) enprintf(2, "%s: is shorted than expected\n", stream.file); return !anything; diff --git a/src/blind-single-colour.c b/src/blind-single-colour.c @@ -76,7 +76,7 @@ main(int argc, char *argv[]) while (inf || stream.frames--) { for (y = stream.height; y--;) { for (x = stream.width; x;) { - x -= n = ELEMENTSOF(buf) < x ? ELEMENTSOF(buf) : x; + x -= n = MIN(ELEMENTSOF(buf), x); for (n *= sizeof(*buf); n; n -= (size_t)r) { r = write(STDOUT_FILENO, buf, n); if (r < 0) diff --git a/src/blind-split.c b/src/blind-split.c @@ -71,7 +71,7 @@ main(int argc, char *argv[]) for (end = to_end[i] ? SIZE_MAX : ends[i] * frame_size; ptr < end; ptr += n) { n = end - ptr; if (stream.ptr) { - n = stream.ptr < n ? stream.ptr : n; + n = MIN(stream.ptr, n); ewriteall(fd, stream.buf, n, argv[i * 2]); memmove(stream.buf, stream.buf + n, stream.ptr -= n); } else if ((n = eread_stream(&stream, n))) { diff --git a/src/blind-to-image.c b/src/blind-to-image.c @@ -28,9 +28,9 @@ write_pixel(double R, double G, double B, double A, int bytes, unsigned long lon weprintf("warning: out-of-gamut colour detected\n"); } ; /* TODO gamut */ - R = R < 0 ? 0 : R > 1 ? 1 : R; - G = G < 0 ? 0 : G > 1 ? 1 : G; - B = B < 0 ? 0 : B > 1 ? 1 : B; + R = CLIP(0, R, 1); + G = CLIP(0, G, 1); + B = CLIP(0, B, 1); } if (A < 0 || A > 1) { @@ -133,7 +133,7 @@ main(int argc, char *argv[]) printf("P7\n" "WIDTH %zu\n" "HEIGHT %zu\n" - "DEPTH 4\n" /* Depth actually means channels */ + "DEPTH 4\n" /* channels */ "MAXVAL %llu\n" "TUPLTYPE RGB_ALPHA\n" "ENDHDR\n", stream.width, stream.height, max); diff --git a/src/blind-to-video.c b/src/blind-to-video.c @@ -34,9 +34,9 @@ process_xyza(char *buf, size_t n, int fd, const char *fname) u = (long int)g + 128L * 256L; v = (long int)b + 128L * 256L; *pixels++ = 0xFFFFU; - *pixels++ = htole16((uint16_t)(y < 0 ? 0 : y > 0xFFFFL ? 0xFFFFL : y)); - *pixels++ = htole16((uint16_t)(u < 0 ? 0 : u > 0xFFFFL ? 0xFFFFL : u)); - *pixels++ = htole16((uint16_t)(v < 0 ? 0 : v > 0xFFFFL ? 0xFFFFL : v)); + *pixels++ = htole16((uint16_t)CLIP(0, y, 0xFFFFL)); + *pixels++ = htole16((uint16_t)CLIP(0, u, 0xFFFFL)); + *pixels++ = htole16((uint16_t)CLIP(0, v, 0xFFFFL)); if (pixels == end) ewriteall(fd, pixels = pixbuf, sizeof(pixbuf), fname); } @@ -52,10 +52,10 @@ process_xyza(char *buf, size_t n, int fd, const char *fname) y = (long int)(pixel[0] * 0xFFFFL) + 16L * 256L; u = (long int)(pixel[1] * 0xFFFFL) + 128L * 256L; v = (long int)(pixel[2] * 0xFFFFL) + 128L * 256L; - *pixels++ = htole16((uint16_t)(a < 0 ? 0 : a > 0xFFFFL ? 0xFFFFL : a)); - *pixels++ = htole16((uint16_t)(y < 0 ? 0 : y > 0xFFFFL ? 0xFFFFL : y)); - *pixels++ = htole16((uint16_t)(u < 0 ? 0 : u > 0xFFFFL ? 0xFFFFL : u)); - *pixels++ = htole16((uint16_t)(v < 0 ? 0 : v > 0xFFFFL ? 0xFFFFL : v)); + *pixels++ = htole16((uint16_t)CLIP(0, a, 0xFFFFL)); + *pixels++ = htole16((uint16_t)CLIP(0, y, 0xFFFFL)); + *pixels++ = htole16((uint16_t)CLIP(0, u, 0xFFFFL)); + *pixels++ = htole16((uint16_t)CLIP(0, v, 0xFFFFL)); if (pixels == end) ewriteall(fd, pixels = pixbuf, sizeof(pixbuf), fname); } diff --git a/src/blind-translate.c b/src/blind-translate.c @@ -95,10 +95,10 @@ process(struct stream *stream, struct stream *trstream) left = (trx > 0 ? (size_t)trx : 0) * stream->pixel_size; right = (trx < 0 ? (size_t)-trx : 0) * stream->pixel_size; - above = above < stream->height ? above : stream->height; - below = below < stream->height ? below : stream->height; - left = left < n ? left : n; - right = right < n ? right : n; + above = MIN(above, stream->height); + below = MIN(below, stream->height); + left = MIN(left, n); + right = MIN(right, n); } } while (process_frame(stream, buf, n, above, below, left, right)); diff --git a/src/blind-transpose.c b/src/blind-transpose.c @@ -30,7 +30,7 @@ main(int argc, char *argv[]) echeck_frame_size(stream.width, stream.height, stream.pixel_size, 0, "<stdin>"); n = stream.width * stream.height * (ps = stream.pixel_size); buf = emalloc(n); - image = emalloc(n); + image = emalloc(n); /* TODO optimise to a frame row */ srch *= ps; srcw *= dx = imgw * ps; diff --git a/src/stream.c b/src/stream.c @@ -124,8 +124,7 @@ size_t enread_stream(int status, struct stream *stream, size_t n) { ssize_t r = read(stream->fd, stream->buf + stream->ptr, - sizeof(stream->buf) - stream->ptr < n ? - sizeof(stream->buf) - stream->ptr : n); + MIN(sizeof(stream->buf) - stream->ptr, n)); if (r < 0) enprintf(status, "read %s:", stream->file); stream->ptr += (size_t)r; @@ -183,7 +182,7 @@ enread_frame(int status, struct stream *stream, void *buf, size_t n) size_t m; if (stream->ptr) { - m = stream->ptr < n ? stream->ptr : n; + m = MIN(stream->ptr, n); memcpy(buffer + stream->xptr, stream->buf, m); memmove(stream->buf, stream->buf + m, stream->ptr -= m); stream->xptr += m; @@ -221,7 +220,7 @@ nprocess_each_frame_segmented(int status, struct stream *stream, int output_fd, if (stream->ptr < n && !enread_stream(status, stream, SIZE_MAX)) enprintf(status, "%s: file is shorter than expected\n", stream->file); r = stream->ptr - (stream->ptr % stream->pixel_size); - r = r < n ? r : n; + r = MIN(r, n); (process)(stream, r, frame); enwriteall(status, output_fd, stream->buf, r, output_fname); memmove(stream->buf, stream->buf + r, stream->ptr -= r); @@ -250,7 +249,7 @@ nprocess_two_streams(int status, struct stream *left, struct stream *right, int break; } - n = left->ptr < right->ptr ? left->ptr : right->ptr; + n = MIN(left->ptr, right->ptr); n -= n % left->pixel_size; left->ptr -= n; right->ptr -= n; diff --git a/src/util.c b/src/util.c @@ -162,7 +162,7 @@ writezeroes(int fd, void *buf, size_t bufsize, size_t n) { size_t p, m; for (p = 0; p < n; p += m) { - m = bufsize < n - p ? bufsize : n - p; + m = MIN(bufsize, n - p); if (writeall(fd, buf, m)) return -1; } diff --git a/src/util.h b/src/util.h @@ -2,6 +2,9 @@ #include "arg.h" #define ELEMENTSOF(ARRAY) (sizeof(ARRAY) / sizeof(*(ARRAY))) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define CLIP(A, B, C) ((B) < (A) ? (A) : (B) > (C) ? (C) : (B)) #define USAGE(SYNOPSIS)\ static void usage(void)\