Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-raw

Source of release 0.8.0

0.8.0 / src / raw / develop.lucb

#==============================================================================================
#
#   raw/develop - From the balanced plane to developed tiles
#
#   DESCRIPTION:
#       The developed image (after orientation) is cut into square tiles that
#       worker threads take in turn (parallel.lucb). For each, the matching
#       rectangle of the plane is demosaiced in blocks of at most `block_size`
#       (AHD inside the five-pixel border, the border by averaging; bilinear;
#       Markesteijn for X-Trans), multiplied by the camera-to-sRGB matrix with
#       the exposure gain folded in, turned by the orientation into float32
#       RGB or RGBA and handed to a sink. Each worker keeps its own scratch;
#       tiles share nothing but the read-only plane, so the result does not
#       depend on the number of threads or the tile size.
#
#==============================================================================================

## The largest rectangle one demosaic call covers: the AHD and X-Trans
## scratch buffers are sized for it.
let block_size: usize = 256

## One worker's buffers, allocated the first time it takes a tile.
struct Workspace:
    var ahd: AhdScratch
    var xtrans: XtransScratch
    var rgb: u16[]
    var pixels: f32[]
    var ready: bool

    func prepare(job: const Job*) -> !:
        errdefer self.close()
        if job.hexagons != none:
            self.xtrans = try XtransScratch.create()
        elif job.quality == .best and job.plane.channels == 1:
            self.ahd = try AhdScratch.create(block_size)
        self.rgb = try new u16[job.tile * job.tile * 3] in memory.heap
        self.pixels = try new f32[job.tile * job.tile * job.channels] in memory.heap
        self.ready = true

    func close():
        self.ahd.close()
        self.xtrans.close()
        if self.rgb.length > 0:
            free(self.rgb) in memory.heap
        if self.pixels.length > 0:
            free(self.pixels) in memory.heap
        self = Workspace()

## The shared, read-only description of one develop.
struct Job:
    var plane: const Plane*
    var table: const LabTable*
    var quality: Quality
    var matrix: f32[9]
    var orientation: u64
    var width: usize
    var height: usize
    var tile: usize
    var across: usize
    var channels: usize
    var hexagons: const Hexagons*?
    var context: void*?
    var sink: func(void*?, const Tile*) -> unit!
    var spaces: Workspace*
    var demosaic_ns: @u64
    var output_ns: @u64

## A half-open range [start, end) of one axis.
struct Extent:
    var start: usize
    var end: usize

## The range `range` of an axis of `length`, mirrored when `flip`.
func mirrored(range: Extent, length: usize, flip: bool) -> Extent:
    return Extent(start = length - range.end, end = length - range.start) if flip else range

## The output tile of item `index`: items run down the plane's rows whatever
## the orientation, so a pipeline (pipeline.lucb) can follow the sensor data
## as it arrives.
func tile_of(job: const Job*, index: usize) -> usize:
    let o = job.orientation
    let down = (job.height + job.tile - 1) // job.tile
    if o < 5:
        let row = index // job.across
        let ty = down - 1 - row if o == 3 or o == 4 else row
        return ty * job.across + index % job.across
    let column = index // down
    let tx = job.across - 1 - column if o == 6 or o == 7 else column
    return (index % down) * job.across + tx

