Skip to content

Commit

Permalink
Merge pull request #4609 from hyarion/cleanup-draft-load-texture
Browse files Browse the repository at this point in the history
Draft: Simplify load_texture after py3 requirement
  • Loading branch information
yorikvanhavre committed Mar 26, 2021
2 parents 09e5ad7 + 6d37a38 commit 3025bd8
Showing 1 changed file with 13 additions and 42 deletions.
55 changes: 13 additions & 42 deletions src/Mod/Draft/draftutils/gui_utils.py
Expand Up @@ -38,7 +38,6 @@
# @{
import math
import os
import six

import FreeCAD as App
import draftutils.utils as utils
Expand Down Expand Up @@ -599,63 +598,35 @@ def load_texture(filename, size=None, gui=App.GuiUp):
buffersize = p.byteCount()
width = size[0]
height = size[1]
numcomponents = int(float(buffersize) / (width * height))
numcomponents = int(buffersize / (width * height))

img = coin.SoSFImage()
byteList = []
# isPy2 = sys.version_info.major < 3
isPy2 = six.PY2
byteList = bytearray()

# The SoSFImage needs to be filled with bytes.
# The pixel information is converted into a Qt color, gray,
# red, green, blue, or transparency (alpha),
# depending on the input image.
#
# If Python 2 is used, the color is turned into a character,
# which is of type 'byte', and added to the byte list.
# If Python 3 is used, characters are unicode strings,
# so they need to be encoded into 'latin-1'
# to produce the correct bytes for the list.
for y in range(height):
# line = width*numcomponents*(height-(y));
for x in range(width):
rgb = p.pixel(x, y)
if numcomponents == 1 or numcomponents == 2:
gray = chr(QtGui.qGray(rgb))
if isPy2:
byteList.append(gray)
else:
byteList.append(gray.encode('latin-1'))
rgba = p.pixel(x, y)
if numcomponents <= 2:
byteList.append(QtGui.qGray(rgba))

if numcomponents == 2:
alpha = chr(QtGui.qAlpha(rgb))
if isPy2:
byteList.append(alpha)
else:
byteList.append(alpha.encode('latin-1'))
elif numcomponents == 3 or numcomponents == 4:
red = chr(QtGui.qRed(rgb))
green = chr(QtGui.qGreen(rgb))
blue = chr(QtGui.qBlue(rgb))

if isPy2:
byteList.append(red)
byteList.append(green)
byteList.append(blue)
else:
byteList.append(red.encode('latin-1'))
byteList.append(green.encode('latin-1'))
byteList.append(blue.encode('latin-1'))
byteList.append(QtGui.qAlpha(rgba))

elif numcomponents <= 4:
byteList.append(QtGui.qRed(rgba))
byteList.append(QtGui.qGreen(rgba))
byteList.append(QtGui.qBlue(rgba))

if numcomponents == 4:
alpha = chr(QtGui.qAlpha(rgb))
if isPy2:
byteList.append(alpha)
else:
byteList.append(alpha.encode('latin-1'))
byteList.append(QtGui.qAlpha(rgba))
# line += numcomponents

_bytes = b"".join(byteList)
_bytes = bytes(byteList)
img.setValue(size, numcomponents, _bytes)
except FileNotFoundError as exc:
_wrn("load_texture: {0}, {1}".format(exc.strerror,
Expand Down

0 comments on commit 3025bd8

Please sign in to comment.