Skip to content

Commit 1cfb236

Browse files
committed
remove 1d arrays from the test, since even after Transpose, 1d arrays are still contiguous
Signed-off-by: Afsaneh Sheikhmiri <[email protected]>
1 parent f56ef65 commit 1cfb236

File tree

4 files changed

+83
-63
lines changed

4 files changed

+83
-63
lines changed

src/bindings/python/PyCPUProcessor.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,12 @@ written to the dstImgDesc image, leaving srcImgDesc unchanged.
9292
9393
)doc")
9494
.def("applyRGB", [](CPUProcessorRcPtr & self, py::buffer & data)
95-
{
95+
{
9696
py::buffer_info info = data.request();
9797
checkBufferDivisible(info, 3);
9898

9999
// --- detect C-contiguous ---
100-
bool isC = true;
101-
ptrdiff_t itemsize = info.itemsize;
102-
auto shape = info.shape;
103-
auto strides = info.strides;
104-
py::ssize_t ndim = info.ndim;
105-
106-
ptrdiff_t expected = itemsize;
107-
for (py::ssize_t i = ndim - 1; i >= 0; --i)
108-
{
109-
if (strides[i] != expected) { isC = false; break; }
110-
expected *= shape[i];
111-
}
112-
113-
if (!isC)
114-
{
115-
throw std::runtime_error("applyRGB only supports C-contiguous (row-major) arrays");
116-
}
100+
checkCContiguousArray(info);
117101

118102
// --- proceed normally ---
119103
BitDepth bitDepth = getBufferBitDepth(info);
@@ -136,7 +120,7 @@ written to the dstImgDesc image, leaving srcImgDesc unchanged.
136120
yStrideBytes);
137121

138122
self->apply(img);
139-
},
123+
},
140124
"data"_a,
141125
R"doc(
142126
Apply to a packed RGB array adhering to the Python buffer protocol.
@@ -191,6 +175,8 @@ float values is returned, leaving the input list unchanged.
191175
py::buffer_info info = data.request();
192176
checkBufferDivisible(info, 4);
193177

178+
// --- detect C-contiguous ---
179+
checkCContiguousArray(info);
194180
// Interpret as single row of RGBA pixels
195181
BitDepth bitDepth = getBufferBitDepth(info);
196182

src/bindings/python/PyUtils.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,27 @@ void checkBufferType(const py::buffer_info & info, BitDepth bitDepth)
179179
checkBufferType(info, bitDepthToDtype(bitDepth));
180180
}
181181

182+
void checkCContiguousArray(const py::buffer_info & info)
183+
{
184+
bool isC = true;
185+
ptrdiff_t itemsize = info.itemsize;
186+
auto shape = info.shape;
187+
auto strides = info.strides;
188+
py::ssize_t ndim = info.ndim;
189+
190+
ptrdiff_t expected = itemsize;
191+
for (py::ssize_t i = ndim - 1; i >= 0; --i)
192+
{
193+
if (strides[i] != expected) { isC = false; break; }
194+
expected *= shape[i];
195+
}
196+
197+
if (!isC)
198+
{
199+
throw std::runtime_error("function only supports C-contiguous (row-major) arrays");
200+
}
201+
}
202+
182203
void checkBufferDivisible(const py::buffer_info & info, py::ssize_t numChannels)
183204
{
184205
if (info.size % numChannels != 0)

src/bindings/python/PyUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ unsigned long getBufferLut3DGridSize(const py::buffer_info & info);
8989
// Throw if vector size is not divisible by channel count
9090
void checkVectorDivisible(const std::vector<float> & pixel, size_t numChannels);
9191

92+
// Throw if array is not C-contiguous
93+
void checkCContiguousArray(const py::buffer_info & info);
94+
9295
} // namespace OCIO_NAMESPACE
9396

9497
#endif // INCLUDED_OCIO_PYUTILS_H

tests/python/CPUProcessorTest.py

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -390,15 +390,15 @@ def test_apply_rgb_buffer_column_major(self):
390390
logger.warning("NumPy not found. Skipping test!")
391391
return
392392

