From d05b6a0c662d89de86e1fcdbf251d3ba79ff634c Mon Sep 17 00:00:00 2001 From: "Brett M. Morris" Date: Wed, 6 Dec 2023 15:18:54 -0500 Subject: [PATCH 1/3] preserve alpha compositing with cmap.set_bad --- glue/viewers/image/composite_array.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/glue/viewers/image/composite_array.py b/glue/viewers/image/composite_array.py index b24b7117f..646ca36de 100644 --- a/glue/viewers/image/composite_array.py +++ b/glue/viewers/image/composite_array.py @@ -146,7 +146,9 @@ def __call__(self, bounds=None): # 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] + bad_rgba = layer['cmap'].get_bad().tolist() + bad_color = bad_rgba[:3] + bad_alpha = bad_rgba[3] layer_cmap = layer['cmap'].with_extremes( bad=bad_color + [layer['alpha']] ) @@ -162,8 +164,7 @@ def __call__(self, bounds=None): # 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 bad_alpha == 1: if layer['alpha'] == 1: img[...] = 0 else: @@ -175,6 +176,9 @@ def __call__(self, bounds=None): # Use traditional alpha compositing alpha_plane = layer['alpha'] * plane[:, :, 3] + # ensure "bad" alpha is preserved: + alpha_plane[~np.isfinite(data)] *= bad_alpha + plane[:, :, 0] = plane[:, :, 0] * alpha_plane plane[:, :, 1] = plane[:, :, 1] * alpha_plane plane[:, :, 2] = plane[:, :, 2] * alpha_plane From 7a474b2576f0ba10113ebc48650509cd052cbbdb Mon Sep 17 00:00:00 2001 From: "Brett M. Morris" Date: Tue, 12 Dec 2023 17:00:53 -0500 Subject: [PATCH 2/3] make the bad alpha feature opt-in --- glue/viewers/image/composite_array.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/glue/viewers/image/composite_array.py b/glue/viewers/image/composite_array.py index 646ca36de..1fd806a31 100644 --- a/glue/viewers/image/composite_array.py +++ b/glue/viewers/image/composite_array.py @@ -29,6 +29,7 @@ def __init__(self, **kwargs): self._first = True self._mode = 'color' + self._allow_bad_alpha = False @property def mode(self): @@ -148,10 +149,14 @@ def __call__(self, bounds=None): if hasattr(layer['cmap'], 'get_bad'): bad_rgba = layer['cmap'].get_bad().tolist() bad_color = bad_rgba[:3] - bad_alpha = bad_rgba[3] - layer_cmap = layer['cmap'].with_extremes( - bad=bad_color + [layer['alpha']] - ) + bad_alpha = bad_rgba[3:] + + if self._allow_bad_alpha: + bad_rgba = bad_color + bad_alpha + else: + bad_rgba = bad_color + [layer['alpha']] + + layer_cmap = layer['cmap'].with_extremes(bad=bad_rgba) else: layer_cmap = layer['cmap'] @@ -176,8 +181,9 @@ def __call__(self, bounds=None): # Use traditional alpha compositing alpha_plane = layer['alpha'] * plane[:, :, 3] - # ensure "bad" alpha is preserved: - alpha_plane[~np.isfinite(data)] *= bad_alpha + if self._allow_bad_alpha: + # ensure "bad" alpha is preserved: + alpha_plane[~np.isfinite(data)] *= bad_alpha plane[:, :, 0] = plane[:, :, 0] * alpha_plane plane[:, :, 1] = plane[:, :, 1] * alpha_plane From 822a9dcdea6f5895e66e3f7bb5b47f24c3c01c33 Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Wed, 21 Feb 2024 15:09:40 +0000 Subject: [PATCH 3/3] Fixed if statement logic --- glue/viewers/image/composite_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/glue/viewers/image/composite_array.py b/glue/viewers/image/composite_array.py index 1fd806a31..4ef60c7e0 100644 --- a/glue/viewers/image/composite_array.py +++ b/glue/viewers/image/composite_array.py @@ -169,7 +169,7 @@ def __call__(self, bounds=None): # 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 and bad_alpha == 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: