Merge pull request #503 from kiwi0fruit/patch-1

Fix bug in RawImageWidget.py
This commit is contained in:
Ogi Moore 2020-06-09 20:50:58 -07:00 committed by GitHub
commit 26d54e7cc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,32 +1,43 @@
# -*- coding: utf-8 -*-
"""
RawImageWidget.py
Copyright 2010-2016 Luke Campagnola
Distributed under MIT/X11 license. See license.txt for more infomation.
"""
from ..Qt import QtCore, QtGui from ..Qt import QtCore, QtGui
try: try:
from ..Qt import QtOpenGL from ..Qt import QtOpenGL
from OpenGL.GL import * from OpenGL.GL import *
HAVE_OPENGL = True HAVE_OPENGL = True
except Exception: except (ImportError, AttributeError):
# Would prefer `except ImportError` here, but some versions of pyopengl generate # Would prefer `except ImportError` here, but some versions of pyopengl generate
# AttributeError upon import # AttributeError upon import
HAVE_OPENGL = False HAVE_OPENGL = False
from .. import functions as fn from .. import getConfigOption, functions as fn
import numpy as np
class RawImageWidget(QtGui.QWidget): class RawImageWidget(QtGui.QWidget):
""" """
Widget optimized for very fast video display. Widget optimized for very fast video display.
Generally using an ImageItem inside GraphicsView is fast enough. Generally using an ImageItem inside GraphicsView is fast enough.
On some systems this may provide faster video. See the VideoSpeedTest example for benchmarking. On some systems this may provide faster video. See the VideoSpeedTest example for benchmarking.
""" """
def __init__(self, parent=None, scaled=False): 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. 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.
""" """
QtGui.QWidget.__init__(self, parent=None) QtGui.QWidget.__init__(self, parent)
self.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)) self.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding))
self.scaled = scaled self.scaled = scaled
self.opts = None self.opts = None
self.image = None self.image = None
def setImage(self, img, *args, **kargs): def setImage(self, img, *args, **kargs):
""" """
img must be ndarray of shape (x,y), (x,y,3), or (x,y,4). img must be ndarray of shape (x,y), (x,y,3), or (x,y,4).
@ -43,22 +54,22 @@ class RawImageWidget(QtGui.QWidget):
argb, alpha = fn.makeARGB(self.opts[0], *self.opts[1], **self.opts[2]) argb, alpha = fn.makeARGB(self.opts[0], *self.opts[1], **self.opts[2])
self.image = fn.makeQImage(argb, alpha) self.image = fn.makeQImage(argb, alpha)
self.opts = () self.opts = ()
#if self.pixmap is None: # if self.pixmap is None:
#self.pixmap = QtGui.QPixmap.fromImage(self.image) # self.pixmap = QtGui.QPixmap.fromImage(self.image)
p = QtGui.QPainter(self) p = QtGui.QPainter(self)
if self.scaled: if self.scaled:
rect = self.rect() rect = self.rect()
ar = rect.width() / float(rect.height()) ar = rect.width() / float(rect.height())
imar = self.image.width() / float(self.image.height()) imar = self.image.width() / float(self.image.height())
if ar > imar: if ar > imar:
rect.setWidth(int(rect.width() * imar/ar)) rect.setWidth(int(rect.width() * imar / ar))
else: else:
rect.setHeight(int(rect.height() * ar/imar)) rect.setHeight(int(rect.height() * ar / imar))
p.drawImage(rect, self.image) p.drawImage(rect, self.image)
else: else:
p.drawImage(QtCore.QPointF(), self.image) p.drawImage(QtCore.QPointF(), self.image)
#p.drawPixmap(self.rect(), self.pixmap) # p.drawPixmap(self.rect(), self.pixmap)
p.end() p.end()
@ -67,14 +78,18 @@ if HAVE_OPENGL:
""" """
Similar to RawImageWidget, but uses a GL widget to do all drawing. Similar to RawImageWidget, but uses a GL widget to do all drawing.
Perfomance varies between platforms; see examples/VideoSpeedTest for benchmarking. Perfomance varies between platforms; see examples/VideoSpeedTest for benchmarking.
Checks if setConfigOptions(imageAxisOrder='row-major') was set.
""" """
def __init__(self, parent=None, scaled=False): def __init__(self, parent=None, scaled=False):
QtOpenGL.QGLWidget.__init__(self, parent=None) QtOpenGL.QGLWidget.__init__(self, parent)
self.scaled = scaled self.scaled = scaled
self.image = None self.image = None
self.uploaded = False self.uploaded = False
self.smooth = False self.smooth = False
self.opts = None self.opts = None
self.row_major = getConfigOption('imageAxisOrder') == 'row-major'
def setImage(self, img, *args, **kargs): def setImage(self, img, *args, **kargs):
""" """
@ -88,7 +103,7 @@ if HAVE_OPENGL:
def initializeGL(self): def initializeGL(self):
self.texture = glGenTextures(1) self.texture = glGenTextures(1)
def uploadTexture(self): def uploadTexture(self):
glEnable(GL_TEXTURE_2D) glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texture) glBindTexture(GL_TEXTURE_2D, self.texture)
@ -100,17 +115,22 @@ if HAVE_OPENGL:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER)
#glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER) # glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER)
shape = self.image.shape
if self.row_major:
### Test texture dimensions first image = self.image
#glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGBA, shape[0], shape[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, None) else:
#if glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH) == 0: image = self.image.transpose((1, 0, 2))
#raise Exception("OpenGL failed to create 2D texture (%dx%d); too large for this hardware." % shape[:2])
# ## Test texture dimensions first
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, shape[0], shape[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, self.image.transpose((1,0,2))) # shape = self.image.shape
# glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGBA, shape[0], shape[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, None)
# if glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH) == 0:
# raise Exception("OpenGL failed to create 2D texture (%dx%d); too large for this hardware." % shape[:2])
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.shape[1], image.shape[0], 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
glDisable(GL_TEXTURE_2D) glDisable(GL_TEXTURE_2D)
def paintGL(self): def paintGL(self):
if self.image is None: if self.image is None:
if self.opts is None: if self.opts is None:
@ -118,26 +138,23 @@ if HAVE_OPENGL:
img, args, kwds = self.opts img, args, kwds = self.opts
kwds['useRGBA'] = True kwds['useRGBA'] = True
self.image, alpha = fn.makeARGB(img, *args, **kwds) self.image, alpha = fn.makeARGB(img, *args, **kwds)
if not self.uploaded: if not self.uploaded:
self.uploadTexture() self.uploadTexture()
glViewport(0, 0, self.width() * self.devicePixelRatio(), self.height() * self.devicePixelRatio()) glViewport(0, 0, self.width() * self.devicePixelRatio(), self.height() * self.devicePixelRatio())
glEnable(GL_TEXTURE_2D) glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texture) glBindTexture(GL_TEXTURE_2D, self.texture)
glColor4f(1,1,1,1) glColor4f(1, 1, 1, 1)
glBegin(GL_QUADS) glBegin(GL_QUADS)
glTexCoord2f(0,0) glTexCoord2f(0, 1)
glVertex3f(-1,-1,0) glVertex3f(-1, -1, 0)
glTexCoord2f(1,0) glTexCoord2f(1, 1)
glVertex3f(1, -1, 0) glVertex3f(1, -1, 0)
glTexCoord2f(1,1) glTexCoord2f(1, 0)
glVertex3f(1, 1, 0) glVertex3f(1, 1, 0)
glTexCoord2f(0,1) glTexCoord2f(0, 0)
glVertex3f(-1, 1, 0) glVertex3f(-1, 1, 0)
glEnd() glEnd()
glDisable(GL_TEXTURE_3D) glDisable(GL_TEXTURE_3D)