diff options
| author | Mattias Andrée <maandree@kth.se> | 2017-01-08 09:08:54 +0100 |
|---|---|---|
| committer | Mattias Andrée <maandree@kth.se> | 2017-01-08 09:11:23 +0100 |
| commit | bfc501a39adef716ef022c51e3a7757ae55f0c71 (patch) | |
| tree | 6395661d19c0d0856c37c61fee634455ef7e11d4 /src/vu-frame-to-image.c | |
| parent | Rewrite vu-image-to-frame in C for performance and proper error handling (diff) | |
| download | blind-bfc501a39adef716ef022c51e3a7757ae55f0c71.tar.gz blind-bfc501a39adef716ef022c51e3a7757ae55f0c71.tar.bz2 blind-bfc501a39adef716ef022c51e3a7757ae55f0c71.tar.xz | |
Add vu-frame-to-image
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src/vu-frame-to-image.c')
| -rw-r--r-- | src/vu-frame-to-image.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/vu-frame-to-image.c b/src/vu-frame-to-image.c new file mode 100644 index 0000000..e4466cf --- /dev/null +++ b/src/vu-frame-to-image.c @@ -0,0 +1,67 @@ +/* See LICENSE file for copyright and license details. */ +#include "arg.h" +#include "util.h" + +#include <inttypes.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +static void +usage(void) +{ + eprintf("usage: %s -w width -h height\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + size_t width = 0, height = 0; + size_t p, n; + ssize_t r; + char buf[8096]; + + ARGBEGIN { + case 'w': + if (tozu(EARGF(usage()), 1, SIZE_MAX, &width)) + eprintf("argument of -w must be an integer in [1, %zu]\n", SIZE_MAX); + break; + case 'h': + if (tozu(EARGF(usage()), 1, SIZE_MAX, &height)) + eprintf("argument of -h must be an integer in [1, %zu]\n", SIZE_MAX); + break; + default: + usage(); + } ARGEND; + + if (!width || !height || argc) + usage(); + + printf("P7\n" + "WIDTH %zu\n" + "HEIGHT %zu\n" + "DEPTH 4\n" + "MAXVAL 255\n" + "TUPLTYPE RGB_ALPHA\n" + "ENDHDR\n", width, height); + fflush(stdout); + if (ferror(stdout)) + eprintf("<stdout>:"); + + for (n = 0;;) { + for (p = 0; p < n;) { + r = write(STDOUT_FILENO, buf + p, n - p); + if (r < 0) + eprintf("write <stdin>:"); + p += (size_t)r; + } + r = read(STDIN_FILENO, buf, sizeof(buf)); + if (r < 0) + eprintf("read <stdin>:"); + if (r == 0) + break; + n = (size_t)r; + } + + return 0; +} |
