]> git.tdb.fi Git - libs/gl.git/commitdiff
Export raw texture data to a separate file
authorMikko Rasa <tdb@tdb.fi>
Tue, 2 Nov 2021 11:02:14 +0000 (13:02 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 2 Nov 2021 13:11:16 +0000 (15:11 +0200)
blender/io_mspgl/datafile.py
blender/io_mspgl/export_texture.py

index 6ed3ea4575a3b63ff32de414d8fc7fbaaa2dcbee..b9d0b3afe0b1267e62e5d35e85a7779aa9973734 100644 (file)
@@ -1,3 +1,5 @@
+import struct
+
 class Statement:
        def __init__(self, keyword, *args):
                self.keyword = keyword
@@ -19,7 +21,7 @@ class Statement:
                                        s += " false"
                        elif type(a)==str:
                                s += ' "{}"'.format(a)
-                       elif type(a)==Resource:
+                       elif type(a)==Resource or type(a)==RawData:
                                s += ' "{}"'.format(a.name)
                        else:
                                s += " {}".format(a)
@@ -43,6 +45,21 @@ class Token:
                return self.text
 
 
+class RawData:
+       def __init__(self, name, data):
+               self.name = name
+               self.data = data
+
+       def collect_references(self):
+               return []
+
+       def write_to_file(self, fn):
+               with open(fn, "wb") as out_file:
+                       out_file.write(b"MDR\x01")
+                       out_file.write(struct.pack(">QH", len(self.data), 0))
+                       out_file.write(self.data)
+
+
 class Resource:
        def __init__(self, name, keyword):
                self.name = name
@@ -56,11 +73,11 @@ class Resource:
 
                resources = []
                for a in args:
-                       if type(a)==Resource:
+                       if type(a)==Resource or type(a)==RawData:
                                resources.append(a)
 
                if not resources:
-                       raise TypeError("create_reference_statement expected a Resource argument, found none")
+                       raise TypeError("create_reference_statement expected a Resource or RawData argument, found none")
 
                self.references += [r for r in resources if r not in self.references]
                return Statement(keyword, *args)
index 3d21cd1a796a77d540cda19ff72ba39179509ea6..dc291c9cf64cd0c93a96df939bc0b1fd685bdc36 100644 (file)
@@ -2,10 +2,6 @@ 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)
 
@@ -28,7 +24,7 @@ def pixels_to_gray(pixels):
 class TextureExporter:
        def export_texture(self, tex_node, usage='RGB', *, invert_green=False):
                image = tex_node.image
-               from .datafile import Resource, Statement, Token
+               from .datafile import RawData, Resource, Statement, Token
                tex_res = Resource(image.name+".tex", "texture")
 
                tex_res.statements.append(Statement("type", Token("\\2d")))
@@ -58,14 +54,16 @@ class TextureExporter:
                        pixels = tuple(image.pixels)
                        texdata = ""
                        if usage=='RGBA':
-                               texdata = encode_pixels(pixels_to_rgba(pixels))
+                               texdata = pixels_to_rgba(pixels)
                        elif usage=='GRAY':
-                               texdata = encode_pixels(pixels_to_gray(pixels))
+                               texdata = pixels_to_gray(pixels)
                        elif invert_green:
-                               texdata = encode_pixels(pixels_to_rgb_invert_green(pixels))
+                               texdata = pixels_to_rgb_invert_green(pixels)
                        else:
-                               texdata = encode_pixels(pixels_to_rgb(pixels))
-                       tex_res.statements.append(Statement("raw_data", texdata))
+                               texdata = pixels_to_rgb(pixels)
+
+                       data = RawData(image.name+".mdr", bytes(texdata))
+                       tex_res.statements.append(tex_res.create_reference_statement("external_data", data))
 
                return tex_res