Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite OpenEXR python bindings using pybind11 and numpy #1756

Merged
merged 13 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/python-wheels-publish-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ jobs:
env:
CIBW_ARCHS_LINUX: x86_64
CIBW_ARCHS_MACOS: x86_64 arm64 universal2
# Build Python 3.7 through 3.11.
# Skip python 3.6 since scikit-build-core requires 3.7+
# Skip 32-bit wheels builds on Windows
# Also skip the PyPy builds, since they fail the unit tests
CIBW_SKIP: cp36-* *-win32 *_i686 pp*
CIBW_BUILD: "cp37-* cp38-* cp39-* cp310-* cp311-*"
CIBW_SKIP: "*-win32 *_i686"
CIBW_TEST_SKIP: "*-macosx_universal2:arm64"
CIBW_ENVIRONMENT: OPENEXR_RELEASE_CANDIDATE_TAG="${{ github.ref_name }}"

Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/python-wheels-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ jobs:
with:
output-dir: wheelhouse
env:
CIBW_BUILD: cp312-*
CIBW_ARCHS_LINUX: x86_64
CIBW_ARCHS_MACOS: x86_64 arm64 universal2
# Build Python 3.7 through 3.11.
# Skip python 3.6 since scikit-build-core requires 3.7+
# Skip 32-bit wheels builds on Windows
# Also skip the PyPy builds, since they fail the unit tests
CIBW_SKIP: cp36-* *-win32 *_i686 pp*
CIBW_BUILD: "cp37-* cp38-* cp39-* cp310-* cp311-*"
CIBW_SKIP: "*-win32 *_i686"
CIBW_TEST_SKIP: "*arm64"

- name: Upload artifact
Expand Down
12 changes: 8 additions & 4 deletions .github/workflows/python-wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ on:
branches-ignore:
- RB-*
paths:
- 'src/lib/**'
- 'src/wrappers/python/**'
- 'pyproject.toml'
- '.github/workflows/python-wheels.yml'
pull_request:
branches-ignore:
- RB-*
paths:
- 'src/lib/**'
- 'src/wrappers/python/**'
- 'pyproject.toml'
- '.github/workflows/python-wheels.yml'
Expand All @@ -33,7 +35,7 @@ permissions:

jobs:
build_wheels:
name: Python Wheels - ${{ matrix.os }}
name: Python Wheels - ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
Expand All @@ -45,7 +47,7 @@ jobs:
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Install Python
uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
uses: actions/setup-python@39cd14951b08e74b54015e9e001cdefcf80e669f # v5.1.1
with:
python-version: '3.x'

Expand All @@ -58,11 +60,14 @@ jobs:
uses: pypa/cibuildwheel@7e5a838a63ac8128d71ab2dfd99e4634dd1bca09 # v2.19.2
env:
CIBW_ARCHS_MACOS: x86_64 arm64 universal2
# Build Python 3.7 through 3.11.
# Skip python 3.6 since scikit-build-core requires 3.7+
# Skip 32-bit wheels builds on Windows
# Also skip the PyPy builds, since they fail the unit tests
CIBW_SKIP: cp36-* *-win32 *_i686 pp*
CIBW_BUILD: "cp37-* cp38-* cp39-* cp310-* cp311-*"
CIBW_SKIP: "*-win32 *_i686"
CIBW_TEST_SKIP: "*-macosx*arm64"
OPENEXR_TEST_IMAGE_REPO: "https://github.com/raw/AcademySoftwareFoundation/openexr-images/main"

