diff options
author | Devon Sawatsky <novedevo@gmail.com> | 2021-09-27 21:06:04 -0700 |
---|---|---|
committer | gennyble <gen@nyble.dev> | 2021-09-28 23:05:18 +0000 |
commit | 1b948cafcc32d48413a46540caa51b9e9084c7d1 (patch) | |
tree | 1d72ac0a709fbc2d0db64159ceb8325f24304835 /src | |
parent | 94a6f25f35ffd44cb2028ea431197fe000bc8f7b (diff) | |
download | colorsquash-1b948cafcc32d48413a46540caa51b9e9084c7d1.tar.gz colorsquash-1b948cafcc32d48413a46540caa51b9e9084c7d1.zip |
remove a loop, improve clarity and speed
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/src/main.rs b/src/main.rs index 7f1e1fa..f1b23fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,26 +28,22 @@ fn main() { } // Max complexity is O(n * max_colors) - for color in image.pixels() { - let mut min_difference = f32::MAX; - let mut min_difference_color = *color; - - for index in 0..selected_colors.len() { - let difference = rgb_difference(color, unsafe { selected_colors.get_unchecked(index) }); - /*if difference == 0.0 { - continue 'sorted_colors; - }*/ - if difference < min_difference { - min_difference = difference; - min_difference_color = unsafe { *selected_colors.get_unchecked(index) }; + for color in image.pixels_mut() { + let quantized = color_map.entry(*color).or_insert({ + let mut min_difference = f32::MAX; + let mut min_difference_color = *color; + + for selected_color in &selected_colors { + let difference = rgb_difference(color, selected_color); + if difference < min_difference { + min_difference = difference; + min_difference_color = *selected_color; + } } - } - - color_map.insert(*color, min_difference_color); - } + min_difference_color + }); - for pixel in image.pixels_mut() { - pixel.clone_from(color_map.get(pixel).unwrap()); + *color = *quantized; } image.save(outname).expect("Failed to write out"); |