Last active
April 17, 2021 00:52
-
-
Save bmaia/466e6a332f9ffe96b1d13c3f2f21b463 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sys | |
# | |
# NBA Jam/Midway IMG format parser | |
# https://github.com/historicalsource/nba-jam | |
# | |
if __name__ == '__main__': | |
filename = sys.argv[1] | |
with open(filename, "rb") as fp: | |
print('=================') | |
print('image information') | |
print('=================') | |
# total images in file | |
num_images = int.from_bytes(fp.read(1), 'little') | |
print('total images: ',num_images) | |
# img list pointer | |
fp.seek(4) | |
file_list_offset = bytearray(fp.read(4)) | |
# we need to reverse the order (little endian) | |
offset = int.from_bytes(file_list_offset, byteorder='little') | |
fp.seek(offset) | |
# for each of the images, read 50 bytes to retrieve file names | |
while num_images > 0: | |
# print(hex(fp.tell())) | |
image_metadata = fp.read(50) | |
file_name_length = image_metadata.find(b'\00') | |
print(image_metadata[0:file_name_length].decode('utf-8')) | |
num_images -= 1 | |
# palette metadata is stored right after the image metadata | |
# in blocks of 26 bytes with the same delimiter on offset[25] | |
print('===================') | |
print('palette information') | |
print('===================') | |
palette_metadata = fp.read(26) | |
delimiter = palette_metadata[25] | |
while True: | |
# print(hex(fp.tell())) | |
if len(palette_metadata) == 26: | |
if palette_metadata[25] == delimiter: | |
palette_name_length = palette_metadata.find(b'\00') | |
print(palette_metadata[0:palette_name_length].decode('utf-8')) | |
palette_offset = int.from_bytes(palette_metadata[14:18], byteorder='little') | |
print('offset: ', hex(palette_offset)) | |
palette_metadata = fp.read(26) | |
else: | |
break | |
else: | |
break |
Author
bmaia
commented
Apr 17, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment