aboutsummaryrefslogtreecommitdiffstats
path: root/src/vu-transpose.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2017-01-11 09:11:51 +0100
committerMattias Andrée <maandree@kth.se>2017-01-11 09:11:51 +0100
commitb7a82c980fe7e0c1f9029b55be97422428d65d5a (patch)
tree67bedc856eb1f72a2daa8ef8347b904269b06df5 /src/vu-transpose.c
parentvu-crop: add -t (diff)
downloadblind-b7a82c980fe7e0c1f9029b55be97422428d65d5a.tar.gz
blind-b7a82c980fe7e0c1f9029b55be97422428d65d5a.tar.bz2
blind-b7a82c980fe7e0c1f9029b55be97422428d65d5a.tar.xz
Clean up code
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src/vu-transpose.c')
-rw-r--r--src/vu-transpose.c77
1 files changed, 18 insertions, 59 deletions
diff --git a/src/vu-transpose.c b/src/vu-transpose.c
index d9b7aeb..0c32d81 100644
--- a/src/vu-transpose.c
+++ b/src/vu-transpose.c
@@ -1,5 +1,4 @@
/* See LICENSE file for copyright and license details. */
-#include "arg.h"
#include "stream.h"
#include "util.h"
@@ -8,79 +7,39 @@
#include <string.h>
#include <unistd.h>
-static void
-transpose(const struct stream *stream, const char *restrict buf, char *restrict image)
-{
- size_t x, y, i, b = 0, h, w;
- w = stream->width * (h = stream->height * stream->pixel_size);
- for (y = 0; y < h; y += stream->pixel_size)
- for (x = 0; x < w; x += h)
- for (i = 0; i < stream->pixel_size; i++)
- image[x + y + i] = buf[b++];
-}
-
-static void
-usage(void)
-{
- eprintf("usage: %s\n", argv0);
-}
+USAGE("")
int
main(int argc, char *argv[])
{
struct stream stream;
char *buf, *image;
- size_t ptr, n;
- ssize_t r;
-
- ARGBEGIN {
- default:
- usage();
- } ARGEND;
+ size_t n, imgw, imgh, x, y, i, b;
- if (argc)
- usage();
+ ENOFLAGS(argc);
stream.file = "<stdin>";
stream.fd = STDIN_FILENO;
einit_stream(&stream);
fprint_stream_head(stdout, &stream);
- fflush(stdout);
- if (ferror(stdout))
- eprintf("<stdout>:");
+ efflush(stdout, "<stdout>");
- if (stream.width > SIZE_MAX / stream.pixel_size)
- eprintf("<stdin>: video frame is too large\n");
- n = stream.width * stream.pixel_size;
- if (n > SIZE_MAX / stream.height)
- eprintf("<stdin>: video frame is too large\n");
- n *= stream.height;
- if (!(buf = malloc(n)))
- eprintf("malloc:");
- if (!(image = malloc(n)))
- eprintf("malloc:");
+ echeck_frame_size(stream.width, stream.height, stream.pixel_size, 0, "<stdin>");
+ n = stream.width * stream.height * stream.pixel_size;
+ buf = emalloc(n);
+ image = emalloc(n);
- memcpy(buf, stream.buf, ptr = stream.ptr);
- for (;;) {
- for (; ptr < n; ptr += (size_t)r) {
- r = read(stream.fd, buf + ptr, n - ptr);
- if (r < 0) {
- eprintf("read %s:", stream.file);
- } else if (r == 0) {
- if (!ptr)
- break;
- eprintf("%s: incomplete frame", stream.file);
- }
- }
- if (!ptr)
- break;
- transpose(&stream, buf, image);
- for (ptr = 0; ptr < n; ptr += (size_t)r) {
- r = write(STDOUT_FILENO, image + ptr, n - ptr);
- if (r < 0)
- eprintf("write <stdout>");
- }
+ imgw = stream.width * (imgh = stream.height * stream.pixel_size);
+ memcpy(buf, stream.buf, stream.ptr);
+ while (eread_frame(&stream, buf, n)) {
+ for (b = y = 0; y < imgh; y += stream.pixel_size)
+ for (x = 0; x < imgw; x += imgh)
+ for (i = 0; i < stream.pixel_size; i++)
+ image[x + y + i] = buf[b++];
+ ewriteall(STDOUT_FILENO, image, n, "<stdout>");
}
+ free(buf);
+ free(image);
return 0;
}