From e5f405e03c8f786395ab88b8fbce5c53973cad1d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 10 Jan 2017 02:31:16 +0100 Subject: Rename vu-image-to-frame to vu-from-image and vu-frame-to-image to vu-to-image MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/vu-frame-to-image.c | 149 --------------------------------------------- src/vu-from-image.c | 159 ++++++++++++++++++++++++++++++++++++++++++++++++ src/vu-image-to-frame.c | 159 ------------------------------------------------ src/vu-to-image.c | 149 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 308 insertions(+), 308 deletions(-) delete mode 100644 src/vu-frame-to-image.c create mode 100644 src/vu-from-image.c delete mode 100644 src/vu-image-to-frame.c create mode 100644 src/vu-to-image.c (limited to 'src') diff --git a/src/vu-frame-to-image.c b/src/vu-frame-to-image.c deleted file mode 100644 index 3b93dd9..0000000 --- a/src/vu-frame-to-image.c +++ /dev/null @@ -1,149 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include "arg.h" -#include "stream.h" -#include "util.h" - -#include -#include -#include -#include -#include - -static int luma_warning_triggered = 0; -static int gamut_warning_triggered = 0; -static int alpha_warning_triggered = 0; - -static void -usage(void) -{ - eprintf("usage: %s [-d depth]\n", argv0); -} - -static void -write_pixel(double R, double G, double B, double A, int bytes, unsigned long long int max) -{ - unsigned long long int colours[4]; - unsigned char buf[4 * 8]; - int i, j, k, bm = bytes - 1; - size_t ptr, n; - ssize_t r; - - if (R < 0 || G < 0 || B < 0 || R > 1 || G > 1 || B > 1) { - if (gamut_warning_triggered) { - gamut_warning_triggered = 1; - 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; - } - - if (A < 0 || A > 1) { - if (alpha_warning_triggered) { - alpha_warning_triggered = 1; - weprintf("warning: alpha values truncated\n"); - } - A = A < 0 ? 0 : 1; - } - - colours[0] = srgb_encode(R) * max + 0.5; - colours[1] = srgb_encode(G) * max + 0.5; - colours[2] = srgb_encode(B) * max + 0.5; - colours[3] = A * max + 0.5; - - for (i = k = 0; i < 4; i++) { - for (j = 0; j < bytes; j++, k++) { - buf[k + bm - j] = (unsigned char)(colours[j]); - colours[j] >>= 8; - } - } - - n = (size_t)bytes * 4; - for (ptr = 0; ptr < n; ptr += (size_t)r) { - r = write(STDOUT_FILENO, buf + ptr, n - ptr); - if (r < 0) - eprintf("write :"); - } -} - -static void -process_xyza(struct stream *stream, size_t n, int bytes, unsigned long long int max) -{ - size_t i; - double X, Y, Z, A, R, G, B; - for (i = 0; i < n; i += stream->pixel_size) { - X = ((double *)(stream->buf + i))[0]; - Y = ((double *)(stream->buf + i))[1]; - Z = ((double *)(stream->buf + i))[2]; - A = ((double *)(stream->buf + i))[3]; - - if (Y < 0 || Y > 1) { - if (luma_warning_triggered) { - luma_warning_triggered = 1; - weprintf("warning: %s colour detected\n", - Y < 0 ? "subblack" : "superwhite"); - } - } - - srgb_to_ciexyz(X, Y, Z, &R, &G, &B); - write_pixel(R, G, B, A, bytes, max); - } -} - -int -main(int argc, char *argv[]) -{ - struct stream stream; - int depth = 16, bytes; - unsigned long long int max; - size_t n; - void (*process)(struct stream *stream, size_t n, int bytes, unsigned long long int max); - - ARGBEGIN { - case 'd': - if (toi(EARGF(usage()), 1, 64, &depth)) - eprintf("argument of -d must be an integer in [1, 64]\n"); - break; - default: - usage(); - } ARGEND; - - if (argc) - usage(); - - stream.fd = STDIN_FILENO; - stream.file = ""; - einit_stream(&stream); - - max = 1ULL << (depth - 1); - max |= max - 1; - for (bytes = 1; bytes * 8 < depth; bytes++); - - if (!strcmp(stream.pixfmt, "xyza")) - process = process_xyza; - else - eprintf("pixel format %s is not supported, try xyza\n", stream.pixfmt); - - printf("P7\n" - "WIDTH %zu\n" - "HEIGHT %zu\n" - "DEPTH 4\n" /* Depth actually means channels */ - "MAXVAL %llu\n" - "TUPLTYPE RGB_ALPHA\n" - "ENDHDR\n", stream.width, stream.height, max); - fflush(stdout); - if (ferror(stdout)) - eprintf(":"); - - for (;;) { - n = stream.ptr; - n -= n % stream.pixel_size; - process(&stream, n, bytes, max); - memmove(stream.buf, stream.buf + n, stream.ptr -= n); - if (!eread_stream(&stream, SIZE_MAX)) - break; - } - - return 0; -} diff --git a/src/vu-from-image.c b/src/vu-from-image.c new file mode 100644 index 0000000..7e70999 --- /dev/null +++ b/src/vu-from-image.c @@ -0,0 +1,159 @@ +/* See LICENSE file for copyright and license details. */ +#include "arg.h" +#include "util.h" + +#include +#include +#include + +static double +get_value(void *buffer) +{ + unsigned char *buf = buffer; + unsigned long int value; + double ret; + value = (unsigned long int)(buf[0]) << 12; + value += (unsigned long int)(buf[1]) << 8; + value += (unsigned long int)(buf[2]) << 4; + value += (unsigned long int)(buf[3]); + ret = value; + value = 1UL << 15; + value |= value - 1; + ret /= value; + return ret; +} + +static void +usage(void) +{ + eprintf("usage: [-h] %s\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + int pipe_rw[2]; + int i, old_fd; + pid_t pid; + int status; + char buf[8096]; + size_t ptr, ptw, n; + char *p; + ssize_t r; + double red, green, blue, pixel[4]; + char width[3 * sizeof(size_t) + 1] = {0}; + char height[3 * sizeof(size_t) + 1] = {0}; + int headless = 0; + + ARGBEGIN { + case 'h': + headless = 1; + break; + default: + usage(); + } ARGEND; + if (argc) + usage(); + + if (pipe(pipe_rw)) + eprintf("pipe:"); + + if (pipe_rw[0] == STDIN_FILENO || pipe_rw[1] == STDIN_FILENO) + eprintf("no stdin open\n"); + if (pipe_rw[0] == STDOUT_FILENO || pipe_rw[1] == STDOUT_FILENO) + eprintf("no stdout open\n"); + for (i = 0; i < 2; i++) { + if (pipe_rw[i] == STDERR_FILENO) { + pipe_rw[i] = dup(old_fd = pipe_rw[i]); + if (pipe_rw[i] < 0) + eprintf("dup:"); + close(old_fd); + } + } + + pid = fork(); + if (pid < 0) + eprintf("fork:"); + + if (!pid) { + close(pipe_rw[0]); + if (dup2(pipe_rw[1], STDOUT_FILENO) < 0) + eprintf("dup2:"); + close(pipe_rw[1]); + /* XXX Is there a way to convert directly to raw XYZ? (Would avoid gamut truncation) */ + execlp("convert", "convert", "-", "-depth", "32", "-alpha", "activate", "pam:-", NULL); + eprintf("exec convert:"); + } + + close(pipe_rw[1]); + + for (ptr = 0;;) { + r = read(pipe_rw[0], buf + ptr, sizeof(buf) - ptr); + if (r < 0) + eprintf("read :"); + if (r == 0) + eprintf("convertion failed\n"); + ptr += (size_t)r; + + for (;;) { + p = memchr(buf, '\n', ptr); + if (!p) { + if (ptr == sizeof(buf)) + eprintf("convertion failed\n"); + break; + } + *p++ = '\0'; + if (strstr(buf, "WIDTH ") == buf) { + if (*width || !buf[6] || strlen(buf + 6) >= sizeof(width)) + eprintf("convertion failed\n"); + strcpy(width, buf + 6); + } else if (strstr(buf, "HEIGHT ") == buf) { + if (*height || !buf[7] || strlen(buf + 7) >= sizeof(height)) + eprintf("convertion failed\n"); + strcpy(height, buf + 7); + } else if (!strcmp(buf, "ENDHDR")) { + memmove(buf, p, ptr -= (size_t)(p - buf)); + goto header_done; + } + memmove(buf, p, ptr -= (size_t)(p - buf)); + } + } +header_done: + n = ptr; + + if (!*width || !*height) + eprintf("convertion failed\n"); + + if (!headless) { + printf("%s %s xyza\n%cuivf", width, height, 0); + fflush(stdout); + if (ferror(stdout)) + eprintf(":"); + } + + for (;;) { + for (ptr = 0; ptr + 15 < n; ptr += 16) { + red = get_value(buf + ptr + 0); + green = get_value(buf + ptr + 4); + blue = get_value(buf + ptr + 8); + pixel[3] = get_value(buf + ptr + 12); + + srgb_to_ciexyz(red, green, blue, pixel + 0, pixel + 1, pixel + 2); + + for (ptw = 0; ptw < sizeof(pixel); ptw += (size_t)r) { + r = write(STDOUT_FILENO, (char *)pixel + ptw, sizeof(pixel) - ptw); + if (r < 0) + eprintf("write :"); + } + } + r = read(pipe_rw[0], buf, sizeof(buf)); + if (r < 0) + eprintf("read :"); + if (r == 0) + break; + n = (size_t)r; + } + + while (waitpid(pid, &status, 0) != pid); + return !!status; +} diff --git a/src/vu-image-to-frame.c b/src/vu-image-to-frame.c deleted file mode 100644 index 7e70999..0000000 --- a/src/vu-image-to-frame.c +++ /dev/null @@ -1,159 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include "arg.h" -#include "util.h" - -#include -#include -#include - -static double -get_value(void *buffer) -{ - unsigned char *buf = buffer; - unsigned long int value; - double ret; - value = (unsigned long int)(buf[0]) << 12; - value += (unsigned long int)(buf[1]) << 8; - value += (unsigned long int)(buf[2]) << 4; - value += (unsigned long int)(buf[3]); - ret = value; - value = 1UL << 15; - value |= value - 1; - ret /= value; - return ret; -} - -static void -usage(void) -{ - eprintf("usage: [-h] %s\n", argv0); -} - -int -main(int argc, char *argv[]) -{ - int pipe_rw[2]; - int i, old_fd; - pid_t pid; - int status; - char buf[8096]; - size_t ptr, ptw, n; - char *p; - ssize_t r; - double red, green, blue, pixel[4]; - char width[3 * sizeof(size_t) + 1] = {0}; - char height[3 * sizeof(size_t) + 1] = {0}; - int headless = 0; - - ARGBEGIN { - case 'h': - headless = 1; - break; - default: - usage(); - } ARGEND; - if (argc) - usage(); - - if (pipe(pipe_rw)) - eprintf("pipe:"); - - if (pipe_rw[0] == STDIN_FILENO || pipe_rw[1] == STDIN_FILENO) - eprintf("no stdin open\n"); - if (pipe_rw[0] == STDOUT_FILENO || pipe_rw[1] == STDOUT_FILENO) - eprintf("no stdout open\n"); - for (i = 0; i < 2; i++) { - if (pipe_rw[i] == STDERR_FILENO) { - pipe_rw[i] = dup(old_fd = pipe_rw[i]); - if (pipe_rw[i] < 0) - eprintf("dup:"); - close(old_fd); - } - } - - pid = fork(); - if (pid < 0) - eprintf("fork:"); - - if (!pid) { - close(pipe_rw[0]); - if (dup2(pipe_rw[1], STDOUT_FILENO) < 0) - eprintf("dup2:"); - close(pipe_rw[1]); - /* XXX Is there a way to convert directly to raw XYZ? (Would avoid gamut truncation) */ - execlp("convert", "convert", "-", "-depth", "32", "-alpha", "activate", "pam:-", NULL); - eprintf("exec convert:"); - } - - close(pipe_rw[1]); - - for (ptr = 0;;) { - r = read(pipe_rw[0], buf + ptr, sizeof(buf) - ptr); - if (r < 0) - eprintf("read :"); - if (r == 0) - eprintf("convertion failed\n"); - ptr += (size_t)r; - - for (;;) { - p = memchr(buf, '\n', ptr); - if (!p) { - if (ptr == sizeof(buf)) - eprintf("convertion failed\n"); - break; - } - *p++ = '\0'; - if (strstr(buf, "WIDTH ") == buf) { - if (*width || !buf[6] || strlen(buf + 6) >= sizeof(width)) - eprintf("convertion failed\n"); - strcpy(width, buf + 6); - } else if (strstr(buf, "HEIGHT ") == buf) { - if (*height || !buf[7] || strlen(buf + 7) >= sizeof(height)) - eprintf("convertion failed\n"); - strcpy(height, buf + 7); - } else if (!strcmp(buf, "ENDHDR")) { - memmove(buf, p, ptr -= (size_t)(p - buf)); - goto header_done; - } - memmove(buf, p, ptr -= (size_t)(p - buf)); - } - } -header_done: - n = ptr; - - if (!*width || !*height) - eprintf("convertion failed\n"); - - if (!headless) { - printf("%s %s xyza\n%cuivf", width, height, 0); - fflush(stdout); - if (ferror(stdout)) - eprintf(":"); - } - - for (;;) { - for (ptr = 0; ptr + 15 < n; ptr += 16) { - red = get_value(buf + ptr + 0); - green = get_value(buf + ptr + 4); - blue = get_value(buf + ptr + 8); - pixel[3] = get_value(buf + ptr + 12); - - srgb_to_ciexyz(red, green, blue, pixel + 0, pixel + 1, pixel + 2); - - for (ptw = 0; ptw < sizeof(pixel); ptw += (size_t)r) { - r = write(STDOUT_FILENO, (char *)pixel + ptw, sizeof(pixel) - ptw); - if (r < 0) - eprintf("write :"); - } - } - r = read(pipe_rw[0], buf, sizeof(buf)); - if (r < 0) - eprintf("read :"); - if (r == 0) - break; - n = (size_t)r; - } - - while (waitpid(pid, &status, 0) != pid); - return !!status; -} diff --git a/src/vu-to-image.c b/src/vu-to-image.c new file mode 100644 index 0000000..3b93dd9 --- /dev/null +++ b/src/vu-to-image.c @@ -0,0 +1,149 @@ +/* See LICENSE file for copyright and license details. */ +#include "arg.h" +#include "stream.h" +#include "util.h" + +#include +#include +#include +#include +#include + +static int luma_warning_triggered = 0; +static int gamut_warning_triggered = 0; +static int alpha_warning_triggered = 0; + +static void +usage(void) +{ + eprintf("usage: %s [-d depth]\n", argv0); +} + +static void +write_pixel(double R, double G, double B, double A, int bytes, unsigned long long int max) +{ + unsigned long long int colours[4]; + unsigned char buf[4 * 8]; + int i, j, k, bm = bytes - 1; + size_t ptr, n; + ssize_t r; + + if (R < 0 || G < 0 || B < 0 || R > 1 || G > 1 || B > 1) { + if (gamut_warning_triggered) { + gamut_warning_triggered = 1; + 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; + } + + if (A < 0 || A > 1) { + if (alpha_warning_triggered) { + alpha_warning_triggered = 1; + weprintf("warning: alpha values truncated\n"); + } + A = A < 0 ? 0 : 1; + } + + colours[0] = srgb_encode(R) * max + 0.5; + colours[1] = srgb_encode(G) * max + 0.5; + colours[2] = srgb_encode(B) * max + 0.5; + colours[3] = A * max + 0.5; + + for (i = k = 0; i < 4; i++) { + for (j = 0; j < bytes; j++, k++) { + buf[k + bm - j] = (unsigned char)(colours[j]); + colours[j] >>= 8; + } + } + + n = (size_t)bytes * 4; + for (ptr = 0; ptr < n; ptr += (size_t)r) { + r = write(STDOUT_FILENO, buf + ptr, n - ptr); + if (r < 0) + eprintf("write :"); + } +} + +static void +process_xyza(struct stream *stream, size_t n, int bytes, unsigned long long int max) +{ + size_t i; + double X, Y, Z, A, R, G, B; + for (i = 0; i < n; i += stream->pixel_size) { + X = ((double *)(stream->buf + i))[0]; + Y = ((double *)(stream->buf + i))[1]; + Z = ((double *)(stream->buf + i))[2]; + A = ((double *)(stream->buf + i))[3]; + + if (Y < 0 || Y > 1) { + if (luma_warning_triggered) { + luma_warning_triggered = 1; + weprintf("warning: %s colour detected\n", + Y < 0 ? "subblack" : "superwhite"); + } + } + + srgb_to_ciexyz(X, Y, Z, &R, &G, &B); + write_pixel(R, G, B, A, bytes, max); + } +} + +int +main(int argc, char *argv[]) +{ + struct stream stream; + int depth = 16, bytes; + unsigned long long int max; + size_t n; + void (*process)(struct stream *stream, size_t n, int bytes, unsigned long long int max); + + ARGBEGIN { + case 'd': + if (toi(EARGF(usage()), 1, 64, &depth)) + eprintf("argument of -d must be an integer in [1, 64]\n"); + break; + default: + usage(); + } ARGEND; + + if (argc) + usage(); + + stream.fd = STDIN_FILENO; + stream.file = ""; + einit_stream(&stream); + + max = 1ULL << (depth - 1); + max |= max - 1; + for (bytes = 1; bytes * 8 < depth; bytes++); + + if (!strcmp(stream.pixfmt, "xyza")) + process = process_xyza; + else + eprintf("pixel format %s is not supported, try xyza\n", stream.pixfmt); + + printf("P7\n" + "WIDTH %zu\n" + "HEIGHT %zu\n" + "DEPTH 4\n" /* Depth actually means channels */ + "MAXVAL %llu\n" + "TUPLTYPE RGB_ALPHA\n" + "ENDHDR\n", stream.width, stream.height, max); + fflush(stdout); + if (ferror(stdout)) + eprintf(":"); + + for (;;) { + n = stream.ptr; + n -= n % stream.pixel_size; + process(&stream, n, bytes, max); + memmove(stream.buf, stream.buf + n, stream.ptr -= n); + if (!eread_stream(&stream, SIZE_MAX)) + break; + } + + return 0; +} -- cgit v1.2.3-70-g09d2