openmv/util/gen_rgb2yuv.py
Kwabena W. Agyeman 88f37014f1 Finished updating tables.
With new RGB565<->RGB888 scaling. This included redoing the LAB/YUV/XYZ
tables. I translated the table gen code to python also and added
comments as to where the math came from.

And yes, I tested and compared the tables to make sure they weren't
borken. The tables are slightly different... but, if look at the
progression of values loosely you'll see the triplets are very close to
each other when doing a compare. This is to be expected given I used a
slightly better scaling algo.
2016-02-16 19:37:19 -05:00

26 lines
825 B
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
sys.stdout.write("#include <stdint.h>\n")
sys.stdout.write("const int8_t yuv_table[196608] = {\n") # 65536 * 3
for i in range(65536):
r = ((((i >> 3) & 31) * 255) + 15.5) // 31
g = (((((i & 7) << 3) | (i >> 13)) * 255) + 31.5) // 63
b = ((((i >> 8) & 31) * 255) + 15.5) // 31
# https://en.wikipedia.org/wiki/YCbCr (ITU-R BT.601 conversion)
y = int((r * +0.299000) + (g * +0.587000) + (b * +0.114000)) - 128;
u = int((r * -0.168736) + (g * -0.331264) + (b * +0.500000));
v = int((r * +0.500000) + (g * -0.418688) + (b * -0.081312));
sys.stdout.write(" %4d, %4d, %4d" % (y, u, v))
if (i + 1) % 4:
sys.stdout.write(", ")
elif i != 65535:
sys.stdout.write(",\n")
else:
sys.stdout.write("\n};\n")