+import struct
+
class Statement:
def __init__(self, keyword, *args):
self.keyword = keyword
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)
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
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)
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)
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")))
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