649c8d47c53f900cbc8440930c4a13ee7a370efb
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / createARRIColorSpaces.py
1 import math
2 import array
3
4 import aces_ocio.generateLUT as genlut
5 from aces_ocio.util import ColorSpace, mat44FromMat33
6
7
8
9 #
10 # LogC to ACES
11 #
12 def createLogC(gamut, transferFunction, exposureIndex, name, lutDir, lutResolution1d):
13     name = "%s (EI%s) - %s" % (transferFunction, exposureIndex, gamut)
14     if transferFunction == "":
15         name = "Linear - %s" % gamut
16     if gamut == "":
17         name = "%s (EI%s)" % (transferFunction, exposureIndex)
18
19     cs = ColorSpace(name)
20     cs.description = name
21     cs.equalityGroup = ''
22     cs.family = 'ARRI'
23     cs.isData=False
24
25     # Globals
26     IDT_maker_version = "0.08"
27
28     nominalEI = 400.0
29     blackSignal = 0.003907
30     midGraySignal = 0.01
31     encodingGain = 0.256598
32     encodingOffset = 0.391007
33
34     def gainForEI(EI) :
35         return (math.log(EI/nominalEI)/math.log(2) * (0.89 - 1) / 3 + 1) * encodingGain
36
37     def LogCInverseParametersForEI(EI) :
38         cut = 1.0 / 9.0
39         slope = 1.0 / (cut * math.log(10))
40         offset = math.log10(cut) - slope * cut
41         gain = EI / nominalEI
42         gray = midGraySignal / gain
43         # The higher the EI, the lower the gamma
44         encGain = gainForEI(EI)
45         encOffset = encodingOffset
46         for i in range(0,3) :
47             nz = ((95.0 / 1023.0 - encOffset) / encGain - offset) / slope
48             encOffset = encodingOffset - math.log10(1 + nz) * encGain
49         # Calculate some intermediate values
50         a = 1.0 / gray
51         b = nz - blackSignal / gray
52         e = slope * a * encGain
53         f = encGain * (slope * b + offset) + encOffset
54         # Manipulations so we can return relative exposure
55         s = 4 / (0.18 * EI)
56         t = blackSignal
57         b = b + a * t
58         a = a * s
59         f = f + e * t
60         e = e * s
61         return { 'a' : a,
62                  'b' : b,
63                  'cut' : (cut - b) / a,
64                  'c' : encGain,
65                  'd' : encOffset,
66                  'e' : e,
67                  'f' : f }
68
69     def logCtoLinear(codeValue, exposureIndex):
70         p = LogCInverseParametersForEI(exposureIndex)
71         breakpoint = p['e'] * p['cut'] + p['f']
72         if (codeValue > breakpoint):
73             linear = (pow(10,(codeValue/1023.0 - p['d']) / p['c']) - p['b']) / p['a']
74         else:
75             linear = (codeValue/1023.0 - p['f']) / p['e']
76
77         #print( codeValue, linear )
78         return linear
79
80
81     cs.toReferenceTransforms = []
82
83     if transferFunction == "V3 LogC":
84         data = array.array('f', "\0" * lutResolution1d * 4)
85         for c in range(lutResolution1d):
86             data[c] = logCtoLinear(1023.0*c/(lutResolution1d-1), int(exposureIndex))
87
88         lut = "%s_to_linear.spi1d" % ("%s_%s" % (transferFunction, exposureIndex))
89
90         # Remove spaces and parentheses
91         lut = lut.replace(' ', '_').replace(')', '_').replace('(', '_')
92
93         genlut.writeSPI1D(lutDir + "/" + lut, 0.0, 1.0, data, lutResolution1d, 1)
94
95         #print( "Writing %s" % lut)
96         cs.toReferenceTransforms.append( {
97             'type':'lutFile', 
98             'path':lut, 
99             'interpolation':'linear', 
100             'direction':'forward'
101         } )
102
103     if gamut == 'Wide Gamut':
104         cs.toReferenceTransforms.append( {
105             'type':'matrix',
106             'matrix':mat44FromMat33([0.680206, 0.236137, 0.083658, 
107                         0.085415, 1.017471, -0.102886, 
108                         0.002057, -0.062563, 1.060506]),
109             'direction':'forward'
110         })
111
112     cs.fromReferenceTransforms = []
113     return cs
114
115 def createColorSpaces(lutDir, lutResolution1d):
116     colorspaces = []
117
118     transferFunction = "V3 LogC"
119     gamut = "Wide Gamut"
120     #EIs = [160.0, 200.0, 250.0, 320.0, 400.0, 500.0, 640.0, 800.0, 1000.0, 1280.0, 1600.0, 2000.0, 2560.0, 3200.0]
121     EIs = [160, 200, 250, 320, 400, 500, 640, 800, 1000, 1280, 1600, 2000, 2560, 3200]
122     defaultEI = 800
123
124     # Full conversion
125     for EI in EIs:
126         LogCEIfull = createLogC(gamut, transferFunction, EI, "LogC", lutDir, lutResolution1d)
127         colorspaces.append(LogCEIfull)
128
129     # Linearization only
130     for EI in [800]:
131         LogCEIlinearization = createLogC("", transferFunction, EI, "LogC", lutDir, lutResolution1d)
132         colorspaces.append(LogCEIlinearization)
133
134     # Primaries
135     LogCEIprimaries = createLogC(gamut, "", defaultEI, "LogC", lutDir, lutResolution1d)
136     colorspaces.append(LogCEIprimaries)
137
138     return colorspaces