#[derive(Debug, Clone, Copy)]
pub struct Halo {
pub sigma_um: f64,
pub strength: [f64; 3],
}
#[derive(Debug, Clone, Copy)]
pub struct Halation {
pub pixel_um: f64,
pub scatter: Halo,
pub reflection: Halo,
}
impl Halation {
pub const BASE_THICKNESS_MM: f64 = 0.135;
pub const FILM_INDEX: f64 = 1.5;
#[must_use]
pub fn reflection_radius_um(f_number: f64) -> f64 {
let outside = (1.0 / (2.0 * f_number.max(1e-3))).atan();
let inside = (outside.sin() / Self::FILM_INDEX).asin();
2.0 * Self::BASE_THICKNESS_MM * 1000.0 * inside.tan()
}
#[must_use]
pub fn for_aperture(pixel_um: f64, f_number: f64) -> Self {
Self {
pixel_um,
scatter: Halo {
sigma_um: 6.0,
strength: [0.020, 0.018, 0.015],
},
reflection: Halo {
sigma_um: 0.5 * Self::reflection_radius_um(f_number),
strength: [0.090, 0.030, 0.012],
},
}
}
#[must_use]
pub fn typical(pixel_um: f64) -> Self {
Self::for_aperture(pixel_um, 4.0)
}
#[must_use]
pub fn is_noop(&self) -> bool {
let inert =
|halo: &Halo| halo.sigma_um <= 0.0 || halo.strength.iter().all(|&value| value <= 0.0);
inert(&self.scatter) && inert(&self.reflection)
}
pub fn apply(&self, log_e: &mut [[f64; 3]], width: usize, height: usize) {
if self.is_noop() || width == 0 || height == 0 {
return;
}
let pixels = (width * height).min(log_e.len());
let height = pixels / width;
if height == 0 {
return;
}
let mut source = Vec::with_capacity(pixels);
for pixel in &log_e[..pixels] {
let sum: f64 = pixel.iter().map(|&value| 10.0_f64.powf(value)).sum();
source.push(sum / 3.0);
}
let scatter = self.halo_plane(&source, width, height, &self.scatter);
let reflection = self.halo_plane(&source, width, height, &self.reflection);
for (index, pixel) in log_e.iter_mut().enumerate().take(pixels) {
for (channel, value) in pixel.iter_mut().enumerate() {
let exposure = 10.0_f64.powf(*value).max(1e-10);
let added = self.scatter.strength[channel] * scatter[index]
+ self.reflection.strength[channel] * reflection[index];
*value = (exposure + added).log10();
}
}
}
fn halo_plane(&self, source: &[f64], width: usize, height: usize, halo: &Halo) -> Vec<f64> {
if halo.sigma_um <= 0.0 || halo.strength.iter().all(|&value| value <= 0.0) {
return vec![0.0; source.len()];
}
let sigma_px = halo.sigma_um / self.pixel_um.max(1e-9);
crate::psf::gaussian_blur(source, width, height, sigma_px)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::psf::gaussian_blur as blur;
use crate::psf::gaussian_kernel;
const W: usize = 64;
const H: usize = 64;
fn highlight(log_e: f64) -> Vec<[f64; 3]> {
let mut buffer = vec![[-6.0; 3]; W * H];
buffer[(H / 2) * W + W / 2] = [log_e; 3];
buffer
}
#[test]
fn the_blur_preserves_a_flat_plane() {
let plane = vec![0.25; W * H];
let blurred = blur(&plane, W, H, 3.0);
for value in blurred {
assert!((value - 0.25).abs() < 1e-12, "{value}");
}
}
#[test]
fn a_flat_frame_gains_exactly_the_strengths() {
let mut buffer = vec![[0.2, 0.2, 0.2]; W * H];
let halation = Halation::typical(6.0);
halation.apply(&mut buffer, W, H);
let gain = 1.0 + halation.scatter.strength[0] + halation.reflection.strength[0];
let expected = 0.2 + gain.log10();
for pixel in &buffer {
assert!(
(pixel[0] - expected).abs() < 1e-9,
"{:?} vs {expected}",
pixel[0]
);
}
}
#[test]
fn the_reflection_halo_is_the_size_the_base_allows() {
assert!((Halation::reflection_radius_um(5.6) - 16.0).abs() < 0.5);
assert!((Halation::reflection_radius_um(1.4) - 62.1).abs() < 1.0);
assert!((Halation::reflection_radius_um(16.0) - 5.6).abs() < 0.3);
let mut previous = f64::INFINITY;
for aperture in [1.4, 2.0, 2.8, 4.0, 5.6, 8.0, 11.0, 16.0] {
let radius = Halation::reflection_radius_um(aperture);
assert!(
radius < previous,
"f/{aperture} is not narrower than the last"
);
previous = radius;
}
let straight = 2.0 * Halation::BASE_THICKNESS_MM * 1000.0 * (1.0_f64 / 8.0).atan().tan();
let ratio = Halation::reflection_radius_um(4.0) / straight;
assert!(
(0.6..0.75).contains(&ratio),
"the index of the base is not bending the cone the way it should: {ratio}"
);
let typical = Halation::typical(6.0);
assert!(
(typical.reflection.sigma_um - 0.5 * Halation::reflection_radius_um(4.0)).abs() < 1e-9
);
assert!((typical.scatter.sigma_um - 6.0).abs() < 1e-9);
}
#[test]
fn the_glow_is_red() {
let mut buffer = highlight(3.0);
Halation::typical(6.0).apply(&mut buffer, W, H);
let near = (H / 2) * W + W / 2 + 3;
let pixel = buffer[near];
assert!(pixel[0] > pixel[1], "red {pixel:?}");
assert!(pixel[1] > pixel[2], "green over blue {pixel:?}");
}
#[test]
fn the_halo_falls_off_with_distance() {
let mut buffer = highlight(3.0);
Halation::typical(6.0).apply(&mut buffer, W, H);
let at = |offset: usize| buffer[(H / 2) * W + W / 2 + offset][0];
assert!(at(1) > at(5), "near {} vs far {}", at(1), at(5));
assert!(at(5) > at(20), "near {} vs far {}", at(5), at(20));
}
#[test]
fn the_returned_energy_does_not_depend_on_the_scan() {
let energy = |pixel_um: f64| {
let mut buffer = highlight(3.0);
let before: f64 = buffer.iter().map(|p| 10.0_f64.powf(p[0])).sum();
Halation::typical(pixel_um).apply(&mut buffer, W, H);
let after: f64 = buffer.iter().map(|p| 10.0_f64.powf(p[0])).sum();
after - before
};
let fine = energy(3.0);
let coarse = energy(12.0);
let relative = (fine - coarse).abs() / fine;
assert!(relative < 0.01, "fine {fine} vs coarse {coarse}");
}
#[test]
fn the_halo_widens_when_the_pixels_shrink() {
let reach = |pixel_um: f64| {
let mut buffer = highlight(3.0);
Halation::typical(pixel_um).apply(&mut buffer, W, H);
let row = &buffer[(H / 2) * W..(H / 2 + 1) * W];
row.iter().filter(|pixel| pixel[0] > -2.0).count()
};
let fine = reach(3.0);
let coarse = reach(12.0);
assert!(fine > coarse, "fine {fine} px vs coarse {coarse} px");
}
#[test]
fn a_zero_strength_is_a_no_op() {
let mut halation = Halation::typical(6.0);
halation.reflection.strength = [0.0; 3];
halation.scatter.strength = [0.0; 3];
assert!(halation.is_noop());
let mut buffer = highlight(4.0);
let before = buffer.clone();
halation.apply(&mut buffer, W, H);
assert_eq!(buffer, before);
}
#[test]
fn the_kernel_is_normalised() {
let kernel = gaussian_kernel(2.5);
let sum: f64 = kernel.iter().sum();
assert!((sum - 1.0).abs() < 1e-12, "sum {sum}");
}
}