From f7ee78d6efeb37d0d113fe66e71cc3986218820b Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 1 Jun 2023 14:09:38 +1200 Subject: [PATCH] Most popular edge color first --- src/reduction/palette.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/reduction/palette.rs b/src/reduction/palette.rs index 7d8204ae..f9c1c671 100644 --- a/src/reduction/palette.rs +++ b/src/reduction/palette.rs @@ -189,11 +189,16 @@ pub fn sorted_palette_battiato(png: &PngImage) -> Option { let matrix = co_occurrence_matrix(palette.len(), &png.data); let edges = weighted_edges(&matrix); let mut old_map = battiato_tsp(palette.len(), edges); - // Keep the same first element - // This is safe to do if the palette is full since the delta filters are wrapping operations - if palette.len() == 256 { - let first = old_map.iter().position(|&i| i == 0).unwrap(); - old_map.rotate_left(first); + + // Put the most popular edge color first, which can help slightly if the filter bytes are 0 + let keep_first = most_popular_edge_color(palette.len(), png); + let first_idx = old_map.iter().position(|&i| i == keep_first).unwrap(); + // If the index is past halfway, reverse the order so as to minimize the change + if first_idx >= old_map.len() / 2 { + old_map.reverse(); + old_map.rotate_right(first_idx + 1); + } else { + old_map.rotate_left(first_idx); } // Check if anything changed @@ -222,6 +227,16 @@ pub fn sorted_palette_battiato(png: &PngImage) -> Option { }) } +// 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]; + for line in png.scan_lines(false) { + counts[line.data[0] as usize] += 1; + counts[line.data[line.data.len() - 1] as usize] += 1; + } + counts.iter().enumerate().max_by_key(|(_, &v)| v).unwrap().0 +} + // Calculate co-occurences matrix fn co_occurrence_matrix(num_colors: usize, data: &[u8]) -> Vec> { let mut matrix = vec![vec![0; num_colors]; num_colors];