dymokomi/luce-raw
Source of release 0.8.1
0.8.1 / src / raw / xtrans_tile.lucb
#==============================================================================================
#
# raw/xtrans_tile - One tile of the X-Trans demosaic
#
# DESCRIPTION:
# The steps of LibRaw's xtrans_interpolate for one region of the plane, in
# its integer arithmetic: green in four directions, two refinement passes on
# a copy (directions four to seven), red and blue for solitary greens, for
# each other, and for 2×2 green blocks, then CIELAB derivatives, homogeneity
# and the average of the best directions. Region coordinates are absolute;
# buffers are `xtrans_side` square.
#
#==============================================================================================
## Buffers for one tile: the plane as LibRaw's image holds it (with a margin of
## `source_margin`), eight directions of RGB, their derivatives and
## homogeneity, and one direction's CIELAB.
struct XtransScratch:
var source: u16[]
var rgb: u16[]
var lab: i32[]
var derivative: f32[]
var homogeneity: u8[]
var sums: u16[]
static func create() -> XtransScratch!:
let area = xtrans_side * xtrans_side
var scratch = XtransScratch()
errdefer scratch.close()
scratch.source = try new u16[source_side * source_side * 3] in memory.heap
scratch.rgb = try new u16[xtrans_directions * area * 3] in memory.heap
scratch.lab = try new i32[area * 3] in memory.heap
scratch.derivative = try new f32[xtrans_directions * area] in memory.heap
scratch.homogeneity = try new u8[xtrans_directions * area] in memory.heap
scratch.sums = try new u16[xtrans_directions * area] in memory.heap
return scratch
func close():
if self.source.length > 0:
free(self.source) in memory.heap
if self.rgb.length > 0:
free(self.rgb) in memory.heap
if self.lab.length > 0:
free(self.lab) in memory.heap
if self.derivative.length > 0:
free(self.derivative) in memory.heap
if self.homogeneity.length > 0:
free(self.homogeneity) in memory.heap
if self.sums.length > 0:
free(self.sums) in memory.heap
self = XtransScratch()
inline func clamp16(value: i64) -> u16:
return 0 if value < 0 else 65535 if value > 65535 else (u16)value
## LibRaw's LIM: `value` no higher than `high`, then no lower than `low`;
## branch-free, as whether a value leaves its hexagon's range is data.
inline func bounded(value: i64, low: u16, high: u16) -> u16:
return (u16)max64((i64)low, min64(value, (i64)high))
## Demosaic the region [top, bottom) × [left, right) and write the rectangle
## [x0, x1) × [y0, y1) of it to `out` (RGB rows of `stride` pixels from
## (`ox`, `oy`)).
func xtrans_tile(plane: const Plane*, rings: const Hexagons*, table: const LabTable*, scratch: XtransScratch*, top: usize, left: usize, bottom: usize, right: usize, x0: usize, y0: usize, x1: usize, y1: usize, out: u16[], stride: usize, ox: usize, oy: usize):
let ts = (i64)xtrans_side
let area = xtrans_side * xtrans_side
let rgb = scratch.rgb
let w = (i64)plane.width
let t = (i64)top
let l = (i64)left
let mrow = (i64)bottom
let mcol = (i64)right
assert(bottom - top <= xtrans_side and right - left <= xtrans_side and bottom <= plane.height and right <= plane.width)
# The source: each pixel as LibRaw's image holds it, its own color and, on
# red and blue pixels, green's lower limit; the green step reads it up to
# six pixels beyond the region.
let m = (i64)source_margin
let ss = source_side
let source = scratch.source.data
let low = rings.low.data
let high = rings.high.data
for row in math.imax(0, t - m)..<math.imin((i64)plane.height, mrow + m):
let colors = &plane.pattern[(usize)(row % 6) * 6]
let first = math.imax(0, l - m)
var phase = (usize)(first % 6)
let at = (usize)(row * w)
var q = source + ((usize)(row - t + m) * ss + (usize)(first - l + m)) * 3
for col in first..<math.imin(w, mcol + m):
let own = (usize)colors[phase]
phase = 0 if phase == 5 else phase + 1
let i = at + (usize)col
q[0] = 0
q[1] = 0
q[2] = 0
q[own] = plane.data[i]
if own != 1:
q[1] = low[i]
q = q + 3
# Direction 0 is the region of the source; 1 to 3 copy it.
for row in t..<mrow:
let line = source + ((usize)(row - t + m) * ss + (usize)m) * 3
memory.copy(rgb[(usize)((row - t) * ts) * 3..], u16[](line, (usize)(mcol - l) * 3), (usize)(mcol - l) * 3)
for d: usize in 1..<4:
memory.copy(rgb[d * area * 3..], rgb[..<area * 3], area * 3)
# Green horizontally, vertically and along both diagonals.
let data = rgb.data
for row in t..<mrow:
let colors = &plane.pattern[(usize)(row % 6) * 6]
let flip: usize = 1 if (row - (i64)rings.solitary_row) % 3 == 0 else 0
let row_base = (usize)(row % 3) * 3
var phase = (usize)(l % 6)
for col in l..<mcol:
let f = (isize)colors[phase]
let base = (row_base + phase % 3) * 8
phase = 0 if phase == 5 else phase + 1
if f == 1:
continue
let s = source + ((usize)(row - t + m) * ss + (usize)(col - l + m)) * 3
let hx = &rings.source[base]
let h0 = (isize)hx[0] * 3
let h1 = (isize)hx[1] * 3
let h2 = (isize)hx[2] * 3
let h3 = (isize)hx[3] * 3
let h4 = (isize)hx[4] * 3
let h5 = (isize)hx[5] * 3
let own = (i32)*(s + f)
let color0 = 174 *% ((i32)*(s + (h1 + 1)) +% (i32)*(s + (h0 + 1))) -% 46 *% ((i32)*(s + (2 * h1 + 1)) +% (i32)*(s + (2 * h0 + 1)))
let color1 = 223 *% (i32)*(s + (h3 + 1)) +% (i32)*(s + (h2 + 1)) *% 33 +% 92 *% (own -% (i32)*(s + (f - h2)))
let color2 = 164 *% (i32)*(s + (h4 + 1)) +% 92 *% (i32)*(s + (1 - 2 * h4)) +% 33 *% (2 *% own -% (i32)*(s + (3 * h4 + f)) -% (i32)*(s + (f - 3 * h4)))
let color3 = 164 *% (i32)*(s + (h5 + 1)) +% 92 *% (i32)*(s + (1 - 2 * h5)) +% 33 *% (2 *% own -% (i32)*(s + (3 * h5 + f)) -% (i32)*(s + (f - 3 * h5)))
let i = (usize)(row * w + col)
let o = (usize)((row - t) * ts + col - l) * 3 + 1
let lo = low[i]
let hi = high[i]
data[flip * area * 3 + o] = bounded((i64)(color0 >> 8), lo, hi)
data[(1 ^ flip) * area * 3 + o] = bounded((i64)(color1 >> 8), lo, hi)
data[(2 ^ flip) * area * 3 + o] = bounded((i64)(color2 >> 8), lo, hi)
data[(3 ^ flip) * area * 3 + o] = bounded((i64)(color3 >> 8), lo, hi)
for pass: usize in 0..<3:
# From the second pass on, directions 4 to 7 are refined copies.
let shift = 0 if pass == 0 else 4 * area * 3
if pass == 1:
memory.copy(rgb[shift..], rgb[..<shift], shift)
let work = rgb[shift..]
if pass > 0:
xtrans_refine_green(plane, rings, work, t, l, mrow, mcol)
xtrans_solitary(plane, rings, work, t, l, mrow, mcol)
xtrans_opposite(plane, rings, work, t, l, mrow, mcol)
xtrans_blocks(plane, rings, work, t, l, mrow, mcol)
xtrans_choose(plane, rings, table, scratch, t, l, mrow - t, mcol - l, x0, y0, x1, y1, out, stride, ox, oy)
## Recalculate green from the interpolated values of closer pixels.
func xtrans_refine_green(plane: const Plane*, rings: const Hexagons*, rgb: u16[], t: i64, l: i64, mrow: i64, mcol: i64):
let area = xtrans_side * xtrans_side
let w = (i64)plane.width
let data = rgb.data
let low = rings.low.data
let high = rings.high.data
var offsets: isize[72]
for i: usize in 0..<72:
offsets[i] = (isize)rings.tile[i] * 3
for row in t + 2..<mrow - 2:
let colors = &plane.pattern[(usize)(row % 6) * 6]
let flip: usize = 1 if (row - (i64)rings.solitary_row) % 3 == 0 else 0
let row_base = (usize)(row % 3) * 3
var phase = (usize)((l + 2) % 6)
for col in l + 2..<mcol - 2:
let f = (isize)colors[phase]
let base = (row_base + phase % 3) * 8
phase = 0 if phase == 5 else phase + 1
if f == 1:
continue
let i = (usize)(row * w + col)
let at = (usize)tile_index(row, col, t, l)
let lo = low[i]
let hi = high[i]
for d: usize in 3..<6:
let hd = offsets[base + d]
let q = data + (((d - 2) ^ flip) * area * 3 + at)
let a = q - 2 * hd
let b = q + hd
let value = (i32)a[1] +% 2 *% (i32)b[1] -% (i32)*(a + f) -% 2 *% (i32)*(b + f) +% 3 *% (i32)*(q + f)
q[1] = bounded((i64)third32(value), lo, hi)