use crate::engine::content::ContentKey;
use crate::error::Result;
use crate::input::{LumaImage, bt601};
use crate::ladder::AlphaBackground;
use crate::report::{
AlphaEnvelope, AlphaMode, AlphaPaletteProbe, AlphaPlacement, AlphaProbe, AlphaReport, Symbology,
};
use crate::score::{Bound, Calibrated, CellProbe};
use crate::transform::{self, SourcePlanes};
const ENVELOPE_BASE_SIDE: u32 = 512;
const ENVELOPE_RUNGS: [u8; 5] = [0, 64, 128, 192, 255];
#[inline]
pub(crate) fn composite(fg: u8, bg: u8, a: u8) -> u8 {
let n = u32::from(fg) * u32::from(a) + u32::from(bg) * (255 - u32::from(a)) + 127;
u8::try_from(n / 255).unwrap_or(u8::MAX)
}
pub(crate) fn auto_background(content_luma: u8) -> [u8; 3] {
if content_luma < 128 { [255; 3] } else { [0; 3] }
}
pub(crate) fn has_transparency(rgba: &[u8]) -> bool {
rgba.as_chunks::<4>().0.iter().any(|px| px[3] < 255)
}
pub(crate) struct ContentStats {
pub transparent: u64,
pub content_luma: u8,
}
pub(crate) fn content_stats(rgba: &[u8]) -> ContentStats {
let mut transparent = 0u64;
let mut weight = 0u64;
let mut weighted_luma = 0u64;
for px in rgba.as_chunks::<4>().0 {
let a = u64::from(px[3]);
if a < 255 {
transparent += 1;
}
weight += a;
weighted_luma += u64::from(bt601(px[0], px[1], px[2])) * a;
}
let content_luma = (weighted_luma + weight / 2)
.checked_div(weight)
.map_or(0, |mean| u8::try_from(mean).unwrap_or(u8::MAX));
ContentStats {
transparent,
content_luma,
}
}
#[derive(Debug)]
pub(crate) enum Envelope {
Measured(AlphaEnvelope),
Undecodable,
Cut,
}
#[derive(Debug, Clone)]
pub(crate) struct AlphaContext {
alpha: Vec<u8>,
fg_luma: Vec<u8>,
width: u32,
height: u32,
coverage: f32,
content_luma: u8,
mode: AlphaBackground,
background: [u8; 3],
}
impl AlphaContext {
#[expect(
clippy::cast_precision_loss,
reason = "transparent/total are bounded by Limits::max_pixels (64MP < 2^52) — exact in f64"
)]
#[expect(
clippy::cast_possible_truncation,
reason = "a 0..=1 ratio rounded to 3 decimals fits f32 exactly"
)]
pub(crate) fn new(
alpha: Vec<u8>,
fg_luma: Vec<u8>,
width: u32,
height: u32,
stats: &ContentStats,
mode: AlphaBackground,
background: [u8; 3],
) -> Self {
let total = u64::from(width) * u64::from(height);
let coverage = if total == 0 {
0.0
} else {
((stats.transparent as f64 / total as f64) * 1000.0).round() / 1000.0
};
Self {
alpha,
fg_luma,
width,
height,
coverage: coverage as f32,
content_luma: stats.content_luma,
mode,
background,
}
}
pub(crate) fn opposite_planes(&self) -> Option<(SourcePlanes, [u8; 3])> {
if self.mode != AlphaBackground::Auto {
return None;
}
let bg = if self.background == [255; 3] {
[0; 3]
} else {
[255; 3]
};
let luma: Vec<u8> = self
.fg_luma
.iter()
.zip(&self.alpha)
.map(|(&fg, &a)| composite(fg, bg[0], a))
.collect();
Some((
SourcePlanes::luma_only(LumaImage::new(luma, self.width, self.height)),
bg,
))
}
pub(crate) fn adopt_background(&mut self, background: [u8; 3]) {
self.background = background;
}
pub(crate) fn envelope(
&self,
expected: &ContentKey,
symbology: Symbology,
palette: &[[u8; 3]],
bound: &mut Bound<'_>,
) -> Result<Envelope> {
if !bound.admit()? {
return Ok(Envelope::Cut);
}
let premultiplied: Vec<u8> = self
.fg_luma
.iter()
.zip(&self.alpha)
.map(|(&fg, &a)| composite(fg, 0, a))
.collect();
let pm = transform::downscale_to(
&LumaImage::new(premultiplied, self.width, self.height),
ENVELOPE_BASE_SIDE,
);
let alpha = transform::downscale_to(
&LumaImage::new(self.alpha.clone(), self.width, self.height),
ENVELOPE_BASE_SIDE,
);
let over = |bg: u8| -> LumaImage {
let luma: Vec<u8> = pm
.data()
.iter()
.zip(alpha.data())
.map(|(&p, &a)| p.saturating_add(composite(0, bg, a)))
.collect();
LumaImage::new(luma, pm.width(), pm.height())
};
let base = over(bt601(
self.background[0],
self.background[1],
self.background[2],
));
let cell = match CellProbe::calibrate_admitted(&base, expected, symbology, bound)? {
Calibrated::Class(cell) => cell,
Calibrated::Undecodable => return Ok(Envelope::Undecodable),
Calibrated::Interrupted => return Ok(Envelope::Cut),
};
let mut probe = |bg: u8| -> Result<Option<bool>> {
if !bound.admit()? {
return Ok(None);
}
Ok(Some(crate::score::cell_passes(
&over(bg),
expected,
cell,
bound,
)))
};
let mut probes: Vec<(u8, bool)> = Vec::with_capacity(ENVELOPE_RUNGS.len() + 4);
for bg in ENVELOPE_RUNGS {
match probe(bg)? {
Some(decoded) => probes.push((bg, decoded)),
None => return Ok(Envelope::Cut),
}
}
let mut refinements = Vec::new();
for pair in probes.windows(2) {
if pair[0].1 != pair[1].1 {
let mid = u8::midpoint(pair[0].0, pair[1].0);
match probe(mid)? {
Some(decoded) => refinements.push((mid, decoded)),
None => return Ok(Envelope::Cut),
}
}
}
probes.extend(refinements);
probes.sort_unstable_by_key(|&(bg, _)| bg);
let mut palette_probes = Vec::with_capacity(palette.len());
for &color in palette {
let luma = bt601(color[0], color[1], color[2]);
match probe(luma)? {
Some(decoded) => palette_probes.push(AlphaPaletteProbe {
background: background_name(color),
background_luma: luma,
decoded,
}),
None => return Ok(Envelope::Cut),
}
}
let (safe_luma, placement) = classify(&probes);
Ok(Envelope::Measured(AlphaEnvelope {
probes: probes
.into_iter()
.map(|(background_luma, decoded)| AlphaProbe {
background_luma,
decoded,
})
.collect(),
safe_luma,
placement,
palette: palette_probes,
}))
}
pub(crate) fn into_report(
self,
fallback_used: bool,
envelope: Option<AlphaEnvelope>,
) -> AlphaReport {
let mode = match self.mode {
AlphaBackground::Auto | AlphaBackground::None => AlphaMode::Auto,
AlphaBackground::White => AlphaMode::White,
AlphaBackground::Black => AlphaMode::Black,
AlphaBackground::Custom(_) => AlphaMode::Custom,
};
AlphaReport {
coverage: self.coverage,
content_luma: self.content_luma,
mode,
background: background_name(self.background),
fallback_used,
envelope,
}
}
}
fn background_name(bg: [u8; 3]) -> String {
match bg {
[255, 255, 255] => "white".to_owned(),
[0, 0, 0] => "black".to_owned(),
[r, g, b] => format!("#{r:02x}{g:02x}{b:02x}"),
}
}
fn classify(probes: &[(u8, bool)]) -> (Vec<[u8; 2]>, AlphaPlacement) {
let mut bands: Vec<[u8; 2]> = Vec::new();
let mut previous_decoded = false;
for &(bg, decoded) in probes {
if decoded {
match bands.last_mut() {
Some(band) if previous_decoded => band[1] = bg,
_ => bands.push([bg, bg]),
}
}
previous_decoded = decoded;
}
let placement = if bands.is_empty() {
AlphaPlacement::None
} else if probes.iter().all(|&(_, decoded)| decoded) {
AlphaPlacement::Any
} else if bands.len() == 1 && bands[0][1] == 255 {
AlphaPlacement::LightOnly
} else if bands.len() == 1 && bands[0][0] == 0 {
AlphaPlacement::DarkOnly
} else {
AlphaPlacement::Mixed
};
(bands, placement)
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used)]
use super::*;
use crate::ladder::CancelToken;
use web_time::Instant;
#[test]
fn composite_is_exact_at_the_anchors() {
assert_eq!(composite(40, 255, 255), 40);
assert_eq!(composite(40, 255, 0), 255);
}
#[test]
fn split_composite_stays_within_one_lsb_of_direct() {
for fg in 0..=255u8 {
for a in 0..=255u8 {
for &bg in &[0u8, 1, 64, 128, 192, 254, 255] {
let split = composite(fg, 0, a).saturating_add(composite(0, bg, a));
let direct = composite(fg, bg, a);
assert!(
split.abs_diff(direct) <= 1,
"fg={fg} bg={bg} a={a}: split {split} vs direct {direct}"
);
}
}
}
}
#[test]
fn composite_half_alpha_pins_the_rounding() {
assert_eq!(composite(0, 255, 128), 127);
assert_eq!(composite(255, 0, 128), 128);
}
#[test]
fn auto_background_flips_at_the_midpoint() {
assert_eq!(auto_background(0), [255; 3]);
assert_eq!(auto_background(127), [255; 3]);
assert_eq!(auto_background(128), [0; 3]);
assert_eq!(auto_background(255), [0; 3]);
}
#[test]
fn content_stats_weights_luma_by_alpha() {
let rgba = [0u8, 0, 0, 255, 255, 255, 255, 0];
let stats = content_stats(&rgba);
assert_eq!(stats.transparent, 1);
assert_eq!(stats.content_luma, 0);
}
#[test]
fn content_stats_semi_transparent_contributes_proportionally() {
let rgba = [255u8, 255, 255, 128];
let stats = content_stats(&rgba);
assert_eq!(stats.transparent, 1);
assert_eq!(stats.content_luma, 255);
}
#[test]
fn content_stats_rounds_the_mean_luma_half_up() {
assert_eq!(content_stats(&[0, 0, 0, 255, 1, 1, 1, 255]).content_luma, 1);
let quarter = [0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 1, 1, 1, 255];
assert_eq!(content_stats(&quarter).content_luma, 0);
}
#[test]
fn content_stats_fully_transparent_is_zero_luma() {
let rgba = [200u8, 200, 200, 0];
let stats = content_stats(&rgba);
assert_eq!(stats.transparent, 1);
assert_eq!(stats.content_luma, 0, "no visible mass → deterministic 0");
}
#[test]
fn classify_all_decoded_is_any() {
let probes: Vec<(u8, bool)> = ENVELOPE_RUNGS.iter().map(|&bg| (bg, true)).collect();
let (bands, placement) = classify(&probes);
assert_eq!(bands, vec![[0, 255]]);
assert_eq!(placement, AlphaPlacement::Any);
}
#[test]
fn classify_light_suffix_is_light_only() {
let probes = vec![
(0, false),
(64, false),
(96, false),
(128, true),
(192, true),
(255, true),
];
let (bands, placement) = classify(&probes);
assert_eq!(bands, vec![[128, 255]]);
assert_eq!(placement, AlphaPlacement::LightOnly);
}
#[test]
fn classify_dark_prefix_is_dark_only() {
let probes = vec![
(0, true),
(64, true),
(128, false),
(192, false),
(255, false),
];
let (bands, placement) = classify(&probes);
assert_eq!(bands, vec![[0, 64]]);
assert_eq!(placement, AlphaPlacement::DarkOnly);
}
#[test]
fn classify_split_ends_are_mixed_with_two_bands() {
let probes = vec![
(0, true),
(64, false),
(128, false),
(192, false),
(255, true),
];
let (bands, placement) = classify(&probes);
assert_eq!(bands, vec![[0, 0], [255, 255]]);
assert_eq!(placement, AlphaPlacement::Mixed);
}
#[test]
fn classify_nothing_decoded_is_none() {
let probes: Vec<(u8, bool)> = ENVELOPE_RUNGS.iter().map(|&bg| (bg, false)).collect();
let (bands, placement) = classify(&probes);
assert_eq!(bands.len(), 0);
assert_eq!(placement, AlphaPlacement::None);
}
#[test]
fn classify_mid_only_band_is_mixed() {
let probes = vec![
(0, false),
(64, true),
(128, true),
(192, false),
(255, false),
];
let (bands, placement) = classify(&probes);
assert_eq!(bands, vec![[64, 128]]);
assert_eq!(placement, AlphaPlacement::Mixed);
}
#[test]
fn background_name_spells_white_black_and_hex() {
assert_eq!(background_name([255; 3]), "white");
assert_eq!(background_name([0; 3]), "black");
assert_eq!(background_name([26, 26, 46]), "#1a1a2e");
}
fn transparent_qr(content: &str) -> (Vec<u8>, u32) {
let code = qrcode::QrCode::new(content.as_bytes()).unwrap();
let width = code.width();
let side = (width + 8) * 6;
let mut rgba = vec![0u8; side * side * 4];
for (i, color) in code.to_colors().iter().enumerate() {
if *color == qrcode::Color::Dark {
let (mx, my) = (i % width + 4, i / width + 4);
for y in my * 6..(my + 1) * 6 {
for x in mx * 6..(mx + 1) * 6 {
rgba[(y * side + x) * 4 + 3] = 255;
}
}
}
}
(rgba, u32::try_from(side).unwrap())
}
#[test]
fn a_cut_envelope_is_told_from_an_undecodable_one() {
use crate::input::{ImageInput, Limits};
let (rgba, side) = transparent_qr("https://qrcode-ai.com/envelope-cut");
let normalized = crate::transform::normalize_with(
&ImageInput::rgba8(&rgba, side, side),
&Limits::default(),
AlphaBackground::Auto,
)
.unwrap();
let context = normalized.alpha.expect("a transparent input");
let cancel = CancelToken::new();
let walked = crate::ladder::run(
&normalized.planes,
&crate::ladder::ScanConfig::full(),
&cancel,
None,
)
.unwrap();
let key = walked.contents[0].0.clone();
let envelope = |key: &ContentKey, deadline| {
context.envelope(
key,
Symbology::QrCode,
&[],
&mut Bound::new(&cancel, deadline),
)
};
assert!(matches!(
envelope(&key, None).unwrap(),
Envelope::Measured(_)
));
assert!(matches!(
envelope(&key, Some(Instant::now())).unwrap(),
Envelope::Cut
));
let other = ContentKey {
raw: b"another content".to_vec(),
..key
};
assert!(matches!(
envelope(&other, None).unwrap(),
Envelope::Undecodable
));
}
#[test]
fn the_envelope_bisects_each_verdict_boundary_and_no_other_gap() {
use crate::input::{ImageInput, Limits};
let (rgba, side) = transparent_qr("https://qrcode-ai.com/envelope-bands");
let normalized = crate::transform::normalize_with(
&ImageInput::rgba8(&rgba, side, side),
&Limits::default(),
AlphaBackground::Auto,
)
.unwrap();
let context = normalized.alpha.expect("a transparent input");
let cancel = CancelToken::new();
let walked = crate::ladder::run(
&normalized.planes,
&crate::ladder::ScanConfig::full(),
&cancel,
None,
)
.unwrap();
let key = walked.contents[0].0.clone();
let Envelope::Measured(envelope) = context
.envelope(&key, Symbology::QrCode, &[], &mut Bound::new(&cancel, None))
.unwrap()
else {
panic!("a measured envelope");
};
let rungs: Vec<&AlphaProbe> = envelope
.probes
.iter()
.filter(|probe| ENVELOPE_RUNGS.contains(&probe.background_luma))
.collect();
assert_eq!(rungs.len(), ENVELOPE_RUNGS.len());
let boundaries: Vec<u8> = rungs
.windows(2)
.filter(|pair| pair[0].decoded != pair[1].decoded)
.map(|pair| u8::midpoint(pair[0].background_luma, pair[1].background_luma))
.collect();
assert!(!boundaries.is_empty(), "{:?}", envelope.probes);
let bisections: Vec<u8> = envelope
.probes
.iter()
.map(|probe| probe.background_luma)
.filter(|background| !ENVELOPE_RUNGS.contains(background))
.collect();
assert_eq!(bisections, boundaries, "{:?}", envelope.probes);
}
}