Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-image

Source of release 0.45.1

0.45.1 / src / composite / tests.lucb

#==============================================================================================
#
#   composite/tests - The compositor tests
#
#   DESCRIPTION:
#       Flattening with opacity, the identity cache and levels, styles, and blur across cells.
#
#==============================================================================================

test "composite flattens layers with opacity, caches by identity and scales levels":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var document = try Document.create(device, 300, 260)
    defer document.destroy()
    let count = (usize)300 * 260 * 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
    # Bottom: opaque red over the top-left 300x260; top: 50% green pixel at (20, 10).
    for index in 0..<(usize)(300 * 260):
        pixels[index * 4] = 1.0
        pixels[index * 4 + 3] = 1.0
    let bottom = try document.add_layer("Bottom")
    var red = try Tiles.create(device, 300, 260)
    try red.write(pixels)
    try document.set_tiles(bottom, red)
    for index in 0..<count:
        pixels[index] = 0.0
    pixels[(10 * 300 + 20) * 4 + 1] = 1.0
    pixels[(10 * 300 + 20) * 4 + 3] = 0.5
    let top = try document.add_layer("Top")
    var green = try Tiles.create(device, 300, 260)
    try green.write(pixels)
    try document.set_tiles(top, green)
    var composer = try Composer.create(device, 4)
    defer composer.destroy()
    let flat = try composer.tile(&document, 0, 0, 0)
    assert(composer.renders == 1)
    var bytes: u8[8]
    try flat.read(bytes, gpu.Region(20, 10, 1, 1))
    let r = from_half((u16)bytes[0] | ((u16)bytes[1] << 8))
    let g = from_half((u16)bytes[2] | ((u16)bytes[3] << 8))
    let a = from_half((u16)bytes[6] | ((u16)bytes[7] << 8))
    # Mixed as sRGB-encoded values: half of each encoded, 0.214 in linear light.
    assert(r > 0.209 and r < 0.219 and g > 0.209 and g < 0.219 and a == 1.0)
    # The same request is a cache hit; a change to the top layer is not, and
    # hiding it leaves the bottom-only tile at (1, 1) untouched.
    _ = try composer.tile(&document, 0, 0, 0)
    assert(composer.renders == 1)
    _ = try composer.tile(&document, 1, 1, 0)
    assert(composer.renders == 2)
    try document.set_appearance(top, true, 0.25)
    _ = try composer.tile(&document, 0, 0, 0)
    assert(composer.renders == 3)
    _ = try composer.tile(&document, 1, 1, 0)
    assert(composer.renders == 3)
    # Level 1 covers the whole document in one tile.
    let coarse = try composer.tile(&document, 0, 0, 1)
    try coarse.read(bytes, gpu.Region(100, 100, 1, 1))
    assert(from_half((u16)bytes[0] | ((u16)bytes[1] << 8)) > 0.99)
    try coarse.read(bytes, gpu.Region(200, 200, 1, 1))
    assert(from_half((u16)bytes[6] | ((u16)bytes[7] << 8)) == 0.0)
    # Multiply: half-alpha green over red blends to black where it lands, so
    # the pixel is half red; the cache key sees the mode change.
    try document.set_appearance(top, true, 1.0, 2)
    let multiplied = try composer.tile(&document, 0, 0, 0)
    try multiplied.read(bytes, gpu.Region(20, 10, 1, 1))
    let red_left = from_half((u16)bytes[0] | ((u16)bytes[1] << 8))
    assert(red_left > 0.209 and red_left < 0.219 and from_half((u16)bytes[2] | ((u16)bytes[3] << 8)) < 0.01)
    assert(from_half((u16)bytes[6] | ((u16)bytes[7] << 8)) == 1.0)
    # A mask painted black at the pixel hides the top layer there; clipping the
    # top layer to an empty layer below hides it everywhere.
    try document.set_appearance(top, true, 1.0, 0)
    try document.set_mask(top, true)
    var hide = try Tiles.create(device, 300, 260)
    for index in 0..<count:
        pixels[index] = 0.0
    for index in 0..<(usize)(300 * 260):
        pixels[index * 4 + 3] = 1.0
    pixels[(10 * 300 + 20) * 4 + 3] = 1.0
    pixels[(10 * 300 + 21) * 4] = 1.0
    pixels[(10 * 300 + 21) * 4 + 3] = 1.0
    try hide.write(pixels)
    let old_mask = try document.take_mask(top, hide)
    var dropped = old_mask
    dropped.destroy()
    let masked = try composer.tile(&document, 0, 0, 0)
    try masked.read(bytes, gpu.Region(20, 10, 1, 1))
    assert(from_half((u16)bytes[0] | ((u16)bytes[1] << 8)) > 0.99 and from_half((u16)bytes[2] | ((u16)bytes[3] << 8)) < 0.01)
    try document.set_mask(top, false)
    let between = try document.add_layer("Empty")
    try document.remove(between)
    let holder = try document.add_group("Holder")
    let clipped = try document.add_layer("Clipped", holder)
    try document.set_tiles(clipped, try green.clone())
    try document.set_clipped(clipped, true)
    let alone = try composer.tile(&document, 0, 0, 0)
    try alone.read(bytes, gpu.Region(20, 10, 1, 1))
    # The top (unclipped, green at half alpha) still shows; the clipped copy in
    # the group has no base and adds nothing: red half encoded as before.
    let clipped_red = from_half((u16)bytes[0] | ((u16)bytes[1] << 8))
    assert(clipped_red > 0.209 and clipped_red < 0.219)
    # Blending with gamma 1.0 mixes linear light: half of each, and a new key.
    document.linear_blend = true
    let linear = try composer.tile(&document, 0, 0, 0)
    try linear.read(bytes, gpu.Region(20, 10, 1, 1))
    let linear_red = from_half((u16)bytes[0] | ((u16)bytes[1] << 8))
    assert(linear_red > 0.49 and linear_red < 0.51)
    document.linear_blend = false
    var rejected = false
    _ = composer.tile(&document, 4, 0, 0) catch failure:
        assert(failure.code == invalid)
        rejected = true
        recover coarse
    assert(rejected)

