diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2020-06-24 15:20:02 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2020-06-24 15:44:14 -0400 |
commit | 39c0348c9f98b4dd29bd112a0a2a42faa67c92d4 (patch) | |
tree | 6c8ed80bd8cbbb0af79c9ac57bdb39634fa178fd /src/frontier | |
parent | adaafdd7043507cbceae65e78c38954e47103b5c (diff) | |
download | kd-forest-master.tar.xz |
Diffstat (limited to 'src/frontier')
-rw-r--r-- | src/frontier/image.rs | 11 | ||||
-rw-r--r-- | src/frontier/mean.rs | 19 | ||||
-rw-r--r-- | src/frontier/min.rs | 17 |
3 files changed, 24 insertions, 23 deletions
diff --git a/src/frontier/image.rs b/src/frontier/image.rs index 5642297..18bf620 100644 --- a/src/frontier/image.rs +++ b/src/frontier/image.rs @@ -1,16 +1,17 @@ //! Frontier that targets an image. -use super::{Frontier, Pixel}; +use super::{Frontier, Pixel, Target}; use crate::color::{ColorSpace, Rgb8}; -use crate::metric::soft::SoftKdTree; -use crate::metric::NearestNeighbors; +use crate::soft::SoftKdTree; + +use acap::NearestNeighbors; use image::RgbImage; /// A [Frontier] that places colors on the closest pixel of a target image. #[derive(Debug)] -pub struct ImageFrontier<C: ColorSpace> { +pub struct ImageFrontier<C> { nodes: SoftKdTree<Pixel<C>>, width: u32, height: u32, @@ -58,7 +59,7 @@ impl<C: ColorSpace> Frontier for ImageFrontier<C> { fn place(&mut self, rgb8: Rgb8) -> Option<(u32, u32)> { let color = C::from(rgb8); - if let Some(node) = self.nodes.nearest(&color).map(|n| n.item) { + if let Some(node) = self.nodes.nearest(&Target(color)).map(|n| n.item) { let pos = node.pos; node.delete(); diff --git a/src/frontier/mean.rs b/src/frontier/mean.rs index 6a32b97..3c441b8 100644 --- a/src/frontier/mean.rs +++ b/src/frontier/mean.rs @@ -1,19 +1,19 @@ //! Mean selection frontier. -use super::{neighbors, Frontier, Pixel}; +use super::{neighbors, Frontier, RcPixel, Target}; use crate::color::{ColorSpace, Rgb8}; -use crate::metric::soft::SoftKdForest; -use crate::metric::NearestNeighbors; +use crate::soft::SoftKdForest; + +use acap::NearestNeighbors; use std::iter; -use std::rc::Rc; /// A pixel on a mean frontier. #[derive(Debug)] enum MeanPixel<C> { Empty, - Fillable(Rc<Pixel<C>>), + Fillable(RcPixel<C>), Filled(C), } @@ -27,9 +27,10 @@ impl<C: ColorSpace> MeanPixel<C> { } /// A [Frontier] that looks at the average color of each pixel's neighbors. +#[derive(Debug)] pub struct MeanFrontier<C> { pixels: Vec<MeanPixel<C>>, - forest: SoftKdForest<Rc<Pixel<C>>>, + forest: SoftKdForest<RcPixel<C>>, width: u32, height: u32, len: usize, @@ -45,7 +46,7 @@ impl<C: ColorSpace> MeanFrontier<C> { pixels.push(MeanPixel::Empty); } - let pixel0 = Rc::new(Pixel::new(x0, y0, C::from(Rgb8::from([0, 0, 0])))); + let pixel0 = RcPixel::new(x0, y0, C::from(Rgb8::from([0, 0, 0]))); let i = (x0 + y0 * width) as usize; pixels[i] = MeanPixel::Fillable(pixel0.clone()); @@ -99,7 +100,7 @@ impl<C: ColorSpace> MeanFrontier<C> { .map(MeanPixel::filled_color) .flatten(), ); - let pixel = Rc::new(Pixel::new(x, y, color)); + let pixel = RcPixel::new(x, y, color); self.pixels[i] = MeanPixel::Fillable(pixel.clone()); pixels.push(pixel); } @@ -135,7 +136,7 @@ impl<C: ColorSpace> Frontier for MeanFrontier<C> { fn place(&mut self, rgb8: Rgb8) -> Option<(u32, u32)> { let color = C::from(rgb8); - let (x, y) = self.forest.nearest(&color).map(|n| n.item.pos)?; + let (x, y) = self.forest.nearest(&Target(color)).map(|n| n.item.pos)?; self.fill(x, y, color); diff --git a/src/frontier/min.rs b/src/frontier/min.rs index 269f3b7..95b3321 100644 --- a/src/frontier/min.rs +++ b/src/frontier/min.rs @@ -1,19 +1,18 @@ //! Minimum selection frontier. -use super::{neighbors, Frontier, Pixel}; +use super::{neighbors, Frontier, RcPixel, Target}; use crate::color::{ColorSpace, Rgb8}; -use crate::metric::soft::SoftKdForest; -use crate::metric::NearestNeighbors; +use crate::soft::SoftKdForest; -use rand::Rng; +use acap::NearestNeighbors; -use std::rc::Rc; +use rand::Rng; /// A pixel on a min frontier. #[derive(Debug)] struct MinPixel<C> { - pixel: Option<Rc<Pixel<C>>>, + pixel: Option<RcPixel<C>>, filled: bool, } @@ -31,7 +30,7 @@ impl<C: ColorSpace> MinPixel<C> { pub struct MinFrontier<C, R> { rng: R, pixels: Vec<MinPixel<C>>, - forest: SoftKdForest<Rc<Pixel<C>>>, + forest: SoftKdForest<RcPixel<C>>, width: u32, height: u32, x0: u32, @@ -94,7 +93,7 @@ impl<C: ColorSpace, R: Rng> MinFrontier<C, R> { return None; } - let rc = Rc::new(Pixel::new(x, y, color)); + let rc = RcPixel::new(x, y, color); pixel.pixel = Some(rc.clone()); pixel.filled = true; @@ -144,7 +143,7 @@ impl<C: ColorSpace, R: Rng> Frontier for MinFrontier<C, R> { let color = C::from(rgb8); let (x, y) = self .forest - .nearest(&color) + .nearest(&Target(color)) .map(|n| n.item.pos) .map(|(x, y)| self.free_neighbor(x, y).unwrap()) .unwrap_or((self.x0, self.y0)); |