MCPcopy Create free account
hub / github.com/ImageOptim/libimagequant / new

Method new

src/nearest.rs:6–32  ·  view source on GitHub ↗
(palette: &'pal PalF)

Source from the content-addressed store, hash-verified

4impl<'pal> Nearest<'pal> {
5 #[inline(never)]
6 pub fn new(palette: &'pal PalF) -> Result<Self, Error> {
7 if palette.len() > PalIndex::MAX as usize + 1 {
8 return Err(Error::Unsupported);
9 }
10 let mut indexes: Vec<_> = (0..palette.len())
11 .map(|idx| MapIndex { idx: idx as _ })
12 .collect();
13 if indexes.is_empty() {
14 return Err(Error::Unsupported);
15 }
16 let mut handle = Nearest {
17 root: vp_create_node(&mut indexes, palette),
18 palette,
19 nearest_other_color_dist: [0.; MAX_COLORS],
20 };
21 for (i, color) in palette.as_slice().iter().enumerate() {
22 let mut best = Visitor {
23 idx: 0,
24 distance: f32::MAX,
25 distance_squared: f32::MAX,
26 exclude: Some(i as PalIndex),
27 };
28 vp_search_node(&handle.root, color, &mut best);
29 handle.nearest_other_color_dist[i] = best.distance_squared / 4.;
30 }
31 Ok(handle)
32 }
33}
34
35impl Nearest<'_> {

Callers

nothing calls this directly

Calls 4

vp_create_nodeFunction · 0.85
vp_search_nodeFunction · 0.85
lenMethod · 0.45
as_sliceMethod · 0.45

Tested by

no test coverage detected