Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: components/favicon_base/select_favicon_frames.cc

Issue 2739173002: Always select best favicon bitmap (Closed)
Patch Set: WIP. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/favicon_base/select_favicon_frames.h" 5 #include "components/favicon_base/select_favicon_frames.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <limits> 9 #include <limits>
10 #include <map> 10 #include <map>
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 48
49 return bitmap; 49 return bitmap;
50 } 50 }
51 51
52 size_t GetCandidateIndexWithBestScore( 52 size_t GetCandidateIndexWithBestScore(
53 const std::vector<gfx::Size>& candidate_sizes, 53 const std::vector<gfx::Size>& candidate_sizes,
54 int desired_size, 54 int desired_size,
55 float* score) { 55 float* score) {
56 DCHECK_NE(desired_size, 0); 56 DCHECK_NE(desired_size, 0);
57 57
58 // Try to find an exact match.
59 for (size_t i = 0; i < candidate_sizes.size(); ++i) {
60 if (candidate_sizes[i].width() == desired_size &&
61 candidate_sizes[i].height() == desired_size) {
62 *score = 1;
63 return i;
64 }
65 }
66
67 // Huge favicon bitmaps often have a completely different visual style from
68 // smaller favicon bitmaps. Avoid them.
69 const int kHugeEdgeSize = desired_size * 8;
70
71 // Order of preference:
72 // 1) Bitmaps with width and height smaller than |kHugeEdgeSize|.
73 // 2) Bitmaps which need to be scaled down instead of up.
74 // 3) Bitmaps which do not need to be scaled as much.
75 size_t candidate_index = std::numeric_limits<size_t>::max(); 58 size_t candidate_index = std::numeric_limits<size_t>::max();
76 float candidate_score = 0; 59 float candidate_score = 0;
77 for (size_t i = 0; i < candidate_sizes.size(); ++i) { 60 for (size_t i = 0; i < candidate_sizes.size() && candidate_score != 1.0f;
78 float average_edge = 61 ++i) {
79 (candidate_sizes[i].width() + candidate_sizes[i].height()) / 2.0f; 62 float score = GetFaviconCandidateScore(candidate_sizes[i], desired_size);
80
81 float score = 0;
82 if (candidate_sizes[i].width() >= kHugeEdgeSize ||
83 candidate_sizes[i].height() >= kHugeEdgeSize) {
84 score = std::min(1.0f, desired_size / average_edge) * 0.01f;
85 } else if (candidate_sizes[i].width() >= desired_size &&
86 candidate_sizes[i].height() >= desired_size) {
87 score = desired_size / average_edge * 0.01f + 0.15f;
88 } else {
89 score = std::min(1.0f, average_edge / desired_size) * 0.01f + 0.1f;
90 }
91
92 if (candidate_index == std::numeric_limits<size_t>::max() || 63 if (candidate_index == std::numeric_limits<size_t>::max() ||
93 score > candidate_score) { 64 score > candidate_score) {
94 candidate_index = i; 65 candidate_index = i;
95 candidate_score = score; 66 candidate_score = score;
96 } 67 }
97 } 68 }
98 *score = candidate_score; 69 *score = candidate_score;
99 70
100 return candidate_index; 71 return candidate_index;
101 } 72 }
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 image_source->AddImageSkiaRep( 221 image_source->AddImageSkiaRep(
251 gfx::ImageSkiaRep(GetResizedBitmap(bitmaps[index], 222 gfx::ImageSkiaRep(GetResizedBitmap(bitmaps[index],
252 original_sizes[index], 223 original_sizes[index],
253 desired_sizes[i]), 224 desired_sizes[i]),
254 favicon_scales[i])); 225 favicon_scales[i]));
255 } 226 }
256 return gfx::ImageSkia(image_source, 227 return gfx::ImageSkia(image_source,
257 gfx::Size(desired_size_in_dip, desired_size_in_dip)); 228 gfx::Size(desired_size_in_dip, desired_size_in_dip));
258 } 229 }
259 230
231 gfx::ImageSkia CreateFaviconImageSkiaWithScaleFactors(
232 const std::vector<SkBitmap>& bitmaps,
233 const std::vector<gfx::Size>& original_sizes,
234 const std::vector<float>& desired_scale_factors,
235 int desired_size_in_dip) {
236 std::vector<int> desired_sizes;
237 std::vector<float> scale_factors = desired_scale_factors;
238
239 for (size_t i = 0; i < desired_scale_factors.size(); ++i) {
240 if (desired_scale_factors[i] == 0) {
241 desired_sizes.push_back(original_sizes[i].width());
242 scale_factors[i] = 1.0f * desired_sizes.back() / desired_size_in_dip;
243 } else {
244 desired_sizes.push_back(static_cast<int>(
245 ceil(desired_size_in_dip * desired_scale_factors[i])));
246 }
247 }
248
249 std::vector<SelectionResult> results;
250 // TODO(mastiz) / DONOTSUBMIT: Why is this actually needed? FaviconHandler
251 // should have selected the correct bitmaps.
252 GetCandidateIndicesWithBestScores(original_sizes, desired_sizes,
253 /*score=*/nullptr, &results);
254 if (results.size() == 0)
255 return gfx::ImageSkia();
256
257 FaviconImageSource* image_source = new FaviconImageSource;
258
259 for (size_t i = 0; i < results.size(); ++i) {
260 size_t index = results[i].index;
261 image_source->AddImageSkiaRep(gfx::ImageSkiaRep(
262 GetResizedBitmap(bitmaps[index], original_sizes[index],
263 desired_sizes[i]),
264 scale_factors[i]));
265 }
266 return gfx::ImageSkia(image_source,
267 gfx::Size(desired_size_in_dip, desired_size_in_dip));
268 }
269
260 void SelectFaviconFrameIndices(const std::vector<gfx::Size>& frame_pixel_sizes, 270 void SelectFaviconFrameIndices(const std::vector<gfx::Size>& frame_pixel_sizes,
261 const std::vector<int>& desired_sizes, 271 const std::vector<int>& desired_sizes,
262 std::vector<size_t>* best_indices, 272 std::vector<size_t>* best_indices,
263 float* match_score) { 273 float* match_score) {
264 std::vector<SelectionResult> results; 274 std::vector<SelectionResult> results;
265 GetCandidateIndicesWithBestScores( 275 GetCandidateIndicesWithBestScores(
266 frame_pixel_sizes, desired_sizes, match_score, &results); 276 frame_pixel_sizes, desired_sizes, match_score, &results);
267 277
268 std::set<size_t> already_added; 278 std::set<size_t> already_added;
269 for (size_t i = 0; i < results.size(); ++i) { 279 for (size_t i = 0; i < results.size(); ++i) {
270 size_t index = results[i].index; 280 size_t index = results[i].index;
271 // GetCandidateIndicesWithBestScores() will return duplicate indices if the 281 // GetCandidateIndicesWithBestScores() will return duplicate indices if the
272 // bitmap data with |frame_pixel_sizes[index]| should be used for multiple 282 // bitmap data with |frame_pixel_sizes[index]| should be used for multiple
273 // scale factors. Remove duplicates here such that |best_indices| contains 283 // scale factors. Remove duplicates here such that |best_indices| contains
274 // no duplicates. 284 // no duplicates.
275 if (already_added.find(index) == already_added.end()) { 285 if (already_added.find(index) == already_added.end()) {
276 already_added.insert(index); 286 already_added.insert(index);
277 best_indices->push_back(index); 287 best_indices->push_back(index);
278 } 288 }
279 } 289 }
280 } 290 }
291
292 float GetFaviconCandidateScore(const gfx::Size& candidate_size,
293 int desired_size) {
294 // Huge favicon bitmaps often have a completely different visual style from
295 // smaller favicon bitmaps. Avoid them.
296 const int kHugeEdgeSize = desired_size * 8;
297
298 float average_edge =
299 (candidate_size.width() + candidate_size.height()) / 2.0f;
300 if (candidate_size.width() == desired_size &&
301 candidate_size.height() == desired_size) {
302 return 1.0f;
303 } else if (candidate_size.width() >= kHugeEdgeSize ||
304 candidate_size.height() >= kHugeEdgeSize) {
305 return std::min(1.0f, desired_size / average_edge) * 0.01f;
306 } else if (candidate_size.width() >= desired_size &&
307 candidate_size.height() >= desired_size) {
308 return desired_size / average_edge * 0.01f + 0.15f;
309 } else {
310 return std::min(1.0f, average_edge / desired_size) * 0.01f + 0.1f;
311 }
312 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698