Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-image

Source of release 0.45.1

0.45.1 / src / transform.lucb

## Destructive geometry: a layer's pixels moved by whole pixels, or scaled,
## turned and moved together (free transform), or distorted in perspective. Pixels moved past the canvas
## are kept past it (see `Tiles`) and come back when moved back, as in
## Compositor.
from luce_gpu import gpu
import interop
from luce_std import math
import memory
from luce_canvas.tiles import Tiles, tile_size, tile_texture
from extent import relocate, landing_cells, canvas_part, reaches_past_part
from transform_window import Footprint, Window, footprint, level_for, gather, window_map
from luce_canvas.pyramid import Pyramid, tile_bytes
from projective import Warp, affine_warp, multiply
from document import Document
from shaders import warp_frag_words, warp_frag_msl, float_frag_words, float_frag_msl
from luce_raster.errors import invalid

## Move `layer`'s pixels (or its mask) by (`dx`, `dy`); returns the tiles from before.
pub func translate(document: Document*, layer: u64, dx: i64, dy: i64, on_mask: bool = false) -> Tiles!:
    let target = document.find(layer) else error(invalid, "no such layer")
    let tiles = (target.mask_tiles() else error(invalid, "the layer has no mask")) if on_mask else (target.tiles() else error(invalid, "a group cannot be moved"))
    if dx < -65536 or dx > 65536 or dy < -65536 or dy > 65536:
        error(invalid, "moves are limited to 65536 pixels")
    var moved = try Tiles.create(document.device(), tiles.width(), tiles.height())
    errdefer moved.destroy()
    let empty = 1.0 if on_mask else 0.0
    try relocate(tiles, &moved, 1.0, 1.0, (f64)dx, (f64)dy, false, gpu.Color(empty, empty, empty, empty))
    # In place, so pointers to the layer's tiles stay good.
    let before = *tiles
    *tiles = moved
    target.revision += 1
    return before

func floor_div(a: i64, b: i64) -> i64:
    return (a - math.mod_floor(a, b)) // b

## How a transform resamples: Compositor's Nearest, Smooth and High quality.
pub let sampling_nearest: i64 = 0
pub let sampling_smooth: i64 = 1
pub let sampling_high: i64 = 2