test "a styled layer's glow is composited under it":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var document = try Document.create(device, 64, 64)
    defer document.destroy()
    let count = (usize)64 * 64 * 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
    for y in 24..<(usize)40:
        for x in 24..<(usize)40:
            pixels[(y * 64 + x) * 4] = 1.0
            pixels[(y * 64 + x) * 4 + 3] = 1.0
    let id = try document.add_layer("Square")
    var tiles = try Tiles.create(device, 64, 64)
    try tiles.write(pixels)
    try document.set_tiles(id, tiles)
    var composer = try Composer.create(device, 4)
    defer composer.destroy()
    var plain = try composer.tile(&document, 0, 0, 0)
    let bytes = try new u8[(usize)tile_size * (usize)tile_size * 8] --- in memory.heap
    defer free(bytes) in memory.heap
    try plain.read(bytes)
    assert(bytes[(32 * 256 + 12) * 8 + 7] == 0)
    (document.find(id) else trap("layer")).style.glow = true
    (document.find(id) else trap("layer")).style.glow_size = 10.0
    var glowing = try composer.tile(&document, 0, 0, 0)
    try glowing.read(bytes)
    # The half at byte 6..7 is alpha; a glow beside the square makes it nonzero.
    assert(bytes[(32 * 256 + 20) * 8 + 7] != 0)
    assert(composer.renders == 2)

test "a blur layer softens what is below it across cell edges, and the cache follows its neighbours":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var document = try Document.create(device, 512, 64)
    defer document.destroy()
    let layer = try document.add_layer("Bar")
    let count = (usize)512 * 64 * 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
    # Opaque white on the left cell up to its edge.
    for y in 0..<(usize)64:
        for x in 0..<(usize)256:
            for channel in 0..<(usize)4:
                pixels[(y * 512 + x) * 4 + channel] = 1.0
    var tiles = try Tiles.create(device, 512, 64)
    try tiles.write(pixels)
    try document.set_tiles(layer, tiles)
    let values: f64[1] = [6.0]
    _ = try document.add_adjustment("Blur", (i64)blur, values)
    var composer = try Composer.create(device)
    defer composer.destroy()
    let right = try composer.tile(&document, 1, 0, 0)
    let bytes = try new u8[(usize)tile_size * (usize)tile_size * 8] --- in memory.heap
    defer free(bytes) in memory.heap
    try right.read(bytes)
    # Just right of the edge the white bleeds in (alpha half in f16 bytes 6..7).
    let at = ((usize)32 * (usize)tile_size + 2) * 8
    let alpha = from_half((u16)bytes[at + 6] | ((u16)bytes[at + 7] << 8))
    assert(alpha > 0.1 and alpha < 0.6)
    # Painting the left cell changes the right cell's key.
    let before = composer.renders
    for y in 0..<(usize)64:
        for x in 0..<(usize)256:
            pixels[(y * 512 + x) * 4 + 3] = 0.0
    let current = (document.find(layer) else trap("layer")).tiles() else trap("tiles")
    try current.write(pixels)
    _ = try composer.tile(&document, 1, 0, 0)
    assert(composer.renders == before + 1)