Skip to content

Commit d56b3b1

Browse files
committed
Add mono output format tests for GenTL backend
Extend the GenTL test settings factory to accept and propagate `preserve_mono`. Add a parametrized backend test that validates mono frame handling across `Mono8`/`Mono12` inputs, including uint8 normalization, grayscale-to-BGR expansion when mono preservation is disabled, and persistence/reporting of `actual_output_format` and `preserve_mono` in backend settings.
1 parent 7c153c8 commit d56b3b1

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

‎tests/cameras/backends/conftest.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,7 @@ def _make(
12621262
fps=0.0,
12631263
exposure=0,
12641264
gain=0.0,
1265+
preserve_mono=False,
12651266
enabled=True,
12661267
properties=None,
12671268
):
@@ -1293,6 +1294,7 @@ def _make(
12931294
fps=fps,
12941295
exposure=exposure,
12951296
gain=gain,
1297+
preserve_mono=preserve_mono,
12961298
enabled=enabled,
12971299
properties=props,
12981300
)

‎tests/cameras/backends/test_gentl_backend.py‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,143 @@ def test_open_starts_stream_and_read_returns_frame(patch_gentl_sdk, gentl_settin
6767
assert be._device_label is None
6868

6969

70+
@pytest.mark.parametrize(
71+
(
72+
"pixel_format",
73+
"preserve_mono",
74+
"source_dtype",
75+
"expected_shape",
76+
"expected_output_format",
77+
),
78+
[
79+
(
80+
"Mono8",
81+
True,
82+
np.uint8,
83+
(2, 3),
84+
"Mono8",
85+
),
86+
(
87+
"Mono8",
88+
False,
89+
np.uint8,
90+
(2, 3, 3),
91+
"BGR8",
92+
),
93+
(
94+
"Mono12",
95+
True,
96+
np.uint16,
97+
(2, 3),
98+
"Mono8",
99+
),
100+
],
101+
)
102+
def test_mono_output_format_conversion_and_persistence(
103+
patch_gentl_sdk,
104+
gentl_settings_factory,
105+
pixel_format,
106+
preserve_mono,
107+
source_dtype,
108+
expected_shape,
109+
expected_output_format,
110+
):
111+
gb = patch_gentl_sdk
112+
113+
settings = gentl_settings_factory(
114+
preserve_mono=preserve_mono,
115+
properties={
116+
"gentl": {
117+
"pixel_format": pixel_format,
118+
}
119+
},
120+
)
121+
backend = gb.GenTLCameraBackend(settings)
122+
123+
if source_dtype == np.uint8:
124+
source = np.array(
125+
[
126+
[0, 32, 64],
127+
[128, 192, 255],
128+
],
129+
dtype=np.uint8,
130+
)
131+
else:
132+
source = np.array(
133+
[
134+
[0, 512, 1024],
135+
[2048, 3072, 4095],
136+
],
137+
dtype=np.uint16,
138+
)
139+
140+
component = types.SimpleNamespace(
141+
data=source.ravel(),
142+
height=source.shape[0],
143+
width=source.shape[1],
144+
)
145+
buffer = types.SimpleNamespace(
146+
payload=types.SimpleNamespace(
147+
components=[component],
148+
)
149+
)
150+
151+
class FetchContext:
152+
def __enter__(self):
153+
return buffer
154+
155+
def __exit__(
156+
self,
157+
exc_type,
158+
exc_value,
159+
traceback,
160+
):
161+
return False
162+
163+
class FakeAcquirer:
164+
remote_device = types.SimpleNamespace(
165+
node_map=types.SimpleNamespace(),
166+
)
167+
168+
def fetch(self, timeout):
169+
return FetchContext()
170+
171+
backend._acquirer = FakeAcquirer()
172+
backend._pixel_format = pixel_format
173+
backend._camera_pixel_format = pixel_format
174+
175+
captured = backend.read()
176+
frame = captured.frame
177+
178+
assert frame.dtype == np.uint8
179+
assert frame.shape == expected_shape
180+
181+
# Native camera format and emitted application format are distinct.
182+
assert backend.actual_pixel_format == pixel_format
183+
assert backend.actual_output_format == expected_output_format
184+
185+
namespace = settings.properties["gentl"]
186+
assert namespace["actual_output_format"] == expected_output_format
187+
assert namespace["preserve_mono"] is preserve_mono
188+
189+
if preserve_mono:
190+
assert frame.ndim == 2
191+
else:
192+
assert frame.ndim == 3
193+
assert frame.shape[2] == 3
194+
195+
# Grayscale-to-BGR conversion duplicates the mono value
196+
# across all three channels.
197+
np.testing.assert_array_equal(
198+
frame[:, :, 0],
199+
frame[:, :, 1],
200+
)
201+
np.testing.assert_array_equal(
202+
frame[:, :, 1],
203+
frame[:, :, 2],
204+
)
205+
206+
70207
def test_fast_start_does_not_start_stream_and_read_times_out(patch_gentl_sdk, gentl_settings_factory):
71208
gb = patch_gentl_sdk
72209

0 commit comments

Comments
 (0)