remove little-endian assumption for image export

This commit is contained in:
KIU Shueng Chuan 2021-06-09 09:06:02 +08:00
parent 6f0ffcbf8f
commit 765f9648cd
2 changed files with 18 additions and 23 deletions

View File

@ -1,8 +1,9 @@
from .Exporter import Exporter from .Exporter import Exporter
from ..parametertree import Parameter from ..parametertree import Parameter
from ..Qt import QtGui, QtCore, QtSvg, QT_LIB from ..Qt import QtCore, QtGui, QtWidgets
from .. import functions as fn from .. import functions as fn
import numpy as np import numpy as np
import sys
translate = QtCore.QCoreApplication.translate translate = QtCore.QCoreApplication.translate
__all__ = ['ImageExporter'] __all__ = ['ImageExporter']
@ -73,15 +74,8 @@ class ImageExporter(Exporter):
targetRect = QtCore.QRect(0, 0, w, h) targetRect = QtCore.QRect(0, 0, w, h)
sourceRect = self.getSourceRect() sourceRect = self.getSourceRect()
bg = np.empty((h, w, 4), dtype=np.ubyte) self.png = QtGui.QImage(w, h, QtGui.QImage.Format.Format_ARGB32)
color = self.params['background'] self.png.fill(self.params['background'])
bg[:,:,0] = color.blue()
bg[:,:,1] = color.green()
bg[:,:,2] = color.red()
bg[:,:,3] = color.alpha()
self.png = fn.makeQImage(bg, alpha=True, copy=False, transpose=False)
self.bg = bg
## set resolution of image: ## set resolution of image:
origTargetRect = self.getTargetRect() origTargetRect = self.getTargetRect()
@ -104,13 +98,18 @@ class ImageExporter(Exporter):
painter.end() painter.end()
if self.params['invertValue']: if self.params['invertValue']:
mn = bg[...,:3].min(axis=2) bg = fn.qimage_to_ndarray(self.png)
mx = bg[...,:3].max(axis=2) if sys.byteorder == 'little':
cv = slice(0, 3)
else:
cv = slice(1, 4)
mn = bg[...,cv].min(axis=2)
mx = bg[...,cv].max(axis=2)
d = (255 - mx) - mn d = (255 - mx) - mn
bg[...,:3] += d[...,np.newaxis] bg[...,cv] += d[...,np.newaxis]
if copy: if copy:
QtGui.QApplication.clipboard().setImage(self.png) QtWidgets.QApplication.clipboard().setImage(self.png)
elif toBytes: elif toBytes:
return self.png return self.png
else: else:

View File

@ -557,15 +557,11 @@ class GLViewWidget(QtWidgets.QOpenGLWidget):
pixels[...,3] = 255 pixels[...,3] = 255
glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels) glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels)
# swap B,R channels for Qt pixels = pixels[::-1].copy() # flip vertical
tmp = pixels[...,0].copy()
pixels[...,0] = pixels[...,2] qimg = fn.ndarray_to_qimage(pixels, QtGui.QImage.Format.Format_RGBA8888)
pixels[...,2] = tmp return qimg
pixels = pixels[::-1] # flip vertical
img = fn.makeQImage(pixels, transpose=False)
return img
def renderToArray(self, size, format=GL_BGRA, type=GL_UNSIGNED_BYTE, textureSize=1024, padding=256): def renderToArray(self, size, format=GL_BGRA, type=GL_UNSIGNED_BYTE, textureSize=1024, padding=256):
w,h = map(int, size) w,h = map(int, size)