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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 (candidate_sizes[i].width() + candidate_sizes[i].height()) / 2.0f; | 81 (candidate_sizes[i].width() + candidate_sizes[i].height()) / 2.0f; |
82 | 82 |
83 float score = 0; | 83 float score = 0; |
84 if (candidate_sizes[i].width() >= kHugeEdgeSize || | 84 if (candidate_sizes[i].width() >= kHugeEdgeSize || |
85 candidate_sizes[i].height() >= kHugeEdgeSize) { | 85 candidate_sizes[i].height() >= kHugeEdgeSize) { |
86 score = std::min(1.0f, desired_size / average_edge) * 0.01f; | 86 score = std::min(1.0f, desired_size / average_edge) * 0.01f; |
87 } else if (candidate_sizes[i].width() >= desired_size && | 87 } else if (candidate_sizes[i].width() >= desired_size && |
88 candidate_sizes[i].height() >= desired_size) { | 88 candidate_sizes[i].height() >= desired_size) { |
89 score = desired_size / average_edge * 0.01f + 0.15f; | 89 score = desired_size / average_edge * 0.01f + 0.15f; |
90 } else { | 90 } else { |
91 score = std::min(1.0f, average_edge / desired_size) * | 91 score = std::min(1.0f, average_edge / desired_size) * 0.01f + 0.1f; |
92 0.01f + | |
93 0.1f; | |
94 } | 92 } |
95 | 93 |
96 if (candidate_index == std::numeric_limits<size_t>::max() || | 94 if (candidate_index == std::numeric_limits<size_t>::max() || |
97 score > candidate_score) { | 95 score > candidate_score) { |
98 candidate_index = i; | 96 candidate_index = i; |
99 candidate_score = score; | 97 candidate_score = score; |
100 } | 98 } |
101 } | 99 } |
102 *score = candidate_score; | 100 *score = candidate_score; |
103 | 101 |
(...skipping 29 matching lines...) Expand all Loading... |
133 const std::vector<gfx::Size>& candidate_sizes, | 131 const std::vector<gfx::Size>& candidate_sizes, |
134 const std::vector<int>& desired_sizes, | 132 const std::vector<int>& desired_sizes, |
135 float* match_score, | 133 float* match_score, |
136 std::vector<SelectionResult>* results) { | 134 std::vector<SelectionResult>* results) { |
137 if (candidate_sizes.empty() || desired_sizes.empty()) { | 135 if (candidate_sizes.empty() || desired_sizes.empty()) { |
138 if (match_score) | 136 if (match_score) |
139 *match_score = 0.0f; | 137 *match_score = 0.0f; |
140 return; | 138 return; |
141 } | 139 } |
142 | 140 |
143 std::vector<int>::const_iterator zero_size_it = std::find( | 141 std::vector<int>::const_iterator zero_size_it = |
144 desired_sizes.begin(), desired_sizes.end(), 0); | 142 std::find(desired_sizes.begin(), desired_sizes.end(), 0); |
145 if (zero_size_it != desired_sizes.end()) { | 143 if (zero_size_it != desired_sizes.end()) { |
146 // Just return the biggest image available. | 144 // Just return the biggest image available. |
147 SelectionResult result; | 145 SelectionResult result; |
148 result.index = BiggestCandidate(candidate_sizes); | 146 result.index = BiggestCandidate(candidate_sizes); |
149 result.desired_size = 0; | 147 result.desired_size = 0; |
150 result.resize_method = NONE; | 148 result.resize_method = NONE; |
151 results->push_back(result); | 149 results->push_back(result); |
152 if (match_score) | 150 if (match_score) |
153 *match_score = 1.0f; | 151 *match_score = 1.0f; |
154 return; | 152 return; |
155 } | 153 } |
156 | 154 |
157 float total_score = 0; | 155 float total_score = 0; |
158 for (size_t i = 0; i < desired_sizes.size(); ++i) { | 156 for (size_t i = 0; i < desired_sizes.size(); ++i) { |
159 float score; | 157 float score; |
160 SelectionResult result; | 158 SelectionResult result; |
161 result.desired_size = desired_sizes[i]; | 159 result.desired_size = desired_sizes[i]; |
162 result.index = GetCandidateIndexWithBestScore(candidate_sizes, | 160 result.index = GetCandidateIndexWithBestScore( |
163 result.desired_size, | 161 candidate_sizes, result.desired_size, &score, &result.resize_method); |
164 &score, | |
165 &result.resize_method); | |
166 results->push_back(result); | 162 results->push_back(result); |
167 total_score += score; | 163 total_score += score; |
168 } | 164 } |
169 | 165 |
170 if (match_score) | 166 if (match_score) |
171 *match_score = total_score / desired_sizes.size(); | 167 *match_score = total_score / desired_sizes.size(); |
172 } | 168 } |
173 | 169 |
174 // Resize |source_bitmap| using |resize_method|. | 170 // Resize |source_bitmap| using |resize_method|. |
175 SkBitmap GetResizedBitmap(const SkBitmap& source_bitmap, | 171 SkBitmap GetResizedBitmap(const SkBitmap& source_bitmap, |
(...skipping 11 matching lines...) Expand all Loading... |
187 desired_size, | 183 desired_size, |
188 desired_size); | 184 desired_size); |
189 } | 185 } |
190 return source_bitmap; | 186 return source_bitmap; |
191 } | 187 } |
192 | 188 |
193 } // namespace | 189 } // namespace |
194 | 190 |
195 const float kSelectFaviconFramesInvalidScore = -1.0f; | 191 const float kSelectFaviconFramesInvalidScore = -1.0f; |
196 | 192 |
197 gfx::ImageSkia SelectFaviconFrames( | 193 gfx::ImageSkia SelectFaviconFrames(const std::vector<SkBitmap>& bitmaps, |
198 const std::vector<SkBitmap>& bitmaps, | 194 const std::vector<gfx::Size>& original_sizes, |
199 const std::vector<gfx::Size>& original_sizes, | 195 const std::vector<float>& favicon_scales, |
200 const std::vector<ui::ScaleFactor>& scale_factors, | 196 int desired_size_in_dip, |
201 int desired_size_in_dip, | 197 float* match_score) { |
202 float* match_score) { | |
203 std::vector<int> desired_sizes; | 198 std::vector<int> desired_sizes; |
204 std::map<int, float> scale_map; | 199 std::map<int, float> scale_map; |
205 if (desired_size_in_dip == 0) { | 200 if (desired_size_in_dip == 0) { |
206 desired_sizes.push_back(0); | 201 desired_sizes.push_back(0); |
207 scale_map[0] = 1.0f; | 202 scale_map[0] = 1.0f; |
208 } else { | 203 } else { |
209 for (size_t i = 0; i < scale_factors.size(); ++i) { | 204 for (size_t i = 0; i < favicon_scales.size(); ++i) { |
210 float scale = ui::GetScaleForScaleFactor(scale_factors[i]); | 205 float scale = favicon_scales[i]; |
211 int desired_size = ceil(desired_size_in_dip * scale); | 206 int desired_size = ceil(desired_size_in_dip * scale); |
212 desired_sizes.push_back(desired_size); | 207 desired_sizes.push_back(desired_size); |
213 scale_map[desired_size] = scale; | 208 scale_map[desired_size] = scale; |
214 } | 209 } |
215 } | 210 } |
216 | 211 |
217 std::vector<SelectionResult> results; | 212 std::vector<SelectionResult> results; |
218 GetCandidateIndicesWithBestScores( | 213 GetCandidateIndicesWithBestScores( |
219 original_sizes, desired_sizes, match_score, &results); | 214 original_sizes, desired_sizes, match_score, &results); |
220 | 215 |
221 gfx::ImageSkia multi_image; | 216 gfx::ImageSkia multi_image; |
222 for (size_t i = 0; i < results.size(); ++i) { | 217 for (size_t i = 0; i < results.size(); ++i) { |
223 const SelectionResult& result = results[i]; | 218 const SelectionResult& result = results[i]; |
224 SkBitmap resized_bitmap = GetResizedBitmap(bitmaps[result.index], | 219 SkBitmap resized_bitmap = GetResizedBitmap( |
225 result.desired_size, | 220 bitmaps[result.index], result.desired_size, result.resize_method); |
226 result.resize_method); | |
227 | 221 |
228 std::map<int, float>::const_iterator it = scale_map.find( | 222 std::map<int, float>::const_iterator it = |
229 result.desired_size); | 223 scale_map.find(result.desired_size); |
230 DCHECK(it != scale_map.end()); | 224 DCHECK(it != scale_map.end()); |
231 float scale = it->second; | 225 float scale = it->second; |
232 multi_image.AddRepresentation(gfx::ImageSkiaRep(resized_bitmap, scale)); | 226 multi_image.AddRepresentation(gfx::ImageSkiaRep(resized_bitmap, scale)); |
233 } | 227 } |
234 return multi_image; | 228 return multi_image; |
235 } | 229 } |
236 | 230 |
237 void SelectFaviconFrameIndices( | 231 void SelectFaviconFrameIndices(const std::vector<gfx::Size>& frame_pixel_sizes, |
238 const std::vector<gfx::Size>& frame_pixel_sizes, | 232 const std::vector<int>& desired_sizes, |
239 const std::vector<int>& desired_sizes, | 233 std::vector<size_t>* best_indices, |
240 std::vector<size_t>* best_indices, | 234 float* match_score) { |
241 float* match_score) { | |
242 std::vector<SelectionResult> results; | 235 std::vector<SelectionResult> results; |
243 GetCandidateIndicesWithBestScores( | 236 GetCandidateIndicesWithBestScores( |
244 frame_pixel_sizes, desired_sizes, match_score, &results); | 237 frame_pixel_sizes, desired_sizes, match_score, &results); |
245 | 238 |
246 std::set<size_t> already_added; | 239 std::set<size_t> already_added; |
247 for (size_t i = 0; i < results.size(); ++i) { | 240 for (size_t i = 0; i < results.size(); ++i) { |
248 size_t index = results[i].index; | 241 size_t index = results[i].index; |
249 // GetCandidateIndicesWithBestScores() will return duplicate indices if the | 242 // GetCandidateIndicesWithBestScores() will return duplicate indices if the |
250 // 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 |
251 // scale factors. Remove duplicates here such that |best_indices| contains | 244 // scale factors. Remove duplicates here such that |best_indices| contains |
252 // no duplicates. | 245 // no duplicates. |
253 if (already_added.find(index) == already_added.end()) { | 246 if (already_added.find(index) == already_added.end()) { |
254 already_added.insert(index); | 247 already_added.insert(index); |
255 best_indices->push_back(index); | 248 best_indices->push_back(index); |
256 } | 249 } |
257 } | 250 } |
258 } | 251 } |
OLD | NEW |