use anyhow::Result;
use crate::image::Image;
pub fn compute_histogram(image: &Image<u8, 1>, num_bins: usize) -> Result<Vec<usize>> {
if num_bins == 0 || num_bins > 256 {
return Err(anyhow::anyhow!(
"Invalid number of bins. Must be in the range [1, 256]."
));
}
let scale = 256.0 / num_bins as f32;
let mut histogram = vec![0; num_bins];
image.data.fold(&mut histogram, |histogram, &pixel| {
let bin_pos = (pixel as f32 / scale).floor();
histogram[bin_pos as usize] += 1;
histogram
});
Ok(histogram)
}
#[cfg(test)]
mod tests {
use crate::image::{Image, ImageSize};
use anyhow::Result;
#[test]
fn test_compute_histogram() -> Result<()> {
let image = Image::new(
ImageSize {
width: 3,
height: 3,
},
vec![0, 2, 4, 128, 130, 132, 254, 255, 255],
)?;
let histogram = super::compute_histogram(&image, 3)?;
assert_eq!(histogram, vec![3, 3, 3]);
Ok(())
}
}