Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-render

Source of release 0.6.2

0.6.2 / src / raster / alpha_runs.lucb

#==============================================================================================
#
#   alpha_runs - run-length coverage rows
#
#   DESCRIPTION:
#       Port of tiny-skia alpha_runs.rs (AlphaRuns: the sparse run-length encoded alpha of
#       one scanline, filled by the scan converters). An `AlphaRun` is Rust's
#       `Option<NonZeroU16>`: a u16 where 0 is None.
#
#==============================================================================================

type AlphaRun = u16

## Sparse array of run-length-encoded alpha (supersampling coverage) values. Sparseness
## allows us to independently compose several paths into the same AlphaRuns buffer.
struct AlphaRuns:
    var runs: AlphaRun[]
    var alpha: u8[]

    static func new_(width: u32) -> AlphaRuns:
        let runs = (new AlphaRun[(usize)width + 1] in memory.heap) else trap("raster: out of memory")
        let alpha = (new u8[(usize)width + 1] in memory.heap) else trap("raster: out of memory")
        var result = AlphaRuns(runs = runs, alpha = alpha)
        result.reset(width)
        return result

    func release():
        free(self.runs) in memory.heap
        free(self.alpha) in memory.heap

    ## Returns 0-255 given 0-256.
    static func catch_overflow(alpha: u16) -> AlphaU8:
        return (u8)(alpha - (alpha >> 8))

    ## Returns true if the scanline contains only a single run, of alpha value 0.
    func is_empty() -> bool:
        let run = self.runs[0]
        if run != 0:
            return self.alpha[0] == 0 and self.runs[(usize)run] == 0
        return true

    ## Reinitialize for a new scanline.
    func reset(width: u32):
        let run = u16(width)
        self.runs[0] = run
        self.runs[(usize)width] = 0
        self.alpha[0] = 0

    ## Insert into the buffer a run starting at (x-offset_x).
    ##
    ## if start_alpha > 0
    ##     one pixel with value += start_alpha,
    ##         max 255
    ## if middle_count > 0
    ##     middle_count pixels with value += max_value
    ## if stop_alpha > 0
    ##     one pixel with value += stop_alpha
    ##
    ## Returns the offset_x value that should be passed on the next call, assuming we're on
    ## the same scanline. If the caller is switching scanlines, then offset_x should be 0
    ## when this is called.
    func add(x0: u32, start_alpha: AlphaU8, middle_count0: usize, stop_alpha: AlphaU8, max_value: u8, offset_x: usize) -> usize:
        var x = (usize)x0
        var middle_count = middle_count0

        var runs_offset = offset_x
        var alpha_offset = offset_x
        var last_alpha_offset = offset_x
        x -= offset_x

        if start_alpha != 0:
            AlphaRuns.break_run(self.runs[runs_offset..], self.alpha[alpha_offset..], x, 1)
            # I should be able to just add alpha[x] + start_alpha. However, if the trailing
            # edge of the previous span and the leading edge of the current span round to the
            # same super-sampled x value, I might overflow to 256 with this add, hence the
            # funny subtract (crud).
            let tmp = (u16)self.alpha[alpha_offset + x] + (u16)start_alpha
            # was (tmp >> 7), but that seems wrong if we're trying to catch 256
            self.alpha[alpha_offset + x] = (u8)(tmp - (tmp >> 8))

            runs_offset += x + 1
            alpha_offset += x + 1
            x = 0

        if middle_count != 0:
            AlphaRuns.break_run(self.runs[runs_offset..], self.alpha[alpha_offset..], x, middle_count)
            alpha_offset += x
            runs_offset += x
            x = 0
            while true:
                let a = AlphaRuns.catch_overflow((u16)self.alpha[alpha_offset] + (u16)max_value)
                self.alpha[alpha_offset] = a

                let n = (usize)self.runs[runs_offset]
                alpha_offset += n
                runs_offset += n
                middle_count -= n

                if middle_count == 0:
                    break

            last_alpha_offset = alpha_offset

        if stop_alpha != 0:
            AlphaRuns.break_run(self.runs[runs_offset..], self.alpha[alpha_offset..], x, 1)
            alpha_offset += x
            self.alpha[alpha_offset] +%= stop_alpha
            last_alpha_offset = alpha_offset

        # new offset_x
        return last_alpha_offset

    ## Break the runs in the buffer at offsets x and x+count, properly updating the runs to
    ## the right and left.
    ##
    ## i.e. from the state AAAABBBB, run-length encoded as A4B4, break_run(..., 2, 5) would
    ## produce AAAABBBB rle as A2A2B3B1.
    ##
    ## Allows add() to sum another run to some of the new sub-runs. i.e. adding ..CCCCC. would
    ## produce AADDEEEB, rle as A2D2E3B1.
    static func break_run(runs: AlphaRun[], alpha: u8[], x0: usize, count: usize):
        var x = x0
        let orig_x = x
        var runs_offset: usize = 0
        var alpha_offset: usize = 0

        while x > 0:
            let n = (usize)runs[runs_offset]

            if x < n:
                alpha[alpha_offset + x] = alpha[alpha_offset]
                runs[runs_offset + 0] = (u16)x
                runs[runs_offset + x] = (u16)(n - x)
                break
            runs_offset += n
            alpha_offset += n
            x -= n

        runs_offset = orig_x
        alpha_offset = orig_x
        x = count

        while true:
            let n = (usize)runs[runs_offset]

            if x < n:
                alpha[alpha_offset + x] = alpha[alpha_offset]
                runs[runs_offset + 0] = (u16)x
                runs[runs_offset + x] = (u16)(n - x)
                break

            x -= n
            if x == 0:
                break

            runs_offset += n
            alpha_offset += n

    ## Cut (at offset x in the buffer) a run into two shorter runs with matching alpha
    ## values. Used by the RectClipBlitter to trim a RLE encoding to match the clipping
    ## rectangle.
    static func break_at(alpha: AlphaU8[], runs: AlphaRun[], x0: i32):
        var x = x0
        var alpha_i: usize = 0
        var run_i: usize = 0
        while x > 0:
            let n = runs[run_i]
            let n_usize = (usize)n
            let n_i32 = (i32)n
            if x < n_i32:
                # Skia's runs[0] and runs[x], relative to the current run (tiny-skia indexes
                # from the start of the buffer here).
                alpha[alpha_i + (usize)x] = alpha[alpha_i]
                runs[run_i] = (u16)x
                runs[run_i + (usize)x] = (u16)(n_i32 - x)
                break

            run_i += n_usize
            alpha_i += n_usize
            x -= n_i32