Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-image

Source of release 0.45.1

0.45.1 / src / canvas / retouch_effects.lucb

#==============================================================================================
#
#   canvas/retouch_effects - What the retouching strokes paint, made only where they reach
#
#   DESCRIPTION:
#       Clone stamp, the blur tool, dodge and burn paint an effect image through their
#       coverage. Each is an EffectSource: the stroke asks for a cell's effect tile the first
#       time a dab reaches that cell, so a stroke on a huge canvas costs the cells it crosses,
#       never the whole layer. Every source reads the picture as it was when the stroke began
#       (shared tiles, or a frozen copy of the document), so paint laid down by the stroke
#       never feeds back into what it samples.
#
#       Spot healing works out its patch when the stroke ends, in a window around the spot:
#       the spot, its ring, and as far as heal() looks for a matching patch.
#
#==============================================================================================

# -- Clone Stamp.

## A Clone Stamp's source: the layer (or with All Layers, the whole document)
## as the stroke began, read shifted by the clone offset.
struct CloneSource:
    var tiles: Tiles
    ## The frozen document, and the compositor to flatten it with, for All Layers.
    var frozen: Document*?
    var composer: Composer*?
    var device: gpu.Device
    var dx: i64
    var dy: i64
    var extent_x: i64
    var extent_y: i64

    ## Whether the layer has a tile in any cell of the range (inclusive).
    func tiles_under(left: i64, top: i64, right: i64, bottom: i64) -> bool:
        for row in top..<bottom + 1:
            for column in left..<right + 1:
                if self.tiles.painted((u32)column, (u32)row):
                    return true
        return false

    ## A source cell: the layer's tile, or the frozen document flattened there
    ## (borrowed until the compositor's next request).
    func cell_at(column: u32, row: u32) -> gpu.Texture?!:
        if let document = self.frozen:
            let composer = self.composer else trap("a frozen clone without a compositor")
            return try composer.tile(document, column, row, 0)
        return (try self.tiles.texture(column, row))

func clone_source(canvas: Canvas*, tiles: const Tiles*, dx: i64, dy: i64) -> EffectSource!:
    let device = canvas.device else trap("cloning without a device")
    var frozen: Document*? = none
    var composer: Composer*? = none
    if canvas.clone_all:
        let document = canvas.document else trap("cloning without a document")
        composer = canvas.composer else trap("cloning without a compositor")
        frozen = try document.frozen()
    var copy = tiles.clone() catch failure:
        if let made = frozen:
            unbox_document(made)
        error(failure.code, failure.message)
    let boxed = new CloneSource(tiles = copy, frozen = frozen, composer = composer, device = device, dx = dx, dy = dy,
                                extent_x = (i64)canvas.extent_x, extent_y = (i64)canvas.extent_y) in memory.heap catch failure:
        copy.destroy()
        if let made = frozen:
            unbox_document(made)
        error(failure.code, failure.message)
    return EffectSource(context = (void*)boxed, produce = clone_cell, release = release_clone)

