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