no need to form a 1d 32-bit lut for array indexing

you can index (y, x) into a lookup table of shape (nentry, 3) or
(nentry, 4) and get an output of shape (y, x, 3) or (y, x, 4)
This commit is contained in:
KIU Shueng Chuan 2021-04-13 03:35:17 +08:00
parent 900d6743d8
commit 45cf3100de
2 changed files with 8 additions and 16 deletions

View File

@ -420,7 +420,6 @@ class ImageItem(GraphicsObject):
# into a single lut for better performance
scale = None
levels = self.levels
augmented_alpha = False
while True:
if image.dtype not in (self._xp.ubyte, self._xp.uint16):
@ -464,18 +463,14 @@ class ImageItem(GraphicsObject):
# apply the effective lut early for the following types:
if image.dtype == self._xp.uint16 and (image.ndim == 2 or image.shape[2] == 1):
# 1) uint16 mono
if lut.ndim == 2:
if lut.shape[1] == 3: # rgb
# convert rgb lut to rgba so that it is 32-bits
lut = numpy.column_stack([lut, numpy.full(lut.shape[0], 255, dtype=numpy.uint8)])
augmented_alpha = True
if lut.shape[1] == 4: # rgba
lut = lut.view(numpy.uint32)
image = lut.ravel()[image]
# before lookup, remove any pesky trailing dimensions
if image.ndim == 3 and image.shape[-1] == 1:
image = image.reshape(image.shape[:-1])
if lut.ndim == 2 and lut.shape[-1] == 1:
lut = lut.ravel()
image = lut[image]
lut = None
# now both levels and lut are None
if image.dtype == numpy.uint32:
image = image.view(numpy.uint8).reshape(image.shape + (4,))
elif image.ndim == 3 and image.shape[2] == 3:
# 2) {uint8, uint16} rgb
# for rgb images, the lut will be 1d
@ -526,10 +521,7 @@ class ImageItem(GraphicsObject):
elif image.shape[2] == 3:
fmt = QtGui.QImage.Format.Format_RGB888
elif image.shape[2] == 4:
if augmented_alpha:
fmt = QtGui.QImage.Format.Format_RGBX8888
else:
fmt = QtGui.QImage.Format.Format_RGBA8888
fmt = QtGui.QImage.Format.Format_RGBA8888
elif is_indexed8:
# levels and/or lut --> lut-only
fmt = QtGui.QImage.Format.Format_Indexed8

View File

@ -69,7 +69,7 @@ def test_uint16():
levels = None
check_format((h, w), dtype, levels, lut_mono1, Format.Format_Grayscale8)
check_format((h, w), dtype, levels, lut_mono2, Format.Format_Grayscale8)
check_format((h, w), dtype, levels, lut_rgb, Format.Format_RGBX8888)
check_format((h, w), dtype, levels, lut_rgb, Format.Format_RGB888)
check_format((h, w), dtype, levels, lut_rgba, Format.Format_RGBA8888)
levels = [lo, hi]