-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathflim.py
More file actions
276 lines (227 loc) · 6.84 KB
/
flim.py
File metadata and controls
276 lines (227 loc) · 6.84 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""
flim - Filmic Color Transform
input color space: Linear BT.709 I-D65
output color space: sRGB
repo:
https://github.com/bean-mhm/flim
"""
import numpy as np
import colour
import joblib
from utils import *
# use parallel processing?
parallel = True
# print the current indices while processing? (slow)
print_indices = False
# transform a 3D LUT
def apply_transform(table: np.ndarray, preset: dict):
if len(table.shape) != 4:
raise Exception(
'table must have 4 dimensions (3 for xyz, 1 for the color channels)'
)
if table.shape[3] != 3:
raise Exception('the fourth axis must have a size of 3 (RGB)')
# LUT decompression: map range
table = colour.algebra.linear_conversion(
table,
np.array([0., 1.]),
np.array([
preset['lut_compress_log2_min'],
preset['lut_compress_log2_max']
])
)
# LUT decompression: exponent
colour.algebra.set_spow_enable(True)
table = np.power(2., table)
# LUT decompression: offset
table -= 2.**preset['lut_compress_log2_min']
# eliminate negative values
table = np.maximum(table, 0.)
# pre-exposure
table *= 2**preset['pre_exposure']
# gamut extension matrix
extend_mat = gamut_extension_mat(
preset['extended_gamut_red_scale'],
preset['extended_gamut_green_scale'],
preset['extended_gamut_blue_scale'],
preset['extended_gamut_red_rot'],
preset['extended_gamut_green_rot'],
preset['extended_gamut_blue_rot'],
preset['extended_gamut_red_mul'],
preset['extended_gamut_green_mul'],
preset['extended_gamut_blue_mul']
)
extend_mat_inv = np.linalg.inv(extend_mat)
# backlight in the extended gamut
backlight_ext = np.matmul(extend_mat, preset['print_backlight'])
# upper and lower limits in the print (in the extended gamut!)
big = 10_000_000.
white_cap = negative_and_print(
np.array([big, big, big]),
preset,
backlight_ext
)
black_cap = negative_and_print(
np.array([0., 0., 0.]),
preset,
backlight_ext
) / white_cap
# element-wise transform (calls transform_rgb)
if parallel:
print('starting parallel element-wise transform...')
num_points = table.shape[0] * table.shape[1] * table.shape[2]
stride_y = table.shape[0]
stride_z = table.shape[0] * table.shape[1]
results = joblib.Parallel(n_jobs=8)(
joblib.delayed(run_parallel)(
table,
(i % stride_y, (i % stride_z) // stride_y, i // stride_z),
preset,
extend_mat,
extend_mat_inv,
white_cap,
black_cap,
backlight_ext
)
for i in range(num_points)
)
# arrange the results
print('arranging the results...')
for z in range(table.shape[2]):
for y in range(table.shape[1]):
for x in range(table.shape[0]):
index = x + (y * stride_y) + (z * stride_z)
table[x, y, z] = results[index]
else:
print('starting serial element-wise transform...')
for z in range(table.shape[2]):
for y in range(table.shape[1]):
if print_indices:
print(f'at [0, {y}, {z}]')
for x in range(table.shape[0]):
table[x, y, z] = transform_rgb(
table[x, y, z],
preset,
extend_mat,
extend_mat_inv,
white_cap,
black_cap,
backlight_ext
)
# OETF: Linear BT.709 I-D65 -> sRGB
table = colour.models.eotf_inverse_sRGB(table)
return table
# calls transform_rgb
def run_parallel(
table,
indices,
preset: dict,
extend_mat,
extend_mat_inv,
white_cap,
black_cap,
backlight_ext
):
result = transform_rgb(
table[indices],
preset,
extend_mat,
extend_mat_inv,
white_cap,
black_cap,
backlight_ext
)
if print_indices:
print(f'{indices} done')
return result
def negative_and_print(inp, preset: dict, backlight_ext):
log2_min = preset['sigmoid_log2_min']
log2_max = preset['sigmoid_log2_max']
sigmoid_points = np.array([
preset['sigmoid_toe_x'],
preset['sigmoid_toe_y'],
preset['sigmoid_shoulder_x'],
preset['sigmoid_shoulder_y']
])
# develop negative
inp = rgb_develop(
inp,
preset['negative_film_exposure'],
log2_min,
log2_max,
sigmoid_points,
preset['negative_film_density']
)
# backlight
inp = inp * backlight_ext
# develop print
inp = rgb_develop(
inp,
preset['print_film_exposure'],
log2_min,
log2_max,
sigmoid_points,
preset['print_film_density']
)
return inp
# transform a single RGB triplet (you should never directly call this function)
def transform_rgb(
inp,
preset: dict,
extend_mat,
extend_mat_inv,
white_cap,
black_cap,
backlight_ext
):
luminance_weights = preset['luminance_weights']
luminance_weights_norm = \
luminance_weights / np.dot(luminance_weights, np.array([1., 1., 1.]))
# pre-formation filter
inp = inp * lerp(
np.array([1., 1., 1.]),
preset['pre_formation_filter'],
preset['pre_formation_filter_strength']
)
# convert to the extended gamut
inp = np.matmul(extend_mat, inp)
# develop negative and print
inp = negative_and_print(inp, preset, backlight_ext)
# white cap
inp /= white_cap
# black cap
if preset['black_point'] in ['Auto', 'auto', '', None]:
inp = rgb_uniform_offset(
inp,
np.dot(black_cap, luminance_weights_norm),
0.,
luminance_weights_norm
)
else:
inp = rgb_uniform_offset(
inp,
preset['black_point'] / 1000.,
0.,
luminance_weights_norm
)
# convert from the extended gamut and clip out-of-gamut triplets
inp = np.matmul(extend_mat_inv, inp)
inp = np.maximum(inp, 0.)
# post-formation filter
inp = inp * lerp(
np.array([1., 1., 1.]),
preset['post_formation_filter'],
preset['post_formation_filter_strength']
)
# clip
inp = np.clip(inp, 0., 1.)
# midtone saturation
mono = np.dot(inp, luminance_weights_norm)
midtone_fac = max(1. - (abs(mono - .5) / .45), 0.)
inp = lerp(
inp,
rgb_adjust_hsv(inp, .5, preset['midtone_saturation'], 1.),
midtone_fac
)
# clip and return
return np.clip(inp, 0., 1.)