From 1082d0a7fd7bf7efa269fde035b58c02785781ec Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 13 Apr 2021 01:25:22 +0300 Subject: [PATCH] Use more efficient methods to manipulate and store texture data --- blender/io_mspgl/export_texture.py | 30 +++++++++++++++++++++++------- scripts/maketex.py | 28 +++++++++++++++++++--------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/blender/io_mspgl/export_texture.py b/blender/io_mspgl/export_texture.py index 2ffb5246..6ec13ead 100644 --- a/blender/io_mspgl/export_texture.py +++ b/blender/io_mspgl/export_texture.py @@ -1,4 +1,23 @@ import os +import base64 +import codecs + +def encode_pixels(pixels): + from .datafile import Token + return Token(codecs.decode(b"="+base64.b64encode(bytes(pixels))+b"=", "ascii")) + +def pixels_to_rgba(pixels): + return (int(p*255) for p in pixels) + +def pixels_to_rgb(pixels): + for i in range(0, len(pixels), 4): + yield int(pixels[i]*255) + yield int(pixels[i+1]*255) + yield int(pixels[i+2]*255) + +def pixels_to_gray(pixels): + for i in range(0, len(pixels), 4): + yield int((pixels[i]+pixels[i+1]+pixels[i+2])*255/3) class TextureExporter: def __init__(self): @@ -31,17 +50,14 @@ class TextureExporter: tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1])) + pixels = tuple(image.pixels) texdata = "" if usage=='RGBA': - for p in image.pixels: - texdata += "\\x{:02X}".format(int(p*255)) + texdata = encode_pixels(pixels_to_rgba(pixels)) elif usage=='GRAY': - for i in range(0, len(image.pixels), 4): - texdata += "\\x{:02X}".format(image.pixels[i]) + texdata = encode_pixels(pixels_to_gray(pixels)) else: - for i in range(0, len(image.pixels), 4): - for j in range(3): - texdata += "\\x{:02X}".format(int(image.pixels[i+j]*255)) + texdata = encode_pixels(pixels_to_rgb(pixels)) tex_res.statements.append(Statement("raw_data", texdata)) return tex_res diff --git a/scripts/maketex.py b/scripts/maketex.py index 57380c0b..72ff49ac 100755 --- a/scripts/maketex.py +++ b/scripts/maketex.py @@ -13,8 +13,21 @@ def escape(str): result += c return result +def values(data, size): + for y in range(size[1]-1, -1, -1): + for x in range(size[0]): + yield data[x+y*size[0]] + +def values_multi(data, size): + for y in range(size[1]-1, -1, -1): + for x in range(size[0]): + for c in data[x+y*size[0]]: + yield c + def make_tex(fn, mipmap=False, srgb=False): from PIL import Image + import base64 + import codecs img = Image.open(fn) @@ -30,15 +43,12 @@ def make_tex(fn, mipmap=False, srgb=False): result = "storage {} {} {};\n".format(fmt, img.size[0], img.size[1]) if mipmap: result += "generate_mipmap true;\n" - result += "raw_data \"" - data = list(img.getdata()) - for y in range(img.size[1]): - i = (img.size[1]-1-y)*img.size[0] - if len(img.getbands())==1: - result += escape("".join([chr(v) for v in data[i:i+img.size[0]]])) - else: - result += escape("".join(["".join([chr(v) for v in p]) for p in data[i:i+img.size[0]]])) - result += "\";\n" + + if len(img.getbands())==1: + pixels = values(img.getdata(), img.size) + else: + pixels = values_multi(img.getdata(), img.size) + result += "raw_data ={}=;\n".format(codecs.decode(base64.b64encode(bytes(pixels)), "ascii")) return result -- 2.45.2