From 36ccf4d4bf57f12dae13543b37453dc6433018e5 Mon Sep 17 00:00:00 2001 From: gennyble Date: Mon, 10 Jun 2024 17:17:55 -0500 Subject: Use leftover bits in HighestBits selector --- squash/Cargo.toml | 12 ++++++------ src/selection.rs | 21 ++++++++++++++++----- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/squash/Cargo.toml b/squash/Cargo.toml index 3d94c1a..12889f3 100644 --- a/squash/Cargo.toml +++ b/squash/Cargo.toml @@ -7,14 +7,14 @@ license = "ISC" description = "CLI tool for quantizing colours" repository = "https://github.com/gennyble/colorsquash/tree/main/squash" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] # the meat 'o the thing! the meaning behind it all -colorsquash = { path = "..", version = "0.2.0", default-features = false, features = [ - "gifed", -] } +[dependencies.colorsquash] +path = ".." +version = "0.2.0" +default-features = false # `kmeans` crate currently very broken +features = ["gifed"] +[dependencies] # just useful tools for writing binaries anyhow = "1.0.75" camino = "1.1.6" diff --git a/src/selection.rs b/src/selection.rs index 84d5ea3..aee3b2f 100644 --- a/src/selection.rs +++ b/src/selection.rs @@ -337,17 +337,28 @@ pub struct HighestBits {} impl Selector for HighestBits { fn select(&mut self, max_colors: usize, image: ImageData) -> Vec { - let max_bits = max_colors.next_power_of_two().ilog2() / 3; - let shift = 8 - max_bits; + let bits = max_colors.next_power_of_two().ilog2(); + let leftover = bits % 3; + let shift = 8 - (bits / 3); + + //TODO: gen- we're taking red/green here because, as i remember, they + // are the colours to which we are most sensetive? but it would be cool + // if this was selectable + let (rshift, gshift, bshift) = match leftover { + 0 => (shift, shift, shift), + 1 => (shift, shift - 1, shift), + 2 => (shift - 1, shift - 1, shift), + _ => unreachable!(), + }; image .0 .iter() .map(|color| { RGB8::new( - color.r >> shift << shift, - color.g >> shift << shift, - color.b >> shift << shift, + color.r >> rshift << rshift, + color.g >> gshift << gshift, + color.b >> bshift << bshift, ) }) .collect::>() -- cgit 1.4.1-3-g733a5