dymokomi/luce-raw
Source of release 0.8.0
0.8.0 / src / raw / levels.lucb
#==============================================================================================
#
# raw/levels - Black and white levels, white balance and clipping
#
# DESCRIPTION:
# normalize takes the developed region of the mosaic (the active area, or its
# default crop) and maps every sample to 16 bits where the white level after
# white balance is 65535: subtract the black level (a repeating pattern plus
# DNG's per-row and per-column deltas), scale by the white level and the
# color's multiplier, clip. The demosaic works on that Plane, as dcraw's does.
#
#==============================================================================================
## Balanced, clipped sensor values: `width` × `height` pixels of `channels`
## (one for a color filter mosaic, three for LinearRaw). `pattern` is the 6×6
## filter from the plane's corner; a Bayer filter's 2×2 is also `cfa`.
struct Plane:
var data: u16[]
var width: usize
var height: usize
var channels: usize
var cfa: u8[4]
var pattern: u8[36]
var xtrans: bool
## The filter color at (`x`, `y`).
func color(x: usize, y: usize) -> usize:
return (usize)self.pattern[(y % 6) * 6 + x % 6]
func close():
if self.data.length > 0:
free(self.data) in memory.heap
self = Plane()
## A DNG black delta tag as reals, or empty.
func delta_values(entry: const Entry*, count: usize) -> f64[]!:
var empty: f64[]
if entry.count == 0:
return empty
if entry.count < count:
error(corrupt, "DNG black level deltas are too short")
let out = try new f64[count] in memory.heap
errdefer free(out) in memory.heap
for i in 0..<count:
out[i] = try entry.real(i)
return out
## Free what `deltas` allocated.
func release(values: f64[]):
if values.length > 0:
free(values) in memory.heap
## What each band of `normalize` reads.
struct Levels:
var meta: const Meta*
var mosaic: const Mosaic*
var plane: Plane*
var horizontal: const f64[]
var vertical: const f64[]
var scales: f64[3]
var ox: usize
var oy: usize
var tables: LevelTables*?
var half: bool
## Lookup tables for the usual mosaic: no DNG black deltas and a black pattern
## that repeats within the filter's six. One table per distinct black level
## and color maps every sample to its leveled value (as level_row computes
## it) up to the first that clips; larger samples read that last entry. `of`
## names the table of each position of the 6×6 period, from the active area's
## corner.
struct LevelTables:
var data: u16[]
var last: usize[12]
var blacks: f64[12]
var scales: f64[12]
var count: usize
var of: u8[36]
## Tables for `job`, or none when the mosaic needs the general path.
static func plan(job: const Levels*) -> LevelTables?!:
let meta = job.meta
let rows = meta.black_rows
let columns = meta.black_columns
if meta.samples != 1 or job.horizontal.length > 0 or job.vertical.length > 0 or rows == 0 or columns == 0 or 6 % rows != 0 or 6 % columns != 0:
return none
var tables = LevelTables()
for i: usize in 0..<36:
let black = meta.black[((i // 6) % rows) * columns + (i % 6) % columns]
let color = (usize)meta.pattern[i]
let scale = job.scales[color if color < 3 else 1]
var id = tables.count
for k in 0..<tables.count:
if tables.blacks[k] == black and tables.scales[k] == scale:
id = k
if id == tables.count:
if id == tables.last.length:
return none
tables.blacks[id] = black
tables.scales[id] = scale
tables.count += 1
tables.of[i] = (u8)id
tables.data = try new u16[tables.count * 65536] in memory.heap
return tables
func close():
if self.data.length > 0:
free(self.data) in memory.heap
self = LevelTables()
## Fill table `index` of the Levels' tables; `parallel` calls it.
func level_table(context: void*, index: usize, worker: usize) -> !:
let job = (Levels*)context
let tables = job.tables else return
let table = tables.data.data + index * 65536
let black = tables.blacks[index]
let scale = tables.scales[index]
var last: usize = 65535
for s: usize in 0..<65536:
let value = ((f64)s - black) * scale
if value >= 65535.0:
table[s] = 65535
last = s
break
table[s] = (u16)value if value > 0.0 else 0
tables.last[index] = last
let band_rows: usize = 64
## Developed row `y` (every sample of it, `width` pixels) balanced and clipped
## to 16 bits into `out`: the sample less its black level (the repeating
## pattern and DNG's row and column deltas), times its color's scale.
func level_row(job: const Levels*, y: usize, width: usize, out: noalias u16*):
let meta = job.meta
let nc = meta.samples
let columns = meta.black_columns
let ay = job.oy + y
let source = job.mosaic.data.data + ((meta.top + ay) * job.mosaic.width + meta.left + job.ox) * nc
if let tables = job.tables:
let ids = &tables.of[(ay % 6) * 6]
let data = tables.data.data
var phase = job.ox % 6
for x in 0..<width:
let id = (usize)ids[phase]
let value = (usize)source[x]
let last = tables.last[id]
out[x] = data[id * 65536 + (value if value < last else last)]
phase = 0 if phase == 5 else phase + 1
return
let blacks = &meta.black[(ay % meta.black_rows) * columns * nc]
let colors = &meta.pattern[(ay % 6) * 6]
let row_delta = job.vertical[ay] if job.vertical.length > 0 else 0.0
let deltas = job.horizontal.length > 0
let scale0 = job.scales[0]
let scale1 = job.scales[1]
let scale2 = job.scales[2]
var column = job.ox % columns
var phase = job.ox % 6
var at: usize = 0
for x in 0..<width:
let column_delta = job.horizontal[job.ox + x] if deltas else 0.0
for s in 0..<nc:
let color = s if nc == 3 else (usize)colors[phase]
let scale = scale0 if color == 0 else scale1 if color == 1 else scale2
let value = ((f64)source[at] - (blacks[column *% nc +% s] + row_delta + column_delta)) * scale
out[at] = 65535 if value >= 65535.0 else (u16)value if value > 0.0 else 0
at +%= 1
column = 0 if column + 1 == columns else column + 1
phase = 0 if phase == 5 else phase + 1
## Normalize rows [64 × index, 64 × index + 64) of the plane; `parallel` calls it.
func normalize_band(context: void*, index: usize, worker: usize) -> !:
let job = (Levels*)context
let plane = job.plane
let row = plane.width * plane.channels
let end = math.imin((i64)((index + 1) * band_rows), (i64)plane.height)
for y in index * band_rows..<(usize)end:
level_row(job, y, plane.width, plane.data.data + y * row)
## Rows [64 × index, 64 × index + 64) of a half-size plane; `parallel` calls it.
func half_band(context: void*, index: usize, worker: usize) -> !:
let job = (Levels*)context
let meta = job.meta
let plane = job.plane
let nc = meta.samples
let width = plane.width * 2
let rows = try new u16[width * nc * 2] in memory.heap
defer free(rows) in memory.heap
let upper = rows.data
let lower = rows.data + width * nc
let end = math.imin((i64)((index + 1) * band_rows), (i64)plane.height)
for y in index * band_rows..<(usize)end:
level_row(job, 2 * y, width, upper)
level_row(job, 2 * y + 1, width, lower)
let row = plane.data[y * plane.width * 3..<(y + 1) * plane.width * 3]
let top = &meta.pattern[((job.oy + 2 * y) % 6) * 6]
let bottom = &meta.pattern[((job.oy + 2 * y + 1) % 6) * 6]
if nc == 1 and not meta.xtrans:
half_bayer_row(upper, lower, top, bottom, job.ox, plane.width, row.data)
continue
var missing = false
for x in 0..<plane.width:
var rgb: u32[3]
if nc == 3:
for s: usize in 0..<3:
let a = 6 * x + s
rgb[s] = ((u32)upper[a] + (u32)upper[a + 3] + (u32)lower[a] + (u32)lower[a + 3] + 2) >> 2
else:
# In scan order, as LibRaw lets the last sample of a color win.
var greens: u32 = 0
var seen: u8[3]
for i: usize in 0..<4:
let px = 2 * x + i % 2
let c = (usize)(top if i < 2 else bottom)[(job.ox + px) % 6]
let value = (u32)(upper if i < 2 else lower)[px]
if c == 1 and not meta.xtrans and seen[1] > 0:
greens = value
else:
rgb[c] = value
seen[c] += 1
if not meta.xtrans:
rgb[1] = (rgb[1] + greens) >> 1
elif seen[0] == 0 and seen[2] == 0:
missing = true
row[x * 3] = (u16)rgb[0]
row[x * 3 + 1] = (u16)rgb[1]
row[x * 3 + 2] = (u16)rgb[2]
# X-Trans cells of green alone take red and blue from either side.
if missing:
for x in 1..<plane.width - 1:
let px = 2 * x
let py = 2 * y
var colored = false
for i: usize in 0..<4:
if meta.pattern[((job.oy + py + i // 2) % 6) * 6 + (job.ox + px + i % 2) % 6] != 1:
colored = true
if not colored:
row[x * 3] = (u16)(((u32)row[x * 3 - 3] + (u32)row[x * 3 + 3]) >> 1)
row[x * 3 + 2] = (u16)(((u32)row[x * 3 - 1] + (u32)row[x * 3 + 5]) >> 1)
## Level band `index` of a plane (full or half size); `parallel` calls it.
func level_band(context: void*, index: usize, worker: usize) -> !:
let job = (Levels*)context
if job.half:
try half_band(context, index, worker)
else:
try normalize_band(context, index, worker)
## How many mosaic rows level band `index` reads from the top.
func level_rows(job: const Levels*, index: usize) -> usize:
let end = (usize)math.imin((i64)((index + 1) * band_rows), (i64)job.plane.height)
return job.meta.top + job.oy + end * (2 if job.half else 1)
## What the level bands share besides the plane: DNG's black deltas and the
## lookup tables.
struct LevelStage:
var horizontal: f64[]
var vertical: f64[]
var tables: LevelTables
var bands: usize
## Allocate `plane` for `mosaic`'s active area, or its default crop (see
## normalize), and answer the Levels its bands run, which points into
## this stage.
func open(meta: const Meta*, mosaic: const Mosaic*, look: const Development*, crop: bool, half: bool, plane: Plane*, threads: usize) -> Levels!:
errdefer self.close()
let ox = meta.crop_x if crop else 0
let oy = meta.crop_y if crop else 0
let width = meta.crop_width if crop else meta.width
let height = meta.crop_height if crop else meta.height
let nc = meta.samples
let count = width *? height else error(limit, "raw image size overflows")
_ = count *? nc else error(limit, "raw image size overflows")
*plane = Plane(width = width, height = height, channels = nc)
if half:
plane.width = width // 2
plane.height = height // 2
plane.channels = 3
if plane.width == 0 or plane.height == 0:
error(invalid, "raw image is too small for half size")
plane.data = try new u16[plane.width * plane.height * plane.channels] in memory.heap
plane.xtrans = meta.xtrans and not half
for i: usize in 0..<36:
plane.pattern[i] = (u8)meta.color(ox + i % 6, oy + i // 6)
for i: usize in 0..<4:
plane.cfa[i] = plane.pattern[(i // 2) * 6 + i % 2]
self.horizontal = try delta_values(&meta.black_h, meta.width)
self.vertical = try delta_values(&meta.black_v, meta.height)
# The scale of each color: multiplier × 65535 / (white − black); black
# varies per position, so the white's span is taken against the
# pattern's first entry.
var job = Levels(meta = meta, mosaic = mosaic, plane = plane, horizontal = self.horizontal, vertical = self.vertical, ox = ox, oy = oy, half = half)
for c: usize in 0..<3:
let white = meta.white[c if nc == 3 else 0]
let span = white - meta.black[c if nc == 3 else 0]
if span <= 0.0:
error(corrupt, "raw white level is not above the black level")
job.scales[c] = look.multipliers[c] * 65535.0 / span
if let planned = try LevelTables.plan(&job):
self.tables = planned
job.tables = &self.tables
try parallel(self.tables.count, threads, (void*)&job, level_table)
self.bands = (plane.height + band_rows - 1) // band_rows
return job
func close():
release(self.horizontal)
release(self.vertical)
self.tables.close()
self = LevelStage()
## One row of a half-size Bayer plane from its two leveled sensor rows: every
## 2×2 cell has the same colors in the same places, so red and blue are read
## where they are and the two greens averaged, as half_band's general path.
func half_bayer_row(upper: const u16*, lower: const u16*, top: const u8*, bottom: const u8*, ox: usize, width: usize, out: noalias u16*):
# Where each color sits in the cell: 0 and 1 the upper row, 2 and 3 the lower.
var red: usize = 0
var blue: usize = 0
var green: usize = 0
var other: usize = 0
var greens: usize = 0
for i: usize in 0..<4:
let c = (top if i < 2 else bottom)[(ox + i % 2) % 6]
if c == 0:
red = i
elif c == 2:
blue = i
elif greens == 0:
green = i
greens = 1
else:
other = i
let rows: (const u16*)[2] = [upper, lower]
let r = rows[red >> 1] + (red & 1)
let b = rows[blue >> 1] + (blue & 1)
let g = rows[green >> 1] + (green & 1)
let h = rows[other >> 1] + (other & 1)
var q = out
for x in 0..<width:
let at = 2 *% x
q[0] = r[at]
q[1] = (u16)(((u32)g[at] +% (u32)h[at]) >> 1)
q[2] = b[at]
q = q + 3
## The balanced Plane of `mosaic`'s active area, or of its default crop: each
## sample less its black level, scaled so white after white balance is 65535.
## With `half`, each 2×2 cell of it becomes one RGB pixel instead, as LibRaw's
## half_size: red and blue as they are, the two greens of a Bayer cell averaged
## (the last green of an X-Trans cell), and X-Trans cells without red or blue
## given the mean of their left and right neighbours'. A three-channel
## (LinearRaw) plane averages each 2×2 block.
func normalize(meta: const Meta*, mosaic: const Mosaic*, look: const Development*, crop: bool, half: bool, threads: usize) -> Plane!:
var plane = Plane()
errdefer plane.close()
var stage = LevelStage()
defer stage.close()
var job = try stage.open(meta, mosaic, look, crop, half, &plane, threads)
try parallel(stage.bands, threads, (void*)&job, level_band)
return plane