Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-raw

Source of release 0.8.0

0.8.0 / src / raw / crx_top.lucb

#==============================================================================================
#
#   raw/crx_top - CRX: first lines and lines without a reference
#
#   DESCRIPTION:
#       The first line of a subband has no line above: it predicts from the left
#       only. Subbands that are not partially decodable (wavelet detail bands)
#       never predict from the line above; they keep, per column, the `k` their
#       previous line used. `crx_decode_line` picks the variant for a subband's
#       next line and copies the result into its band buffer.
#
#==============================================================================================

## A line of a band without a reference line: crxDecodeLineNoRefPrevLine.
func crx_no_reference_line(p: CrxLine*) -> !:
    let w = p.width
    let l0 = p.l0
    let l1 = p.l1
    var i: i32 = 0
    while i < w - 1:
        let at = (i64)i
        if (crx_get(p, l0 + at + 2) | crx_get(p, l0 + at + 1) | crx_get(p, l1 + at)) != 0:
            let code = crx_code(p)
            crx_set(p, l1 + at + 1, crx_signed(code))
            p.k = crx_predict_k(p.k, code, 0)
            crx_progress_k(p, at + 1)
        else:
            var count: i32 = 0
            if fuji_read(&p.reader, 1) != 0:
                count = 1
                if i != w - 1:
                    while fuji_read(&p.reader, 1) != 0:
                        count += (i32)crx_run_steps[(usize)p.s]
                        if i + count > w:
                            count = w - i
                            break
                        if p.s < 31:
                            p.s += 1
                        if i + count == w:
                            break
                    if i + count < w:
                        if crx_run_bits[(usize)p.s] > 0:
                            count += fuji_read(&p.reader, (i32)crx_run_bits[(usize)p.s])
                        if p.s > 0:
                            p.s -= 1
                    if i + count > w:
                        p.reader.fault = true
                        count = w - i
            if count > 0:
                for j in 0..<(i64)count:
                    crx_set(p, l1 + at + 1 + j, 0)
                    p.progress[(usize)(p.l2 + at + j)] = 0
                i += count
            let next = (i64)i
            if i >= w - 1:
                if i == w - 1:
                    let code = crx_code(p)
                    crx_set(p, l1 + next + 1, crx_signed(code + 1))
                    p.k = crx_predict_k(p.k, code, 15)
                    p.progress[(usize)(p.l2 + next)] = p.k
                # C's `continue` still steps the loop counter.
                i += 1
                continue
            let code = crx_code(p)
            crx_set(p, l1 + next + 1, crx_signed(code + 1))
            p.k = crx_predict_k(p.k, code, 0)
            crx_progress_k(p, next + 1)
        p.progress[(usize)(p.l2 + (i64)i)] = p.k
        i += 1
    if i == w - 1:
        let code = crx_code(p)
        crx_set(p, l1 + (i64)i + 1, crx_signed(code))
        p.k = crx_predict_k(p.k, code, 15)
        p.progress[(usize)(p.l2 + (i64)i)] = p.k

## The adjustment of `k` towards the previous line's at column `at`.
inline func crx_progress_k(p: CrxLine*, at: i64):
    if p.progress[(usize)(p.l2 + at)] - p.k <= 1:
        if p.k >= 15:
            p.k = 15
    else:
        p.k += 1

## The first line of a band with reference lines: crxDecodeTopLine.
func crx_top_line(p: CrxLine*) -> !:
    crx_set(p, p.l1, 0)
    var length = p.width
    while length > 1:
        if crx_get(p, p.l1) != 0:
            crx_set(p, p.l1 + 1, crx_get(p, p.l1))
        else:
            let count = crx_run(p, length)
            length -= count
            for _ in 0..<count:
                crx_set(p, p.l1 + 1, crx_get(p, p.l1))
                p.l1 += 1
            if count > 0 and length <= 0:
                break
            crx_set(p, p.l1 + 1, 0)
        let code = crx_code(p)
        crx_set(p, p.l1 + 1, crx_get(p, p.l1 + 1) +% crx_signed(code))
        p.k = crx_predict_k(p.k, code, 15)
        p.l1 += 1
        length -= 1
    if length == 1:
        crx_set(p, p.l1 + 1, crx_get(p, p.l1))
        let code = crx_code(p)
        crx_set(p, p.l1 + 1, crx_get(p, p.l1 + 1) +% crx_signed(code))
        p.k = crx_predict_k(p.k, code, 15)
        p.l1 += 1
    crx_set(p, p.l1 + 1, crx_get(p, p.l1) +% 1)

