Struct color_quant::NeuQuant

source ·
pub struct NeuQuant {
    pub(crate) network: Vec<Quad<f64>>,
    pub(crate) colormap: Vec<Quad<i32>>,
    pub(crate) netindex: Vec<usize>,
    pub(crate) bias: Vec<f64>,
    pub(crate) freq: Vec<f64>,
    pub(crate) samplefac: i32,
    pub(crate) netsize: usize,
}

Fields§

§network: Vec<Quad<f64>>§colormap: Vec<Quad<i32>>§netindex: Vec<usize>§bias: Vec<f64>§freq: Vec<f64>§samplefac: i32§netsize: usize

Implementations§

source§

impl NeuQuant

source

pub fn new(samplefac: i32, colors: usize, pixels: &[u8]) -> Self

Creates a new neuronal network and trains it with the supplied data.

Pixels are assumed to be in RGBA format. colors should be $>=64$. samplefac determines the faction of the sample that will be used to train the network. Its value must be in the range $[1, 30]$. A value of $1$ thus produces the best result but is also slowest. $10$ is a good compromise between speed and quality.

source

pub fn init(&mut self, pixels: &[u8])

Initializes the neuronal network and trains it with the supplied data.

This method gets called by Self::new.

source

pub fn map_pixel(&self, pixel: &mut [u8])

Maps the rgba-pixel in-place to the best-matching color in the color map.

source

pub fn index_of(&self, pixel: &[u8]) -> usize

Finds the best-matching index in the color map.

pixel is assumed to be in RGBA format.

source

pub fn lookup(&self, idx: usize) -> Option<[u8; 4]>

Lookup pixel values for color at idx in the colormap.

source

pub fn color_map_rgba(&self) -> Vec<u8>

Returns the RGBA color map calculated from the sample.

source

pub fn color_map_rgb(&self) -> Vec<u8>

Returns the RGBA color map calculated from the sample.

source

pub(crate) fn salter_single(&mut self, alpha: f64, i: i32, quad: Quad<f64>)

Move neuron i towards biased (a,b,g,r) by factor alpha

source

pub(crate) fn alter_neighbour( &mut self, alpha: f64, rad: i32, i: i32, quad: Quad<f64>, )

Move neuron adjacent neurons towards biased (a,b,g,r) by factor alpha

source

pub(crate) fn contest(&mut self, b: f64, g: f64, r: f64, a: f64) -> i32

Search for biased BGR values finds closest neuron (min dist) and updates freq finds best neuron (min dist-bias) and returns position for frequently chosen neurons, freq[i] is high and bias[i] is negative bias[i] = gamma*((1/self.netsize)-freq[i])

source

pub(crate) fn learn(&mut self, pixels: &[u8])

Main learning loop Note: the number of learning cycles is crucial and the parameters are not optimized for net sizes < 26 or > 256. 1064 colors seems to work fine

source

pub(crate) fn build_colormap(&mut self)

initializes the color map

source

pub(crate) fn build_netindex(&mut self)

Insertion sort of network and building of netindex[0..255]

source

pub(crate) fn search_netindex(&self, b: u8, g: u8, r: u8, a: u8) -> usize

Search for best matching color

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.