mirror of
https://github.com/grp/Wii.py.git
synced 2025-06-19 07:15:49 -04:00
removed toPNG, no going back now
This commit is contained in:
parent
d2db117aa1
commit
019c15daf6
107
TPL.py
107
TPL.py
@ -362,113 +362,6 @@ class TPL():
|
||||
iv += 1
|
||||
z = 0
|
||||
return out
|
||||
def toPNG(self, outfile): #single texture only
|
||||
"""This converts a TPL texture to a PNG image. You specify the input TPL filename in the initializer, and you specify the output filename in the outfile parameter to this method. Returns the output filename.
|
||||
|
||||
This only supports single textured TPL images."""
|
||||
if(self.file):
|
||||
data = open(self.file, "rb").read()
|
||||
else:
|
||||
data = self.data
|
||||
|
||||
header = self.TPLHeader()
|
||||
textures = []
|
||||
pos = 0
|
||||
|
||||
header.unpack(data[pos:pos + len(header)])
|
||||
pos += len(header)
|
||||
|
||||
palette_offsets = []
|
||||
|
||||
for i in range(header.ntextures):
|
||||
tmp = self.TPLTexture()
|
||||
tmp.unpack(data[pos:pos + len(tmp)])
|
||||
textures.append(tmp)
|
||||
pos += len(tmp)
|
||||
if(tmp.palette_offset > 0):
|
||||
palette_offsets.append(tmp.palette_offset)
|
||||
|
||||
if(header.ntextures > 1):
|
||||
raise ValueError("Only one texture supported. Don't touch me!")
|
||||
|
||||
for i in range(header.ntextures):
|
||||
head = textures[i]
|
||||
tex = self.TPLTextureHeader()
|
||||
tex.unpack(data[head.header_offset:head.header_offset + len(tex)])
|
||||
w = tex.width
|
||||
h = tex.height
|
||||
|
||||
if(tex.format == 0): #I4, 4-bit
|
||||
tpldata = struct.unpack(">" + str((w * h) / 2) + "B", data[tex.data_off:tex.data_off + ((w * h) / 2)])
|
||||
rgbdata = self.I4((w, h), tpldata)
|
||||
|
||||
elif(tex.format == 1): #I8, 8-bit
|
||||
print w*h, tex.data_off, len(data)-tex.data_off
|
||||
tpldata = struct.unpack(">" + str(w * h) + "B", data[tex.data_off:tex.data_off + (w * h * 1)])
|
||||
rgbdata = self.I8((w, h), tpldata)
|
||||
elif(tex.format == 2): #IA4, 8-bit
|
||||
tpldata = struct.unpack(">" + str(w * h) + "B", data[tex.data_off:tex.data_off + (w * h * 1)])
|
||||
rgbdata = self.IA4((w, h), tpldata)
|
||||
|
||||
elif(tex.format == 4): #RGB565, 16-bit
|
||||
tpldata = struct.unpack(">" + str(w * h) + "H", data[tex.data_off:tex.data_off + (w * h * 2)])
|
||||
rgbdata = self.RGB565((w, h), tpldata)
|
||||
elif(tex.format == 5): #RGB5A3, 16-bit
|
||||
tpldata = struct.unpack(">" + str(w * h) + "H", data[tex.data_off:tex.data_off + (w * h * 2)])
|
||||
rgbdata = self.RGB5A3((w, h), tpldata)
|
||||
elif(tex.format == 3): #IA8, 16-bit
|
||||
tpldata = struct.unpack(">" + str(w * h) + "H", data[tex.data_off:tex.data_off + (w * h * 2)])
|
||||
rgbdata = self.IA8((w, h), tpldata)
|
||||
|
||||
elif(tex.format == 6): #RGBA8, 32-bit, but for easyness's sake lets do it with 16-bit
|
||||
tpldata = struct.unpack(">" + str(w * h * 2) + "H", data[tex.data_off:tex.data_off + (w * h * 4)])
|
||||
rgbdata = self.RGBA8((w, h), tpldata)
|
||||
|
||||
elif(tex.format == 8 or tex.format == 9 or tex.format == 10):
|
||||
palhead = self.TPLPaletteHeader()
|
||||
offs = palette_offsets.pop(0)
|
||||
palhead.unpack(data[offs:offs + len(palhead)])
|
||||
|
||||
tpldata = struct.unpack(">" + str(palhead.nitems) + "H", data[palhead.offset:palhead.offset + (palhead.nitems * 2)])
|
||||
if(palhead.format == 0):
|
||||
palette_data = self.IA8((palhead.nitems, 1), tpldata)[0]
|
||||
elif(palhead.format == 1):
|
||||
palette_data = self.RGB565((palhead.nitems, 1), tpldata)[0]
|
||||
elif(palhead.format == 2):
|
||||
palette_data = self.RGB5A3((palhead.nitems, 1), tpldata)[0]
|
||||
|
||||
paldata = []
|
||||
for i in range(0, palhead.nitems * 4, 4):
|
||||
tmp = 0
|
||||
tmp |= palette_data[i + 0] << 24
|
||||
tmp |= palette_data[i + 1] << 16
|
||||
tmp |= palette_data[i + 2] << 8
|
||||
tmp |= palette_data[i + 3] << 0
|
||||
paldata.append(tmp)
|
||||
|
||||
if(tex.format == 8):
|
||||
tpldata = struct.unpack(">" + str((w * h) / 2) + "B", data[tex.data_off:tex.data_off + ((w * h) / 2)])
|
||||
rgbdata = self.CI4((w, h), tpldata, paldata)
|
||||
if(tex.format == 9):
|
||||
tpldata = struct.unpack(">" + str(w * h) + "B", data[tex.data_off:tex.data_off + (w * h * 1)])
|
||||
rgbdata = self.CI8((w, h), tpldata, paldata)
|
||||
if(tex.format == 10):
|
||||
tpldata = struct.unpack(">" + str(w * h) + "H", data[tex.data_off:tex.data_off + (w * h * 2)])
|
||||
rgbdata = self.CI14X2((w, h), tpldata, paldata)
|
||||
elif(tex.format == 14):
|
||||
sz = ((w + 7) >> 3) * ((w + 7) >> 3) * 32
|
||||
#print sz
|
||||
#print len(data[tex.data_off:])
|
||||
tpldata = struct.unpack(">" + str(sz / 2) + "H", data[tex.data_off:tex.data_off + sz])
|
||||
|
||||
rgbdata = self.CMP((w, h), tpldata)
|
||||
else:
|
||||
raise TypeError("Unsupported TPL Format: " + str(tex.format))
|
||||
|
||||
output = png.Writer(width = w, height = h, alpha = True, bitdepth = 8)
|
||||
output.write(open(outfile, "wb"), rgbdata)
|
||||
|
||||
return outfile
|
||||
def toImage(self, outfile):
|
||||
"""This converts a TPL texture to a PNG image. You specify the input TPL filename in the initializer, and you specify the output filename in the outfile parameter to this method. Returns the output filename.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user