Skip to content

Commit c3fc914

Browse files
committed
adding unit test for applyRGB buffer, check in the applyRGB if the buffer is C order or not, for now throw error
Signed-off-by: Afsaneh Sheikhmiri <[email protected]>
1 parent 7e7f2de commit c3fc914

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/bindings/python/PyCPUProcessor.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ written to the dstImgDesc image, leaving srcImgDesc unchanged.
100100
BitDepth bitDepth = getBufferBitDepth(info);
101101

102102
py::gil_scoped_release release;
103+
bool isCContiguous = true;
104+
if (info.ndim >= 2)
105+
{
106+
// last dimension stride should be itemsize
107+
if (info.strides.back() != info.itemsize)
108+
{
109+
isCContiguous = false;
110+
}
111+
}
112+
if (!isCContiguous)
113+
{
114+
throw std::runtime_error("applyRGB only supports C-contiguous (row-major) arrays");
115+
}
103116

104117
long numChannels = 3;
105118
long width = (long)info.size / numChannels;

tests/python/CPUProcessorTest.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,89 @@ def test_apply_rgb_list(self):
385385
delta=self.FLOAT_DELTA
386386
)
387387

388+
def test_apply_rgb_buffer_column_major(self):
389+
if not np:
390+
logger.warning("NumPy not found. Skipping test!")
391+
return
392+
393+
for arr, cpu_proc_fwd, cpu_proc_inv in [
394+
(
395+
self.float_rgb_2d,
396+
self.default_cpu_proc_fwd,
397+
self.default_cpu_proc_inv
398+
),
399+
(
400+
self.float_rgb_3d,
401+
self.default_cpu_proc_fwd,
402+
self.default_cpu_proc_inv
403+
),
404+
(
405+
self.half_rgb_2d,
406+
self.half_cpu_proc_fwd,
407+
self.half_cpu_proc_inv
408+
),
409+
(
410+
self.half_rgb_3d,
411+
self.half_cpu_proc_fwd,
412+
self.half_cpu_proc_inv
413+
),
414+
(
415+
self.uint16_rgb_2d,
416+
self.uint16_cpu_proc_fwd,
417+
self.uint16_cpu_proc_inv
418+
),
419+
(
420+
self.uint16_rgb_3d,
421+
self.uint16_cpu_proc_fwd,
422+
self.uint16_cpu_proc_inv
423+
),
424+
(
425+
self.uint8_rgb_2d,
426+
self.uint8_cpu_proc_fwd,
427+
self.uint8_cpu_proc_inv
428+
),
429+
(
430+
self.uint8_rgb_3d,
431+
self.uint8_cpu_proc_fwd,
432+
self.uint8_cpu_proc_inv
433+
),
434+
]:
435+
# Transpose to column-major format and
436+
# Process duplicate array
437+
arr_copy = arr.copy().T
438+
439+
cpu_proc_fwd.applyRGB(arr_copy)
440+
for i in range(arr_copy.size):
441+
if arr.dtype in (np.float32, np.float16):
442+
self.assertAlmostEqual(
443+
arr_copy.flat[i],
444+
arr.flat[i] * 0.5,
445+
delta=self.FLOAT_DELTA
446+
)
447+
else:
448+
self.assertAlmostEqual(
449+
arr_copy.flat[i],
450+
arr.flat[i] // 2,
451+
delta=self.UINT_DELTA
452+
)
453+
454+
# Inverse transform roundtrips values in place
455+
cpu_proc_inv.applyRGB(arr_copy)
456+
457+
for i in range(arr_copy.size):
458+
if arr.dtype in (np.float32, np.float16):
459+
self.assertAlmostEqual(
460+
arr_copy.flat[i],
461+
arr.flat[i],
462+
delta=self.FLOAT_DELTA
463+
)
464+
else:
465+
self.assertAlmostEqual(
466+
arr_copy.flat[i],
467+
arr.flat[i],
468+
delta=self.UINT_DELTA
469+
)
470+
388471
def test_apply_rgb_buffer(self):
389472
if not np:
390473
logger.warning("NumPy not found. Skipping test!")

0 commit comments

Comments
 (0)