This source file includes following definitions.
- Add
- Add
- GetBest
- GetClosestAspect
- GetBest
- GetWithExactAspect
#include "ui/gfx/image/image_family.h"
#include <cmath>
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/size.h"
namespace gfx {
ImageFamily::const_iterator::const_iterator() {}
ImageFamily::const_iterator::const_iterator(const const_iterator& other)
: map_iterator_(other.map_iterator_) {}
ImageFamily::const_iterator::const_iterator(
const std::map<MapKey, gfx::Image>::const_iterator& other)
: map_iterator_(other) {}
ImageFamily::const_iterator::~const_iterator() {}
ImageFamily::ImageFamily() {}
ImageFamily::~ImageFamily() {}
void ImageFamily::Add(const gfx::Image& image) {
gfx::Size size = image.Size();
if (size.IsEmpty()) {
map_[MapKey(1.0f, 0)] = image;
} else {
float aspect = static_cast<float>(size.width()) / size.height();
DCHECK_GT(aspect, 0.0f);
map_[MapKey(aspect, size.width())] = image;
}
}
void ImageFamily::Add(const gfx::ImageSkia& image_skia) {
Add(gfx::Image(image_skia));
}
const gfx::Image* ImageFamily::GetBest(int width, int height) const {
if (map_.empty())
return NULL;
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_GT(desired_aspect, 0.0f);
float closest_aspect = GetClosestAspect(desired_aspect);
int desired_width = closest_aspect <= desired_aspect ?
width : static_cast<int>(ceilf(height * closest_aspect));
return GetWithExactAspect(closest_aspect, desired_width);
}
float ImageFamily::GetClosestAspect(float desired_aspect) const {
std::map<MapKey, gfx::Image>::const_iterator greater_or_equal =
map_.lower_bound(MapKey(desired_aspect, 0));
if (greater_or_equal != map_.end() &&
greater_or_equal->first.aspect() == desired_aspect) {
return desired_aspect;
}
if (greater_or_equal != map_.begin()) {
std::map<MapKey, gfx::Image>::const_iterator less_than =
greater_or_equal;
--less_than;
float thinner_aspect = less_than->first.aspect();
DCHECK_GT(thinner_aspect, 0.0f);
DCHECK_LT(thinner_aspect, desired_aspect);
if (greater_or_equal != map_.end()) {
float wider_aspect = greater_or_equal->first.aspect();
DCHECK_GT(wider_aspect, desired_aspect);
if ((wider_aspect / desired_aspect) < (desired_aspect / thinner_aspect))
return wider_aspect;
}
return thinner_aspect;
} else {
DCHECK(greater_or_equal != map_.end());
float wider_aspect = greater_or_equal->first.aspect();
DCHECK_GT(wider_aspect, desired_aspect);
return wider_aspect;
}
}
const gfx::Image* ImageFamily::GetBest(const gfx::Size& size) const {
return GetBest(size.width(), size.height());
}
const gfx::Image* ImageFamily::GetWithExactAspect(float aspect,
int width) const {
std::map<MapKey, gfx::Image>::const_iterator greater_or_equal =
map_.lower_bound(MapKey(aspect, width));
if (greater_or_equal != map_.end() &&
greater_or_equal->first.aspect() == aspect) {
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;
DCHECK_EQ(less_than->first.aspect(), aspect);
return &less_than->second;
}
}