### Example Searches Source: https://docs.rs/quantette/0.6.0/quantette/trait.BoundedIndex.html?search= These are example search queries demonstrating common patterns. ```text Example searches: * std::vec * u32 -> bool * Option, (T -> U) -> Option ``` -------------------------------- ### CreateImageError Search Examples Source: https://docs.rs/quantette/0.6.0/quantette/struct.CreateImageError.html?search= Provides example searches to help users find relevant information about CreateImageError. ```APIDOC ## Example Searches ### Search for standard vector types: ``` std::vec ``` ### Search for type conversion from u32 to bool: ``` u32 -> bool ``` ### Search for Option type with generic transformation: ``` Option, (T -> U) -> Option ``` ``` -------------------------------- ### Full Image WuU8x3 Quantization Example Source: https://docs.rs/quantette/0.6.0/quantette/wu/struct.WuU8x3.html Shows how to quantize colors from a full image using WuU8x3. This example requires the 'ImageBuf' type and demonstrates mapping the quantized colors back to an image. The 'wu' module and 'palette' crate must be imported. ```rust use quantette::{ wu::{BinnerU8x3, WuU8x3}, ImageBuf, PaletteSize, }; use palette::Srgb; let image = ImageBuf::new(1, 1, vec![Srgb::new(0, 0, 0)])?; let binner = BinnerU8x3::rgb(); let color_map = WuU8x3::run_image(image.as_ref(), binner).unwrap().color_map(PaletteSize::MAX); let quantized = image.map_to_image(&color_map); assert_eq!(image.dimensions(), quantized.dimensions()); ``` -------------------------------- ### Example Usage of PaletteBuf_search Source: https://docs.rs/quantette/0.6.0/quantette/struct.PaletteBuf.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Demonstrates the expected output when iterating over a PaletteBuf_search. ```rust assert_eq!(iter.next(), Some(Range { start: 0, end: 0 })); assert_eq!(iter.next(), Some(Range { start: 1, end: 3 })); assert_eq!(iter.next(), Some(Range { start: 4, end: 4 })); assert_eq!(iter.next(), Some(Range { start: 5, end: 6 })); ``` -------------------------------- ### Converting an RgbImage to ImageRef Source: https://docs.rs/quantette/0.6.0/quantette/type.ImageRef.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Example showing how to convert an RgbImage from the 'image' crate into an ImageRef. ```APIDOC ## Example: Converting an `RgbImage` to `ImageRef` ### Description This example demonstrates converting an existing `RgbImage` (from the popular 'image' crate) into an `ImageRef` using `try_from`. ### Code ```rust let image = RgbImage::new(256, 256); let image = ImageRef::try_from(&image)?; ``` ``` -------------------------------- ### Converting an RgbImage to ImageMut Source: https://docs.rs/quantette/0.6.0/quantette/type.ImageMut.html?search= Example showing how to convert a mutable RgbImage from the 'image' crate into an ImageMut. ```APIDOC Converting a mutable reference to a `RgbImage` from the `image` crate to an `ImageMut`: ```rust let mut image = RgbImage::new(256, 256); let image = ImageMut::try_from(&mut image)?; ``` ``` -------------------------------- ### Split Off Slice Starting with Third Element Source: https://docs.rs/quantette/0.6.0/quantette/struct.PaletteBuf.html?search=u32+-%3E+bool Demonstrates using `split_off` to remove and return a subslice starting from a specified index. This example splits off the elements from the third one onwards. ```Rust let mut slice: &[_] = &['a', 'b', 'c', 'd']; let mut tail = slice.split_off(2..).unwrap(); assert_eq!(slice, &['a', 'b']); assert_eq!(tail, &['c', 'd']); ``` -------------------------------- ### Pipeline Initialization and Configuration Source: https://docs.rs/quantette/0.6.0/quantette/struct.Pipeline.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Demonstrates how to create a new Pipeline instance and configure various quantization options using method chaining. ```APIDOC ## Pipeline::new() ### Description Initializes a new `Pipeline` builder with default settings. ### Method Associated function (constructor) ### Parameters None ### Returns A new `Pipeline` instance. ## Pipeline::palette_size(size: u16) -> &mut Self ### Description Sets the desired size of the output color palette. ### Method Builder method ### Parameters - **size** (u16) - The number of colors in the palette. ### Returns A mutable reference to the `Pipeline` instance. ## Pipeline::ditherer(ditherer: Option>) -> &mut Self ### Description Sets the ditherer algorithm to use for quantization. ### Method Builder method ### Parameters - **ditherer** (Option>) - An optional ditherer implementation. `None` disables dithering. ### Returns A mutable reference to the `Pipeline` instance. ## Pipeline::quantize_method(method: QuantizeMethod) -> &mut Self ### Description Specifies the method to be used for color quantization. ### Method Builder method ### Parameters - **method** (QuantizeMethod) - The quantization algorithm to apply (e.g., `kmeans()`). ### Returns A mutable reference to the `Pipeline` instance. ## Pipeline::parallel(parallel: bool) -> &mut Self ### Description Enables or disables parallel processing for quantization. ### Method Builder method ### Parameters - **parallel** (bool) - `true` to enable parallel processing, `false` otherwise. ### Returns A mutable reference to the `Pipeline` instance. ### Example ```rust use quantette::{Pipeline, QuantizeMethod}; let pipeline = Pipeline::new() .palette_size(32u16.try_into()?) .ditherer(None) .quantize_method(QuantizeMethod::kmeans()) .parallel(true); ``` ``` -------------------------------- ### Split Off Slice Starting with Third Element Mutably Source: https://docs.rs/quantette/0.6.0/quantette/struct.PaletteBuf.html?search=u32+-%3E+bool Demonstrates using `split_off_mut` to remove and return a mutable reference to a subslice starting from a specified index. This example splits off mutable elements from the third one onwards. ```Rust let mut slice: &mut [_] = &mut ['a', 'b', 'c', 'd']; let mut tail = slice.split_off_mut(2..).unwrap(); assert_eq!(slice, &mut ['a', 'b']); assert_eq!(tail, &mut ['c', 'd']); ``` -------------------------------- ### Pipeline Initialization and Configuration Source: https://docs.rs/quantette/0.6.0/quantette/struct.Pipeline.html?search= Demonstrates how to create a new Pipeline instance and configure various quantization options such as palette size, dithering, quantization method, and parallel processing. ```APIDOC ## Pipeline::new() ### Description Initializes a new `Pipeline` struct with default settings. ### Method Associated function (constructor) ### Parameters None ### Returns A new `Pipeline` instance. ## Pipeline::palette_size(size: u16) ### Description Sets the desired size of the color palette for quantization. ### Method Builder method ### Parameters - **size** (u16) - The number of colors in the palette. ### Returns `Self` (the `Pipeline` instance) for chaining. ## Pipeline::ditherer(ditherer: Option) ### Description Configures the dithering algorithm to use during quantization. Setting to `None` disables dithering. ### Method Builder method ### Parameters - **ditherer** (Option) - An optional ditherer implementation. ### Returns `Self` (the `Pipeline` instance) for chaining. ## Pipeline::quantize_method(method: QuantizeMethod) ### Description Specifies the quantization algorithm to be used. ### Method Builder method ### Parameters - **method** (QuantizeMethod) - The quantization method, e.g., `QuantizeMethod::kmeans()`. ### Returns `Self` (the `Pipeline` instance) for chaining. ## Pipeline::parallel(parallel: bool) ### Description Enables or disables parallel processing for quantization. ### Method Builder method ### Parameters - **parallel** (bool) - `true` to enable parallel processing, `false` otherwise. ### Returns `Self` (the `Pipeline` instance) for chaining. ### Example ```rust use quantette::{Pipeline, QuantizeMethod}; let pipeline = Pipeline::new() .palette_size(32u16.try_into()?) .ditherer(None) .quantize_method(QuantizeMethod::kmeans()) .parallel(true); ``` ``` -------------------------------- ### Pipeline Configuration Example Source: https://docs.rs/quantette/0.6.0/quantette/struct.Pipeline.html Demonstrates how to configure a Pipeline instance by setting various options like palette size, ditherer, quantization method, and parallelism. Ensure necessary cargo features are enabled for some options. ```rust use quantette::{Pipeline, QuantizeMethod}; let pipeline = Pipeline::new() .palette_size(32u16.try_into()?) .ditherer(None) .quantize_method(QuantizeMethod::kmeans()) .parallel(true); ``` -------------------------------- ### Iterating over a slice Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Use `iter()` to get an immutable iterator over the elements of a slice. The iterator yields items from start to end. ```Rust let x = &[1, 2, 4]; let mut iterator = x.iter(); assert_eq!(iterator.next(), Some(&1)); assert_eq!(iterator.next(), Some(&2)); assert_eq!(iterator.next(), Some(&4)); assert_eq!(iterator.next(), None); ``` -------------------------------- ### Configure Pipeline Options Source: https://docs.rs/quantette/0.6.0/quantette/struct.Pipeline.html?search=std%3A%3Avec Example of setting up a Pipeline with specific quantization options like palette size, ditherer, quantization method, and parallel processing. Note that some options require specific cargo features. ```rust use quantette::{Pipeline, QuantizeMethod}; let pipeline = Pipeline::new() .palette_size(32u16.try_into()) .ditherer(None) .quantize_method(QuantizeMethod::kmeans()) .parallel(true); ``` -------------------------------- ### Mutably iterating over a slice Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Use `iter_mut()` to get a mutable iterator that allows modifying each value. The iterator yields items from start to end. ```Rust let x = &mut [1, 2, 4]; for elem in x.iter_mut() { *elem += 2; } assert_eq!(x, &[3, 4, 6]); ``` -------------------------------- ### Reverse split subslices by predicate Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Use `rsplit` to get an iterator over subslices separated by elements that match a predicate, starting from the end of the slice. The matched element is not included. ```rust let slice = [11, 22, 33, 0, 44, 55]; let mut iter = slice.rsplit(|num| *num == 0); assert_eq!(iter.next().unwrap(), &[44, 55]); assert_eq!(iter.next().unwrap(), &[11, 22, 33]); assert_eq!(iter.next(), None); ``` ```rust let v = &[0, 1, 1, 2, 3, 5, 8]; let mut it = v.rsplit(|n| *n % 2 == 0); assert_eq!(it.next().unwrap(), &[]); assert_eq!(it.next().unwrap(), &[3, 5]); assert_eq!(it.next().unwrap(), &[1, 1]); assert_eq!(it.next().unwrap(), &[]); assert_eq!(it.next(), None); ``` -------------------------------- ### Full Image WuU8x3 Quantization Example Source: https://docs.rs/quantette/0.6.0/quantette/wu/struct.WuU8x3.html?search= Shows how to quantize an entire image using WuU8x3, including creating an ImageBuf, running the quantization, and mapping the original image to the new color map. Requires 'wu' and 'ImageBuf' imports. ```rust use quantette::{ wu::{BinnerU8x3, WuU8x3}, ImageBuf, PaletteSize, }; use palette::Srgb; let image = ImageBuf::new(1, 1, vec![Srgb::new(0, 0, 0)])?; let binner = BinnerU8x3::rgb(); let color_map = WuU8x3::run_image(image.as_ref(), binner).unwrap().color_map(PaletteSize::MAX); let quantized = image.map_to_image(&color_map); assert_eq!(image.dimensions(), quantized.dimensions()); ``` -------------------------------- ### Minimal WuF32x3 Example Source: https://docs.rs/quantette/0.6.0/quantette/wu/struct.WuF32x3.html Demonstrates basic usage of WuF32x3 for quantizing a small slice of colors. Ensure 'Oklab' and 'BinnerF32x3' are correctly imported and initialized. ```rust use quantette::{PaletteSize, wu::{WuF32x3, BinnerF32x3}}; use palette::Oklab; let input = vec![Oklab::new(0.0, 0.0, 0.0)]; let binner = BinnerF32x3::oklab_from_srgb8(); let palette = WuF32x3::run_slice(&input, binner).unwrap().palette(PaletteSize::MAX); assert_eq!(palette.len(), input.len()); ``` -------------------------------- ### Reverse split mutable subslices by predicate Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Use `rsplit_mut` to get an iterator over mutable subslices separated by elements that match a predicate, starting from the end of the slice. The matched element is not included. ```rust let mut v = [100, 400, 300, 200, 600, 500]; let mut count = 0; for group in v.rsplit_mut(|num| *num % 3 == 0) { count += 1; group[0] = count; } assert_eq!(v, [3, 400, 300, 2, 600, 1]); ``` -------------------------------- ### Get Element Offset by Pointer Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Find the index of an element reference within a slice using pointer arithmetic. Returns `None` if the element reference does not point to the start of an element. Panics if `T` is zero-sized. ```rust let nums: &[u32] = &[1, 7, 1, 1]; let num = &nums[2]; assert_eq!(num, &1); assert_eq!(nums.element_offset(num), Some(2)); ``` ```rust let arr: &[[u32; 2]] = &[[0, 1], [2, 3]]; let flat_arr: &[u32] = arr.as_flattened(); let ok_elm: &[u32; 2] = flat_arr[0..2].try_into().unwrap(); let weird_elm: &[u32; 2] = flat_arr[1..3].try_into().unwrap(); assert_eq!(ok_elm, &[0, 1]); assert_eq!(weird_elm, &[1, 2]); assert_eq!(arr.element_offset(ok_elm), Some(0)); // Points to element 0 assert_eq!(arr.element_offset(weird_elm), None); // Points between element 0 and 1 ``` -------------------------------- ### Pipeline::new Source: https://docs.rs/quantette/0.6.0/quantette/struct.Pipeline.html Creates a new `Pipeline` instance with default configuration options. ```APIDOC ## Pipeline::new ### Description Creates a new `Pipeline` with default options. ### Method `new()` ### Returns - `Self`: A new `Pipeline` instance. ``` -------------------------------- ### Get element index using pointer arithmetic Source: https://docs.rs/quantette/0.6.0/quantette/struct.PaletteBuf.html?search=u32+-%3E+bool Returns the index of an element reference within the slice using pointer arithmetic. Returns `None` if the element reference does not point to the start of an element. Panics if `T` is zero-sized. ```rust let nums: &[u32] = &[1, 7, 1, 1]; let num = &nums[2]; assert_eq!(num, &1); assert_eq!(nums.element_offset(num), Some(2)); ``` -------------------------------- ### KmeansOptions Builder Example Source: https://docs.rs/quantette/0.6.0/quantette/kmeans/struct.KmeansOptions.html Demonstrates how to configure KmeansOptions using its builder API. Set sampling factor, maximum samples, batch size, and seed for custom quantization. ```rust KmeansOptions::new() .sampling_factor(0.5) .max_samples(512 * 512 / 2) .batch_size(2048) .seed(42); ``` -------------------------------- ### get Source: https://docs.rs/quantette/0.6.0/quantette/struct.PaletteBuf.html?search= Safely gets a reference to an element or subslice by index. Returns None if the index is out of bounds. ```APIDOC ## pub fn get(&self, index: I) -> Option<&>::Output> ### Description Returns a reference to an element or subslice depending on the type of index. If given a position, returns a reference to the element at that position or `None` if out of bounds. If given a range, returns the subslice corresponding to that range, or `None` if out of bounds. ### Method `get` ### Parameters #### Generic Parameters - `I`: The type of the index, which must implement `SliceIndex<[T]>`. #### Self - `&self`: An immutable reference to the slice. #### Parameters - `index`: The index or range to access. ### Return Value - `Option<&>::Output>`: An Option containing a reference to the element or subslice, or `None` if the index is out of bounds. ``` -------------------------------- ### Full Image Quantization with WuF32x3 Source: https://docs.rs/quantette/0.6.0/quantette/wu/struct.WuF32x3.html Illustrates quantizing an entire image using WuF32x3. This example requires 'ImageBuf' and 'Oklab' types, and demonstrates generating a color map and re-quantizing the image. ```rust use quantette::{ wu::{BinnerF32x3, WuF32x3}, ImageBuf, PaletteSize, }; use palette::Oklab; let image = ImageBuf::new(1, 1, vec![Oklab::new(0.0, 0.0, 0.0)])?; let binner = BinnerF32x3::oklab_from_srgb8(); let color_map = WuF32x3::run_image(image.as_ref(), binner).unwrap().color_map(PaletteSize::MAX); let quantized = image.map_to_image(&color_map); assert_eq!(image.dimensions(), quantized.dimensions()); ``` -------------------------------- ### Get Element or Subslice by Index Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Use `get` to safely retrieve a reference to an element or subslice. Returns `None` if the index is out of bounds. ```rust let v = [10, 40, 30]; assert_eq!(Some(&40), v.get(1)); assert_eq!(Some(&[10, 40][..]), v.get(0..2)); assert_eq!(None, v.get(3)); assert_eq!(None, v.get(0..4)); ``` -------------------------------- ### Example: Map Palette from SRGB to Oklab Source: https://docs.rs/quantette/0.6.0/quantette/struct.IndexedImageCounts.html?search= Demonstrates mapping an IndexedImageCounts from SRGB to Oklab color space using a batch function. This is recommended for efficiency. ```rust use quantette::color_space::srgb8_to_oklab; let srgb_counts = IndexedImageCounts::>::default(); let oklab_counts = srgb_counts.map(|palette| srgb8_to_oklab(&palette)); ``` -------------------------------- ### Get First Element of Slice Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Use `first()` to get an `Option` containing a reference to the first element. Returns `None` if the slice is empty. ```rust let v = [10, 40, 30]; assert_eq!(Some(&10), v.first()); let w: &[i32] = &[]; assert_eq!(None, w.first()); ``` -------------------------------- ### Examples of ImageMut Usage Source: https://docs.rs/quantette/0.6.0/quantette/type.ImageMut.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Demonstrates how to directly create an ImageMut from a slice and how to convert a mutable reference from the 'image' crate to an ImageMut. ```APIDOC ## Examples Directly creating an `ImageMut` from a slice: ```rust let (width, height) = (512, 512); let mut pixels = vec![Srgb::new(0, 0, 0); (width * height) as usize]; let image = ImageMut::new(width, height, &mut pixels).unwrap(); ``` Converting a mutable reference to a `RgbImage` from the `image` crate to an `ImageMut`: ```rust let mut image = RgbImage::new(256, 256); let image = ImageMut::try_from(&mut image)?; ``` ``` -------------------------------- ### Creating an ImageRef Directly Source: https://docs.rs/quantette/0.6.0/quantette/type.ImageRef.html Example demonstrating how to directly create an ImageRef instance from width, height, and a pixel slice. ```APIDOC let (width, height) = (512, 512); let pixels = vec![Srgb::new(0, 0, 0); (width * height) as usize]; let image = ImageRef::new(width, height, &pixels).unwrap(); ``` -------------------------------- ### Get Last Chunk of Slice Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Use `last_chunk` to get a reference to the last N items of a slice. Returns `None` if the slice is shorter than N. ```rust let u = [10, 40, 30]; assert_eq!(Some(&[40, 30]), u.last_chunk::<2>()); let v: &[i32] = &[10]; assert_eq!(None, v.last_chunk::<2>()); let w: &[i32] = &[]; assert_eq!(Some(&[]), w.last_chunk::<0>()); ``` -------------------------------- ### Get Slice as Fixed-Size Array (as_array) Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html?search=u32+-%3E+bool Attempt to get a reference to the underlying array if its size `N` matches the slice length. Returns `None` otherwise. ```rust let data = [1, 2, 3]; let slice = &data[..]; let arr_ref: Option<&[i32; 3]> = slice.as_array(); assert!(arr_ref.is_some()); let short_slice = &data[..2]; let arr_ref_short: Option<&[i32; 3]> = short_slice.as_array(); assert!(arr_ref_short.is_none()); ``` -------------------------------- ### Create a new Pipeline Source: https://docs.rs/quantette/0.6.0/quantette/struct.Pipeline.html Instantiate a new Pipeline with default settings. ```rust let pipeline = Pipeline::new(); ``` -------------------------------- ### Minimal WuU8x3 Quantization Example Source: https://docs.rs/quantette/0.6.0/quantette/wu/struct.WuU8x3.html Demonstrates the basic usage of WuU8x3 for quantizing a small set of colors from a slice. Ensure the 'wu' module and 'palette' crate are imported. ```rust use quantette::{PaletteSize, wu::{WuU8x3, BinnerU8x3}}; use palette::Srgb; let input = vec![Srgb::new(0, 0, 0)]; let binner = BinnerU8x3::rgb(); let palette = WuU8x3::run_slice(&input, binner).unwrap().palette(PaletteSize::MAX); assert_eq!(palette.len(), input.len()); ``` -------------------------------- ### Get Slice as Array Reference Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Attempts to get a reference to the underlying array if the requested size `N` matches the slice length. Returns `None` otherwise. ```rust let slice: &[i32] = &[1, 2, 3]; let array_ref: Option<&[i32; 3]> = slice.as_array(); ``` -------------------------------- ### Get Mutable Slice as Fixed-Size Array (as_mut_array) Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html?search=u32+-%3E+bool Attempt to get a mutable reference to the underlying array if its size `N` matches the slice length. Returns `None` otherwise. ```rust let mut data = [1, 2, 3]; let slice = &mut data[..]; let arr_mut_ref: Option<&mut [i32; 3]> = slice.as_mut_array(); assert!(arr_mut_ref.is_some()); let mut short_data = [1, 2]; let short_slice = &mut short_data[..]; let arr_mut_ref_short: Option<&mut [i32; 3]> = short_slice.as_mut_array(); assert!(arr_mut_ref_short.is_none()); ``` -------------------------------- ### Creating and Converting PaletteSize Source: https://docs.rs/quantette/0.6.0/quantette/struct.PaletteSize.html?search= Demonstrates creating PaletteSize from various integer types using try_into, TryFrom, and specific conversion functions. Also shows usage of MIN and MAX constants and clamping. ```rust let size: PaletteSize = 64u16.try_into()?; assert_eq!(size, 64u16); assert_eq!(PaletteSize::try_from(16usize)?, 16usize); assert_eq!(PaletteSize::try_from_u16(256), Some(PaletteSize::MAX)); assert_eq!(PaletteSize::try_from_u16(1024), None); assert_eq!(PaletteSize::from_u16_clamped(1024), PaletteSize::MAX); assert_eq!(PaletteSize::from_nz_u8(NonZeroU8::MIN), PaletteSize::MIN); ``` -------------------------------- ### Get Mutable Slice as Array Reference Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Attempts to get a mutable reference to the underlying array if the requested size `N` matches the slice length. Returns `None` otherwise. ```rust let mut slice: &mut [i32] = &mut [1, 2, 3]; let array_mut_ref: Option<&mut [i32; 3]> = slice.as_mut_array(); ``` -------------------------------- ### Applying Pipeline to Image Source: https://docs.rs/quantette/0.6.0/quantette/struct.Pipeline.html Shows how to use a configured Pipeline to quantize an input image and output an sRGB8 image. The input image can be an ImageBuf or a reference to one. ```rust use quantette::{ImageBuf, Pipeline, QuantizeMethod}; // let img = image::open("some image")?.into_rgb8(); let img = image::RgbImage::new(256, 256); let img = ImageBuf::try_from(img)?; let quantized = pipeline .clone() .input_image(img.as_ref()) .output_srgb8_image(); assert_eq!(img.dimensions(), quantized.dimensions()); ``` -------------------------------- ### Full Image WuF32x3 Quantization Example Source: https://docs.rs/quantette/0.6.0/quantette/wu/struct.WuF32x3.html?search=u32+-%3E+bool Illustrates quantizing an entire image using WuF32x3, generating a color map and re-quantizing the image. Requires 'quantette', 'palette', and 'threads' features for parallel processing if applicable. ```rust use quantette::{ wu::{BinnerF32x3, WuF32x3}, ImageBuf, PaletteSize, }; use palette::Oklab; let image = ImageBuf::new(1, 1, vec![Oklab::new(0.0, 0.0, 0.0)])?; let binner = BinnerF32x3::oklab_from_srgb8(); let color_map = WuF32x3::run_image(image.as_ref(), binner).unwrap().color_map(PaletteSize::MAX); let quantized = image.map_to_image(&color_map); assert_eq!(image.dimensions(), quantized.dimensions()); ``` -------------------------------- ### Safe Get Disjoint Mut References Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Safely get mutable references to multiple disjoint indices at once. Returns an error if indices are out-of-bounds or overlapping. This method performs an O(n^2) check for overlaps. ```rust let v = &mut [1, 2, 3]; if let Ok([a, b]) = v.get_disjoint_mut([0, 2]) { *a = 413; *b = 612; } assert_eq!(v, &[413, 2, 612]); ``` ```rust let v = &mut [1, 2, 3]; if let Ok([a, b]) = v.get_disjoint_mut([0..1, 1..3]) { a[0] = 8; b[0] = 88; b[1] = 888; } assert_eq!(v, &[8, 88, 888]); ``` ```rust let v = &mut [1, 2, 3]; if let Ok([a, b]) = v.get_disjoint_mut([1..=2, 0..=0]) { a[0] = 11; a[1] = 111; b[0] = 1; } assert_eq!(v, &[1, 11, 111]); ``` -------------------------------- ### Get Mutable Slice as Mutable Array Reference Source: https://docs.rs/quantette/0.6.0/quantette/struct.PaletteBuf.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Attempts to get a mutable reference to the slice's underlying array. Returns `None` if the requested array size `N` does not match the slice length. ```rust let mut slice = &mut [1, 2, 3]; let array_mut_ref: Option<&mut [i32; 3]> = slice.as_mut_array(); let none_mut_ref: Option<&mut [i32; 2]> = slice.as_mut_array(); assert!(array_mut_ref.is_some()); assert!(none_mut_ref.is_none()); ``` -------------------------------- ### Minimal Kmeans Quantization Example Source: https://docs.rs/quantette/0.6.0/quantette/kmeans/struct.Kmeans.html?search=u32+-%3E+bool Demonstrates basic k-means quantization using a small input slice and a predefined centroid. Ensures the output palette length matches the initial centroid count. ```rust use quantette::{PaletteBuf, kmeans::{Kmeans, KmeansOptions}}; use palette::Srgb; let input = vec![Srgb::::new(0, 0, 0)]; let centroids = PaletteBuf::new(vec![Srgb::new(0, 0, 0)])?; let palette = Kmeans::run_slice(&input, centroids.clone(), KmeansOptions::new())?.into_palette(); assert_eq!(palette.len(), centroids.len()); ``` -------------------------------- ### Unsafe Get Disjoint Unchecked Mut References Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Use this unsafe method to get mutable references to multiple disjoint indices at once without any checks. Calling with overlapping or out-of-bounds indices is undefined behavior. ```rust let x = &mut [1, 2, 4]; unsafe { let [a, b] = x.get_disjoint_unchecked_mut([0, 2]); *a *= 10; *b *= 100; } assert_eq!(x, &[10, 2, 400]); ``` ```rust let x = &mut [1, 2, 4]; unsafe { let [a, b] = x.get_disjoint_unchecked_mut([0..1, 1..3]); a[0] = 8; b[0] = 88; b[1] = 888; } assert_eq!(x, &[8, 88, 888]); ``` ```rust let x = &mut [1, 2, 4]; unsafe { let [a, b] = x.get_disjoint_unchecked_mut([1..=2, 0..=0]); a[0] = 11; a[1] = 111; b[0] = 1; } assert_eq!(x, &[1, 11, 111]); ``` -------------------------------- ### output_oklab_palette_and_image Source: https://docs.rs/quantette/0.6.0/quantette/struct.PipelineWithImageRefInput.html Runs the pipeline and returns the PaletteBuf and quantized ImageBuf. Returns None if the input image was empty. ```APIDOC ## output_oklab_palette_and_image ### Description Runs the pipeline and returns the `PaletteBuf` and quantized `ImageBuf`. Returns `None` if the input image was empty. ### Method `output_oklab_palette_and_image(self) -> Option<(PaletteBuf, ImageBuf)>` ``` -------------------------------- ### From Implementations Source: https://docs.rs/quantette/0.6.0/quantette/struct.PaletteSize.html?search=std%3A%3Avec Allows conversion from other types to PaletteSize and vice versa. ```APIDOC ## fn from(value: NonZeroU8) -> Self ### Description Converts to this type from the input type `NonZeroU8`. ### Method from ### Parameters - `value`: A `NonZeroU8` value. ### Returns A PaletteSize instance created from the input value. ``` ```APIDOC ## fn from(size: PaletteSize) -> Self ### Description Converts to `NonZeroU16` from `PaletteSize`. ### Method from ### Parameters - `size`: A PaletteSize value. ### Returns A `NonZeroU16` instance created from the input size. ``` ```APIDOC ## fn from(size: PaletteSize) -> Self ### Description Converts to `u16` from `PaletteSize`. ### Method from ### Parameters - `size`: A PaletteSize value. ### Returns A `u16` instance created from the input size. ``` ```APIDOC ## fn from(size: PaletteSize) -> Self ### Description Converts to `usize` from `PaletteSize`. ### Method from ### Parameters - `size`: A PaletteSize value. ### Returns A `usize` instance created from the input size. ``` -------------------------------- ### Get Mutable First Fixed-Size Chunk Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Use `first_chunk_mut::()` to get a mutable array reference to the first `N` elements. Returns `None` if the slice is too short. Allows modification of the initial chunk. ```rust let x = &mut [0, 1, 2]; if let Some(first) = x.first_chunk_mut::<2>() { first[0] = 5; first[1] = 4; } assert_eq!(x, &[5, 4, 2]); assert_eq!(None, x.first_chunk_mut::<4>()); ``` -------------------------------- ### type_id Source: https://docs.rs/quantette/0.6.0/quantette/struct.PaletteCounts.html?search=u32+-%3E+bool Gets the `TypeId` of the color. ```APIDOC ## type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Parameters - **&self**: A reference to the color. ### Returns - **TypeId**: The type identifier of the color. ``` -------------------------------- ### type_id Source: https://docs.rs/quantette/0.6.0/quantette/kmeans/struct.Kmeans.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Gets the `TypeId` of the object. ```APIDOC ## type_id ### Description Gets the `TypeId` of `self`. ### Method Signature `fn type_id(&self) -> TypeId` ### Returns - `TypeId`: The unique identifier for the type of `self`. ``` -------------------------------- ### Example: Map Palette Color by Color Source: https://docs.rs/quantette/0.6.0/quantette/struct.IndexedImageCounts.html?search= Shows how to map each color in the palette individually using `into_iter`, `map`, and `collect`. This is an alternative to batch mapping. ```rust let srgb_counts = IndexedImageCounts::>::default(); let lin_srgb_counts: IndexedImageCounts = srgb_counts.map(|palette| palette.into_iter().map(|srgb| srgb.into_linear()).collect()); ``` -------------------------------- ### Create and Use Palette Source: https://docs.rs/quantette/0.6.0/quantette/struct.Palette.html Demonstrates creating a Palette from a vector, accessing its length and size, and retrieving elements by index. The length and size should always be equal. ```rust let mut data = vec![Srgb::new(0, 0, 0)]; let palette = Palette::new(&data)?; assert_eq!(&data, palette); let len = palette.len(); let size = palette.size(); assert_eq!(len, size); let first = palette[0u8]; assert_eq!(first, Srgb::new(0, 0, 0)); ``` -------------------------------- ### Any::type_id Source: https://docs.rs/quantette/0.6.0/quantette/dither/struct.FloydSteinberg.html?search= Gets the `TypeId` of the object. ```APIDOC ## fn type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Signature ```rust fn type_id(&self) -> TypeId ``` ``` -------------------------------- ### type_id Source: https://docs.rs/quantette/0.6.0/quantette/color_map/struct.NearestNeighborParallelColorMap.html Gets the `TypeId` of `self`. ```APIDOC ## type_id ### Description Gets the `TypeId` of `self`. ### Method Signature fn type_id(&self) -> TypeId ``` -------------------------------- ### Converting to ImageRef from RgbImage Source: https://docs.rs/quantette/0.6.0/quantette/type.ImageRef.html Example showing how to convert an RgbImage from the 'image' crate into an ImageRef using try_from. ```APIDOC let image = RgbImage::new(256, 256); let image = ImageRef::try_from(&image)?; ``` -------------------------------- ### type_id Source: https://docs.rs/quantette/0.6.0/quantette/wu/struct.WuF32x3ParallelColorMap.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Gets the `TypeId` of the color type. ```APIDOC ## type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Method (Implicitly part of the `Any` trait implementation) ### Response - **TypeId**: The unique identifier for the type of `self`. ``` -------------------------------- ### Input Image and Output Generation Source: https://docs.rs/quantette/0.6.0/quantette/struct.Pipeline.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Shows how to provide an input image and generate different types of quantized output, such as an image or a color palette. ```APIDOC ## Pipeline::input_image(image: &dyn Image) -> &Self ### Description Sets the input image for the quantization process. ### Method Builder method ### Parameters - **image** (&dyn Image) - A reference to the input image data. ### Returns A reference to the `Pipeline` instance. ## Pipeline::output_srgb8_image() -> ImageBuf ### Description Quantizes the input image and returns the result as an SRGB8 image buffer. ### Method Operation method ### Parameters None ### Returns An `ImageBuf` containing the quantized SRGB8 image. ## Pipeline::output_srgb8_palette() -> Option ### Description Quantizes the input image and returns the resulting color palette if available. ### Method Operation method ### Parameters None ### Returns An `Option` containing the extracted color palette, or `None` if no palette could be generated. ### Example ```rust use quantette::{ImageBuf, Pipeline, QuantizeMethod}; // Assume 'pipeline' is already configured and 'img' is an ImageBuf // let img = image::open("some image")?.into_rgb8(); let img = image::RgbImage::new(256, 256); let img = ImageBuf::try_from(img)?; let quantized = pipeline .clone() .input_image(img.as_ref()) .output_srgb8_image(); assert_eq!(img.dimensions(), quantized.dimensions()); let palette = pipeline .input_image(img.as_ref()) .output_srgb8_palette() .map(|palette| palette.into_vec()) .unwrap_or_default(); assert_eq!(palette.len(), 1); ``` ``` -------------------------------- ### type_id Source: https://docs.rs/quantette/0.6.0/quantette/wu/struct.BinnerU8x3.html?search=std%3A%3Avec Gets the `TypeId` of the color instance. ```APIDOC ## type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Method (Implicitly defined by trait implementation) ### Response - **TypeId**: The unique identifier for the type of `self`. ``` -------------------------------- ### Output OKLAB Palette and Image Source: https://docs.rs/quantette/0.6.0/quantette/struct.PipelineWithImageRefInput.html?search= Runs the pipeline and returns the PaletteBuf and quantized ImageBuf. Returns None if the input image was empty. ```rust pub fn output_oklab_palette_and_image( self, ) -> Option<(PaletteBuf, ImageBuf)> ``` -------------------------------- ### type_id Source: https://docs.rs/quantette/0.6.0/quantette/enum.QuantizeMethod.html Gets the TypeId of the current instance. ```APIDOC ## type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ``` -------------------------------- ### Full Image Quantization with Wu Centroids Source: https://docs.rs/quantette/0.6.0/quantette/kmeans/struct.Kmeans.html Illustrates a full image quantization workflow. It uses Wu's method to generate initial centroids from an image and then applies Kmeans::run_image for quantization, mapping the result back to an image. ```rust use quantette::{ kmeans::{Kmeans, KmeansOptions}, wu::{BinnerF32x3, WuF32x3}, ImageBuf, PaletteBuf, PaletteSize, }; use palette::Oklab; let image = ImageBuf::new(1, 1, vec![Oklab::new(0.0, 0.0, 0.0)])?; let binner = BinnerF32x3::oklab_from_srgb8(); let centroids = WuF32x3::run_image(image.as_ref(), binner).unwrap().palette(PaletteSize::MAX); let color_map = Kmeans::run_image(image.as_ref(), centroids, KmeansOptions::new()).into_color_map(); let quantized = image.map_to_image(&color_map); assert_eq!(image.dimensions(), quantized.dimensions()); ``` -------------------------------- ### type_id Source: https://docs.rs/quantette/0.6.0/quantette/enum.PaletteInColorSpace.html?search= Gets the `TypeId` of the color space. ```APIDOC ## type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) - **TypeId** (type) - The unique identifier for the type. ### Response Example None ``` -------------------------------- ### Any Source: https://docs.rs/quantette/0.6.0/quantette/enum.CreatePaletteCountsErrorReason.html?search=u32+-%3E+bool Provides a way to get the `TypeId` of a type. ```APIDOC ## type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Method `type_id` ### Returns - `TypeId`: The type identifier. ``` -------------------------------- ### type_id Source: https://docs.rs/quantette/0.6.0/quantette/enum.CreatePaletteCountsErrorReason.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Gets the `TypeId` of the current type. ```APIDOC ## type_id ### Description Gets the `TypeId` of `self`. ### Method `type_id` ### Returns A `TypeId` representing the type of `self`. ``` -------------------------------- ### output_srgb8_palette_and_image Source: https://docs.rs/quantette/0.6.0/quantette/struct.PipelineWithImageRefInput.html Runs the pipeline and returns the PaletteBuf> and quantized ImageBuf>. Returns None if the input image was empty. ```APIDOC ## output_srgb8_palette_and_image ### Description Runs the pipeline and returns the `PaletteBuf>` and quantized `ImageBuf>`. Returns `None` if the input image was empty. ### Method `output_srgb8_palette_and_image(self) -> Option<(PaletteBuf>, ImageBuf>)>` ``` -------------------------------- ### Minimal Kmeans Example Source: https://docs.rs/quantette/0.6.0/quantette/kmeans/struct.Kmeans.html Demonstrates the basic usage of Kmeans::run_slice with a single color input and a custom centroid. Ensures the resulting palette length matches the initial centroid count. ```rust use quantette::{PaletteBuf, kmeans::{Kmeans, KmeansOptions}}; use palette::Srgb; let input = vec![Srgb::::new(0, 0, 0)]; let centroids = PaletteBuf::new(vec![Srgb::new(0, 0, 0)])?; let palette = Kmeans::run_slice(&input, centroids.clone(), KmeansOptions::new())?.into_palette(); assert_eq!(palette.len(), centroids.len()); ``` -------------------------------- ### Any for T Source: https://docs.rs/quantette/0.6.0/quantette/color_map/struct.PaletteSubstitution.html?search= Provides a method to get the `TypeId` of a type. ```APIDOC ## type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Method `type_id` ### Response - `TypeId`: The unique identifier for the type. ``` -------------------------------- ### Any Source: https://docs.rs/quantette/0.6.0/quantette/color_map/struct.PaletteSubstitution.html?search=std%3A%3Avec Provides a method to get the `TypeId` of a color. ```APIDOC ## type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Method `type_id` ``` -------------------------------- ### KmeansOptions Builder API Source: https://docs.rs/quantette/0.6.0/quantette/kmeans/struct.KmeansOptions.html Demonstrates how to use the builder pattern to create and configure KmeansOptions. ```APIDOC ## KmeansOptions ### Description The various options for k-means quantization. This struct has a builder API. See the docs for each of the following functions for more details: * `sampling_factor` * `max_samples` * `batch_size` * `seed` ### `new()` Create a new `KmeansOptions` with default options. ### `sampling_factor(sampling_factor: f32)` Sets the proportion of the input to sample. This is typically in the range `0.0..=1.0`. It can be above `1.0`, but this may not give noticeably better results. You can also set this to `f32::INFINITY` to instead use `max_samples` to limit the number of samples independent of the input size. The default sampling factor is `1.0`. ### `max_samples(max_samples: u32)` Sets the maximum number of pixels to sample from the input. The number of samples determined by the `sampling_factor` is proportional to the input size. However, samples after a certain point will likely not affect the results in a significant way, since the k-means quantization incorporates a learning rate that increasingly diminishes the impact of new samples. So, you can use this option to limit the number samples in the case of a large input. Also, you can set `sampling_factor` to `f32::INFINITY`, which means that this setting will determine the exact number of samples that will occur. The default maximum samples is `262144`. ### `batch_size(batch_size: u32)` Sets the number of samples to batch together each iteration. This option is only used by the parallel versions of the k-means quantization functions. Increasing the batch size reduces the running time but with dimishing returns. Smaller batch sizes are more accurate but are slower to run. The default batch size is `4096`. ### `seed(seed: u64)` Sets the seed number used for the random number generators. The default seed is `0`. ### Examples ```rust KmeansOptions::new() .sampling_factor(0.5) .max_samples(512 * 512 / 2) .batch_size(2048) .seed(42); ``` ``` -------------------------------- ### PaletteSize Conversion from Integer Types Source: https://docs.rs/quantette/0.6.0/quantette/struct.PaletteSize.html Demonstrates how to convert various integer types into a PaletteSize instance using `try_from`. ```APIDOC ## PaletteSize Conversion ### Description Converts integer types to `PaletteSize` using `try_from`. ### Conversions #### `TryFrom>` - **Type**: `Result` - **Function**: `try_from(value: NonZeroU16)` - **Description**: Attempts to convert a `NonZeroU16` into a `PaletteSize`. #### `TryFrom` - **Type**: `Result` - **Function**: `try_from(value: u16)` - **Description**: Attempts to convert a `u16` into a `PaletteSize`. #### `TryFrom` - **Type**: `Result` - **Function**: `try_from(value: u8)` - **Description**: Attempts to convert a `u8` into a `PaletteSize`. #### `TryFrom` - **Type**: `Result` - **Function**: `try_from(value: usize)` - **Description**: Attempts to convert a `usize` into a `PaletteSize`. ### Error Type - **Error**: `PaletteSizeFromIntError` - **Description**: The type returned in the event of a conversion error. ``` -------------------------------- ### type_id Source: https://docs.rs/quantette/0.6.0/quantette/color_map/struct.NearestNeighborParallelColorMap.html?search= Gets the `TypeId` of the color map. ```APIDOC ## type_id ### Description Gets the `TypeId` of `self`. ### Method `type_id` ### Return Value The `TypeId` of the color map. ``` -------------------------------- ### Get IndexedImage height Source: https://docs.rs/quantette/0.6.0/quantette/struct.IndexedImage.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Retrieves the height of the IndexedImage. ```rust let height = image.height(); ``` -------------------------------- ### Any Source: https://docs.rs/quantette/0.6.0/quantette/kmeans/struct.KmeansOptions.html Provides runtime type information. ```APIDOC ## fn type_id(&self) -> TypeId Gets the `TypeId` of `self`. ``` -------------------------------- ### KmeansOptions::new Source: https://docs.rs/quantette/0.6.0/quantette/kmeans/struct.KmeansOptions.html?search=u32+-%3E+bool Creates a new KmeansOptions instance with default values. ```APIDOC ## KmeansOptions::new ### Description Creates a new `KmeansOptions` with default options. ### Returns A new `KmeansOptions` instance. ``` -------------------------------- ### Get IndexedImage width Source: https://docs.rs/quantette/0.6.0/quantette/struct.IndexedImage.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Retrieves the width of the IndexedImage. ```rust let width = image.width(); ```