use crate::kernels::geometry::{js_ceil, js_floor};
use crate::kernels::region::{Mask, Region};
pub type AtlasMask<'a> = Mask<&'a [u8]>;
pub type AtlasRegion<'a> = Region<&'a [u8]>;
pub fn label_atlas(
regions: &[AtlasRegion],
width: usize,
height: usize,
out: &mut [u8],
) -> Option<Vec<u32>> {
if regions.len() > 254 {
return None;
}
out.fill(0);
let mut counts = vec![0u32; regions.len() + 1];
for (index, region) in regions.iter().enumerate() {
let code = (index + 1) as u8;
let r = region.rect;
let x0 = 0i64.max(js_floor(r.x) as i64) as usize;
let y0 = 0i64.max(js_floor(r.y) as i64) as usize;
let x1 = (width as i64).min(js_ceil(r.x + r.width) as i64).max(0) as usize;
let y1 = (height as i64).min(js_ceil(r.y + r.height) as i64).max(0) as usize;
for y in y0..y1.min(height) {
for x in x0..x1.min(width) {
let i = y * width + x;
if out[i] == 0 && region.contains(x as f64, y as f64, width as f64, height as f64) {
out[i] = code;
counts[code as usize] += 1;
}
}
}
}
Some(counts)
}