def _png_scanline(line_bytes, fu=4, prev=None):
    """ Scanline unfiltering, taken from png.py
    """
    filter = ord(line_bytes[0:1])
    line1 = bytearray(line_bytes[1:])
    line2 = bytearray(line_bytes[1:])
    if filter == 0:
        pass
    elif filter == 1:
        ai = 0
        for i in range(fu, len(line2)):
            x = line1[i]
            a = line2[ai]
            line2[i] = x + a & 255
            ai += 1
    elif filter == 2:
        for i in range(len(line2)):
            x = line1[i]
            b = prev[i]
            line2[i] = x + b & 255
    elif filter == 3:
        ai = -fu
        for i in range(len(line2)):
            x = line1[i]
            if ai < 0:
                a = 0
            else:
                a = line2[ai]
            b = prev[i]
            line2[i] = x + (a + b >> 1) & 255
            ai += 1
    elif filter == 4:
        ai = -fu
        for i in range(len(line2)):
            x = line1[i]
            if ai < 0:
                a = c = 0
            else:
                a = line2[ai]
                c = prev[ai]
            b = prev[i]
            p = a + b - c
            pa = abs(p - a)
            pb = abs(p - b)
            pc = abs(p - c)
            if pa <= pb and pa <= pc:
                pr = a
            elif pb <= pc:
                pr = b
            else:
                pr = c
            line2[i] = x + pr & 255
            ai += 1
    else:
        raise RuntimeError('Invalid filter %r' % filter)
    return line2
