#!/usr/bin/env python3 """Decodes a Wallpaper Engine .tex file's first mipmap into a plain image file. Only handles the common case: TEXV0005/TEXI0001 header, TEXB0003/TEXB0004 container, FreeImage-encoded payload (FIF_PNG in every sample seen so far) - i.e. the mipmap bytes are themselves a complete PNG/etc file, not raw pixel data. Doesn't touch mipmaps beyond the first, animated (TEXS*) textures, or DXT/BC-compressed formats. Usage: decode_tex.py Needs: pip install lz4 (only when the mipmap is LZ4-compressed, i.e. most of them). """ import struct import sys try: import lz4.block except ImportError: lz4 = None def decode(path, out_path): f = open(path, 'rb') magic1 = f.read(9) assert magic1[:8] == b'TEXV0005', magic1 magic2 = f.read(9) assert magic2[:8] == b'TEXI0001', magic2 fmt, flags, tex_w, tex_h, w, h = struct.unpack('<6I', f.read(24)) f.read(4) # ignored trailing header field container_magic = f.read(9) image_count = struct.unpack('