dymokomi/luce-raw
Source of release 0.8.0
#==============================================================================================
#
# raw/ahd - Adaptive homogeneity-directed demosaicing
#
# DESCRIPTION:
# Hirakawa and Parks' AHD as dcraw and LibRaw implement it, in the same integer
# arithmetic so results agree with them: green is interpolated once
# horizontally and once vertically, red and blue follow each green, both
# candidates go to CIELAB, and each pixel takes the direction whose 3×3
# neighbourhood is more homogeneous in lightness and chroma. Work is per tile;
# a tile reads up to five pixels beyond its rectangle, so the five-pixel border
# of the image is filled by averaging neighbours instead (border.lucb).
#
#==============================================================================================
## CIELAB in dcraw's integer scale (64 per unit) from 16-bit camera RGB: XYZ in
## 16.16 fixed point, then tables of the cube-root response already scaled
## for L*, a* and b*, so no pixel needs a float conversion.
struct LabTable:
var lightness: i32[]
var red_green: i32[]
var blue_yellow: i32[]
var to_xyz: i64[9]
static func create(to_xyz: f64[9]) -> LabTable!:
var table = LabTable()
errdefer table.close()
table.lightness = try new i32[65536] in memory.heap
table.red_green = try new i32[65536] in memory.heap
table.blue_yellow = try new i32[65536] in memory.heap
for i in 0..<table.lightness.length:
let r = (f64)i / 65535.0
let f = math.cbrt(r) if r > 0.008856 else 7.787 * r + 16.0 / 116.0
table.lightness[i] = (i32)(64.0 * (116.0 * f - 16.0))
table.red_green[i] = (i32)math.round(64.0 * 500.0 * f)
table.blue_yellow[i] = (i32)math.round(64.0 * 200.0 * f)
for i: usize in 0..<9:
table.to_xyz[i] = (i64)math.round(math.clamp(to_xyz[i], -1000.0, 1000.0) * 65536.0)
return table
func close():
if self.lightness.length > 0:
free(self.lightness) in memory.heap
if self.red_green.length > 0:
free(self.red_green) in memory.heap
if self.blue_yellow.length > 0:
free(self.blue_yellow) in memory.heap
self = LabTable()
## An index clamped to 16-bit sample range.
inline func clip16(value: i64) -> usize:
return 0 if value < 0 else 65535 if value > 65535 else (usize)value
## A value clamped to 16-bit sample range.
inline func clamp_sample(value: i32) -> u16:
return 0 if value < 0 else 65535 if value > 65535 else (u16)value
## Scratch for one tile: two directions of RGB, Lab and homogeneity over the
## tile and a three-pixel apron.
struct AhdScratch:
var rgb: u16[]
var lab: i32[]
var homogeneity: u8[]
var capacity: usize
static func create(tile: usize) -> AhdScratch!:
let size = (tile + 6) * (tile + 6)
var scratch = AhdScratch(capacity = size)
errdefer scratch.close()
scratch.rgb = try new u16[size * 6] in memory.heap
scratch.lab = try new i32[size * 6] in memory.heap
scratch.homogeneity = try new u8[size * 2] in memory.heap
return scratch
func close():
if self.rgb.length > 0:
free(self.rgb) in memory.heap
if self.lab.length > 0:
free(self.lab) in memory.heap
if self.homogeneity.length > 0:
free(self.homogeneity) in memory.heap
self = AhdScratch()
## AHD for the rectangle [x0, x1) × [y0, y1), which must lie at least five
## pixels inside the plane; results go to `out` as RGB rows of `out_stride`
## pixels starting at (`ox`, `oy`) of `out`'s frame. The work arrays cover the
## rectangle and a three-pixel apron; direction 0 is horizontal, 1 vertical.
## The kernels below index with pointers: every offset they form stays inside
## the scratch (sized for the tile plus apron) or the plane (five pixels of
## margin), which this function's bounds establish once.
func ahd_tile(plane: const Plane*, table: const LabTable*, scratch: AhdScratch*, x0: usize, y0: usize, x1: usize, y1: usize, out: u16[], out_stride: usize, ox: usize, oy: usize):
let tw = x1 - x0 + 6
let th = y1 - y0 + 6
let size = tw * th
assert(size <= scratch.capacity and x0 >= 5 and y0 >= 5 and x1 + 5 <= plane.width and y1 + 5 <= plane.height)
assert((oy + y1 - y0 - 1) * out_stride + ox + x1 - x0 <= out.length // 3)
let horizontal = scratch.rgb.data
let vertical = scratch.rgb.data + size * 3
let lab_h = scratch.lab.data
let lab_v = scratch.lab.data + size * 3
let homogeneous_h = scratch.homogeneity.data
let homogeneous_v = scratch.homogeneity.data + size
ahd_green(plane, horizontal, vertical, x0 - 3, y0 - 3, tw, th)
ahd_red_blue(plane, table, horizontal, lab_h, x0 - 3, y0 - 3, tw, th)
ahd_red_blue(plane, table, vertical, lab_v, x0 - 3, y0 - 3, tw, th)
ahd_homogeneity(lab_h, lab_v, homogeneous_h, homogeneous_v, tw, th)
ahd_combine(horizontal, vertical, homogeneous_h, homogeneous_v, tw, th, out.data + (oy * out_stride + ox) * 3, out_stride)
## Green at every red and blue pixel, interpolated along the row into
## `horizontal` and along the column into `vertical`, limited to the range of
## its two neighbours.
func ahd_green(plane: const Plane*, horizontal: noalias u16*, vertical: noalias u16*, bx: usize, by: usize, tw: usize, th: usize):
let w = plane.width
let w2 = w * 2
for ty in 0..<th:
let y = by + ty
let first = (y & 1) * 2
let source = plane.data.data + (y * w + bx)
let hrow = horizontal + (ty * tw * 3 + 1)
let vrow = vertical + (ty * tw * 3 + 1)
for parity: usize in 0..<2:
if plane.cfa[first + ((bx + parity) & 1)] == 1:
continue
var tx = parity
while tx < tw:
let s = source + tx
let v = (i32)*s
let left = (i32)*(s - 1)
let right = (i32)*(s + 1)
let h = ((left +% v +% right) *% 2 -% (i32)*(s - 2) -% (i32)*(s + 2)) >> 2
let low_h = min32(left, right)
let high_h = max32(left, right)
hrow[tx *% 3] = (u16)max32(low_h, min32(h, high_h))
let up = (i32)*(s - w)
let down = (i32)*(s + w)
let u = ((up +% v +% down) *% 2 -% (i32)*(s - w2) -% (i32)*(s + w2)) >> 2
let low_v = min32(up, down)
let high_v = max32(up, down)
vrow[tx *% 3] = (u16)max32(low_v, min32(u, high_v))
tx +%= 2
## Red and blue from one direction's green, and the CIELAB of the result.
func ahd_red_blue(plane: const Plane*, table: const LabTable*, rgb: noalias u16*, lab: noalias i32*, bx: usize, by: usize, tw: usize, th: usize):
let w = plane.width
let stride = tw * 3
let m0 = table.to_xyz[0]
let m1 = table.to_xyz[1]
let m2 = table.to_xyz[2]
let m3 = table.to_xyz[3]
let m4 = table.to_xyz[4]
let m5 = table.to_xyz[5]
let m6 = table.to_xyz[6]
let m7 = table.to_xyz[7]
let m8 = table.to_xyz[8]
let lightness = table.lightness.data
let red_green = table.red_green.data
let blue_yellow = table.blue_yellow.data
for ty in 1..<th - 1:
let y = by + ty
let first = (y & 1) * 2
let below = ((y + 1) & 1) * 2
let source = plane.data.data + (y * w + bx)
let row = rgb + ty * stride
let lab_row = lab + ty * stride
for parity: usize in 1..<3:
let own = (usize)plane.cfa[first + ((bx + parity) & 1)]
let c = (usize)plane.cfa[below + ((bx + parity) & 1)] if own == 1 else 2 - own
var tx = parity
while tx < tw - 1:
let s = source + tx
let q = row + tx *% 3
let sample = *s
if own == 1:
let g = (i32)sample
let across = g +% (((i32)*(s - 1) +% (i32)*(s + 1) -% (i32)*(q - 2) -% (i32)*(q + 4)) >> 1)
q[2 - c] = clamp_sample(across)
let down = g +% (((i32)*(s - w) +% (i32)*(s + w) -% (i32)*(q - stride + 1) -% (i32)*(q + stride + 1)) >> 1)
q[c] = clamp_sample(down)
else:
let corners = (i32)*(s - w - 1) +% (i32)*(s - w + 1) +% (i32)*(s + w - 1) +% (i32)*(s + w + 1)
let up = q - stride
let under = q + stride
let greens = (i32)*(up - 2) +% (i32)*(up + 4) +% (i32)*(under - 2) +% (i32)*(under + 4)
q[c] = clamp_sample((i32)q[1] +% ((corners -% greens +% 1) >> 2))
q[own] = sample
let r = (i64)q[0]
let g = (i64)q[1]
let b = (i64)q[2]
let fx = clip16((m0 *% r +% m1 *% g +% m2 *% b +% 32768) >> 16)
let fy = clip16((m3 *% r +% m4 *% g +% m5 *% b +% 32768) >> 16)
let fz = clip16((m6 *% r +% m7 *% g +% m8 *% b +% 32768) >> 16)
let l = lab_row + tx *% 3
l[0] = lightness[fy]
l[1] = red_green[fx] -% red_green[fy]
l[2] = blue_yellow[fy] -% blue_yellow[fz]
tx +%= 2
## For each pixel and direction, how many of its four neighbours lie within
## the tighter direction's lightness and chroma tolerance. Branch-free: which
## neighbours pass is data, and mispredicted branches would dominate.
func ahd_homogeneity(lab_h: const i32*, lab_v: const i32*, out_h: noalias u8*, out_v: noalias u8*, tw: usize, th: usize):
let row = tw * 3
for ty in 2..<th - 2:
for tx in 2..<tw - 2:
let at = ty *% tw +% tx
let h = lab_h + at *% 3
let v = lab_v + at *% 3
# Horizontal: left, right, up, down.
let hl0 = difference_l(h, h - 3)
let hl1 = difference_l(h, h + 3)
let hl2 = difference_l(h, h - row)
let hl3 = difference_l(h, h + row)
let ha0 = difference_ab(h, h - 3)
let ha1 = difference_ab(h, h + 3)
let ha2 = difference_ab(h, h - row)
let ha3 = difference_ab(h, h + row)
let vl0 = difference_l(v, v - 3)
let vl1 = difference_l(v, v + 3)
let vl2 = difference_l(v, v - row)
let vl3 = difference_l(v, v + row)
let va0 = difference_ab(v, v - 3)
let va1 = difference_ab(v, v + 3)
let va2 = difference_ab(v, v - row)
let va3 = difference_ab(v, v + row)
let leps = min32(max32(hl0, hl1), max32(vl2, vl3))
let abeps = min64(max64(ha0, ha1), max64(va2, va3))
out_h[at] = ((u8)(hl0 <= leps) & (u8)(ha0 <= abeps)) +% ((u8)(hl1 <= leps) & (u8)(ha1 <= abeps)) +% ((u8)(hl2 <= leps) & (u8)(ha2 <= abeps)) +% ((u8)(hl3 <= leps) & (u8)(ha3 <= abeps))
out_v[at] = ((u8)(vl0 <= leps) & (u8)(va0 <= abeps)) +% ((u8)(vl1 <= leps) & (u8)(va1 <= abeps)) +% ((u8)(vl2 <= leps) & (u8)(va2 <= abeps)) +% ((u8)(vl3 <= leps) & (u8)(va3 <= abeps))
## Branch-free maxima and minima of small values (their difference fits).
inline func max32(a: i32, b: i32) -> i32:
let d = a -% b
return b +% (d & ~(d >> 31))
inline func min32(a: i32, b: i32) -> i32:
let d = a -% b
return b +% (d & (d >> 31))
inline func max64(a: i64, b: i64) -> i64:
let d = a -% b
return b +% (d & ~(d >> 63))
inline func min64(a: i64, b: i64) -> i64:
let d = a -% b
return b +% (d & (d >> 63))
inline func difference_l(a: const i32*, b: const i32*) -> i32:
let d = *a -% *b
let sign = d >> 31
return (d ^ sign) -% sign
inline func difference_ab(a: const i32*, b: const i32*) -> i64:
let da = (i64)a[1] -% (i64)b[1]
let db = (i64)a[2] -% (i64)b[2]
return da *% da +% db *% db
## Each pixel takes the direction more homogeneous over its 3×3 neighbourhood,
## or the mean of both on a tie; `out` is the first output pixel, rows of
## `out_stride` pixels.
func ahd_combine(horizontal: const u16*, vertical: const u16*, homogeneous_h: const u8*, homogeneous_v: const u8*, tw: usize, th: usize, out: noalias u16*, out_stride: usize):
for ty in 3..<th - 3:
let target_row = out + (ty - 3) * out_stride * 3
for tx in 3..<tw - 3:
let at = ty *% tw +% tx
let hu = homogeneous_h + (at - tw)
let hc = homogeneous_h + at
let hd = homogeneous_h + (at + tw)
let vu = homogeneous_v + (at - tw)
let vc = homogeneous_v + at
let vd = homogeneous_v + (at + tw)
let h = (u32)*(hu - 1) +% (u32)*hu +% (u32)*(hu + 1) +% (u32)*(hc - 1) +% (u32)*hc +% (u32)*(hc + 1) +% (u32)*(hd - 1) +% (u32)*hd +% (u32)*(hd + 1)
let v = (u32)*(vu - 1) +% (u32)*vu +% (u32)*(vu + 1) +% (u32)*(vc - 1) +% (u32)*vc +% (u32)*(vc + 1) +% (u32)*(vd - 1) +% (u32)*vd +% (u32)*(vd + 1)
let a = horizontal + at *% 3
let b = vertical + at *% 3
let target = target_row + (tx - 3) *% 3
# Branch-free: horizontal, vertical, or the mean of both on a tie.
let take_h = (u32)(h > v)
let take_v = (u32)(v > h)
let tie = 1 - take_h - take_v
for c: usize in 0..<3:
let x = (u32)a[c]
let y = (u32)b[c]
target[c] = (u16)(x *% take_h +% y *% take_v +% ((x +% y) >> 1) *% tie)