Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(color): reject unsupported CFA in rgb_to_raw (#5340)

NiteeshGowda72c996899a6f01
diff --git a/changelog.d/5340.breaking.md b/changelog.d/5340.breaking.md
new file mode 100644
index 000000000..a6d42a257
--- /dev/null
+++ b/changelog.d/5340.breaking.md
@@ -0,0 +1,3 @@
+`rgb_to_raw` and `RgbToRaw` now raise `ValueError` for a `cfa` that is not a `CFA` member, as
+`raw_to_rgb` and `raw_to_rgb_2x2_downscaled` already did. Previously any other value, such as a
+string, an integer or `None`, silently returned the green channel as the mosaic.
diff --git a/kornia/color/raw.py b/kornia/color/raw.py
index 6fdfbf2c9..a0413edb1 100644
--- a/kornia/color/raw.py
+++ b/kornia/color/raw.py
@@ -229,6 +229,8 @@ def rgb_to_raw(image: torch.Tensor, cfa: CFA) -> torch.Tensor:
elif cfa == CFA.GR:
output[..., :, 1::2, ::2] = image[..., 0:1, 1::2, ::2] # red
output[..., :, ::2, 1::2] = image[..., 2:3, ::2, 1::2] # blue
+ else:
+ raise ValueError(f"Unsupported CFA value. Got {cfa}")
return output
diff --git a/tests/color/test_raw.py b/tests/color/test_raw.py
index ca8b9d572..001d52011 100644
--- a/tests/color/test_raw.py
+++ b/tests/color/test_raw.py
@@ -175,6 +175,10 @@ class TestRgbToRaw(BaseTester):
with pytest.raises(ValueError):
assert kornia.color.rgb_to_raw(img, kornia.color.raw.CFA.BG)
+ img = torch.rand(1, 3, 2, 3, device=device, dtype=dtype)
+ with pytest.raises(ValueError, match="Unsupported CFA value"):
+ kornia.color.rgb_to_raw(img, "bad")
+
# Reverse test in rawtorgb is sufficient functional test
def test_gradcheck(self, device, dtype):