Struct nannou::image::math::nq::NeuQuant

source ·
pub struct NeuQuant { /* private fields */ }
👎Deprecated: Use the color_quant crate instead
Expand description

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§

source§

impl NeuQuant

The implementation only calls the corresponding inner color_quant methods.

These exist purely to keep a type separate from color_quant::NeuQuant and the interface stable for this major version. The type will be changed to a pure re-export in the next version or might be removed.

source

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

source

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

source

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

source

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

source

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

Trait Implementations§

source§

impl ColorMap for NeuQuant

source§

fn has_lookup(&self) -> bool

Indicate NeuQuant implements lookup.

§

type Color = Rgba<u8>

The color type on which the map operates on
source§

fn index_of(&self, color: &Rgba<u8>) -> usize

Returns the index of the closest match of color in the color map.
source§

fn lookup(&self, idx: usize) -> Option<<NeuQuant as ColorMap>::Color>

Looks up color by index in the color map. If idx is out of range for the color map, or ColorMap doesn’t implement lookup None is returned.
source§

fn map_color(&self, color: &mut Rgba<u8>)

Maps color to the closest color in the color map.
source§

impl From<NeuQuant> for NeuQuant

source§

fn from(inner: NeuQuant) -> NeuQuant

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
where T: Component + Float, Swp: WhitePoint, Dwp: WhitePoint, D: AdaptFrom<S, Swp, Dwp, T>,

source§

fn adapt_into_using<M>(self, method: M) -> D
where M: TransformMatrix<Swp, Dwp, T>,

Convert the source color to the destination color using the specified method
source§

fn adapt_into(self) -> D

Convert the source color to the destination color using the bradford method by default
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, U> ConvertInto<U> for T
where U: ConvertFrom<T>,

source§

fn convert_into(self) -> U

Convert into T with values clamped to the color defined bounds Read more
source§

fn convert_unclamped_into(self) -> U

Convert into T. The resulting color might be invalid in its color space Read more
source§

fn try_convert_into(self) -> Result<U, OutOfBounds<U>>

Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSync for T
where T: Sync,