diff --git a/rust/src/main.rs b/rust/src/main.rs index aece64b..16561cb 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -29,12 +29,10 @@ fn main() -> Result<()> { output.put_pixel(x, y, *pixel); } } - quadtree_quant( - &mut output, - args.max_err.unwrap_or(2000.0) * 128.0 * 128.0, - [0, 0], - 128, - ); + let error_scale_factor = 0.2; // Adjusted by feel. Q=0 is unrecognizable but not blank. + let error_per_pixel = (1.0 - args.max_err.unwrap_or(80.0) / 100.0) * error_scale_factor; + let sq_error_per_pixel = error_per_pixel * error_per_pixel; + quadtree_quant(&mut output, sq_error_per_pixel * 128.0 * 128.0, [0, 0], 128); output.save(&args.output)?; println!("Wrote {}", &args.output); Ok(()) @@ -83,10 +81,12 @@ fn quadtree_quant(img: &mut RgbImage, max_err: f32, pos: [u32; 2], size: u32) { fn redmean_sq(x: [u8; 3], y: [u8; 3]) -> f32 { let mut ds: [f32; 3] = [0.0, 0.0, 0.0]; - for i in 0..2 { - ds[i] = x[i] as f32 - y[i] as f32; - ds[i] *= ds[i]; + for (i, d) in ds.iter_mut().enumerate() { + let delta = x[i] as f32 - y[i] as f32; + *d = delta * delta; } let rm = (x[0] as f32 + y[0] as f32) / 2.0; - (2.0 + rm / 256.0) * ds[0] + 4.0 * ds[1] + (2.0 + (255.0 - rm) / 256.0) * ds[2] + let result = (2.0 + rm / 256.0) * ds[0] + 4.0 * ds[1] + (2.0 + (255.0 - rm) / 256.0) * ds[2]; + // normalize value to the range 0.0 - 1.0 + result / 584971.0 }