]> git.tdb.fi Git - libs/gl.git/commitdiff
Use more efficient methods to manipulate and store texture data
authorMikko Rasa <tdb@tdb.fi>
Mon, 12 Apr 2021 22:25:22 +0000 (01:25 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 12 Apr 2021 22:25:52 +0000 (01:25 +0300)
blender/io_mspgl/export_texture.py
scripts/maketex.py

index 2ffb5246cf448839c9a594e1accd1397c018da79..6ec13eadc583ff680495451609b25651bc173f1c 100644 (file)
@@ -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
index 57380c0b7a5ef11b1fa4c3883bc594a38cddc6f9..72ff49acb7a1173e536a72721bed4844bd7f5065 100755 (executable)
@@ -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