summaryrefslogtreecommitdiff
path: root/lut.py
blob: 954f094f86babd848d7c19383b21b2e76457da8d (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import math

BIT_DEPTH = 12
DCI_GAMMA = 2.6
SRGB_GAMMA = 2.4;
BIT_LENGTH = int(math.pow(2, BIT_DEPTH))
COLOR_DEPTH = BIT_LENGTH - 1

def boilerplate(f):
    print >>f, """/*
    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/


/* This file is auto-generated by the build scripts; edits will be lost
   on ./waf configure.
*/
"""

def make_luts():
    cc = open('src/lut.cc', 'w')

    boilerplate(cc)

    print >>cc,"#include \"lut.h\""

    print >>cc, """
/* sRGB color matrix for XYZ -> RGB */
float color_matrix[3][3] = {
  { 3.240454836, -1.537138850, -0.498531547},
  {-0.969266390,  1.876010929,  0.041556082},
  { 0.055643420, -0.204025854,  1.057225162}
};\n\n
"""

    print >>cc, """
float lut_in[COLOR_DEPTH + 1] = {\n
\t/* Bit depth:       %d
\t * Reference white: DCI
\t * Gamma:           %f
\t */
""" % (BIT_DEPTH, DCI_GAMMA)

    c = 0
    for i in range(0, BIT_LENGTH):
	v = math.pow (i / (BIT_LENGTH - 1.0), DCI_GAMMA);

        if (c == 0):
            print >> cc,"    ",

        if i < BIT_LENGTH - 1:
            print >> cc,"%06f, " % v,
            if c == 12:
                c = 0;
                print >> cc,""
            else:
                c += 1
        else:
            print >> cc,"%06f" % v

    print >>cc,"};"

    print >>cc, """
int lut_out[COLOR_DEPTH + 1] = {
\t/* Bit depth:       %d
\t * Reference white: sRGB
\t * Gamma:           %f
\t */
"""

    c = 0
    for i in range (0, BIT_LENGTH):
        v = i / (BIT_LENGTH - 1.0)

	if (v < (0.04045 / 12.92)):
	    v *= 12.92
	else:
	    v = (1.055 * pow (v, (1 / SRGB_GAMMA))) - 0.055;

	v *= 255

        if c == 0:
            print >> cc,"    ",

        if i < BIT_LENGTH - 1:
            print >> cc, "%d, " % v,
            if c == 12:
                c = 0;
                print >> cc,""
            else:
                c += 1
        else:
            print >> cc,"%d" % v

    print >>cc,"};"
            
    h = open('src/lut.h', 'w')

    boilerplate(h)

    print >> h,"""
#define COLOR_DEPTH     (%d)
#define DCI_COEFFICIENT (48.0/52.37)

extern float color_matrix[3][3];
extern int lut_out[COLOR_DEPTH + 1];
extern float lut_in[COLOR_DEPTH + 1];
""" % COLOR_DEPTH

if __name__ == "__main__":
    make_luts()