Chromium Code Reviews| Index: media/base/video_util.cc |
| diff --git a/media/base/video_util.cc b/media/base/video_util.cc |
| index d7946743eae091cef3f9952c4a0d6cb5acd4ab40..4721f3f890575bb040981151c56162be227adba2 100644 |
| --- a/media/base/video_util.cc |
| +++ b/media/base/video_util.cc |
| @@ -270,6 +270,24 @@ void RotatePlaneByPixels( |
| } |
| } |
| +// Common logic for the letterboxing and scale-within/scale-encompassing |
| +// functions. Scales |size| to either fit within or encompass |target|, |
| +// depending on whether |fit_within_target| is true. |
| +template<bool fit_within_target> |
|
DaleCurtis
2015/05/09 01:50:21
Seems like this is just a static function with an
miu
2015/05/09 03:52:56
Done.
|
| +static gfx::Size ScaleSizeToTarget(const gfx::Size& size, |
| + const gfx::Size& target) { |
| + if (size.IsEmpty()) |
| + return gfx::Size(); // Corner case: Aspect ratio is undefined. |
| + |
| + // Scale |size|, preserving aspect ratio. |
| + const int64 x = static_cast<int64>(size.width()) * target.height(); |
| + const int64 y = static_cast<int64>(size.height()) * target.width(); |
| + const bool use_target_width = fit_within_target ? (y < x) : (x < y); |
| + return use_target_width ? |
| + gfx::Size(target.width(), static_cast<int>(y / size.width())) : |
| + gfx::Size(static_cast<int>(x / size.height()), target.height()); |
| +} |
| + |
| gfx::Rect ComputeLetterboxRegion(const gfx::Rect& bounds, |
| const gfx::Size& content) { |
| // If |content| has an undefined aspect ratio, let's not try to divide by |
| @@ -277,19 +295,34 @@ gfx::Rect ComputeLetterboxRegion(const gfx::Rect& bounds, |
| if (content.IsEmpty()) |
| return gfx::Rect(); |
| - int64 x = static_cast<int64>(content.width()) * bounds.height(); |
| - int64 y = static_cast<int64>(content.height()) * bounds.width(); |
| - |
| - gfx::Size letterbox(bounds.width(), bounds.height()); |
| - if (y < x) |
| - letterbox.set_height(static_cast<int>(y / content.width())); |
| - else |
| - letterbox.set_width(static_cast<int>(x / content.height())); |
| gfx::Rect result = bounds; |
| - result.ClampToCenteredSize(letterbox); |
| + result.ClampToCenteredSize(ScaleSizeToTarget<true>(content, bounds.size())); |
| return result; |
| } |
| +gfx::Size ScaleSizeToFitWithinTarget(const gfx::Size& size, |
| + const gfx::Size& target) { |
| + return ScaleSizeToTarget<true>(size, target); |
| +} |
| + |
| +gfx::Size ScaleSizeToEncompassTarget(const gfx::Size& size, |
| + const gfx::Size& target) { |
| + return ScaleSizeToTarget<false>(size, target); |
| +} |
| + |
| +gfx::Size PadToMatchAspectRatio(const gfx::Size& size, |
| + const gfx::Size& target) { |
| + if (target.IsEmpty()) |
| + return gfx::Size(); // Aspect ratio is undefined. |
| + |
| + const int64 x = static_cast<int64>(size.width()) * target.height(); |
| + const int64 y = static_cast<int64>(size.height()) * target.width(); |
| + if (x < y) |
| + return gfx::Size(static_cast<int>(y / target.height()), size.height()); |
| + else |
|
DaleCurtis
2015/05/09 01:50:21
Remove else?
miu
2015/05/09 03:52:56
Done.
|
| + return gfx::Size(size.width(), static_cast<int>(x / target.width())); |
| +} |
| + |
| void CopyRGBToVideoFrame(const uint8* source, |
| int stride, |
| const gfx::Rect& region_in_frame, |