## Free transform and distort: a layer's pixels resampled through a `Warp`
## on the GPU with `warp.frag` — the whole layer, past the canvas too, or only
## the selected pixels (a floating selection). Built once per device; a
## floating selection's preview draws with `float.frag` (transform_preview.lucb).
pub struct Transformer:
    var owner: gpu.Device
    var shader: gpu.Shader
    var pipeline: gpu.Pipeline
    var float_shader: gpu.Shader
    var floating: gpu.Pipeline
    ## A clear tile, for a preview's cell with nothing to read.
    var clear: gpu.Texture

    pub static func create(device: gpu.Device) -> Transformer!:
        var shader = try gpu.Shader.create(device, warp_frag_words, warp_frag_msl)
        errdefer shader.destroy()
        var pipeline = try gpu.Pipeline.create(device, shader, gpu.Blend.replace, gpu.Format.rgba16_float)
        errdefer pipeline.destroy()
        var float_shader = try gpu.Shader.create(device, float_frag_words, float_frag_msl)
        errdefer float_shader.destroy()
        var floating = try gpu.Pipeline.create(device, float_shader, gpu.Blend.replace, gpu.Format.rgba16_float)
        errdefer floating.destroy()
        var clear = try tile_texture(device)
        errdefer clear.destroy()
        let frame = try clear.frame()
        _ = try frame.value().present(gpu.Color(0.0, 0.0, 0.0, 0.0))
        frame.release()
        return Transformer(owner = device, shader = shader, pipeline = pipeline, float_shader = float_shader, floating = floating, clear = clear)

    ## Replace `layer`'s pixels (or, `on_mask`, its mask) with them taken
    ## through `warp`. What leaves the document is kept past it; a mask
    ## reveals where it came from nothing, as an unpainted mask does. A
    ## distortion names the `source` box its pixels lie in, so the map is not
    ## followed past its horizon.
    pub func apply(document: Document*, layer: u64, warp: Warp, on_mask: bool = false, source: Landed? = none, sampling: i64 = sampling_smooth) -> !:
        let target = document.find(layer) else error(invalid, "no such layer")
        let tiles = (target.mask_tiles() else error(invalid, "the layer has no mask")) if on_mask else (target.tiles() else error(invalid, "a group cannot be transformed"))
        # Nothing painted: no pixels to move, or a mask revealing everything wherever it goes.
        let occupied = tiles.occupied() else return
        let outside = 1.0 if on_mask else 0.0
        let empty = gpu.Color(outside, outside, outside, outside)
        # Where the layer has pixels, past the canvas too.
        let limit = Footprint(left = (f64)(occupied.left * (i64)tile_size), top = (f64)(occupied.top * (i64)tile_size),
                              right = (f64)(occupied.right * (i64)tile_size), bottom = (f64)(occupied.bottom * (i64)tile_size))
        let box = source else Landed(left = limit.left, top = limit.top, right = limit.right, bottom = limit.bottom)
        let landed = try landing(warp, box.left, box.top, box.right, box.bottom)
        var fresh = try Tiles.create(self.owner, tiles.width(), tiles.height())
        errdefer fresh.destroy()
        let range = landing_cells(landed.left, landed.top, landed.right, landed.bottom)
        if landed.left < 0.0 or landed.top < 0.0 or landed.right > (f64)tiles.width() or landed.bottom > (f64)tiles.height():
            try fresh.cover(range)
        # Each output cell reads only the source pixels its map reaches, gathered
        # into a small window (at a coarser level where it shrinks a lot).
        var pyramid = try Pyramid.create(self.owner, 256 * tile_bytes)
        defer pyramid.destroy()
        let reachable = Footprint(left = math.max(limit.left, box.left - 2.0), top = math.max(limit.top, box.top - 2.0),
                                  right = math.min(limit.right, box.right + 2.0), bottom = math.min(limit.bottom, box.bottom + 2.0))
        for row in range.top..<range.bottom:
            for column in range.left..<range.right:
                let reads = footprint(warp, column, row, reachable) else continue
                pyramid.hold()
                var window = try gather(self.owner, tiles, &pyramid, reads, level_for(reads), on_mask, empty)
                let width = (f64)(try window.texture.width())
                let height = (f64)(try window.texture.height())
                var params = shader_params(window_map(warp, &window), width, height, outside, false, sampling, document.mixes_encoded(on_mask))
                let texture = self.shade_cell(&fresh, &params, (f64)(column * (i64)tile_size), (f64)(row * (i64)tile_size), window.texture, window.texture) catch failure:
                    window.destroy()
                    error(failure.code, failure.message)
                # The pass holds what it samples; the window can go now.
                window.destroy()
                # Which parts of the cell the landed box reaches.
                let x = landed.left - (f64)(column * (i64)tile_size)
                let y = landed.top - (f64)(row * (i64)tile_size)
                let canvas = canvas_part(&fresh, column, row)
                try fresh.place(column, row, texture, x < canvas.width and y < canvas.height,
                                reaches_past_part(x, y, landed.right - landed.left, landed.bottom - landed.top, canvas))
        try fresh.settle(on_mask)
        # In place, so pointers to the layer's tiles stay good.
        var before = *tiles
        *tiles = fresh
        before.destroy()
        target.revision += 1

    ## Take only the pixels `selection` covers (coverage in red; its painted
    ## box `left`..`right`, `top`..`bottom`) through `warp`, over what the
    ## selection leaves behind: a floating selection, set down on the canvas.
    pub func apply_selected(document: Document*, layer: u64, warp: Warp, selection: const Tiles*, left: i64, top: i64, right: i64, bottom: i64,
                            sampling: i64 = sampling_smooth) -> !:
        let target = document.find(layer) else error(invalid, "no such layer")
        let tiles = target.tiles() else error(invalid, "a group cannot be transformed")
        if right <= left or bottom <= top:
            return
        # Cells are replaced as they are made, so every window reads the layer as it was.
        var before = try tiles.clone()
        defer before.destroy()
        var pyramid = try Pyramid.create(self.owner, tile_bytes)
        defer pyramid.destroy()
        let lifted = Footprint(left = (f64)left, top = (f64)top, right = (f64)right, bottom = (f64)bottom)
        let clear = gpu.Color(0.0, 0.0, 0.0, 0.0)
        let landed = try landing(warp, (f64)left, (f64)top, (f64)right, (f64)bottom)
        for row in 0..<tiles.rows():
            for column in 0..<tiles.columns():
                let x0 = (f64)(column * tile_size)
                let y0 = (f64)(row * tile_size)
                let x1 = x0 + (f64)tile_size
                let y1 = y0 + (f64)tile_size
                # Only cells the selection leaves or the pixels land on change.
                let under = x0 < (f64)right and x1 > (f64)left and y0 < (f64)bottom and y1 > (f64)top
                let landing_here = x0 < landed.right and x1 > landed.left and y0 < landed.bottom and y1 > landed.top
                if not under and not landing_here:
                    continue
                # The window holds what lands here and what is left under it, at full size.
                var reads = Footprint(left = x0, top = y0, right = x1, bottom = y1)
                if let far = footprint(warp, (i64)column, (i64)row, lifted):
                    reads = Footprint(left = math.min(reads.left, far.left), top = math.min(reads.top, far.top),
                                      right = math.max(reads.right, far.right), bottom = math.max(reads.bottom, far.bottom))
                var pixels = try gather(self.owner, &before, &pyramid, reads, 0, false, clear)
                defer pixels.destroy()
                var coverage = try gather(self.owner, selection, &pyramid, reads, 0, false, clear)
                defer coverage.destroy()
                # The output pixel, and where it reads, both in the window's texels.
                let back = Warp(m = [1.0, 0.0, (f64)pixels.left, 0.0, 1.0, (f64)pixels.top, 0.0, 0.0, 1.0])
                var params = shader_params(multiply(window_map(warp, &pixels), back), (f64)(try pixels.texture.width()), (f64)(try pixels.texture.height()), 0.0, true, sampling,
                                           document.mixes_encoded())
                let shaded = try self.shade_cell(tiles, &params, x0 - (f64)pixels.left, y0 - (f64)pixels.top, pixels.texture, coverage.texture)
                try tiles.set(column, row, shaded)
        target.revision += 1

    ## One output cell of `tiles` shaded with `params`, as a fresh tile texture.
    ## `origin_x`, `origin_y`: the cell's first pixel, in the coordinates `params`' map reads.
    func shade_cell(tiles: const Tiles*, params: f32[20]*, origin_x: f64, origin_y: f64, source: gpu.Texture, selection: gpu.Texture) -> gpu.Texture!:
        (*params)[6] = (f32)origin_x
        (*params)[7] = (f32)origin_y
        var texture = try tiles.blank()
        errdefer texture.destroy()
        let frame = try texture.frame()
        defer frame.release()
        let area = try frame.value().target()
        defer area.release()
        try gpu.shade(area.value(), self.pipeline, gpu.Rect(width = (f64)tile_size, height = (f64)tile_size), u8[]((u8*)&(*params)[0], 80), [source, selection], gpu.Filter.nearest)
        _ = try frame.value().present(gpu.Color(0.0, 0.0, 0.0, 0.0))
        return texture

    ## The device the transformer draws on.
    pub func device() -> gpu.Device:
        return self.owner

    ## As `shade_cell`, into a tile texture of its own rather than one of a
    ## layer's (a preview's cell, handed to the compositor).
    pub func shade_tile(params: f32[20]*, origin_x: f64, origin_y: f64, source: gpu.Texture, selection: gpu.Texture) -> gpu.Texture!:
        (*params)[6] = (f32)origin_x
        (*params)[7] = (f32)origin_y
        var texture = try tile_texture(self.owner)
        errdefer texture.destroy()
        let frame = try texture.frame()
        defer frame.release()
        let area = try frame.value().target()
        defer area.release()
        try gpu.shade(area.value(), self.pipeline, gpu.Rect(width = (f64)tile_size, height = (f64)tile_size), u8[]((u8*)&(*params)[0], 80), [source, selection], gpu.Filter.nearest)
        _ = try frame.value().present(gpu.Color(0.0, 0.0, 0.0, 0.0))
        return texture

    ## `float.frag` with `params` (the map at 0..5 and 11..13, as `shader_params`
    ## lays it out) into a tile of its own: the lifted pixels of `source` and
    ## `selection` (`size` texels) landed over `base` less `base_selection`.
    pub func shade_floating(params: f32[20]*, origin_x: f64, origin_y: f64, source: gpu.Texture, selection: gpu.Texture,
                            base: gpu.Texture?, base_selection: gpu.Texture?) -> gpu.Texture!:
        let p = *params
        # float.frag's push constants: the map, the origin, the size, the perspective row, the sampling, the mixing.
        var packed: f32[16] = [p[0], p[1], p[2], p[3], p[4], p[5], (f32)origin_x, (f32)origin_y, p[8], p[9], p[11], p[12], p[13], p[15], p[16], 0.0]
        var texture = try tile_texture(self.owner)
        errdefer texture.destroy()
        let frame = try texture.frame()
        defer frame.release()
        let area = try frame.value().target()
        defer area.release()
        try gpu.shade(area.value(), self.floating, gpu.Rect(width = (f64)tile_size, height = (f64)tile_size), u8[]((u8*)&packed[0], 64),
                      [source, selection, base else self.clear, base_selection else self.clear], gpu.Filter.nearest)
        _ = try frame.value().present(gpu.Color(0.0, 0.0, 0.0, 0.0))
        return texture

    ## The clear tile.
    pub func clear_tile() -> gpu.Texture:
        return self.clear

    pub func destroy():
        self.clear.destroy()
        self.floating.destroy()
        self.float_shader.destroy()
        self.pipeline.destroy()
        self.shader.destroy()

