def load(f):
    bitmaps = []
    palette = []
    table = []
    f.seek(3)
    version = f.read(3)
    if ((version != b'89a') and (version != b'87a')):
        raise RuntimeError('Invalid GIF version')
    width = int.from_bytes(f.read(2), 'little')
    height = int.from_bytes(f.read(2), 'little')
    gct_header = int.from_bytes(f.read(1), 'little')
    if ((gct_header & 128) != 128):
        raise NotImplementedError('Only gifs with a global color table are supported')
    gct_size = (2 ** ((gct_header & 7) + 1))
    bg_color_index = int.from_bytes(f.read(1), 'little')
    f.seek(1, 1)
    for i in range(gct_size):
        color = f.read(3)
        palette.append(color)
    while True:
        separator = f.read(1)
        if separator:
            separator = int.from_bytes(separator, 'little')
            if (separator == 33):
                label = int.from_bytes(f.read(1), 'little')
                if (label == 249):
                    print('Graphic Control Extension')
                    f.seek(1, 1)
                    packed = int.from_bytes(f.read(1), 'little')
                    delay = (int.from_bytes(f.read(2), 'little') / 100)
                    if ((packed & 1) == 1):
                        transparency_index = int.from_bytes(f.read(1), 'little')
                    else:
                        f.seek(1, 1)
                    f.seek(1, 1)
                elif (label == 255):
                    print('Application Extension')
                    f.seek(1, 1)
                    application = f.read(8)
                    if (application == b'NETSCAPE'):
                        f.seek(5, 1)
                        loop_count = int.from_bytes(f.read(2), 'little')
                        f.seek(1, 1)
                    else:
                        raise NotImplementedError(('Unimplemented application extension: ' + ''.join([chr(b) for b in application])))
                elif (label == 254):
                    comment = b''
                    while (not comment.endswith(b'\x00')):
                        byte = f.read(1)
                        comment += byte
                    comment = ''.join([chr(b) for b in comment])
                    print(comment)
                else:
                    raise NotImplementedError(('Unimplemented extension: ' + hex(label)))
            elif (separator == 44):
                print('Image Descriptor')
                image_start_x = int.from_bytes(f.read(2), 'little')
                image_start_y = int.from_bytes(f.read(2), 'little')
                image_width = int.from_bytes(f.read(2), 'little')
                image_height = int.from_bytes(f.read(2), 'little')
                f.seek(1, 1)
                print('Image Data')
                lzw_code_size = int.from_bytes(f.read(1), 'little')
                compressed = bytearray()
                while True:
                    block_size = int.from_bytes(f.read(1), 'little')
                    if (block_size == 0):
                        break
                    compressed += f.read(block_size)
                bitmap = decompress(compressed, lzw_code_size)
                bitmaps.append(bitmap)
            elif (separator == 59):
                break
            else:
                raise RuntimeError(('Got an unexpected separator: ' + hex(separator)))
def load(f):
    bitmaps = []
    palette = []
    table = []
    f.seek(3)
    version = f.read(3)
    if ((version != b'89a') and (version != b'87a')):
        raise RuntimeError('Invalid GIF version')
    width = int.from_bytes(f.read(2), 'little')
    height = int.from_bytes(f.read(2), 'little')
    gct_header = int.from_bytes(f.read(1), 'little')
    if ((gct_header & 128) != 128):
        raise NotImplementedError('Only gifs with a global color table are supported')
    gct_size = (2 ** ((gct_header & 7) + 1))
    bg_color_index = int.from_bytes(f.read(1), 'little')
    f.seek(1, 1)
    for i in range(gct_size):
        color = f.read(3)
        palette.append(color)
    while True:
        separator = f.read(1)
        if separator:
            separator = int.from_bytes(separator, 'little')
            if (separator == 33):
                label = int.from_bytes(f.read(1), 'little')
                if (label == 249):
                    print('Graphic Control Extension')
                    f.seek(1, 1)
                    packed = int.from_bytes(f.read(1), 'little')
                    delay = (int.from_bytes(f.read(2), 'little') / 100)
                    if ((packed & 1) == 1):
                        transparency_index = int.from_bytes(f.read(1), 'little')
                    else:
                        f.seek(1, 1)
                    f.seek(1, 1)
                elif (label == 255):
                    print('Application Extension')
                    f.seek(1, 1)
                    application = f.read(8)
                    if (application == b'NETSCAPE'):
                        f.seek(5, 1)
                        loop_count = int.from_bytes(f.read(2), 'little')
                        f.seek(1, 1)
                    else:
                        raise NotImplementedError(('Unimplemented application extension: ' + .join([chr(b) for b in application])))
                elif (label == 254):
                    comment = b
                    while (not comment.endswith(b'\x00')):
                        byte = f.read(1)
                        comment += byte
                    comment = .join([chr(b) for b in comment])
                    print(comment)
                else:
                    raise NotImplementedError(('Unimplemented extension: ' + hex(label)))
            elif (separator == 44):
                print('Image Descriptor')
                image_start_x = int.from_bytes(f.read(2), 'little')
                image_start_y = int.from_bytes(f.read(2), 'little')
                image_width = int.from_bytes(f.read(2), 'little')
                image_height = int.from_bytes(f.read(2), 'little')
                f.seek(1, 1)
                print('Image Data')
                lzw_code_size = int.from_bytes(f.read(1), 'little')
                compressed = bytearray()
                while True:
                    block_size = int.from_bytes(f.read(1), 'little')
                    if (block_size == 0):
                        break
                    compressed += f.read(block_size)
                bitmap = decompress(compressed, lzw_code_size)
                bitmaps.append(bitmap)
            elif (separator == 59):
                break
            else:
                raise RuntimeError(('Got an unexpected separator: ' + hex(separator)))