Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-image

Source of release 0.45.0

0.45.0 / src / canvas / refine_tests.lucb

#==============================================================================================
#
#   canvas/refine_tests - Select › Modify › Border and Smooth, Grow and Similar, and
#   Transform Selection through the canvas, with their undo steps
#
#==============================================================================================

test "border, smooth, grow and similar change the selection as one step each":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var canvas = try Canvas(700, 500)
    defer canvas.close()
    let layer = try canvas.add_layer("Paint")
    var target = try gpu.Texture.create(device, 64, 64, gpu.Format.rgba8_linear)
    defer target.destroy()
    _ = try shown_at(&canvas, &target, 0, 0)
    # A red block on the left, a blue one on the right, a red one past a gap.
    try canvas.set_brush(10.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, false)
    try canvas.select_rectangle(0, 0, 300, 500, 0)
    try canvas.fill(layer, false)
    try canvas.set_brush(10.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, false)
    try canvas.select_rectangle(300, 0, 100, 500, 0)
    try canvas.fill(layer, false)
    try canvas.set_brush(10.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, false)
    try canvas.select_rectangle(400, 0, 300, 500, 0)
    try canvas.fill(layer, false)
    # Grow from a small red patch takes the red block, not past the blue.
    try canvas.select_rectangle(50, 50, 10, 10, 0)
    try canvas.grow_selection(layer, 10.0, true)
    assert(canvas.undo_name() == "Grow" and canvas.selected_at(250, 400) == 255 and canvas.selected_at(350, 250) == 0 and canvas.selected_at(600, 250) == 0)
    try canvas.undo()
    assert(canvas.selected_at(250, 400) == 0 and canvas.selected_at(55, 55) == 255)
    # Similar reaches the red past the blue too.
    try canvas.grow_selection(layer, 10.0, false)
    assert(canvas.undo_name() == "Similar" and canvas.selected_at(600, 250) == 255 and canvas.selected_at(350, 250) == 0)
    # Border of the left block: its edge, not its middle.
    try canvas.select_rectangle(100, 100, 200, 200, 0)
    try canvas.border_selection(10)
    assert(canvas.undo_name() == "Border" and canvas.selected_at(100, 200) > 0 and canvas.selected_at(200, 200) == 0 and canvas.selected_at(60, 200) == 0)
    try canvas.undo()
    assert(canvas.selected_at(200, 200) == 255)
    try canvas.smooth_selection(5)
    assert(canvas.undo_name() == "Smooth" and canvas.selected_at(200, 200) == 255 and canvas.selected_at(101, 101) < 255)