## A box of document pixels, fractional.
pub struct Landed:
    pub var left: f64
    pub var top: f64
    pub var right: f64
    pub var bottom: f64

## Where the box `left`..`right`, `top`..`bottom` lands through `warp` (a
## backwards map), a pixel's margin around it for the resampling.
pub func landing(warp: Warp, left: f64, top: f64, right: f64, bottom: f64) -> Landed!:
    let ahead = try warp.forward((left + right) * 0.5, (top + bottom) * 0.5)
    var landed = Landed(left = 1.0e18, top = 1.0e18, right = -1.0e18, bottom = -1.0e18)
    for corner in 0..<(usize)4:
        var x = 0.0
        var y = 0.0
        if not ahead.map(left if corner % 2 == 0 else right, top if corner < 2 else bottom, &x, &y):
            error(invalid, "the distortion pulls the picture past its horizon")
        landed.left = math.min(landed.left, x - 1.0)
        landed.top = math.min(landed.top, y - 1.0)
        landed.right = math.max(landed.right, x + 1.0)
        landed.bottom = math.max(landed.bottom, y + 1.0)
    return landed

## `warp.frag`'s push constants: the map, the tile origin (set per cell), the
## source's size, the outside value, whether it lifts a selection, the
## sampling, and whether samples mix as sRGB-encoded values.
pub func shader_params(warp: Warp, width: f64, height: f64, outside: f64, lifting: bool, sampling: i64, srgb: bool) -> f32[20]:
    let m = warp.m
    return [(f32)m[0], (f32)m[1], (f32)m[2], (f32)m[3], (f32)m[4], (f32)m[5], 0.0, 0.0, (f32)width, (f32)height, (f32)outside,
            (f32)m[6], (f32)m[7], (f32)m[8], 1.0 if lifting else 0.0, (f32)sampling, 1.0 if srgb else 0.0, 0.0, 0.0, 0.0]

