#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
use atools::prelude::*;
use std::cmp::{max, min};
const RADIUS_DEC: i32 = 30;
const ALPHA_BIASSHIFT: i32 = 10; const INIT_ALPHA: i32 = 1 << ALPHA_BIASSHIFT;
const GAMMA: f64 = 1024.0;
const BETA: f64 = 1.0 / GAMMA;
const BETAGAMMA: f64 = BETA * GAMMA;
const PRIMES: [usize; 4] = [499, 491, 487, 503];
pub enum ControlFlow {
Break,
Continue,
}
impl ControlFlow {
fn is_break(self) -> bool {
matches!(self, ControlFlow::Break)
}
}
type Neuron = [f64; 4];
pub type Color = [u8; 4];
pub struct NeuQuant {
network: Vec<Neuron>,
colormap: Vec<Color>,
netindex: Box<[usize; 256]>,
bias: Vec<f64>, freq: Vec<f64>,
samplefac: i32,
netsize: usize,
}
impl NeuQuant {
pub fn new(samplefac: i32, colors: usize, pixels: &[[u8; 4]]) -> Self {
let netsize = colors;
let mut this = NeuQuant {
network: Vec::with_capacity(netsize),
colormap: Vec::with_capacity(netsize),
netindex: vec![0; 256].try_into().unwrap(),
bias: Vec::with_capacity(netsize),
freq: Vec::with_capacity(netsize),
netsize: colors,
samplefac,
};
this.init(pixels);
this
}
pub fn init(&mut self, pixels: &[[u8; 4]]) {
self.network.clear();
self.colormap.clear();
self.bias.clear();
self.freq.clear();
let freq = (self.netsize as f64).recip();
for i in 0..self.netsize {
let tmp = (i as f64) * 256.0 / (self.netsize as f64);
let a = if i < 16 { i as f64 * 16.0 } else { 255.0 };
self.network.push([tmp, tmp, tmp, a]);
self.colormap.push([0, 0, 0, 255]);
self.freq.push(freq);
self.bias.push(0.0);
}
self.learn(pixels);
self.build_colormap();
self.build_netindex();
}
#[inline(always)]
pub fn map_pixel(&self, pixel: &mut [u8; 4]) {
let [r, g, b, a] = *pixel;
let i = self.search_netindex(b, g, r, a);
*pixel = self.colormap[i];
}
#[inline(always)]
pub fn index_of(&self, [r, g, b, a]: [u8; 4]) -> usize {
self.search_netindex(b, g, r, a)
}
pub fn lookup(&self, idx: usize) -> Option<[u8; 4]> {
self.colormap.get(idx).copied()
}
pub fn take_color_map(self) -> Vec<[u8; 4]> {
self.colormap
}
pub fn color_map_rgba(&self) -> impl Iterator<Item = [u8; 4]> + '_ {
self.colormap.iter().copied()
}
pub fn color_map_rgb(&self) -> impl Iterator<Item = [u8; 3]> + '_ {
self.colormap.iter().map(|q| q.init())
}
fn salter_single(&mut self, alpha: f64, i: i32, quad: [f64; 4]) {
let n = &mut self.network[i as usize];
*n = n.asub(alpha.mul(n.asub(quad)));
}
fn alter_neighbour(&mut self, alpha: f64, rad: i32, i: i32, quad: [f64; 4]) {
let lo = max(i - rad, 0);
let hi = min(i + rad, self.netsize as i32);
let mut j = i + 1;
let mut k = i - 1;
let mut q = 0;
while (j < hi) || (k > lo) {
let rad_sq = rad as f64 * rad as f64;
let alpha = (alpha * (rad_sq - q as f64 * q as f64)) / rad_sq;
q += 1;
if j < hi {
let p = &mut self.network[j as usize];
*p = p.asub(alpha.mul(p.asub(quad)));
j += 1;
}
if k > lo {
let p = &mut self.network[k as usize];
*p = p.asub(alpha.mul(p.asub(quad)));
k -= 1;
}
}
}
fn contest(&mut self, b: f64, g: f64, r: f64, a: f64) -> i32 {
use std::f64;
let mut bestd = f64::MAX;
let mut bestbiasd: f64 = bestd;
let mut bestpos = -1;
let mut bestbiaspos: i32 = bestpos;
for i in 0..self.netsize {
let bestbiasd_biased = bestbiasd + self.bias[i];
let mut dist;
let &[nr, ng, nb, na] = &self.network[i];
dist = (nb - b).abs();
dist += (nr - r).abs();
if dist < bestd || dist < bestbiasd_biased {
dist += (ng - g).abs();
dist += (na - a).abs();
if dist < bestd {
bestd = dist;
bestpos = i as i32;
}
let biasdist = dist - self.bias[i];
if biasdist < bestbiasd {
bestbiasd = biasdist;
bestbiaspos = i as i32;
}
}
self.freq[i] -= BETA * self.freq[i];
self.bias[i] += BETAGAMMA * self.freq[i];
}
self.freq[bestpos as usize] += BETA;
self.bias[bestpos as usize] -= BETAGAMMA;
bestbiaspos
}
fn learn(&mut self, pixels: &[[u8; 4]]) {
let initrad: i32 = self.netsize as i32 / 8; let radiusbiasshift: i32 = 6;
let radiusbias: i32 = 1 << radiusbiasshift;
let init_bias_radius: i32 = initrad * radiusbias;
let mut bias_radius = init_bias_radius;
let alphadec = 30 + ((self.samplefac - 1) / 3);
let lengthcount = pixels.len();
let samplepixels = lengthcount / self.samplefac as usize;
let n_cycles = match self.netsize >> 1 {
n if n <= 100 => 100,
n => n,
};
let delta = match samplepixels / n_cycles {
0 => 1,
n => n,
};
let mut alpha = INIT_ALPHA;
let mut rad = bias_radius >> radiusbiasshift;
if rad <= 1 {
rad = 0
};
let mut pos = 0;
let step = *PRIMES
.iter()
.find(|&&prime| lengthcount % prime != 0)
.unwrap_or(&PRIMES[3]);
let mut i = 0;
while i < samplepixels {
let [r, g, b, a] = pixels[pos].map(|x| x as f64);
let j = self.contest(b, g, r, a);
let alpha_ = (1.0 * alpha as f64) / INIT_ALPHA as f64;
self.salter_single(alpha_, j, [r, g, b, a]);
if rad > 0 {
self.alter_neighbour(alpha_, rad, j, [r, g, b, a])
};
pos += step;
while pos >= lengthcount {
pos -= lengthcount
}
i += 1;
if i % delta == 0 {
alpha -= alpha / alphadec;
bias_radius -= bias_radius / RADIUS_DEC;
rad = bias_radius >> radiusbiasshift;
if rad <= 1 {
rad = 0
};
}
}
}
fn build_colormap(&mut self) {
for (m, n) in self.colormap.iter_mut().zip(self.network.iter()) {
*m = n.map(|x| x.round() as u8);
}
}
fn build_netindex(&mut self) {
let mut previouscol = 0;
let mut startpos = 0;
for i in 0..self.netsize {
let mut p = self.colormap[i];
let mut q;
let mut smallpos = i;
let mut smallval = p[1] as usize; for j in (i + 1)..self.netsize {
q = self.colormap[j];
if (q[1] as usize) < smallval {
smallpos = j;
smallval = q[1] as usize; }
}
q = self.colormap[smallpos];
if i != smallpos {
::std::mem::swap(&mut p, &mut q);
self.colormap[i] = p;
self.colormap[smallpos] = q;
}
if smallval != previouscol {
self.netindex[previouscol] = (startpos + i) >> 1;
for j in (previouscol + 1)..smallval {
self.netindex[j] = i
}
previouscol = smallval;
startpos = i;
}
}
let max_netpos = self.netsize - 1;
self.netindex[previouscol] = (startpos + max_netpos) >> 1;
for j in (previouscol + 1)..256 {
self.netindex[j] = max_netpos
} }
fn search_netindex(&self, b: u8, g: u8, r: u8, a: u8) -> usize {
let mut best_dist = std::i32::MAX;
let first_guess = self.netindex[g as usize];
let mut best_pos = first_guess;
let mut i = best_pos;
#[inline]
fn sqr_dist(a: i32, b: u8) -> i32 {
let dist = a - b as i32;
dist * dist
}
{
let mut cmp = |i| {
let x: [u8; 4] = self.colormap[i];
let [pr, pg, pb, pa] = x.map(|x| x as i32);
let mut dist = sqr_dist(pg, g);
if dist > best_dist {
return ControlFlow::Break;
}
dist += sqr_dist(pr, r);
if dist >= best_dist {
return ControlFlow::Continue;
}
dist += sqr_dist(pb, b);
if dist >= best_dist {
return ControlFlow::Continue;
}
dist += sqr_dist(pa, a);
if dist >= best_dist {
return ControlFlow::Continue;
}
best_dist = dist;
best_pos = i;
ControlFlow::Continue
};
while i < self.netsize {
i = if cmp(i).is_break() { break } else { i + 1 };
}
let mut j = first_guess.wrapping_sub(1);
while j < self.netsize {
j = if cmp(j).is_break() {
break;
} else {
j.wrapping_sub(1)
};
}
}
best_pos
}
}