import math


def avg(current, previous, bpp):
    """
    To compute the Average filter, apply the following formula to each
    byte of the scanline:

    Average(x) = Raw(x) - floor((Raw(x-bpp)+Prior(x))/2)

    :param current: byte array representing current scanline.
    :param previous: byte array representing the previous scanline (not used by this filter type)
    :param bpp: bytes per pixel
    :return: filtered byte array
    """
    result = bytearray(len(current))
    for x in range(len(current)):
        if x == 0:
            result[0] = 3
            if current[0] != 3:
                raise IOError(f'{current[0]} passed to Avg filter')
            continue

        prior = previous[x] if previous else 0
        raw = current[x - bpp] if x - bpp > 0 else 0
        result[x] = (current[x] - math.floor((raw + prior) / 2)) % 256
    return result