Skip to content

Commit 9681c51

Browse files
wip new API
1 parent d0eee81 commit 9681c51

File tree

3 files changed

+109
-52
lines changed

3 files changed

+109
-52
lines changed

colour_clf_io/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@
103103
__change_version__ = "1"
104104
__version__ = f"{__major_version__}.{__minor_version__}.{__change_version__}"
105105

106+
try:
107+
from colour_clf_io.processing import CLFProcessList
108+
109+
__all__ += ["CLFProcessList"]
110+
except ImportError:
111+
pass
112+
106113

107114
def read_clf_from_file(path: str | Path) -> ProcessList:
108115
"""

colour_clf_io/processing.py

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -833,48 +833,65 @@ def as_LUT_sequence_item( # noqa: PLR0911
833833
raise RuntimeError(message)
834834

835835

836-
def apply(
837-
process_list: clf.ProcessList,
838-
value: NDArrayFloat,
839-
normalised_values: bool = False,
840-
) -> NDArrayFloat:
841-
"""
842-
Apply the transformation described by the given :class:`colour_clf_io.ProcessList`
843-
to the given value.
844-
845-
Parameters
846-
----------
847-
process_list
848-
The :class:`colour_clf_io.ProcessList` instance to apply.
849-
value
850-
Input value in the form of an array. Shape and data format need to be
851-
compatible with the given `process_list`.
852-
normalised_values
853-
Indicates whether the input values are normalised to the range 0..1. If
854-
this is the case, the range will be expanded to the input range expected
855-
by the given `process_list`.
856-
857-
Returns
858-
-------
859-
:class:`NDArrayFloat`
860-
Result of applying the given :class:`colour_clf_io.ProcessList`.
861-
862-
Raises
863-
------
864-
:class:`CLFExecutionError`
865-
If the given *process_list* is invalid according to the CLF specification
866-
(e.g., missing or invalid parameters).
867-
"""
868-
if not normalised_values:
869-
value = value / process_list.process_nodes[0].in_bit_depth.scale_factor()
870-
871-
lut_sequence_items = [
872-
as_LUT_sequence_item(node) for node in process_list.process_nodes
873-
]
874-
sequence = LUTSequence(*lut_sequence_items)
875-
result = sequence.apply(value)
876-
877-
if not normalised_values:
878-
result = result * process_list.process_nodes[-1].out_bit_depth.scale_factor()
879-
880-
return result
836+
# def apply(
837+
# process_list: clf.ProcessList,
838+
# value: NDArrayFloat,
839+
# normalised_values: bool = False,
840+
# ) -> NDArrayFloat:
841+
# """
842+
# Apply the transformation described by the given :class:`colour_clf_io.ProcessList`
843+
# to the given value.
844+
#
845+
# Parameters
846+
# ----------
847+
# process_list
848+
# The :class:`colour_clf_io.ProcessList` instance to apply.
849+
# value
850+
# Input value in the form of an array. Shape and data format need to be
851+
# compatible with the given `process_list`.
852+
# normalised_values
853+
# Indicates whether the input values are normalised to the range 0..1. If
854+
# this is the case, the range will be expanded to the input range expected
855+
# by the given `process_list`.
856+
#
857+
# Returns
858+
# -------
859+
# :class:`NDArrayFloat`
860+
# Result of applying the given :class:`colour_clf_io.ProcessList`.
861+
#
862+
# Raises
863+
# ------
864+
# :class:`CLFExecutionError`
865+
# If the given *process_list* is invalid according to the CLF specification
866+
# (e.g., missing or invalid parameters).
867+
# """
868+
# if not normalised_values:
869+
# value = value / process_list.process_nodes[0].in_bit_depth.scale_factor()
870+
#
871+
# lut_sequence_items = [
872+
# as_LUT_sequence_item(node) for node in process_list.process_nodes
873+
# ]
874+
# sequence = LUTSequence(*lut_sequence_items)
875+
# result = sequence.apply(value)
876+
#
877+
# if not normalised_values:
878+
# result = result * process_list.process_nodes[-1].out_bit_depth.scale_factor()
879+
#
880+
# return result
881+
882+
883+
class CLFProcessList(LUTSequence):
884+
"""
885+
Defines a *LUT* sequence created from a `colour_clf_io.ProcessList`. Creates the
886+
nodes needed to execute the transformation described in the *Process List*.
887+
"""
888+
889+
def __init__(
890+
self,
891+
process_list: clf.ProcessList,
892+
) -> None:
893+
lut_sequence_items = [
894+
as_LUT_sequence_item(node) for node in process_list.process_nodes
895+
]
896+
sequence = LUTSequence(*lut_sequence_items)
897+
super().__init__(*sequence)

