Last active
July 30, 2019 04:04
-
-
Save sh1boot/19bfdcdcb80ab4c60cf3de617e253607 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
void split(uint8_t* r, uint8_t* g, uint8_t* b, uint16_t rgb16) { | |
*r = (((rgb16 >> 11) & 0x1f) * 255 + 15) / 31; | |
*g = (((rgb16 >> 5) & 0x3f) * 255 + 31) / 63; | |
*b = (((rgb16 >> 0) & 0x1f) * 255 + 15) / 31; | |
} | |
uint8_t Y(uint8_t r, uint8_t g, uint8_t b) { return ((r * 66 + g * 129 + b * 25 + 128) >> 8) + 16; } | |
uint8_t U(uint8_t r, uint8_t g, uint8_t b) { return ((r * -38 + g * -74 + b * 112 + 128) >> 8) + 128; } | |
uint8_t V(uint8_t r, uint8_t g, uint8_t b) { return ((r * 112 + g * -94 + b * -18 + 128) >> 8) + 128; } | |
uint32_t ref(uint16_t left, uint16_t right) { | |
uint8_t lr, lg, lb, rr, rg, rb; | |
split(&lr, &lg, &lb, left); | |
split(&rr, &rg, &rb, right); | |
uint8_t b0 = Y(lr, lg, lb); | |
uint8_t b1 = (U(lr, lg, lb) + U(rr, rg, rb)) >> 1; | |
uint8_t b2 = Y(rr, rg, rb); | |
uint8_t b3 = (V(lr, lg, lb) + U(rr, rg, rb)) >> 1; | |
return (uint32_t)b3 << 24 | (uint32_t)b2 << 16 | (uint32_t)b1 << 8 | b0; | |
} | |
uint32_t t0[256], t1[256], t2[256], t3[256]; | |
void dut_setup() { | |
uint32_t base = ref(0,0); | |
for (int i = 0; i < 256; ++i) { | |
t0[i] = ref(0, i << 0) - base; | |
t1[i] = ref(0, i << 8) - base; | |
t2[i] = ref(i << 0, 0) - base; | |
t3[i] = ref(i << 8, 0) - base; | |
t0[i] += base; | |
} | |
} | |
uint32_t dut(uint16_t left, uint16_t right) { | |
return t0[right & 255] + t1[right >> 8] + t2[left & 255] + t3[left >> 8]; | |
} | |
int main(void) { | |
dut_setup(); | |
for (int i = 0; i < 65536; ++i) { | |
for (int j = 0; j < 65536; ++j) { | |
uint32_t r = ref(i, j); | |
uint32_t d = dut(i, j); | |
uint32_t map = 0; | |
int e = 0; | |
for (int k = 0; k < 32; k += 8) { | |
int diff = abs((int)((r >> k) & 255) - (int)((d >> k) & 255)); | |
if (e < diff) e = diff; | |
map |= diff << k; | |
} | |
if (e > 3) printf("%04x,%04x: %08x != %08x %08x %08x\n", i, j, d, r, d ^ r, map); | |
} | |
if ((i & 255) == 0) printf("%d\n", i >> 8); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
uint8_t Y(uint8_t r, uint8_t g, uint8_t b) { return ((r * 66 + g * 129 + b * 25 + 128) >> 8) + 16; } | |
uint8_t U(uint8_t r, uint8_t g, uint8_t b) { return ((r * -38 + g * -74 + b * 112 + 128) >> 8) + 128; } | |
uint8_t V(uint8_t r, uint8_t g, uint8_t b) { return ((r * 112 + g * -94 + b * -18 + 128) >> 8) + 128; } | |
uint32_t ref(uint8_t const* p) { | |
uint8_t lr = p[0], lg = p[1], lb = p[2], rr = p[3], rg = p[4], rb = p[5]; | |
uint8_t b0 = Y(lr, lg, lb); | |
uint8_t b1 = (U(lr, lg, lb) + U(rr, rg, rb)) >> 1; | |
uint8_t b2 = Y(rr, rg, rb); | |
uint8_t b3 = (V(lr, lg, lb) + U(rr, rg, rb)) >> 1; | |
return (uint32_t)b3 << 24 | (uint32_t)b2 << 16 | (uint32_t)b1 << 8 | b0; | |
} | |
uint32_t t0[256], t1[256], t2[256], t3[256], t4[256], t5[256]; | |
void dut_setup() { | |
uint8_t rgbrgb[11] = { 0 }; | |
uint32_t base = ref(rgbrgb); | |
for (int i = 0; i < 256; ++i) { | |
rgbrgb[5] = i; | |
t0[i] = ref(rgbrgb + 5) - base; | |
t1[i] = ref(rgbrgb + 4) - base; | |
t2[i] = ref(rgbrgb + 3) - base; | |
t3[i] = ref(rgbrgb + 2) - base; | |
t4[i] = ref(rgbrgb + 1) - base; | |
t5[i] = ref(rgbrgb + 0) - base; | |
t0[i] += base; | |
} | |
} | |
uint32_t dut(uint8_t const* p) { | |
return t0[p[0]] + t1[p[1]] + t2[p[2]] + t3[p[3]] + t4[p[4]] + t5[p[5]]; | |
} | |
uint64_t hash24(uint64_t x) { | |
x = ((x ^ (x >> 12)) * 0x46e963) & 0xffffff; | |
x = ((x ^ (x >> 10)) * 0x6da653) & 0xffffff; | |
return x ^ (x >> 12); | |
} | |
int main(void) { | |
dut_setup(); | |
for (int i = 0; i < 65536; ++i) { | |
for (int j = 0; j < 65536; ++j) { | |
int ii = hash24(i), jj = hash24(j); | |
uint8_t test[6] = { ii & 255, (ii >> 8) & 255, (ii >> 16) & 255, | |
jj & 255, (jj >> 8) & 255, (jj >> 16) & 255 }; | |
uint32_t r = ref(test); | |
uint32_t d = dut(test); | |
uint32_t map = 0; | |
int e = 0; | |
for (int k = 0; k < 32; k += 8) { | |
int diff = abs((int)((r >> k) & 255) - (int)((d >> k) & 255)); | |
if (e < diff) e = diff; | |
map |= diff << k; | |
} | |
if (e > 4) printf("%06x,%06x: %08x != %08x %08x %08x\n", ii, jj, d, r, d ^ r, map); | |
} | |
if ((i & 255) == 0) printf("%d\n", i >> 8); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment