Exemple #1
0
def background(input_path, palette_path=None):

    palette_path = Palette.getGlobalPalettePath(input_path, palette_path)
    global_palette = Palette.singlePalette(palette_path)

    f = File.File(input_path)
    local_palette = Palette.readSinglePalette(f)
    palette = Palette.combinePalettes(local_palette, global_palette)

    image = Image.Image(WIDTH, HEIGHT)
    image.name = input_path.name + ".png"

    n_pixels = WIDTH * HEIGHT
    for pixel in range(n_pixels):
        colour_index = f.readUInt8()
        image.set(palette[colour_index], pixel)

    return {
        "name": input_path.name,
        "image": image,
    }
Exemple #2
0
def sprite(sprite_path, background_path, palette_path=None):

    palette_path = Palette.getGlobalPalettePath(sprite_path, palette_path)
    global_palette = Palette.singlePalette(palette_path)
    local_palette = Palette.singlePalette(background_path)
    palette = Palette.combinePalettes(local_palette, global_palette)

    f = File(sprite_path)

    spr = {
        "name": sprite_path.name,
        "blocks": [],
        "images": {},
    }

    block = _readBlockHeader(f)
    assert (block["name"] == "SPRT")
    assert (f.readUInt32() == 0x100)
    eof = block["offset"] + block["length"]

    block_offsets = []

    while f.pos() < eof:
        block = _readBlockHeader(f)

        if block["name"] == "LIST":
            block["entries"] = []
            length = f.readUInt32()
            for i in range(length):
                offset = f.readUInt32()
                block["entries"].append({"offset": block["offset"] + offset})

        elif block["name"] == "SETF":
            block["flag"] = f.readUInt32()  # 0, 1, 2, 3 or 4

        elif block["name"] == "POSN":
            block["x"] = f.readUInt32()
            block["y"] = f.readUInt32()

        elif block["name"] == "COMP":
            image = _readImage(f, block, palette)
            offset = image.pop("offset")
            block["image_offset"] = offset
            if image["image"] is not None:
                image["image"].name = _imageName(sprite_path, offset)
                spr["images"][offset] = image

        elif block["name"] == "TIME":
            block["time"] = f.readUInt32()

        elif block["name"] == "MARK":
            pass

        elif block["name"] == "MASK":
            pass

        elif block["name"] == "SCOM":
            image = _readImage(f, block, palette)
            offset = image.pop("offset")
            block["image_offset"] = offset
            if image["image"] is not None:
                image["image"].name = _imageName(sprite_path, offset)
                spr["images"][offset] = image

        elif block["name"] == "RAND":
            rand_extra = f.readUInt32()
            block["min"] = f.readUInt32()
            block["max"] = block["min"] + rand_extra

        elif block["name"] == "JUMP":
            block["index"] = f.readUInt32()

        elif block["name"] == "RPOS":
            block["dx"] = f.readSInt32()
            block["dy"] = f.readSInt32()

        elif block["name"] == "SNDF":
            sound_volume = f.readUInt32()  # 75, 95 or 100. volume?
            assert (f.readUInt32() == 0)
            sound_file = str(f.read(16))
            block["volume"] = sound_volume
            block["file"] = sound_file

        elif block["name"] == "MPOS":
            block["x"] = f.readUInt32()
            block["y"] = f.readUInt32()

        elif block["name"] == "PAUS":
            pass

        elif block["name"] == "EXIT":
            pass

        elif block["name"] == "STAT":
            pass

        elif block["name"] == "RGBP":
            palette = Palette.readFullPalette(f)

        else:
            raise ValueError("Unknown block {} at {:#x}".format(
                block["name"], block["start"]))

        block_offsets.append(block["offset"])
        block.pop("length")  # we don't need this any more
        spr["blocks"].append(block)

    for block in spr["blocks"]:
        if block["name"] == "LIST":
            for entry in block["entries"]:
                entry["index"] = block_offsets.index(entry["offset"])

    return spr