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

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

Issue 246893004: Moves SelectFaviconFrames from history to favicon_base component. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed 64 bits v2. Created 6 years, 7 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
« no previous file with comments | « components/favicon_base/select_favicon_frames.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/history/select_favicon_frames.h" 5 #include "components/favicon_base/select_favicon_frames.h"
6 6
7 #include <limits>
7 #include <set> 8 #include <set>
8 9
9 #include "skia/ext/image_operations.h" 10 #include "skia/ext/image_operations.h"
10 #include "third_party/skia/include/core/SkCanvas.h" 11 #include "third_party/skia/include/core/SkCanvas.h"
11 #include "ui/gfx/image/image.h" 12 #include "ui/gfx/image/image.h"
12 #include "ui/gfx/image/image_skia.h" 13 #include "ui/gfx/image/image_skia.h"
13 #include "ui/gfx/size.h" 14 #include "ui/gfx/size.h"
14 15
15 namespace { 16 namespace {
16 17
17 size_t BiggestCandidate(const std::vector<gfx::Size>& candidate_sizes) { 18 size_t BiggestCandidate(const std::vector<gfx::Size>& candidate_sizes) {
18 size_t max_index = 0; 19 size_t max_index = 0;
19 int max_area = candidate_sizes[0].GetArea(); 20 int max_area = candidate_sizes[0].GetArea();
20 for (size_t i = 1; i < candidate_sizes.size(); ++i) { 21 for (size_t i = 1; i < candidate_sizes.size(); ++i) {
21 int area = candidate_sizes[i].GetArea(); 22 int area = candidate_sizes[i].GetArea();
22 if (area > max_area) { 23 if (area > max_area) {
23 max_area = area; 24 max_area = area;
24 max_index = i; 25 max_index = i;
25 } 26 }
26 } 27 }
27 return max_index; 28 return max_index;
28 } 29 }
29 30
30 SkBitmap SampleNearestNeighbor(const SkBitmap& contents, int desired_size) { 31 SkBitmap SampleNearestNeighbor(const SkBitmap& contents, int desired_size) {
31 SkBitmap bitmap; 32 SkBitmap bitmap;
32 bitmap.setConfig( 33 bitmap.setConfig(SkBitmap::kARGB_8888_Config, desired_size, desired_size);
33 SkBitmap::kARGB_8888_Config, desired_size, desired_size);
34 bitmap.allocPixels(); 34 bitmap.allocPixels();
35 if (!contents.isOpaque()) 35 if (!contents.isOpaque())
36 bitmap.eraseARGB(0, 0, 0, 0); 36 bitmap.eraseARGB(0, 0, 0, 0);
37 37
38 { 38 {
39 SkCanvas canvas(bitmap); 39 SkCanvas canvas(bitmap);
40 SkRect dest(SkRect::MakeWH(desired_size, desired_size)); 40 SkRect dest(SkRect::MakeWH(desired_size, desired_size));
41 canvas.drawBitmapRect(contents, NULL, dest); 41 canvas.drawBitmapRect(contents, NULL, dest);
42 } 42 }
43 43
44 return bitmap; 44 return bitmap;
45 } 45 }
46 46
47 enum ResizeMethod { 47 enum ResizeMethod { NONE, SAMPLE_NEAREST_NEIGHBOUR, LANCZOS };
48 NONE,
49 SAMPLE_NEAREST_NEIGHBOUR,
50 LANCZOS
51 };
52 48
53 size_t GetCandidateIndexWithBestScore( 49 size_t GetCandidateIndexWithBestScore(
54 const std::vector<gfx::Size>& candidate_sizes_in_pixel, 50 const std::vector<gfx::Size>& candidate_sizes_in_pixel,
55 ui::ScaleFactor scale_factor, 51 ui::ScaleFactor scale_factor,
56 int desired_size_in_dip, 52 int desired_size_in_dip,
57 float* score, 53 float* score,
58 ResizeMethod* resize_method) { 54 ResizeMethod* resize_method) {
59 DCHECK_NE(desired_size_in_dip, 0); 55 DCHECK_NE(desired_size_in_dip, 0);
60 56
61 float scale = ui::GetImageScale(scale_factor); 57 float scale = ui::GetImageScale(scale_factor);
(...skipping 12 matching lines...) Expand all
74 70
75 // Huge favicon bitmaps often have a completely different visual style from 71 // Huge favicon bitmaps often have a completely different visual style from
76 // smaller favicon bitmaps. Avoid these favicon bitmaps when a favicon of 72 // smaller favicon bitmaps. Avoid these favicon bitmaps when a favicon of
77 // gfx::kFaviconSize DIP is requested. 73 // gfx::kFaviconSize DIP is requested.
78 const int kHugeEdgeSizeInPixel = desired_size_in_pixel * 8; 74 const int kHugeEdgeSizeInPixel = desired_size_in_pixel * 8;
79 75
80 // Order of preference: 76 // Order of preference:
81 // 1) Bitmaps with width and height smaller than |kHugeEdgeSizeInPixel|. 77 // 1) Bitmaps with width and height smaller than |kHugeEdgeSizeInPixel|.
82 // 2) Bitmaps which need to be scaled down instead of up. 78 // 2) Bitmaps which need to be scaled down instead of up.
83 // 3) Bitmaps which do not need to be scaled as much. 79 // 3) Bitmaps which do not need to be scaled as much.
84 int candidate_index = -1; 80 size_t candidate_index = std::numeric_limits<size_t>::max();
85 float candidate_score = 0; 81 float candidate_score = 0;
86 for (size_t i = 0; i < candidate_sizes_in_pixel.size(); ++i) { 82 for (size_t i = 0; i < candidate_sizes_in_pixel.size(); ++i) {
87 float average_edge_in_pixel = (candidate_sizes_in_pixel[i].width() + 83 float average_edge_in_pixel = (candidate_sizes_in_pixel[i].width() +
88 candidate_sizes_in_pixel[i].height()) / 2.0f; 84 candidate_sizes_in_pixel[i].height()) /
85 2.0f;
89 86
90 float score = 0; 87 float score = 0;
91 if (candidate_sizes_in_pixel[i].width() >= kHugeEdgeSizeInPixel || 88 if (candidate_sizes_in_pixel[i].width() >= kHugeEdgeSizeInPixel ||
92 candidate_sizes_in_pixel[i].height() >= kHugeEdgeSizeInPixel) { 89 candidate_sizes_in_pixel[i].height() >= kHugeEdgeSizeInPixel) {
93 score = std::min(1.0f, desired_size_in_pixel / average_edge_in_pixel) * 90 score =
94 0.01f; 91 std::min(1.0f, desired_size_in_pixel / average_edge_in_pixel) * 0.01f;
95 } else if (candidate_sizes_in_pixel[i].width() >= desired_size_in_pixel && 92 } else if (candidate_sizes_in_pixel[i].width() >= desired_size_in_pixel &&
96 candidate_sizes_in_pixel[i].height() >= desired_size_in_pixel) { 93 candidate_sizes_in_pixel[i].height() >= desired_size_in_pixel) {
97 score = desired_size_in_pixel / average_edge_in_pixel * 0.01f + 0.15f; 94 score = desired_size_in_pixel / average_edge_in_pixel * 0.01f + 0.15f;
98 } else { 95 } else {
99 score = std::min(1.0f, average_edge_in_pixel / desired_size_in_pixel) * 96 score = std::min(1.0f, average_edge_in_pixel / desired_size_in_pixel) *
100 0.01f + 0.1f; 97 0.01f +
98 0.1f;
101 } 99 }
102 100
103 if (candidate_index == -1 || score > candidate_score) { 101 if (candidate_index == std::numeric_limits<size_t>::max() ||
102 score > candidate_score) {
104 candidate_index = i; 103 candidate_index = i;
105 candidate_score = score; 104 candidate_score = score;
106 } 105 }
107 } 106 }
108 *score = candidate_score; 107 *score = candidate_score;
109 108
110 // Integer multiples are built using nearest neighbor sampling. Otherwise, 109 // Integer multiples are built using nearest neighbor sampling. Otherwise,
111 // Lanczos scaling is used. 110 // Lanczos scaling is used.
112 const gfx::Size& candidate_size_in_pixel = 111 const gfx::Size& candidate_size_in_pixel =
113 candidate_sizes_in_pixel[candidate_index]; 112 candidate_sizes_in_pixel[candidate_index];
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 *match_score = 1.0f; 158 *match_score = 1.0f;
160 return; 159 return;
161 } 160 }
162 161
163 float total_score = 0; 162 float total_score = 0;
164 for (size_t i = 0; i < scale_factors.size(); ++i) { 163 for (size_t i = 0; i < scale_factors.size(); ++i) {
165 float score; 164 float score;
166 SelectionResult result; 165 SelectionResult result;
167 result.scale_factor = scale_factors[i]; 166 result.scale_factor = scale_factors[i];
168 result.index = GetCandidateIndexWithBestScore(candidate_sizes, 167 result.index = GetCandidateIndexWithBestScore(candidate_sizes,
169 result.scale_factor, desired_size, &score, &result.resize_method); 168 result.scale_factor,
169 desired_size,
170 &score,
171 &result.resize_method);
170 results->push_back(result); 172 results->push_back(result);
171 total_score += score; 173 total_score += score;
172 } 174 }
173 175
174 if (match_score) 176 if (match_score)
175 *match_score = total_score / scale_factors.size(); 177 *match_score = total_score / scale_factors.size();
176 } 178 }
177 179
178 // Resize |source_bitmap| using |resize_method|. 180 // Resize |source_bitmap| using |resize_method|.
179 SkBitmap GetResizedBitmap(const SkBitmap& source_bitmap, 181 SkBitmap GetResizedBitmap(const SkBitmap& source_bitmap,
180 int desired_size_in_dip, 182 int desired_size_in_dip,
181 ui::ScaleFactor scale_factor, 183 ui::ScaleFactor scale_factor,
182 ResizeMethod resize_method) { 184 ResizeMethod resize_method) {
183 float scale = ui::GetImageScale(scale_factor); 185 float scale = ui::GetImageScale(scale_factor);
184 int desired_size_in_pixel = static_cast<int>( 186 int desired_size_in_pixel =
185 desired_size_in_dip * scale + 0.5f); 187 static_cast<int>(desired_size_in_dip * scale + 0.5f);
186 188
187 switch (resize_method) { 189 switch (resize_method) {
188 case NONE: 190 case NONE:
189 return source_bitmap; 191 return source_bitmap;
190 case SAMPLE_NEAREST_NEIGHBOUR: 192 case SAMPLE_NEAREST_NEIGHBOUR:
191 return SampleNearestNeighbor(source_bitmap, desired_size_in_pixel); 193 return SampleNearestNeighbor(source_bitmap, desired_size_in_pixel);
192 case LANCZOS: 194 case LANCZOS:
193 return skia::ImageOperations::Resize( 195 return skia::ImageOperations::Resize(
194 source_bitmap, skia::ImageOperations::RESIZE_LANCZOS3, 196 source_bitmap,
195 desired_size_in_pixel, desired_size_in_pixel); 197 skia::ImageOperations::RESIZE_LANCZOS3,
198 desired_size_in_pixel,
199 desired_size_in_pixel);
196 } 200 }
197 return source_bitmap; 201 return source_bitmap;
198 } 202 }
199 203
200 } // namespace 204 } // namespace
201 205
202 const float kSelectFaviconFramesInvalidScore = -1.0f; 206 const float kSelectFaviconFramesInvalidScore = -1.0f;
203 207
204 gfx::ImageSkia SelectFaviconFrames( 208 gfx::ImageSkia SelectFaviconFrames(
205 const std::vector<SkBitmap>& bitmaps, 209 const std::vector<SkBitmap>& bitmaps,
206 const std::vector<gfx::Size>& original_sizes, 210 const std::vector<gfx::Size>& original_sizes,
207 const std::vector<ui::ScaleFactor>& scale_factors, 211 const std::vector<ui::ScaleFactor>& scale_factors,
208 int desired_size, 212 int desired_size,
209 float* match_score) { 213 float* match_score) {
210 std::vector<SelectionResult> results; 214 std::vector<SelectionResult> results;
211 GetCandidateIndicesWithBestScores(original_sizes, scale_factors, 215 GetCandidateIndicesWithBestScores(
212 desired_size, match_score, &results); 216 original_sizes, scale_factors, desired_size, match_score, &results);
213 217
214 gfx::ImageSkia multi_image; 218 gfx::ImageSkia multi_image;
215 for (size_t i = 0; i < results.size(); ++i) { 219 for (size_t i = 0; i < results.size(); ++i) {
216 const SelectionResult& result = results[i]; 220 const SelectionResult& result = results[i];
217 SkBitmap resized_bitmap = GetResizedBitmap(bitmaps[result.index], 221 SkBitmap resized_bitmap = GetResizedBitmap(bitmaps[result.index],
218 desired_size, result.scale_factor, result.resize_method); 222 desired_size,
219 multi_image.AddRepresentation( 223 result.scale_factor,
220 gfx::ImageSkiaRep(resized_bitmap, 224 result.resize_method);
221 ui::GetImageScale(result.scale_factor))); 225 multi_image.AddRepresentation(gfx::ImageSkiaRep(
226 resized_bitmap, ui::GetImageScale(result.scale_factor)));
222 } 227 }
223 return multi_image; 228 return multi_image;
224 } 229 }
225 230
226 void SelectFaviconFrameIndices( 231 void SelectFaviconFrameIndices(
227 const std::vector<gfx::Size>& frame_pixel_sizes, 232 const std::vector<gfx::Size>& frame_pixel_sizes,
228 const std::vector<ui::ScaleFactor>& scale_factors, 233 const std::vector<ui::ScaleFactor>& scale_factors,
229 int desired_size, 234 int desired_size,
230 std::vector<size_t>* best_indices, 235 std::vector<size_t>* best_indices,
231 float* match_score) { 236 float* match_score) {
232 std::vector<SelectionResult> results; 237 std::vector<SelectionResult> results;
233 GetCandidateIndicesWithBestScores(frame_pixel_sizes, scale_factors, 238 GetCandidateIndicesWithBestScores(
234 desired_size, match_score, &results); 239 frame_pixel_sizes, scale_factors, desired_size, match_score, &results);
235 240
236 std::set<size_t> already_added; 241 std::set<size_t> already_added;
237 for (size_t i = 0; i < results.size(); ++i) { 242 for (size_t i = 0; i < results.size(); ++i) {
238 size_t index = results[i].index; 243 size_t index = results[i].index;
239 // GetCandidateIndicesWithBestScores() will return duplicate indices if the 244 // GetCandidateIndicesWithBestScores() will return duplicate indices if the
240 // bitmap data with |frame_pixel_sizes[index]| should be used for multiple 245 // bitmap data with |frame_pixel_sizes[index]| should be used for multiple
241 // scale factors. Remove duplicates here such that |best_indices| contains 246 // scale factors. Remove duplicates here such that |best_indices| contains
242 // no duplicates. 247 // no duplicates.
243 if (already_added.find(index) == already_added.end()) { 248 if (already_added.find(index) == already_added.end()) {
244 already_added.insert(index); 249 already_added.insert(index);
245 best_indices->push_back(index); 250 best_indices->push_back(index);
246 } 251 }
247 } 252 }
248 } 253 }
OLDNEW
« no previous file with comments | « components/favicon_base/select_favicon_frames.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698