| OLD | NEW |
| 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 Loading... |
| 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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 // GetCandidateIndicesWithBestScores() will return duplicate indices if the | 242 // GetCandidateIndicesWithBestScores() will return duplicate indices if the |
| 272 // bitmap data with |frame_pixel_sizes[index]| should be used for multiple | 243 // bitmap data with |frame_pixel_sizes[index]| should be used for multiple |
| 273 // scale factors. Remove duplicates here such that |best_indices| contains | 244 // scale factors. Remove duplicates here such that |best_indices| contains |
| 274 // no duplicates. | 245 // no duplicates. |
| 275 if (already_added.find(index) == already_added.end()) { | 246 if (already_added.find(index) == already_added.end()) { |
| 276 already_added.insert(index); | 247 already_added.insert(index); |
| 277 best_indices->push_back(index); | 248 best_indices->push_back(index); |
| 278 } | 249 } |
| 279 } | 250 } |
| 280 } | 251 } |
| 252 |
| 253 float GetFaviconCandidateScore(const gfx::Size& candidate_size, |
| 254 int desired_size) { |
| 255 // Huge favicon bitmaps often have a completely different visual style from |
| 256 // smaller favicon bitmaps. Avoid them. |
| 257 const int kHugeEdgeSize = desired_size * 8; |
| 258 |
| 259 float average_edge = |
| 260 (candidate_size.width() + candidate_size.height()) / 2.0f; |
| 261 if (candidate_size.width() == desired_size && |
| 262 candidate_size.height() == desired_size) { |
| 263 return 1.0f; |
| 264 } else if (candidate_size.width() >= kHugeEdgeSize || |
| 265 candidate_size.height() >= kHugeEdgeSize) { |
| 266 return std::min(1.0f, desired_size / average_edge) * 0.01f; |
| 267 } else if (candidate_size.width() >= desired_size && |
| 268 candidate_size.height() >= desired_size) { |
| 269 return desired_size / average_edge * 0.01f + 0.15f; |
| 270 } else { |
| 271 return std::min(1.0f, average_edge / desired_size) * 0.01f + 0.1f; |
| 272 } |
| 273 } |
| OLD | NEW |