test "transform selection reshapes the outline, not the pixels, and undoes as one step":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var canvas = try Canvas(400, 300)
    defer canvas.close()
    let layer = try canvas.add_layer("Paint")
    var target = try gpu.Texture.create(device, 64, 64, gpu.Format.rgba8_linear)
    defer target.destroy()
    _ = try shown_at(&canvas, &target, 0, 0)
    try canvas.set_brush(10.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, false)
    try canvas.select_rectangle(100, 100, 100, 100, 0)
    try canvas.fill(layer, false)
    let steps = canvas.history_steps()
    try canvas.begin_selection_transform()
    assert(canvas.transforming_selection())
    # Twice the size about the middle: the previews start from the original
    # each time, and only record the warp; the selection stays till kept.
    try canvas.preview_selection_transform(1.5, 1.5, 0.0, 0.0, 0.0, 150.0, 150.0)
    try canvas.preview_selection_transform(2.0, 2.0, 0.0, 0.0, 0.0, 150.0, 150.0)
    assert(canvas.selected_at(60, 60) == 0 and canvas.selection_left() == 100)
    # A frame at half size shows it a level down, over the cells in view.
    let session = canvas.selection_warping else trap("a transform is under way")
    try prepare_selection_warp(session, Scope(level = 1, box = CellBox(everything = false, left = 0, top = 0, right = 256, bottom = 256)))
    let shown = session.shown.selection else trap("a preview shows")
    assert(session.shown.level == 1 and shown.width() == 200 and shown.at(30, 30) == 255 and shown.at(20, 20) == 0)
    try canvas.end_selection_transform(true)
    assert(not canvas.transforming_selection() and canvas.history_steps() == steps + 1 and canvas.undo_name() == "Transform Selection")
    # Bilinear edges may cover a pixel more, partly.
    assert(canvas.selection_left() >= 49 and canvas.selection_left() <= 50 and canvas.selection_width() >= 200 and canvas.selection_width() <= 202)
    assert(canvas.selected_at(52, 150) == 255 and canvas.selected_at(247, 150) == 255)
    # The pixels stayed where they were.
    let pixels = try new f32[400 * 300 * 4] --- in memory.heap
    defer free(pixels) in memory.heap
    try layer_tiles(&canvas, layer).read(pixels)
    assert(pixels[(60 * 400 + 60) * 4 + 3] == 0.0 and pixels[(150 * 400 + 150) * 4 + 3] == 1.0)
    try canvas.undo()
    assert(canvas.selection_left() == 100 and canvas.selection_width() == 100)
    # Cancelled, it is as it was, and no step is added.
    try canvas.begin_selection_transform()
    try canvas.preview_selection_distort(100.0, 100.0, 100.0, 100.0, 80.0, 90.0, 220.0, 100.0, 200.0, 200.0, 100.0, 200.0)
    let distorting = canvas.selection_warping else trap("a transform is under way")
    try prepare_selection_warp(distorting, Scope())
    assert((distorting.shown.selection else trap("a preview shows")).at(85, 95) == 255)
    try canvas.end_selection_transform(false)
    assert(canvas.selection_left() == 100 and canvas.history_steps() == steps)

test "quick mask paints the selection with the painting tools and turns back into the selection":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var canvas = try Canvas(300, 300)
    defer canvas.close()
    let layer = try canvas.add_layer("Paint")
    var target = try gpu.Texture.create(device, 64, 64, gpu.Format.rgba8_linear)
    defer target.destroy()
    # The view's pipelines draw into the test's texture, not a window.
    canvas.view_format = gpu.Format.rgba8_linear
    # A white layer, so the tint shows against it.
    try canvas.set_brush(10.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, false)
    _ = try shown_at(&canvas, &target, 0, 0)
    try canvas.fill(layer, false)
    try canvas.select_rectangle(0, 0, 40, 300, 0)
    let steps = canvas.history_steps()
    try canvas.enter_quick_mask()
    assert(canvas.quick_mask() and canvas.undo_name() == "Quick Mask" and canvas.history_steps() == steps + 1)
    # The layer is as it was: the red tint is the view's alone, at half over
    # white as encoded values (green and blue 128; the linear target stores 55).
    let tinted = try shown_at(&canvas, &target, 50, 10)
    assert(tinted[0] == 255 and tinted[1] >= 54 and tinted[1] <= 56 and tinted[2] == tinted[1])
    assert((try shown_at(&canvas, &target, 10, 10))[1] == 255)
    let white = try new f32[300 * 300 * 4] --- in memory.heap
    defer free(white) in memory.heap
    try layer_tiles(&canvas, layer).read(white)
    assert(white[(10 * 300 + 50) * 4 + 1] == 1.0)
    # Black masks, white selects: a gradient masking the top third and
    # selecting the rest, then a black stroke down the lower selected band.
    try canvas.fill_gradient(layer, 0.0, 100.0, 0.0, 101.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0)
    try canvas.set_brush(8.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, false)
    try canvas.begin_stroke(layer, 20.0, 150.0)
    try canvas.extend_stroke(20.0, 250.0)
    try canvas.end_stroke()
    # The layer's own pixels are as they were.
    let pixels = try new f32[300 * 300 * 4] --- in memory.heap
    defer free(pixels) in memory.heap
    try layer_tiles(&canvas, layer).read(pixels)
    assert(pixels[(200 * 300 + 20) * 4] == 1.0 and pixels[(200 * 300 + 20) * 4 + 3] == 1.0)
    # A retouch tool is refused, as on any mask, rather than reaching the layer.
    try canvas.set_retouch(3)
    var refused = false
    canvas.begin_stroke(layer, 150.0, 150.0) catch failure:
        refused = true
    assert(refused)
    try canvas.set_retouch(0)
    let painted = canvas.history_steps()
    try canvas.exit_quick_mask()
    assert(not canvas.quick_mask() and canvas.undo_name() == "Exit Quick Mask")
    # Photoshop's order: Quick Mask, the edits on the mask, Exit Quick Mask.
    assert(canvas.history_steps() == painted + 1 and canvas.history_step_name(steps) == "Quick Mask" and canvas.history_step_name(painted - 1) == "Brush Stroke")
    assert(canvas.selected_at(5, 50) == 0 and canvas.selected_at(150, 150) == 255 and canvas.selected_at(20, 200) == 0 and canvas.selected_at(150, 250) == 255)
    # Undoing Exit Quick Mask brings the mode back with its mask as painted,
    # and the stroke before it then undoes on the mask.
    try canvas.undo()
    assert(canvas.quick_mask() and canvas.selected_at(5, 50) == 255 and canvas.selected_at(150, 250) == 0)
    try canvas.undo()
    assert(canvas.quick_mask() and canvas.undo_name() == "Gradient")
    try canvas.exit_quick_mask()
    assert(canvas.selected_at(20, 200) == 255 and canvas.selected_at(5, 50) == 0)
    # Undoing back past Quick Mask takes the mode down; redoing brings it back.
    try canvas.undo()
    try canvas.undo()
    try canvas.undo()
    assert(not canvas.quick_mask() and canvas.selected_at(20, 50) == 255 and canvas.redo_name() == "Quick Mask")
    try canvas.redo()
    assert(canvas.quick_mask())
    try canvas.undo()
    assert(not canvas.quick_mask() and canvas.selected_at(20, 50) == 255)
    # Nothing selected stays nothing selected through an unpainted Quick Mask.
    try canvas.deselect()
    try canvas.enter_quick_mask()
    try canvas.exit_quick_mask()
    assert(not canvas.has_selection())

