'Parse image from file-like object containing ico file data'

def __init__(self, buf):

    s = buf.read(6)
    if (not _accept(s)):
        raise SyntaxError('not    an    ICO    file')
    self.buf = buf
    self.entry = []
    self.nb_items = i16(s[4:])
    for i in range(self.nb_items):
        s = buf.read(16)
        icon_header = {'width': i8(s[0]), 'height': i8(s[1]), 'nb_color': i8(s[2]), 'reserved': i8(s[3]), 'planes': i16(s[4:]), 'bpp': i16(s[6:]), 'size': i32(s[8:]), 'offset': i32(s[12:])}
        for j in ('width', 'height'):
            if (not icon_header[j]):
                icon_header[j] = 256
        icon_header['color_depth'] = (icon_header['bpp'] or ((icon_header['nb_color'] != 0) and ceil(log(icon_header['nb_color'], 2))) or 256)
        icon_header['dim'] = (icon_header['width'], icon_header['height'])
        icon_header['square'] = (icon_header['width'] * icon_header['height'])
        self.entry.append(icon_header)
    self.entry = sorted(self.entry, key=(lambda x: x['color_depth']))
    self.entry = sorted(self.entry, key=(lambda x: x['square']))
    self.entry.reverse()
