blob: d2125f0cc7cd5e3e5ea699d278fec33ed9d97e6e [file] [log] [blame]
Hans Verkuilac49de82016-04-03 16:42:42 -03001/*
2 * v4l2-rect.h - v4l2_rect helper functions
3 *
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
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.
9 *
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.
18 */
19
20#ifndef _V4L2_RECT_H_
21#define _V4L2_RECT_H_
22
23#include <linux/videodev2.h>
24
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.
29 */
30static inline void v4l2_rect_set_size_to(struct v4l2_rect *r,
31 const struct v4l2_rect *size)
32{
33 r->width = size->width;
34 r->height = size->height;
35}
36
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
41 */
42static inline void v4l2_rect_set_min_size(struct v4l2_rect *r,
43 const struct v4l2_rect *min_size)
44{
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;
49}
50
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
55 */
56static inline void v4l2_rect_set_max_size(struct v4l2_rect *r,
57 const struct v4l2_rect *max_size)
58{
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;
63}
64
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
69 */
70static inline void v4l2_rect_map_inside(struct v4l2_rect *r,
71 const struct v4l2_rect *boundary)
72{
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;
82}
83
84/**
85 * v4l2_rect_same_size() - return true if r1 has the same size as r2
86 * @r1: rectangle.
87 * @r2: rectangle.
88 *
89 * Return true if both rectangles have the same size.
90 */
91static inline bool v4l2_rect_same_size(const struct v4l2_rect *r1,
92 const struct v4l2_rect *r2)
93{
94 return r1->width == r2->width && r1->height == r2->height;
95}
96
97/**
98 * v4l2_rect_intersect() - calculate the intersection of two rects.
99 * @r: intersection of @r1 and @r2.
100 * @r1: rectangle.
101 * @r2: rectangle.
102 */
103static inline void v4l2_rect_intersect(struct v4l2_rect *r,
104 const struct v4l2_rect *r1,
105 const struct v4l2_rect *r2)
106{
107 int right, bottom;
108
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);
115}
116
117/**
118 * v4l2_rect_scale() - scale rect r by to/from
119 * @r: rect to be scaled.
120 * @from: from rectangle.
121 * @to: to rectangle.
122 *
123 * This scales rectangle @r horizontally by @to->width / @from->width and
124 * vertically by @to->height / @from->height.
125 *
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.
129 */
130static inline void v4l2_rect_scale(struct v4l2_rect *r,
131 const struct v4l2_rect *from,
132 const struct v4l2_rect *to)
133{
134 if (from->width == 0 || from->height == 0) {
135 r->left = r->top = r->width = r->height = 0;
136 return;
137 }
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;
142}
143
144/**
145 * v4l2_rect_overlap() - do r1 and r2 overlap?
146 * @r1: rectangle.
147 * @r2: rectangle.
148 *
149 * Returns true if @r1 and @r2 overlap.
150 */
151static inline bool v4l2_rect_overlap(const struct v4l2_rect *r1,
152 const struct v4l2_rect *r2)
153{
154 /*
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.
158 */
159 if (r1->left >= r2->left + r2->width ||
160 r2->left >= r1->left + r1->width)
161 return false;
162 /*
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.
166 */
167 if (r1->top >= r2->top + r2->height ||
168 r2->top >= r1->top + r1->height)
169 return false;
170 return true;
171}
172
173#endif