Linux 4.14.5
[linux/fpc-iii.git] / include / media / v4l2-rect.h
blobd2125f0cc7cd5e3e5ea699d278fec33ed9d97e6e
1 /*
2 * v4l2-rect.h - v4l2_rect helper functions
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
20 #ifndef _V4L2_RECT_H_
21 #define _V4L2_RECT_H_
23 #include <linux/videodev2.h>
25 /**
26 * v4l2_rect_set_size_to() - copy the width/height values.
27 * @r: rect whose width and height fields will be set
28 * @size: rect containing the width and height fields you need.
30 static inline void v4l2_rect_set_size_to(struct v4l2_rect *r,
31 const struct v4l2_rect *size)
33 r->width = size->width;
34 r->height = size->height;
37 /**
38 * v4l2_rect_set_min_size() - width and height of r should be >= min_size.
39 * @r: rect whose width and height will be modified
40 * @min_size: rect containing the minimal width and height
42 static inline void v4l2_rect_set_min_size(struct v4l2_rect *r,
43 const struct v4l2_rect *min_size)
45 if (r->width < min_size->width)
46 r->width = min_size->width;
47 if (r->height < min_size->height)
48 r->height = min_size->height;
51 /**
52 * v4l2_rect_set_max_size() - width and height of r should be <= max_size
53 * @r: rect whose width and height will be modified
54 * @max_size: rect containing the maximum width and height
56 static inline void v4l2_rect_set_max_size(struct v4l2_rect *r,
57 const struct v4l2_rect *max_size)
59 if (r->width > max_size->width)
60 r->width = max_size->width;
61 if (r->height > max_size->height)
62 r->height = max_size->height;
65 /**
66 * v4l2_rect_map_inside()- r should be inside boundary.
67 * @r: rect that will be modified
68 * @boundary: rect containing the boundary for @r
70 static inline void v4l2_rect_map_inside(struct v4l2_rect *r,
71 const struct v4l2_rect *boundary)
73 v4l2_rect_set_max_size(r, boundary);
74 if (r->left < boundary->left)
75 r->left = boundary->left;
76 if (r->top < boundary->top)
77 r->top = boundary->top;
78 if (r->left + r->width > boundary->width)
79 r->left = boundary->width - r->width;
80 if (r->top + r->height > boundary->height)
81 r->top = boundary->height - r->height;
84 /**
85 * v4l2_rect_same_size() - return true if r1 has the same size as r2
86 * @r1: rectangle.
87 * @r2: rectangle.
89 * Return true if both rectangles have the same size.
91 static inline bool v4l2_rect_same_size(const struct v4l2_rect *r1,
92 const struct v4l2_rect *r2)
94 return r1->width == r2->width && r1->height == r2->height;
97 /**
98 * v4l2_rect_intersect() - calculate the intersection of two rects.
99 * @r: intersection of @r1 and @r2.
100 * @r1: rectangle.
101 * @r2: rectangle.
103 static inline void v4l2_rect_intersect(struct v4l2_rect *r,
104 const struct v4l2_rect *r1,
105 const struct v4l2_rect *r2)
107 int right, bottom;
109 r->top = max(r1->top, r2->top);
110 r->left = max(r1->left, r2->left);
111 bottom = min(r1->top + r1->height, r2->top + r2->height);
112 right = min(r1->left + r1->width, r2->left + r2->width);
113 r->height = max(0, bottom - r->top);
114 r->width = max(0, right - r->left);
118 * v4l2_rect_scale() - scale rect r by to/from
119 * @r: rect to be scaled.
120 * @from: from rectangle.
121 * @to: to rectangle.
123 * This scales rectangle @r horizontally by @to->width / @from->width and
124 * vertically by @to->height / @from->height.
126 * Typically @r is a rectangle inside @from and you want the rectangle as
127 * it would appear after scaling @from to @to. So the resulting @r will
128 * be the scaled rectangle inside @to.
130 static inline void v4l2_rect_scale(struct v4l2_rect *r,
131 const struct v4l2_rect *from,
132 const struct v4l2_rect *to)
134 if (from->width == 0 || from->height == 0) {
135 r->left = r->top = r->width = r->height = 0;
136 return;
138 r->left = (((r->left - from->left) * to->width) / from->width) & ~1;
139 r->width = ((r->width * to->width) / from->width) & ~1;
140 r->top = ((r->top - from->top) * to->height) / from->height;
141 r->height = (r->height * to->height) / from->height;
145 * v4l2_rect_overlap() - do r1 and r2 overlap?
146 * @r1: rectangle.
147 * @r2: rectangle.
149 * Returns true if @r1 and @r2 overlap.
151 static inline bool v4l2_rect_overlap(const struct v4l2_rect *r1,
152 const struct v4l2_rect *r2)
155 * IF the left side of r1 is to the right of the right side of r2 OR
156 * the left side of r2 is to the right of the right side of r1 THEN
157 * they do not overlap.
159 if (r1->left >= r2->left + r2->width ||
160 r2->left >= r1->left + r1->width)
161 return false;
163 * IF the top side of r1 is below the bottom of r2 OR
164 * the top side of r2 is below the bottom of r1 THEN
165 * they do not overlap.
167 if (r1->top >= r2->top + r2->height ||
168 r2->top >= r1->top + r1->height)
169 return false;
170 return true;
173 #endif