Skip to content

Commit 47f776c

Browse files
committed
Split PyUtils into separate Utils and bufferUtils header to reduce exposure to pybind11/numpy.h
Signed-off-by: Kevin Wheatley <kevin.wheatley@framestore.com>
1 parent 9d03b2e commit 47f776c

File tree

9 files changed

+66
-40
lines changed

9 files changed

+66
-40
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
// Copyright Contributors to the OpenColorIO Project.
3+
4+
#ifndef INCLUDED_OCIO_PYBUFFERUTILS_H
5+
#define INCLUDED_OCIO_PYBUFFERUTILS_H
6+
7+
#include <string>
8+
9+
#include <OpenColorABI.h>
10+
#include <OpenColorIO/OpenColorTypes.h>
11+
12+
#include <pybind11/pybind11.h>
13+
#include <pybind11/numpy.h>
14+
15+
namespace OCIO_NAMESPACE
16+
{
17+
18+
// Convert Python buffer protocol format code to NumPy dtype name
19+
std::string formatCodeToDtypeName(const std::string & format, pybind11::ssize_t numBits);
20+
// Convert OCIO BitDepth to NumPy dtype
21+
pybind11::dtype bitDepthToDtype(BitDepth bitDepth);
22+
// Convert OCIO BitDepth to data type byte count
23+
pybind11::ssize_t bitDepthToBytes(BitDepth bitDepth);
24+
// Convert OCIO ChannelOrdering to channel count
25+
long chanOrderToNumChannels(ChannelOrdering chanOrder);
26+
27+
// Return string that describes Python buffer's N-dimensional array shape
28+
std::string getBufferShapeStr(const pybind11::buffer_info & info);
29+
// Return BitDepth for a supported Python buffer data type
30+
BitDepth getBufferBitDepth(const pybind11::buffer_info & info);
31+
32+
// Throw if Python buffer format is incompatible with a NumPy dtype
33+
void checkBufferType(const pybind11::buffer_info & info, const pybind11::dtype & dt);
34+
// Throw if Python buffer format is incompatible with an OCIO BitDepth
35+
void checkBufferType(const pybind11::buffer_info & info, BitDepth bitDepth);
36+
// Throw if Python buffer size is not divisible by channel count
37+
void checkBufferDivisible(const pybind11::buffer_info & info, pybind11::ssize_t numChannels);
38+
// Throw if Python buffer does not have an exact count of entries
39+
void checkBufferSize(const pybind11::buffer_info & info, pybind11::ssize_t numEntries);
40+
41+
// Calculate 3D grid size from a packed 3D LUT buffer
42+
unsigned long getBufferLut3DGridSize(const pybind11::buffer_info & info);
43+
44+
// Throw if vector size is not divisible by channel count
45+
void checkVectorDivisible(const std::vector<float> & pixel, size_t numChannels);
46+
47+
// Throw if array is not C-contiguous
48+
void checkCContiguousArray(const pybind11::buffer_info & info);
49+
50+
} // namespace OCIO_NAMESPACE
51+
52+
#endif // INCLUDED_OCIO_PYBUFFERUTILS_H

src/bindings/python/PyCPUProcessor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
#include <pybind11/pybind11.h>
99
#include <pybind11/stl.h>
10+
#include <pybind11/numpy.h>
1011

1112
#include "PyDynamicProperty.h"
1213
#include "PyOpenColorIO.h"
13-
#include "PyUtils.h"
14+
#include "PyBufferUtils.h"
1415
#include "PyImageDesc.h"
1516
#include "docstrings.h"
1617

src/bindings/python/PyGpuShaderDesc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
#include <OpenColorIO/OpenColorIO.h>
77

88
#include <pybind11/pybind11.h>
9+
#include <pybind11/numpy.h>
910

1011
#include "PyOpenColorIO.h"
1112
#include "PyGpuShaderCreator.h"
1213
#include "PyUtils.h"
14+
#include "PyBufferUtils.h"
1315
#include "docstrings.h"
1416

1517

src/bindings/python/PyPackedImageDesc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
#include <pybind11/pybind11.h>
1010
#include <pybind11/numpy.h>
1111

12+
#include "PyOpenColorIO.h"
1213
#include "PyImageDesc.h"
13-
#include "PyUtils.h"
14+
#include "PyBufferUtils.h"
1415
#include "docstrings.h"
1516