393-
for arr, cpu_proc_fwd, cpu_proc_inv in [
394-
(self.float_rgb_2d, self.default_cpu_proc_fwd, self.default_cpu_proc_inv),
395-
(self.float_rgb_3d, self.default_cpu_proc_fwd, self.default_cpu_proc_inv),
396-
(self.half_rgb_2d, self.half_cpu_proc_fwd, self.half_cpu_proc_inv),
397-
(self.half_rgb_3d, self.half_cpu_proc_fwd, self.half_cpu_proc_inv),
398-
(self.uint16_rgb_2d, self.uint16_cpu_proc_fwd, self.uint16_cpu_proc_inv),
399-
(self.uint16_rgb_3d, self.uint16_cpu_proc_fwd, self.uint16_cpu_proc_inv),
400-
(self.uint8_rgb_2d, self.uint8_cpu_proc_fwd, self.uint8_cpu_proc_inv),
401-
(self.uint8_rgb_3d, self.uint8_cpu_proc_fwd, self.uint8_cpu_proc_inv),
393+
for arr, cpu_proc_fwd in [
394+
(self.float_rgb_2d, self.default_cpu_proc_fwd),
395+
(self.float_rgb_3d, self.default_cpu_proc_fwd),
396+
(self.half_rgb_2d, self.half_cpu_proc_fwd),
397+
(self.half_rgb_3d, self.half_cpu_proc_fwd),
398+
(self.uint16_rgb_2d, self.uint16_cpu_proc_fwd),
399+
(self.uint16_rgb_3d, self.uint16_cpu_proc_fwd),
400+
(self.uint8_rgb_2d, self.uint8_cpu_proc_fwd),
401+
(self.uint8_rgb_3d, self.uint8_cpu_proc_fwd),
402402
]:
403403
# Transpose to F-order (column-major)
404404
arr_copy = arr.copy().T
@@ -407,41 +407,6 @@ def test_apply_rgb_buffer_column_major(self):
407407
with self.assertRaises(RuntimeError):
408408
cpu_proc_fwd.applyRGB(arr_copy)
409409

410-
# Convert back to C-order and retry
411-
arr_copy_c = np.ascontiguousarray(arr_copy)
412-
cpu_proc_fwd.applyRGB(arr_copy_c)
413-
414-
# Check forward transform
415-
for i in range(arr_copy_c.size):
416-
if arr.dtype in (np.float32, np.float16):
417-
self.assertAlmostEqual(
418-
arr_copy_c.flat[i],
419-
arr.flat[i] * 0.5,
420-
delta=self.FLOAT_DELTA
421-
)
422-
else:
423-
self.assertAlmostEqual(
424-
arr_copy_c.flat[i],
425-
arr.flat[i] // 2,
426-
delta=self.UINT_DELTA
427-
)
428-
429-
# Inverse transform
430-
cpu_proc_inv.applyRGB(arr_copy_c)
431-
for i in range(arr_copy_c.size):
432-
if arr.dtype in (np.float32, np.float16):
433-
self.assertAlmostEqual(
434-
arr_copy_c.flat[i],
435-
arr.flat[i],
436-
delta=self.FLOAT_DELTA
437-
)
438-
else:
439-
self.assertAlmostEqual(
440-
arr_copy_c.flat[i],
441-
arr.flat[i],
442-
delta=self.UINT_DELTA
443-
)
444-
445410
def test_apply_rgb_buffer(self):
446411
if not np:
447412
logger.warning("NumPy not found. Skipping test!")
@@ -684,3 +649,48 @@ def test_apply_rgba_buffer(self):
684649
arr.flat[i],
685650
delta=self.UINT_DELTA
686651
)
652+
653+
def test_apply_rgba_buffer_column_major(self):
654+
if not np:
655+
logger.warning("NumPy not found. Skipping test!")
656+
return
657+
658+
for arr, cpu_proc_fwd in [
659+
(
660+
self.float_rgba_2d,
661+
self.default_cpu_proc_fwd
662+
),
663+
(
664+
self.float_rgba_3d,
665+
self.default_cpu_proc_fwd
666+
),
667+
(
668+
self.half_rgba_2d,
669+
self.half_cpu_proc_fwd
670+
),
671+
(
672+
self.half_rgba_3d,
673+
self.half_cpu_proc_fwd
674+
),
675+
(
676+
self.uint16_rgba_2d,
677+
self.uint16_cpu_proc_fwd
678+
),
679+
(
680+
self.uint16_rgba_3d,
681+
self.uint16_cpu_proc_fwd
682+
),
683+
(
684+
self.uint8_rgba_2d,
685+
self.uint8_cpu_proc_fwd
686+
),
687+
(
688+
self.uint8_rgba_3d,
689+
self.uint8_cpu_proc_fwd,
690+
),
691+
]:
692+
# Transpose to F-order (column-major)
693+
arr_copy = arr.copy().T
694+
# Expect runtime error for non-C-contiguous array
695+
with self.assertRaises(RuntimeError):
696+
cpu_proc_fwd.applyRGBA(arr_copy)

0 commit comments

Comments
 (0)