diff options
author | tommycli <tommycli@chromium.org> | 2014-11-13 18:37:33 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-11-14 02:37:59 +0000 |
commit | 417ae173d4e1bd96eddf7d80f1d0ba76706105b7 (patch) | |
tree | b8d4873fcf8fb24f7992188fa6514a3aea0265a8 /chrome/browser/thumbnails | |
parent | b2ead6df9ea247e5e9a2f97199d2a25b52804f36 (diff) | |
download | chromium_src-417ae173d4e1bd96eddf7d80f1d0ba76706105b7.zip chromium_src-417ae173d4e1bd96eddf7d80f1d0ba76706105b7.tar.gz chromium_src-417ae173d4e1bd96eddf7d80f1d0ba76706105b7.tar.bz2 |
Color_utils: Move image boringness scoring from thumbnails to ui/gfx.
This is a spinoff of https://codereview.chromium.org/707623002/, and
allows the CalculateBoringScore luma histogram metric to be reused in
other parts of the code. (It's used in content/ in above patch).
BUG=403800
Review URL: https://codereview.chromium.org/720343002
Cr-Commit-Position: refs/heads/master@{#304153}
Diffstat (limited to 'chrome/browser/thumbnails')
4 files changed, 5 insertions, 57 deletions
diff --git a/chrome/browser/thumbnails/content_based_thumbnailing_algorithm.cc b/chrome/browser/thumbnails/content_based_thumbnailing_algorithm.cc index 9d8b42b..92b83e6 100644 --- a/chrome/browser/thumbnails/content_based_thumbnailing_algorithm.cc +++ b/chrome/browser/thumbnails/content_based_thumbnailing_algorithm.cc @@ -10,6 +10,7 @@ #include "chrome/browser/thumbnails/simple_thumbnail_crop.h" #include "content/public/browser/browser_thread.h" #include "third_party/skia/include/core/SkBitmap.h" +#include "ui/gfx/color_utils.h" #include "ui/gfx/scrollbar_size.h" #include "ui/gfx/size_conversions.h" #include "ui/gfx/skbitmap_operations.h" @@ -76,7 +77,7 @@ void ContentBasedThumbnailingAlgorithm::ProcessBitmap( if (source_bitmap.width() <= target_thumbnail_size.width() || source_bitmap.height() <= target_thumbnail_size.height()) { context->score.boring_score = - SimpleThumbnailCrop::CalculateBoringScore(source_bitmap); + color_utils::CalculateBoringScore(source_bitmap); context->score.good_clipping = (context->clip_result == CLIP_RESULT_WIDER_THAN_TALL || context->clip_result == CLIP_RESULT_TALLER_THAN_WIDE || @@ -187,7 +188,7 @@ void ContentBasedThumbnailingAlgorithm::CreateRetargetedThumbnail( base::TimeTicks::Now() - begin_compute_thumbnail); } context->score.boring_score = - SimpleThumbnailCrop::CalculateBoringScore(source_bitmap); + color_utils::CalculateBoringScore(source_bitmap); if (!processing_failed) context->score.boring_score *= kScoreBoostFromSuccessfulRetargeting; context->score.good_clipping = diff --git a/chrome/browser/thumbnails/simple_thumbnail_crop.cc b/chrome/browser/thumbnails/simple_thumbnail_crop.cc index 26a89f4..b438536 100644 --- a/chrome/browser/thumbnails/simple_thumbnail_crop.cc +++ b/chrome/browser/thumbnails/simple_thumbnail_crop.cc @@ -50,7 +50,7 @@ void SimpleThumbnailCrop::ProcessBitmap( ComputeTargetSizeAtMaximumScale(target_size_), &context->clip_result); - context->score.boring_score = CalculateBoringScore(thumbnail); + context->score.boring_score = color_utils::CalculateBoringScore(thumbnail); context->score.good_clipping = (context->clip_result == CLIP_RESULT_WIDER_THAN_TALL || context->clip_result == CLIP_RESULT_TALLER_THAN_WIDE || @@ -59,17 +59,6 @@ void SimpleThumbnailCrop::ProcessBitmap( callback.Run(*context.get(), thumbnail); } -double SimpleThumbnailCrop::CalculateBoringScore(const SkBitmap& bitmap) { - if (bitmap.isNull() || bitmap.empty()) - return 1.0; - int histogram[256] = {0}; - color_utils::BuildLumaHistogram(bitmap, histogram); - - int color_count = *std::max_element(histogram, histogram + 256); - int pixel_count = bitmap.width() * bitmap.height(); - return static_cast<double>(color_count) / pixel_count; -} - SkBitmap SimpleThumbnailCrop::GetClippedBitmap(const SkBitmap& bitmap, int desired_width, int desired_height, diff --git a/chrome/browser/thumbnails/simple_thumbnail_crop.h b/chrome/browser/thumbnails/simple_thumbnail_crop.h index ce40374..e54e5f2 100644 --- a/chrome/browser/thumbnails/simple_thumbnail_crop.h +++ b/chrome/browser/thumbnails/simple_thumbnail_crop.h @@ -27,13 +27,6 @@ class SimpleThumbnailCrop : public ThumbnailingAlgorithm { const ConsumerCallback& callback, const SkBitmap& bitmap) override; - // Calculates how "boring" a thumbnail is. The boring score is the - // 0,1 ranged percentage of pixels that are the most common - // luma. Higher boring scores indicate that a higher percentage of a - // bitmap are all the same brightness. - // Statically exposed for use by tests only. - static double CalculateBoringScore(const SkBitmap& bitmap); - // Gets the clipped bitmap from |bitmap| per the aspect ratio of the // desired width and the desired height. For instance, if the input // bitmap is vertically long (ex. 400x900) and the desired size is @@ -69,6 +62,6 @@ class SimpleThumbnailCrop : public ThumbnailingAlgorithm { DISALLOW_COPY_AND_ASSIGN(SimpleThumbnailCrop); }; -} +} // namespace thumbnails #endif // CHROME_BROWSER_THUMBNAILS_SIMPLE_THUMBNAIL_CROP_H_ diff --git a/chrome/browser/thumbnails/simple_thumbnail_crop_unittest.cc b/chrome/browser/thumbnails/simple_thumbnail_crop_unittest.cc index c00abb2..fc05f10 100644 --- a/chrome/browser/thumbnails/simple_thumbnail_crop_unittest.cc +++ b/chrome/browser/thumbnails/simple_thumbnail_crop_unittest.cc @@ -23,41 +23,6 @@ using thumbnails::SimpleThumbnailCrop; typedef testing::Test SimpleThumbnailCropTest; -TEST_F(SimpleThumbnailCropTest, CalculateBoringScore_Empty) { - SkBitmap bitmap; - EXPECT_DOUBLE_EQ(1.0, SimpleThumbnailCrop::CalculateBoringScore(bitmap)); -} - -TEST_F(SimpleThumbnailCropTest, CalculateBoringScore_SingleColor) { - const gfx::Size kSize(20, 10); - gfx::Canvas canvas(kSize, 1.0f, true); - // Fill all pixels in black. - canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK); - - SkBitmap bitmap = - skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); - // The thumbnail should deserve the highest boring score. - EXPECT_DOUBLE_EQ(1.0, SimpleThumbnailCrop::CalculateBoringScore(bitmap)); -} - -TEST_F(SimpleThumbnailCropTest, CalculateBoringScore_TwoColors) { - const gfx::Size kSize(20, 10); - - gfx::Canvas canvas(kSize, 1.0f, true); - // Fill all pixels in black. - canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK); - // Fill the left half pixels in white. - canvas.FillRect(gfx::Rect(0, 0, kSize.width() / 2, kSize.height()), - SK_ColorWHITE); - - SkBitmap bitmap = - skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); - ASSERT_EQ(kSize.width(), bitmap.width()); - ASSERT_EQ(kSize.height(), bitmap.height()); - // The thumbnail should be less boring because two colors are used. - EXPECT_DOUBLE_EQ(0.5, SimpleThumbnailCrop::CalculateBoringScore(bitmap)); -} - TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_TallerThanWide) { // The input bitmap is vertically long. gfx::Canvas canvas(gfx::Size(40, 90), 1.0f, true); |