summaryrefslogtreecommitdiff
path: root/data/frames/gen-lut.py
blob: bd371a483c0593fa23c23bc35420d827373db0e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python3
# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: 2016 Renesas Electronics Corporation

import argparse
import math
import sys

clu_configs = {
    'zero': {
        'scale': 0.0,
        'a': 0.0,
        'freq': 1.0,
        'weights': (1.0, 1.0, 1.0)
    },
    'identity': {
        'scale': 1.0,
        'a': 0.0,
        'freq': 1.0,
        'weights': (1.0, 1.0, 1.0)
    },
    # Keep the three weights different to generate an anisothropic
    # look up table.
    'wave': {
        'scale': 1.0,
        'a': 0.1,
        'freq': 3.0,
        'weights': (1.0, 2.0, 3.0)
    },
}

lut_configs = {
    'zero':     (0.0, 1.0, 1.0, 1.0),
    'identity': (1.0, 1.0, 1.0, 1.0),
    'gamma':    (1.0, 0.5, 1.0, 2.0),
}

def clu_value(x, y, z, scale, a, freq, weights):
    x = x / 16.
    y = y / 16.
    z = z / 16.

    dist = math.sqrt((x*x*weights[0] + y*y*weights[1] + z*z*weights[2]) / 3. / sum(weights))
    offset = a * math.sin(dist * freq * 2 * math.pi)

    x = max(0, min(255, int((x*scale + offset) * 256)))
    y = max(0, min(255, int((y*scale + offset) * 256)))
    z = max(0, min(255, int((z*scale + offset) * 256)))

    return (z, y, x, 0)

def generate_clu(config):
    clu = bytearray()

    for z in range(17):
        for y in range(17):
            for x in range(17):
                clu.extend(clu_value(x, y, z, **config))

    return clu


def gamma(vin, gamma, scale):
    return int(255 * scale * math.pow(vin / 255., gamma))

def generate_lut(config):
    lut = bytearray()
    for i in range(256):
        lut.extend([gamma(i, g, config[0]) for g in config[1:]])
        lut.append(0)

    return lut


def main(argv):
    # Parse command line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('--output', '-o', metavar='file', type=str,
                        help='Output file name. Defaults to standard output if not specified.')
    parser.add_argument('--type', '-t', dest='table_type', type=str, required=True,
                        choices=['clu', 'lut'], help='Select the look up table type.')
    parser.add_argument('table', type=str,
                        help='Table contents')
    args = parser.parse_args(argv[1:])

    out = open(args.output, 'wb')

    if args.table_type == 'clu':
        table = generate_clu(clu_configs[args.table])
    else:
        table = generate_lut(lut_configs[args.table])

    out.write(table)

    return 0

if __name__ == '__main__':
    sys.exit(main(sys.argv))