test "a free transform scales, turns and flips a layer about a pivot, and a mask with white around it":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var document = try Document.create(device, 128, 128)
    defer document.destroy()
    let layer = try document.add_layer("Square")
    let count = (usize)128 * 128 * 4
    let pixels = try new f32[count] --- in memory.heap
    defer free(pixels) in memory.heap
    for index in 0..<count:
        pixels[index] = 0.0
    # An opaque green 32×32 square in the middle of the (single) tile.
    for y in 48..<(usize)80:
        for x in 48..<(usize)80:
            pixels[(y * 128 + x) * 4 + 1] = 1.0
            pixels[(y * 128 + x) * 4 + 3] = 1.0
    var tiles = try Tiles.create(device, 128, 128)
    try tiles.write(pixels)
    try document.set_tiles(layer, tiles)
    var transformer = try Transformer.create(device)
    defer transformer.destroy()
    let current = (document.find(layer) else trap("layer")).tiles() else trap("tiles")
    let found = try current.content_bounds() else trap("the square has bounds")
    assert(found.left == 48 and found.top == 48 and found.right == 80 and found.bottom == 80)
    # Doubling about the square's centre (64, 64) makes it 64×64: (32..96).
    try transformer.apply(&document, layer, try affine_warp(2.0, 2.0, 0.0, 0.0, 0.0, 64.0, 64.0))
    try current.read(pixels)
    assert(pixels[(40 * 128 + 40) * 4 + 3] > 0.99 and pixels[(40 * 128 + 40) * 4 + 1] > 0.99)
    assert(pixels[(20 * 128 + 64) * 4 + 3] < 0.01 and pixels[(64 * 128 + 100) * 4 + 3] < 0.01)
    # A quarter turn keeps a centred square where it is; moving it by 40 shifts it.
    try transformer.apply(&document, layer, try affine_warp(1.0, 1.0, math.pi * 0.5, 40.0, 0.0, 64.0, 64.0))
    try current.read(pixels)
    assert(pixels[(64 * 128 + 110) * 4 + 3] > 0.99 and pixels[(64 * 128 + 40) * 4 + 3] < 0.01)
    let moved = try current.content_bounds() else trap("the square has bounds")
    # Doubled bilinearly, its edges went soft a pixel further out: 31..97, then 71.. after the move.
    assert(moved.left == 71 and moved.right == 128 and moved.top == 31 and moved.bottom == 97)
    # A flip about the document's middle mirrors it exactly: 71..128 lands on 0..57.
    try transformer.apply(&document, layer, try affine_warp(-1.0, 1.0, 0.0, 0.0, 0.0, 64.0, 64.0))
    let flipped = try current.content_bounds() else trap("the square has bounds")
    assert(flipped.left == 0 and flipped.right == 57 and flipped.top == 31 and flipped.bottom == 97)
    # A mask painted black in the middle, moved right by 32: black follows, and
    # where it left becomes white again, as around it.
    try document.set_mask(layer, true)
    let mask_tiles = (document.find(layer) else trap("layer")).mask_tiles() else trap("mask")
    for index in 0..<count:
        pixels[index] = 1.0
    for y in 48..<(usize)80:
        for x in 48..<(usize)80:
            for channel in 0..<(usize)3:
                pixels[(y * 128 + x) * 4 + channel] = 0.0
    try mask_tiles.write(pixels)
    try transformer.apply(&document, layer, try affine_warp(1.0, 1.0, 0.0, 32.0, 0.0, 64.0, 64.0), on_mask = true)
    let moved_mask = (document.find(layer) else trap("layer")).mask_tiles() else trap("mask")
    try moved_mask.read(pixels)
    assert(pixels[(64 * 128 + 90) * 4] < 0.01 and pixels[(64 * 128 + 60) * 4] > 0.99 and pixels[(64 * 128 + 10) * 4] > 0.99)
    var rejected = false
    let flat_warp = affine_warp(0.0, 1.0, 0.0, 0.0, 0.0, 64.0, 64.0) catch failure:
        assert(failure.code == invalid)
        rejected = true
        recover Warp(m = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0])
    assert(rejected and flat_warp.m[0] == 1.0)

