Created
April 25, 2020 21:35
-
-
Save remia/2b6f825ed33239457b8c7a2dc26e9bda to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import colour | |
import numpy as np | |
import matplotlib.pyplot as plt | |
np.set_printoptions(suppress=True) | |
""" | |
Example shader generated by ocio v1, using Troy's config untouched, lutsize is 32 | |
vec4 OCIODisplay(vec4 inPixel, sampler3D lut3d) | |
{ | |
vec4 out_pixel = inPixel; | |
out_pixel.rgb = max( | |
vec3(1.17549e-38, 1.17549e-38, 1.17549e-38), | |
vec3(1, 1, 1) * out_pixel.rgb + vec3(0, 0, 0) | |
); | |
// log2 (log base convert) => 1.4427 == 1 / log(2) | |
out_pixel.rgb = vec3(1.4427, 1.4427, 1.4427) * log(out_pixel.rgb) + vec3(0, 0, 0); | |
// scale to range => 0.04 == 1 / (lg2max - lg2min) | |
out_pixel = vec4(0.04, 0.04, 0.04, 1) * out_pixel; | |
// offset to range => 0.498956 == |lg2min / (lg2max - lg2min)| | |
out_pixel = vec4(0.498956, 0.498956, 0.498956, 0) + out_pixel; | |
// sample using opengl coordinate system | |
// 0.96875 == (lutsize - 1 / lutsize) | |
// 0.015625 == (1.0 / 2 * lutsize) | |
out_pixel.rgb = texture3D(lut3d, 0.96875 * out_pixel.rgb + 0.015625).rgb; | |
return out_pixel; | |
} | |
""" | |
lg2_min = -12.4739 | |
lg2_max = 12.5261 | |
scale = 1.0 / (lg2_max - lg2_min) | |
offset = abs(lg2_min / (lg2_max - lg2_min)) | |
lut_size = 32 | |
lut = colour.LUT3D(size=lut_size) | |
lut.table = (lut.table - offset) / scale | |
lut.table = np.power(2, lut.table) | |
lut.table = np.clip(lut.table, 0.0, 1.0) | |
lut.table = colour.models.sRGB_COLOURSPACE.cctf_encoding(lut.table) | |
input_rgb = [1.0, 1.0, 1.0] | |
rgb = np.log2(input_rgb) | |
rgb = scale * rgb + offset | |
output_rgb = colour.table_interpolation(rgb, lut.table, method='Trilinear') | |
print("Allocation lg2 {}".format([lg2_min, lg2_max])) | |
print("Input {} => Output {}".format(input_rgb, np.rint(output_rgb * 255))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment