Skip to main content

letterbox_rect

Function letterbox_rect 

Source
pub fn letterbox_rect(
    src_w: usize,
    src_h: usize,
    dst_w: usize,
    dst_h: usize,
    placement: LetterboxPlacement,
) -> Region
Expand description

The rectangle an src_w×src_h source occupies when letterboxed into a dst_w×dst_h destination: the larger axis fills, the other is scaled to keep the aspect ratio (rounded, at least one pixel), and placement positions it. A zero-sized source fills the destination.

This is the one letterbox computation: convert paints this rectangle, and ContentRegions are built from it.

use edgefirst_tensor::{letterbox_rect, LetterboxPlacement, Region};

let r = letterbox_rect(1280, 720, 640, 640, LetterboxPlacement::Center);
assert_eq!(r, Region::new(0, 140, 640, 360));
let r = letterbox_rect(1280, 720, 640, 640, LetterboxPlacement::TopLeft);
assert_eq!(r, Region::new(0, 0, 640, 360));