Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Correct numpy dtype comparisons in image_resample #28458

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

Merged
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
17 changes: 17 additions & 0 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1576,3 +1576,20 @@ def test_non_transdata_image_does_not_touch_aspect():
assert ax.get_aspect() == 1
ax.imshow(im, transform=ax.transAxes, aspect=2)
assert ax.get_aspect() == 2


@pytest.mark.parametrize(
'dtype',
('float64', 'float32', 'int16', 'uint16', 'int8', 'uint8'),
)
@pytest.mark.parametrize('ndim', (2, 3))
def test_resample_dtypes(dtype, ndim):
# Issue 28448, incorrect dtype comparisons in C++ image_resample can raise
# ValueError: arrays must be of dtype byte, short, float32 or float64
rng = np.random.default_rng(4181)
shape = (2, 2) if ndim == 2 else (2, 2, 3)
data = rng.uniform(size=shape).astype(np.dtype(dtype, copy=True))
fig, ax = plt.subplots()
axes_image = ax.imshow(data)
# Before fix the following raises ValueError for some dtypes.
axes_image.make_image(None)[0]
24 changes: 12 additions & 12 deletions src/_image_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,20 @@ image_resample(py::array input_array,

if (auto resampler =
(ndim == 2) ? (
(dtype.is(py::dtype::of<std::uint8_t>())) ? resample<agg::gray8> :
(dtype.is(py::dtype::of<std::int8_t>())) ? resample<agg::gray8> :
(dtype.is(py::dtype::of<std::uint16_t>())) ? resample<agg::gray16> :
(dtype.is(py::dtype::of<std::int16_t>())) ? resample<agg::gray16> :
(dtype.is(py::dtype::of<float>())) ? resample<agg::gray32> :
(dtype.is(py::dtype::of<double>())) ? resample<agg::gray64> :
(dtype.equal(py::dtype::of<std::uint8_t>())) ? resample<agg::gray8> :
(dtype.equal(py::dtype::of<std::int8_t>())) ? resample<agg::gray8> :
(dtype.equal(py::dtype::of<std::uint16_t>())) ? resample<agg::gray16> :
(dtype.equal(py::dtype::of<std::int16_t>())) ? resample<agg::gray16> :
(dtype.equal(py::dtype::of<float>())) ? resample<agg::gray32> :
(dtype.equal(py::dtype::of<double>())) ? resample<agg::gray64> :
nullptr) : (
// ndim == 3
(dtype.is(py::dtype::of<std::uint8_t>())) ? resample<agg::rgba8> :
(dtype.is(py::dtype::of<std::int8_t>())) ? resample<agg::rgba8> :
(dtype.is(py::dtype::of<std::uint16_t>())) ? resample<agg::rgba16> :
(dtype.is(py::dtype::of<std::int16_t>())) ? resample<agg::rgba16> :
(dtype.is(py::dtype::of<float>())) ? resample<agg::rgba32> :
(dtype.is(py::dtype::of<double>())) ? resample<agg::rgba64> :
(dtype.equal(py::dtype::of<std::uint8_t>())) ? resample<agg::rgba8> :
(dtype.equal(py::dtype::of<std::int8_t>())) ? resample<agg::rgba8> :
(dtype.equal(py::dtype::of<std::uint16_t>())) ? resample<agg::rgba16> :
(dtype.equal(py::dtype::of<std::int16_t>())) ? resample<agg::rgba16> :
(dtype.equal(py::dtype::of<float>())) ? resample<agg::rgba32> :
(dtype.equal(py::dtype::of<double>())) ? resample<agg::rgba64> :
nullptr)) {
Py_BEGIN_ALLOW_THREADS
resampler(
Expand Down
Loading