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
3 // found in the LICENSE file.
5 #include "components/favicon_base/select_favicon_frames.h"
10 #include "skia/ext/image_operations.h"
11 #include "third_party/skia/include/core/SkCanvas.h"
12 #include "ui/gfx/image/image.h"
13 #include "ui/gfx/image/image_skia.h"
14 #include "ui/gfx/size.h"
18 size_t BiggestCandidate(const std::vector<gfx::Size>& candidate_sizes) {
20 int max_area = candidate_sizes[0].GetArea();
21 for (size_t i = 1; i < candidate_sizes.size(); ++i) {
22 int area = candidate_sizes[i].GetArea();
23 if (area > max_area) {
31 SkBitmap SampleNearestNeighbor(const SkBitmap& contents, int desired_size) {
33 bitmap.setConfig(SkBitmap::kARGB_8888_Config, desired_size, desired_size);
35 if (!contents.isOpaque())
36 bitmap.eraseARGB(0, 0, 0, 0);
39 SkCanvas canvas(bitmap);
40 SkRect dest(SkRect::MakeWH(desired_size, desired_size));
41 canvas.drawBitmapRect(contents, NULL, dest);
47 enum ResizeMethod { NONE, SAMPLE_NEAREST_NEIGHBOUR, LANCZOS };
49 size_t GetCandidateIndexWithBestScore(
50 const std::vector<gfx::Size>& candidate_sizes_in_pixel,
51 ui::ScaleFactor scale_factor,
52 int desired_size_in_dip,
54 ResizeMethod* resize_method) {
55 DCHECK_NE(desired_size_in_dip, 0);
57 float scale = ui::GetImageScale(scale_factor);
58 int desired_size_in_pixel =
59 static_cast<int>(desired_size_in_dip * scale + 0.5f);
61 // Try to find an exact match.
62 for (size_t i = 0; i < candidate_sizes_in_pixel.size(); ++i) {
63 if (candidate_sizes_in_pixel[i].width() == desired_size_in_pixel &&
64 candidate_sizes_in_pixel[i].height() == desired_size_in_pixel) {
66 *resize_method = NONE;
71 // Huge favicon bitmaps often have a completely different visual style from
72 // smaller favicon bitmaps. Avoid these favicon bitmaps when a favicon of
73 // gfx::kFaviconSize DIP is requested.
74 const int kHugeEdgeSizeInPixel = desired_size_in_pixel * 8;
76 // Order of preference:
77 // 1) Bitmaps with width and height smaller than |kHugeEdgeSizeInPixel|.
78 // 2) Bitmaps which need to be scaled down instead of up.
79 // 3) Bitmaps which do not need to be scaled as much.
80 size_t candidate_index = std::numeric_limits<size_t>::max();
81 float candidate_score = 0;
82 for (size_t i = 0; i < candidate_sizes_in_pixel.size(); ++i) {
83 float average_edge_in_pixel = (candidate_sizes_in_pixel[i].width() +
84 candidate_sizes_in_pixel[i].height()) /
88 if (candidate_sizes_in_pixel[i].width() >= kHugeEdgeSizeInPixel ||
89 candidate_sizes_in_pixel[i].height() >= kHugeEdgeSizeInPixel) {
91 std::min(1.0f, desired_size_in_pixel / average_edge_in_pixel) * 0.01f;
92 } else if (candidate_sizes_in_pixel[i].width() >= desired_size_in_pixel &&
93 candidate_sizes_in_pixel[i].height() >= desired_size_in_pixel) {
94 score = desired_size_in_pixel / average_edge_in_pixel * 0.01f + 0.15f;
96 score = std::min(1.0f, average_edge_in_pixel / desired_size_in_pixel) *
101 if (candidate_index == std::numeric_limits<size_t>::max() ||
102 score > candidate_score) {
104 candidate_score = score;
107 *score = candidate_score;
109 // Integer multiples are built using nearest neighbor sampling. Otherwise,
110 // Lanczos scaling is used.
111 const gfx::Size& candidate_size_in_pixel =
112 candidate_sizes_in_pixel[candidate_index];
113 if (candidate_size_in_pixel.IsEmpty()) {
114 *resize_method = NONE;
115 } else if (desired_size_in_pixel % candidate_size_in_pixel.width() == 0 &&
116 desired_size_in_pixel % candidate_size_in_pixel.height() == 0) {
117 *resize_method = SAMPLE_NEAREST_NEIGHBOUR;
119 *resize_method = LANCZOS;
121 return candidate_index;
124 // Represents the index of the best candidate for a |scale_factor| from the
125 // |candidate_sizes| passed into GetCandidateIndicesWithBestScores().
126 struct SelectionResult {
127 // index in |candidate_sizes| of the best candidate.
130 // The ScaleFactor for which |index| is the best candidate.
131 ui::ScaleFactor scale_factor;
133 // How the bitmap data that the bitmap with |candidate_sizes[index]| should
134 // be resized for displaying in the UI.
135 ResizeMethod resize_method;
138 void GetCandidateIndicesWithBestScores(
139 const std::vector<gfx::Size>& candidate_sizes,
140 const std::vector<ui::ScaleFactor>& scale_factors,
143 std::vector<SelectionResult>* results) {
144 if (candidate_sizes.empty()) {
150 if (desired_size == 0) {
151 // Just return the biggest image available.
152 SelectionResult result;
153 result.index = BiggestCandidate(candidate_sizes);
154 result.scale_factor = ui::SCALE_FACTOR_100P;
155 result.resize_method = NONE;
156 results->push_back(result);
162 float total_score = 0;
163 for (size_t i = 0; i < scale_factors.size(); ++i) {
165 SelectionResult result;
166 result.scale_factor = scale_factors[i];
167 result.index = GetCandidateIndexWithBestScore(candidate_sizes,
171 &result.resize_method);
172 results->push_back(result);
173 total_score += score;
177 *match_score = total_score / scale_factors.size();
180 // Resize |source_bitmap| using |resize_method|.
181 SkBitmap GetResizedBitmap(const SkBitmap& source_bitmap,
182 int desired_size_in_dip,
183 ui::ScaleFactor scale_factor,
184 ResizeMethod resize_method) {
185 float scale = ui::GetImageScale(scale_factor);
186 int desired_size_in_pixel =
187 static_cast<int>(desired_size_in_dip * scale + 0.5f);
189 switch (resize_method) {
191 return source_bitmap;
192 case SAMPLE_NEAREST_NEIGHBOUR:
193 return SampleNearestNeighbor(source_bitmap, desired_size_in_pixel);
195 return skia::ImageOperations::Resize(
197 skia::ImageOperations::RESIZE_LANCZOS3,
198 desired_size_in_pixel,
199 desired_size_in_pixel);
201 return source_bitmap;
206 const float kSelectFaviconFramesInvalidScore = -1.0f;
208 gfx::ImageSkia SelectFaviconFrames(
209 const std::vector<SkBitmap>& bitmaps,
210 const std::vector<gfx::Size>& original_sizes,
211 const std::vector<ui::ScaleFactor>& scale_factors,
213 float* match_score) {
214 std::vector<SelectionResult> results;
215 GetCandidateIndicesWithBestScores(
216 original_sizes, scale_factors, desired_size, match_score, &results);
218 gfx::ImageSkia multi_image;
219 for (size_t i = 0; i < results.size(); ++i) {
220 const SelectionResult& result = results[i];
221 SkBitmap resized_bitmap = GetResizedBitmap(bitmaps[result.index],
224 result.resize_method);
225 multi_image.AddRepresentation(gfx::ImageSkiaRep(
226 resized_bitmap, ui::GetImageScale(result.scale_factor)));
231 void SelectFaviconFrameIndices(
232 const std::vector<gfx::Size>& frame_pixel_sizes,
233 const std::vector<ui::ScaleFactor>& scale_factors,
235 std::vector<size_t>* best_indices,
236 float* match_score) {
237 std::vector<SelectionResult> results;
238 GetCandidateIndicesWithBestScores(
239 frame_pixel_sizes, scale_factors, desired_size, match_score, &results);
241 std::set<size_t> already_added;
242 for (size_t i = 0; i < results.size(); ++i) {
243 size_t index = results[i].index;
244 // GetCandidateIndicesWithBestScores() will return duplicate indices if the
245 // bitmap data with |frame_pixel_sizes[index]| should be used for multiple
246 // scale factors. Remove duplicates here such that |best_indices| contains
248 if (already_added.find(index) == already_added.end()) {
249 already_added.insert(index);
250 best_indices->push_back(index);