test "translating a layer moves its pixels across tiles, and what leaves the canvas comes back":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var document = try Document.create(device, 300, 40)
    defer document.destroy()
    let layer = try document.add_layer("Dot")
    let count = (usize)300 * 40 * 4
    let pixels = try new f32[count] --- in memory.heap
    defer free(pixels) in memory.heap
    for index in 0..<count:
        pixels[index] = 0.0
    pixels[(10 * 300 + 250) * 4 + 1] = 1.0
    pixels[(10 * 300 + 250) * 4 + 3] = 1.0
    # A soft white texel: moving must not darken what is partly covered.
    for channel in 0..<(usize)3:
        pixels[(10 * 300 + 20) * 4 + channel] = 1.0
    pixels[(10 * 300 + 20) * 4 + 3] = 0.5
    var tiles = try Tiles.create(device, 300, 40)
    try tiles.write(pixels)
    try document.set_tiles(layer, tiles)
    var before = try translate(&document, layer, 20, 5)
    before.destroy()
    let current = (document.find(layer) else trap("layer")).tiles() else trap("tiles")
    try current.read(pixels)
    assert(pixels[(15 * 300 + 270) * 4 + 1] > 0.99 and pixels[(10 * 300 + 250) * 4 + 3] == 0.0)
    let soft = (15 * 300 + 40) * 4
    assert(pixels[soft] > 0.99 and pixels[soft + 1] > 0.99 and pixels[soft + 2] > 0.99 and pixels[soft + 3] > 0.49 and pixels[soft + 3] < 0.51)
    assert(current.painted(1, 0))
    var again = try translate(&document, layer, 100, 0)
    again.destroy()
    try current.read(pixels)
    assert(pixels[(15 * 300 + 270) * 4 + 3] == 0.0 and current.has_outside())
    # Past the right edge (partly inside the edge cell) and past the top-left, then back.
    var back = try translate(&document, layer, -100, 0)
    back.destroy()
    try current.read(pixels)
    assert(pixels[(15 * 300 + 270) * 4 + 1] > 0.99 and not current.has_outside())
    var away = try translate(&document, layer, -600, -300)
    away.destroy()
    try current.read(pixels)
    assert(pixels[(15 * 300 + 270) * 4 + 3] == 0.0 and current.has_outside() and not current.painted(0, 0) and not current.painted(1, 0))
    var home = try translate(&document, layer, 600, 300)
    home.destroy()
    try current.read(pixels)
    assert(pixels[(15 * 300 + 270) * 4 + 1] > 0.99 and pixels[soft + 3] > 0.49 and pixels[soft + 3] < 0.51 and not current.has_outside())
    # The layer's clone carries what is past the canvas, for history.
    var far = try translate(&document, layer, 0, 50)
    var kept = try current.clone()
    defer kept.destroy()
    assert(kept.has_outside() and not far.has_outside())
    far.destroy()

