diff options
| author | 2023-04-27 15:12:55 +0000 | |
|---|---|---|
| committer | 2023-04-28 13:40:44 +0000 | |
| commit | 1439c86ff54664012c4a30288518671aba1d7176 (patch) | |
| tree | b2bce9c74919dbae68c9aa4b2c197406936fd183 | |
| parent | fbba5e15f6a4c58432da5b6ea1cb79d18fa86266 (diff) | |
Use less clusters for color extraction for small bitmaps
The color extraction in WallpaperColors uses a K-Mean algorithm.
Currently, it uses 128 clusters, so it generates 128 colors, and then select the most important ones.
But for most widgets this method is called with highly downscaled bitmaps (as small as 20x20 px), so we're trying to k-mean 128 clusters with only a couple hundreds of datapoints, which is most likely instable.
This CL changes the number of cluster to be max(5, min(nPixels / 16, 128)). Thus, image with less than 2048 px (about 45x45) will use less than 128 clusters, but always at least 5 clusters.
Bug: 273505854
Bug: 268180548
Test: perfetto trace
Test: manual: observe the result on widget colors
Change-Id: I34c13cfca392bb64bae4e7bf0adc1933c0eeb67c
| -rw-r--r-- | core/java/android/app/WallpaperColors.java | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/core/java/android/app/WallpaperColors.java b/core/java/android/app/WallpaperColors.java index a34a50c4b7b0..be1d8b8ad7d3 100644 --- a/core/java/android/app/WallpaperColors.java +++ b/core/java/android/app/WallpaperColors.java @@ -213,9 +213,17 @@ public final class WallpaperColors implements Parcelable { .resizeBitmapArea(MAX_WALLPAPER_EXTRACTION_AREA) .generate(); } else { + // in any case, always use between 5 and 128 clusters + int minClusters = 5; + int maxClusters = 128; + + // if the bitmap is very small, use bitmapArea/16 clusters instead of 128 + int minPixelsPerCluster = 16; + int numberOfColors = Math.max(minClusters, + Math.min(maxClusters, bitmapArea / minPixelsPerCluster)); palette = Palette .from(bitmap, new CelebiQuantizer()) - .maximumColorCount(128) + .maximumColorCount(numberOfColors) .resizeBitmapArea(MAX_WALLPAPER_EXTRACTION_AREA) .generate(); } |