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

View File

@ -69,7 +69,7 @@ def test_uint16():
levels = None levels = None
check_format((h, w), dtype, levels, lut_mono1, Format.Format_Grayscale8) 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_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) check_format((h, w), dtype, levels, lut_rgba, Format.Format_RGBA8888)
levels = [lo, hi] levels = [lo, hi]