Missing files.
[libdcp.git] / lut.py
1 import math
2
3 BIT_DEPTH = 12
4 DCI_GAMMA = 2.6
5 SRGB_GAMMA = 2.4;
6 BIT_LENGTH = int(math.pow(2, BIT_DEPTH))
7 COLOR_DEPTH = BIT_LENGTH - 1
8
9 def boilerplate(f):
10     print >>f, """/*
11     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
12
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 */
28
29
30 /* This file is auto-generated by the build scripts; edits will be lost
31    on ./waf configure.
32 */
33 """
34
35 def make_luts():
36     cc = open('src/lut.cc', 'w')
37
38     boilerplate(cc)
39
40     print >>cc,"#include \"lut.h\""
41
42     print >>cc, """
43 /* sRGB color matrix for XYZ -> RGB */
44 float color_matrix[3][3] = {
45   { 3.240454836, -1.537138850, -0.498531547},
46   {-0.969266390,  1.876010929,  0.041556082},
47   { 0.055643420, -0.204025854,  1.057225162}
48 };\n\n
49 """
50
51     print >>cc, """
52 float lut_in[COLOR_DEPTH + 1] = {\n
53 \t/* Bit depth:       %d
54 \t * Reference white: DCI
55 \t * Gamma:           %f
56 \t */
57 """ % (BIT_DEPTH, DCI_GAMMA)
58
59     c = 0
60     for i in range(0, BIT_LENGTH):
61         v = math.pow (i / (BIT_LENGTH - 1.0), DCI_GAMMA);
62
63         if (c == 0):
64             print >> cc,"    ",
65
66         if i < BIT_LENGTH - 1:
67             print >> cc,"%06f, " % v,
68             if c == 12:
69                 c = 0;
70                 print >> cc,""
71             else:
72                 c += 1
73         else:
74             print >> cc,"%06f" % v
75
76     print >>cc,"};"
77
78     print >>cc, """
79 int lut_out[COLOR_DEPTH + 1] = {
80 \t/* Bit depth:       %d
81 \t * Reference white: sRGB
82 \t * Gamma:           %f
83 \t */
84 """
85
86     c = 0
87     for i in range (0, BIT_LENGTH):
88         v = i / (BIT_LENGTH - 1.0)
89
90         if (v < (0.04045 / 12.92)):
91             v *= 12.92
92         else:
93             v = (1.055 * pow (v, (1 / SRGB_GAMMA))) - 0.055;
94
95         v *= 255
96
97         if c == 0:
98             print >> cc,"    ",
99
100         if i < BIT_LENGTH - 1:
101             print >> cc, "%d, " % v,
102             if c == 12:
103                 c = 0;
104                 print >> cc,""
105             else:
106                 c += 1
107         else:
108             print >> cc,"%d" % v
109
110     print >>cc,"};"
111             
112     h = open('src/lut.h', 'w')
113
114     boilerplate(h)
115
116     print >> h,"""
117 #define COLOR_DEPTH     (%d)
118 #define DCI_COEFFICIENT (48.0/52.37)
119
120 extern float color_matrix[3][3];
121 extern int lut_out[COLOR_DEPTH + 1];
122 extern float lut_in[COLOR_DEPTH + 1];
123 """ % COLOR_DEPTH
124
125 if __name__ == "__main__":
126     make_luts()