pico_detect/detect/
padding.rs

1use image::GenericImageView;
2use imageproc::rect::Rect;
3
4/// Padding around a rectangular region.
5#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
6pub struct Padding {
7    pub top: i32,
8    pub right: i32,
9    pub bottom: i32,
10    pub left: i32,
11}
12
13impl Padding {
14    /// Create a new padding with specified values.
15    #[inline]
16    pub fn new(top: i32, right: i32, bottom: i32, left: i32) -> Self {
17        Self {
18            top,
19            right,
20            bottom,
21            left,
22        }
23    }
24
25    /// Set the top padding value.
26    #[inline]
27    pub fn top(self, value: i32) -> Self {
28        Self { top: value, ..self }
29    }
30
31    /// Set the right padding value.
32    #[inline]
33    pub fn right(self, value: i32) -> Self {
34        Self {
35            right: value,
36            ..self
37        }
38    }
39
40    /// Set the bottom padding value.
41    #[inline]
42    pub fn bottom(self, value: i32) -> Self {
43        Self {
44            bottom: value,
45            ..self
46        }
47    }
48
49    /// Create padding with the same value for all sides.
50    #[inline]
51    pub fn all(value: i32) -> Self {
52        Self {
53            top: value,
54            right: value,
55            bottom: value,
56            left: value,
57        }
58    }
59
60    /// Set vertical padding (top and bottom).
61    #[inline]
62    pub fn vertical(self, value: i32) -> Self {
63        Self {
64            top: value,
65            bottom: value,
66            ..self
67        }
68    }
69
70    /// Set horizontal padding (left and right).
71    #[inline]
72    pub fn horizontal(self, value: i32) -> Self {
73        Self {
74            right: value,
75            left: value,
76            ..self
77        }
78    }
79
80    /// Set the left padding value.
81    #[inline]
82    pub fn left(self, value: i32) -> Self {
83        Self {
84            left: value,
85            ..self
86        }
87    }
88
89    /// Convert padding to a rectangle.
90    #[inline]
91    pub fn rect(self, width: u32, height: u32) -> Rect {
92        let w = (width as i32) - self.right - self.left;
93        let h = (height as i32) - self.bottom - self.top;
94        Rect::at(self.left, self.top).of_size(w as u32, h as u32)
95    }
96
97    /// Convert padding to a rectangle based on the image dimensions.
98    #[inline]
99    pub fn image_rect<I: GenericImageView>(self, image: &I) -> Rect {
100        self.rect(image.width(), image.height())
101    }
102}