1617

src/bindings/python/PyPlanarImageDesc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#include <OpenColorIO/OpenColorIO.h>
88

99
#include <pybind11/pybind11.h>
10+
#include <pybind11/numpy.h>
1011

12+
#include "PyOpenColorIO.h"
1113
#include "PyImageDesc.h"
12-
#include "PyUtils.h"
14+
#include "PyBufferUtils.h"
1315
#include "docstrings.h"
1416

1517

src/bindings/python/PyUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <pybind11/pybind11.h>
1515
#include <pybind11/numpy.h>
1616

17-
#include "PyUtils.h"
17+
#include "PyBufferUtils.h"
1818

1919

2020
namespace py = pybind11;

src/bindings/python/PyUtils.h

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
#ifndef INCLUDED_OCIO_PYUTILS_H
55
#define INCLUDED_OCIO_PYUTILS_H
66

7-
#include <string>
87
#include <tuple>
9-
#include <vector>
108
#include <sstream>
119

1210
#include "OpenColorABI.h"
1311

1412
#include <pybind11/pybind11.h>
15-
#include <pybind11/numpy.h>
16-
17-
#include "PyOpenColorIO.h"
1813

1914

2015
namespace OCIO_NAMESPACE
@@ -67,37 +62,6 @@ struct PyIterator
6762
int m_i = 0;
6863
};
6964

70-
// Convert Python buffer protocol format code to NumPy dtype name
71-
std::string formatCodeToDtypeName(const std::string & format, pybind11::ssize_t numBits);
72-
// Convert OCIO BitDepth to NumPy dtype
73-
pybind11::dtype bitDepthToDtype(BitDepth bitDepth);
74-
// Convert OCIO BitDepth to data type byte count
75-
pybind11::ssize_t bitDepthToBytes(BitDepth bitDepth);
76-
// Convert OCIO ChannelOrdering to channel count
77-
long chanOrderToNumChannels(ChannelOrdering chanOrder);
78-
79-
// Return string that describes Python buffer's N-dimensional array shape
80-
std::string getBufferShapeStr(const pybind11::buffer_info & info);
81-
// Return BitDepth for a supported Python buffer data type
82-
BitDepth getBufferBitDepth(const pybind11::buffer_info & info);
83-
84-
// Throw if Python buffer format is incompatible with a NumPy dtype
85-
void checkBufferType(const pybind11::buffer_info & info, const pybind11::dtype & dt);
86-
// Throw if Python buffer format is incompatible with an OCIO BitDepth
87-
void checkBufferType(const pybind11::buffer_info & info, BitDepth bitDepth);
88-
// Throw if Python buffer size is not divisible by channel count
89-
void checkBufferDivisible(const pybind11::buffer_info & info, pybind11::ssize_t numChannels);
90-
// Throw if Python buffer does not have an exact count of entries
91-
void checkBufferSize(const pybind11::buffer_info & info, pybind11::ssize_t numEntries);
92-
93-
// Calculate 3D grid size from a packed 3D LUT buffer
94-
unsigned long getBufferLut3DGridSize(const pybind11::buffer_info & info);
95-
96-
// Throw if vector size is not divisible by channel count
97-
void checkVectorDivisible(const std::vector<float> & pixel, size_t numChannels);
98-
99-
// Throw if array is not C-contiguous
100-
void checkCContiguousArray(const pybind11::buffer_info & info);
10165

10266
} // namespace OCIO_NAMESPACE
10367

src/bindings/python/transforms/PyLut1DTransform.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#include <pybind11/pybind11.h>
99
#include <pybind11/numpy.h>
1010

11+
#include "PyOpenColorIO.h"
1112
#include "PyTransform.h"
1213
#include "PyUtils.h"
14+
#include "PyBufferUtils.h"
1315
#include "docstrings.h"
1416

1517

src/bindings/python/transforms/PyLut3DTransform.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#include <pybind11/pybind11.h>
99
#include <pybind11/numpy.h>
1010

11+
#include "PyOpenColorIO.h"
1112
#include "PyTransform.h"
1213
#include "PyUtils.h"
14+
#include "PyBufferUtils.h"
1315
#include "docstrings.h"
1416

1517

0 commit comments

Comments
 (0)