Skip to content

Commit

Permalink
Most popular edge color first
Browse files Browse the repository at this point in the history
  • Loading branch information
andrews05 committed Jun 1, 2023
1 parent 7f25122 commit f7ee78d
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/reduction/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,16 @@ pub fn sorted_palette_battiato(png: &PngImage) -> Option<PngImage> {
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
Expand Down Expand Up @@ -222,6 +227,16 @@ 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];
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<Vec<usize>> {
let mut matrix = vec![vec![0; num_colors]; num_colors];
Expand Down

0 comments on commit f7ee78d

Please sign in to comment.