commit deec79ce61b8661d3ad76f3ecc10a80d1ce19cdd
Author: Mattias Andrée <maandree_AT_kth.se>
AuthorDate: Sun Apr 9 17:33:31 2017 +0200
Commit: Mattias Andrée <maandree_AT_kth.se>
CommitDate: Sun Apr 9 17:57:27 2017 +0200
Clean up
Signed-off-by: Mattias Andrée <maandree_AT_kth.se>
diff --git a/src/blind-arithm.c b/src/blind-arithm.c
index bc30bee..613783e 100644
--- a/src/blind-arithm.c
+++ b/src/blind-arithm.c
_AT_@ -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
index c08864e..fa76762 100644
--- a/src/blind-crop.c
+++ b/src/blind-crop.c
_AT_@ -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
index c1e1bdc..d9efacf 100644
--- a/src/blind-cut.c
+++ b/src/blind-cut.c
_AT_@ -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
index f01fa8f..6636ee0 100644
--- a/src/blind-decompress.c
+++ b/src/blind-decompress.c
_AT_@ -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
index b37a9d8..3dd108d 100644
--- a/src/blind-flip.c
+++ b/src/blind-flip.c
_AT_@ -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);
_AT_@ -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
index 41ab0ca..14d2326 100644
--- a/src/blind-next-frame.c
+++ b/src/blind-next-frame.c
_AT_@ -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)
_AT_@ -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))
_AT_@ -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
index a0f5ccb..955511a 100644
--- a/src/blind-single-colour.c
+++ b/src/blind-single-colour.c
_AT_@ -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
index 90dee19..78c569a 100644
--- a/src/blind-split.c
+++ b/src/blind-split.c
_AT_@ -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
index 4512085..48cf625 100644
--- a/src/blind-to-image.c
+++ b/src/blind-to-image.c
_AT_@ -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) {
_AT_@ -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
index 04cfd91..4da9306 100644
--- a/src/blind-to-video.c
+++ b/src/blind-to-video.c
_AT_@ -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);
}
_AT_@ -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
index 95b8dab..fc6cf6f 100644
--- a/src/blind-translate.c
+++ b/src/blind-translate.c
_AT_@ -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
index 9421fc5..c640d36 100644
--- a/src/blind-transpose.c
+++ b/src/blind-transpose.c
_AT_@ -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
index 63f92c6..483ca83 100644
--- a/src/stream.c
+++ b/src/stream.c
_AT_@ -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;
_AT_@ -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;
_AT_@ -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);
_AT_@ -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
index 92b4d20..21b2b69 100644
--- a/src/util.c
+++ b/src/util.c
_AT_@ -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
index a9704ec..d948452 100644
--- a/src/util.h
+++ b/src/util.h
_AT_@ -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)\
Received on Sun Apr 09 2017 - 23:53:57 CEST
This archive was generated by hypermail 2.3.0
: Mon Apr 10 2017 - 00:00:56 CEST