From 96298d8ecf5dce0d9649451a241e3510a029f9d2 Mon Sep 17 00:00:00 2001 From: Jennifer Taylor Date: Fri, 6 Nov 2020 19:57:35 +0000 Subject: [PATCH] Fix T*BB font decoding issue by setting the correct endianness. --- bemani/format/dxt.py | 24 ++++++++++++------------ bemani/utils/afputils.py | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/bemani/format/dxt.py b/bemani/format/dxt.py index d06651b..5c8c207 100644 --- a/bemani/format/dxt.py +++ b/bemani/format/dxt.py @@ -13,14 +13,14 @@ import struct from typing import List, Optional, Tuple -def unpack(_bytes: bytes) -> int: +def unpack(endian: str, _bytes: bytes) -> int: STRUCT_SIGNS = { 1: 'B', 2: 'H', 4: 'I', 8: 'Q' } - return struct.unpack('>' + STRUCT_SIGNS[len(_bytes)], _bytes)[0] + return struct.unpack(endian + STRUCT_SIGNS[len(_bytes)], _bytes)[0] # This function converts RGB565 format to raw pixels @@ -46,23 +46,23 @@ class DXTBuffer: self.decompressed_buffer: List[Optional[bytes]] = [None] * ((width * height) * 2) # Dont ask me why - def DXT5Decompress(self, filedata: bytes) -> bytes: + def DXT5Decompress(self, filedata: bytes, endian: str = "<") -> bytes: # Loop through each block and decompress it file = io.BytesIO(filedata) for row in range(self.block_county): for col in range(self.block_countx): # Get the alpha values - a0 = unpack(file.read(1)) - a1 = unpack(file.read(1)) + a0 = unpack(endian, file.read(1)) + a1 = unpack(endian, file.read(1)) atable = file.read(6) acode0 = atable[2] | (atable[3] << 8) | (atable[4] << 16) | (atable[5] << 24) acode1 = atable[0] | (atable[1] << 8) # Color 1 color 2, color look up table - c0 = unpack(file.read(2)) - c1 = unpack(file.read(2)) - ctable = unpack(file.read(4)) + c0 = unpack(endian, file.read(2)) + c1 = unpack(endian, file.read(2)) + ctable = unpack(endian, file.read(4)) # The 4x4 Lookup table loop for j in range(4): @@ -81,16 +81,16 @@ class DXTBuffer: return b''.join([x for x in self.decompressed_buffer if x is not None]) - def DXT1Decompress(self, filedata: bytes) -> bytes: + def DXT1Decompress(self, filedata: bytes, endian: str = "<") -> bytes: # Loop through each block and decompress it file = io.BytesIO(filedata) for row in range(self.block_county): for col in range(self.block_countx): # Color 1 color 2, color look up table - c0 = unpack(file.read(2)) - c1 = unpack(file.read(2)) - ctable = unpack(file.read(4)) + c0 = unpack(endian, file.read(2)) + c1 = unpack(endian, file.read(2)) + ctable = unpack(endian, file.read(4)) # The 4x4 Lookup table loop for j in range(4): diff --git a/bemani/utils/afputils.py b/bemani/utils/afputils.py index c5def58..d41b09d 100644 --- a/bemani/utils/afputils.py +++ b/bemani/utils/afputils.py @@ -283,7 +283,7 @@ def extract( img = Image.frombuffer( 'RGBA', (width, height), - dxt.DXT5Decompress(raw_data[64:]), + dxt.DXT5Decompress(raw_data[64:], endian=endian), 'raw', 'RGBA', 0,