## A cell of the clone: the picture at the cell's place plus the offset, copied
## from the up to four source cells under it; none where it reads nothing.
func clone_cell(context: void*?, column: u32, row: u32) -> gpu.Texture?!:
    let source = (CloneSource*)(context else trap("a clone without its source"))
    let cell_x = (i64)(column * tile_size)
    let cell_y = (i64)(row * tile_size)
    # The source window, clipped to the picture.
    let x0 = math.imax(0, cell_x + source.dx)
    let y0 = math.imax(0, cell_y + source.dy)
    let x1 = math.imin(source.extent_x, cell_x + source.dx + (i64)tile_size)
    let y1 = math.imin(source.extent_y, cell_y + source.dy + (i64)tile_size)
    if x1 <= x0 or y1 <= y0:
        return none
    let size = (i64)tile_size
    if source.frozen == none and not source.tiles_under(x0 // size, y0 // size, (x1 - 1) // size, (y1 - 1) // size):
        return none
    var cell = try tile_texture(source.device)
    errdefer cell.destroy()
    let frame = try cell.frame()
    defer frame.release()
    let target = try frame.value().target()
    defer target.release()
    for from_row in y0 // size..<(y1 - 1) // size + 1:
        for from_column in x0 // size..<(x1 - 1) // size + 1:
            let texture = (try source.cell_at((u32)from_column, (u32)from_row)) else continue
            # The part of this source cell inside the window, and where it lands.
            let left = math.imax(x0, from_column * size)
            let top = math.imax(y0, from_row * size)
            let right = math.imin(x1, from_column * size + size)
            let bottom = math.imin(y1, from_row * size + size)
            try gpu.copy_image(target.value(), texture, gpu.Rect((f64)(left - source.dx - cell_x), (f64)(top - source.dy - cell_y), (f64)(right - left), (f64)(bottom - top)),
                               gpu.Region((u32)(left - from_column * size), (u32)(top - from_row * size), (u32)(right - left), (u32)(bottom - top)))
    _ = try frame.value().present(gpu.Color(0.0, 0.0, 0.0, 0.0))
    return cell

func release_clone(context: void*?):
    let source = (CloneSource*)(context else return)
    source.tiles.destroy()
    if let document = source.frozen:
        unbox_document(document)
    free(source) in memory.heap

# -- Blur tool.

## The blur tool's source: the layer as the stroke began, blurred a cell at a time.
struct BlurSource:
    var tiles: Tiles
    var filters: Filters
    var radius: f64
    ## Whether it blurs the encoded values, as the document mixes its colors.
    var srgb: bool

func blur_source(canvas: Canvas*, tiles: const Tiles*, radius: f64) -> EffectSource!:
    let filters = canvas.filters else trap("blurring without filters")
    let document = canvas.document else trap("blurring without a document")
    var copy = try tiles.clone()
    let boxed = new BlurSource(tiles = copy, filters = filters, radius = radius, srgb = document.mixes_encoded()) in memory.heap catch failure:
        copy.destroy()
        error(failure.code, failure.message)
    return EffectSource(context = (void*)boxed, produce = blur_cell, release = release_blur)

func blur_cell(context: void*?, column: u32, row: u32) -> gpu.Texture?!:
    let source = (BlurSource*)(context else trap("a blur without its source"))
    return try source.filters.blur_cell(&source.tiles, column, row, source.radius, source.srgb)

func release_blur(context: void*?):
    let source = (BlurSource*)(context else return)
    source.tiles.destroy()
    free(source) in memory.heap

# -- Dodge and Burn.

## Dodge and Burn's source: the layer as the stroke began through a Levels move,
## a cell at a time.
struct ToneSource:
    var tiles: Tiles
    var adjuster: Adjuster
    var values: f64[20]

## The layer lightened (`dodge`) or darkened, as Photoshop's Dodge and Burn:
## in the shadows, midtones or highlights (`canvas.tone_range` 0, 1, 2) by
## `canvas.tone_exposure` (0..1), a Levels move on the encoded values. The
## stroke brushes it in by its coverage.
func tone_source(canvas: Canvas*, tiles: const Tiles*, dodge: bool) -> EffectSource!:
    let adjuster = canvas.adjuster else trap("toning without an adjuster")
    let amount = math.clamp(canvas.tone_exposure, 0.0, 1.0)
    # Levels on RGB: input black, input white, gamma, output black, output white.
    var values: f64[20]
    for at in 0..<(usize)20:
        values[at] = 0.0
    values[1] = 1.0
    values[2] = 1.0
    values[4] = 1.0
    if canvas.tone_range == 0:
        if dodge:
            values[3] = amount * 0.3
        else:
            values[0] = amount * 0.3
    elif canvas.tone_range == 2:
        if dodge:
            values[1] = 1.0 - amount * 0.3
        else:
            values[4] = 1.0 - amount * 0.3
    else:
        values[2] = 1.0 + amount * 0.8 if dodge else 1.0 / (1.0 + amount * 0.8)
    var copy = try tiles.clone()
    let boxed = new ToneSource(tiles = copy, adjuster = adjuster, values = values) in memory.heap catch failure:
        copy.destroy()
        error(failure.code, failure.message)
    return EffectSource(context = (void*)boxed, produce = tone_cell, release = release_tone)

func tone_cell(context: void*?, column: u32, row: u32) -> gpu.Texture?!:
    let source = (ToneSource*)(context else trap("a tone without its source"))
    let texture = (try source.tiles.texture(column, row)) else return none
    return try source.adjuster.adjusted(texture, levels, source.values[..<20])

func release_tone(context: void*?):
    let source = (ToneSource*)(context else return)
    source.tiles.destroy()
    free(source) in memory.heap

# -- Spot healing.

## End a spot healing stroke: the painted spot (the stroke's coverage) filled
## from the picture beside it, mixed in by the coverage at the tool's opacity.
## Only a window around the spot is read, healed and written back.
func finish_heal(canvas: Canvas*, stroke: Stroke*) -> !:
    let document = canvas.document else trap("stroke without a document")
    var painter = canvas.painter else trap("stroke without a painter")
    let device = canvas.device else trap("stroke without a device")
    let original = stroke.original()
    let bytes = try new u8[(usize)tile_size * (usize)tile_size * 8] --- in memory.heap
    defer free(bytes) in memory.heap
    let hole = (try spot_bounds(device, stroke, original)) else return
    let window = heal_window(hole, (i64)canvas.extent_x, (i64)canvas.extent_y)
    let width = (usize)(window.right - window.left)
    let height = (usize)(window.bottom - window.top)
    let pixels = try new f32[width * height * 4] --- in memory.heap
    defer free(pixels) in memory.heap
    let painted = try new u8[width * height] --- in memory.heap
    defer free(painted) in memory.heap
    # The patch mixes the picture's colors as the document blends them.
    let encoded = document.mixes_encoded()
    try gather_spot(device, stroke, original, window, pixels, painted, encoded)
    if not (try heal(pixels, width, height, painted)):
        return
    # Back to straight alpha (and linear light) for the effect tiles.
    for at in 0..<width * height:
        let alpha = pixels[at * 4 + 3]
        for channel in 0..<(usize)3:
            let straight = pixels[at * 4 + channel] / alpha if alpha > 0.0 else 0.0
            pixels[at * 4 + channel] = (f32)transfer.srgb_decode((f64)straight) if encoded else straight
    # The effect: in each cell the stroke reached, the layer as it was with the window's part healed.
    var healed = try Tiles.create(device, canvas.extent_x, canvas.extent_y)
    errdefer healed.destroy()
    var read = HealRead(stroke = stroke, original = original, columns = original.columns(), window = window, pixels = pixels, bytes = bytes, healed = &healed, device = device)
    try read_pieces(device, (usize)original.columns() * (usize)original.rows(), (void*)&read, healed_piece, healed_visit)
    try stroke.use_effect(&painter, try stroke_target(document, canvas.stroke_layer, false, true), healed, 2.0, canvas.heal_opacity)

## The box of the pixels the stroke painted (coverage past 1%); none when it painted nothing.
func spot_bounds(device: gpu.Device, stroke: Stroke*, original: const Tiles*) -> Bounds?!:
    var read = SpotRead(stroke = stroke, original = original, columns = original.columns())
    try read_pieces(device, (usize)original.columns() * (usize)original.rows(), (void*)&read, coverage_piece, bounds_visit)
    return read.bounds if read.found else none

## What the reads of a spot share: the stroke, the layer as it was, the grid's
## columns, and what is found.
struct SpotRead:
    var stroke: Stroke*
    var original: const Tiles*
    var columns: u32
    var found: bool
    var bounds: Bounds

## The stroke's coverage in cell `index` of the grid, where it painted.
func coverage_piece(context: void*?, index: usize) -> Piece!:
    let read = (SpotRead*)(context else trap("a spot read lost its state"))
    let column = (u32)(index % (usize)read.columns)
    let row = (u32)(index // (usize)read.columns)
    let coverage = read.stroke.coverage_at(column, row) else return no_piece()
    return Piece(texture = coverage, region = gpu.Region(0, 0, tile_size, read.original.region(column, row).height), present = true)

## Grow the box by the painted texels of a cell's coverage.
func bounds_visit(context: void*?, index: usize, bytes: const u8[]) -> !:
    let read = (SpotRead*)(context else trap("a spot read lost its state"))
    let column = (u32)(index % (usize)read.columns)
    let row = (u32)(index // (usize)read.columns)
    let area = read.original.region(column, row)
    for y in 0..<bytes.length // ((usize)tile_size * 8):
        for x in 0..<(usize)area.width:
            if not painted_at(bytes, x, y):
                continue
            let px = (i64)(column * tile_size) + (i64)x
            let py = (i64)(row * tile_size) + (i64)y
            if not read.found:
                read.bounds = Bounds(left = px, top = py, right = px + 1, bottom = py + 1)
                read.found = true
                continue
            read.bounds.left = math.imin(read.bounds.left, px)
            read.bounds.top = math.imin(read.bounds.top, py)
            read.bounds.right = math.imax(read.bounds.right, px + 1)
            read.bounds.bottom = math.imax(read.bounds.bottom, py + 1)

## Whether the coverage texel at (`x`, `y`) of a read-back cell counts as painted.
func painted_at(bytes: const u8[], x: usize, y: usize) -> bool:
    let at = (y * (usize)tile_size + x) * 8
    return from_half((u16)bytes[at + 6] | ((u16)bytes[at + 7] << 8)) > 0.01

## Everything heal() reads for a spot in `hole`: the spot, the ring around it
## (a band up to 16 pixels), and every shift it tries (up to 2.8 times the
## ring's box, and 3 pixels of refinement), inside the picture. Healing within
## this window gives what healing the whole picture would.
func heal_window(hole: Bounds, extent_x: i64, extent_y: i64) -> Bounds:
    let size = math.imax(hole.right - hole.left, hole.bottom - hole.top)
    let band = math.imin(16, math.imax(2, size // 8))
    let box_width = math.imin(extent_x, hole.right + band) - math.imax(0, hole.left - band)
    let box_height = math.imin(extent_y, hole.bottom + band) - math.imax(0, hole.top - band)
    let reach = (i64)math.ceil((f64)math.imax(box_width, box_height) * 2.8) + band + 8
    return Bounds(left = math.imax(0, hole.left - reach), top = math.imax(0, hole.top - reach),
                  right = math.imin(extent_x, hole.right + reach), bottom = math.imin(extent_y, hole.bottom + reach))

## The window's pixels (premultiplied, their colors sRGB-encoded with
## `encoded`) from the layer as it was, and where the stroke painted.
func gather_spot(device: gpu.Device, stroke: Stroke*, original: const Tiles*, window: Bounds, pixels: f32[], painted: u8[], encoded: bool) -> !:
    for index in 0..<pixels.length:
        pixels[index] = 0.0
    for index in 0..<painted.length:
        painted[index] = 0
    let size = (i64)tile_size
    var read = WindowRead(stroke = stroke, original = original, window = window, pixels = pixels, painted = painted, encoded = encoded,
                          first_column = window.left // size, first_row = window.top // size,
                          columns = (window.right - 1) // size + 1 - window.left // size)
    let rows = (window.bottom - 1) // size + 1 - window.top // size
    # Two pieces a cell: the layer's tile, then the stroke's coverage.
    try read_pieces(device, (usize)(read.columns * rows) * 2, (void*)&read, window_piece, window_visit)

## A spot's window being gathered: its cells from (`first_column`, `first_row`), `columns` wide.
struct WindowRead:
    var stroke: Stroke*
    var original: const Tiles*
    var window: Bounds
    var pixels: f32[]
    var painted: u8[]
    var encoded: bool
    var first_column: i64
    var first_row: i64
    var columns: i64

func window_cell(read: const WindowRead*, index: usize) -> (i64, i64):
    let cell = (i64)(index // 2)
    return (read.first_column + cell % read.columns, read.first_row + cell // read.columns)

func window_piece(context: void*?, index: usize) -> Piece!:
    let read = (WindowRead*)(context else trap("a window read lost its state"))
    let (column, row) = window_cell(read, index)
    let texture = ((try read.original.texture((u32)column, (u32)row)) if index % 2 == 0 else read.stroke.coverage_at((u32)column, (u32)row)) else return no_piece()
    return Piece(texture = texture, region = gpu.Region(0, 0, tile_size, tile_size), present = true)

func window_visit(context: void*?, index: usize, bytes: const u8[]) -> !:
    let read = (WindowRead*)(context else trap("a window read lost its state"))
    if bytes.length == 0:
        return
    let (column, row) = window_cell(read, index)
    let size = (i64)tile_size
    let window = read.window
    let width = (usize)(window.right - window.left)
    let cell_x = column * size
    let cell_y = row * size
    let x0 = math.imax(window.left, cell_x)
    let y0 = math.imax(window.top, cell_y)
    let x1 = math.imin(window.right, cell_x + size)
    let y1 = math.imin(window.bottom, cell_y + size)
    if index % 2 == 0:
        for y in y0..<y1:
            for x in x0..<x1:
                let source = (usize)((y - cell_y) * size + x - cell_x) * 8
                let target = ((usize)(y - window.top) * width + (usize)(x - window.left)) * 4
                let alpha = from_half((u16)bytes[source + 6] | ((u16)bytes[source + 7] << 8))
                for channel in 0..<(usize)3:
                    let value = from_half((u16)bytes[source + channel * 2] | ((u16)bytes[source + channel * 2 + 1] << 8))
                    read.pixels[target + channel] = ((f32)transfer.srgb_encode((f64)value) if read.encoded else value) * alpha
                read.pixels[target + 3] = alpha
        return
    for y in y0..<y1:
        for x in x0..<x1:
            if painted_at(bytes, (usize)(x - cell_x), (usize)(y - cell_y)):
                read.painted[(usize)(y - window.top) * width + (usize)(x - window.left)] = 1

## The healed cells being made: each cell the stroke reached, the layer as it
## was with the window's part healed, into `healed`.
struct HealRead:
    var stroke: Stroke*
    var original: const Tiles*
    var columns: u32
    var window: Bounds
    var pixels: const f32[]
    var bytes: u8[]
    var healed: Tiles*
    var device: gpu.Device

func healed_piece(context: void*?, index: usize) -> Piece!:
    let read = (HealRead*)(context else trap("a heal lost its state"))
    let column = (u32)(index % (usize)read.columns)
    let row = (u32)(index // (usize)read.columns)
    if read.stroke.coverage_at(column, row) == none:
        return no_piece()
    let texture = (try read.original.texture(column, row)) else return no_piece()
    return Piece(texture = texture, region = gpu.Region(0, 0, tile_size, tile_size), present = true)

func healed_visit(context: void*?, index: usize, bytes: const u8[]) -> !:
    let read = (HealRead*)(context else trap("a heal lost its state"))
    let column = (u32)(index % (usize)read.columns)
    let row = (u32)(index // (usize)read.columns)
    if read.stroke.coverage_at(column, row) == none:
        return
    # The cell as it was (clear where it had no tile), then the healed part over it.
    if bytes.length == 0:
        memory.set(read.bytes, 0)
    else:
        memory.copy(read.bytes, bytes, bytes.length)
    spot_into_cell(read.pixels, read.window, column, row, read.bytes)
    var texture = try tile_texture(read.device)
    errdefer texture.destroy()
    try texture.upload(read.bytes)
    try read.healed.set(column, row, texture)

## The healed window's straight-alpha pixels written over a read-back cell.
func spot_into_cell(pixels: const f32[], window: Bounds, column: u32, row: u32, bytes: u8[]):
    let size = (i64)tile_size
    let cell_x = (i64)column * size
    let cell_y = (i64)row * size
    let width = (usize)(window.right - window.left)
    for y in math.imax(window.top, cell_y)..<math.imin(window.bottom, cell_y + size):
        for x in math.imax(window.left, cell_x)..<math.imin(window.right, cell_x + size):
            let source = ((usize)(y - window.top) * width + (usize)(x - window.left)) * 4
            let target = (usize)((y - cell_y) * size + x - cell_x) * 8
            for channel in 0..<(usize)4:
                let half = to_half(pixels[source + channel])
                bytes[target + channel * 2] = (u8)(half & 0xff)
                bytes[target + channel * 2 + 1] = (u8)(half >> 8)