makeRGBA/ImageItem: Applying alpha mask on numpy.nan data values (#406)

* Applying alpha mask on numpy.nan data values

* Typesafe, checking for `data.dtype.kind`
This commit is contained in:
Mi! 2019-09-27 22:31:47 +02:00 committed by Ogi Moore
parent 96a4270a30
commit 071e429535

View File

@ -1035,7 +1035,6 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False):
============== ================================================================================== ============== ==================================================================================
""" """
profile = debug.Profiler() profile = debug.Profiler()
if data.ndim not in (2, 3): if data.ndim not in (2, 3):
raise TypeError("data must be 2D or 3D") raise TypeError("data must be 2D or 3D")
if data.ndim == 3 and data.shape[2] > 4: if data.ndim == 3 and data.shape[2] > 4:
@ -1084,6 +1083,11 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False):
else: else:
dtype = np.min_scalar_type(lut.shape[0]-1) dtype = np.min_scalar_type(lut.shape[0]-1)
# awkward, but fastest numpy native nan evaluation
#
nanMask = None
if data.dtype.kind == 'f' and np.isnan(data.min()):
nanMask = np.isnan(data)
# Apply levels if given # Apply levels if given
if levels is not None: if levels is not None:
if isinstance(levels, np.ndarray) and levels.ndim == 2: if isinstance(levels, np.ndarray) and levels.ndim == 2:
@ -1107,9 +1111,7 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False):
maxVal = np.nextafter(maxVal, 2*maxVal) maxVal = np.nextafter(maxVal, 2*maxVal)
data = rescaleData(data, scale/(maxVal-minVal), minVal, dtype=dtype) data = rescaleData(data, scale/(maxVal-minVal), minVal, dtype=dtype)
profile() profile()
# apply LUT if given # apply LUT if given
if lut is not None: if lut is not None:
data = applyLookupTable(data, lut) data = applyLookupTable(data, lut)
@ -1153,6 +1155,11 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False):
else: else:
alpha = True alpha = True
# apply nan mask through alpha channel
if nanMask is not None:
alpha = True
imgData[nanMask, 3] = 0
profile() profile()
return imgData, alpha return imgData, alpha