Chromium Code Reviews| Index: ui/gfx/image/image_family.cc |
| diff --git a/ui/gfx/image/image_family.cc b/ui/gfx/image/image_family.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..539cc9a607fc133aad1ef842591efb166221951e |
| --- /dev/null |
| +++ b/ui/gfx/image/image_family.cc |
| @@ -0,0 +1,123 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/gfx/image/image_family.h" |
| + |
| +#include <cmath> |
| + |
| +#include "ui/gfx/image/image.h" |
| +#include "ui/gfx/image/image_skia.h" |
| + |
| +namespace gfx { |
| + |
| +class Size; |
|
pkotwicz
2013/03/27 19:04:53
Remove this?
Matt Giuca
2013/03/28 03:54:03
Actually converted to an #include since I'm using
|
| + |
| +ImageFamily::ImageFamily() {} |
| +ImageFamily::~ImageFamily() {} |
| + |
| +void ImageFamily::Add(const gfx::Image& image) |
| +{ |
|
Robert Sesek
2013/03/27 17:42:50
Opening braces go on the line of the method defini
Matt Giuca
2013/03/28 03:54:03
Done.
|
| + const gfx::Size& size = image.Size(); |
| + int width = size.width(); |
| + int height = size.height(); |
|
pkotwicz
2013/03/27 19:04:53
Nit: I believe gfx::Size::IsEmpty() checks if eith
Matt Giuca
2013/03/28 03:54:03
Done.
|
| + // If either width or height is 0, both are. |
| + if (height == 0 || width == 0) { |
| + map_[MapKey(1.0f, 0)] = image; |
| + } else { |
| + float aspect = static_cast<float>(width) / height; |
| + DCHECK(aspect > 0.0f); |
|
Robert Sesek
2013/03/27 17:42:50
DCHECK_GT
Matt Giuca
2013/03/28 03:54:03
Done (and all of the other DCHECKs too, except the
|
| + map_[MapKey(aspect, width)] = image; |
| + } |
| +} |
| + |
| +void ImageFamily::Add(const gfx::ImageSkia& image_skia) |
| +{ |
| + gfx::Image image(image_skia); |
| + Add(image); |
|
pkotwicz
2013/03/27 19:04:53
Nit, merge the two lines into one
Matt Giuca
2013/03/28 03:54:03
Done.
|
| +} |
| + |
| +const gfx::Image* ImageFamily::Get(int width, int height) const |
|
Robert Sesek
2013/03/27 17:42:50
This method is rather large. Would it make sense t
Matt Giuca
2013/03/28 03:54:03
Done. I'll discuss in the free-form comments.
|
| +{ |
| + if (map_.empty()) |
| + return NULL; |
| + |
| + // If either width or height is 0, both are. |
| + float desired_aspect; |
| + if (height == 0 || width == 0) { |
| + desired_aspect = 1.0f; |
| + height = 0; |
| + width = 0; |
| + } else { |
| + desired_aspect = static_cast<float>(width) / height; |
| + } |
| + DCHECK(desired_aspect > 0.0f); |
|
Robert Sesek
2013/03/27 17:42:50
_GT
Matt Giuca
2013/03/28 03:54:03
Done.
|
| + |
| + // Get iterator to images >= and < the desired aspect ratio and size. |
| + std::map<MapKey, gfx::Image>::const_iterator greater_or_equal = |
| + map_.lower_bound(MapKey(desired_aspect, width)); |
| + if (greater_or_equal != map_.end() && |
| + greater_or_equal->first.aspect() == desired_aspect) { |
| + // Exact same aspect ratio, and we have found the smallest image of the same |
| + // size or greater. This is ideal. |
| + return &greater_or_equal->second; |
| + } |
| + |
| + float closest_aspect; // Closest aspect ratio to the desired one. |
| + if (greater_or_equal != map_.begin()) { |
| + std::map<MapKey, gfx::Image>::const_iterator less_than = |
| + greater_or_equal; |
| + --less_than; |
| + if (less_than->first.aspect() == desired_aspect) { |
| + // Exact same aspect ratio, and we have found the largest image smaller |
| + // than desired. |
| + return &less_than->second; |
| + } |
| + |
| + float thinner_aspect = less_than->first.aspect(); |
| + DCHECK(thinner_aspect > 0.0f); |
|
Robert Sesek
2013/03/27 17:42:50
_GT
Matt Giuca
2013/03/28 03:54:03
Done.
|
| + DCHECK(thinner_aspect < desired_aspect); |
|
Robert Sesek
2013/03/27 17:42:50
_LT
Matt Giuca
2013/03/28 03:54:03
Done.
|
| + closest_aspect = thinner_aspect; |
| + if (greater_or_equal != map_.end()) { |
| + float wider_aspect = greater_or_equal->first.aspect(); |
| + DCHECK(wider_aspect > desired_aspect); |
|
Robert Sesek
2013/03/27 17:42:50
_GT
Matt Giuca
2013/03/28 03:54:03
Done.
|
| + if ((wider_aspect / desired_aspect) < (desired_aspect / thinner_aspect)) |
| + closest_aspect = wider_aspect; |
| + } |
| + } else { |
| + // No aspect ratio is less than or equal to desired_aspect. |
| + DCHECK(greater_or_equal != map_.end()); |
| + closest_aspect = greater_or_equal->first.aspect(); |
| + DCHECK(closest_aspect > desired_aspect); |
|
Robert Sesek
2013/03/27 17:42:50
_GT
Matt Giuca
2013/03/28 03:54:03
Done.
|
| + } |
| + |
| + int desired_width; |
| + if (desired_aspect >= closest_aspect) { |
| + desired_width = closest_aspect == 0 ? height : width; |
| + } else { |
| + desired_width = static_cast<int>(ceilf(height * closest_aspect)); |
| + } |
| + |
| + // Search again, this time with the aspect ratio we know exists. |
| + greater_or_equal = map_.lower_bound(MapKey(closest_aspect, desired_width)); |
| + if (greater_or_equal != map_.end() && |
| + greater_or_equal->first.aspect() == closest_aspect) { |
| + // We have found the smallest image of the same size or greater. |
| + return &greater_or_equal->second; |
| + } |
| + |
| + DCHECK(greater_or_equal != map_.begin()); |
| + std::map<MapKey, gfx::Image>::const_iterator less_than = greater_or_equal; |
| + --less_than; |
| + // This must be true because there must be at least one image with |
| + // closest_aspect. |
| + DCHECK(less_than->first.aspect() == closest_aspect); |
| + // We have found the largest image smaller than desired. |
| + return &less_than->second; |
| +} |
| + |
| +ImageFamily::const_iterator::const_iterator( |
| + const std::map<MapKey, gfx::Image>::const_iterator& other) |
| + : map_iterator_(other) {} |
| + |
| +} // namespace gfx |