Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-render

Source of release 0.6.2

0.6.2 / src / raster / mipmap.lucb

#==============================================================================================
#
#   mipmap - the mip levels of an image and the level a scale samples
#
#   DESCRIPTION:
#       Port of Skia m144's SkMipmap (src/core/SkMipmap.cpp: ComputeLevelCount, Build,
#       ComputeLevel) with its HQ downsampler for 8888 pixels
#       (src/core/SkMipmapHQDownSampler.cpp: a 2x box filter, or a 1-2-1 triangle filter
#       along an odd dimension, summed in 16-bit lanes and shifted down), and of
#       SkMipmapAccessor's choice of levels (src/core/SkMipmapAccessor.cpp) for linear
#       mipmap sampling. Levels halve each dimension down to 1x1, flooring odd sizes.
#
#       The GPU player samples the same levels: mipmap_pack writes them beside the base image
#       (level 1 to its right, each next level below the one before), mipmap_level_rect says
#       where, and mipmap_choice picks the levels and their weight as the pattern does.
#
#       Copyright 2015 Google Inc.
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-3-Clause (see LICENSE-skia)
#
#==============================================================================================

## SkMipmap::ComputeLevelCount: the levels below the base, down to 1x1.
func mipmap_level_count(width: u32, height: u32) -> usize:
    let largest = width if width > height else height
    if largest < 2:
        return 0
    var significant_bits: usize = 0
    var v = largest
    while v != 0:
        significant_bits += 1
        v >>= 1
    return significant_bits - 1

## The four channels of a pixel as 16-bit lanes (ColorTypeFilter_8888::Expand).
func mip_expand(p: PremultipliedColorU8) -> u32[4]:
    return [(u32)p.red(), (u32)p.green(), (u32)p.blue(), (u32)p.alpha()]

## ColorTypeFilter_8888::Compact of `sum` shifted right by `bits` (the cast to bytes keeps the
## low 8 bits).
func mip_compact(sum: u32[4], bits: u32) -> PremultipliedColorU8:
    return PremultipliedColorU8.from_rgba_unchecked((u8)((sum[0] >> bits) & 0xff), (u8)((sum[1] >> bits) & 0xff), (u8)((sum[2] >> bits) & 0xff), (u8)((sum[3] >> bits) & 0xff))

## a + b + b + c, lane by lane (add_121).
func mip_add_121(a: u32[4], b: u32[4], c: u32[4]) -> u32[4]:
    return [a[0] + b[0] + b[0] + c[0], a[1] + b[1] + b[1] + c[1], a[2] + b[2] + b[2] + c[2], a[3] + b[3] + b[3] + c[3]]

func mip_add(a: u32[4], b: u32[4]) -> u32[4]:
    return [a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]]

## HQDownSampler::buildLevel: `dst` (half of `src`, floored) from `src`, with the filter that
## covers `src`'s odd or even width and height.
func mip_build_level(dst: Pixmap*, src: PixmapRef):
    let width = (usize)src.width()
    let height = (usize)src.height()
    let s = src.pixels()
    let d = dst.pixels_mut()
    let dst_width = (usize)dst.width()
    # The taps along each axis: 1 (a dimension of 1), 2 (even) or 3 (odd).
    let taps_x: usize = 1 if width == 1 else (3 if width & 1 == 1 else 2)
    let taps_y: usize = 1 if height == 1 else (3 if height & 1 == 1 else 2)
    for y in 0..<(usize)dst.height():
        let row0 = 2 * y * width
        for x in 0..<dst_width:
            let x0 = 2 * x
            # The columns of the taps, summed down (1-2-1 for three rows).
            var columns: u32[3][4] = ---
            for i in 0..<taps_x:
                let c0 = mip_expand(s[row0 + x0 + i])
                if taps_y == 1:
                    columns[i] = c0
                elif taps_y == 2:
                    columns[i] = mip_add(c0, mip_expand(s[row0 + width + x0 + i]))
                else:
                    columns[i] = mip_add_121(c0, mip_expand(s[row0 + width + x0 + i]), mip_expand(s[row0 + 2 * width + x0 + i]))
            var sum = columns[0]
            if taps_x == 2:
                sum = mip_add(columns[0], columns[1])
            elif taps_x == 3:
                if taps_y == 3:
                    # downsample_3_3: a + (add_121 of the middle column << 1) + c.
                    let b = columns[1]
                    sum = mip_add(mip_add(columns[0], [b[0] << 1, b[1] << 1, b[2] << 1, b[3] << 1]), columns[2])
                elif taps_y == 2:
                    # downsample_3_2: a + (b0 + b0 + b1 + b1) + c.
                    let b = columns[1]
                    sum = mip_add(mip_add(columns[0], mip_add(b, b)), columns[2])
                else:
                    sum = mip_add_121(columns[0], columns[1], columns[2])
            # The weights add up to 2^shift: one bit per 2-tap axis, two per 3-tap axis.
            var shift: u32 = 0
            shift += 1 if taps_x == 2 else (2 if taps_x == 3 else 0)
            shift += 1 if taps_y == 2 else (2 if taps_y == 3 else 0)
            d[y * dst_width + x] = mip_compact(sum, shift)

