[−][src]Struct purrmitive::math::nq::NeuQuant
Neural network color quantizer
Examples
use image::imageops::colorops::{index_colors, ColorMap}; use image::math::nq::NeuQuant; use image::{ImageBuffer, Rgba, RgbaImage}; // Create simple color image with RGBA pixels. let (w, h) = (2, 2); let red: Rgba<u8> = [255, 0, 0, 255].into(); let green: Rgba<u8> = [0, 255, 0, 255].into(); let blue: Rgba<u8> = [0, 0, 255, 255].into(); let white: Rgba<u8> = [255, 255, 255, 255].into(); let mut color_image = RgbaImage::new(w, h); color_image.put_pixel(0, 0, red); color_image.put_pixel(1, 0, green); color_image.put_pixel(0, 1, blue); color_image.put_pixel(1, 1, white); // Create a `NeuQuant` colormap that will build an approximate color palette that best matches // the original image. // Note, the NeuQuant algorithm is only designed to work with 6-8 bit output, so `colors` // should be a power of 2 in the range [64, 256]. let pixels = color_image.clone().into_raw(); let cmap = NeuQuant::new(1, 256, &pixels); // Map the original image through the color map to create an indexed image stored in a // `GrayImage`. let palletized = index_colors(&color_image, &cmap); // Map indexed image back `RgbaImage`. Note the NeuQuant algorithm creates an approximation of // the original colors, so even in this simple example the output is not pixel equivalent to // the original. let mapped = ImageBuffer::from_fn(w, h, |x, y| -> Rgba<u8> { let p = palletized.get_pixel(x, y); cmap.lookup(p.0[0] as usize) .expect("indexed color out-of-range") .into() });
Implementations
impl NeuQuant
[src]
pub fn new(samplefac: i32, colors: usize, pixels: &[u8]) -> NeuQuant
[src]
Creates a new neural network and trains it with the supplied data
pub fn init(&mut self, pixels: &[u8])
[src]
Initializes the neural network and trains it with the supplied data
pub fn map_pixel(&self, pixel: &mut [u8])
[src]
Maps the pixel in-place to the best-matching color in the color map
pub fn index_of(&self, pixel: &[u8]) -> usize
[src]
Finds the best-matching index in the color map for pixel
pub fn lookup(&self, idx: usize) -> Option<[u8; 4]>
[src]
Lookup pixel values for color at idx
in the colormap.
Trait Implementations
impl ColorMap for NeuQuant
[src]
type Color = Rgba<u8>
The color type on which the map operates on
pub fn index_of(&self, color: &Rgba<u8>) -> usize
[src]
pub fn lookup(&self, idx: usize) -> Option<<NeuQuant as ColorMap>::Color>
[src]
pub fn has_lookup(&self) -> bool
[src]
Indicate NeuQuant implements lookup
.
pub fn map_color(&self, color: &mut Rgba<u8>)
[src]
Auto Trait Implementations
impl RefUnwindSafe for NeuQuant
[src]
impl Send for NeuQuant
[src]
impl Sync for NeuQuant
[src]
impl Unpin for NeuQuant
[src]
impl UnwindSafe for NeuQuant
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> SetParameter for T
[src]
pub fn set<T>(&mut self, value: T) -> <T as Parameter<Self>>::Result where
T: Parameter<Self>,
[src]
T: Parameter<Self>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,