v4l2-rect.h 5.2 KB

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