test "a large shrink reads a coarser level and keeps the picture, with no texture the size of the layer":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    # 6000 by 1024: the left half opaque red, the right half opaque blue.
    var document = try Document.create(device, 6000, 1024)
    defer document.destroy()
    let layer = try document.add_layer("Wide")
    let count = (usize)6000 * 1024 * 4
    let pixels = try new f32[count] --- in memory.heap
    defer free(pixels) in memory.heap
    for y in 0..<(usize)1024:
        for x in 0..<(usize)6000:
            let at = (y * 6000 + x) * 4
            pixels[at] = 1.0 if x < 3000 else 0.0
            pixels[at + 1] = 0.0
            pixels[at + 2] = 0.0 if x < 3000 else 1.0
            pixels[at + 3] = 1.0
    var tiles = try Tiles.create(device, 6000, 1024)
    try tiles.write(pixels)
    try document.set_tiles(layer, tiles)
    var transformer = try Transformer.create(device)
    defer transformer.destroy()
    # A tenth the size about the top-left: 600 by 102, red then blue.
    try transformer.apply(&document, layer, try affine_warp(0.1, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0))
    let result = (document.find(layer) else trap("layer")).tiles() else trap("tiles")
    let back = try new f32[count] --- in memory.heap
    defer free(back) in memory.heap
    try result.read(back)
    let red = (50 * 6000 + 100) * 4
    let blue = (50 * 6000 + 500) * 4
    let beyond = (50 * 6000 + 800) * 4
    assert(back[red] > 0.95 and back[red + 2] < 0.05 and back[red + 3] > 0.95)
    assert(back[blue] < 0.05 and back[blue + 2] > 0.95 and back[blue + 3] > 0.95)
    assert(back[beyond + 3] == 0.0)