pub struct PixelSet { /* private fields */ }Expand description
A compact, sorted collection of pixels, optimized for fast set-like operations and spatial queries.
§Overview
PixelSet is a high-performance container that stores Pixel values in a
strictly sorted (y, x) order. This ensures:
- Cache-friendly iteration (pixels are stored linearly in scanline order)
- Fast binary-search membership checks (
O(log n)per lookup) - Efficient merges, intersections, and differences (
O(n)with linear scans) - Minimal memory overhead compared to hash-based or tree-based structures
§Usage
use image::DynamicImage;
use pixelset::{Pixel, PixelSet, Color};
let image = DynamicImage::new_rgb8(1, 1);
let mut set = PixelSet::from_image(&image);§Guarantees
Internally, pixel order is always sorted by (y, x).
Methods preserve this sorting parity with the exception of [new],
which expects a sanitized sorted list.
Highly optimized for set operations, only struggling with additions or removals of individual pixels.
Implementations§
Source§impl PixelSet
impl PixelSet
Sourcepub fn has(&self, pixel: Pixel) -> bool
pub fn has(&self, pixel: Pixel) -> bool
Returns true if the set contains the specified pixel,
performing binary search.
Complexity: O(log n).
Sourcepub fn is_subset(&self, other: &PixelSet) -> bool
pub fn is_subset(&self, other: &PixelSet) -> bool
Returns true if every pixel in this set is also present in other.
Complexity: O(n log m).
Sourcepub fn filter(&self, predicate: impl Fn(Pixel) -> bool) -> Self
pub fn filter(&self, predicate: impl Fn(Pixel) -> bool) -> Self
Returns a new PixelSet containing pixels whose colors in the given image
satisfy a predicate.
Sourcepub fn filter_color(
&self,
image: &DynamicImage,
predicate: impl Fn(Color) -> bool,
) -> Self
pub fn filter_color( &self, image: &DynamicImage, predicate: impl Fn(Color) -> bool, ) -> Self
Returns a new PixelSet containing only pixels whose color in the given image
exactly matches the specified color.
Sourcepub fn select(&self, image: &DynamicImage, query: Color) -> Self
pub fn select(&self, image: &DynamicImage, query: Color) -> Self
Returns a new PixelSet containing only those pixels whose color in
the provided image exactly matches the specified color.
Source§impl PixelSet
impl PixelSet
Sourcepub fn add(&mut self, pixel: Pixel)
pub fn add(&mut self, pixel: Pixel)
Inserts a single new pixel into the set while maintaining sorted order and uniqueness.
Uses binary search to find the insertion point.
Worst-case complexity: O(n) due to element shifting.
Sourcepub fn discard(&mut self, pixel: Pixel)
pub fn discard(&mut self, pixel: Pixel)
Removes a pixel from the set, maintaining sort order.
Uses binary search to locate the pixel.
Worst-case complexity: O(n) due to element shifting.
Sourcepub fn extend(&self, other: &Self) -> Self
pub fn extend(&self, other: &Self) -> Self
Returns a new PixelSet representing the union of this set and another, with all
pixels from both sets are included.
Complexity: O(n + m).
Sourcepub fn without(&self, other: &Self) -> Self
pub fn without(&self, other: &Self) -> Self
Returns a new PixelSet with pixels in this set that are not in other,
performing a set difference.
Complexity: O(n + m).
Sourcepub fn remove(&mut self, other: &Self)
pub fn remove(&mut self, other: &Self)
Removes all pixels that appear in other from this set, modifying the
set in-place.
Complexity: O(n + m).
Sourcepub fn and(&self, other: &Self) -> Self
pub fn and(&self, other: &Self) -> Self
Returns a new PixelSet containing only the pixels that appear in
both sets, performing a set intersection.
Complexity: O(n + m)
Sourcepub fn intersects(&self, other: &Self) -> bool
pub fn intersects(&self, other: &Self) -> bool
Returns true if this set shares any pixel with another set.
Complexity: O(n log m).
Source§impl PixelSet
impl PixelSet
Sourcepub fn new(pixels: Vec<Pixel>) -> Self
pub fn new(pixels: Vec<Pixel>) -> Self
Creates a new PixelSet from an unsorted list of pixels.
The pixels are sorted into (y, x) order, removing all duplicates,
operating in O(n) time with the usage of radix sort.
Sourcepub fn new_unchecked(pixels: Vec<Pixel>) -> Self
pub fn new_unchecked(pixels: Vec<Pixel>) -> Self
Constructs a PixelSet from a vector that is already known to be
sorted in strictly ascending (y, x) order and contains no duplicates.
This constructor performs no validation. Callers must ensure the input satisfies sorting order and lacks duplicates.
Sourcepub fn from_image(image: &DynamicImage) -> Self
pub fn from_image(image: &DynamicImage) -> Self
Creates a PixelSet containing a pixel for every coordinate in the
given image.
Source§impl PixelSet
impl PixelSet
Sourcepub fn recolor<T: Into<Color>>(
&self,
image: &mut DynamicImage,
applier: impl Fn(Pixel) -> Option<T>,
)
pub fn recolor<T: Into<Color>>( &self, image: &mut DynamicImage, applier: impl Fn(Pixel) -> Option<T>, )
Applies a color-producing function to each pixel and writes the resulting color into the image.
The closure may return None to indicate that a pixel’s color should
remain unchanged.
Sourcepub fn fill(&self, image: &mut DynamicImage, color: Color)
pub fn fill(&self, image: &mut DynamicImage, color: Color)
Fills all pixels in the set with a single uniform color.
Sourcepub fn transform<T: Into<Color>>(
&self,
image: &mut DynamicImage,
applier: impl Fn(Color) -> Option<T>,
)
pub fn transform<T: Into<Color>>( &self, image: &mut DynamicImage, applier: impl Fn(Color) -> Option<T>, )
Reads the color of each pixel from the image, applies a transformation closure, and writes back a new color if one is produced.
Sourcepub fn neighbors(&self, image: &DynamicImage) -> Self
pub fn neighbors(&self, image: &DynamicImage) -> Self
Returns a PixelSet representing all 8-connected neighbors of all
pixels in this set, constrained to the image bounds.
Sourcepub fn touching(&self, other: &Self, image: &DynamicImage) -> Self
pub fn touching(&self, other: &Self, image: &DynamicImage) -> Self
Returns the subset of pixels in this set that are adjacent to another set.
A pixel is included in the result if included in self, and adjacent to
a pixel in other.
Sourcepub fn as_colors(&self, image: &DynamicImage) -> impl Iterator<Item = Color>
pub fn as_colors(&self, image: &DynamicImage) -> impl Iterator<Item = Color>
Returns an iterator over the colors of all pixels in this set when viewed in the provided image.
Sourcepub fn mean_color(&self, image: &DynamicImage) -> Option<Color>
pub fn mean_color(&self, image: &DynamicImage) -> Option<Color>
Computes the average color of all pixels in the set when sampled from the given image.
Returns None if the set is empty.
Trait Implementations§
Source§impl<'a> IntoIterator for &'a PixelSet
impl<'a> IntoIterator for &'a PixelSet
Source§impl<'a> IntoIterator for &'a mut PixelSet
impl<'a> IntoIterator for &'a mut PixelSet
Auto Trait Implementations§
impl Freeze for PixelSet
impl RefUnwindSafe for PixelSet
impl Send for PixelSet
impl Sync for PixelSet
impl Unpin for PixelSet
impl UnwindSafe for PixelSet
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more