Commit b4c4b350 authored by estade's avatar estade Committed by Commit bot

ChromeOS color extraction - only look at a maximum of 10k pixels in the

input image.

The pixels that are chosen are evenly distributed throughout the image.

This proved to be more effective than limiting the color space (i.e. the
number of unique colors).

On at least one large sample wallpaper and my workstation, this reduces
runtime from hundreds of ms to <10ms.

BUG=687852

Review-Url: https://codereview.chromium.org/2797033002
Cr-Commit-Position: refs/heads/master@{#462892}
parent a7c962cd
......@@ -368,15 +368,16 @@ SkColor CalculateProminentColor(const SkBitmap& bitmap,
const uint32_t* pixels = static_cast<uint32_t*>(bitmap.getPixels());
const int pixel_count = bitmap.width() * bitmap.height();
// We don't know exactly how many distinct colors there will be, so just
// reserve enough space to keep the maximum number of table resizes low.
// In our testing set, 2/3 of wallpapers have <200k unique colors and 1/4
// have <100k. Thus 200k is picked as a number that usually amounts to zero
// resizes but usually doesn't waste a lot of space.
std::unordered_map<SkColor, int> color_counts(200000);
// For better performance, only consider at most 10k pixels (evenly
// distributed throughout the image). This has a very minor impact on the
// outcome but improves runtime substantially for large images. 10,007 is a
// prime number to reduce the chance of picking an unrepresentative sample.
constexpr int kMaxConsideredPixels = 10007;
const int pixel_increment = std::max(1, pixel_count / kMaxConsideredPixels);
std::unordered_map<SkColor, int> color_counts(kMaxConsideredPixels);
// First extract all colors into counts.
for (int i = 0; i < pixel_count; ++i) {
for (int i = 0; i < pixel_count; i += pixel_increment) {
// SkBitmap uses pre-multiplied alpha but the prominent color algorithm
// needs non-pre-multiplied alpha.
const SkColor pixel = SkUnPreMultiply::PMColorToColor(pixels[i]);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment