From 45cf3100de637ed7e53ebb565fbb840ae1534255 Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Tue, 13 Apr 2021 03:35:17 +0800 Subject: [PATCH] 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) --- pyqtgraph/graphicsItems/ImageItem.py | 22 ++++++------------- .../tests/test_ImageItemFormat.py | 2 +- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/pyqtgraph/graphicsItems/ImageItem.py b/pyqtgraph/graphicsItems/ImageItem.py index 681ec1de..f084b093 100644 --- a/pyqtgraph/graphicsItems/ImageItem.py +++ b/pyqtgraph/graphicsItems/ImageItem.py @@ -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 diff --git a/pyqtgraph/graphicsItems/tests/test_ImageItemFormat.py b/pyqtgraph/graphicsItems/tests/test_ImageItemFormat.py index 0838c115..09409f36 100644 --- a/pyqtgraph/graphicsItems/tests/test_ImageItemFormat.py +++ b/pyqtgraph/graphicsItems/tests/test_ImageItemFormat.py @@ -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]