1 /* SPDX-License-Identifier: GPL-2.0-only */ << 2 /* 1 /* 3 * v4l2-rect.h - v4l2_rect helper functions 2 * v4l2-rect.h - v4l2_rect helper functions 4 * 3 * 5 * Copyright 2014 Cisco Systems, Inc. and/or i 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. 6 */ 18 */ 7 19 8 #ifndef _V4L2_RECT_H_ 20 #ifndef _V4L2_RECT_H_ 9 #define _V4L2_RECT_H_ 21 #define _V4L2_RECT_H_ 10 22 11 #include <linux/videodev2.h> 23 #include <linux/videodev2.h> 12 24 13 /** 25 /** 14 * v4l2_rect_set_size_to() - copy the width/he 26 * v4l2_rect_set_size_to() - copy the width/height values. 15 * @r: rect whose width and height fields will 27 * @r: rect whose width and height fields will be set 16 * @size: rect containing the width and height 28 * @size: rect containing the width and height fields you need. 17 */ 29 */ 18 static inline void v4l2_rect_set_size_to(struc 30 static inline void v4l2_rect_set_size_to(struct v4l2_rect *r, 19 const 31 const struct v4l2_rect *size) 20 { 32 { 21 r->width = size->width; 33 r->width = size->width; 22 r->height = size->height; 34 r->height = size->height; 23 } 35 } 24 36 25 /** 37 /** 26 * v4l2_rect_set_min_size() - width and height 38 * v4l2_rect_set_min_size() - width and height of r should be >= min_size. 27 * @r: rect whose width and height will be mod 39 * @r: rect whose width and height will be modified 28 * @min_size: rect containing the minimal widt 40 * @min_size: rect containing the minimal width and height 29 */ 41 */ 30 static inline void v4l2_rect_set_min_size(stru 42 static inline void v4l2_rect_set_min_size(struct v4l2_rect *r, 31 cons 43 const struct v4l2_rect *min_size) 32 { 44 { 33 if (r->width < min_size->width) 45 if (r->width < min_size->width) 34 r->width = min_size->width; 46 r->width = min_size->width; 35 if (r->height < min_size->height) 47 if (r->height < min_size->height) 36 r->height = min_size->height; 48 r->height = min_size->height; 37 } 49 } 38 50 39 /** 51 /** 40 * v4l2_rect_set_max_size() - width and height 52 * v4l2_rect_set_max_size() - width and height of r should be <= max_size 41 * @r: rect whose width and height will be mod 53 * @r: rect whose width and height will be modified 42 * @max_size: rect containing the maximum widt 54 * @max_size: rect containing the maximum width and height 43 */ 55 */ 44 static inline void v4l2_rect_set_max_size(stru 56 static inline void v4l2_rect_set_max_size(struct v4l2_rect *r, 45 cons 57 const struct v4l2_rect *max_size) 46 { 58 { 47 if (r->width > max_size->width) 59 if (r->width > max_size->width) 48 r->width = max_size->width; 60 r->width = max_size->width; 49 if (r->height > max_size->height) 61 if (r->height > max_size->height) 50 r->height = max_size->height; 62 r->height = max_size->height; 51 } 63 } 52 64 53 /** 65 /** 54 * v4l2_rect_map_inside()- r should be inside 66 * v4l2_rect_map_inside()- r should be inside boundary. 55 * @r: rect that will be modified 67 * @r: rect that will be modified 56 * @boundary: rect containing the boundary for 68 * @boundary: rect containing the boundary for @r 57 */ 69 */ 58 static inline void v4l2_rect_map_inside(struct 70 static inline void v4l2_rect_map_inside(struct v4l2_rect *r, 59 const 71 const struct v4l2_rect *boundary) 60 { 72 { 61 v4l2_rect_set_max_size(r, boundary); 73 v4l2_rect_set_max_size(r, boundary); 62 if (r->left < boundary->left) 74 if (r->left < boundary->left) 63 r->left = boundary->left; 75 r->left = boundary->left; 64 if (r->top < boundary->top) 76 if (r->top < boundary->top) 65 r->top = boundary->top; 77 r->top = boundary->top; 66 if (r->left + r->width > boundary->lef !! 78 if (r->left + r->width > boundary->width) 67 r->left = boundary->left + bou !! 79 r->left = boundary->width - r->width; 68 if (r->top + r->height > boundary->top !! 80 if (r->top + r->height > boundary->height) 69 r->top = boundary->top + bound !! 81 r->top = boundary->height - r->height; 70 } 82 } 71 83 72 /** 84 /** 73 * v4l2_rect_same_size() - return true if r1 h 85 * v4l2_rect_same_size() - return true if r1 has the same size as r2 74 * @r1: rectangle. 86 * @r1: rectangle. 75 * @r2: rectangle. 87 * @r2: rectangle. 76 * 88 * 77 * Return true if both rectangles have the sam 89 * Return true if both rectangles have the same size. 78 */ 90 */ 79 static inline bool v4l2_rect_same_size(const s 91 static inline bool v4l2_rect_same_size(const struct v4l2_rect *r1, 80 const s 92 const struct v4l2_rect *r2) 81 { 93 { 82 return r1->width == r2->width && r1->h 94 return r1->width == r2->width && r1->height == r2->height; 83 } 95 } 84 96 85 /** 97 /** 86 * v4l2_rect_same_position() - return true if << 87 * @r1: rectangle. << 88 * @r2: rectangle. << 89 * << 90 * Return true if both rectangles have the sam << 91 */ << 92 static inline bool v4l2_rect_same_position(con << 93 con << 94 { << 95 return r1->top == r2->top && r1->left << 96 } << 97 << 98 /** << 99 * v4l2_rect_equal() - return true if r1 equal << 100 * @r1: rectangle. << 101 * @r2: rectangle. << 102 * << 103 * Return true if both rectangles have the sam << 104 */ << 105 static inline bool v4l2_rect_equal(const struc << 106 const struc << 107 { << 108 return v4l2_rect_same_size(r1, r2) && << 109 } << 110 << 111 /** << 112 * v4l2_rect_intersect() - calculate the inter 98 * v4l2_rect_intersect() - calculate the intersection of two rects. 113 * @r: intersection of @r1 and @r2. 99 * @r: intersection of @r1 and @r2. 114 * @r1: rectangle. 100 * @r1: rectangle. 115 * @r2: rectangle. 101 * @r2: rectangle. 116 */ 102 */ 117 static inline void v4l2_rect_intersect(struct 103 static inline void v4l2_rect_intersect(struct v4l2_rect *r, 118 const s 104 const struct v4l2_rect *r1, 119 const s 105 const struct v4l2_rect *r2) 120 { 106 { 121 int right, bottom; 107 int right, bottom; 122 108 123 r->top = max(r1->top, r2->top); 109 r->top = max(r1->top, r2->top); 124 r->left = max(r1->left, r2->left); 110 r->left = max(r1->left, r2->left); 125 bottom = min(r1->top + r1->height, r2- 111 bottom = min(r1->top + r1->height, r2->top + r2->height); 126 right = min(r1->left + r1->width, r2-> 112 right = min(r1->left + r1->width, r2->left + r2->width); 127 r->height = max(0, bottom - r->top); 113 r->height = max(0, bottom - r->top); 128 r->width = max(0, right - r->left); 114 r->width = max(0, right - r->left); 129 } 115 } 130 116 131 /** 117 /** 132 * v4l2_rect_scale() - scale rect r by to/from 118 * v4l2_rect_scale() - scale rect r by to/from 133 * @r: rect to be scaled. 119 * @r: rect to be scaled. 134 * @from: from rectangle. 120 * @from: from rectangle. 135 * @to: to rectangle. 121 * @to: to rectangle. 136 * 122 * 137 * This scales rectangle @r horizontally by @t 123 * This scales rectangle @r horizontally by @to->width / @from->width and 138 * vertically by @to->height / @from->height. 124 * vertically by @to->height / @from->height. 139 * 125 * 140 * Typically @r is a rectangle inside @from an 126 * Typically @r is a rectangle inside @from and you want the rectangle as 141 * it would appear after scaling @from to @to. 127 * it would appear after scaling @from to @to. So the resulting @r will 142 * be the scaled rectangle inside @to. 128 * be the scaled rectangle inside @to. 143 */ 129 */ 144 static inline void v4l2_rect_scale(struct v4l2 130 static inline void v4l2_rect_scale(struct v4l2_rect *r, 145 const struc 131 const struct v4l2_rect *from, 146 const struc 132 const struct v4l2_rect *to) 147 { 133 { 148 if (from->width == 0 || from->height = 134 if (from->width == 0 || from->height == 0) { 149 r->left = r->top = r->width = 135 r->left = r->top = r->width = r->height = 0; 150 return; 136 return; 151 } 137 } 152 r->left = (((r->left - from->left) * t 138 r->left = (((r->left - from->left) * to->width) / from->width) & ~1; 153 r->width = ((r->width * to->width) / f 139 r->width = ((r->width * to->width) / from->width) & ~1; 154 r->top = ((r->top - from->top) * to->h 140 r->top = ((r->top - from->top) * to->height) / from->height; 155 r->height = (r->height * to->height) / 141 r->height = (r->height * to->height) / from->height; 156 } 142 } 157 143 158 /** 144 /** 159 * v4l2_rect_overlap() - do r1 and r2 overlap? 145 * v4l2_rect_overlap() - do r1 and r2 overlap? 160 * @r1: rectangle. 146 * @r1: rectangle. 161 * @r2: rectangle. 147 * @r2: rectangle. 162 * 148 * 163 * Returns true if @r1 and @r2 overlap. 149 * Returns true if @r1 and @r2 overlap. 164 */ 150 */ 165 static inline bool v4l2_rect_overlap(const str 151 static inline bool v4l2_rect_overlap(const struct v4l2_rect *r1, 166 const str 152 const struct v4l2_rect *r2) 167 { 153 { 168 /* 154 /* 169 * IF the left side of r1 is to the ri 155 * IF the left side of r1 is to the right of the right side of r2 OR 170 * the left side of r2 is to the ri 156 * the left side of r2 is to the right of the right side of r1 THEN 171 * they do not overlap. 157 * they do not overlap. 172 */ 158 */ 173 if (r1->left >= r2->left + r2->width | 159 if (r1->left >= r2->left + r2->width || 174 r2->left >= r1->left + r1->width) 160 r2->left >= r1->left + r1->width) 175 return false; 161 return false; 176 /* 162 /* 177 * IF the top side of r1 is below the 163 * IF the top side of r1 is below the bottom of r2 OR 178 * the top side of r2 is below the 164 * the top side of r2 is below the bottom of r1 THEN 179 * they do not overlap. 165 * they do not overlap. 180 */ 166 */ 181 if (r1->top >= r2->top + r2->height || 167 if (r1->top >= r2->top + r2->height || 182 r2->top >= r1->top + r1->height) 168 r2->top >= r1->top + r1->height) 183 return false; 169 return false; 184 return true; << 185 } << 186 << 187 /** << 188 * v4l2_rect_enclosed() - is r1 enclosed in r2 << 189 * @r1: rectangle. << 190 * @r2: rectangle. << 191 * << 192 * Returns true if @r1 is enclosed in @r2. << 193 */ << 194 static inline bool v4l2_rect_enclosed(struct v << 195 struct v << 196 { << 197 if (r1->left < r2->left || r1->top < r << 198 return false; << 199 if (r1->left + r1->width > r2->left + << 200 return false; << 201 if (r1->top + r1->height > r2->top + r << 202 return false; << 203 << 204 return true; 170 return true; 205 } 171 } 206 172 207 #endif 173 #endif 208 174
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.