2 # -*- coding: utf-8 -*-
7 import aces_ocio.generateLUT as genlut
8 from aces_ocio.util import ColorSpace, mat44FromMat33
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
19 name = "%s (EI%s)" % (transferFunction, exposureIndex)
28 IDT_maker_version = "0.08"
31 blackSignal = 0.003907
33 encodingGain = 0.256598
34 encodingOffset = 0.391007
37 return (math.log(EI/nominalEI)/math.log(2) * (0.89 - 1) / 3 + 1) * encodingGain
39 def LogCInverseParametersForEI(EI) :
41 slope = 1.0 / (cut * math.log(10))
42 offset = math.log10(cut) - slope * cut
44 gray = midGraySignal / gain
45 # The higher the EI, the lower the gamma
46 encGain = gainForEI(EI)
47 encOffset = encodingOffset
49 nz = ((95.0 / 1023.0 - encOffset) / encGain - offset) / slope
50 encOffset = encodingOffset - math.log10(1 + nz) * encGain
51 # Calculate some intermediate values
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
65 'cut' : (cut - b) / a,
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']
77 linear = (codeValue/1023.0 - p['f']) / p['e']
79 #print( codeValue, linear )
83 cs.toReferenceTransforms = []
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))
90 lut = "%s_to_linear.spi1d" % ("%s_%s" % (transferFunction, exposureIndex))
92 # Remove spaces and parentheses
93 lut = lut.replace(' ', '_').replace(')', '_').replace('(', '_')
95 genlut.writeSPI1D(lutDir + "/" + lut, 0.0, 1.0, data, lutResolution1d, 1)
97 #print( "Writing %s" % lut)
98 cs.toReferenceTransforms.append( {
101 'interpolation':'linear',
102 'direction':'forward'
105 if gamut == 'Wide Gamut':
106 cs.toReferenceTransforms.append( {
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'
114 cs.fromReferenceTransforms = []
117 def createColorSpaces(lutDir, lutResolution1d):
120 transferFunction = "V3 LogC"
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]
128 LogCEIfull = createLogC(gamut, transferFunction, EI, "LogC", lutDir, lutResolution1d)
129 colorspaces.append(LogCEIfull)
133 LogCEIlinearization = createLogC("", transferFunction, EI, "LogC", lutDir, lutResolution1d)
134 colorspaces.append(LogCEIlinearization)
137 LogCEIprimaries = createLogC(gamut, "", defaultEI, "LogC", lutDir, lutResolution1d)
138 colorspaces.append(LogCEIprimaries)