use crate::input::LumaImage;
use crate::matrix::sampler::GridSampler;
use crate::report::{Iso15415Report, IsoGrade, IsoParameter, Point, StructuralReport, UecReport};
fn grade_floor(value: f32, floors: [f32; 4]) -> IsoGrade {
if value >= floors[0] {
IsoGrade::A
} else if value >= floors[1] {
IsoGrade::B
} else if value >= floors[2] {
IsoGrade::C
} else if value >= floors[3] {
IsoGrade::D
} else {
IsoGrade::F
}
}
fn grade_ceiling(value: f32, ceilings: [f32; 4]) -> IsoGrade {
if value <= ceilings[0] {
IsoGrade::A
} else if value <= ceilings[1] {
IsoGrade::B
} else if value <= ceilings[2] {
IsoGrade::C
} else if value <= ceilings[3] {
IsoGrade::D
} else {
IsoGrade::F
}
}
fn percentile(sorted: &[u8], p: f32) -> u8 {
debug_assert_ne!(sorted.len(), 0);
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_precision_loss,
reason = "index math over ≤177² module samples, p clamped"
)]
let idx = ((sorted.len() - 1) as f32 * p.clamp(0.0, 1.0)).round() as usize;
sorted[idx]
}
fn contrast_and_modulation(samples: &[u8]) -> Option<(IsoParameter, IsoParameter)> {
if samples.len() < 16 {
return None;
}
let mut sorted = samples.to_vec();
sorted.sort_unstable();
let r_low = f32::from(percentile(&sorted, 0.02));
let r_high = f32::from(percentile(&sorted, 0.98));
let span = r_high - r_low;
let sc_value = span / 255.0;
let symbol_contrast = IsoParameter {
value: sc_value,
grade: grade_floor(sc_value, [0.70, 0.55, 0.40, 0.20]),
};
if span <= f32::EPSILON {
return Some((
symbol_contrast,
IsoParameter {
value: 0.0,
grade: IsoGrade::F,
},
));
}
let global_threshold = f32::midpoint(r_high, r_low);
let mut mods: Vec<f32> = samples
.iter()
.map(|&r| ((2.0 * (f32::from(r) - global_threshold).abs()) / span).min(1.0))
.collect();
mods.sort_unstable_by(f32::total_cmp);
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_precision_loss,
reason = "index math over ≤177² module samples"
)]
let mod_value = mods[((mods.len() - 1) as f32 * 0.05).round() as usize];
let modulation = IsoParameter {
value: mod_value,
grade: grade_floor(mod_value, [0.50, 0.40, 0.30, 0.20]),
};
Some((symbol_contrast, modulation))
}
#[expect(
clippy::cast_possible_truncation,
reason = "the one rounding of the f64 distance to f32"
)]
fn dist(a: Point, b: Point) -> f32 {
let (dx, dy) = (f64::from(a.x - b.x), f64::from(a.y - b.y));
(dx * dx + dy * dy).sqrt() as f32
}
pub(super) fn axial_nonuniformity(corners: [Point; 4]) -> IsoParameter {
let x_span = f32::midpoint(dist(corners[0], corners[1]), dist(corners[3], corners[2]));
let y_span = f32::midpoint(dist(corners[0], corners[3]), dist(corners[1], corners[2]));
let mean = f32::midpoint(x_span, y_span);
let value = if mean <= f32::EPSILON {
1.0
} else {
(x_span - y_span).abs() / mean
};
IsoParameter {
value,
grade: grade_ceiling(value, [0.06, 0.08, 0.10, 0.12]),
}
}
fn fixed_pattern_damage(structural: &StructuralReport) -> IsoParameter {
let worst = structural
.finder_integrity
.iter()
.copied()
.fold(1.0f32, f32::min);
let mut grade = grade_floor(worst, [0.95, 0.90, 0.80, 0.70]);
if !structural.quiet_zone_ok {
grade = grade.max(IsoGrade::D); }
IsoParameter {
value: worst,
grade,
}
}
pub(crate) fn compute(
img: &LumaImage,
corners: [Point; 4],
version: u8,
structural: &StructuralReport,
uec: Option<&UecReport>,
) -> Option<Iso15415Report> {
let modules = u32::from(version) * 4 + 17;
let sampler = GridSampler::new(img, corners, modules)?;
#[expect(clippy::cast_possible_wrap, reason = "modules ≤ 177")]
let n = modules as i32;
let mut module_means = Vec::with_capacity((modules * modules) as usize);
for my in 0..n {
for mx in 0..n {
if let Some(v) = sampler.module(mx, my) {
module_means.push(v);
}
}
}
let (symbol_contrast, modulation) = contrast_and_modulation(&module_means)?;
let anu = axial_nonuniformity(corners);
let fpd = fixed_pattern_damage(structural);
let uec_param = uec.map(|u| IsoParameter {
value: u.margin,
grade: grade_floor(u.margin, [0.62, 0.50, 0.37, 0.25]),
});
let overall = [
symbol_contrast.grade,
modulation.grade,
anu.grade,
fpd.grade,
]
.into_iter()
.chain(uec_param.map(|p| p.grade))
.max()
.unwrap_or(IsoGrade::F);
Some(Iso15415Report {
symbol_contrast,
modulation,
axial_nonuniformity: anu,
fixed_pattern_damage: fpd,
unused_error_correction: uec_param,
overall,
})
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::cast_precision_loss)]
use super::*;
fn p(x: f32, y: f32) -> Point {
Point { x, y }
}
#[test]
fn modules_formula_drives_the_sample_grid() {
let side = 210u32;
let data: Vec<u8> = (0..side * side)
.map(|i| {
let (x, y) = (i % side, i / side);
if (x / 5 + y / 5) % 2 == 0 { 0 } else { 255 }
})
.collect();
let img = LumaImage::new(data, side, side);
let corners = [
p(0.0, 0.0),
p((side - 1) as f32, 0.0),
p((side - 1) as f32, (side - 1) as f32),
p(0.0, (side - 1) as f32),
];
let structural = StructuralReport {
finder_integrity: [1.0, 1.0, 1.0],
quiet_zone_ok: true,
};
let report = compute(&img, corners, 1, &structural, None).expect("gradeable");
assert_eq!(report.symbol_contrast.grade, IsoGrade::A, "{report:?}");
assert_eq!(
report.modulation.grade,
IsoGrade::D,
"correct modules=21 → modulation D; a mutated version*4+17 re-phases the grid: {:?}",
report.modulation
);
}
#[test]
fn grade_bands_hit_their_exact_thresholds() {
let sc = [0.70, 0.55, 0.40, 0.20];
assert_eq!(grade_floor(0.70, sc), IsoGrade::A);
assert_eq!(grade_floor(0.699, sc), IsoGrade::B);
assert_eq!(grade_floor(0.55, sc), IsoGrade::B);
assert_eq!(grade_floor(0.40, sc), IsoGrade::C);
assert_eq!(grade_floor(0.20, sc), IsoGrade::D);
assert_eq!(grade_floor(0.199, sc), IsoGrade::F);
let anu = [0.06, 0.08, 0.10, 0.12];
assert_eq!(grade_ceiling(0.06, anu), IsoGrade::A);
assert_eq!(grade_ceiling(0.061, anu), IsoGrade::B);
assert_eq!(grade_ceiling(0.12, anu), IsoGrade::D);
assert_eq!(grade_ceiling(0.121, anu), IsoGrade::F);
}
#[test]
fn anu_square_is_a_and_two_to_one_is_f() {
let square =
axial_nonuniformity([p(0.0, 0.0), p(100.0, 0.0), p(100.0, 100.0), p(0.0, 100.0)]);
assert_eq!(square.grade, IsoGrade::A);
assert!(square.value < 0.001, "{}", square.value);
let stretched =
axial_nonuniformity([p(0.0, 0.0), p(200.0, 0.0), p(200.0, 100.0), p(0.0, 100.0)]);
assert_eq!(stretched.grade, IsoGrade::F);
assert!(
(stretched.value - 2.0 / 3.0).abs() < 0.001,
"{}",
stretched.value
);
}
#[test]
fn contrast_and_modulation_on_a_clean_bimodal_distribution() {
let samples: Vec<u8> = (0..200).map(|i| if i % 2 == 0 { 0 } else { 255 }).collect();
let (sc, modulation) = contrast_and_modulation(&samples).unwrap();
assert_eq!(sc.grade, IsoGrade::A);
assert!((sc.value - 1.0).abs() < 0.01, "{}", sc.value);
assert_eq!(modulation.grade, IsoGrade::A);
assert!(
(modulation.value - 1.0).abs() < 0.01,
"{}",
modulation.value
);
}
#[test]
fn weak_modulation_grades_down_even_with_decent_contrast() {
let mut samples: Vec<u8> = (0..180)
.map(|i| if i % 2 == 0 { 10 } else { 245 })
.collect();
samples.extend(std::iter::repeat_n(128u8, 20));
let (sc, modulation) = contrast_and_modulation(&samples).unwrap();
assert_eq!(sc.grade, IsoGrade::A, "{sc:?}");
assert_eq!(modulation.grade, IsoGrade::F, "{modulation:?}");
}
#[test]
fn flat_image_is_double_f_not_a_panic() {
let samples = vec![128u8; 64];
let (sc, modulation) = contrast_and_modulation(&samples).unwrap();
assert_eq!(sc.grade, IsoGrade::F);
assert_eq!(modulation.grade, IsoGrade::F);
}
#[test]
fn percentile_index_uses_len_minus_one() {
let mut samples = vec![40u8; 196];
samples.extend([240u8; 4]);
let (sc, modulation) = contrast_and_modulation(&samples).unwrap();
assert_eq!(sc.grade, IsoGrade::F, "{sc:?}");
assert_eq!(modulation.grade, IsoGrade::F, "{modulation:?}");
}
#[test]
fn sixteen_module_means_is_the_minimum_admitted() {
let sixteen: Vec<u8> = (0..16).map(|i| if i % 2 == 0 { 0 } else { 255 }).collect();
assert!(contrast_and_modulation(&sixteen).is_some());
let fifteen: Vec<u8> = (0..15).map(|i| if i % 2 == 0 { 0 } else { 255 }).collect();
assert!(contrast_and_modulation(&fifteen).is_none());
}
#[test]
fn modulation_percentile_index_uses_len_minus_one() {
let mut samples = vec![0u8; 14];
samples.extend(std::iter::repeat_n(255u8, 13));
samples.extend([70u8, 95u8, 103u8]); let (sc, modulation) = contrast_and_modulation(&samples).unwrap();
assert_eq!(sc.grade, IsoGrade::A, "span 255 → contrast A: {sc:?}");
assert_eq!(
modulation.grade,
IsoGrade::D,
"5th-pct index 1 (mods[1]=0.255 → D), not index 2 (mods[2]=0.451 → B): {modulation:?}"
);
}
#[test]
fn fixed_pattern_damage_caps_at_d_on_quiet_zone_violation() {
let pristine = StructuralReport {
finder_integrity: [1.0, 1.0, 1.0],
quiet_zone_ok: false,
};
let fpd = fixed_pattern_damage(&pristine);
assert_eq!(fpd.grade, IsoGrade::D, "{fpd:?}");
let damaged = StructuralReport {
finder_integrity: [1.0, 0.82, 1.0],
quiet_zone_ok: true,
};
assert_eq!(fixed_pattern_damage(&damaged).grade, IsoGrade::C);
}
}