pico_detect/detect/
padding.rs1use image::GenericImageView;
2use imageproc::rect::Rect;
3
4#[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 #[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 #[inline]
27 pub fn top(self, value: i32) -> Self {
28 Self { top: value, ..self }
29 }
30
31 #[inline]
33 pub fn right(self, value: i32) -> Self {
34 Self {
35 right: value,
36 ..self
37 }
38 }
39
40 #[inline]
42 pub fn bottom(self, value: i32) -> Self {
43 Self {
44 bottom: value,
45 ..self
46 }
47 }
48
49 #[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 #[inline]
62 pub fn vertical(self, value: i32) -> Self {
63 Self {
64 top: value,
65 bottom: value,
66 ..self
67 }
68 }
69
70 #[inline]
72 pub fn horizontal(self, value: i32) -> Self {
73 Self {
74 right: value,
75 left: value,
76 ..self
77 }
78 }
79
80 #[inline]
82 pub fn left(self, value: i32) -> Self {
83 Self {
84 left: value,
85 ..self
86 }
87 }
88
89 #[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 #[inline]
99 pub fn image_rect<I: GenericImageView>(self, image: &I) -> Rect {
100 self.rect(image.width(), image.height())
101 }
102}