summaryrefslogtreecommitdiffstats
path: root/chrome/browser/favicon/favicon_util.cc
diff options
context:
space:
mode:
authorpkotwicz@chromium.org <pkotwicz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-09-18 00:26:30 +0000
committerpkotwicz@chromium.org <pkotwicz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-09-18 00:26:30 +0000
commit263cb08f526585b58aeb6cf82e67c7a53491e403 (patch)
treef33b5930ea9b9dc8498d378917141170a78c84fa /chrome/browser/favicon/favicon_util.cc
parent7b904af5b9825658d3fc02b9c5fa7db5afdef277 (diff)
downloadchromium_src-263cb08f526585b58aeb6cf82e67c7a53491e403.zip
chromium_src-263cb08f526585b58aeb6cf82e67c7a53491e403.tar.gz
chromium_src-263cb08f526585b58aeb6cf82e67c7a53491e403.tar.bz2
This CL:
- Passes in ImageHostMsg_DidDownloadImage the original sizes of the bitmaps before they were resized as a result of the max size passed by ImageMsg_DownloadImage. This enables FaviconHandler to properly score the "goodness" of a bitmap. - Removes the unused "preferred size" parameters from the ImageMsg_DownloadImage and ImageHostMsg_DidDownloadImage messages - Adds a method in the anonymous namespace of FaviconUtil so that FaviconUtil::SelectFaviconFramesFromPNGs() does not use SelectFaviconFrames(). The old behavior is confusing because the call to SelectFaviconFrames() in FaviconUtil::SelectFaviconFramesFromPNGs() was operating on already resized bitmaps BUG=278457 TEST=FaviconHandlerTest.MultipleFavicons R=joth,cevans,jam,sky TBR=benwells (For trivial change to shell_window.*) Review URL: https://chromiumcodereview.appspot.com/23708024 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@223748 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/favicon/favicon_util.cc')
-rw-r--r--chrome/browser/favicon/favicon_util.cc84
1 files changed, 67 insertions, 17 deletions
diff --git a/chrome/browser/favicon/favicon_util.cc b/chrome/browser/favicon/favicon_util.cc
index 4d7dff2..4f2db61 100644
--- a/chrome/browser/favicon/favicon_util.cc
+++ b/chrome/browser/favicon/favicon_util.cc
@@ -8,6 +8,7 @@
#include "chrome/common/favicon/favicon_types.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/skia/include/core/SkCanvas.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/favicon_size.h"
#include "ui/gfx/image/image_png_rep.h"
@@ -76,6 +77,62 @@ std::vector<gfx::ImagePNGRep> SelectFaviconFramesFromPNGsWithoutResizing(
return png_reps;
}
+// Returns a resampled bitmap of
+// |desired_size_in_pixel| x |desired_size_in_pixel| by resampling the best
+// bitmap out of |input_bitmaps|. ResizeBitmapByDownsamplingIfPossible() is
+// similar to SelectFaviconFrames() but it operates on bitmaps which have
+// already been resampled via SelectFaviconFrames().
+SkBitmap ResizeBitmapByDownsamplingIfPossible(
+ const std::vector<SkBitmap>& input_bitmaps,
+ int desired_size_in_pixel) {
+ DCHECK(!input_bitmaps.empty());
+ DCHECK_NE(desired_size_in_pixel, 0);
+
+ SkBitmap best_bitmap;
+ for (size_t i = 0; i < input_bitmaps.size(); ++i) {
+ const SkBitmap& input_bitmap = input_bitmaps[i];
+ if (input_bitmap.width() == desired_size_in_pixel &&
+ input_bitmap.height() == desired_size_in_pixel) {
+ return input_bitmap;
+ } else if (best_bitmap.isNull()) {
+ best_bitmap = input_bitmap;
+ } else if (input_bitmap.width() >= best_bitmap.width() &&
+ input_bitmap.height() >= best_bitmap.height()) {
+ if (best_bitmap.width() < desired_size_in_pixel ||
+ best_bitmap.height() < desired_size_in_pixel) {
+ best_bitmap = input_bitmap;
+ }
+ } else {
+ if (input_bitmap.width() >= desired_size_in_pixel &&
+ input_bitmap.height() >= desired_size_in_pixel) {
+ best_bitmap = input_bitmap;
+ }
+ }
+ }
+
+ if (desired_size_in_pixel % best_bitmap.width() == 0 &&
+ desired_size_in_pixel % best_bitmap.height() == 0) {
+ // Use nearest neighbour resampling if upsampling by an integer. This
+ // makes the result look similar to the result of SelectFaviconFrames().
+ SkBitmap bitmap;
+ bitmap.setConfig(SkBitmap::kARGB_8888_Config,
+ desired_size_in_pixel,
+ desired_size_in_pixel);
+ bitmap.allocPixels();
+ if (!best_bitmap.isOpaque())
+ bitmap.eraseARGB(0, 0, 0, 0);
+
+ SkCanvas canvas(bitmap);
+ SkRect dest(SkRect::MakeWH(desired_size_in_pixel, desired_size_in_pixel));
+ canvas.drawBitmapRect(best_bitmap, NULL, dest);
+ return bitmap;
+ }
+ return skia::ImageOperations::Resize(best_bitmap,
+ skia::ImageOperations::RESIZE_LANCZOS3,
+ desired_size_in_pixel,
+ desired_size_in_pixel);
+}
+
} // namespace
// static
@@ -168,8 +225,16 @@ gfx::Image FaviconUtil::SelectFaviconFramesFromPNGs(
if (bitmaps.empty())
return gfx::Image();
- gfx::ImageSkia resized_image_skia = SelectFaviconFrames(bitmaps,
- scale_factors_to_generate, favicon_size, NULL);
+ gfx::ImageSkia resized_image_skia;
+ for (size_t i = 0; i < scale_factors_to_generate.size(); ++i) {
+ ui::ScaleFactor scale_factor = scale_factors_to_generate[i];
+ int desired_size_in_pixel =
+ ceil(favicon_size * ui::GetScaleFactorScale(scale_factor));
+ SkBitmap bitmap = ResizeBitmapByDownsamplingIfPossible(
+ bitmaps, desired_size_in_pixel);
+ resized_image_skia.AddRepresentation(
+ gfx::ImageSkiaRep(bitmap, scale_factor));
+ }
if (png_reps.empty())
return gfx::Image(resized_image_skia);
@@ -187,18 +252,3 @@ gfx::Image FaviconUtil::SelectFaviconFramesFromPNGs(
return gfx::Image(png_reps);
}
-
-// static
-size_t FaviconUtil::SelectBestFaviconFromBitmaps(
- const std::vector<SkBitmap>& bitmaps,
- const std::vector<ui::ScaleFactor>& scale_factors,
- int desired_size) {
- std::vector<gfx::Size> sizes;
- for (size_t i = 0; i < bitmaps.size(); ++i)
- sizes.push_back(gfx::Size(bitmaps[i].width(), bitmaps[i].height()));
- std::vector<size_t> selected_bitmap_indices;
- SelectFaviconFrameIndices(sizes, scale_factors, desired_size,
- &selected_bitmap_indices, NULL);
- DCHECK_EQ(1u, selected_bitmap_indices.size());
- return selected_bitmap_indices[0];
-}