forked from imageworks/OpenColorIO-Configs
-
Notifications
You must be signed in to change notification settings - Fork 51
/
arri.py
227 lines (184 loc) · 6 KB
/
arri.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Implements support for *ARRI* colorspaces conversions and transfer functions.
"""
from __future__ import division
import array
import math
import os
import PyOpenColorIO as ocio
import aces_ocio.generate_lut as genlut
from aces_ocio.utilities import ColorSpace, mat44_from_mat33, sanitize
__author__ = 'ACES Developers'
__copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
__license__ = ''
__maintainer__ = 'ACES Developers'
__email__ = 'aces@oscars.org'
__status__ = 'Production'
__all__ = ['create_log_c',
'create_colorspaces']
def create_log_c(gamut,
transfer_function,
exposure_index,
lut_directory,
lut_resolution_1d,
aliases):
"""
Object description.
LogC to ACES.
Parameters
----------
parameter : type
Parameter description.
Returns
-------
type
Return value description.
"""
name = '%s (EI%s) - %s' % (transfer_function, exposure_index, gamut)
if transfer_function == '':
name = 'Linear - ARRI %s' % gamut
if gamut == '':
name = 'Curve - %s (EI%s)' % (transfer_function, exposure_index)
cs = ColorSpace(name)
cs.description = name
cs.aliases = aliases
cs.equality_group = ''
cs.family = 'Input/ARRI'
cs.is_data = False
if gamut and transfer_function:
cs.aces_transform_id = (
'IDT.ARRI.Alexa-v3-logC-EI%s.a1.v1' % exposure_index)
# A linear space needs allocation variables.
if transfer_function == '':
cs.allocation_type = ocio.Constants.ALLOCATION_LG2
cs.allocation_vars = [-8, 5, 0.00390625]
IDT_maker_version = '0.08'
nominal_EI = 400
black_signal = 0.003907
mid_gray_signal = 0.01
encoding_gain = 0.256598
encoding_offset = 0.391007
def gain_for_EI(EI):
return (math.log(EI / nominal_EI) / math.log(2) * (
0.89 - 1) / 3 + 1) * encoding_gain
def log_c_inverse_parameters_for_EI(EI):
cut = 1 / 9
slope = 1 / (cut * math.log(10))
offset = math.log10(cut) - slope * cut
gain = EI / nominal_EI
gray = mid_gray_signal / gain
# The higher the EI, the lower the gamma.
enc_gain = gain_for_EI(EI)
enc_offset = encoding_offset
for i in range(0, 3):
nz = ((95 / 1023 - enc_offset) / enc_gain - offset) / slope
enc_offset = encoding_offset - math.log10(1 + nz) * enc_gain
a = 1 / gray
b = nz - black_signal / gray
e = slope * a * enc_gain
f = enc_gain * (slope * b + offset) + enc_offset
# Ensuring we can return relative exposure.
s = 4 / (0.18 * EI)
t = black_signal
b += a * t
a *= s
f += e * t
e *= s
return {'a': a,
'b': b,
'cut': (cut - b) / a,
'c': enc_gain,
'd': enc_offset,
'e': e,
'f': f}
def normalized_log_c_to_linear(code_value, exposure_index):
p = log_c_inverse_parameters_for_EI(exposure_index)
breakpoint = p['e'] * p['cut'] + p['f']
if code_value > breakpoint:
linear = ((pow(10, (code_value - p['d']) / p['c']) -
p['b']) / p['a'])
else:
linear = (code_value - p['f']) / p['e']
return linear
cs.to_reference_transforms = []
if transfer_function == 'V3 LogC':
data = array.array('f', '\0' * lut_resolution_1d * 4)
for c in range(lut_resolution_1d):
data[c] = normalized_log_c_to_linear(c / (lut_resolution_1d - 1),
int(exposure_index))
lut = '%s_to_linear.spi1d' % (
'%s_%s' % (transfer_function, exposure_index))
lut = sanitize(lut)
genlut.write_SPI_1d(
os.path.join(lut_directory, lut),
0,
1,
data,
lut_resolution_1d,
1)
cs.to_reference_transforms.append({
'type': 'lutFile',
'path': lut,
'interpolation': 'linear',
'direction': 'forward'})
if gamut == 'Wide Gamut':
cs.to_reference_transforms.append({
'type': 'matrix',
'matrix': mat44_from_mat33([0.680206, 0.236137, 0.083658,
0.085415, 1.017471, -0.102886,
0.002057, -0.062563, 1.060506]),
'direction': 'forward'})
cs.from_reference_transforms = []
return cs
def create_colorspaces(lut_directory, lut_resolution_1d):
"""
Generates the colorspace conversions.
Parameters
----------
parameter : type
Parameter description.
Returns
-------
type
Return value description.
"""
colorspaces = []
transfer_function = 'V3 LogC'
gamut = 'Wide Gamut'
# EIs = [160, 200, 250, 320, 400, 500, 640, 800,
# 1000, 1280, 1600, 2000, 2560, 3200]
EIs = [160, 200, 250, 320, 400, 500, 640, 800,
1000, 1280, 1600, 2000, 2560, 3200]
default_EI = 800
# Full Conversion
for EI in EIs:
log_c_EI_full = create_log_c(
gamut,
transfer_function,
EI,
lut_directory,
lut_resolution_1d,
['%sei%s_%s' % ('logc3', str(EI), 'arriwide')])
colorspaces.append(log_c_EI_full)
# Linearization Only
for EI in [800]:
log_c_EI_linearization = create_log_c(
'',
transfer_function,
EI,
lut_directory,
lut_resolution_1d,
['crv_%sei%s' % ('logc3', str(EI))])
colorspaces.append(log_c_EI_linearization)
# Primaries Only
log_c_EI_primaries = create_log_c(
gamut,
'',
default_EI,
lut_directory,
lut_resolution_1d,
['%s_%s' % ('lin', 'arriwide')])
colorspaces.append(log_c_EI_primaries)
return colorspaces