colour_clf_io/tests/processing/test_common.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import numpy as np
1111

1212
import colour_clf_io as clf
13-
from colour_clf_io.processing import apply
13+
from colour_clf_io.processing import CLFProcessList
1414

1515
__all__ = [
1616
"assert_ocio_consistency",
@@ -100,6 +100,35 @@ def result_as_array(result_text: str) -> NDArrayFloat:
100100
return np.array(result_values)
101101

102102

103+
def apply_input_normalisation(
104+
process_list: clf.processing.CLFProcessList,
105+
lut_sequence: clf.processing.CLFProcessList,
106+
) -> None:
107+
input_scale = process_list.process_nodes[0].in_bit_depth.scale_factor()
108+
output_scale = process_list.process_nodes[-1].out_bit_depth.scale_factor()
109+
110+
pre_scaling_node = clf.processing.Range(
111+
clf.Range(
112+
min_in_value=0,
113+
max_in_value=input_scale,
114+
min_out_value=0,
115+
max_out_value=1,
116+
style=clf.RangeStyle.NO_CLAMP,
117+
)
118+
)
119+
post_scaling_node = clf.processing.Range(
120+
clf.Range(
121+
min_in_value=0,
122+
max_in_value=1,
123+
min_out_value=0,
124+
max_out_value=output_scale,
125+
)
126+
)
127+
sequence = process_list.sequence
128+
sequence = [pre_scaling_node, *sequence, post_scaling_node]
129+
process_list.sequence = sequence
130+
131+
103132
def assert_ocio_consistency(
104133
value: NDArrayFloat, snippet: str, err_msg: str = "", decimals: int = 5
105134
) -> None:
@@ -110,7 +139,9 @@ def assert_ocio_consistency(
110139
if process_list is None:
111140
err = "Invalid CLF snippet."
112141
raise AssertionError(err)
113-
process_list_output = apply(process_list, value, normalised_values=True)
142+
lut_sequence = CLFProcessList(process_list)
143+
apply_input_normalisation(process_list, lut_sequence)
144+
process_list_output = lut_sequence.apply(value)
114145
value_tuple = value[0], value[1], value[2]
115146
ocio_output = ocio_output_for_snippet(snippet, value_tuple)
116147
# Note: OCIO only accepts 16-bit floats so the precision agreement is limited.
@@ -119,17 +150,19 @@ def assert_ocio_consistency(
119150
)
120151

121152

122-
def assert_ocio_consistency_for_file(value_rgb: NDArrayFloat, clf_path: str) -> None:
153+
def assert_ocio_consistency_for_file(value: NDArrayFloat, clf_path: str) -> None:
123154
"""Assert that the colour library calculates the same output as the OCIO reference
124155
implementation for the given file.
125156
"""
126157

127-
clf_data = clf.read_clf_from_file(clf_path)
128-
if clf_data is None:
158+
process_list = clf.read_clf_from_file(clf_path)
159+
if process_list is None:
129160
err = "Invalid CLF snippet."
130161
raise AssertionError(err)
131-
process_list_output = apply(clf_data, value_rgb, normalised_values=True)
132-
ocio_output = ocio_output_for_file(clf_path, value_rgb)
162+
lut_sequence = CLFProcessList(process_list)
163+
apply_input_normalisation(process_list, lut_sequence)
164+
process_list_output = lut_sequence.apply(value)
165+
ocio_output = ocio_output_for_file(clf_path, value)
133166
np.testing.assert_array_almost_equal(process_list_output, ocio_output)
134167

135168

0 commit comments

Comments
 (0)