diff --git a/NinjaTexture.py b/NinjaTexture.py index a9b5b0a..bacef4a 100644 --- a/NinjaTexture.py +++ b/NinjaTexture.py @@ -30,32 +30,32 @@ import struct class NinjaTexture: # { # Color Formats - ARGB_1555 = 0x00 - RGB_565 = 0x01 - ARGB_4444 = 0x02 - YUV_422 = 0x03 - BUMP = 0x04 - RGB_555 = 0x05 - ARGB_8888 = 0x06 - YUV_420 = 0x06 + ARGB_1555 = 0x00 + RGB_565 = 0x01 + ARGB_4444 = 0x02 + YUV_422 = 0x03 + BUMP = 0x04 + RGB_555 = 0x05 + ARGB_8888 = 0x06 + YUV_420 = 0x06 # Data Formats - TWIDDLED = 0x01 - TWIDDLED_MM = 0x02 - VQ = 0x03 - VQ_MM = 0x04 - PALETTIZE4 = 0x05 - PALETTIZE4_MM = 0x06 - PALETTIZE8 = 0x07 - PALETTIZE8_MM = 0x08 - RECTANGLE = 0x09 - STRIDE = 0x0B - TWIDDLED_RECTANGLE = 0x0D - ABGR = 0x0E - ABGR_MM = 0x0F - SMALLVQ = 0x10 - SMALLVQ_MM = 0x11 - TWIDDLED_MM_ALIAS = 0x12 + TWIDDLED = 0x01 + TWIDDLED_MM = 0x02 + VQ = 0x03 + VQ_MM = 0x04 + PALETTIZE4 = 0x05 + PALETTIZE4_MM = 0x06 + PALETTIZE8 = 0x07 + PALETTIZE8_MM = 0x08 + RECTANGLE = 0x09 + STRIDE = 0x0B + TWIDDLED_RECTANGLE = 0x0D + ABGR = 0x0E + ABGR_MM = 0x0F + SMALLVQ = 0x10 + SMALLVQ_MM = 0x11 + TWIDDLED_MM_ALIAS = 0x12 def __init__(self, filename): # { @@ -113,6 +113,10 @@ class NinjaTexture: # { bytes = self.file.read(2) p = struct.unpack('H', bytes) tex['size'] = p[0] + lo_bytes = tex['size'] & 0x0f + hi_bytes = (tex['size'] >> 4) & 0x0f + tex['width'] = (1 << (lo_bytes + 2)) + tex['height'] = (1 << (hi_bytes + 2)) if flags & 0x01: bytes = self.file.read(4) p = struct.unpack('I', bytes) @@ -149,15 +153,17 @@ class NinjaTexture: # { self.width = p[3] self.height = p[4] - print("Color Format: %d" % color_format) - print("Data Format: %d" % data_format) - print("Width: %d" % width) - print("Height: %d" % height) + print("Color Format: %d" % self.color_format) + print("Data Format: %d" % self.data_format) + print("Width: %d" % self.width) + print("Height: %d" % self.height) self.isTwiddled = False self.isMipmap = False self.isCompressed = False self.codebook_size = 0 + self.mipWidth = self.width + self.mipHeight = self.height twiddled = ( NinjaTexture.TWIDDLED, @@ -176,9 +182,9 @@ class NinjaTexture: # { ) palette = ( - NinjaTexture.PALETTIZE4 - NinjaTexture.PALETTIZE4_MM - NinjaTexture.PALETTIZE8 + NinjaTexture.PALETTIZE4, + NinjaTexture.PALETTIZE4_MM, + NinjaTexture.PALETTIZE8, NinjaTexture.PALETTIZE8_MM ) @@ -196,6 +202,8 @@ class NinjaTexture: # { if self.data_format == NinjaTexture.VQ: self.isCompressed = true + self.mipWidth = self.width / 2 + self.mipHeight = self.height / 2 if self.width <= 16: self.codebook_size = 16 elif self.width == 32: @@ -206,6 +214,8 @@ class NinjaTexture: # { self.codebook_size = 256 elif self.data_format == NinjaTexture.VQ_MM: self.isCompressed = true + self.mipWidth = self.width / 2 + self.mipHeight = self.height / 2 if self.width <= 16: self.codebook_size = 16 elif self.width == 32: @@ -217,8 +227,51 @@ class NinjaTexture: # { print("NEED TO DEFINE A PALETTE!!!") sys.exit() - if self.data_format innot_supported: + if self.data_format in not_supported: print("THIS DATA FORMAT IS NOT SUPPORTED") sys.exit() + if self.isCompressed: + print("Reading codebook") + cb_len = 2 * 4 * self.codebook_size + self.codebook = self.file.read(cb_len) + + if self.isMipmap: + print("Seeking passed mipmaps") + seek_ofs = self.get_mipmap_size() + self.file.read(seek_ofs) + + if self.isTwiddled: + print("Reading twiddled texture") + sys.exit() + + return None + + def get_mipmap_size(self): + mipCount = 0 + seek_ofs = 0 + width = self.width + + while width : + mipCount = mipCount + 1 + width = int(width / 2) + + while mipCount: + mipWidth = (self.width >> (mipCount - 1)) + mipHeight = (self.height >> (mipCount - 1)) + mipSize = mipWidth * mipHeight + + mipCount = mipCount - 1 + + if mipCount > 0: + if self.isCompressed: + seek_ofs = seek_ofs + int(mipSize / 4) + else: + seek_ofs = seek_ofs + (2 * mipSize) + else: + if self.isCompressed: + seek_ofs = seek_ofs + 1 + else: + seek_ofs = seek_ofs + 2 + return seek_ofs