| 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/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 Loading... |
| 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 Loading... |
| 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) { |
| 248 DCHECK_NE(0, desired_size); |
| 249 |
| 250 if (input_bitmaps.empty()) |
| 251 return SkBitmap(); |
| 252 |
| 253 SkBitmap best_bitmap; |
| 254 for (size_t i = 0; i < input_bitmaps.size(); ++i) { |
| 255 const SkBitmap& input_bitmap = input_bitmaps[i]; |
| 256 if (input_bitmap.width() == desired_size && |
| 257 input_bitmap.height() == desired_size) { |
| 258 return input_bitmap; |
| 259 } else if (best_bitmap.isNull()) { |
| 260 best_bitmap = input_bitmap; |
| 261 } else if (input_bitmap.width() >= best_bitmap.width() && |
| 262 input_bitmap.height() >= best_bitmap.height()) { |
| 263 if (best_bitmap.width() < desired_size || |
| 264 best_bitmap.height() < desired_size) { |
| 265 best_bitmap = input_bitmap; |
| 266 } |
| 267 } else { |
| 268 if (input_bitmap.width() >= desired_size && |
| 269 input_bitmap.height() >= desired_size) { |
| 270 best_bitmap = input_bitmap; |
| 271 } |
| 272 } |
| 273 } |
| 274 |
| 275 if (desired_size % best_bitmap.width() == 0 && |
| 276 desired_size % best_bitmap.height() == 0) { |
| 277 // Use nearest neighbour resampling if upsampling by an integer. This |
| 278 // makes the result look similar to the result of SelectFaviconFrames(). |
| 279 SkBitmap bitmap; |
| 280 bitmap.allocN32Pixels(desired_size, desired_size); |
| 281 if (!best_bitmap.isOpaque()) |
| 282 bitmap.eraseARGB(0, 0, 0, 0); |
| 283 |
| 284 SkCanvas canvas(bitmap); |
| 285 canvas.drawBitmapRect( |
| 286 best_bitmap, SkRect::MakeIWH(desired_size, desired_size), NULL); |
| 287 return bitmap; |
| 288 } |
| 289 return skia::ImageOperations::Resize(best_bitmap, |
| 290 skia::ImageOperations::RESIZE_LANCZOS3, |
| 291 desired_size, |
| 292 desired_size); |
| 293 } |
| 294 |
| 298 } // namespace favicon_base | 295 } // namespace favicon_base |
| OLD | NEW |