diff --git a/NinjaTexture.py b/NinjaTexture.py index a79a7d8..e05f086 100644 --- a/NinjaTexture.py +++ b/NinjaTexture.py @@ -92,8 +92,10 @@ class NinjaTexture: # { if tex['bitmap'] is None: continue imgName = "output/" + tex['name'] + '.png' - print(tex['bitmap']) - png.from_array(tex['bitmap'], 'RGBA').save(imgName) + w = png.Writer(tex['width'], tex['height'], greyscale=False, alpha=True,compression=9) + f = open(imgName, 'wb') + w.write(f, tex['bitmap']) + f.close() return None @@ -117,8 +119,8 @@ class NinjaTexture: # { if flags & 0x08: bytes = self.file.read(0x1c) - name = str(bytes) - tex['name'] = name.replace('\\x00', '') + name = bytes.decode() + tex['name'] = name.replace('\x00', '') if flags & 0x04: bytes = self.file.read(2) p = struct.unpack('H', bytes) @@ -155,8 +157,8 @@ class NinjaTexture: # { return None - def readPvr(self, info): - print(info) + def readPvr(self, tex): + print(tex) # Read Header bytes = self.file.read(8) @@ -178,6 +180,9 @@ class NinjaTexture: # { self.codebook_size = 0 self.mipWidth = self.width self.mipHeight = self.height + tex['complete'] = False + + self.bitmap = [[0 for x in range(self.width * 4)] for y in range(self.height)] twiddled = ( NinjaTexture.TWIDDLED, @@ -255,32 +260,25 @@ class NinjaTexture: # { seek_ofs = self.get_mipmap_size() self.file.read(seek_ofs) - self.startOfs = self.file.tell() - if self.isTwiddled: - print("Reading twiddled texture") - self.dst_array = self.detwiddle(self.mipWidth, self.mipHeight) - else: - return None - - tmp_bitmap = [] - for i in range(len(self.dst_array)): - if self.color_format == 0: - color = self.dst_array[i] - bitmap = self.ARGB_1555(color) - tmp_bitmap.extend(bitmap) - elif self.color_format == 1: - color = self.dst_array[i] - bitmap = self.ARGB_565(color) - tmp_bitmap.extend(bitmap) - elif self.color_format == 2: - color = self.dst_array[i] - bitmap = self.ARGB_4444(color) - tmp_bitmap.extend(bitmap) - else: - print("Color format: ", self.color_format) - noesis.doException("Non supported pvr color format") - return struct.pack('B'*len(tmp_bitmap), *tmp_bitmap) + self.startOfs = self.file.tell() + for y in range(self.height): + for x in range(self.width): + i = self.untwiddle(x, y) + self.file.seek(self.startOfs + (i * 2), 0) + b = self.file.read(2) + color = struct.unpack('H', b)[0] + if self.color_format == 0: + rgba = self.ARGB_1555(color) + elif self.color_format == 1: + rgba = self.RGB_565(color) + elif self.color_format == 2: + rgba = self.ARGB_4444(color) + self.bitmap[y][x*4 + 0] = rgba[0] + self.bitmap[y][x*4 + 1] = rgba[1] + self.bitmap[y][x*4 + 2] = rgba[2] + self.bitmap[y][x*4 + 3] = rgba[3] + return self.bitmap def get_mipmap_size(self): mipCount = 0 @@ -310,22 +308,6 @@ class NinjaTexture: # { seek_ofs = seek_ofs + 2 return seek_ofs - def detwiddle(self, w, h): - arr = [None] * (w * h) - for y in range(h): - for x in range(w): - i = self.untwiddle(x, y) - idx = y * h + x - if self.isCompressed: - self.file.seek(self.startOfs + i, 0) - b = self.file.read(1) - arr[idx] = struct.unpack('B', b)[0] - else: - self.file.seek(self.startOfs + (i * 2), 0) - b = self.file.read(2) - arr[idx] = struct.unpack('H', b)[0] - return arr - def untwiddle(self, x, y): key = "{0}_{1}".format(x,y) if key in NinjaTexture.LOOKUP_TABLE: @@ -348,18 +330,18 @@ class NinjaTexture: # { r = (v >> (10-3)) & 0xf8 g = (v >> (5-3)) & 0xf8 b = (v << 3) & 0xf8 - return [r,g,b,a] + return (r,g,b,a) def ARGB_4444 (self, v): a = (v >> (12-4)) & 0xf0 r = (v >> (8-4)) & 0xf0 g = (v >> (4-4)) & 0xf0 b = (v << 4) & 0xf0 - return [r,g,b,a] + return (r,g,b,a) - def ARGB_565(self, v): + def RGB_565(self, v): a = 0xff r = (v >> (11-3)) & (0x1f<<3) g = (v >> (5-2)) & (0x3f<<2) b = (v << 3) & (0x1f<<3) - return [r,g,b,a] + return (r,g,b,a) diff --git a/output/readme.txt b/output/readme.txt deleted file mode 100644 index e69de29..0000000