#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 2025/10/27
# Author: DarkRanger <darkranger.no-ip.org>
import os, sys
import zlib

from PIL import Image

IMAGE_BIN = "images.bin"
DECOMPRESS_COPY = False

def make_info(name="", width=0, height=0):
    return {"name": name, "width": width, "height": height}

def load_images():
    input_file = open(IMAGE_BIN, "rb")
    input_data = input_file.read()
    input_file.close()

    if DECOMPRESS_COPY:
        output_file = open("images", "wb")
        output_file.write(decompressed_data)
        output_file.close()

    decompressed_data = zlib.decompress(input_data)
    image_count = int.from_bytes(decompressed_data[0:4], byteorder="little", signed=False)
    print("Image count: {0}".format(image_count))
    offset = 4
    infos = []
    byte_selection = []

    for i in range(0, image_count):
        name = decompressed_data[offset:offset+64].decode("utf-8").strip("\x00")
        offset = offset + 64
        width = int.from_bytes(decompressed_data[offset:offset+4], byteorder="little", signed=False)
        offset = offset + 4
        height = int.from_bytes(decompressed_data[offset:offset+4], byteorder="little", signed=False)
        offset = offset + 4
        #print("{0:02}: {1} {2}x{3}".format(i + 1, name, width, height))
        infos.append(make_info(name=name, width=width, height=height))
    
    for i, info in enumerate(infos):
        print("{0:02}: {1} {2}x{3}".format(i + 1, info["name"], info["width"], info["height"]))
        image_size =  info["width"] * info["height"] * 4
        image_data = decompressed_data[offset:offset + image_size]
        offset = offset + image_size
        img = Image.frombytes("RGBA", (info["width"], info["height"]), image_data)
        img.save(info["name"])

if __name__ == "__main__":
    appdir = os.path.abspath(os.path.dirname(sys.argv[0]))
    os.chdir(appdir)
    load_images()
