use crate::compat::{vec, Vec};
pub(crate) fn nearest_scale(
data: &[u8],
source_width: u32,
source_height: u32,
width: u32,
height: u32,
) -> Vec<u8> {
let mut out = vec![0u8; (width as usize) * (height as usize) * 4];
if source_width == 0 || source_height == 0 || width == 0 || height == 0 {
return out;
}
for y in 0..height {
let source_y = (y as u64 * source_height as u64 / height as u64) as u32;
for x in 0..width {
let source_x = (x as u64 * source_width as u64 / width as u64) as u32;
let source = ((source_y as usize * source_width as usize + source_x as usize) * 4)
.min(data.len().saturating_sub(4));
let destination = (y as usize * width as usize + x as usize) * 4;
out[destination..destination + 4].copy_from_slice(&data[source..source + 4]);
}
}
out
}