Skip to content

Commit c79b59e

Browse files
authored
Merge pull request #32275 from zekebarger/speed-up-premultiplied-alpha-resampling
Speed up RGBA-stage image resampling
2 parents 374b2da + cd98107 commit c79b59e

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

‎lib/matplotlib/image.py‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,6 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
461461
if not (A.ndim == 2 or A.ndim == 3 and A.shape[-1] in (3, 4)):
462462
raise ValueError(f"Invalid shape {A.shape} for image data")
463463

464-
float_rgba_in = A.ndim == 3 and A.shape[-1] == 4 and A.dtype.kind == 'f'
465-
466464
# if antialiased, this needs to change as window sizes
467465
# change:
468466
interpolation_stage = self._interpolation_stage
@@ -566,13 +564,22 @@ def get_scaled_dtype(A):
566564
# Resample in premultiplied alpha space. (TODO: Consider
567565
# implementing premultiplied-space resampling in
568566
# span_image_resample_rgba_affine::generate?)
569-
if float_rgba_in and np.ndim(alpha) == 0 and np.any(A[..., 3] < 1):
570-
# Do not modify original RGBA input
571-
A = A.copy()
572-
A[..., :3] *= A[..., 3:]
567+
# Multiplying the whole array and then restoring the alpha channel
568+
# is faster than an in-place multiply of the strided A[..., :3]
569+
# view. If alpha is uniformly 1, premultiplication can be skipped.
570+
alpha_in = A[..., 3]
571+
if (alpha_in != 1).any():
572+
A = A * alpha_in[..., None]
573+
A[..., 3] = alpha_in
573574
res = _resample(self, A, out_shape, t)
574-
np.divide(res[..., :3], res[..., 3:], out=res[..., :3],
575-
where=res[..., 3:] != 0)
575+
# Demultiply. Zeroes in the divisor are replaced by ones,
576+
# which leaves the corresponding (premultiplied) RGB values
577+
# untouched. Dividing the whole contiguous array is several
578+
# times faster than np.divide(..., where=...) into the strided
579+
# res[..., :3] view.
580+
alpha_out = res[..., 3].copy()
581+
res /= np.where(alpha_out != 0, alpha_out, 1)[..., None]
582+
res[..., 3] = alpha_out
576583
if post_apply_alpha:
577584
res[..., 3] *= alpha
578585

‎lib/matplotlib/tests/test_image.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,26 @@ def test_resample_dtypes(dtype, ndim):
18601860
axes_image.make_image(None)[0]
18611861

18621862

1863+
@pytest.mark.parametrize('alpha', [0, 0.25, 0.5, 0.75, 1])
1864+
def test_rgba_stage_demultiplies_resampled_alpha(alpha):
1865+
# Issue 32240: resampling in the 'rgba' stage happens in premultiplied alpha
1866+
# space, so the RGB channels are demultiplied afterwards. The original
1867+
# colors must come back independently of the alpha channel, and a zero
1868+
# resampled alpha must not be divided by.
1869+
rgb = [0.2, 0.4, 0.6]
1870+
data = np.empty((4, 4, 4))
1871+
data[..., :3] = rgb
1872+
data[..., 3] = alpha
1873+
fig, ax = plt.subplots()
1874+
axes_image = ax.imshow(data, interpolation='nearest', interpolation_stage='rgba')
1875+
image = axes_image.make_image(None)[0]
1876+
# A fully transparent image premultiplies to zero and is never divided back.
1877+
expected_rgb = np.multiply(rgb, 255) if alpha else [0, 0, 0]
1878+
# The rendered image is a single flat colour, so compare against it directly.
1879+
assert_array_equal(np.unique(image.reshape(-1, 4), axis=0),
1880+
[[*expected_rgb, int(alpha * 255)]])
1881+
1882+
18631883
@pytest.mark.parametrize('intp_stage', ('data', 'rgba'))
18641884
@check_figures_equal(extensions=['png', 'pdf', 'svg'])
18651885
def test_interpolation_stage_rgba_respects_alpha_param(fig_test, fig_ref, intp_stage):

0 commit comments

Comments
 (0)