## A canvas `width` by `height`, black left of column `edge` and white from
## it, drawn once: for the Magnetic Lasso.
func half_white(width: u32, height: u32, edge: i64, target: gpu.Texture*) -> Canvas!:
    var canvas = try Canvas((i64)width, (i64)height)
    errdefer canvas.close()
    let layer = try canvas.add_layer("Paint")
    _ = try shown_at(&canvas, target, 0, 0)
    try canvas.set_brush(10.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, false)
    try canvas.fill(layer, false)
    try canvas.select_rectangle(edge, 0, (i64)width - edge, (i64)height, 0)
    try canvas.set_brush(10.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, false)
    try canvas.fill(layer, false)
    try canvas.deselect()
    return canvas

test "the magnetic lasso hugs an edge, takes anchors back and selects what it closes round":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var target = try gpu.Texture.create(device, 64, 64, gpu.Format.rgba8_linear)
    defer target.destroy()
    var canvas = try half_white(300, 300, 150, &target)
    defer canvas.close()
    try canvas.begin_magnetic(150.0, 10.0, width = 10.0, contrast = 10.0, frequency = 57)
    # The pointer wanders beside the edge; the wire keeps to it.
    try canvas.move_magnetic(141.0, 120.0)
    var count = canvas.magnetic_count()
    assert(count > 50)
    for index in 0..<count:
        assert(canvas.magnetic_x(index) >= 148.0 and canvas.magnetic_x(index) <= 152.0)
    let last = canvas.magnetic_y(count - 1)
    assert(last >= 118.0 and last <= 122.0)
    # Anchors fell on their own along the way.
    assert(canvas.magnetic_anchors() >= 2)
    try canvas.anchor_magnetic(150.0, 290.0)
    # Backspace takes the last anchor back, and the path after the one before.
    try canvas.anchor_magnetic(290.0, 290.0)
    let anchors = canvas.magnetic_anchors()
    let before = canvas.magnetic_anchor(anchors - 2)
    assert(canvas.remove_magnetic_anchor() and canvas.magnetic_anchors() == anchors - 1 and canvas.magnetic_count() == before + 1)
    try canvas.anchor_magnetic(290.0, 290.0)
    try canvas.anchor_magnetic(290.0, 10.0)
    try canvas.close_magnetic(0)
    assert(not canvas.magnetic_active() and canvas.undo_name() == "Magnetic Lasso")
    assert(canvas.selected_at(220, 150) == 255 and canvas.selected_at(100, 150) == 0 and canvas.selected_at(147, 150) == 0)
    # Esc lets it go; Backspace at the first anchor does nothing.
    try canvas.begin_magnetic(20.0, 20.0, level = 1)
    assert(not canvas.remove_magnetic_anchor())
    try canvas.move_magnetic(60.0, 20.0)
    assert(canvas.magnetic_count() > 1)
    canvas.cancel_magnetic()
    assert(not canvas.magnetic_active() and canvas.magnetic_count() == 0)

