/**
 * The `Up` filter is just like the Sub() filter except that the pixel
 * immediately above the current pixel, rather than just to its left, is used
 * as the predictor.
 * `Up(x) = Raw(x) + Prior(x)`
 *
 * @param {Uint8Array} scanline raw data
 * @param {number} scanline_offset
 * @param {Uint8Array} pixels processed output
 * @param {number} offset
 * @param {number} output_offset_previous pass a negative value when there is no previous scanline
 * @param {number} length
 * @see https://www.w3.org/TR/PNG-Filters.html
 */
export function png_filter_unFilterUp(
    scanline,
    scanline_offset,
    pixels,
    offset,
    output_offset_previous,
    length
) {
    let i = 0, byte, prev;

    // Prior(x) is 0 for all x on the first scanline
    if (output_offset_previous < 0) {

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

    } else {

        for (; i < length; i++) {

            // Raw(x)
            byte = scanline[i + scanline_offset];

            // Prior(x)
            prev = pixels[output_offset_previous + i];

            pixels[offset + i] = (byte + prev) & 0xFF;
        }

    }
}
