Jump to content

Recommended Posts

  • Engineers
Posted (edited)
14 hours ago, ikskoks said:

Just curious about those RLE images from old 1997 PC game "My Friend Koo".

 

My Friend Koo RLE samples.zip 649.66 kB · 5 downloads

It's similar to PCX/PackBits, where bytes 0xC0–0xFF indicate a repetition and the remaining ones are literal bytes.
PCX-style RLE: bytes >= 0xC0 specify a repetition count (byte & 0x3F) for the following byte.

A 256-color RGB palette is located in the last 768 bytes of the file.

Standard image size is 640x480, with a short prefix/header preceding the pixel data.

TITLE_decoded.png.73186e7b822c9626c398d3768e2d5ff8.png
1STE.png.8cdbfb399f7ee1ccb07f97e025b465b7.png1STE0.png.6035112fdb358f7c7600cdd21f3ce712.png

Edited by Rabatini
  • Like 1
  • Engineers
  • Solution
Posted
def pcx_style_rle_decode(data):
    out = bytearray()
    i = 0
    n = len(data)
    while i < n:
        b = data[i]
        i += 1
        if b >= 0xC0:
            count = b & 0x3F
            value = data[i]
            i += 1
            out.extend([value] * count)
        else:
            out.append(b)
    return bytes(out)

def decode_rle_image(filepath, width=640, height=480, offset=None):
    with open(filepath, 'rb') as f:
        raw = f.read()
    palette_size = 768
    if len(raw) <= palette_size:
        raise ValueError("Arquivo muito pequeno, não tem dados de imagem + paleta.")
        
    compressed = raw[:-palette_size]
    palette = raw[-palette_size:]
    
    decoded = pcx_style_rle_decode(compressed)
    expected_pixels = width * height

    # Gambiarra caso o offset não seja informado: pega o final dos dados descompactados
    if offset is None:
        offset = len(decoded) - expected_pixels
        if offset < 0:
            offset = 0

    pixels = decoded[offset: offset + expected_pixels]
    return pixels, palette, offset


Here the essential! 

Let me know if you need the complete tool.

  • Thanks 2
Posted

Ah, it was quite simple.  I did wonder if the top 2 bits of the byte had any relevance - for example, to indicate alpha pixels.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...