## The first line of a rounded band: crxDecodeTopLineRounded.
func crx_rounded_top_line(p: CrxLine*) -> !:
    crx_set(p, p.l1, 0)
    var length = p.width
    while length > 1:
        if crx_abs(crx_get(p, p.l1)) > p.rounded_mask:
            crx_set(p, p.l1 + 1, crx_get(p, p.l1))
        else:
            let count = crx_run(p, length)
            length -= count
            for _ in 0..<count:
                crx_set(p, p.l1 + 1, crx_get(p, p.l1))
                p.l1 += 1
            if length <= 0:
                break
            crx_set(p, p.l1 + 1, 0)
        let code = crx_code(p)
        let value = crx_signed(code)
        crx_set(p, p.l1 + 1, crx_get(p, p.l1 + 1) +% p.rounded_mask *% 2 *% value +% (value >> 31))
        p.k = crx_predict_k(p.k, code, 15)
        p.l1 += 1
        length -= 1
    if length == 1:
        let code = crx_code(p)
        let value = crx_signed(code)
        crx_set(p, p.l1 + 1, crx_get(p, p.l1 + 1) +% p.rounded_mask *% 2 *% value +% (value >> 31))
        p.k = crx_predict_k(p.k, code, 15)
        p.l1 += 1
    crx_set(p, p.l1 + 1, crx_get(p, p.l1) +% 1)

## The first line of a band without reference lines:
## crxDecodeTopLineNoRefPrevLine.
func crx_no_reference_top_line(p: CrxLine*) -> !:
    crx_set(p, p.l0, 0)
    crx_set(p, p.l1, 0)
    var length = p.width
    while length > 1:
        if crx_get(p, p.l1) != 0:
            let code = crx_code(p)
            crx_set(p, p.l1 + 1, crx_signed(code))
            p.k = crx_predict_k(p.k, code, 15)
        else:
            let count = crx_run(p, length)
            length -= count
            for _ in 0..<count:
                p.progress[(usize)p.l2] = 0
                crx_set(p, p.l1 + 1, 0)
                p.l1 += 1
                p.l2 += 1
            if length <= 0:
                break
            let code = crx_code(p)
            crx_set(p, p.l1 + 1, crx_signed(code + 1))
            p.k = crx_predict_k(p.k, code, 15)
        p.progress[(usize)p.l2] = p.k
        p.l2 += 1
        p.l1 += 1
        length -= 1
    if length == 1:
        let code = crx_code(p)
        crx_set(p, p.l1 + 1, crx_signed(code))
        p.k = crx_predict_k(p.k, code, 15)
        p.progress[(usize)p.l2] = p.k
        p.l1 += 1
    crx_set(p, p.l1 + 1, 0)

## Decode the band's next line into `out` (its `width` values).
func crx_decode_line(p: CrxLine*, out: i32[]) -> !:
    if p.line >= p.height:
        error(corrupt, "CR3 subband read past its last line")
    let stride = (i64)p.width + 2
    if p.line == 0:
        p.s = 0
        p.k = 0
        p.l0 = 0
        p.l1 = stride
        p.l2 = 0
    else:
        p.l0 = stride if (p.line & 1) != 0 else 0
        p.l1 = 0 if (p.line & 1) != 0 else stride
        p.l2 = 0
    let start = (usize)p.l1 + 1
    if p.line == 0:
        if not p.partial:
            try crx_no_reference_top_line(p)
        elif p.rounded_mask <= 0:
            try crx_top_line(p)
        else:
            p.rounded_bits = 1
            if (p.rounded_mask & ~1) != 0:
                while (p.rounded_mask >> p.rounded_bits) != 0:
                    p.rounded_bits += 1
            try crx_rounded_top_line(p)
    elif not p.partial:
        try crx_no_reference_line(p)
    elif p.rounded_mask <= 0:
        try crx_line(p)
    else:
        try crx_rounded_line(p)
    if p.reader.fault:
        error(corrupt, "CR3 data is invalid or truncated")
    memory.copy(out, p.data[start..], (usize)p.width)
    p.line += 1