/**
 * The `Sub` filter transmits the difference between each byte and the value
 * of the corresponding byte of the prior pixel.
 * `Sub(x) = Raw(x) + Raw(x - bpp)`
 *
 * @param {Uint8Array} scanline raw data
 * @param {number} scanline_offset
 * @param {Uint8Array} pixels processed output
 * @param {number} bpp bytes-per-pixel
 * @param {number} offset
 * @param {number} length
 * @see https://www.w3.org/TR/PNG-Filters.html
 */
export function png_filter_unFilterSub(
    scanline,
    scanline_offset,
    pixels,
    bpp,
    offset,
    length
) {

    let i = 0;

    for (; i < bpp; i++) {
        pixels[offset + i] = scanline[i + scanline_offset];
    }

    for (; i < length; i++) {
        // Raw(x) + Raw(x - bpp)
        const of_i = offset + i;
        pixels[of_i] = (scanline[i + scanline_offset] + pixels[of_i - bpp]) & 0xFF;
    }
}
