Skip to content

Commit

Permalink
media: v4l2-rect.h: fix v4l2_rect_map_inside() top/left adjustments
Browse files Browse the repository at this point in the history
boundary->width and boundary->height are sizes relative to
boundary->left and boundary->top coordinates, but they were not being
taken into consideration to adjust r->left and r->top, leading to the
following error:

Consider the follow as initial values for boundary and r:

struct v4l2_rect boundary = {
	.left = 100,
	.top = 100,
	.width = 800,
	.height = 600,
}

struct v4l2_rect r = {
	.left = 0,
	.top = 0,
	.width = 1920,
	.height = 960,
}

calling v4l2_rect_map_inside(&r, &boundary) was modifying r to:

r = {
	.left = 0,
	.top = 0,
	.width = 800,
	.height = 600,
}

Which is wrongly outside the boundary rectangle, because:

	v4l2_rect_set_max_size(r, boundary); // r->width = 800, r->height = 600
	...
	if (r->left + r->width > boundary->width) // true
		r->left = boundary->width - r->width; // r->left = 800 - 800
	if (r->top + r->height > boundary->height) // true
		r->top = boundary->height - r->height; // r->height = 600 - 600

Fix this by considering top/left coordinates from boundary.

Fixes: ac49de8 ("[media] v4l2-rect.h: new header with struct v4l2_rect helper functions")
Signed-off-by: Helen Koike <[email protected]>
Cc: <[email protected]>      # for v4.7 and up
Signed-off-by: Hans Verkuil <[email protected]>
Signed-off-by: Mauro Carvalho Chehab <[email protected]>
  • Loading branch information
helen-fornazier authored and mchehab committed Jan 8, 2020
1 parent 0f9be03 commit f51e50d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions include/media/v4l2-rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ static inline void v4l2_rect_map_inside(struct v4l2_rect *r,
r->left = boundary->left;
if (r->top < boundary->top)
r->top = boundary->top;
if (r->left + r->width > boundary->width)
r->left = boundary->width - r->width;
if (r->top + r->height > boundary->height)
r->top = boundary->height - r->height;
if (r->left + r->width > boundary->left + boundary->width)
r->left = boundary->left + boundary->width - r->width;
if (r->top + r->height > boundary->top + boundary->height)
r->top = boundary->top + boundary->height - r->height;
}

/**
Expand Down

0 comments on commit f51e50d

Please sign in to comment.