1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * v4l2-rect.h - v4l2_rect helper functions
5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
11 #include <linux/videodev2.h>
14 * v4l2_rect_set_size_to() - copy the width/height values.
15 * @r: rect whose width and height fields will be set
16 * @size: rect containing the width and height fields you need.
18 static inline void v4l2_rect_set_size_to(struct v4l2_rect
*r
,
19 const struct v4l2_rect
*size
)
21 r
->width
= size
->width
;
22 r
->height
= size
->height
;
26 * v4l2_rect_set_min_size() - width and height of r should be >= min_size.
27 * @r: rect whose width and height will be modified
28 * @min_size: rect containing the minimal width and height
30 static inline void v4l2_rect_set_min_size(struct v4l2_rect
*r
,
31 const struct v4l2_rect
*min_size
)
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
;
40 * v4l2_rect_set_max_size() - width and height of r should be <= max_size
41 * @r: rect whose width and height will be modified
42 * @max_size: rect containing the maximum width and height
44 static inline void v4l2_rect_set_max_size(struct v4l2_rect
*r
,
45 const struct v4l2_rect
*max_size
)
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
;
54 * v4l2_rect_map_inside()- r should be inside boundary.
55 * @r: rect that will be modified
56 * @boundary: rect containing the boundary for @r
58 static inline void v4l2_rect_map_inside(struct v4l2_rect
*r
,
59 const struct v4l2_rect
*boundary
)
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
->width
)
67 r
->left
= boundary
->width
- r
->width
;
68 if (r
->top
+ r
->height
> boundary
->height
)
69 r
->top
= boundary
->height
- r
->height
;
73 * v4l2_rect_same_size() - return true if r1 has the same size as r2
77 * Return true if both rectangles have the same size.
79 static inline bool v4l2_rect_same_size(const struct v4l2_rect
*r1
,
80 const struct v4l2_rect
*r2
)
82 return r1
->width
== r2
->width
&& r1
->height
== r2
->height
;
86 * v4l2_rect_intersect() - calculate the intersection of two rects.
87 * @r: intersection of @r1 and @r2.
91 static inline void v4l2_rect_intersect(struct v4l2_rect
*r
,
92 const struct v4l2_rect
*r1
,
93 const struct v4l2_rect
*r2
)
97 r
->top
= max(r1
->top
, r2
->top
);
98 r
->left
= max(r1
->left
, r2
->left
);
99 bottom
= min(r1
->top
+ r1
->height
, r2
->top
+ r2
->height
);
100 right
= min(r1
->left
+ r1
->width
, r2
->left
+ r2
->width
);
101 r
->height
= max(0, bottom
- r
->top
);
102 r
->width
= max(0, right
- r
->left
);
106 * v4l2_rect_scale() - scale rect r by to/from
107 * @r: rect to be scaled.
108 * @from: from rectangle.
111 * This scales rectangle @r horizontally by @to->width / @from->width and
112 * vertically by @to->height / @from->height.
114 * Typically @r is a rectangle inside @from and you want the rectangle as
115 * it would appear after scaling @from to @to. So the resulting @r will
116 * be the scaled rectangle inside @to.
118 static inline void v4l2_rect_scale(struct v4l2_rect
*r
,
119 const struct v4l2_rect
*from
,
120 const struct v4l2_rect
*to
)
122 if (from
->width
== 0 || from
->height
== 0) {
123 r
->left
= r
->top
= r
->width
= r
->height
= 0;
126 r
->left
= (((r
->left
- from
->left
) * to
->width
) / from
->width
) & ~1;
127 r
->width
= ((r
->width
* to
->width
) / from
->width
) & ~1;
128 r
->top
= ((r
->top
- from
->top
) * to
->height
) / from
->height
;
129 r
->height
= (r
->height
* to
->height
) / from
->height
;
133 * v4l2_rect_overlap() - do r1 and r2 overlap?
137 * Returns true if @r1 and @r2 overlap.
139 static inline bool v4l2_rect_overlap(const struct v4l2_rect
*r1
,
140 const struct v4l2_rect
*r2
)
143 * IF the left side of r1 is to the right of the right side of r2 OR
144 * the left side of r2 is to the right of the right side of r1 THEN
145 * they do not overlap.
147 if (r1
->left
>= r2
->left
+ r2
->width
||
148 r2
->left
>= r1
->left
+ r1
->width
)
151 * IF the top side of r1 is below the bottom of r2 OR
152 * the top side of r2 is below the bottom of r1 THEN
153 * they do not overlap.
155 if (r1
->top
>= r2
->top
+ r2
->height
||
156 r2
->top
>= r1
->top
+ r1
->height
)