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

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

Issue 2347173002: Extend FaviconService to support fetching favicons from a Google server (Closed)
Patch Set: Requiring minimum_size Created 4 years 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
OLDNEW
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/favicon_util.h" 5 #include "components/favicon_base/favicon_util.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 desired_pixel_sizes.find(pixel_size.width()); 79 desired_pixel_sizes.find(pixel_size.width());
80 if (it == desired_pixel_sizes.end()) 80 if (it == desired_pixel_sizes.end())
81 continue; 81 continue;
82 82
83 png_reps.push_back(gfx::ImagePNGRep(png_data[i].bitmap_data, it->second)); 83 png_reps.push_back(gfx::ImagePNGRep(png_data[i].bitmap_data, it->second));
84 } 84 }
85 85
86 return png_reps; 86 return png_reps;
87 } 87 }
88 88
89 // Returns a resampled bitmap of |desired_size| x |desired_size| by resampling
90 // the best bitmap out of |input_bitmaps|.
91 // ResizeBitmapByDownsamplingIfPossible() is similar to SelectFaviconFrames()
92 // but it operates on bitmaps which have already been resampled via
93 // SelectFaviconFrames().
94 SkBitmap ResizeBitmapByDownsamplingIfPossible(
95 const std::vector<SkBitmap>& input_bitmaps,
96 int desired_size) {
97 DCHECK(!input_bitmaps.empty());
98 DCHECK_NE(0, desired_size);
99
100 SkBitmap best_bitmap;
101 for (size_t i = 0; i < input_bitmaps.size(); ++i) {
102 const SkBitmap& input_bitmap = input_bitmaps[i];
103 if (input_bitmap.width() == desired_size &&
104 input_bitmap.height() == desired_size) {
105 return input_bitmap;
106 } else if (best_bitmap.isNull()) {
107 best_bitmap = input_bitmap;
108 } else if (input_bitmap.width() >= best_bitmap.width() &&
109 input_bitmap.height() >= best_bitmap.height()) {
110 if (best_bitmap.width() < desired_size ||
111 best_bitmap.height() < desired_size) {
112 best_bitmap = input_bitmap;
113 }
114 } else {
115 if (input_bitmap.width() >= desired_size &&
116 input_bitmap.height() >= desired_size) {
117 best_bitmap = input_bitmap;
118 }
119 }
120 }
121
122 if (desired_size % best_bitmap.width() == 0 &&
123 desired_size % best_bitmap.height() == 0) {
124 // Use nearest neighbour resampling if upsampling by an integer. This
125 // makes the result look similar to the result of SelectFaviconFrames().
126 SkBitmap bitmap;
127 bitmap.allocN32Pixels(desired_size, desired_size);
128 if (!best_bitmap.isOpaque())
129 bitmap.eraseARGB(0, 0, 0, 0);
130
131 SkCanvas canvas(bitmap);
132 canvas.drawBitmapRect(
133 best_bitmap, SkRect::MakeIWH(desired_size, desired_size), NULL);
134 return bitmap;
135 }
136 return skia::ImageOperations::Resize(best_bitmap,
137 skia::ImageOperations::RESIZE_LANCZOS3,
138 desired_size,
139 desired_size);
140 }
141
142 } // namespace 89 } // namespace
143 90
144 std::vector<float> GetFaviconScales() { 91 std::vector<float> GetFaviconScales() {
145 const float kScale1x = 1.0f; 92 const float kScale1x = 1.0f;
146 std::vector<ui::ScaleFactor> resource_scale_factors = 93 std::vector<ui::ScaleFactor> resource_scale_factors =
147 ui::GetSupportedScaleFactors(); 94 ui::GetSupportedScaleFactors();
148 95
149 // TODO(ios): 1.0f should not be necessary on iOS retina devices. However 96 // TODO(ios): 1.0f should not be necessary on iOS retina devices. However
150 // the sync service only supports syncing 100p favicons. Until sync supports 97 // the sync service only supports syncing 100p favicons. Until sync supports
151 // other scales 100p is needed in the list of scales to retrieve and 98 // other scales 100p is needed in the list of scales to retrieve and
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 } 235 }
289 236
290 bitmap_result.bitmap_data = base::RefCountedBytes::TakeVector( 237 bitmap_result.bitmap_data = base::RefCountedBytes::TakeVector(
291 &resized_bitmap_data); 238 &resized_bitmap_data);
292 bitmap_result.pixel_size = 239 bitmap_result.pixel_size =
293 gfx::Size(desired_size_in_pixel, desired_size_in_pixel); 240 gfx::Size(desired_size_in_pixel, desired_size_in_pixel);
294 241
295 return bitmap_result; 242 return bitmap_result;
296 } 243 }
297 244
245 SkBitmap ResizeBitmapByDownsamplingIfPossible(
246 const std::vector<SkBitmap>& input_bitmaps,
247 int desired_size) {
pkotwicz 2016/12/19 00:03:29 Now that this function is not in the anonymous nam
jkrcal 2016/12/19 17:07:19 Done.
248 DCHECK(!input_bitmaps.empty());
249 DCHECK_NE(0, desired_size);
250
251 SkBitmap best_bitmap;
252 for (size_t i = 0; i < input_bitmaps.size(); ++i) {
253 const SkBitmap& input_bitmap = input_bitmaps[i];
254 if (input_bitmap.width() == desired_size &&
255 input_bitmap.height() == desired_size) {
256 return input_bitmap;
257 } else if (best_bitmap.isNull()) {
258 best_bitmap = input_bitmap;
259 } else if (input_bitmap.width() >= best_bitmap.width() &&
260 input_bitmap.height() >= best_bitmap.height()) {
261 if (best_bitmap.width() < desired_size ||
262 best_bitmap.height() < desired_size) {
263 best_bitmap = input_bitmap;
264 }
265 } else {
266 if (input_bitmap.width() >= desired_size &&
267 input_bitmap.height() >= desired_size) {
268 best_bitmap = input_bitmap;
269 }
270 }
271 }
272
273 if (desired_size % best_bitmap.width() == 0 &&
274 desired_size % best_bitmap.height() == 0) {
275 // Use nearest neighbour resampling if upsampling by an integer. This
276 // makes the result look similar to the result of SelectFaviconFrames().
277 SkBitmap bitmap;
278 bitmap.allocN32Pixels(desired_size, desired_size);
279 if (!best_bitmap.isOpaque())
280 bitmap.eraseARGB(0, 0, 0, 0);
281
282 SkCanvas canvas(bitmap);
283 canvas.drawBitmapRect(
284 best_bitmap, SkRect::MakeIWH(desired_size, desired_size), NULL);
285 return bitmap;
286 }
287 return skia::ImageOperations::Resize(best_bitmap,
288 skia::ImageOperations::RESIZE_LANCZOS3,
289 desired_size,
290 desired_size);
291 }
292
298 } // namespace favicon_base 293 } // namespace favicon_base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698