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