use once_cell::sync::OnceCell;
use crate::engine::common::Result;
use crate::engine::{Binarizer, Exceptions, LuminanceSource};
use super::BitMatrix;
const LUMINANCE_BITS: usize = 5;
const LUMINANCE_SHIFT: usize = 8 - LUMINANCE_BITS;
const LUMINANCE_BUCKETS: usize = 1 << LUMINANCE_BITS;
pub struct GlobalHistogramBinarizer<LS: LuminanceSource> {
source: LS,
black_matrix: OnceCell<BitMatrix>,
}
impl<LS: LuminanceSource> Binarizer for GlobalHistogramBinarizer<LS> {
type Source = LS;
fn get_luminance_source(&self) -> &Self::Source {
&self.source
}
fn get_black_matrix(&self) -> Result<&BitMatrix> {
self.black_matrix.get_or_try_init(|| Self::build_black_matrix(&self.source))
}
}
impl<LS: LuminanceSource> GlobalHistogramBinarizer<LS> {
pub fn new(source: LS) -> Self {
Self { source, black_matrix: OnceCell::new() }
}
fn build_black_matrix(source: &LS) -> Result<BitMatrix> {
let width = source.get_width();
let height = source.get_height();
let mut matrix = BitMatrix::new(width as u32, height as u32)?;
let mut localBuckets = [0; LUMINANCE_BUCKETS]; for y in 1..5 {
let row = height * y / 5;
let localLuminances =
source.get_row(row).ok_or(Exceptions::index_out_of_bounds_with("row out of bounds"))?;
let right = (width * 4) / 5;
for pixel in &localLuminances[(width / 5)..right] {
localBuckets[(pixel >> LUMINANCE_SHIFT) as usize] += 1;
}
}
let blackPoint = Self::estimateBlackPoint(&localBuckets)?;
let localLuminances = source.get_matrix();
for y in 0..height {
let offset = y * width;
for x in 0..width {
let pixel = localLuminances[offset + x];
if (pixel as u32) < blackPoint {
matrix.set(x as u32, y as u32);
}
}
}
Ok(matrix)
}
fn estimateBlackPoint<const BUCKET_COUNT: usize>(buckets: &[u32; BUCKET_COUNT]) -> Result<u32> {
let mut maxBucketCount = 0;
let mut firstPeak = 0;
let mut firstPeakSize = 0;
for (x, &bucket) in buckets.iter().enumerate() {
if bucket > firstPeakSize {
firstPeak = x;
firstPeakSize = bucket;
}
if bucket > maxBucketCount {
maxBucketCount = bucket;
}
}
let mut secondPeak = 0;
let mut secondPeakScore = 0;
for (x, bucket) in buckets.iter().enumerate() {
let distanceToBiggest = (x as i32 - firstPeak as i32).unsigned_abs();
let score = *bucket * distanceToBiggest * distanceToBiggest;
if score > secondPeakScore {
secondPeak = x;
secondPeakScore = score;
}
}
if firstPeak > secondPeak {
std::mem::swap(&mut firstPeak, &mut secondPeak);
}
if secondPeak - firstPeak <= BUCKET_COUNT / 16 {
return Err(Exceptions::not_found_with("secondPeak - firstPeak <= numBuckets / 16 "));
}
let mut bestValley = secondPeak as isize - 1;
let mut bestValleyScore = -1;
let mut x = secondPeak as isize;
while x > firstPeak as isize {
let fromFirst = x - firstPeak as isize;
let score =
fromFirst * fromFirst * (secondPeak as isize - x) * (maxBucketCount - buckets[x as usize]) as isize;
if score as i32 > bestValleyScore {
bestValley = x;
bestValleyScore = score as i32;
}
x -= 1;
}
Ok((bestValley as u32) << LUMINANCE_SHIFT)
}
}