def _layerinfo(file):
    # read layerinfo block
    layers = []
    read = file.read

    for i in range(abs(i16(read(2)))):

        # bounding box
        y0 = i32(read(4)); x0 = i32(read(4))
        y1 = i32(read(4)); x1 = i32(read(4))

        # image info
        info = []
        mode = []
        for i in range(i16(read(2))):
            type = i16(read(2))
            if type == 65535:
                m = "A"
            else:
                m = "RGB"[type]
            mode.append(m)
            size = i32(read(4))
            info.append((m, size))

        # figure out the image mode
        mode.sort()
        if mode == ["R"]:
            mode = "L"
        elif mode == ["B", "G", "R"]:
            mode = "RGB"
        elif mode == ["A", "B", "G", "R"]:
            mode = "RGBA"
        else:
            mode = None # unknown

        # skip over blend flags and extra information
        filler = read(12)
        name = ""
        size = i32(read(4))
        combined = 0
        if size:
            length = i32(read(4))
            if length:
                mask_y = i32(read(4)); mask_x = i32(read(4))
                mask_h = i32(read(4)) - mask_y; mask_w = i32(read(4)) - mask_x
                file.seek(length - 16, 1)
            combined += length + 4

            length = i32(read(4))
            if length:
                file.seek(length, 1)
            combined += length + 4

            length = ord(read(1))
            if length:
                name = read(length)
            combined += length + 1

        file.seek(size - combined, 1)
        layers.append((name, mode, (x0, y0, x1, y1)))

    # get tiles
    i = 0
    for name, mode, bbox in layers:
        tile = []
        for m in mode:
            t = _maketile(file, m, bbox, 1)
            if t:
                tile.extend(t)
        layers[i] = name, mode, bbox, tile
        i = i + 1

    return layers
dataset/ETHPy150Open kleientertainment/ds_mod_tools/pkg/win32/Python27/Lib/site-packages/PIL/PsdImagePlugin.py/_layerinfo/original