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

Preserve alpha while compositing with cmap.set_bad #2468

Merged
merged 3 commits into from
Feb 28, 2024
Merged
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
22 changes: 16 additions & 6 deletions glue/viewers/image/composite_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

self._first = True
self._mode = 'color'
self._allow_bad_alpha = False

@property
def mode(self):
Expand Down Expand Up @@ -146,10 +147,16 @@
# ensure "bad" values have the same alpha as the
# rest of the layer:
if hasattr(layer['cmap'], 'get_bad'):
bad_color = layer['cmap'].get_bad().tolist()[:3]
layer_cmap = layer['cmap'].with_extremes(
bad=bad_color + [layer['alpha']]
)
bad_rgba = layer['cmap'].get_bad().tolist()
bad_color = bad_rgba[:3]
bad_alpha = bad_rgba[3:]

if self._allow_bad_alpha:
bad_rgba = bad_color + bad_alpha

Check warning on line 155 in glue/viewers/image/composite_array.py

View check run for this annotation

Codecov / codecov/patch

glue/viewers/image/composite_array.py#L155

Added line #L155 was not covered by tests
else:
bad_rgba = bad_color + [layer['alpha']]

layer_cmap = layer['cmap'].with_extremes(bad=bad_rgba)
else:
layer_cmap = layer['cmap']

Expand All @@ -162,8 +169,7 @@
# Check what the smallest colormap alpha value for this layer is
# - if it is 1 then this colormap does not change transparency,
# and this allows us to speed things up a little.
if layer_cmap(CMAP_SAMPLING)[:, 3].min() == 1:

if layer_cmap(CMAP_SAMPLING)[:, 3].min() == 1 and (not self._allow_bad_alpha or bad_alpha == 1):
if layer['alpha'] == 1:
img[...] = 0
else:
Expand All @@ -175,6 +181,10 @@
# Use traditional alpha compositing
alpha_plane = layer['alpha'] * plane[:, :, 3]

if self._allow_bad_alpha:
# ensure "bad" alpha is preserved:
alpha_plane[~np.isfinite(data)] *= bad_alpha

Check warning on line 186 in glue/viewers/image/composite_array.py

View check run for this annotation

Codecov / codecov/patch

glue/viewers/image/composite_array.py#L186

Added line #L186 was not covered by tests

plane[:, :, 0] = plane[:, :, 0] * alpha_plane
plane[:, :, 1] = plane[:, :, 1] * alpha_plane
plane[:, :, 2] = plane[:, :, 2] * alpha_plane
Expand Down
Loading