Faster, garbage-resilient most_popular_edge_color

This commit is contained in:
Kornel 2024-03-26 15:10:26 +00:00 committed by andrews05
parent d063119d9c
commit 36de54318d

View file

@ -183,12 +183,21 @@ pub fn sorted_palette_battiato(png: &PngImage) -> Option<PngImage> {
// Find the most popular color on the image edges (the pixels neighboring the filter bytes)
fn most_popular_edge_color(num_colors: usize, png: &PngImage) -> usize {
let mut counts = vec![0; num_colors];
let mut counts = [0u32; 256];
for line in png.scan_lines(false) {
counts[line.data[0] as usize] += 1;
counts[line.data[line.data.len() - 1] as usize] += 1;
if let &[first, .., last] = line.data {
counts[first as usize] += 1;
counts[last as usize] += 1;
}
}
counts.iter().enumerate().max_by_key(|(_, &v)| v).unwrap().0
counts
.iter()
.copied()
.take(num_colors)
.enumerate()
.max_by_key(|&(_, v)| v)
.unwrap_or_default()
.0
}
// Calculate co-occurences matrix