use crate::color::Color;
use crate::svg::Table;
pub(crate) struct Filter {
pub(crate) table: Table,
pub(crate) region: [f64; 4],
pub(crate) steps: Vec<Step>,
}
impl Filter {
pub(crate) fn bounds(&self) -> [f32; 4] {
let [x, y, w, h] = self.region;
reach(&exact(&self.table), [x, y, x + w, y + h]).map(|side| side as f32)
}
}
pub(crate) struct Step {
pub(crate) kind: Kind,
pub(crate) inputs: [Input; 2],
pub(crate) place: [Option<f64>; 4],
pub(crate) space: Space,
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub(crate) enum Input {
Source,
Alpha,
Made(usize),
Clear,
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub(crate) enum Space {
Linear,
Screen,
Taken,
}
pub(crate) enum Kind {
Blur([f32; 2]),
Offset([f32; 2]),
Flood(Color),
Merge(Vec<Input>),
Composite(Operator),
Blend(Mode),
Matrix([f32; 20]),
Transfer(Box<[Function; 4]>),
Shadow([f32; 2], [f32; 2], Color),
Morphology(bool, [f32; 2]),
Tile,
Noise(Noise),
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub(crate) enum Operator {
Over,
In,
Out,
Atop,
Xor,
Lighter,
Arithmetic([f32; 4]),
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub(crate) enum Mode {
Normal,
Multiply,
Screen,
Overlay,
Darken,
Lighten,
ColorDodge,
ColorBurn,
HardLight,
SoftLight,
Difference,
Exclusion,
Hue,
Saturation,
Color,
Luminosity,
}
impl Mode {
pub(crate) fn named(name: &str) -> Option<Mode> {
let mode = match name {
"normal" => Mode::Normal,
"multiply" => Mode::Multiply,
"screen" => Mode::Screen,
"overlay" => Mode::Overlay,
"darken" => Mode::Darken,
"lighten" => Mode::Lighten,
"color-dodge" => Mode::ColorDodge,
"color-burn" => Mode::ColorBurn,
"hard-light" => Mode::HardLight,
"soft-light" => Mode::SoftLight,
"difference" => Mode::Difference,
"exclusion" => Mode::Exclusion,
"hue" => Mode::Hue,
"saturation" => Mode::Saturation,
"color" => Mode::Color,
"luminosity" => Mode::Luminosity,
_ => return None,
};
Some(mode)
}
}
#[derive(Clone, PartialEq, Debug)]
pub(crate) enum Function {
Same,
Table(Vec<f32>),
Discrete(Vec<f32>),
Linear(f32, f32),
Gamma(f32, f32, f32),
}
impl Function {
fn of(&self, c: f32) -> f32 {
match self {
Function::Same => c,
Function::Table(values) if values.len() > 1 => {
let steps = values.len() - 1;
let k = ((c * steps as f32) as usize).min(steps - 1);
values[k] + (c * steps as f32 - k as f32) * (values[k + 1] - values[k])
}
Function::Discrete(values) if !values.is_empty() => values[((c * values.len() as f32) as usize).min(values.len() - 1)],
Function::Table(values) | Function::Discrete(values) => values.first().copied().unwrap_or(c),
Function::Linear(slope, intercept) => slope * c + intercept,
Function::Gamma(amplitude, exponent, offset) => amplitude * c.powf(*exponent) + offset,
}
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub(crate) struct Noise {
pub(crate) base: [f32; 2],
pub(crate) octaves: u32,
pub(crate) seed: f32,
pub(crate) stitch: bool,
pub(crate) fractal: bool,
}
pub(crate) fn weight(kind: &Kind) -> f32 {
match kind {
Kind::Flood(_) => 2.0,
Kind::Tile => 3.0,
Kind::Offset(_) | Kind::Transfer(_) => 5.0,
Kind::Blend(Mode::Normal) => 6.0,
Kind::Composite(_) => 8.0,
Kind::Morphology(..) => 9.0,
Kind::Matrix(_) => 12.0,
Kind::Blend(_) => 14.0,
Kind::Blur(_) => 18.0,
Kind::Shadow(..) => 20.0,
Kind::Merge(inputs) => 1.0 + 5.0 * inputs.len() as f32,
Kind::Noise(noise) => 7.0 + 11.0 * noise.octaves as f32,
}
}
pub(crate) const FIXED: f32 = 4.0;
pub(crate) const OCTAVES: u32 = 12;
pub(crate) fn saturated(s: f32) -> [f32; 20] {
#[rustfmt::skip]
let matrix = [
0.213 + 0.787 * s, 0.715 - 0.715 * s, 0.072 - 0.072 * s, 0.0, 0.0,
0.213 - 0.213 * s, 0.715 + 0.285 * s, 0.072 - 0.072 * s, 0.0, 0.0,
0.213 - 0.213 * s, 0.715 - 0.715 * s, 0.072 + 0.928 * s, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0, 0.0,
];
matrix
}
pub(crate) fn hue_turned(degrees: f32) -> [f32; 20] {
let (sin, cos) = degrees.to_radians().sin_cos();
#[rustfmt::skip]
let matrix = [
0.213 + cos * 0.787 - sin * 0.213, 0.715 - cos * 0.715 - sin * 0.715, 0.072 - cos * 0.072 + sin * 0.928, 0.0, 0.0,
0.213 - cos * 0.213 + sin * 0.143, 0.715 + cos * 0.285 + sin * 0.140, 0.072 - cos * 0.072 - sin * 0.283, 0.0, 0.0,
0.213 - cos * 0.213 - sin * 0.787, 0.715 - cos * 0.715 + sin * 0.715, 0.072 + cos * 0.928 + sin * 0.072, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0, 0.0,
];
matrix
}
#[rustfmt::skip]
pub(crate) const LUMINANCE: [f32; 20] = [
0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0,
0.2125, 0.7154, 0.0721, 0.0, 0.0,
];
#[rustfmt::skip]
pub(crate) const SAME: [f32; 20] = [
1.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0, 0.0,
];
pub(crate) fn toned(amount: f32, sepia: bool) -> [f32; 20] {
let rest = 1.0 - amount.clamp(0.0, 1.0);
let [r, g, b, keep] = match sepia {
true => [[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131], [0.607, 0.314, 0.869]],
false => [[0.2126, 0.7152, 0.0722], [0.2126, 0.7152, 0.0722], [0.2126, 0.7152, 0.0722], [0.7874, 0.2848, 0.9278]],
};
#[rustfmt::skip]
let matrix = [
r[0] + keep[0] * rest, r[1] - r[1] * rest, r[2] - r[2] * rest, 0.0, 0.0,
g[0] - g[0] * rest, g[1] + keep[1] * rest, g[2] - g[2] * rest, 0.0, 0.0,
b[0] - b[0] * rest, b[1] - b[1] * rest, b[2] + keep[2] * rest, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0, 0.0,
];
matrix
}
#[derive(Clone)]
struct Picture {
pixels: Vec<u32>,
linear: bool,
part: [usize; 4],
}
struct Room {
left: usize,
top: usize,
wide: usize,
high: usize,
region: [f64; 4],
table: Exact,
back: Exact,
}
pub(crate) fn apply(filter: &Filter, pixels: &mut [u32], wide: usize, top: i32, fit: &Table) {
let Some(room) = Room::of(filter, wide, pixels.len() / wide.max(1), top, fit) else {
return pixels.fill(0);
};
let light = Light::new();
let source = room.taken(pixels, wide);
let mut made: Vec<Picture> = Vec::with_capacity(filter.steps.len());
for step in &filter.steps {
let picture = room.step(step, &source, &made, &light);
made.push(picture);
}
pixels.fill(0);
if let Some(last) = made.pop() {
room.put(&light.taken(last, false), pixels, wide);
}
}
impl Room {
fn of(filter: &Filter, wide: usize, high: usize, top: i32, fit: &Table) -> Option<Room> {
let table = both(&[1.0, 0.0, 0.0, 1.0, 0.0, -f64::from(top)], &both(&exact(fit), &exact(&filter.table)));
let [x, y, w, h] = filter.region;
let [l, t, r, b] = reach(&table, [x, y, x + w, y + h]);
let [l, t, r, b] = [l.max(0.0), t.max(0.0), r.min(wide as f64), b.min(high as f64)];
if !(l < r && t < b && w > 0.0 && h > 0.0) {
return None;
}
let ([left, upper], [right, lower]) = ([l.floor() as usize, t.floor() as usize], [(r.ceil() as usize).min(wide), (b.ceil() as usize).min(high)]);
let table = both(&[1.0, 0.0, 0.0, 1.0, -(left as f64), -(upper as f64)], &table);
let region = [l - left as f64, t - upper as f64, r - left as f64, b - upper as f64];
Some(Room { left, top: upper, wide: right - left, high: lower - upper, region, back: undone(&table)?, table })
}
fn taken(&self, pixels: &[u32], wide: usize) -> Picture {
let mut taken = Vec::with_capacity(self.wide * self.high);
for row in self.top..self.top + self.high {
taken.extend_from_slice(&pixels[row * wide + self.left..row * wide + self.left + self.wide]);
}
Picture { pixels: taken, linear: false, part: [0, 0, self.wide, self.high] }
}
fn put(&self, picture: &Picture, pixels: &mut [u32], wide: usize) {
for (row, line) in picture.pixels.chunks_exact(self.wide).enumerate() {
let at = (self.top + row) * wide + self.left;
pixels[at..at + self.wide].copy_from_slice(line);
}
}
fn clear(&self) -> Picture {
Picture { pixels: vec![0; self.wide * self.high], linear: false, part: [0, 0, self.wide, self.high] }
}
fn step(&self, step: &Step, source: &Picture, made: &[Picture], light: &Light) -> Picture {
let (part, exact) = self.part(step, made);
let wanted = match step.space {
Space::Linear => Some(true),
Space::Screen => Some(false),
Space::Taken => None,
};
let take = |n: usize| self.input(step.inputs[n], source, made, wanted, light);
let (wide, t) = (self.wide, self.table.map(|n| n as f32));
let scale = [t[0].hypot(t[2]), t[1].hypot(t[3])];
let picture = match &step.kind {
Kind::Blur(deviation) => blurred(take(0), part, wide, [deviation[0] * scale[0], deviation[1] * scale[1]]),
Kind::Offset([dx, dy]) => moved(take(0), (part, exact), wide, [t[0] * dx + t[2] * dy, t[1] * dx + t[3] * dy]),
Kind::Flood(color) => flooded(*color, part, wide, self.high),
Kind::Merge(inputs) => merged(inputs.iter().map(|input| self.input(*input, source, made, wanted, light)), part, wide, self.high, wanted),
Kind::Composite(operator) => composited(take(0), &take(1), *operator, part, wide),
Kind::Blend(mode) => blended(take(0), &take(1), *mode, part, wide),
Kind::Matrix(matrix) => matrixed(take(0), matrix, part, wide),
Kind::Transfer(functions) => transferred(take(0), functions, part, wide),
Kind::Shadow(by, deviation, color) => {
let ([dx, dy], spread) = (*by, [deviation[0] * scale[0], deviation[1] * scale[1]]);
shadowed(take(0), (part, exact), wide, [t[0] * dx + t[2] * dy, t[1] * dx + t[3] * dy], spread, *color)
}
Kind::Morphology(thicker, radius) => thickened(take(0), part, wide, *thicker, [radius[0] * scale[0], radius[1] * scale[1]]),
Kind::Tile => tiled(take(0), part, wide),
Kind::Noise(noise) => self.noise(noise, part, wanted.unwrap_or(false)),
};
clear_outside(picture, part, wide)
}
fn part(&self, step: &Step, made: &[Picture]) -> ([usize; 4], [f64; 4]) {
let merged = match &step.kind {
Kind::Merge(inputs) => &inputs[..],
_ => &[],
};
let used = match step.kind {
Kind::Flood(_) | Kind::Noise(_) | Kind::Tile => &[][..],
Kind::Composite(_) | Kind::Blend(_) => &step.inputs[..],
Kind::Merge(_) => merged,
_ => &step.inputs[..1],
};
let parts: Option<Vec<[usize; 4]>> = used.iter().map(|input| if let Input::Made(n) = input { made.get(*n).map(|made| made.part) } else { None }).collect();
let pixels = parts.filter(|parts| !parts.is_empty()).map(|parts| parts.iter().fold([usize::MAX, usize::MAX, 0, 0], |a, b| [a[0].min(b[0]), a[1].min(b[1]), a[2].max(b[2]), a[3].max(b[3])]));
let pixels = pixels.map_or(self.region, |[l, t, r, b]| [l as f64, t as f64, r as f64, b as f64]);
let [mut l, mut t, mut r, mut b] = reach(&self.back, pixels);
if let Some(x) = step.place[0] {
(l, r) = (x, x + r - l);
}
if let Some(y) = step.place[1] {
(t, b) = (y, y + b - t);
}
r = step.place[2].map_or(r, |w| l + w);
b = step.place[3].map_or(b, |h| t + h);
let [l, t, r, b] = reach(&self.table, [l, t, r, b]);
let [l, t, r, b] = [l.max(self.region[0]), t.max(self.region[1]), r.min(self.region[2]), b.min(self.region[3])];
if !(l < r && t < b) {
return ([0; 4], [0.0; 4]);
}
([l.floor() as usize, t.floor() as usize, (r.ceil() as usize).min(self.wide), (b.ceil() as usize).min(self.high)], [l, t, r, b])
}
fn input(&self, input: Input, source: &Picture, made: &[Picture], wanted: Option<bool>, light: &Light) -> Picture {
let picture = match input {
Input::Source => source.clone(),
Input::Alpha => Picture { pixels: source.pixels.iter().map(|pixel| pixel & 0xff00_0000).collect(), linear: wanted.unwrap_or(false), part: source.part },
Input::Made(n) => made.get(n).cloned().unwrap_or_else(|| self.clear()),
Input::Clear => self.clear(),
};
match wanted {
Some(linear) => light.taken(picture, linear),
None => picture,
}
}
fn noise(&self, noise: &Noise, part: [usize; 4], linear: bool) -> Picture {
let lattice = Lattice::new(noise.seed);
let [l, t, r, b] = reach(&self.back, part.map(|side| side as f64));
let mut pixels = vec![0; self.wide * self.high];
for y in part[1]..part[3] {
for x in part[0]..part[2] {
let at = through(&self.back, [x as f64, y as f64]);
pixels[y * self.wide + x] = lattice.pixel(noise, at, [l, t, r - l, b - t]);
}
}
Picture { pixels, linear, part }
}
}
type Exact = [f64; 6];
fn exact(table: &Table) -> Exact {
table.map(f64::from)
}
fn both(outer: &Exact, inner: &Exact) -> Exact {
let ([a, b, c, d, e, f], [p, q, r, s, t, u]) = (*outer, *inner);
[a * p + c * q, b * p + d * q, a * r + c * s, b * r + d * s, a * t + c * u + e, b * t + d * u + f]
}
fn through(table: &Exact, [x, y]: [f64; 2]) -> [f64; 2] {
[table[0] * x + table[2] * y + table[4], table[1] * x + table[3] * y + table[5]]
}
fn undone(table: &Exact) -> Option<Exact> {
let [a, b, c, d, e, f] = *table;
let area = a * d - b * c;
area.is_normal().then(|| [d / area, -b / area, -c / area, a / area, (c * f - d * e) / area, (b * e - a * f) / area])
}
fn reach(table: &Exact, [l, t, r, b]: [f64; 4]) -> [f64; 4] {
let corners = [[l, t], [r, t], [l, b], [r, b]].map(|corner| through(table, corner));
let low = corners.iter().fold([f64::MAX; 2], |low, p| [low[0].min(p[0]), low[1].min(p[1])]);
let high = corners.iter().fold([f64::MIN; 2], |high, p| [high[0].max(p[0]), high[1].max(p[1])]);
[low[0], low[1], high[0], high[1]]
}
fn clear_outside(mut picture: Picture, [l, t, r, b]: [usize; 4], wide: usize) -> Picture {
for (row, line) in picture.pixels.chunks_exact_mut(wide.max(1)).enumerate() {
match (t..b).contains(&row) && l < r {
true => {
line[..l].fill(0);
line[r..].fill(0);
}
false => line.fill(0),
}
}
picture.part = [l, t, r.max(l), b.max(t)];
picture
}
struct Light {
linear: [u8; 256],
screen: [u8; 256],
plain: [u64; 256],
}
impl Light {
fn new() -> Light {
let into = |c: f32| if c <= 0.04045 { c / 12.92 } else { ((c + 0.055) / 1.055).powf(2.4) };
let back = |c: f32| if c <= 0.003_130_8 { 12.92 * c } else { 1.055 * c.powf(1.0 / 2.4) - 0.055 };
let level = |c: f32| (c * 255.0 + 0.5) as u8;
let plain = std::array::from_fn(|a| if a == 0 { 0 } else { (1u64 << 32).div_ceil(a as u64) });
Light { linear: std::array::from_fn(|n| level(into(n as f32 / 255.0))), screen: std::array::from_fn(|n| level(back(n as f32 / 255.0))), plain }
}
fn taken(&self, mut picture: Picture, linear: bool) -> Picture {
if picture.linear == linear {
return picture;
}
let table = if linear { &self.linear } else { &self.screen };
for pixel in &mut picture.pixels {
*pixel = changed(*pixel, table, &self.plain);
}
picture.linear = linear;
picture
}
}
fn changed(pixel: u32, table: &[u8; 256], plain: &[u64; 256]) -> u32 {
let a = pixel >> 24;
if a == 0 {
return 0;
}
let by = plain[a as usize];
let part = |shift: u32| {
let plain = ((((pixel >> shift & 255) * 255 + a / 2) as u64 * by) >> 32) as u32;
(table[plain.min(255) as usize] as u32 * a + 127) / 255
};
a << 24 | part(16) << 16 | part(8) << 8 | part(0)
}
fn four(pixel: u32) -> [u32; 4] {
[pixel >> 24, pixel >> 16 & 255, pixel >> 8 & 255, pixel & 255]
}
fn pixel([a, r, g, b]: [u32; 4]) -> u32 {
a << 24 | r << 16 | g << 8 | b
}
fn times(a: u32, b: u32) -> u32 {
let t = a * b + 128;
((t >> 8) + t) >> 8
}
fn shared(sum: u32) -> u32 {
let t = sum + 128;
(((t >> 8) + t) >> 8).min(255)
}
fn over(top: u32, under: u32) -> u32 {
let (rest, [ua, ur, ug, ub], [ta, tr, tg, tb]) = (255 - (top >> 24), four(under), four(top));
pixel([ta + times(ua, rest), tr + times(ur, rest), tg + times(ug, rest), tb + times(ub, rest)].map(|part| part.min(255)))
}
fn blurred(picture: Picture, part: [usize; 4], wide: usize, [across, down]: [f32; 2]) -> Picture {
let mut picture = clear_outside(picture, part, wide);
if !(across > 0.0 || down > 0.0) {
return picture;
}
let [l, t, r, b] = part;
if l >= r || t >= b {
return picture;
}
match across {
_ if across >= 2.0 => boxes(&mut picture.pixels, wide, part, across, true),
_ if across > 0.0 => bell(&mut picture.pixels, wide, part, across, true),
_ => {}
}
match down {
_ if down >= 2.0 => boxes(&mut picture.pixels, wide, part, down, false),
_ if down > 0.0 => bell(&mut picture.pixels, wide, part, down, false),
_ => {}
}
picture
}
fn boxes(pixels: &mut [u32], wide: usize, part: [usize; 4], deviation: f32, across: bool) {
let d = (deviation * 3.0 * std::f32::consts::TAU.sqrt() / 4.0 + 0.5).floor() as usize;
let sizes = match d % 2 {
_ if d == 0 => [(1, 0); 3],
1 => [(d, d / 2); 3],
_ => [(d, d / 2), (d, d / 2 - 1), (d + 1, d / 2)],
};
for (size, before) in sizes {
match across {
true => box_across(pixels, wide, part, size, before),
false => box_down(pixels, wide, part, size, before),
}
}
}
fn box_across(pixels: &mut [u32], wide: usize, [l, t, r, b]: [usize; 4], size: usize, before: usize) {
let (mut row, share) = (vec![0u32; r - l], 1.0 / size as f32);
for y in t..b {
let line = &mut pixels[y * wide + l..y * wide + r];
row.copy_from_slice(line);
box_line(&row, line, size, before, share);
}
}
fn box_line(row: &[u32], line: &mut [u32], size: usize, before: usize, share: f32) {
let after = size - before;
let mut sum = Sum::default();
row.iter().take(after).for_each(|pixel| sum.add(*pixel));
line[0] = sum.mean(share);
for x in 1..row.len() {
if x > before {
sum.take(row[x - before - 1]);
}
if x + after - 1 < row.len() {
sum.add(row[x + after - 1]);
}
line[x] = sum.mean(share);
}
}
fn box_down(pixels: &mut [u32], wide: usize, [l, t, r, b]: [usize; 4], size: usize, before: usize) {
let (after, share, high) = (size - before, 1.0 / size as f32, b - t);
let taken: Vec<u32> = (t..b).flat_map(|y| pixels[y * wide + l..y * wide + r].iter().copied()).collect();
let (across, mut sums) = (r - l, vec![Sum::default(); r - l]);
let row = |y: usize| &taken[y * across..(y + 1) * across];
(0..after.min(high)).for_each(|y| sums.iter_mut().zip(row(y)).for_each(|(sum, pixel)| sum.add(*pixel)));
for y in 0..high {
if y > before {
sums.iter_mut().zip(row(y - before - 1)).for_each(|(sum, pixel)| sum.take(*pixel));
}
if y > 0 && y + after - 1 < high {
sums.iter_mut().zip(row(y + after - 1)).for_each(|(sum, pixel)| sum.add(*pixel));
}
let line = &mut pixels[(t + y) * wide + l..(t + y) * wide + r];
line.iter_mut().zip(&sums).for_each(|(pixel, sum)| *pixel = sum.mean(share));
}
}
#[derive(Clone, Copy, Default)]
struct Sum {
a: u32,
r: u32,
g: u32,
b: u32,
}
impl Sum {
#[inline(always)]
fn add(&mut self, pixel: u32) {
(self.a, self.r, self.g, self.b) = (self.a + (pixel >> 24), self.r + (pixel >> 16 & 255), self.g + (pixel >> 8 & 255), self.b + (pixel & 255));
}
#[inline(always)]
fn take(&mut self, pixel: u32) {
(self.a, self.r, self.g, self.b) = (self.a - (pixel >> 24), self.r - (pixel >> 16 & 255), self.g - (pixel >> 8 & 255), self.b - (pixel & 255));
}
#[inline(always)]
fn mean(&self, share: f32) -> u32 {
let level = |sum: u32| (sum as f32 * share + 0.5) as u32;
level(self.a) << 24 | level(self.r) << 16 | level(self.g) << 8 | level(self.b)
}
}
#[inline(always)]
fn weigh(sum: &mut [f32; 4], pixel: u32, weight: f32) {
sum[0] += (pixel >> 24) as f32 * weight;
sum[1] += (pixel >> 16 & 255) as f32 * weight;
sum[2] += (pixel >> 8 & 255) as f32 * weight;
sum[3] += (pixel & 255) as f32 * weight;
}
fn bell(pixels: &mut [u32], wide: usize, [l, t, r, b]: [usize; 4], deviation: f32, across: bool) {
let weights = curve(deviation);
let reach = weights.len() / 2;
let taken: Vec<u32> = (t..b).flat_map(|y| pixels[y * wide + l..y * wide + r].iter().copied()).collect();
let (columns, rows) = (r - l, b - t);
let (steps, count, stride) = if across { (1, columns, columns) } else { (columns, rows, 1) };
let lines = if across { rows } else { columns };
for line in 0..lines {
let first = line * stride;
for at in 0..count {
let mut sum = [0.0f32; 4];
(at.saturating_sub(reach)..(at + reach + 1).min(count)).for_each(|n| weigh(&mut sum, taken[first + n * steps], weights[n + reach - at]));
let (x, y) = if across { (at, line) } else { (line, at) };
let level = |part: f32| (part.clamp(0.0, 255.0) + 0.5) as u32;
pixels[(t + y) * wide + l + x] = level(sum[0]) << 24 | level(sum[1]) << 16 | level(sum[2]) << 8 | level(sum[3]);
}
}
}
fn curve(deviation: f32) -> Vec<f32> {
let reach = (deviation * 3.0).ceil().max(1.0) as i32;
let below = |x: f32| 0.5 * (1.0 + erf(x / (deviation * std::f32::consts::SQRT_2)));
let weights: Vec<f32> = (-reach..=reach).map(|n| below(n as f32 + 0.5) - below(n as f32 - 0.5)).collect();
let sum: f32 = weights.iter().sum();
weights.into_iter().map(|weight| weight / sum).collect()
}
fn erf(x: f32) -> f32 {
let t = 1.0 / (1.0 + 0.327_591_1 * x.abs());
let poly = t * (0.254_829_6 + t * (-0.284_496_74 + t * (1.421_413_7 + t * (-1.453_152_1 + t * 1.061_405_4))));
(1.0 - poly * (-x * x).exp()).copysign(x)
}
fn moved(picture: Picture, (part, exact): ([usize; 4], [f64; 4]), wide: usize, [dx, dy]: [f32; 2]) -> Picture {
let source = clear_outside(picture, part, wide);
let high = source.pixels.len() / wide.max(1);
let (whole, rest) = ([dx.floor(), dy.floor()], [dx - dx.floor(), dy - dy.floor()]);
if !(whole[0].abs() < 1e7 && whole[1].abs() < 1e7) {
return Picture { pixels: vec![0; source.pixels.len()], ..source };
}
let [wx, wy] = [whole[0] as i64, whole[1] as i64];
let at = |x: i64, y: i64| match x >= 0 && y >= 0 && (x as usize) < wide && (y as usize) < high {
true => source.pixels[y as usize * wide + x as usize],
false => 0,
};
let weights = [(1.0 - rest[0]) * (1.0 - rest[1]), rest[0] * (1.0 - rest[1]), (1.0 - rest[0]) * rest[1], rest[0] * rest[1]];
let [x, y] = [f64::from(dx), f64::from(dy)];
let kept = [exact[0].max(exact[0] + x), exact[1].max(exact[1] + y), exact[2].min(exact[2] + x), exact[3].min(exact[3] + y)];
let covered = |at: usize, low: f64, high: f64| (high.min(at as f64 + 1.0) - low.max(at as f64)).clamp(0.0, 1.0);
let mut pixels = vec![0; source.pixels.len()];
let whole_move = rest == [0.0, 0.0];
for y in part[1]..part[3] {
let down = covered(y, kept[1], kept[3]);
for x in part[0]..part[2] {
let (x0, y0) = (x as i64 - wx - 1, y as i64 - wy - 1);
let moved = if whole_move { at(x0 + 1, y0 + 1) } else { mixed([at(x0 + 1, y0 + 1), at(x0, y0 + 1), at(x0 + 1, y0), at(x0, y0)], weights) };
let share = down * covered(x, kept[0], kept[2]);
pixels[y * wide + x] = if share >= 1.0 { moved } else { as_much(moved, (share * 255.0 + 0.5) as u32) };
}
}
Picture { pixels, ..source }
}
fn as_much(was: u32, part: u32) -> u32 {
pixel(four(was).map(|c| times(c, part)))
}
fn mixed(near: [u32; 4], weights: [f32; 4]) -> u32 {
let part = |shift: u32| (near.iter().zip(weights).map(|(pixel, weight)| (pixel >> shift & 255) as f32 * weight).sum::<f32>() + 0.5).min(255.0) as u32;
part(24) << 24 | part(16) << 16 | part(8) << 8 | part(0)
}
fn flooded(color: Color, [l, t, r, b]: [usize; 4], wide: usize, high: usize) -> Picture {
let [a, red, green, blue] = four(color.0);
let premultiplied = pixel([a, times(red, a), times(green, a), times(blue, a)]);
let mut pixels = vec![0; wide * high];
for y in t..b {
pixels[y * wide + l..y * wide + r].fill(premultiplied);
}
Picture { pixels, linear: false, part: [l, t, r, b] }
}
fn merged(pictures: impl Iterator<Item = Picture>, part: [usize; 4], wide: usize, high: usize, wanted: Option<bool>) -> Picture {
let mut merged = Picture { pixels: vec![0; wide * high], linear: wanted.unwrap_or(false), part };
for picture in pictures {
merged.linear = wanted.unwrap_or(picture.linear);
merged.pixels.iter_mut().zip(&picture.pixels).for_each(|(under, top)| *under = over(*top, *under));
}
merged
}
fn composited(first: Picture, second: &Picture, operator: Operator, part: [usize; 4], wide: usize) -> Picture {
let mut picture = clear_outside(first, part, wide);
let second = &second.pixels;
for (top, under) in picture.pixels.iter_mut().zip(second) {
let ([ta, ..], [ua, ..]) = (four(*top), four(*under));
*top = match operator {
Operator::Over => over(*top, *under),
Operator::In => pixel(four(*top).map(|part| times(part, ua))),
Operator::Out => pixel(four(*top).map(|part| times(part, 255 - ua))),
Operator::Atop => pixel([0, 1, 2, 3].map(|n| shared(four(*top)[n] * ua + four(*under)[n] * (255 - ta)))),
Operator::Xor => pixel([0, 1, 2, 3].map(|n| shared(four(*top)[n] * (255 - ua) + four(*under)[n] * (255 - ta)))),
Operator::Lighter => pixel([0, 1, 2, 3].map(|n| (four(*top)[n] + four(*under)[n]).min(255))),
Operator::Arithmetic(k) => arithmetic(*top, *under, k),
};
}
clear_outside(picture, part, wide)
}
fn arithmetic(first: u32, second: u32, [k1, k2, k3, k4]: [f32; 4]) -> u32 {
let ([a, ..], [b, ..]) = (four(first), four(second));
let of = |a: u32, b: u32| {
let (a, b) = (a as f32 / 255.0, b as f32 / 255.0);
(k1 * a * b + k2 * a + k3 * b + k4).clamp(0.0, 1.0)
};
let solid = of(a, b);
if solid <= 0.0 {
return 0;
}
let ([_, r1, g1, b1], [_, r2, g2, b2]) = (four(first), four(second));
let level = |part: f32| (part.min(solid) * 255.0 + 0.5) as u32;
pixel([level(solid), level(of(r1, r2)), level(of(g1, g2)), level(of(b1, b2))])
}
fn blended(first: Picture, second: &Picture, mode: Mode, part: [usize; 4], wide: usize) -> Picture {
let mut picture = clear_outside(first, part, wide);
for (top, under) in picture.pixels.iter_mut().zip(&second.pixels) {
*top = match mode {
Mode::Normal => over(*top, *under),
_ => mix(*top, *under, mode),
};
}
clear_outside(picture, part, wide)
}
fn mix(top: u32, under: u32, mode: Mode) -> u32 {
let parts = |pixel: u32| four(pixel).map(|part| part as f32 / 255.0);
let ([sa, sr, sg, sb], [da, dr, dg, db]) = (parts(top), parts(under));
let plain = |c: f32, a: f32| if a > 0.0 { (c / a).min(1.0) } else { 0.0 };
let (source, backdrop) = ([plain(sr, sa), plain(sg, sa), plain(sb, sa)], [plain(dr, da), plain(dg, da), plain(db, da)]);
let mixed = match mode {
Mode::Hue | Mode::Saturation | Mode::Color | Mode::Luminosity => apart(source, backdrop, mode),
_ => [0, 1, 2].map(|n| each(source[n], backdrop[n], mode)),
};
let solid = sa + da - sa * da;
let level = |part: f32| (part.clamp(0.0, 1.0) * 255.0 + 0.5) as u32;
let color = |n: usize, s: f32, d: f32| level((1.0 - sa) * d + (1.0 - da) * s + sa * da * mixed[n]);
pixel([level(solid), color(0, sr, dr), color(1, sg, dg), color(2, sb, db)])
}
fn each(s: f32, d: f32, mode: Mode) -> f32 {
match mode {
Mode::Multiply => s * d,
Mode::Screen => s + d - s * d,
Mode::Overlay => hard_light(d, s),
Mode::Darken => s.min(d),
Mode::Lighten => s.max(d),
Mode::ColorDodge if d == 0.0 => 0.0,
Mode::ColorDodge if s >= 1.0 => 1.0,
Mode::ColorDodge => (d / (1.0 - s)).min(1.0),
Mode::ColorBurn if d >= 1.0 => 1.0,
Mode::ColorBurn if s <= 0.0 => 0.0,
Mode::ColorBurn => 1.0 - ((1.0 - d) / s).min(1.0),
Mode::HardLight => hard_light(s, d),
Mode::SoftLight if s <= 0.5 => d - (1.0 - 2.0 * s) * d * (1.0 - d),
Mode::SoftLight => {
let lifted = if d <= 0.25 { ((16.0 * d - 12.0) * d + 4.0) * d } else { d.sqrt() };
d + (2.0 * s - 1.0) * (lifted - d)
}
Mode::Difference => (s - d).abs(),
Mode::Exclusion => s + d - 2.0 * s * d,
_ => s,
}
}
fn hard_light(s: f32, d: f32) -> f32 {
match s <= 0.5 {
true => d * 2.0 * s,
false => {
let s = 2.0 * s - 1.0;
s + d - s * d
}
}
}
fn apart(source: [f32; 3], backdrop: [f32; 3], mode: Mode) -> [f32; 3] {
let lum = |c: [f32; 3]| 0.3 * c[0] + 0.59 * c[1] + 0.11 * c[2];
let sat = |c: [f32; 3]| c[0].max(c[1]).max(c[2]) - c[0].min(c[1]).min(c[2]);
let with_sat = |c: [f32; 3], s: f32| {
let (low, high) = (c[0].min(c[1]).min(c[2]), c[0].max(c[1]).max(c[2]));
c.map(|part| if high > low { (part - low) * s / (high - low) } else { 0.0 })
};
let with_lum = |c: [f32; 3], l: f32| {
let d = l - lum(c);
let c = c.map(|part| part + d);
let (l, low, high) = (lum(c), c[0].min(c[1]).min(c[2]), c[0].max(c[1]).max(c[2]));
let c = if low < 0.0 { c.map(|part| l + (part - l) * l / (l - low)) } else { c };
if high > 1.0 { c.map(|part| l + (part - l) * (1.0 - l) / (high - l)) } else { c }
};
match mode {
Mode::Hue => with_lum(with_sat(source, sat(backdrop)), lum(backdrop)),
Mode::Saturation => with_lum(with_sat(backdrop, sat(source)), lum(backdrop)),
Mode::Color => with_lum(source, lum(backdrop)),
_ => with_lum(backdrop, lum(source)),
}
}
fn matrixed(picture: Picture, matrix: &[f32; 20], part: [usize; 4], wide: usize) -> Picture {
let mut picture = clear_outside(picture, part, wide);
let [l, t, r, b] = part;
for y in t..b {
for pixel in &mut picture.pixels[y * wide + l..y * wide + r] {
*pixel = matrix_pixel(*pixel, matrix);
}
}
picture
}
fn matrix_pixel(was: u32, m: &[f32; 20]) -> u32 {
let [a, r, g, b] = four(was).map(|part| part as f32 / 255.0);
let plain = if a > 0.0 { [r / a, g / a, b / a, a, 1.0] } else { [0.0, 0.0, 0.0, 0.0, 1.0] };
let row = |n: usize| (0..5).map(|k| m[n * 5 + k] * plain[k]).sum::<f32>();
let solid = row(3).clamp(0.0, 1.0);
let level = |part: f32| (part.clamp(0.0, 1.0) * solid * 255.0 + 0.5) as u32;
pixel([level(1.0), level(row(0)), level(row(1)), level(row(2))])
}
fn transferred(picture: Picture, functions: &[Function; 4], part: [usize; 4], wide: usize) -> Picture {
let mut picture = clear_outside(picture, part, wide);
let tables: Vec<[u8; 256]> = functions.iter().map(|function| std::array::from_fn(|n| (function.of(n as f32 / 255.0).clamp(0.0, 1.0) * 255.0 + 0.5) as u8)).collect();
let [l, t, r, b] = part;
for y in t..b {
for pixel in &mut picture.pixels[y * wide + l..y * wide + r] {
*pixel = transfer_pixel(*pixel, &tables);
}
}
picture
}
fn transfer_pixel(was: u32, tables: &[[u8; 256]]) -> u32 {
let [a, r, g, b] = four(was);
let plain = |c: u32| (c * 255 + a / 2).checked_div(a).map_or(0, |plain| plain.min(255));
let solid = tables[3][a as usize] as u32;
let color = |n: usize, c: u32| (tables[n][plain(c) as usize] as u32 * solid + 127) / 255;
pixel([solid, color(0, r), color(1, g), color(2, b)])
}
fn shadowed(picture: Picture, (part, exact): ([usize; 4], [f64; 4]), wide: usize, by: [f32; 2], deviation: [f32; 2], color: Color) -> Picture {
let alpha = Picture { pixels: picture.pixels.iter().map(|pixel| pixel & 0xff00_0000).collect(), ..picture.clone() };
let shadow = moved(blurred(alpha, part, wide, deviation), (part, exact), wide, by);
let [a, red, green, blue] = four(color.0);
let colored = |cover: u32| {
let solid = times(cover >> 24, a);
pixel([solid, times(red, solid), times(green, solid), times(blue, solid)])
};
let mut pixels: Vec<u32> = shadow.pixels.iter().map(|cover| colored(*cover)).collect();
pixels.iter_mut().zip(&picture.pixels).for_each(|(under, top)| *under = over(*top, *under));
clear_outside(Picture { pixels, ..picture }, part, wide)
}
fn thickened(picture: Picture, part: [usize; 4], wide: usize, thicker: bool, radius: [f32; 2]) -> Picture {
let mut picture = clear_outside(picture, part, wide);
if !(radius[0] > 0.0 || radius[1] > 0.0) {
return picture;
}
let [l, t, r, b] = part;
let [across, down] = radius.map(|radius| if radius > 0.0 { radius.min(1e7).ceil() as usize } else { 0 });
let (mut line, mut out, mut room) = (Vec::new(), Vec::new(), [Vec::new(), Vec::new()]);
for y in t..b {
line.clear();
line.extend_from_slice(&picture.pixels[y * wide + l..y * wide + r]);
extremes(&line, &mut picture.pixels[y * wide + l..y * wide + r], across, thicker, &mut room);
}
for x in l..r {
line.clear();
line.extend((t..b).map(|y| picture.pixels[y * wide + x]));
out.clone_from(&line);
extremes(&line, &mut out, down, thicker, &mut room);
out.iter().enumerate().for_each(|(n, pixel)| picture.pixels[(t + n) * wide + x] = *pixel);
}
picture
}
#[inline(always)]
fn most_of(a: u32, b: u32) -> u32 {
(a >> 24).max(b >> 24) << 24 | (a >> 16 & 255).max(b >> 16 & 255) << 16 | (a >> 8 & 255).max(b >> 8 & 255) << 8 | (a & 255).max(b & 255)
}
#[inline(always)]
fn least_of(a: u32, b: u32) -> u32 {
(a >> 24).min(b >> 24) << 24 | (a >> 16 & 255).min(b >> 16 & 255) << 16 | (a >> 8 & 255).min(b >> 8 & 255) << 8 | (a & 255).min(b & 255)
}
fn extremes(line: &[u32], out: &mut [u32], radius: usize, most: bool, [from_start, to_end]: &mut [Vec<u32>; 2]) {
let (n, size) = (line.len(), radius.saturating_mul(2).saturating_add(1));
let pick = |a: u32, b: u32| if most { most_of(a, b) } else { least_of(a, b) };
if n == 0 {
return;
}
from_start.clear();
from_start.extend_from_slice(line);
to_end.clone_from(from_start);
let mut place = 0;
for at in 1..n {
place = if place + 1 == size { 0 } else { place + 1 };
if place != 0 {
from_start[at] = pick(from_start[at - 1], line[at]);
}
}
let mut place = (n - 1) % size;
for at in (0..n.saturating_sub(1)).rev() {
if place != 0 {
to_end[at] = pick(to_end[at + 1], line[at]);
}
place = if place == 0 { size - 1 } else { place - 1 };
}
let mut ends = [0, radius.min(n - 1) % size];
for (at, out) in out.iter_mut().enumerate() {
let (low, high) = (at.saturating_sub(radius), at.saturating_add(radius).min(n - 1));
*out = match (ends[1] >= ends[0], ends[0] == 0) {
_ if !most && (at < radius || at + radius >= n) => 0,
(true, true) => from_start[high],
(true, false) => to_end[low],
(false, _) => pick(to_end[low], from_start[high]),
};
let next = |end: usize| if end + 1 == size { 0 } else { end + 1 };
ends = [if at >= radius { next(ends[0]) } else { ends[0] }, if at + radius + 1 < n { next(ends[1]) } else { ends[1] }];
}
}
fn tiled(picture: Picture, part: [usize; 4], wide: usize) -> Picture {
let [tl, tt, tr, tb] = picture.part;
let mut pixels = vec![0; picture.pixels.len()];
if tl >= tr || tt >= tb {
return Picture { pixels, part, ..picture };
}
let (tile_wide, tile_high) = (tr - tl, tb - tt);
for y in part[1]..part[3] {
let from = tt + (y as i64 - tt as i64).rem_euclid(tile_high as i64) as usize;
for x in part[0]..part[2] {
pixels[y * wide + x] = picture.pixels[from * wide + tl + (x as i64 - tl as i64).rem_euclid(tile_wide as i64) as usize];
}
}
Picture { pixels, part, ..picture }
}
struct Lattice {
selector: [usize; 514],
gradient: [[[f64; 2]; 514]; 4],
}
const SIZE: usize = 256;
const AWAY: f64 = 4096.0;
impl Lattice {
fn new(seed: f32) -> Lattice {
let mut lattice = Lattice { selector: [0; 514], gradient: [[[0.0; 2]; 514]; 4] };
let mut seed = start(seed.trunc() as i64);
for k in 0..4 {
for i in 0..SIZE {
lattice.selector[i] = i;
lattice.gradient[k][i] = lattice.point(&mut seed);
}
}
for i in (1..SIZE).rev() {
seed = random(seed);
lattice.selector.swap(i, seed as usize % SIZE);
}
for i in 0..SIZE + 2 {
lattice.selector[SIZE + i] = lattice.selector[i];
(0..4).for_each(|k| lattice.gradient[k][SIZE + i] = lattice.gradient[k][i]);
}
lattice
}
fn point(&self, seed: &mut i64) -> [f64; 2] {
let mut next = || {
*seed = random(*seed);
((*seed % (2 * SIZE as i64)) - SIZE as i64) as f64 / SIZE as f64
};
let (x, y) = (next(), next());
let long = (x * x + y * y).sqrt();
[x / long, y / long]
}
fn noise(&self, channel: usize, [x, y]: [f64; 2], stitch: Option<[i64; 4]>) -> f64 {
let (tx, ty) = (x + AWAY, y + AWAY);
let ([mut bx0, mut by0], [rx0, ry0]) = ([tx as i64, ty as i64], [tx - (tx as i64) as f64, ty - (ty as i64) as f64]);
let [mut bx1, mut by1] = [bx0.saturating_add(1), by0.saturating_add(1)];
if let Some([wide, high, wrap_x, wrap_y]) = stitch {
[bx0, bx1] = [bx0, bx1].map(|b| if b >= wrap_x { b - wide } else { b });
[by0, by1] = [by0, by1].map(|b| if b >= wrap_y { b - high } else { b });
}
let [bx0, bx1, by0, by1] = [bx0, bx1, by0, by1].map(|b| (b & 255) as usize);
let (i, j) = (self.selector[bx0], self.selector[bx1]);
let corners = [self.selector[i + by0], self.selector[j + by0], self.selector[i + by1], self.selector[j + by1]];
let s_curve = |t: f64| t * t * (3.0 - 2.0 * t);
let (sx, sy, rx1, ry1) = (s_curve(rx0), s_curve(ry0), rx0 - 1.0, ry0 - 1.0);
let dot = |corner: usize, x: f64, y: f64| x * self.gradient[channel][corner][0] + y * self.gradient[channel][corner][1];
let lerp = |t: f64, a: f64, b: f64| a + t * (b - a);
let a = lerp(sx, dot(corners[0], rx0, ry0), dot(corners[1], rx1, ry0));
let b = lerp(sx, dot(corners[2], rx0, ry1), dot(corners[3], rx1, ry1));
lerp(sy, a, b)
}
fn pixel(&self, noise: &Noise, at: [f64; 2], tile: [f64; 4]) -> u32 {
let [mut fx, mut fy] = noise.base.map(f64::from);
let mut stitch = None;
if noise.stitch {
fx = stitched(fx, tile[2]);
fy = stitched(fy, tile[3]);
let (wide, high) = ((tile[2] * fx + 0.5) as i64, (tile[3] * fy + 0.5) as i64);
stitch = Some([wide, high, ((tile[0] * fx + AWAY) as i64).saturating_add(wide), ((tile[1] * fy + AWAY) as i64).saturating_add(high)]);
}
let mut sums = [0.0f64; 4];
let (mut place, mut ratio) = ([at[0] * fx, at[1] * fy], 1.0);
for _ in 0..noise.octaves.min(OCTAVES) {
for (channel, sum) in sums.iter_mut().enumerate() {
let value = self.noise(channel, place, stitch);
*sum += if noise.fractal { value } else { value.abs() } / ratio;
}
(place, ratio) = ([place[0] * 2.0, place[1] * 2.0], ratio * 2.0);
let wrapped = |wrap: i64| wrap.saturating_mul(2).saturating_sub(AWAY as i64);
stitch = stitch.map(|[wide, high, wrap_x, wrap_y]| [wide.saturating_mul(2), high.saturating_mul(2), wrapped(wrap_x), wrapped(wrap_y)]);
}
let level = |sum: f64| ((if noise.fractal { (sum * 255.0 + 255.0) / 2.0 } else { sum * 255.0 }) + 0.5).clamp(0.0, 255.0) as u32;
let [r, g, b, a] = sums.map(level);
pixel([a, times(r, a), times(g, a), times(b, a)])
}
}
fn stitched(frequency: f64, side: f64) -> f64 {
if frequency == 0.0 {
return frequency;
}
let (low, high) = ((side * frequency).floor() / side, (side * frequency).ceil() / side);
if frequency / low < high / frequency { low } else { high }
}
fn start(seed: i64) -> i64 {
let seed = if seed <= 0 { -(seed % (RANDOM_M - 1)) + 1 } else { seed };
seed.min(RANDOM_M - 1)
}
fn random(seed: i64) -> i64 {
let next = 16807 * (seed % 127_773) - 2836 * (seed / 127_773);
if next <= 0 { next + RANDOM_M } else { next }
}
const RANDOM_M: i64 = 2_147_483_647;
#[cfg(test)]
mod tests {
use super::*;
fn chance(seed: &mut u64) -> u32 {
*seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1_442_695_040_888_963_407);
(*seed >> 32) as u32
}
#[test]
fn thicker_and_thinner_are_the_most_and_least_of_each_window() {
let mut seed = 7;
for (n, radius) in (1..40).flat_map(|n| [0, 1, 2, 3, 7, 50].map(|radius| (n, radius))) {
let line: Vec<u32> = (0..n).map(|_| chance(&mut seed)).collect();
for most in [true, false] {
let mut out = vec![0; n];
extremes(&line, &mut out, radius, most, &mut [Vec::new(), Vec::new()]);
let plain = (0..n).map(|at| match most {
true => line[at.saturating_sub(radius)..(at + radius + 1).min(n)].iter().fold(0, |a, b| most_of(a, *b)),
false if at < radius || at + radius >= n => 0,
false => line[at - radius..=at + radius].iter().fold(u32::MAX, |a, b| least_of(a, *b)),
});
assert_eq!(out, plain.collect::<Vec<u32>>(), "{n} long, radius {radius}, most {most}");
}
}
}
#[test]
fn stitched_noise_meets_itself_at_the_edges_of_its_tile() {
let lattice = Lattice::new(3.0);
let noise = |stitch: bool| Noise { base: [0.052, 0.07], octaves: 3, seed: 3.0, stitch, fractal: true };
let tile = [0.0, 0.0, 40.0, 30.0];
for along in [0.5, 7.25, 13.0, 29.5] {
let [left, right, top, bottom] = [[0.0, along], [40.0, along], [along, 0.0], [along, 30.0]].map(|at| lattice.pixel(&noise(true), at, tile));
assert!(left == right && top == bottom, "{along}");
assert_ne!(lattice.pixel(&noise(false), [0.0, along], tile), lattice.pixel(&noise(false), [40.0, along], tile));
}
}
#[test]
fn the_bell_curve_is_the_integral_of_itself() {
for x in [-3.0f32, -1.2, -0.3, 0.0, 0.1, 0.7, 1.9, 4.0] {
let steps = 100_000;
let integral = (0..steps).map(|n| (n as f64 + 0.5) * x as f64 / steps as f64).map(|t| (-t * t).exp()).sum::<f64>() * x as f64 / steps as f64;
assert!((erf(x) as f64 - integral * 2.0 / std::f64::consts::PI.sqrt()).abs() < 3e-6, "{x}");
}
let weights = curve(1.0);
assert!(weights.len() == 7 && (weights.iter().sum::<f32>() - 1.0).abs() < 1e-6 && (weights[3] - 0.3830).abs() < 3e-4 && (weights[4] - 0.2418).abs() < 3e-4);
}
#[test]
fn a_blur_keeps_all_it_spreads() {
let wide = 64;
let mut pixels = vec![0u32; wide * wide];
(28..36).for_each(|y| pixels[y * wide + 28..y * wide + 36].fill(0xff40_80c0));
for deviation in [0.6, 1.5, 2.0, 3.7, 5.0] {
let picture = Picture { pixels: pixels.clone(), linear: false, part: [0, 0, wide, wide] };
let blurred = blurred(picture, [0, 0, wide, wide], wide, [deviation, deviation]);
let solid = |pixels: &[u32]| pixels.iter().map(|pixel| (pixel >> 24) as f32).sum::<f32>();
let (before, after) = (solid(&pixels), solid(&blurred.pixels));
assert!((before - after).abs() / before < 0.01, "{deviation}: {before} {after}");
}
}
#[test]
fn colours_are_unmultiplied_as_a_division_would() {
let light = Light::new();
let same: [u8; 256] = std::array::from_fn(|n| n as u8);
for a in 1..256u32 {
for c in 0..=a {
let divided = (c * 255 + a / 2) / a;
let pixel = a << 24 | c << 16;
assert_eq!(changed(pixel, &same, &light.plain) >> 16 & 255, (divided * a + 127) / 255, "{c} of {a}");
}
}
}
}