Skip to main content

video_interop/
geometry.rs

1use crate::ValidationError;
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4#[cfg_attr(feature = "rustler", derive(rustler::NifStruct))]
5#[cfg_attr(feature = "rustler", module = "VideoInterop.Rect")]
6pub struct Rect {
7    pub x: u32,
8    pub y: u32,
9    pub width: u32,
10    pub height: u32,
11}
12
13impl Rect {
14    pub fn validate(&self, coded_width: u32, coded_height: u32) -> Result<(), ValidationError> {
15        if coded_width == 0 || coded_height == 0 {
16            return Err(ValidationError::ZeroCodedSize {
17                width: coded_width,
18                height: coded_height,
19            });
20        }
21        if self.width == 0 || self.height == 0 {
22            return Err(ValidationError::ZeroVisibleSize {
23                width: self.width,
24                height: self.height,
25            });
26        }
27
28        let right = self
29            .x
30            .checked_add(self.width)
31            .ok_or(ValidationError::VisibleRectOverflow)?;
32        let bottom = self
33            .y
34            .checked_add(self.height)
35            .ok_or(ValidationError::VisibleRectOverflow)?;
36
37        if right > coded_width || bottom > coded_height {
38            return Err(ValidationError::VisibleRectOutOfBounds {
39                x: self.x,
40                y: self.y,
41                width: self.width,
42                height: self.height,
43                coded_width,
44                coded_height,
45            });
46        }
47
48        Ok(())
49    }
50}