use candle_core::{Result, Tensor};
pub fn pad(image: &Tensor, max_h: usize, max_w: usize) -> Result<Tensor> {
let (c, h, w) = image.dims3()?;
let new_image = Tensor::zeros((c, max_h, max_w), image.dtype(), image.device())?;
new_image.slice_assign(&[&(..c), &(..h), &(..w)], image)
}
pub fn make_pixel_mask(image: &Tensor, h: usize, w: usize) -> Result<Tensor> {
let (_c, max_h, max_w) = image.dims3()?;
let mask = Tensor::ones((h, w), image.dtype(), image.device())?;
let zeros = Tensor::zeros((max_h, max_w), image.dtype(), image.device())?;
zeros.slice_assign(&[&(..h), &(..w)], &mask)
}
pub fn get_resize_image_size(
(h, w): (usize, usize),
(min_len, max_len): (usize, usize),
) -> (usize, usize) {
let aspect_ratio = w as f64 / h as f64;
let (new_h, new_w) = if w >= h && w > max_len {
((max_len as f64 / aspect_ratio) as usize, max_len)
} else if h > w && h > max_len {
(max_len, (max_len as f64 * aspect_ratio) as usize)
} else {
(h, w)
};
(new_h.max(min_len), new_w.max(min_len))
}