1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/favicon_base/select_favicon_frames.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <map>
#include <memory>
#include <set>
#include <utility>
#include "base/containers/contains.h"
#include "components/favicon_base/favicon_util.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkImage.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "ui/gfx/image/image_skia_source.h"
namespace {
size_t BiggestCandidate(const std::vector<gfx::Size>& candidate_sizes) {
size_t max_index = 0;
int max_area = candidate_sizes[0].GetArea();
for (size_t i = 1; i < candidate_sizes.size(); ++i) {
int area = candidate_sizes[i].GetArea();
if (area > max_area) {
max_area = area;
max_index = i;
}
}
return max_index;
}
SkBitmap SampleNearestNeighbor(const SkBitmap& contents, int desired_size) {
SkBitmap bitmap;
bitmap.allocN32Pixels(desired_size, desired_size);
if (!contents.isOpaque())
bitmap.eraseARGB(0, 0, 0, 0);
{
SkCanvas canvas(bitmap, SkSurfaceProps{});
canvas.drawImageRect(contents.asImage(),
SkRect::MakeIWH(desired_size, desired_size),
SkSamplingOptions());
}
return bitmap;
}
size_t GetCandidateIndexWithBestScore(
const std::vector<gfx::Size>& candidate_sizes,
int desired_size,
float* output_score) {
DCHECK_NE(desired_size, 0);
// Try to find an exact match.
for (size_t i = 0; i < candidate_sizes.size(); ++i) {
if (candidate_sizes[i].width() == desired_size &&
candidate_sizes[i].height() == desired_size) {
*output_score = 1;
return i;
}
}
// Huge favicon bitmaps often have a completely different visual style from
// smaller favicon bitmaps. Avoid them.
const int kHugeEdgeSize = desired_size * 8;
// Order of preference:
// 1) Bitmaps with width and height smaller than |kHugeEdgeSize|.
// 2) Bitmaps which need to be scaled down instead of up.
// 3) Bitmaps which do not need to be scaled as much.
size_t candidate_index = std::numeric_limits<size_t>::max();
float candidate_score = 0;
for (size_t i = 0; i < candidate_sizes.size(); ++i) {
float average_edge =
(candidate_sizes[i].width() + candidate_sizes[i].height()) / 2.0f;
float score = 0;
if (candidate_sizes[i].width() >= kHugeEdgeSize ||
candidate_sizes[i].height() >= kHugeEdgeSize) {
score = std::min(1.0f, desired_size / average_edge) * 0.01f;
} else if (candidate_sizes[i].width() >= desired_size &&
candidate_sizes[i].height() >= desired_size) {
score = desired_size / average_edge * 0.01f + 0.15f;
} else {
score = std::min(1.0f, average_edge / desired_size) * 0.01f + 0.1f;
}
if (candidate_index == std::numeric_limits<size_t>::max() ||
score > candidate_score) {
candidate_index = i;
candidate_score = score;
}
}
*output_score = candidate_score;
return candidate_index;
}
// Represents the index of the best candidate for |desired_size| from the
// |candidate_sizes| passed into GetCandidateIndicesWithBestScores().
struct SelectionResult {
// index in |candidate_sizes| of the best candidate.
size_t index;
// The desired size for which |index| is the best candidate.
int desired_size;
};
void GetCandidateIndicesWithBestScores(
const std::vector<gfx::Size>& candidate_sizes,
const std::vector<int>& desired_sizes,
float* match_score,
std::vector<SelectionResult>* results) {
if (candidate_sizes.empty() || desired_sizes.empty()) {
if (match_score)
*match_score = 0.0f;
return;
}
if (base::Contains(desired_sizes, 0)) {
// Just return the biggest image available.
SelectionResult result;
result.index = BiggestCandidate(candidate_sizes);
result.desired_size = 0;
results->push_back(result);
if (match_score)
*match_score = 1.0f;
return;
}
float total_score = 0;
for (size_t i = 0; i < desired_sizes.size(); ++i) {
float score;
SelectionResult result;
result.desired_size = desired_sizes[i];
result.index = GetCandidateIndexWithBestScore(
candidate_sizes, result.desired_size, &score);
results->push_back(result);
total_score += score;
}
if (match_score)
*match_score = total_score / desired_sizes.size();
}
// Resize |source_bitmap|
SkBitmap GetResizedBitmap(const SkBitmap& source_bitmap,
gfx::Size original_size,
int desired_size_in_pixel) {
if (desired_size_in_pixel == 0 ||
(original_size.width() == desired_size_in_pixel &&
original_size.height() == desired_size_in_pixel)) {
return source_bitmap;
}
if (desired_size_in_pixel % original_size.width() == 0 &&
desired_size_in_pixel % original_size.height() == 0) {
return SampleNearestNeighbor(source_bitmap, desired_size_in_pixel);
}
return skia::ImageOperations::Resize(source_bitmap,
skia::ImageOperations::RESIZE_LANCZOS3,
desired_size_in_pixel,
desired_size_in_pixel);
}
class FaviconImageSource : public gfx::ImageSkiaSource {
public:
FaviconImageSource() = default;
FaviconImageSource(const FaviconImageSource&) = delete;
FaviconImageSource& operator=(const FaviconImageSource&) = delete;
~FaviconImageSource() override = default;
// gfx::ImageSkiaSource:
gfx::ImageSkiaRep GetImageForScale(float scale) override {
const gfx::ImageSkiaRep* rep = nullptr;
// gfx::ImageSkia passes one of the resource scale factors. The source
// should return:
// 1) The ImageSkiaRep with the highest scale if all available
// scales are smaller than |scale|.
// 2) The ImageSkiaRep with the smallest one that is larger than |scale|.
// Note: Keep this logic consistent with the PNGImageSource in
// ui/gfx/image.cc.
// TODO(oshima): consolidate these logic into one place.
for (std::vector<gfx::ImageSkiaRep>::const_iterator iter =
image_skia_reps_.begin();
iter != image_skia_reps_.end(); ++iter) {
if ((*iter).scale() == scale)
return (*iter);
if (!rep || rep->scale() < (*iter).scale())
rep = &(*iter);
if (rep->scale() >= scale)
break;
}
DCHECK(rep);
return rep ? *rep : gfx::ImageSkiaRep();
}
void AddImageSkiaRep(const gfx::ImageSkiaRep& rep) {
image_skia_reps_.push_back(rep);
}
private:
std::vector<gfx::ImageSkiaRep> image_skia_reps_;
};
} // namespace
const float kSelectFaviconFramesInvalidScore = -1.0f;
gfx::ImageSkia CreateFaviconImageSkia(
const std::vector<SkBitmap>& bitmaps,
const std::vector<gfx::Size>& original_sizes,
int desired_size_in_dip,
float* score) {
DCHECK_EQ(bitmaps.size(), original_sizes.size());
const std::vector<float>& favicon_scales = favicon_base::GetFaviconScales();
std::vector<int> desired_sizes;
if (desired_size_in_dip == 0) {
desired_sizes.push_back(0);
} else {
for (auto iter = favicon_scales.begin(); iter != favicon_scales.end();
++iter) {
desired_sizes.push_back(
static_cast<int>(ceil(desired_size_in_dip * (*iter))));
}
}
std::vector<SelectionResult> results;
GetCandidateIndicesWithBestScores(original_sizes,
desired_sizes,
score,
&results);
if (results.size() == 0)
return gfx::ImageSkia();
if (desired_size_in_dip == 0) {
size_t index = results[0].index;
return gfx::ImageSkia::CreateFromBitmap(bitmaps[index], 1.0f);
}
auto image_source = std::make_unique<FaviconImageSource>();
for (size_t i = 0; i < results.size(); ++i) {
size_t index = results[i].index;
image_source->AddImageSkiaRep(
gfx::ImageSkiaRep(GetResizedBitmap(bitmaps[index],
original_sizes[index],
desired_sizes[i]),
favicon_scales[i]));
}
return gfx::ImageSkia(std::move(image_source),
gfx::Size(desired_size_in_dip, desired_size_in_dip));
}
void SelectFaviconFrameIndices(const std::vector<gfx::Size>& frame_pixel_sizes,
const std::vector<int>& desired_sizes,
std::vector<size_t>* best_indices,
float* match_score) {
std::vector<SelectionResult> results;
GetCandidateIndicesWithBestScores(
frame_pixel_sizes, desired_sizes, match_score, &results);
if (!best_indices)
return;
std::set<size_t> already_added;
for (size_t i = 0; i < results.size(); ++i) {
size_t index = results[i].index;
// GetCandidateIndicesWithBestScores() will return duplicate indices if the
// bitmap data with |frame_pixel_sizes[index]| should be used for multiple
// scale factors. Remove duplicates here such that |best_indices| contains
// no duplicates.
if (already_added.find(index) == already_added.end()) {
already_added.insert(index);
best_indices->push_back(index);
}
}
}
|