From 4154609e5de76bc8858d3f86e0af09ba2700c488 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 23 Sep 2017 21:57:45 +0200 Subject: Fix colour distortion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This matrices are not convert to or from the proper YUV colour space both rather the ones used in videos. srgb_to_yuv was created by converting the image [sRGB(100%, 0, 0); sRGB(0, 100%, 0); sRGB(0, 0, 100%)] to YUV with FFmpeg and transposing the matrix. yuv_to_srgb is the inversion of that matrix, the values in the first column should all be 1 but are not due to rounding/truncation error. Signed-off-by: Mattias Andrée --- src/blind-convert.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/blind-convert.c') diff --git a/src/blind-convert.c b/src/blind-convert.c index fd4240d..a61baba 100644 --- a/src/blind-convert.c +++ b/src/blind-convert.c @@ -47,7 +47,7 @@ static size_t remove_alpha_u16(uint16_t *buf, size_t n) { size_t i, j; - long int a, max = (long int)UINT16_MAX, ymax = 0xDAF4L; + long int a, max = (long int)0xFF00L, ymax = 0xDAF4L; for (i = j = 0; i < n; i += 4, j += 3) { a = (long int)(buf[i + 3]); buf[j + 0] = (uint16_t)(((long int)(buf[i + 0]) - 0x1001L) * a / ymax + 0x1001L); @@ -108,19 +108,23 @@ raw1_to_raw0(uint16_t *buf, size_t n) #define RAW2_TO_RAW3(TYPE, WITH_ALPHA)\ do {\ size_t i;\ - TYPE max = (TYPE)UINT16_MAX, ymax = (TYPE)0xDAF4;\ + TYPE max = (TYPE)0xFF00L, ymax = (TYPE)0xDAF4L;\ if (sizeof(*in) > sizeof(*out)) {\ for (i = 0; i < n; i += 3 + WITH_ALPHA) {\ out[i + 0] = (TYPE)((long int)(in[i + 0]) - 0x1001L) / ymax;\ out[i + 1] = (TYPE)((long int)(in[i + 1]) - 0x8000L) / max;\ out[i + 2] = (TYPE)((long int)(in[i + 2]) - 0x8000L) / max;\ - if (WITH_ALPHA)\ + if (WITH_ALPHA) {\ out[i + 3] = (TYPE)(in[i + 3]) / max;\ + out[i + 3] = CLIP(0, out[i + 3], 1);\ + }\ }\ } else {\ for (i = n; i; i -= 3 + WITH_ALPHA) {\ - if (WITH_ALPHA)\ + if (WITH_ALPHA) {\ out[i - 1] = (TYPE)(in[i - 1]) / max;\ + out[i - 1] = CLIP(0, out[i - 1], 1);\ + }\ out[i - 1 - WITH_ALPHA] = (TYPE)((long int)(in[i - 1 - WITH_ALPHA]) - 0x8000L) / max;\ out[i - 2 - WITH_ALPHA] = (TYPE)((long int)(in[i - 2 - WITH_ALPHA]) - 0x8000L) / max;\ out[i - 3 - WITH_ALPHA] = (TYPE)((long int)(in[i - 3 - WITH_ALPHA]) - 0x1001L) / ymax;\ @@ -136,7 +140,7 @@ static void raw2a_to_raw3a_f (uint16_t *in, float *out, size_t n) { RAW2_TO_RAW #define RAW3_TO_RAW2(TYPE, WITH_ALPHA)\ do {\ size_t i;\ - TYPE max = (TYPE)UINT16_MAX, ymax = (TYPE)0xDAF4;\ + TYPE max = (TYPE)0xFF00L, ymax = (TYPE)0xDAF4L;\ long int y, u, v;\ if (sizeof(*in) > sizeof(*out)) {\ for (i = 0; i < n; i += 3 + WITH_ALPHA) {\ -- cgit v1.2.3-70-g09d2