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