- name: Upload artifact
uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b # v4.3.4
Expand All @@ -71,4 +76,3 @@ jobs:
path: |
./wheelhouse/*.whl
./wheelhouse/*.tar.gz

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (c) Contributors to the OpenEXR Project.

[build-system]
requires = ["scikit-build-core==0.8.1"]
requires = ["scikit-build-core==0.8.1", "pybind11"]
build-backend = "scikit_build_core.build"

[project]
Expand Down Expand Up @@ -67,6 +67,7 @@ CMAKE_POSITION_INDEPENDENT_CODE = 'ON'

[tool.cibuildwheel]
test-command = "pytest -s {project}/src/wrappers/python/tests"
test-requires = ["numpy"]
test-extras = ["test"]
test-skip = ["*universal2:arm64"]
build-verbosity = 1
Expand Down
37 changes: 37 additions & 0 deletions share/ci/scripts/install_pybind11.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright Contributors to the OpenColorIO Project.

set -ex

PYBIND11_VERSION="$1"

if [[ $OSTYPE == "msys" ]]; then
SUDO=""
else
SUDO="sudo"
fi

git clone https://github.com/pybind/pybind11.git
cd pybind11

if [ "$PYBIND11_VERSION" == "latest" ]; then
LATEST_TAG=$(git describe --abbrev=0 --tags)
git checkout tags/${LATEST_TAG} -b ${LATEST_TAG}
else
git checkout tags/v${PYBIND11_VERSION} -b v${PYBIND11_VERSION}
fi

mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release \
-DPYBIND11_INSTALL=ON \
-DPYBIND11_TEST=OFF \
../.
$SUDO cmake --build . \
--target install \
--config Release \
--parallel 2

cd ../..
rm -rf pybind11
12 changes: 12 additions & 0 deletions src/lib/OpenEXR/ImfKeyCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ KeyCode::operator= (const KeyCode& other)
return *this;
}

bool
KeyCode::operator== (const KeyCode& other) const
{
return (_filmMfcCode == other._filmMfcCode &&
_filmType == other._filmType &&
_prefix == other._prefix &&
_count == other._count &&
_perfOffset == other._perfOffset &&
_perfsPerFrame == other._perfsPerFrame &&
_perfsPerCount == other._perfsPerCount);
}

int
KeyCode::filmMfcCode () const
{
Expand Down
2 changes: 2 additions & 0 deletions src/lib/OpenEXR/ImfKeyCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class IMF_EXPORT_TYPE KeyCode
IMF_EXPORT
KeyCode& operator= (const KeyCode& other);

bool operator== (const KeyCode& other) const;

//----------------------------
// Access to individual fields
//----------------------------
Expand Down
5 changes: 5 additions & 0 deletions src/lib/OpenEXR/ImfPreviewImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ struct IMF_EXPORT_TYPE PreviewRgba
unsigned char a = 255)
: r (r), g (g), b (b), a (a)
{}

bool operator==(const PreviewRgba& other) const
{
return r == other.r && g == other.g && b == other.b && a == other.a;
}
};

class IMF_EXPORT_TYPE PreviewImage
Expand Down
5 changes: 3 additions & 2 deletions src/wrappers/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ if(NOT "${CMAKE_PROJECT_NAME}" STREQUAL "OpenEXR")
endif()

find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
find_package(pybind11 CONFIG REQUIRED)

python_add_library (PyOpenEXR MODULE OpenEXR.cpp)
python_add_library (PyOpenEXR MODULE PyOpenEXR.cpp PyOpenEXR_old.cpp)

target_link_libraries (PyOpenEXR PRIVATE "${Python_LIBRARIES}" OpenEXR::OpenEXR)
target_link_libraries (PyOpenEXR PRIVATE "${Python_LIBRARIES}" OpenEXR::OpenEXR pybind11::headers)

# 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
Expand Down
115 changes: 15 additions & 100 deletions src/wrappers/python/Imath.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# Copyright (c) Contributors to the OpenEXR Project.

"""
:mod:`Imath` --- Support types for OpenEXR library
==================================================
:mod:`Imath` --- Deprecated component of OpenEXR
================================================
"""

class chromaticity(object):
"""Store chromaticity coordinates in *x* and *y*."""
"""This class is deprecated and will be removed in a future release"""
def __init__(self, x, y):
self.x = x
self.y = y
Expand All @@ -17,7 +17,7 @@ def __eq__(self, other):
return (self.x, self.y) == (other.x, other.y)

class point(object):
"""Point is a 2D point, with members *x* and *y*."""
"""This class is deprecated and will be removed in a future release"""
def __init__(self, x, y):
self.x = x;
self.y = y;
Expand All @@ -27,15 +27,15 @@ def __eq__(self, other):
return (self.x, self.y) == (other.x, other.y)

class V2i(point):
"""V2i is a 2D point, with members *x* and *y*."""
"""This class is deprecated and will be removed in a future release"""
pass

class V2f(point):
"""V2f is a 2D point, with members *x* and *y*."""
"""This class is deprecated and will be removed in a future release"""
pass

class Box:
"""Box is a 2D box, specified by its two corners *min* and *max*, both of which are :class:`point` """
"""This class is deprecated and will be removed in a future release"""
def __init__(self, min = None, max = None):
self.min = min
self.max = max
Expand All @@ -45,18 +45,15 @@ def __eq__(self, other):
return (self.min, self.max) == (other.min, other.max)

class Box2i(Box):
"""Box2i is a 2D box, specified by its two corners *min* and *max*."""
"""This class is deprecated and will be removed in a future release"""
pass

class Box2f(Box):
"""Box2f is a 2D box, specified by its two corners *min* and *max*."""
"""This class is deprecated and will be removed in a future release"""
pass

class Chromaticities:
"""
Chromaticities holds the set of chromaticity coordinates for *red*, *green*, *blue*, and *white*.
Each primary is a :class:`chromaticity`.
"""
"""This class is deprecated and will be removed in a future release"""
def __init__(self, red = None, green = None, blue = None, white = None):
self.red = red
self.green = green
Expand All @@ -79,47 +76,14 @@ def __eq__(self, other):
return self.v == other.v

class LineOrder(Enumerated):
"""
.. index:: INCREASING_Y, DECREASING_Y, RANDOM_Y

LineOrder can have three possible values:
``INCREASING_Y``,
``DECREASING_Y``,
``RANDOM_Y``.

.. doctest::

>>> import Imath
>>> print Imath.LineOrder(Imath.LineOrder.DECREASING_Y)
DECREASING_Y
"""
"""This class is deprecated and will be removed in a future release"""
INCREASING_Y = 0
DECREASING_Y = 1
RANDOM_Y = 2
names = ["INCREASING_Y", "DECREASING_Y", "RANDOM_Y"]

class Compression(Enumerated):
"""
.. index:: NO_COMPRESSION, RLE_COMPRESSION, ZIPS_COMPRESSION, ZIP_COMPRESSION, PIZ_COMPRESSION, PXR24_COMPRESSION, B44_COMPRESSION, B44A_COMPRESSION, DWAA_COMPRESSION, DWAB_COMPRESSION,

Compression can have possible values:
``NO_COMPRESSION``,
``RLE_COMPRESSION``,
``ZIPS_COMPRESSION``,
``ZIP_COMPRESSION``,
``PIZ_COMPRESSION``,
``PXR24_COMPRESSION``,
``B44_COMPRESSION``,
``B44A_COMPRESSION``,
``DWAA_COMPRESSION``,
``DWAB_COMPRESSION``.

.. doctest::

>>> import Imath
>>> print Imath.Compression(Imath.Compression.RLE_COMPRESSION)
RLE_COMPRESSION
"""
"""This class is deprecated and will be removed in a future release"""
NO_COMPRESSION = 0
RLE_COMPRESSION = 1
ZIPS_COMPRESSION = 2
Expand All @@ -136,40 +100,14 @@ class Compression(Enumerated):
]

class PixelType(Enumerated):
"""
.. index:: UINT, HALF, FLOAT

PixelType can have possible values ``UINT``, ``HALF``, ``FLOAT``.

.. doctest::

>>> import Imath
>>> print Imath.PixelType(Imath.PixelType.HALF)
HALF
"""
"""This class is deprecated and will be removed in a future release"""
UINT = 0
HALF = 1
FLOAT = 2
names = ["UINT", "HALF", "FLOAT"]

class Channel:
"""
Channel defines the type and spatial layout of a channel.
*type* is a :class:`PixelType`.
*xSampling* is the number of X-axis pixels between samples.
*ySampling* is the number of Y-axis pixels between samples.

.. doctest::

>>> import Imath
>>> print Imath.Channel(Imath.PixelType(Imath.PixelType.FLOAT), 4, 4)
FLOAT (4, 4)
>>> print Imath.Channel(Imath.PixelType.FLOAT, 4, 4)
Traceback (most recent call last):
...
TypeError: type needs to be a PixelType.
"""

"""This class is deprecated and will be removed in a future release"""
def __init__(self, type = PixelType(PixelType.HALF), xSampling = 1, ySampling = 1):
self.type = type
self.xSampling = xSampling
Expand Down Expand Up @@ -235,30 +173,7 @@ def __eq__(self, other):
return self.__dict__ == other.__dict__

class PreviewImage:
"""
.. index:: RGBA, thumbnail, preview, JPEG, PIL, Python Imaging Library

PreviewImage is a small preview image, intended as a thumbnail version of the full image.
The image has size (*width*, *height*) and 8-bit pixel values are
given by string *pixels* in RGBA order from top-left to bottom-right.

For example, to create a preview image from a JPEG file using the popular
`Python Imaging Library <http://www.pythonware.com/library/pil/handbook/index.htm>`_:

.. doctest::

>>> import Image
>>> import Imath
>>> im = Image.open("lena.jpg").resize((100, 100)).convert("RGBA")
>>> print Imath.PreviewImage(im.size[0], im.size[1], im.tostring())
<Imath.PreviewImage instance 100x100>
"""
def __init__(self, width, height, pixels):
self.width = width
self.height = height
self.pixels = pixels
def __repr__(self):
return "<Imath.PreviewImage instance %dx%d>" % (self.width, self.height)
"""This class is deprecated and will be removed in a future release"""

class LevelMode(Enumerated):
ONE_LEVEL = 0
Expand Down
Loading
Loading