|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import struct |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | + |
| 8 | +def rgb565_to_rgb888(value): |
| 9 | + red5 = (value >> 11) & 0x1F |
| 10 | + green6 = (value >> 5) & 0x3F |
| 11 | + blue5 = value & 0x1F |
| 12 | + return ( |
| 13 | + (red5 * 255 + 15) // 31, |
| 14 | + (green6 * 255 + 31) // 63, |
| 15 | + (blue5 * 255 + 15) // 31, |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +def parse_im8c_var(path): |
| 20 | + data = path.read_bytes() |
| 21 | + if data[:8] != b"**TI83F*": |
| 22 | + raise ValueError(f"{path} is not a TI var file") |
| 23 | + |
| 24 | + if len(data) < 74: |
| 25 | + raise ValueError(f"{path} is too short to contain an IM8C payload") |
| 26 | + |
| 27 | + data_length = data[57] | (data[58] << 8) |
| 28 | + payload = data[74:72 + data_length] |
| 29 | + if payload[:4] != b"IM8C": |
| 30 | + raise ValueError(f"{path} does not contain an IM8C payload") |
| 31 | + |
| 32 | + pos = 4 |
| 33 | + width = payload[pos] | (payload[pos + 1] << 8) | (payload[pos + 2] << 16) |
| 34 | + pos += 3 |
| 35 | + height = payload[pos] | (payload[pos + 1] << 8) | (payload[pos + 2] << 16) |
| 36 | + pos += 3 |
| 37 | + |
| 38 | + palette_marker = payload[pos] |
| 39 | + pos += 1 |
| 40 | + if palette_marker != 0x01: |
| 41 | + raise ValueError(f"Unsupported palette marker 0x{palette_marker:02X}") |
| 42 | + |
| 43 | + has_alpha = payload[pos] != 0 |
| 44 | + pos += 1 |
| 45 | + transparent_index = payload[pos] |
| 46 | + pos += 1 |
| 47 | + |
| 48 | + palette_count = payload[pos] |
| 49 | + pos += 1 |
| 50 | + if palette_count == 0: |
| 51 | + palette_count = 256 |
| 52 | + |
| 53 | + palette = [] |
| 54 | + for _ in range(palette_count): |
| 55 | + palette.append(payload[pos] | (payload[pos + 1] << 8)) |
| 56 | + pos += 2 |
| 57 | + |
| 58 | + return { |
| 59 | + "width": width, |
| 60 | + "height": height, |
| 61 | + "has_alpha": has_alpha, |
| 62 | + "transparent_index": transparent_index, |
| 63 | + "palette": palette, |
| 64 | + "compressed_image_data": payload[pos:], |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | +def decode_im8c_rle(width, height, compressed_image_data): |
| 69 | + expected_pixels = width * height |
| 70 | + pixels = bytearray() |
| 71 | + pos = 0 |
| 72 | + |
| 73 | + while pos < len(compressed_image_data) and len(pixels) < expected_pixels: |
| 74 | + control = compressed_image_data[pos] |
| 75 | + pos += 1 |
| 76 | + |
| 77 | + if control & 0x80: |
| 78 | + if pos >= len(compressed_image_data): |
| 79 | + raise ValueError("Truncated IM8C RLE run") |
| 80 | + |
| 81 | + run_length = (control & 0x7F) + 2 |
| 82 | + palette_index = compressed_image_data[pos] |
| 83 | + pos += 1 |
| 84 | + pixels.extend([palette_index] * min(run_length, expected_pixels - len(pixels))) |
| 85 | + else: |
| 86 | + literal_length = control + 1 |
| 87 | + available = min(literal_length, len(compressed_image_data) - pos, expected_pixels - len(pixels)) |
| 88 | + pixels.extend(compressed_image_data[pos:pos + available]) |
| 89 | + pos += available |
| 90 | + |
| 91 | + if len(pixels) < expected_pixels: |
| 92 | + pixels.extend([0] * (expected_pixels - len(pixels))) |
| 93 | + |
| 94 | + return pixels |
| 95 | + |
| 96 | + |
| 97 | +def indices_to_rgba(width, height, indices, palette, has_alpha, transparent_index): |
| 98 | + rgba = bytearray() |
| 99 | + for y in range(height): |
| 100 | + for x in range(width): |
| 101 | + palette_index = indices[y * width + x] |
| 102 | + if has_alpha and palette_index == transparent_index: |
| 103 | + rgba.extend((0, 0, 0, 0)) |
| 104 | + else: |
| 105 | + rgba.extend((*rgb565_to_rgb888(palette[palette_index]), 255)) |
| 106 | + return rgba |
| 107 | + |
| 108 | + |
| 109 | +def make_bmp(width, height, rgba_pixels): |
| 110 | + row_stride = width * 4 |
| 111 | + pixel_array_size = row_stride * height |
| 112 | + pixel_offset = 14 + 124 |
| 113 | + file_size = pixel_offset + pixel_array_size |
| 114 | + |
| 115 | + bmp = bytearray() |
| 116 | + bmp.extend(b"BM") |
| 117 | + bmp.extend(struct.pack("<IHHI", file_size, 0, 0, pixel_offset)) |
| 118 | + bmp.extend(struct.pack("<IIIHHIIIIII", 124, width, height, 1, 32, 3, pixel_array_size, 2835, 2835, 0, 0)) |
| 119 | + bmp.extend(struct.pack("<IIIII", 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000, 0x73524742)) |
| 120 | + bmp.extend(b"\x00" * 36) |
| 121 | + bmp.extend(struct.pack("<III", 0, 0, 0)) |
| 122 | + bmp.extend(struct.pack("<IIII", 0, 0, 0, 0)) |
| 123 | + |
| 124 | + for row in range(height): |
| 125 | + src_y = height - 1 - row |
| 126 | + row_start = src_y * width * 4 |
| 127 | + for x in range(width): |
| 128 | + src = row_start + x * 4 |
| 129 | + bmp.extend((rgba_pixels[src + 2], rgba_pixels[src + 1], rgba_pixels[src], rgba_pixels[src + 3])) |
| 130 | + |
| 131 | + return bmp |
| 132 | + |
| 133 | + |
| 134 | +def main(): |
| 135 | + parser = argparse.ArgumentParser(description="Decode a TI Python IM8C AppVar into a BMP preview.") |
| 136 | + parser.add_argument("input", type=Path, help="Input .8xv file containing an IM8C payload") |
| 137 | + parser.add_argument("output", type=Path, nargs="?", help="Output BMP path (defaults next to the input)") |
| 138 | + args = parser.parse_args() |
| 139 | + |
| 140 | + output = args.output if args.output is not None else args.input.with_suffix(".bmp") |
| 141 | + |
| 142 | + parsed = parse_im8c_var(args.input) |
| 143 | + indices = decode_im8c_rle(parsed["width"], parsed["height"], parsed["compressed_image_data"]) |
| 144 | + rgba_pixels = indices_to_rgba( |
| 145 | + parsed["width"], |
| 146 | + parsed["height"], |
| 147 | + indices, |
| 148 | + parsed["palette"], |
| 149 | + parsed["has_alpha"], |
| 150 | + parsed["transparent_index"], |
| 151 | + ) |
| 152 | + bmp = make_bmp(parsed["width"], parsed["height"], rgba_pixels) |
| 153 | + output.write_bytes(bmp) |
| 154 | + print(f"Wrote {output} ({parsed['width']}x{parsed['height']})") |
| 155 | + |
| 156 | + |
| 157 | +if __name__ == "__main__": |
| 158 | + main() |
0 commit comments