Struct exoquant::Quantizer
[−]
[src]
pub struct Quantizer(_);
The main color quantizer state.
The Quantizer is used to find a palette of colors that represent the colors in an input Histogram as well as possible.
To use it you first create a new Quantizer instance using Quantizer::new
, then call
quantizer.step()
until quantizer.num_colors()
reaches your target color count, then
call quantizer.colors()
to get your final palette Vec
of Color
s.
Optionally you can call quantizer.optimize()
between calls to quantizer.step()
to
run K-Means optimizations during quantization.
If you don't intend to do K-Means optimzations during quantization, you can also just
use the shortcut function Quantizer::create_palette
.
Examples
let mut quantizer = Quantizer::new(&histogram, &colorspace); while quantizer.num_colors() < 256 { quantizer.step(); } let palette = quantizer.colors(&colorspace);
let palette = Quantizer::create_palette(&histogram, &colorspace, 256);
Methods
impl Quantizer
[src]
fn new<T: ColorSpace>(histogram: &Histogram, colorspace: &T) -> Quantizer
Create a new Quantizer state for the given histogram.
fn create_palette<T: ColorSpace>(histogram: &Histogram, colorspace: &T, num_colors: usize) -> Vec<Color>
A shortcut function to directly create a palette from a histogram.
fn num_colors(&self) -> usize
Returns the current number of colors in this Quantizer state.
This starts off at 1 and increases by 1 for each call to quantizer.step()
.
fn step(&mut self)
Run one quantization step which increases the num_colors()
by one.
fn colors<T: ColorSpace>(&self, colorspace: &T) -> Vec<Color>
Returns colors the current Quantizer state represents..
fn optimize(self, optimizer: &Optimizer, num_iterations: usize) -> Quantizer
Run a number of K-Means iteration on the current quantizer state.
This can improve the quality of the final palette by a certain amount,
but note that this is a fairly expensive operation. You might find it
sufficient to only run optimizations on the final palette, in which case
optimizer.optimize_palette
is slightly less expensive.
Examples
let optimizer = optimizer::KMeans; let mut quantizer = Quantizer::new(&histogram, &colorspace); while quantizer.num_colors() < 256 { quantizer.step(); if quantizer.num_colors() % 32 == 0 { quantizer = quantizer.optimize(&optimizer, 4); } } let palette = quantizer.colors(&colorspace);