From 76893178773ed2de54a4fa068e044d00184ca6f9 Mon Sep 17 00:00:00 2001 From: Cary Phillips Date: Sat, 25 May 2024 10:20:00 -0700 Subject: [PATCH] arrayobject Signed-off-by: Cary Phillips --- src/wrappers/python/CMakeLists.txt | 3 +- src/wrappers/python/PyOpenEXR.cpp | 92 +++++++++++++++++++++++--- src/wrappers/python/tests/test_deep.py | 2 +- 3 files changed, 86 insertions(+), 11 deletions(-) diff --git a/src/wrappers/python/CMakeLists.txt b/src/wrappers/python/CMakeLists.txt index 0d2926320b..1c9edb05bd 100644 --- a/src/wrappers/python/CMakeLists.txt +++ b/src/wrappers/python/CMakeLists.txt @@ -9,12 +9,13 @@ if(NOT "${CMAKE_PROJECT_NAME}" STREQUAL "OpenEXR") find_package(OpenEXR) endif() -find_package(Python COMPONENTS Interpreter Development.Module REQUIRED) +find_package(Python COMPONENTS Interpreter Development.Module NumPy REQUIRED) find_package(pybind11 CONFIG REQUIRED) python_add_library (PyOpenEXR MODULE PyOpenEXR.cpp PyOpenEXR_old.cpp) target_link_libraries (PyOpenEXR PRIVATE "${Python_LIBRARIES}" OpenEXR::OpenEXR pybind11::headers) +include_directories(${Python_NumPy_INCLUDE_DIRS}) # The python module should be called "OpenEXR.so", not "PyOpenEXR.so", # but "OpenEXR" is taken as a library name by the main lib, so specify diff --git a/src/wrappers/python/PyOpenEXR.cpp b/src/wrappers/python/PyOpenEXR.cpp index 88de3c4035..995a8bd4d1 100644 --- a/src/wrappers/python/PyOpenEXR.cpp +++ b/src/wrappers/python/PyOpenEXR.cpp @@ -15,6 +15,10 @@ #include #include +#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#include +#include + #include "openexr.h" #include @@ -369,11 +373,13 @@ PyPart::readDeepPixels(MultiPartInputFile& infile, const std::string& type, cons for (auto c = channel_list.begin(); c != channel_list.end(); c++) { std::string py_channel_name = c.name(); +#if XXX char channel_name; int nrgba = 0; if (rgba) nrgba = rgba_channel(channel_list, c.name(), py_channel_name, channel_name); - +#endif + auto py_channel_name_str = py::str(py_channel_name); if (!channels.contains(py_channel_name_str)) @@ -388,7 +394,7 @@ PyPart::readDeepPixels(MultiPartInputFile& infile, const std::string& type, cons C.pLinear = c.channel().pLinear; C._type = c.channel().type; - C.deep_samples = new Array2D(width, height); + C.deep_samples = new Array2D(height, width); channels[py_channel_name.c_str()] = C; @@ -491,21 +497,21 @@ PyPart::readDeepPixels(MultiPartInputFile& infile, const std::string& type, cons switch (C._type) { case UINT: - for (int i=0; i(S[y][x]); s[i] = 0; } break; case HALF: - for (int i=0; i(S[y][x]); s[i] = 0; } break; case FLOAT: - for (int i=0; i(S[y][x]); s[i] = 0; @@ -532,21 +538,21 @@ PyPart::readDeepPixels(MultiPartInputFile& infile, const std::string& type, cons switch (C._type) { case UINT: - for (int i=0; i(S[y][x]); std::cout << "sample: " << C.name << "[" << y << "][" << x << "][" << i << "]=" << s[i] << std::endl; } break; case HALF: - for (int i=0; i(S[y][x]); std::cout << "sample: " << C.name << "[" << y << "][" << x << "][" << i << "]=" << s[i] << std::endl; } break; case FLOAT: - for (int i=0; i(S[y][x]); std::cout << "sample: " << C.name << "[" << y << "][" << x << "][" << i << "]=" << s[i] << std::endl; @@ -1598,7 +1604,7 @@ PyChannel::diff(const PyChannel& other) const return ss.str(); } - for (size_t i=0; i(py_obj); +} + +#if XXX +int main(int argc, char *argv[]) { + // Initialize the Python interpreter + Py_Initialize(); + import_array(); // Initialize NumPy + + // Create the 2D array of arrays + PyObject *array_of_arrays = create_2d_array_of_arrays(); + + // Print the result (optional, for verification) + PyObject_Print(array_of_arrays, stdout, 0); + printf("\n"); + + // Clean up and exit + Py_DECREF(array_of_arrays); + Py_Finalize(); + return 0; +} +#endif + #define DEEP_EXAMPLE 1 #if DEEP_EXAMPLE @@ -2069,6 +2135,7 @@ PYBIND11_MODULE(OpenEXR, m) .def("__repr__", [](const PyChannel& c) { return repr(c); }) .def(py::self == py::self) .def(py::self != py::self) + .def("diff", &PyChannel::diff) .def_readwrite("name", &PyChannel::name) .def("type", &PyChannel::pixelType) .def_readwrite("xSampling", &PyChannel::xSampling) @@ -2086,6 +2153,8 @@ PYBIND11_MODULE(OpenEXR, m) py::arg("name")="") .def("__repr__", [](const PyPart& p) { return repr(p); }) .def(py::self == py::self) + .def(py::self != py::self) + .def("diff", &PyPart::diff) .def("name", &PyPart::name) .def("type", &PyPart::type) .def("width", &PyPart::width) @@ -2110,6 +2179,8 @@ PYBIND11_MODULE(OpenEXR, m) .def("__enter__", &PyFile::__enter__) .def("__exit__", &PyFile::__exit__) .def(py::self == py::self) + .def(py::self != py::self) + .def("diff", &PyFile::diff) .def_readwrite("filename", &PyFile::filename) .def_readwrite("parts", &PyFile::parts) .def("header", &PyFile::header, py::arg("part_index") = 0) @@ -2118,6 +2189,9 @@ PYBIND11_MODULE(OpenEXR, m) ; #if DEEP_EXAMPLE +// import_array(); + + m.def("create_2d_array_of_arrays", &py_create_2d_array_of_arrays); m.def("writeDeepExample", &writeDeepExample); m.def("readDeepExample", &readDeepExample); #endif diff --git a/src/wrappers/python/tests/test_deep.py b/src/wrappers/python/tests/test_deep.py index 27f63a5736..1d928cb6c4 100644 --- a/src/wrappers/python/tests/test_deep.py +++ b/src/wrappers/python/tests/test_deep.py @@ -21,7 +21,7 @@ def test_read_deep(self): # OpenEXR.writeDeepExample() # OpenEXR.readDeepExample() -# f = OpenEXR.File("test.deep.exr") + f = OpenEXR.File("test.deep.exr") print("ok") if __name__ == '__main__':