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

Fix compositing multi source with alpha #1453

Merged
merged 1 commit into from
Jan 31, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
- Infer DICOM file size, when possible ([#1448](../../pull/1448))
- Swap styles faster in the frame viewer ([#1452](../../pull/1452))

### Bug Fixes
- Fix an issue with compositing sources in the multi source caused by alpha range ([#1453](../../pull/1453))

## 1.27.1

### Improvements
Expand Down
8 changes: 5 additions & 3 deletions large_image/tilesource/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Any, Dict, List, Optional, Set, Tuple, Union, cast

import numpy as np
import numpy.typing as npt
import PIL
import PIL.Image
import PIL.ImageColor
Expand Down Expand Up @@ -729,16 +730,17 @@ def getAvailableNamedPalettes(includeColors: bool = True, reduced: bool = False)
return sorted(palettes)


def fullAlphaValue(arr: np.ndarray) -> int:
def fullAlphaValue(arr: Union[np.ndarray, npt.DTypeLike]) -> int:
"""
Given a numpy array, return the value that should be used for a fully
opaque alpha channel. For uint variants, this is the max value.

:param arr: a numpy array.
:returns: the value for the alpha channel.
"""
if arr.dtype.kind == 'u':
return np.iinfo(arr.dtype).max
dtype = arr.dtype if isinstance(arr, np.ndarray) else arr
if dtype.kind == 'u':
return np.iinfo(dtype).max
return 1


Expand Down
21 changes: 10 additions & 11 deletions sources/multi/large_image_source_multi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,36 +931,34 @@
return tile
if base is None:
base = np.zeros((0, 0, tile.shape[2]), dtype=tile.dtype)
if tile.shape[2] in {2, 4}:
base[:, :, -1] = fullAlphaValue(tile)
base, tile = _makeSameChannelDepth(base, tile)
if base.shape[0] < tile.shape[0] + y:
vfill = np.zeros(
(tile.shape[0] + y - base.shape[0], base.shape[1], base.shape[2]),
dtype=base.dtype)
if base.shape[2] in {2, 4}:
vfill[:, :, -1] = fullAlphaValue(base)
vfill[:, :, -1] = fullAlphaValue(self.dtype)

Check warning on line 940 in sources/multi/large_image_source_multi/__init__.py

View check run for this annotation

Codecov / codecov/patch

sources/multi/large_image_source_multi/__init__.py#L940

Added line #L940 was not covered by tests
base = np.vstack((base, vfill))
if base.shape[1] < tile.shape[1] + x:
hfill = np.zeros(
(base.shape[0], tile.shape[1] + x - base.shape[1], base.shape[2]),
dtype=base.dtype)
if base.shape[2] in {2, 4}:
hfill[:, :, -1] = fullAlphaValue(base)
hfill[:, :, -1] = fullAlphaValue(self.dtype)

Check warning on line 947 in sources/multi/large_image_source_multi/__init__.py

View check run for this annotation

Codecov / codecov/patch

sources/multi/large_image_source_multi/__init__.py#L947

Added line #L947 was not covered by tests
base = np.hstack((base, hfill))
if base.flags.writeable is False:
base = base.copy()
if base.shape[2] in {2, 4}:
baseA = base[y:y + tile.shape[0], x:x + tile.shape[1], -1].astype(
float) / fullAlphaValue(base)
tileA = tile[:, :, -1].astype(float) / fullAlphaValue(tile)
float) / fullAlphaValue(self.dtype)
tileA = tile[:, :, -1].astype(float) / fullAlphaValue(self.dtype)
outA = tileA + baseA * (1 - tileA)
base[y:y + tile.shape[0], x:x + tile.shape[1], :-1] = (
tile[:, :, :-1] * tileA[..., np.newaxis] +
np.where(tileA[..., np.newaxis], tile[:, :, :-1], 0) +
base[y:y + tile.shape[0], x:x + tile.shape[1], :-1] * baseA[..., np.newaxis] *
(1 - tileA[..., np.newaxis])
) / outA[..., np.newaxis]
base[y:y + tile.shape[0], x:x + tile.shape[1], -1] = outA * fullAlphaValue(base)
) / np.where(outA[..., np.newaxis], outA[..., np.newaxis], 1)
base[y:y + tile.shape[0], x:x + tile.shape[1], -1] = outA * fullAlphaValue(self.dtype)
else:
base[y:y + tile.shape[0], x:x + tile.shape[1], :] = tile
return base
Expand Down Expand Up @@ -1030,8 +1028,9 @@
}
if output['maxWidth'] <= 0 or output['maxHeight'] <= 0:
return None, 0, 0
srcImage = ts.getRegion(
format=TILE_FORMAT_NUMPY, region=region, output=output, frame=frame)[0]
srcImage, _ = ts.getRegion(
region=region, output=output, frame=frame,
format=TILE_FORMAT_NUMPY)
# This is the region we actually took in our source coordinates, scaled
# for if we took a low res version
regioncorners = np.array([
Expand Down