Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/favicon/select_favicon_frames.h" | 5 #include "chrome/browser/favicon/select_favicon_frames.h" |
| 6 | 6 |
| 7 #include "skia/ext/image_operations.h" | 7 #include "skia/ext/image_operations.h" |
| 8 #include "third_party/skia/include/core/SkCanvas.h" | 8 #include "third_party/skia/include/core/SkCanvas.h" |
| 9 #include "ui/gfx/image/image.h" | 9 #include "ui/gfx/image/image.h" |
| 10 #include "ui/gfx/image/image_skia.h" | 10 #include "ui/gfx/image/image_skia.h" |
| 11 #include "ui/gfx/size.h" | 11 #include "ui/gfx/size.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 void SizesFromBitmaps(const std::vector<SkBitmap>& bitmaps, | 15 void SizesFromBitmaps(const std::vector<SkBitmap>& bitmaps, |
| 16 std::vector<gfx::Size>* sizes) { | 16 std::vector<gfx::Size>* sizes) { |
| 17 for (size_t i = 0; i < bitmaps.size(); ++i) | 17 for (size_t i = 0; i < bitmaps.size(); ++i) |
| 18 sizes->push_back(gfx::Size(bitmaps[i].width(), bitmaps[i].height())); | 18 sizes->push_back(gfx::Size(bitmaps[i].width(), bitmaps[i].height())); |
| 19 } | 19 } |
| 20 | 20 |
| 21 void SizesFromFaviconBitmapIDSizes( | |
|
sky
2012/09/04 20:52:29
Add description.
| |
| 22 const std::vector<history::FaviconBitmapIDSize>& bitmap_id_sizes, | |
| 23 std::vector<gfx::Size>* sizes) { | |
| 24 for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) | |
| 25 sizes->push_back(bitmap_id_sizes[i].pixel_size); | |
| 26 } | |
| 27 | |
| 21 size_t BiggestCandidate(const std::vector<gfx::Size>& candidate_sizes) { | 28 size_t BiggestCandidate(const std::vector<gfx::Size>& candidate_sizes) { |
| 22 size_t max_index = 0; | 29 size_t max_index = 0; |
| 23 int max_area = candidate_sizes[0].GetArea(); | 30 int max_area = candidate_sizes[0].GetArea(); |
| 24 for (size_t i = 1; i < candidate_sizes.size(); ++i) { | 31 for (size_t i = 1; i < candidate_sizes.size(); ++i) { |
| 25 int area = candidate_sizes[i].GetArea(); | 32 int area = candidate_sizes[i].GetArea(); |
| 26 if (area > max_area) { | 33 if (area > max_area) { |
| 27 max_area = area; | 34 max_area = area; |
| 28 max_index = i; | 35 max_index = i; |
| 29 } | 36 } |
| 30 } | 37 } |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 } | 139 } |
| 133 } | 140 } |
| 134 *score = 0.1f; | 141 *score = 0.1f; |
| 135 // c) Else, from the biggest smaller variant. | 142 // c) Else, from the biggest smaller variant. |
| 136 if (candidate_index == -1) { | 143 if (candidate_index == -1) { |
| 137 *score = 0; | 144 *score = 0; |
| 138 candidate_index = BiggestCandidate(candidate_sizes); | 145 candidate_index = BiggestCandidate(candidate_sizes); |
| 139 } | 146 } |
| 140 | 147 |
| 141 const gfx::Size& candidate_size = candidate_sizes[candidate_index]; | 148 const gfx::Size& candidate_size = candidate_sizes[candidate_index]; |
| 142 bool is_integer_multiple = desired_size % candidate_size.width() == 0 && | 149 if (candidate_size.IsEmpty()) { |
| 143 desired_size % candidate_size.height() == 0; | 150 *resize_method = NONE; |
| 144 *resize_method = is_integer_multiple ? SAMPLE_NEAREST_NEIGHBOUR : LANCZOS; | 151 } else if (desired_size % candidate_size.width() == 0 && |
| 152 desired_size % candidate_size.height() == 0) { | |
| 153 *resize_method = SAMPLE_NEAREST_NEIGHBOUR; | |
| 154 } else { | |
| 155 *resize_method = LANCZOS; | |
| 156 } | |
| 145 return candidate_index; | 157 return candidate_index; |
| 146 } | 158 } |
| 147 | 159 |
| 148 // Represents the index of the best candidate for a |scale_factor| from the | 160 // Represents the index of the best candidate for a |scale_factor| from the |
| 149 // |candidate_sizes| passed into GetCandidateIndicesWithBestScores(). | 161 // |candidate_sizes| passed into GetCandidateIndicesWithBestScores(). |
| 150 struct SelectionResult { | 162 struct SelectionResult { |
| 151 // index in |candidate_sizes| of the best candidate. | 163 // index in |candidate_sizes| of the best candidate. |
| 152 size_t index; | 164 size_t index; |
| 153 | 165 |
| 154 // The ScaleFactor for which |index| is the best candidate. | 166 // The ScaleFactor for which |index| is the best candidate. |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 239 gfx::ImageSkia multi_image; | 251 gfx::ImageSkia multi_image; |
| 240 for (size_t i = 0; i < results.size(); ++i) { | 252 for (size_t i = 0; i < results.size(); ++i) { |
| 241 const SelectionResult& result = results[i]; | 253 const SelectionResult& result = results[i]; |
| 242 SkBitmap resized_bitmap = GetResizedBitmap(bitmaps[result.index], | 254 SkBitmap resized_bitmap = GetResizedBitmap(bitmaps[result.index], |
| 243 desired_size, result.scale_factor, result.resize_method); | 255 desired_size, result.scale_factor, result.resize_method); |
| 244 multi_image.AddRepresentation( | 256 multi_image.AddRepresentation( |
| 245 gfx::ImageSkiaRep(resized_bitmap, result.scale_factor)); | 257 gfx::ImageSkiaRep(resized_bitmap, result.scale_factor)); |
| 246 } | 258 } |
| 247 return multi_image; | 259 return multi_image; |
| 248 } | 260 } |
| 261 | |
| 262 void SelectFaviconBitmapIDs( | |
| 263 const std::vector<history::FaviconBitmapIDSize>& bitmap_id_sizes, | |
| 264 const std::vector<ui::ScaleFactor>& scale_factors, | |
| 265 int desired_size, | |
| 266 std::vector<history::FaviconBitmapID>* filtered_favicon_bitmap_ids, | |
| 267 float* match_score) { | |
| 268 std::vector<gfx::Size> candidate_sizes; | |
| 269 SizesFromFaviconBitmapIDSizes(bitmap_id_sizes, &candidate_sizes); | |
| 270 | |
| 271 std::vector<SelectionResult> results; | |
| 272 GetCandidateIndicesWithBestScores(candidate_sizes, scale_factors, | |
| 273 desired_size, match_score, &results); | |
| 274 | |
| 275 std::set<history::FaviconBitmapID> already_added; | |
| 276 for (size_t i = 0; i < results.size(); ++i) { | |
| 277 const SelectionResult& result = results[i]; | |
| 278 history::FaviconBitmapID bitmap_id = | |
| 279 bitmap_id_sizes[result.index].bitmap_id; | |
| 280 if (already_added.find(bitmap_id) == already_added.end()) { | |
| 281 already_added.insert(bitmap_id); | |
| 282 filtered_favicon_bitmap_ids->push_back(bitmap_id); | |
| 283 } | |
| 284 } | |
| 285 } | |
| OLD | NEW |