## The plane's rows behind item `index`: [start, end).
func source_rows(job: const Job*, index: usize) -> Extent:
    let tile = tile_of(job, index)
    let o = job.orientation
    let across = Extent(start = tile % job.across * job.tile, end = (usize)math.imin((i64)(tile % job.across * job.tile + job.tile), (i64)job.width))
    let down = Extent(start = tile // job.across * job.tile, end = (usize)math.imin((i64)(tile // job.across * job.tile + job.tile), (i64)job.height))
    return mirrored(down, job.plane.height, o == 3 or o == 4) if o < 5 else mirrored(across, job.plane.height, o == 6 or o == 7)

## Develop output tile of item `index` on `worker`; `parallel` calls it.
func develop_item(context: void*, index: usize, worker: usize) -> !:
    let job = (Job*)context
    let space = job.spaces + worker
    if not space.ready:
        try space.prepare(job)
    let plane = job.plane
    let o = job.orientation
    let number = tile_of(job, index)
    let ox0 = number % job.across * job.tile
    let oy0 = number // job.across * job.tile
    let across = Extent(start = ox0, end = (usize)math.imin((i64)(ox0 + job.tile), (i64)job.width))
    let down = Extent(start = oy0, end = (usize)math.imin((i64)(oy0 + job.tile), (i64)job.height))
    let ow = across.end - across.start
    let oh = down.end - down.start
    # The plane's rectangle behind this tile.
    let sx = mirrored(across, plane.width, o == 2 or o == 3) if o < 5 else mirrored(down, plane.width, o == 7 or o == 8)
    let sy = mirrored(down, plane.height, o == 3 or o == 4) if o < 5 else mirrored(across, plane.height, o == 6 or o == 7)
    let sw = sx.end - sx.start
    let sh = sy.end - sy.start
    let started = time.now()
    var by = sy.start
    while by < sy.end:
        let bh = (usize)math.imin((i64)block_size, (i64)(sy.end - by))
        var bx = sx.start
        while bx < sx.end:
            let bw = (usize)math.imin((i64)block_size, (i64)(sx.end - bx))
            demosaic_block(job, space, bx, by, bx + bw, by + bh, space.rgb, sw, bx - sx.start, by - sy.start)
            bx += bw
        by += bh
    let demosaiced = time.now()
    # Where the plane pixel (u, v) of the rectangle lands in the tile.
    let w = (isize)ow
    let h = (isize)oh
    var base: isize = 0
    var su: isize = 1
    var sv: isize = w
    if o == 2:
        base = w - 1
        su = -1
    elif o == 3:
        base = (h - 1) * w + w - 1
        su = -1
        sv = -w
    elif o == 4:
        base = (h - 1) * w
        sv = -w
    elif o == 5:
        su = w
        sv = 1
    elif o == 6:
        base = w - 1
        su = w
        sv = -1
    elif o == 7:
        base = (h - 1) * w + w - 1
        su = -w
        sv = -1
    elif o == 8:
        base = (h - 1) * w
        su = -w
        sv = 1
    output_rect(job, space.rgb.data, sw, sh, space.pixels.data, base, su, sv)
    let tile = Tile(x = across.start, y = down.start, width = ow, height = oh, channels = job.channels, pixels = space.pixels[..<ow * oh * job.channels])
    try job.sink(job.context, &tile)
    _ = job.demosaic_ns.add(demosaiced - started)
    _ = job.output_ns.add(time.now() - demosaiced)

## Demosaic the plane's [x0, x1) × [y0, y1) (at most `block_size` square) into
## `rgb`, rows of `stride` pixels, from (`ox`, `oy`).
func demosaic_block(job: const Job*, space: Workspace*, x0: usize, y0: usize, x1: usize, y1: usize, rgb: u16[], stride: usize, ox: usize, oy: usize):
    let plane = job.plane
    if plane.channels == 3:
        for y in y0..<y1:
            let source = (y * plane.width + x0) * 3
            memory.copy(rgb[((oy + y - y0) * stride + ox) * 3..], plane.data[source..], (x1 - x0) * 3)
        return
    if job.quality == .fast or (plane.xtrans and job.hexagons == none):
        bilinear_tile(plane, x0, y0, x1, y1, rgb, stride, ox, oy)
        return
    # AHD (or Markesteijn) inside the border, plain averages in it.
    let hexagons = job.hexagons
    let border: usize = 8 if hexagons != none else 5
    let ix0 = math.imax((i64)x0, (i64)border)
    let iy0 = math.imax((i64)y0, (i64)border)
    let ix1 = math.imin((i64)x1, (i64)plane.width - (i64)border)
    let iy1 = math.imin((i64)y1, (i64)plane.height - (i64)border)
    let inner = ix0 < ix1 and iy0 < iy1
    if inner:
        let ax = (usize)ix0 - x0 + ox
        let ay = (usize)iy0 - y0 + oy
        if let rings = hexagons:
            let m = (i64)xtrans_margin
            let top = (usize)math.imax(3, iy0 - m)
            let left = (usize)math.imax(3, ix0 - m)
            let bottom = (usize)math.imin((i64)plane.height - 3, iy1 + m)
            let right = (usize)math.imin((i64)plane.width - 3, ix1 + m)
            xtrans_tile(plane, rings, job.table, &space.xtrans, top, left, bottom, right, (usize)ix0, (usize)iy0, (usize)ix1, (usize)iy1, rgb, stride, ax, ay)
        else:
            ahd_tile(plane, job.table, &space.ahd, (usize)ix0, (usize)iy0, (usize)ix1, (usize)iy1, rgb, stride, ax, ay)
    for y in y0..<y1:
        let middle = inner and (i64)y >= iy0 and (i64)y < iy1
        var x = x0
        while x < x1:
            if middle and (i64)x == ix0:
                x = (usize)ix1
                continue
            let at = ((oy + y - y0) * stride + ox + x - x0) * 3
            mosaic_pixel(plane, x, y, false, rgb[at..<at + 3])
            x += 1

## The matrix (gain folded in) from `sw` × `sh` demosaiced pixels into the
## tile's float pixels: plane pixel (u, v) lands at `base + u·su + v·sv`.
## Negative results clip to zero; RGBA tiles get alpha one.
func output_rect(job: const Job*, rgb: const u16*, sw: usize, sh: usize, pixels: noalias f32*, base: isize, su: isize, sv: isize):
    let m = job.matrix
    let m0 = m[0]
    let m1 = m[1]
    let m2 = m[2]
    let m3 = m[3]
    let m4 = m[4]
    let m5 = m[5]
    let m6 = m[6]
    let m7 = m[7]
    let m8 = m[8]
    let ch = (isize)job.channels
    let step = su *% ch
    for v in 0..<sh:
        let row = rgb + v * sw * 3
        var q = pixels + (base +% (isize)v *% sv) *% ch
        for u in 0..<sw:
            let p = row + u *% 3
            let r = (f32)p[0]
            let g = (f32)p[1]
            let b = (f32)p[2]
            let red = m0 * r + m1 * g + m2 * b
            let green = m3 * r + m4 * g + m5 * b
            let blue = m6 * r + m7 * g + m8 * b
            q[0] = red if red > 0.0 else 0.0
            q[1] = green if green > 0.0 else 0.0
            q[2] = blue if blue > 0.0 else 0.0
            if ch == 4:
                q[3] = 1.0
            q = q + step

## Free the workers' buffers.
func release_spaces(spaces: Workspace[]):
    for i in 0..<spaces.length:
        spaces[i].close()
    free(spaces) in memory.heap

## Everything the tiles of one develop share, built before the first tile:
## the Lab table, the X-Trans hexagons and the workers' buffers.
struct DevelopStage:
    var table: LabTable
    var hexagons: Hexagons
    var spaces: Workspace[]
    var count: usize

    ## Prepare to develop `plane` (see develop_plane) and answer the Job the
    ## tiles run, which points into this stage. With `ranges` false the
    ## X-Trans green ranges are left for the caller to fill.
    func open(plane: const Plane*, look: const Development*, quality: Quality, gain: f64, orientation: u64, tile: usize, alpha: bool, threads: usize, context: void*?, sink: func(void*?, const Tile*) -> unit!, ranges: bool) -> Job!:
        errdefer self.close()
        if tile == 0 or tile > 16384:
            error(invalid, "raw tile size must be 1 to 16384")
        if quality == .best and plane.channels == 1:
            self.table = try LabTable.create(look.to_xyz)
        let o = orientation if orientation >= 1 and orientation <= 8 else 1
        let width = plane.height if o >= 5 else plane.width
        let height = plane.width if o >= 5 else plane.height
        let across = (width + tile - 1) // tile
        self.count = across * ((height + tile - 1) // tile)
        self.spaces = try new Workspace[workers(self.count, threads)] in memory.heap
        var job = Job(plane = plane, table = &self.table, quality = quality, orientation = o, width = width, height = height, tile = tile, across = across, channels = 4 if alpha else 3, context = context, sink = sink, spaces = self.spaces.data)
        let k = gain / 65535.0
        for i: usize in 0..<9:
            job.matrix[i] = (f32)(look.to_rgb[i] * k)
        # X-Trans at best quality: Markesteijn, on planes big enough for its tiles.
        if plane.xtrans and plane.channels == 1 and quality == .best and plane.width >= 32 and plane.height >= 32:
            self.hexagons = try hexagons_of(plane, threads, ranges)
            job.hexagons = &self.hexagons
        return job

    func close():
        self.table.close()
        self.hexagons.close()
        if self.spaces.length > 0:
            release_spaces(self.spaces)
        self = DevelopStage()

## Develop `plane` with `look`'s matrix and `gain` into tiles of the oriented
## image, handing each to `sink(context, tile)` from the worker threads, on
## `threads`; answers the demosaic and output time summed over the threads.
func develop_plane(plane: const Plane*, look: const Development*, quality: Quality, gain: f64, orientation: u64, tile: usize, alpha: bool, threads: usize, context: void*?, sink: func(void*?, const Tile*) -> unit!) -> (u64, u64)!:
    var stage = DevelopStage()
    defer stage.close()
    var job = try stage.open(plane, look, quality, gain, orientation, tile, alpha, threads, context, sink, true)
    try parallel(stage.count, stage.spaces.length, (void*)&job, develop_item)
    return (job.demosaic_ns.load(), job.output_ns.load())