diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2020-06-15 23:08:48 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2020-06-15 23:08:48 +0200 |
commit | 0d41f4dac20cc75881a69fcbc21b00af290213bf (patch) | |
tree | 6fbaf09f51c6d0d9788e98fc0b2b306a14b41a24 /test_coeff.py | |
parent | 25cd4e2bfffb59a1663a0620a445081723e2627e (diff) |
Adapt Jakob and Hanika's interpolation code
Diffstat (limited to 'test_coeff.py')
-rw-r--r-- | test_coeff.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/test_coeff.py b/test_coeff.py new file mode 100644 index 0000000..e53c6f1 --- /dev/null +++ b/test_coeff.py @@ -0,0 +1,59 @@ +import numpy as np, struct +from scipy.interpolate import RegularGridInterpolator +from colour import * +from colour.plotting import * +from colour.models import eotf_inverse_sRGB +from jakob_hanika import jakob_hanika, model_sd +from test_colorchecker import plot_comparison + + + +illuminant = "D65" +ill_xy = ILLUMINANTS["CIE 1931 2 Degree Standard Observer"][illuminant] +ill_sd = SpectralDistribution(ILLUMINANTS_SDS[illuminant]) + + + +class JakobHanikaInterpolator: + def __init__(self): + pass + + def read_file(self, path): + with open(path, "rb") as fd: + if fd.read(4).decode("ISO-8859-1") != "SPEC": + raise ValueError("bad magic number") + + self.res = struct.unpack("i", fd.read(4))[0] + self.scale = np.fromfile(fd, count=self.res, dtype=np.float32) # FIXME: default dtype + coeffs = np.fromfile(fd, count=3*self.res**3*3, dtype=np.float32) # FIXME: default dtype + coeffs = coeffs.reshape(3, self.res, self.res, self.res, 3) + + t = np.linspace(0, 1, self.res) + axes = [self.scale, t, t] + self.cubes = [RegularGridInterpolator(axes, coeffs[j, :, :, :, :], bounds_error=False) for j in range(3)] + + def __call__(self, RGB): + RGB = np.asarray(RGB, dtype=np.float) # FIXME: default dtype + vmax = RGB.max() + imax = RGB.argmax() + cube = self.cubes[imax] + chroma = RGB / (vmax + 1e-10) + chroma = np.array([ + vmax, + chroma[..., (imax + 2) % 3], + chroma[..., (imax + 1) % 3] + ]) + return cube(chroma).squeeze() + + + +if __name__ == "__main__": + jh = JakobHanikaInterpolator() + jh.read_file("data/srgb.coeff") + + linear_RGB = np.array([1, 0, 0]) # A random test color + RGB = eotf_inverse_sRGB(linear_RGB) + XYZ = sRGB_to_XYZ(RGB) + cc = jh(linear_RGB) + + plot_comparison(XYZ, model_sd(cc, primed=False), "Model", 123, ill_sd) |