fix(image): guard shape/dtype edge cases in the data path

This commit is contained in:
2026-07-14 12:53:04 +02:00
committed by Jan Wyzula
parent aca13fb305
commit c1ea65f7f9
2 changed files with 20 additions and 1 deletions
+2
View File
@@ -959,6 +959,8 @@ class Image(ImageBase):
Returns:
np.ndarray: The updated image buffer with adjusted shapes.
"""
# Guard for wrong data shapes
new_data = np.atleast_1d(np.asarray(new_data))
new_len = new_data.shape[0]
if not hasattr(image, "buffer"):
image.buffer = []
+18 -1
View File
@@ -1439,4 +1439,21 @@ def test_image_processor_log_is_finite_for_non_positive():
# maps to 0.
assert out[0, 0] == pytest.approx(-1.0)
assert out[1, 0] == pytest.approx(-1.0)
assert proc.log(np.array([[1.0]]))[0, 0] == pytest.approx(0.0)
assert proc.log(np.array([[1.0]]))[0, 0] == pytest.approx(0.0)
##############################################
# Shape / dtype edge cases in the data path
##############################################
def test_adjust_image_buffer_coerces_list_and_scalar(qtbot, mocked_client):
"""Non-ndarray payloads (python list, 0-d scalar) must not crash the
1D buffer accumulation."""
view = create_widget(qtbot, Image, client=mocked_client)
image = view.main_image
buf = view.adjust_image_buffer(image, [1, 2, 3]) # python list
assert buf.shape == (1, 3)
buf = view.adjust_image_buffer(image, np.array(42.0)) # 0-d scalar
assert buf.shape[0] == 2