ACES 0.7.1
[OpenColorIO-Configs.git] / aces / luts / unbuild / adx_cid_to_rle.py
1 #!/usr/bin/env python
2
3 import math, numpy
4
5 """
6
7 const float REF_PT = (7120.0 - 1520.0) / 8000.0 * (100.0 / 55.0) - log10(0.18);
8
9 const float LUT_1D[11][2] = {
10         {-0.190000000000000, -6.000000000000000},
11         { 0.010000000000000, -2.721718645000000},
12         { 0.028000000000000, -2.521718645000000},
13         { 0.054000000000000, -2.321718645000000},
14         { 0.095000000000000, -2.121718645000000},
15         { 0.145000000000000, -1.921718645000000},
16         { 0.220000000000000, -1.721718645000000},
17         { 0.300000000000000, -1.521718645000000},
18         { 0.400000000000000, -1.321718645000000},
19         { 0.500000000000000, -1.121718645000000},
20         { 0.600000000000000, -0.926545676714876}
21 };
22
23         // Convert Channel Independent Density values to Relative Log Exposure values
24         float logE[3];
25         if ( cid[0] <= 0.6) logE[0] = interpolate1D( LUT_1D, cid[0]);
26         if ( cid[1] <= 0.6) logE[1] = interpolate1D( LUT_1D, cid[1]);
27         if ( cid[2] <= 0.6) logE[2] = interpolate1D( LUT_1D, cid[2]);
28
29         if ( cid[0] > 0.6) logE[0] = ( 100.0 / 55.0) * cid[0] - REF_PT;
30         if ( cid[1] > 0.6) logE[1] = ( 100.0 / 55.0) * cid[1] - REF_PT;
31         if ( cid[2] > 0.6) logE[2] = ( 100.0 / 55.0) * cid[2] - REF_PT;
32 """
33
34
35 def interpolate1D(x, xp, fp):
36     return numpy.interp(x, xp, fp)
37
38 LUT_1D_xp = [-0.190000000000000, 
39               0.010000000000000,
40               0.028000000000000,
41               0.054000000000000,
42               0.095000000000000,
43               0.145000000000000,
44               0.220000000000000,
45               0.300000000000000,
46               0.400000000000000,
47               0.500000000000000,
48               0.600000000000000]
49
50 LUT_1D_fp = [-6.000000000000000, 
51              -2.721718645000000,
52              -2.521718645000000,
53              -2.321718645000000,
54              -2.121718645000000,
55              -1.921718645000000,
56              -1.721718645000000,
57              -1.521718645000000,
58              -1.321718645000000,
59              -1.121718645000000,
60              -0.926545676714876]
61
62 REF_PT = (7120.0 - 1520.0) / 8000.0 * (100.0 / 55.0) - math.log(0.18, 10.0)
63
64 def cid_to_rle(x):
65     if x <= 0.6:
66         return interpolate1D(x, LUT_1D_xp, LUT_1D_fp)
67     return (100.0 / 55.0) * x - REF_PT
68
69 def WriteSPI1D(filename, fromMin, fromMax, data):
70     f = file(filename,'w')
71     f.write("Version 1\n")
72     f.write("From %s %s\n" % (fromMin, fromMax))
73     f.write("Length %d\n" % len(data))
74     f.write("Components 1\n")
75     f.write("{\n")
76     for value in data:
77         f.write("        %s\n" % value)
78     f.write("}\n")
79     f.close()
80
81 def Fit(value, fromMin, fromMax, toMin, toMax):
82     if fromMin == fromMax:
83         raise ValueError("fromMin == fromMax")
84     return (value - fromMin) / (fromMax - fromMin) * (toMax - toMin) + toMin
85
86 NUM_SAMPLES = 2**12
87 RANGE = (-0.19, 3.0)
88 data = []
89 for i in xrange(NUM_SAMPLES):
90     x = i/(NUM_SAMPLES-1.0)
91     x = Fit(x, 0.0, 1.0, RANGE[0], RANGE[1])
92     data.append(cid_to_rle(x))
93
94 WriteSPI1D('adx_cid_to_rle.spi1d', RANGE[0], RANGE[1], data)
95