## SkMipmap::Build: the levels below `src`, each half the one above (floored, at least 1).
func mipmap_build(src: PixmapRef) -> Vec[Pixmap]:
    var levels: Vec[Pixmap]
    let count = mipmap_level_count(src.width(), src.height())
    var width = src.width()
    var height = src.height()
    for i in 0..<count:
        width = width >> 1 if width >> 1 > 0 else 1
        height = height >> 1 if height >> 1 > 0 else 1
        var level = Pixmap.new_(width, height) else trap("raster: out of memory")
        let above = src if i == 0 else levels.data[i - 1].as_ref()
        mip_build_level(&level, above)
        levels.push(level)
    return levels

## SkMipmap::ComputeLevel: the fractional level for sampling at `scale_x`, `scale_y` (image
## pixels per device pixel inverted); -1 when the image is not minified.
func mipmap_compute_level(scale_x: f32, scale_y: f32) -> f32:
    # Use the smallest scale to match the GPU impl.
    let scale = math32.min(scale_x, scale_y)
    if scale >= 1.0 or scale <= 0.0 or not math32.is_finite(scale):
        return -1.0
    # The -0.5 bias here is to emulate GPU's sharpen mipmap option.
    let level = math32.max(-math32.log2(scale) - 0.5, 0.0)
    if not math32.is_finite(level):
        return -1.0
    return level

## SkMatrix::decomposeScale's axis lengths of `inv`; none under perspective or when an axis
## (nearly) vanishes.
func decompose_scale(inv: Transform) -> (f32, f32)?:
    if inv.has_perspective():
        return none
    let sx = point_length(inv.sx, inv.ky)
    let sy = point_length(inv.kx, inv.sy)
    if not math32.is_finite(sx) or not math32.is_finite(sy) or is_nearly_zero(sx) or is_nearly_zero(sy):
        return none
    return (sx, sy)

## SkPoint::Length: in float, or in double when the squares overflow.
func point_length(x: f32, y: f32) -> f32:
    let mag2 = x * x + y * y
    if math32.is_finite(mag2):
        return math32.sqrt(mag2)
    return (f32)math.sqrt((f64)x * (f64)x + (f64)y * (f64)y)

# mark: The choice of levels and their packing ================================================

## SkMipmapAccessor's choice for linear mipmap sampling under `base_inv` (the image's inverse
## matrix) of an image with `levels` levels below it: the upper level (0 the base), the lower
## one when two are blended, and the lower one's weight.
pub struct MipmapChoice:
    pub var upper: usize
    pub var lower: usize?
    pub var weight: f32

## The levels pattern_push_mipmap_stages samples (see MipmapChoice).
pub func mipmap_choice(base_inv: Transform, levels: usize) -> MipmapChoice:
    var level: f32 = 0.0
    var linear = false
    if let scale = decompose_scale(base_inv):
        level = mipmap_compute_level(1.0 / scale.0, 1.0 / scale.1)
        if level <= 0.0:
            level = 0.0
        else:
            linear = true
    # In linear mode the level is the lower of the two to interpolate between: its floor.
    let level_num = (usize)math32.floor(level)
    var choice = MipmapChoice(upper = 0, lower = none, weight = level - (f32)level_num)
    if level_num > 0:
        if level_num - 1 < levels:
            choice.upper = level_num
        else:
            linear = false
    if linear and level_num < levels:
        choice.lower = level_num + 1
    return choice

## The levels below an image of `width` by `height` (SkMipmap::ComputeLevelCount).
pub func mipmap_levels_below(width: u32, height: u32) -> usize:
    return mipmap_level_count(width, height)

## Where level `level` (0 the base) of an image of `width` by `height` lies in mipmap_pack's
## packing: x, y, width, height.
pub func mipmap_level_rect(width: u32, height: u32, level: usize) -> u32[4]:
    if level == 0:
        return [0, 0, width, height]
    var w = width
    var h = height
    var y: u32 = 0
    for i in 0..<level:
        w = w >> 1 if w >> 1 > 0 else 1
        h = h >> 1 if h >> 1 > 0 else 1
        if i + 1 < level:
            y += h
    return [width, y, w, h]

## The size of mipmap_pack's packing of an image of `width` by `height`.
pub func mipmap_packed_size(width: u32, height: u32) -> u32[2]:
    if mipmap_level_count(width, height) == 0:
        return [width, height]
    let first = mipmap_level_rect(width, height, 1)
    return [width + first[2], height]

## Writes the levels below `src` (SkMipmap::Build's pixels) into `out`, premultiplied RGBA
## bytes `out_width` pixels a row, at mipmap_level_rect's places; the base is the caller's.
pub func mipmap_pack(src: PixmapRef, out: u8[], out_width: usize):
    var levels = mipmap_build(src)
    defer:
        for i in 0..<levels.len:
            levels.data[i].release()
        levels.release()
    for i in 0..<levels.len:
        let rect = mipmap_level_rect(src.width(), src.height(), i + 1)
        let pixels = levels.data[i].pixels()
        for y in 0..<(usize)rect[3]:
            for x in 0..<(usize)rect[2]:
                let p = pixels[y * (usize)rect[2] + x]
                let at = (((usize)rect[1] + y) * out_width + (usize)rect[0] + x) * 4
                out[at] = p.red()
                out[at + 1] = p.green()
                out[at + 2] = p.blue()
                out[at + 3] = p.alpha()