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):
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
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)
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