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

Function sort_palette

src/quant.rs:331–362  ·  view source on GitHub ↗
(attr: &Attributes, palette: &mut PalF)

Source from the content-addressed store, hash-verified

329}
330
331fn sort_palette(attr: &Attributes, palette: &mut PalF) {
332 let last_index_transparent = attr.last_index_transparent;
333
334 let mut tmp: ArrayVec<_, { MAX_COLORS }> = palette.iter_mut().map(|(c, p)| (*c, *p)).collect();
335 tmp.sort_by_key(|(color, pop)| {
336 let is_transparent = color.a <= MAX_TRANSP_A;
337 (is_transparent == last_index_transparent, Reverse(OrdFloat::new(pop.popularity())))
338 });
339 palette.iter_mut().zip(tmp).for_each(|((dcol, dpop), (scol, spop))| {
340 *dcol = scol;
341 *dpop = spop;
342 });
343
344 if last_index_transparent {
345 let alpha_index = palette.as_slice().iter().enumerate()
346 .filter(|(_, c)| c.a <= MAX_TRANSP_A)
347 .min_by_key(|(_, c)| OrdFloat::new(c.a))
348 .map(|(i, _)| i);
349 if let Some(alpha_index) = alpha_index {
350 let last_index = palette.as_slice().len() - 1;
351 palette.swap(last_index, alpha_index);
352 }
353 } else {
354 let num_transparent = palette.as_slice().iter().enumerate()
355 .filter(|(_, c)| c.a <= MAX_TRANSP_A)
356 .map(|(i, _)| i + 1) // num entries, not index
357 .max();
358 if let Some(num_transparent) = num_transparent {
359 attr.verbose_print(format!(" eliminated opaque tRNS-chunk entries...{} entr{} transparent", num_transparent, if num_transparent == 1 { "y" } else { "ies" }));
360 }
361 }
362}
363
364impl fmt::Debug for QuantizationResult {
365 #[cold]

Callers 1

newMethod · 0.85

Calls 7

newFunction · 0.85
iter_mutMethod · 0.80
popularityMethod · 0.80
swapMethod · 0.80
verbose_printMethod · 0.80
as_sliceMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected