MCPcopy Create free account
hub / github.com/apple/ml-sharp / sRGB2linearRGB

Function sRGB2linearRGB

src/sharp/utils/color_space.py:31–58  ·  view source on GitHub ↗

SRGB to linearRGB conversion function. Reference: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf Section 7.7.7 Args: sRGB: Input image tensor in sRGB space.

(sRGB: torch.Tensor)

Source from the content-addressed store, hash-verified

29
30
31def sRGB2linearRGB(sRGB: torch.Tensor) -> torch.Tensor:
32 """SRGB to linearRGB conversion function.
33
34 Reference:
35 https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
36 Section 7.7.7
37
38 Args:
39 sRGB: Input image tensor in sRGB space.
40 """
41 # We need to use robust_where to clamp the second branch.
42 # Otherwise, torch.where will lead to NaN in the backward pass, see
43 # https://github.com/pytorch/pytorch/issues/68425
44 THRESHOLD = 0.04045
45
46 def branch_true_func(x):
47 return x / 12.92
48
49 def branch_false_func(x):
50 return ((x + 0.055) / 1.055) ** 2.4
51
52 return robust_where(
53 sRGB <= THRESHOLD,
54 sRGB,
55 branch_true_func,
56 branch_false_func,
57 branch_false_safe_value=THRESHOLD,
58 )
59
60
61def linearRGB2sRGB(linearRGB: torch.Tensor) -> torch.Tensor:

Callers 1

_color_activationMethod · 0.90

Calls 1

robust_whereFunction · 0.90

Tested by

no test coverage detected