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

halide.imageio needs to support arbitrary bufferviews #7137

Merged
merged 3 commits into from
Oct 31, 2022
Merged
Changes from 1 commit
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
37 changes: 20 additions & 17 deletions python_bindings/src/halide/imageio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,58 @@
import imageio.v2 as imageio
except:
import imageio
import numpy
import numpy as np


def is_interleaved(im):
"""If the given ndarray is 3-dimensional and appears to have an interleaved
"""If the given buffer is 3-dimensional and appears to have an interleaved
layout, return True. Otherwise, return False."""

# Assume that 'interleaved' will only apply to channels <= 4
return im.ndim == 3 and im.strides[2] == 1 and im.shape[2] in [1, 2, 3, 4]
mv = memoryview(im)
return mv.ndim == 3 and mv.strides[2] == 1 and mv.shape[2] in [1, 2, 3, 4]


def _as_interleaved(im):
"""If the given ndarray is 3-dimensional and appears to be planar layout,
"""If the given buffer is 3-dimensional and appears to be planar layout,
return a view that is in interleaved form, leaving the input unchanged.
Otherwise, return the image ndarray unchanged.
Otherwise, return the image buffer unchanged.
Note that this call must be used with care, as the returnee may or may
not be a copy."""
if im.ndim == 3 and not is_interleaved(im):
return numpy.moveaxis(im, 0, 2)
mv = memoryview(im)
if mv.ndim == 3 and not is_interleaved(mv):
return np.moveaxis(mv, 0, 2)
else:
return im
return mv


def _as_planar(im):
"""If the given ndarray is 3-dimensional and appears to be interleaved
"""If the given buffer is 3-dimensional and appears to be interleaved
layout, return a view that is in planar form, leaving the input
unchanged. Otherwise, return the image ndarray unchanged.
unchanged. Otherwise, return the image buffer unchanged.
Note that this call must be used with care, as the returnee may or may
not be a copy."""
if is_interleaved(im):
return numpy.moveaxis(im, 2, 0)
mv = memoryview(im)
if is_interleaved(mv):
return np.moveaxis(mv, 2, 0)
else:
return im
return mv


def copy_to_interleaved(im):
"""If the given ndarray is 3-dimensional and appears to be planar
"""If the given buffer is 3-dimensional and appears to be planar
layout, return a copy that is in interleaved form. Otherwise, return
an unchanged copy of the input. Note that this call will always return
a copy, leaving the input unchanged."""
return _as_interleaved(im).copy()
return np.copy(_as_interleaved(im))


def copy_to_planar(im):
"""If the given ndarray is 3-dimensional and appears to be interleaved
"""If the given buffer is 3-dimensional and appears to be interleaved
layout, return a copy that is in planar form. Otherwise, return
an unchanged copy of the input. Note that this call will always return
a copy, leaving the input unchanged."""
return _as_planar(im).copy()
return np.copy(_as_planar(im))


def imread(uri, format=None, **kwargs):
Expand Down