blind

suckless command-line video editing utility
git clone git://git.suckless.org/blind
Log | Files | Refs | README | LICENSE

commit 2b646382a5745b65d32e6bc66b9cf41149673c8c
parent 233e70bd856bb1b90844b51b0e7b3534e804c4ef
Author: Mattias Andrée <maandree@kth.se>
Date:   Sat, 23 Sep 2017 19:14:42 +0200

Fix conversion of Y value

Signed-off-by: Mattias Andrée <maandree@kth.se>

Diffstat:
TODO | 3+--
src/blind-convert.c | 36++++++++++++++++++------------------
src/blind-from-video.c | 14+++++++-------
src/blind-to-video.c | 12++++++------
src/util/colour.h | 12++++++------
5 files changed, 38 insertions(+), 39 deletions(-)

diff --git a/TODO b/TODO @@ -1,5 +1,4 @@ -Why does blind-from-video produce darker images than blind-from-image and -why does blind-to-video produce brighter images than blind-to-image? +Conversion either from or to video changes the brightness of the primitives (but not greys) blind-transform affine transformation by matrix multiplication, -[xy] for tiling, -s for improve quality on downscaling (pixels' neighbours must not change) diff --git a/src/blind-convert.c b/src/blind-convert.c @@ -47,12 +47,12 @@ static size_t remove_alpha_u16(uint16_t *buf, size_t n) { size_t i, j; - long int a, max = (long int)UINT16_MAX; + long int a, max = (long int)UINT16_MAX, 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]) - 16L * 256L) * a / max + 16L * 256L); - buf[j + 1] = (uint16_t)(((long int)(buf[i + 1]) - 128L * 256L) * a / max + 128L * 256L); - buf[j + 2] = (uint16_t)(((long int)(buf[i + 2]) - 128L * 256L) * a / max + 128L * 256L); + buf[j + 0] = (uint16_t)(((long int)(buf[i + 0]) - 0x1001L) * a / ymax + 0x1001L); + buf[j + 1] = (uint16_t)(((long int)(buf[i + 1]) - 0x8000L) * a / max + 0x8000L); + buf[j + 2] = (uint16_t)(((long int)(buf[i + 2]) - 0x8000L) * a / max + 0x8000L); } return j; } @@ -108,12 +108,12 @@ 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;\ + TYPE max = (TYPE)UINT16_MAX, ymax = (TYPE)0xDAF4;\ if (sizeof(*in) > sizeof(*out)) {\ for (i = 0; i < n; i += 3 + WITH_ALPHA) {\ - out[i + 0] = (TYPE)((long int)(in[i + 0]) - 16L * 256L) / max;\ - out[i + 1] = (TYPE)((long int)(in[i + 1]) - 128L * 256L) / max;\ - out[i + 2] = (TYPE)((long int)(in[i + 2]) - 128L * 256L) / max;\ + 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)\ out[i + 3] = (TYPE)(in[i + 3]) / max;\ }\ @@ -121,9 +121,9 @@ raw1_to_raw0(uint16_t *buf, size_t n) for (i = n; i; i -= 3 + WITH_ALPHA) {\ if (WITH_ALPHA)\ out[i - 1] = (TYPE)(in[i - 1]) / max;\ - out[i - 1 - WITH_ALPHA] = (TYPE)((long int)(in[i - 1 - WITH_ALPHA]) - 128L * 256L) / max;\ - out[i - 2 - WITH_ALPHA] = (TYPE)((long int)(in[i - 2 - WITH_ALPHA]) - 128L * 256L) / max;\ - out[i - 3 - WITH_ALPHA] = (TYPE)((long int)(in[i - 3 - WITH_ALPHA]) - 16L * 256L) / max;\ + 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;\ }\ }\ } while (0) @@ -136,13 +136,13 @@ 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;\ + TYPE max = (TYPE)UINT16_MAX, ymax = (TYPE)0xDAF4;\ long int y, u, v;\ if (sizeof(*in) > sizeof(*out)) {\ for (i = 0; i < n; i += 3 + WITH_ALPHA) {\ - y = (long int)(in[i + 0] * max) + 16L * 256L;\ - u = (long int)(in[i + 1] * max) + 128L * 256L;\ - v = (long int)(in[i + 2] * max) + 128L * 256L;\ + y = (long int)(in[i + 0] * ymax) + 0x1001L;\ + u = (long int)(in[i + 1] * max) + 0x8000L;\ + v = (long int)(in[i + 2] * max) + 0x8000L;\ out[i + 0] = (uint16_t)CLIP(0, y, 0xFFFFL);\ out[i + 1] = (uint16_t)CLIP(0, u, 0xFFFFL);\ out[i + 2] = (uint16_t)CLIP(0, v, 0xFFFFL);\ @@ -157,9 +157,9 @@ static void raw2a_to_raw3a_f (uint16_t *in, float *out, size_t n) { RAW2_TO_RAW v = (long int)(in[i - 1] * max);\ out[i - 1] = (uint16_t)CLIP(0, v, 0xFFFFL); \ }\ - v = (long int)(in[i - 1 - WITH_ALPHA] * max) + 128L * 256L;\ - u = (long int)(in[i - 2 - WITH_ALPHA] * max) + 128L * 256L;\ - y = (long int)(in[i - 3 - WITH_ALPHA] * max) + 16L * 256L;\ + v = (long int)(in[i - 1 - WITH_ALPHA] * max) + 0x8000L;\ + u = (long int)(in[i - 2 - WITH_ALPHA] * max) + 0x8000L;\ + y = (long int)(in[i - 3 - WITH_ALPHA] * ymax) + 0x1001L;\ out[i - 1 - WITH_ALPHA] = (uint16_t)CLIP(0, v, 0xFFFFL);\ out[i - 2 - WITH_ALPHA] = (uint16_t)CLIP(0, u, 0xFFFFL);\ out[i - 3 - WITH_ALPHA] = (uint16_t)CLIP(0, y, 0xFFFFL);\ diff --git a/src/blind-from-video.c b/src/blind-from-video.c @@ -75,7 +75,7 @@ get_metadata(char *file, size_t *width, size_t *height) do {\ typedef TYPE pixel_t[4];\ size_t i, ptr;\ - TYPE y, u, v, max = (TYPE)UINT16_MAX;\ + TYPE y, u, v, max = (TYPE)UINT16_MAX, ymax = (TYPE)0xDAF4;\ TYPE r, g, b;\ pixel_t pixels[1024];\ uint16_t *pix;\ @@ -83,9 +83,9 @@ get_metadata(char *file, size_t *width, size_t *height) for (ptr = i = 0; ptr < n; ptr += 8) {\ pix = (uint16_t *)(buf + ptr);\ pixels[i][3] = 1;\ - y = (TYPE)((long int)(le16toh(pix[1])) - 16L * 256L);\ - u = (TYPE)((long int)(le16toh(pix[2])) - 128L * 256L);\ - v = (TYPE)((long int)(le16toh(pix[3])) - 128L * 256L);\ + y = (TYPE)((long int)(le16toh(pix[1])) - 0x1001L);\ + u = (TYPE)((long int)(le16toh(pix[2])) - 0x8000L);\ + v = (TYPE)((long int)(le16toh(pix[3])) - 0x8000L);\ scaled_yuv_to_ciexyz(y, u, v, pixels[i] + 0,\ pixels[i] + 1, pixels[i] + 2);\ if (++i == 1024) {\ @@ -97,9 +97,9 @@ get_metadata(char *file, size_t *width, size_t *height) for (ptr = i = 0; ptr < n; ptr += 8) {\ pix = (uint16_t *)(buf + ptr);\ pixels[i][3] = le16toh(pix[0]) / max;\ - y = (TYPE)((long int)le16toh(pix[1]) - 16L * 256L) / max;\ - u = (TYPE)((long int)le16toh(pix[2]) - 128L * 256L) / max;\ - v = (TYPE)((long int)le16toh(pix[3]) - 128L * 256L) / max;\ + y = (TYPE)((long int)le16toh(pix[1]) - 0x1001L) / ymax;\ + u = (TYPE)((long int)le16toh(pix[2]) - 0x8000L) / max;\ + v = (TYPE)((long int)le16toh(pix[3]) - 0x8000L) / max;\ yuv_to_srgb(y, u, v, &r, &g, &b);\ r = srgb_decode(r);\ g = srgb_decode(g);\ diff --git a/src/blind-to-video.c b/src/blind-to-video.c @@ -99,9 +99,9 @@ PROCESS(struct stream *stream, size_t n) for (ptr = 0; ptr < n; ptr += 4 * sizeof(TYPE)) { pixel = (TYPE *)(buf + ptr); ciexyz_to_scaled_yuv(pixel[0], pixel[1], pixel[2], &r, &g, &b); - y = (long int)r + 16L * 256L; - u = (long int)g + 128L * 256L; - v = (long int)b + 128L * 256L; + y = (long int)r + 0x1001L; + u = (long int)g + 0x8000L; + v = (long int)b + 0x8000L; *pixels++ = 0xFFFFU; *pixels++ = htole((uint16_t)CLIP(0, y, 0xFFFFL)); *pixels++ = htole((uint16_t)CLIP(0, u, 0xFFFFL)); @@ -118,9 +118,9 @@ PROCESS(struct stream *stream, size_t n) g = srgb_encode(g); b = srgb_encode(b); srgb_to_yuv(r, g, b, pixel + 0, pixel + 1, pixel + 2); - y = (long int)(pixel[0] * 0xFFFFL) + 16L * 256L; - u = (long int)(pixel[1] * 0xFFFFL) + 128L * 256L; - v = (long int)(pixel[2] * 0xFFFFL) + 128L * 256L; + y = (long int)(pixel[0] * 0xDAF4L) + 0x1001L; + u = (long int)(pixel[1] * 0xFFFFL) + 0x8000L; + v = (long int)(pixel[2] * 0xFFFFL) + 0x8000L; *pixels++ = htole((uint16_t)CLIP(0, a, 0xFFFFL)); *pixels++ = htole((uint16_t)CLIP(0, y, 0xFFFFL)); *pixels++ = htole((uint16_t)CLIP(0, u, 0xFFFFL)); diff --git a/src/util/colour.h b/src/util/colour.h @@ -99,20 +99,20 @@ MATRIX_MULTIPLY_FUNCTIONS(srgb_to_ciexyz, 0.950302838552371742508739771438) MATRIX_MULTIPLY_FUNCTIONS(scaled_yuv_to_ciexyz, - 0.00001450325106667098632156481796684488472237717360, + 0.00001450325106667098632156481796684488472237717360 / 0.8552986953536278, 0.00000345586790639342739093228633329157872822179343, 0.00000400923398630552893485111398685916128670214675, - 0.00001525902189669641837040624243737596543724066578, + 0.00001525902189669641837040624243737596543724066578 / 0.8552986953536278, -0.00000207722814409390653614547427030512238843584782, -0.00000263898607692305410302407824019166326934282552, - 0.00001661446153041708825425643025752719950105529279, + 0.00001661446153041708825425643025752719950105529279 / 0.8552986953536278, 0.00002885925752619118069149627137104374696718878113, -0.00000071781086875769179526501342566979779746816348) MATRIX_MULTIPLY_FUNCTIONS(ciexyz_to_scaled_yuv, - 26625.38231027395886485464870929718017578125, - 40524.0090949436053051613271236419677734375, - -271.5313105642117079696618020534515380859375, + 26625.38231027395886485464870929718017578125 * 0.8552986953536278, + 40524.0090949436053051613271236419677734375 * 0.8552986953536278, + -271.5313105642117079696618020534515380859375 * 0.8552986953536278, -11278.3751445417292416095733642578125, -26409.91773157499847002327442169189453125, 34100.5706543184860493056476116180419921875,