From 8975286a7c4716892e61fabb0b8ebcb34ff71936 Mon Sep 17 00:00:00 2001 From: Felix Hanau Date: Thu, 18 Jul 2019 17:54:31 +0100 Subject: [PATCH] Implement alpha and luminosity palette sorting --- src/reduction/mod.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/reduction/mod.rs b/src/reduction/mod.rs index 8401b539..c8fd94d9 100644 --- a/src/reduction/mod.rs +++ b/src/reduction/mod.rs @@ -61,9 +61,24 @@ pub fn reduced_palette(png: &PngImage) -> Option { } } + let mut used_enumerated : Vec<(usize, &bool)>= used.iter().enumerate().collect(); + used_enumerated.sort_by(|a, b| { + //Sort by ascending alpha and descending luma. + let color_val = |palette : &Vec>, i| { + let color = palette.get(i).cloned() + .unwrap_or_else(|| RGBA8::new(0, 0, 0, 255)); + let mut result = (color.a as i32) << 18; + result -= (color.r as i32) * 299; + result -= (color.g as i32) * 587; + result -= (color.b as i32) * 114; + result + }; + color_val(palette, a.0).cmp(&color_val(palette, b.0)) + }); + let mut next_index = 0u16; let mut seen = HashMap::with_capacity(palette.len()); - for (i, (used, palette_map)) in used.iter().cloned().zip(palette_map.iter_mut()).enumerate() + for (i, used) in used_enumerated.iter().cloned() { if !used { continue; @@ -75,12 +90,12 @@ pub fn reduced_palette(png: &PngImage) -> Option { .unwrap_or_else(|| RGBA8::new(0, 0, 0, 255)); match seen.entry(color) { Vacant(new) => { - *palette_map = Some(next_index as u8); + palette_map[i] = Some(next_index as u8); new.insert(next_index as u8); next_index += 1; } Occupied(remap_to) => { - *palette_map = Some(*remap_to.get()); + palette_map[i] = Some(*remap_to.get()) } } }