test "the magnetic lasso on 15000 by 24000 reads only the cells round its wire":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var target = try gpu.Texture.create(device, 64, 64, gpu.Format.rgba8_linear)
    defer target.destroy()
    var canvas = try half_white(15000, 24000, 7040, &target)
    defer canvas.close()
    let started = time.now()
    try canvas.begin_magnetic(7040.0, 12000.0)
    var y = 12000.0
    while y < 12400.0:
        y += 8.0
        try canvas.move_magnetic(7046.0, y)
    let count = canvas.magnetic_count()
    let elapsed = time.now() - started
    for index in 0..<count:
        assert(canvas.magnetic_x(index) >= 7038.0 and canvas.magnetic_x(index) <= 7042.0)
    let magnetic = canvas.magnetic else trap("the lasso is under way")
    # 400 pixels down one edge: the column of cells either side, no more.
    assert(magnetic.cells_read() <= 8)
    # Fifty moves in well under a second each, whatever the document's size.
    assert(elapsed < 10000000000)
    canvas.cancel_magnetic()

test "select and mask previews in the view's box, keeps one step, and cancels back":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var target = try gpu.Texture.create(device, 64, 64, gpu.Format.rgba8_linear)
    defer target.destroy()
    # The picture's edge at column 306, the selection's at 300.
    var canvas = try half_white(620, 400, 306, &target)
    defer canvas.close()
    try canvas.select_rectangle(300, 0, 320, 400, 0)
    let steps = canvas.history_steps()
    try canvas.begin_refine()
    assert(canvas.refining())
    try canvas.set_refine_view(2)
    assert(canvas.refine_view_mode() == 2)
    # A setting is only recorded: the selection stays as it began till kept.
    try canvas.preview_refine(12, 0, 0, 0.0, 0.0)
    assert(canvas.selected_at(303, 100) == 255 and canvas.history_steps() == steps)
    # A frame showing the top at full size works that out on a thread of its own.
    let session = canvas.select_mask else trap("Select and Mask is open")
    try prepare_refine_preview(&canvas, Scope(level = 0, box = CellBox(everything = false, left = 0, top = 0, right = 620, bottom = 200)))
    assert(canvas.busy())
    try collect_refine(session, true)
    assert(not canvas.busy())
    let shown = session.shown.selection else trap("a preview shows")
    # Only the box's cells: the edge in the first row of cells moves; below,
    # off the view, edge cells are left clear so its GPU tiles cost the view.
    assert(shown.at(303, 100) == 0 and shown.at(309, 100) == 255 and shown.at(303, 300) == 0 and shown.at(600, 300) == 255)
    let previewed = shown.cell_bytes(1, 0) else trap("the edge cell holds bytes")
    try canvas.end_refine(true, 12)
    assert(not canvas.refining() and canvas.refine_view_mode() == 0 and canvas.undo_name() == "Select and Mask")
    assert(canvas.selected_at(303, 300) == 0 and canvas.selected_at(309, 300) == 255 and canvas.selected_at(303, 100) == 0)
    # OK took the cell the full-size preview had worked out rather than working it again.
    assert((canvas.current().cell_bytes(1, 0) else trap("the edge cell holds bytes")).data == previewed.data)
    try canvas.undo()
    assert(canvas.selected_at(303, 300) == 255)
    # Cancel leaves it as it was.
    try canvas.begin_refine()
    try canvas.preview_refine(0, 0, 20, 0.0, 0.0)
    try canvas.end_refine(false)
    assert(canvas.selected_at(295, 100) == 0 and canvas.selected_at(303, 100) == 255 and canvas.history_steps() == steps)

test "select and mask previews a level down, and a newer setting cancels an older one":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var target = try gpu.Texture.create(device, 64, 64, gpu.Format.rgba8_linear)
    defer target.destroy()
    var canvas = try half_white(1200, 800, 606, &target)
    defer canvas.close()
    try canvas.select_rectangle(600, 0, 600, 800, 0)
    try canvas.begin_refine()
    let session = canvas.select_mask else trap("Select and Mask is open")
    let fit = Scope(level = 2, box = CellBox(everything = false, left = 0, top = 0, right = 512, bottom = 512))
    # A slider dragged: each setting replaces the last, the running one is
    # cancelled, and once it stops the latest starts.
    try canvas.preview_refine(0, 0, 40, 0.0, 0.0)
    try prepare_refine_preview(&canvas, fit)
    try canvas.preview_refine(0, 0, 80, 0.0, 0.0)
    try prepare_refine_preview(&canvas, fit)
    try collect_refine(session, true)
    try prepare_refine_preview(&canvas, fit)
    try collect_refine(session, true)
    let shown = session.shown.selection else trap("a preview shows")
    assert(session.shown.level == 2 and session.shown_setting.feather == 20 and shown.width() == 300 and shown.height() == 200)
    # The preview a quarter size is the full refinement a quarter size, near enough.
    let probes: i64[5] = [120, 140, 150, 160, 180]
    var quarter: i64[5]
    for index in 0..<(usize)5:
        quarter[index] = (i64)shown.at(probes[index], 100)
    try canvas.end_refine(true, 0, 0, 80, 0.0, 0.0)
    for index in 0..<(usize)5:
        let whole = (i64)canvas.selected_at(probes[index] * 4 + 2, 400)
        assert(math.iabs(whole - quarter[index]) <= 12)

test "grow takes its colors from the selection's solid pixels, not its anti-aliased rim":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var canvas = try Canvas(400, 300)
    defer canvas.close()
    let layer = try canvas.add_layer("Paint")
    var target = try gpu.Texture.create(device, 64, 64, gpu.Format.rgba8_linear)
    defer target.destroy()
    _ = try shown_at(&canvas, &target, 0, 0)
    # A pink ground, a red disc on it with a soft (anti-aliased) edge whose
    # blends run from the red to the pink.
    try canvas.set_brush(10.0, 1.0, 1.0, 1.0, 1.0, 0.35, 0.35, false)
    try canvas.select_all()
    try canvas.fill(layer, false)
    try canvas.set_brush(10.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, false)
    try canvas.select_ellipse(100, 50, 200, 200, 0)
    try canvas.fill(layer, false)
    # The disc selected with its soft rim, then grown: the pink stays out.
    try canvas.select_ellipse(100, 50, 200, 200, 0)
    try canvas.grow_selection(layer, 48.0, true)
    assert(canvas.selected_at(200, 150) == 255 and canvas.selected_at(20, 20) == 0 and canvas.selected_at(380, 280) == 0)

test "edit fill is opaque whatever the brush's opacity":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var canvas = try Canvas(64, 64)
    defer canvas.close()
    let layer = try canvas.add_layer("Paint")
    var target = try gpu.Texture.create(device, 64, 64, gpu.Format.rgba8_linear)
    defer target.destroy()
    _ = try shown_at(&canvas, &target, 0, 0)
    # A half-opaque brush, then Edit › Fill with white over the black backdrop.
    try canvas.set_brush(10.0, 1.0, 0.5, 1.0, 1.0, 1.0, 1.0, false)
    try canvas.select_all()
    try canvas.fill(layer, false)
    let shown = try shown_at(&canvas, &target, 32, 32)
    assert(shown[0] == 255 and shown[1] == 255 and shown[2] == 255)