v4l2-rect.h 4.6 KB

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