c1e1bb34ac37844051ccfb49e2e8915611f5e584
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / createSonyColorSpaces.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import array
5
6 import aces_ocio.generateLUT as genlut
7 from aces_ocio.util import ColorSpace, mat44FromMat33
8
9 #
10 # SLog to ACES
11 #
12 def createSlog(gamut, transferFunction, name, lutDir, lutResolution1d):
13     name = "%s - %s" % (transferFunction, gamut)
14     if transferFunction == "":
15         name = "Linear - %s" % gamut
16     if gamut == "":
17         name = "%s" % transferFunction
18
19     cs = ColorSpace(name)
20     cs.description = name
21     cs.equalityGroup = ''
22     cs.family = 'Sony'
23     cs.isData=False
24
25     def sLog1ToLinear(SLog):
26         b = 64.
27         ab = 90.
28         w = 940.
29
30         if (SLog >= ab):
31             lin = ( pow(10., ( ( ( SLog - b) / ( w - b) - 0.616596 - 0.03) / 0.432699)) - 0.037584) * 0.9
32         else:
33             lin = ( ( ( SLog - b) / ( w - b) - 0.030001222851889303) / 5.) * 0.9 
34         return lin
35
36     def sLog2ToLinear(SLog):
37         b = 64.
38         ab = 90.
39         w = 940.
40
41         if (SLog >= ab):
42             lin = ( 219. * ( pow(10., ( ( ( SLog - b) / ( w - b) - 0.616596 - 0.03) / 0.432699)) - 0.037584) / 155.) * 0.9
43         else:
44             lin = ( ( ( SLog - b) / ( w - b) - 0.030001222851889303) / 3.53881278538813) * 0.9
45         return lin
46
47     def sLog3ToLinear(codeValue):
48         if codeValue >= (171.2102946929):
49             linear = pow(10.0, ((codeValue - 420.0) / 261.5)) * (0.18 + 0.01) - 0.01
50         else:
51             linear = (codeValue - 95.0)*0.01125000/(171.2102946929 - 95.0)
52         #print( codeValue, linear )
53         return linear
54
55     cs.toReferenceTransforms = []
56
57     if transferFunction == "S-Log1":
58         data = array.array('f', "\0" * lutResolution1d * 4)
59         for c in range(lutResolution1d):
60             data[c] = sLog1ToLinear(1023.0*c/(lutResolution1d-1))
61
62         lut = "%s_to_linear.spi1d" % transferFunction
63         genlut.writeSPI1D(lutDir + "/" + lut, 0.0, 1.0, data, lutResolution1d, 1)
64
65         #print( "Writing %s" % lut)
66
67         cs.toReferenceTransforms.append( {
68             'type':'lutFile', 
69             'path':lut, 
70             'interpolation':'linear', 
71             'direction':'forward'
72         } )
73     elif transferFunction == "S-Log2":
74         data = array.array('f', "\0" * lutResolution1d * 4)
75         for c in range(lutResolution1d):
76             data[c] = sLog2ToLinear(1023.0*c/(lutResolution1d-1))
77
78         lut = "%s_to_linear.spi1d" % transferFunction
79         genlut.writeSPI1D(lutDir + "/" + lut, 0.0, 1.0, data, lutResolution1d, 1)
80
81         #print( "Writing %s" % lut)
82
83         cs.toReferenceTransforms.append( {
84             'type':'lutFile', 
85             'path':lut, 
86             'interpolation':'linear', 
87             'direction':'forward'
88         } )
89     elif transferFunction == "S-Log3":
90         data = array.array('f', "\0" * lutResolution1d * 4)
91         for c in range(lutResolution1d):
92             data[c] = sLog3ToLinear(1023.0*c/(lutResolution1d-1))
93
94         lut = "%s_to_linear.spi1d" % transferFunction
95         genlut.writeSPI1D(lutDir + "/" + lut, 0.0, 1.0, data, lutResolution1d, 1)
96
97         #print( "Writing %s" % lut)
98
99         cs.toReferenceTransforms.append( {
100             'type':'lutFile', 
101             'path':lut, 
102             'interpolation':'linear', 
103             'direction':'forward'
104         } )
105
106     if gamut == 'S-Gamut':
107         cs.toReferenceTransforms.append( {
108             'type':'matrix',
109             'matrix':mat44FromMat33([0.754338638, 0.133697046, 0.111968437,
110                                     0.021198141, 1.005410934, -0.026610548, 
111                                     -0.009756991, 0.004508563, 1.005253201]),
112             'direction':'forward'
113         })
114     elif gamut == 'S-Gamut Daylight':
115         cs.toReferenceTransforms.append( {
116             'type':'matrix',
117             'matrix':mat44FromMat33([0.8764457030, 0.0145411681, 0.1090131290,
118                                     0.0774075345, 0.9529571767, -0.0303647111, 
119                                     0.0573564351, -0.1151066335, 1.0577501984]),
120             'direction':'forward'
121         })
122     elif gamut == 'S-Gamut Tungsten':
123         cs.toReferenceTransforms.append( {
124             'type':'matrix',
125             'matrix':mat44FromMat33([1.0110238740, -0.1362526051, 0.1252287310, 
126                         0.1011994504, 0.9562196265, -0.0574190769,
127                         0.0600766530, -0.1010185315, 1.0409418785]),
128             'direction':'forward'
129         })
130     elif gamut == 'S-Gamut3.Cine':
131         cs.toReferenceTransforms.append( {
132             'type':'matrix',
133             'matrix':mat44FromMat33([0.6387886672, 0.2723514337, 0.0888598992, 
134                                     -0.0039159061, 1.0880732308, -0.0841573249, 
135                                     -0.0299072021, -0.0264325799, 1.0563397820]),
136             'direction':'forward'
137         })
138     elif gamut == 'S-Gamut3':
139         cs.toReferenceTransforms.append( {
140             'type':'matrix',
141             'matrix':mat44FromMat33([0.7529825954, 0.1433702162, 0.1036471884, 
142                         0.0217076974, 1.0153188355, -0.0370265329, 
143                         -0.0094160528, 0.0033704179, 1.0060456349]),
144             'direction':'forward'
145         })
146
147     cs.fromReferenceTransforms = []
148     return cs
149
150 def createColorSpaces(lutDir, lutResolution1d):
151     colorspaces = []
152
153     # SLog1
154     SLog1SGamut = createSlog("S-Gamut", "S-Log1", "S-Log", lutDir, lutResolution1d)
155     colorspaces.append(SLog1SGamut)
156
157     # SLog2
158     SLog2SGamut = createSlog("S-Gamut", "S-Log2", "S-Log2", lutDir, lutResolution1d)
159     colorspaces.append(SLog2SGamut)
160
161     SLog2SGamutDaylight = createSlog("S-Gamut Daylight", "S-Log2", "S-Log2", lutDir, lutResolution1d)
162     colorspaces.append(SLog2SGamutDaylight)
163
164     SLog2SGamutTungsten = createSlog("S-Gamut Tungsten", "S-Log2", "S-Log2", lutDir, lutResolution1d)
165     colorspaces.append(SLog2SGamutTungsten)
166
167     # SLog3
168     SLog3SGamut3Cine = createSlog("S-Gamut3.Cine", "S-Log3", "S-Log3", lutDir, lutResolution1d)
169     colorspaces.append(SLog3SGamut3Cine)
170
171     SLog3SGamut3 = createSlog("S-Gamut3", "S-Log3", "S-Log3", lutDir, lutResolution1d)
172     colorspaces.append(SLog3SGamut3)
173
174     # Linearization only
175     SLog1 = createSlog("", "S-Log1", "S-Log", lutDir, lutResolution1d)
176     colorspaces.append(SLog1)
177
178     SLog2 = createSlog("", "S-Log2", "S-Log2", lutDir, lutResolution1d)
179     colorspaces.append(SLog2)
180
181     SLog3 = createSlog("", "S-Log3", "S-Log3", lutDir, lutResolution1d)
182     colorspaces.append(SLog3)
183
184     # Primaries only
185     SGamut = createSlog("S-Gamut", "", "S-Log", lutDir, lutResolution1d)
186     colorspaces.append(SGamut)
187
188     SGamutDaylight = createSlog("S-Gamut Daylight", "", "S-Log2", lutDir, lutResolution1d)
189     colorspaces.append(SGamutDaylight)
190
191     SGamutTungsten = createSlog("S-Gamut Tungsten", "", "S-Log2", lutDir, lutResolution1d)
192     colorspaces.append(SGamutTungsten)
193
194     SGamut3Cine = createSlog("S-Gamut3.Cine", "", "S-Log3", lutDir, lutResolution1d)
195     colorspaces.append(SGamut3Cine)
196
197     SGamut3 = createSlog("S-Gamut3", "", "S-Log3", lutDir, lutResolution1d)
198     colorspaces.append(SGamut3)
199
200     return colorspaces
201