1
2
3
4
5
6
7
8#ifndef _V4L2_RECT_H_
9#define _V4L2_RECT_H_
10
11#include <linux/videodev2.h>
12
13
14
15
16
17
18static inline void v4l2_rect_set_size_to(struct v4l2_rect *r,
19 const struct v4l2_rect *size)
20{
21 r->width = size->width;
22 r->height = size->height;
23}
24
25
26
27
28
29
30static inline void v4l2_rect_set_min_size(struct v4l2_rect *r,
31 const struct v4l2_rect *min_size)
32{
33 if (r->width < min_size->width)
34 r->width = min_size->width;
35 if (r->height < min_size->height)
36 r->height = min_size->height;
37}
38
39
40
41
42
43
44static inline void v4l2_rect_set_max_size(struct v4l2_rect *r,
45 const struct v4l2_rect *max_size)
46{
47 if (r->width > max_size->width)
48 r->width = max_size->width;
49 if (r->height > max_size->height)
50 r->height = max_size->height;
51}
52
53
54
55
56
57
58static inline void v4l2_rect_map_inside(struct v4l2_rect *r,
59 const struct v4l2_rect *boundary)
60{
61 v4l2_rect_set_max_size(r, boundary);
62 if (r->left < boundary->left)
63 r->left = boundary->left;
64 if (r->top < boundary->top)
65 r->top = boundary->top;
66 if (r->left + r->width > boundary->left + boundary->width)
67 r->left = boundary->left + boundary->width - r->width;
68 if (r->top + r->height > boundary->top + boundary->height)
69 r->top = boundary->top + boundary->height - r->height;
70}
71
72
73
74
75
76
77
78
79static inline bool v4l2_rect_same_size(const struct v4l2_rect *r1,
80 const struct v4l2_rect *r2)
81{
82 return r1->width == r2->width && r1->height == r2->height;
83}
84
85
86
87
88
89
90
91
92static inline bool v4l2_rect_same_position(const struct v4l2_rect *r1,
93 const struct v4l2_rect *r2)
94{
95 return r1->top == r2->top && r1->left == r2->left;
96}
97
98
99
100
101
102
103
104
105static inline bool v4l2_rect_equal(const struct v4l2_rect *r1,
106 const struct v4l2_rect *r2)
107{
108 return v4l2_rect_same_size(r1, r2) && v4l2_rect_same_position(r1, r2);
109}
110
111
112
113
114
115
116
117static inline void v4l2_rect_intersect(struct v4l2_rect *r,
118 const struct v4l2_rect *r1,
119 const struct v4l2_rect *r2)
120{
121 int right, bottom;
122
123 r->top = max(r1->top, r2->top);
124 r->left = max(r1->left, r2->left);
125 bottom = min(r1->top + r1->height, r2->top + r2->height);
126 right = min(r1->left + r1->width, r2->left + r2->width);
127 r->height = max(0, bottom - r->top);
128 r->width = max(0, right - r->left);
129}
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144static inline void v4l2_rect_scale(struct v4l2_rect *r,
145 const struct v4l2_rect *from,
146 const struct v4l2_rect *to)
147{
148 if (from->width == 0 || from->height == 0) {
149 r->left = r->top = r->width = r->height = 0;
150 return;
151 }
152 r->left = (((r->left - from->left) * to->width) / from->width) & ~1;
153 r->width = ((r->width * to->width) / from->width) & ~1;
154 r->top = ((r->top - from->top) * to->height) / from->height;
155 r->height = (r->height * to->height) / from->height;
156}
157
158
159
160
161
162
163
164
165static inline bool v4l2_rect_overlap(const struct v4l2_rect *r1,
166 const struct v4l2_rect *r2)
167{
168
169
170
171
172
173 if (r1->left >= r2->left + r2->width ||
174 r2->left >= r1->left + r1->width)
175 return false;
176
177
178
179
180
181 if (r1->top >= r2->top + r2->height ||
182 r2->top >= r1->top + r1->height)
183 return false;
184 return true;
185}
186
187
188
189
190
191
192
193
194static inline bool v4l2_rect_enclosed(struct v4l2_rect *r1,
195 struct v4l2_rect *r2)
196{
197 if (r1->left < r2->left || r1->top < r2->top)
198 return false;
199 if (r1->left + r1->width > r2->left + r2->width)
200 return false;
201 if (r1->top + r1->height > r2->top + r2->height)
202 return false;
203
204 return true;
205}
206
207#endif
208