Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-image

Source of release 0.45.0

0.45.0 / src / geometry.lucb

## Document-wide geometry, destructive: changing the canvas size (with the
## existing pixels anchored at an offset, so cropping is a canvas size with a
## negative offset) and resampling the whole image to a new size. Every raster
## layer and mask is rebuilt tile by tile on the GPU; pixels the new canvas
## does not reach are kept past it, as Compositor keeps them.
from luce_gpu import gpu
import interop
from luce_std import math
import memory
from luce_canvas.tiles import Tiles, tile_size, box, unbox
from extent import relocate
from document import Document, Layer
from luce_raster.errors import invalid

## `tiles` drawn into a `width` by `height` layer with its pixels scaled by
## (`scale_x`, `scale_y`) and its origin at (`offset_x`, `offset_y`); what
## falls past the new canvas stays past it. `encoded` resamples the colors as
## sRGB-encoded values.
func rebuilt(device: gpu.Device, tiles: Tiles*, width: u32, height: u32, scale_x: f64, scale_y: f64, offset_x: f64, offset_y: f64, smooth: bool,
             empty: gpu.Color, encoded: bool = false) -> Tiles!:
    var fresh = try Tiles.create(device, width, height)
    errdefer fresh.destroy()
    try relocate(tiles, &fresh, scale_x, scale_y, offset_x, offset_y, smooth, empty, encoded)
    return fresh

## Every layer and mask rebuilt; colors resample as sRGB-encoded values with
## `encoded` (the document's blending), masks as they are.
func rebuild_layers(device: gpu.Device, layers: Layer*[], width: u32, height: u32, scale_x: f64, scale_y: f64, offset_x: f64, offset_y: f64, smooth: bool,
                    encoded: bool) -> !:
    for layer in layers:
        if layer.group:
            try rebuild_layers(device, layer.layers(), width, height, scale_x, scale_y, offset_x, offset_y, smooth, encoded)
            continue
        if let tiles = layer.tiles():
            let replaced = try rebuilt(device, tiles, width, height, scale_x, scale_y, offset_x, offset_y, smooth, gpu.Color(0.0, 0.0, 0.0, 0.0), encoded)
            try layer.replace_tiles(replaced)
        if let mask = layer.mask_tiles():
            let replaced = try rebuilt(device, mask, width, height, scale_x, scale_y, offset_x, offset_y, smooth, gpu.Color(1.0, 1.0, 1.0, 1.0))
            try layer.replace_mask(replaced)

## Change the canvas to `width` by `height`, keeping the existing pixels with
## their top-left at (`offset_x`, `offset_y`) in the new canvas.
pub func resize_canvas(document: Document*, width: u32, height: u32, offset_x: i64, offset_y: i64) -> !:
    if width == 0 or height == 0 or width > 65536 or height > 65536:
        error(invalid, "canvas dimensions must be in 1..65536")
    try rebuild_layers(document.device(), document.layers(), width, height, 1.0, 1.0, (f64)offset_x, (f64)offset_y, false, false)
    try document.set_extent(width, height)

## Resample the whole image to `width` by `height`, smoothly (bilinear, the
## colors mixed as the document blends them) or to the nearest pixel.
pub func resize_image(document: Document*, width: u32, height: u32, smooth: bool = true) -> !:
    if width == 0 or height == 0 or width > 65536 or height > 65536:
        error(invalid, "image dimensions must be in 1..65536")
    let scale_x = (f64)width / (f64)document.width()
    let scale_y = (f64)height / (f64)document.height()
    try rebuild_layers(document.device(), document.layers(), width, height, scale_x, scale_y, 0.0, 0.0, smooth, document.mixes_encoded())
    try document.set_extent(width, height)

test "canvas and image resizing keep pixels in place, scale them, and keep what the canvas no longer reaches":
    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 + 260) * 4 + 1] = 1.0
    pixels[(10 * 300 + 260) * 4 + 3] = 1.0
    # A red dot the crop leaves past the canvas.
    pixels[(10 * 300 + 20) * 4] = 1.0
    pixels[(10 * 300 + 20) * 4 + 3] = 1.0
    var tiles = try Tiles.create(device, 300, 40)
    try tiles.write(pixels)
    try document.set_tiles(layer, tiles)
    # Crop to x 250..300, y 0..40: the dot lands at (10, 10).
    try resize_canvas(&document, 50, 40, -250, 0)
    assert(document.width() == 50 and document.height() == 40)
    let cropped = try new f32[(usize)50 * 40 * 4] --- in memory.heap
    defer free(cropped) in memory.heap
    try ((document.find(layer) else trap("layer")).tiles() else trap("tiles")).read(cropped)
    assert(cropped[(10 * 50 + 10) * 4 + 1] > 0.99)
    # Grow to 100 x 80 with the old canvas at (20, 20): the dot is at (30, 30).
    try resize_canvas(&document, 100, 80, 20, 20)
    let grown = try new f32[(usize)100 * 80 * 4] --- in memory.heap
    defer free(grown) in memory.heap
    try ((document.find(layer) else trap("layer")).tiles() else trap("tiles")).read(grown)
    assert(grown[(30 * 100 + 30) * 4 + 1] > 0.99 and grown[(10 * 100 + 10) * 4 + 3] == 0.0)
    # Double the image: the dot spreads around (60, 60).
    try resize_image(&document, 200, 160)
    let doubled = try new f32[(usize)200 * 160 * 4] --- in memory.heap
    defer free(doubled) in memory.heap
    try ((document.find(layer) else trap("layer")).tiles() else trap("tiles")).read(doubled)
    assert(doubled[(60 * 200 + 60) * 4 + 3] > 0.2 and doubled[(30 * 200 + 30) * 4 + 3] == 0.0)
    # The red dot went to (-230, 10), (-210, 30), then (-420, 60) scaled; a
    # canvas grown 440 to the left shows it again at (20, 60).
    assert(((document.find(layer) else trap("layer")).tiles() else trap("tiles")).has_outside())
    try resize_canvas(&document, 700, 160, 440, 0)
    let revealed = try new f32[(usize)700 * 160 * 4] --- in memory.heap
    defer free(revealed) in memory.heap
    try ((document.find(layer) else trap("layer")).tiles() else trap("tiles")).read(revealed)
    assert(revealed[(60 * 700 + 20) * 4] > 0.2 and revealed[(60 * 700 + 20) * 4 + 3] > 0.2 and revealed[(60 * 700 + 500) * 4 + 3] > 0.2)