File: RawImageWidget.py

package info (click to toggle)
python-pyqtgraph 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,168 kB
  • sloc: python: 54,831; makefile: 128; ansic: 40; sh: 2
file content (266 lines) | stat: -rw-r--r-- 9,990 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""
RawImageWidget.py
Copyright 2010-2016 Luke Campagnola
Distributed under MIT/X11 license. See license.txt for more information.
"""

import importlib
import warnings
import numpy as np

from .. import functions as fn
from .. import functions_qimage
from .. import getConfigOption, getCupy
from ..Qt import QtCore, QtGui, QtWidgets, QT_LIB
from ..Qt import OpenGLConstants as GLC
from ..Qt import OpenGLHelpers

if QT_LIB in ["PyQt5", "PySide2"]:
    QtOpenGL = QtGui
    QtOpenGLWidgets = QtWidgets
else:
    QtOpenGL = importlib.import_module(f"{QT_LIB}.QtOpenGL")
    QtOpenGLWidgets = importlib.import_module(f"{QT_LIB}.QtOpenGLWidgets")

# importing cuda python is fast
try:
    from cuda.bindings import runtime as cudart
except ImportError:
    cudart = None

__all__ = ['RawImageWidget', 'RawImageGLWidget']

class RawImageWidget(QtWidgets.QWidget):
    """
    Widget optimized for very fast video display.
    Generally using an ImageItem inside GraphicsView is fast enough.
    On some systems this may provide faster video. See the VideoSpeedTest example for benchmarking.
    """

    def __init__(self, parent=None, scaled=False):
        """
        Setting scaled=True will cause the entire image to be displayed within the boundaries of the widget.
        This also greatly reduces the speed at which it will draw frames.
        """
        QtWidgets.QWidget.__init__(self, parent)
        self.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Expanding))
        self.scaled = scaled
        self.opts = None
        self.image = None
        self._cp = getCupy()

    def setImage(self, img, *args, **kargs):
        """
        img must be ndarray of shape (x,y), (x,y,3), or (x,y,4).
        Extra arguments are sent to functions.makeARGB
        """
        if getConfigOption('imageAxisOrder') == 'col-major':
            img = img.swapaxes(0, 1)
        self.opts = (img, args, kargs)
        self.image = None
        self.update()

    def paintEvent(self, ev):
        if self.opts is None:
            return
        if self.image is None:
            img = self.opts[0]
            xp = self._cp.get_array_module(img) if self._cp else np

            qimage = None
            if (
                not self.opts[1]    # no positional arguments
                and {"levels", "lut"}.issuperset(self.opts[2])  # no kwargs besides levels and lut
            ):
                transparentLocations = None
                if img.dtype.kind == "f" and xp.isnan(img.min()):
                    nanmask = xp.isnan(img)
                    if nanmask.ndim == 3:
                        nanmask = nanmask.any(axis=2)
                    transparentLocations = nanmask.nonzero()

                qimage = functions_qimage.try_make_qimage(
                    img,
                    levels=self.opts[2].get("levels"),
                    lut=self.opts[2].get("lut"),
                    transparentLocations=transparentLocations
                )

            if qimage is None:
                argb, alpha = fn.makeARGB(self.opts[0], *self.opts[1], **self.opts[2])
                if self._cp and self._cp.get_array_module(argb) == self._cp:
                    argb = argb.get()  # transfer GPU data back to the CPU
                qimage = fn.ndarray_to_qimage(argb, QtGui.QImage.Format.Format_ARGB32)

            self.image = qimage
            self.opts = ()
        # if self.pixmap is None:
            # self.pixmap = QtGui.QPixmap.fromImage(self.image)
        p = QtGui.QPainter(self)
        if self.scaled:
            rect = self.rect()
            ar = rect.width() / float(rect.height())
            imar = self.image.width() / float(self.image.height())
            if ar > imar:
                rect.setWidth(int(rect.width() * imar / ar))
            else:
                rect.setHeight(int(rect.height() * ar / imar))

            p.drawImage(rect, self.image)
        else:
            p.drawImage(QtCore.QPointF(), self.image)
        # p.drawPixmap(self.rect(), self.pixmap)
        p.end()

def checkCudaErrors(result):
    if err_num := result[0]:
        error_name = cudart.cudaGetErrorName(err_num)[1].decode()
        raise RuntimeError(f"{error_name}({err_num})")
    if len(result) == 1:
        return None
    elif len(result) == 2:
        return result[1]
    else:
        return result[1:]

class RawImageGLWidget(QtOpenGLWidgets.QOpenGLWidget):
    """
    Similar to RawImageWidget, but uses a GL widget to do all drawing.
    Performance varies between platforms; see examples/VideoSpeedTest for benchmarking.

    Checks if setConfigOptions(imageAxisOrder='row-major') was set.
    """

    def __init__(self, parent=None, smooth=False):
        super().__init__(parent)
        self.image = None
        self.uploaded = False
        self.smooth = smooth
        self.opts = None
        self.gfx_resource = None

        self.m_texture = QtOpenGL.QOpenGLTexture(QtOpenGL.QOpenGLTexture.Target.Target2D)
        self.m_blitter = QtOpenGL.QOpenGLTextureBlitter()

        self.try_cuda = cudart is not None

    def setImage(self, img, *args, **kargs):
        """
        img must be ndarray of shape (x,y), (x,y,3), or (x,y,4).
        Extra arguments are sent to functions.makeARGB
        """
        if getConfigOption('imageAxisOrder') == 'col-major':
            img = img.swapaxes(0, 1)
        self.opts = (img, args, kargs)
        self.image = None
        self.uploaded = False
        self.update()

    def initializeGL(self):
        ctx = self.context()

        # in Python, slot will not get called during application termination
        ctx.aboutToBeDestroyed.connect(self.cleanup)

        self.glfn = OpenGLHelpers.getFunctions(ctx)

        self.m_blitter.create()

    def cleanup(self):
        # explicit call of cleanup() is needed during application termination
        self.makeCurrent()
        if self.gfx_resource is not None:
            cudart.cudaGraphicsUnregisterResource(self.gfx_resource)
            self.gfx_resource = None
        self.m_texture.destroy()
        self.m_blitter.destroy()
        self.uploaded = False
        self.doneCurrent()

    def uploadTexture(self):
        rgba = self.image
        h, w = rgba.shape[:2]

        if self.m_texture.isCreated() and (w != self.m_texture.width() or h != self.m_texture.height()):
            if self.gfx_resource is not None:
                cudart.cudaGraphicsUnregisterResource(self.gfx_resource)
                self.gfx_resource = None
            self.m_texture.destroy()

        if not self.m_texture.isCreated():
            self.m_texture.setFormat(QtOpenGL.QOpenGLTexture.TextureFormat.RGBA8_UNorm)
            self.m_texture.setSize(w, h)
            self.m_texture.allocateStorage()

        filt = QtOpenGL.QOpenGLTexture.Filter.Linear if self.smooth else QtOpenGL.QOpenGLTexture.Filter.Nearest
        self.m_texture.setMinMagFilters(filt, filt)
        self.m_texture.setWrapMode(QtOpenGL.QOpenGLTexture.WrapMode.ClampToBorder)

        # cupy, pytorch and numba support CAI
        data_on_gpu = hasattr(rgba, '__cuda_array_interface__')

        if data_on_gpu and self.try_cuda and self.gfx_resource is None:
            try:
                self.gfx_resource = checkCudaErrors(cudart.cudaGraphicsGLRegisterImage(
                    self.m_texture.textureId(), self.m_texture.target().value,
                    cudart.cudaGraphicsRegisterFlags.cudaGraphicsRegisterFlagsWriteDiscard
                ))
            except RuntimeError as e:
                # registration can fail if the OpenGL gpu is not the CUDA gpu,
                warnings.warn(f"cudaGraphicsGLRegisterImage failed: {e}")
                # registration failed, don't try to register anymore
                self.try_cuda = False

        if data_on_gpu and self.gfx_resource is None:
            # cannot do gpu direct transfer, so transfer back to cpu
            rgba = rgba.get()   # cupy syntax
            data_on_gpu = False

        if not data_on_gpu:
            self.m_texture.setData(
                QtOpenGL.QOpenGLTexture.PixelFormat.RGBA,
                QtOpenGL.QOpenGLTexture.PixelType.UInt8,
                rgba)
        else:
            cai = rgba.__cuda_array_interface__
            device_ptr = cai['data'][0]
            stream = cai.get('stream')

            checkCudaErrors(cudart.cudaGraphicsMapResources(1, self.gfx_resource, stream))
            try:
                cuda_array = checkCudaErrors(cudart.cudaGraphicsSubResourceGetMappedArray(
                    self.gfx_resource, 0, 0
                ))

                src_pitch = w * 4 * rgba.itemsize   # assume contiguous
                checkCudaErrors(cudart.cudaMemcpy2DToArrayAsync(
                    cuda_array, 0, 0,
                    device_ptr, src_pitch,
                    w * 4 * rgba.itemsize, h,
                    cudart.cudaMemcpyKind.cudaMemcpyDefault,
                    stream
                ))
            finally:
                cudart.cudaGraphicsUnmapResources(1, self.gfx_resource, stream)

    def paintGL(self):
        self.glfn.glClearColor(1, 1, 1, 1)
        self.glfn.glClear(GLC.GL_COLOR_BUFFER_BIT)
        self.glfn.glEnable(GLC.GL_BLEND)
        self.glfn.glBlendFuncSeparate(GLC.GL_SRC_ALPHA, GLC.GL_ONE_MINUS_SRC_ALPHA, 1, GLC.GL_ONE_MINUS_SRC_ALPHA)

        if self.image is None:
            if self.opts is None:
                return
            img, args, kwds = self.opts
            self.image, _ = fn.makeRGBA(img, *args, **kwds)

        if not self.uploaded:
            # mark as uploaded whether or not it succeeds so that we don't retry and refail
            self.uploaded = True
            self.uploadTexture()

        target = QtGui.QMatrix4x4()
        self.m_blitter.bind()
        self.m_blitter.blit(self.m_texture.textureId(), target, QtOpenGL.QOpenGLTextureBlitter.Origin.OriginTopLeft)
        self.m_blitter.release()