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)
| 29 | |
| 30 | |
| 31 | def 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 | |
| 61 | def linearRGB2sRGB(linearRGB: torch.Tensor) -> torch.Tensor: |
no test coverage detected