dymokomi/luce-raw
Source of release 0.8.1
0.8.1 / src / raw / bilinear.lucb
#==============================================================================================
#
# raw/bilinear - The fast demosaic and the border every demosaic shares
#
# DESCRIPTION:
# bilinear_pixel fills a pixel's two missing colors from its 3×3 neighbours,
# orthogonal ones weighted twice the diagonal, as dcraw's lin_interpolate.
# border_pixel is dcraw's border_interpolate: an unweighted mean of the
# neighbours of each missing color, used where AHD lacks the pixels it reads.
# Both skip neighbours outside the plane, so they work at every edge, and
# both work on X-Trans, widening to 5×5 where a 3×3 lacks a color.
#
#==============================================================================================
## Pixel (`x`, `y`) of a mosaic plane as RGB, missing colors averaged from its
## 3×3 neighbours: weighted when `weighted` (bilinear), plainly otherwise.
func mosaic_pixel(plane: const Plane*, x: usize, y: usize, weighted: bool, out: u16[]):
var sums: u32[3]
var weights: u32[3]
let own = plane.color(x, y)
for dy: usize in 0..<3:
if y + dy < 1 or y + dy - 1 >= plane.height:
continue
let yy = y + dy - 1
for dx: usize in 0..<3:
if x + dx < 1 or x + dx - 1 >= plane.width or (dx == 1 and dy == 1):
continue
let xx = x + dx - 1
let c = plane.color(xx, yy)
let w: u32 = ((u32)1 << ((u32)(dx == 1) + (u32)(dy == 1))) if weighted else 1
sums[c] += (u32)plane.data[yy * plane.width + xx] * w
weights[c] += w
# An X-Trans 3×3 can miss a color; widen to 5×5 for it.
for c: usize in 0..<3:
if weights[c] == 0 and c != own:
for yy in (0 if y < 2 else y - 2)..<(y + 3 if y + 3 <= plane.height else plane.height):
for xx in (0 if x < 2 else x - 2)..<(x + 3 if x + 3 <= plane.width else plane.width):
if plane.color(xx, yy) == c:
sums[c] += (u32)plane.data[yy * plane.width + xx]
weights[c] += 1
for c: usize in 0..<3:
out[c] = (u16)(sums[c] // weights[c]) if weights[c] > 0 else 0
out[own] = plane.data[y * plane.width + x]
## The bilinear demosaic of [x0, x1) × [y0, y1) into `out`, RGB rows of
## `stride` pixels from (`ox`, `oy`). A Bayer filter (greens on a diagonal)
## takes the direct kernel inside a one-pixel border; everything else goes
## through `mosaic_pixel`.
func bilinear_tile(plane: const Plane*, x0: usize, y0: usize, x1: usize, y1: usize, out: u16[], stride: usize, ox: usize, oy: usize):
let c = plane.cfa
let bayer = not plane.xtrans and ((c[0] == 1 and c[3] == 1 and c[1] != 1 and c[2] != 1 and c[1] != c[2]) or (c[1] == 1 and c[2] == 1 and c[0] != 1 and c[3] != 1 and c[0] != c[3]))
for y in y0..<y1:
let inner = bayer and y >= 1 and y + 1 < plane.height
var x = x0
while x < x1:
let at = ((oy + y - y0) * stride + ox + x - x0) * 3
if inner and x >= 1 and x + 1 < plane.width:
let end = (usize)math.imin((i64)x1, (i64)plane.width - 1)
bayer_row(plane, x, end, y, out.data + at)
x = end
continue
mosaic_pixel(plane, x, y, true, out[at..<at + 3])
x += 1
## Bilinear on Bayer pixels [x0, x1) of row `y` (inside the one-pixel border)
## into RGB at `out`: a red or blue pixel averages its four greens and its
## four diagonal neighbours; a green one averages each color across the row
## and down the column. Exactly `mosaic_pixel`'s weighted sums, divided.
func bayer_row(plane: const Plane*, x0: usize, x1: usize, y: usize, out: noalias u16*):
let w = plane.width
let row = plane.data.data + y * w
let first = (y & 1) * 2
let below = ((y + 1) & 1) * 2
var q = out
for x in x0..<x1:
let p = row + x
let own = (usize)plane.cfa[first + (x & 1)]
if own == 1:
let across = (usize)plane.cfa[first + ((x + 1) & 1)]
let down = (usize)plane.cfa[below + (x & 1)]
q[1] = *p
q[across] = (u16)(((u32)*(p - 1) +% (u32)*(p + 1)) >> 1)
q[down] = (u16)(((u32)*(p - w) +% (u32)*(p + w)) >> 1)
else:
q[own] = *p
q[1] = (u16)(((u32)*(p - 1) +% (u32)*(p + 1) +% (u32)*(p - w) +% (u32)*(p + w)) >> 2)
q[2 - own] = (u16)(((u32)*(p - w - 1) +% (u32)*(p - w + 1) +% (u32)*(p + w - 1) +% (u32)*(p + w + 1)) >> 2)
q = q + 3