summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/Utilities.java
diff options
context:
space:
mode:
authorSunny Goyal <sunnygoyal@google.com>2017-09-28 19:13:53 -0700
committerSunny Goyal <sunnygoyal@google.com>2017-09-28 19:21:35 -0700
commit9b8e36b95db9ed0888d25f0e8503f8fac79a3a6c (patch)
treec721457a3e400a93ff030c76259c5d78c43a6118 /src/com/android/launcher3/Utilities.java
parent9315c9a8cdd0832d77f23d953da12c25860172e9 (diff)
downloadandroid_packages_apps_Trebuchet-9b8e36b95db9ed0888d25f0e8503f8fac79a3a6c.tar.gz
android_packages_apps_Trebuchet-9b8e36b95db9ed0888d25f0e8503f8fac79a3a6c.tar.bz2
android_packages_apps_Trebuchet-9b8e36b95db9ed0888d25f0e8503f8fac79a3a6c.zip
Avoiding duplicate calls to Bitmap.getPixel(x, y)
Change-Id: I24414b41e9b0e9833cd4e962187a08672bc86c46
Diffstat (limited to 'src/com/android/launcher3/Utilities.java')
-rw-r--r--src/com/android/launcher3/Utilities.java44
1 files changed, 24 insertions, 20 deletions
diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java
index b6876f670..71677782d 100644
--- a/src/com/android/launcher3/Utilities.java
+++ b/src/com/android/launcher3/Utilities.java
@@ -309,6 +309,9 @@ public final class Utilities {
float highScore = -1;
int bestHue = -1;
+ int[] pixels = new int[samples];
+ int pixelCount = 0;
+
for (int y = 0; y < height; y += sampleStride) {
for (int x = 0; x < width; x += sampleStride) {
int argb = bitmap.getPixel(x, y);
@@ -326,6 +329,9 @@ public final class Utilities {
// Defensively avoid array bounds violations.
continue;
}
+ if (pixelCount < samples) {
+ pixels[pixelCount++] = rgb;
+ }
float score = hsv[1] * hsv[2];
hueScoreHistogram[hue] += score;
if (hueScoreHistogram[hue] > highScore) {
@@ -335,31 +341,29 @@ public final class Utilities {
}
}
- SparseArray<Float> rgbScores = new SparseArray<Float>();
+ SparseArray<Float> rgbScores = new SparseArray<>();
int bestColor = 0xff000000;
highScore = -1;
// Go back over the RGB colors that match the winning hue,
// creating a histogram of weighted s*v scores, for up to 100*100 [s,v] buckets.
// The highest-scoring RGB color wins.
- for (int y = 0; y < height; y += sampleStride) {
- for (int x = 0; x < width; x += sampleStride) {
- int rgb = bitmap.getPixel(x, y) | 0xff000000;
- Color.colorToHSV(rgb, hsv);
- int hue = (int) hsv[0];
- if (hue == bestHue) {
- float s = hsv[1];
- float v = hsv[2];
- int bucket = (int) (s * 100) + (int) (v * 10000);
- // Score by cumulative saturation * value.
- float score = s * v;
- Float oldTotal = rgbScores.get(bucket);
- float newTotal = oldTotal == null ? score : oldTotal + score;
- rgbScores.put(bucket, newTotal);
- if (newTotal > highScore) {
- highScore = newTotal;
- // All the colors in the winning bucket are very similar. Last in wins.
- bestColor = rgb;
- }
+ for (int i = 0; i < pixelCount; i++) {
+ int rgb = pixels[i];
+ Color.colorToHSV(rgb, hsv);
+ int hue = (int) hsv[0];
+ if (hue == bestHue) {
+ float s = hsv[1];
+ float v = hsv[2];
+ int bucket = (int) (s * 100) + (int) (v * 10000);
+ // Score by cumulative saturation * value.
+ float score = s * v;
+ Float oldTotal = rgbScores.get(bucket);
+ float newTotal = oldTotal == null ? score : oldTotal + score;
+ rgbScores.put(bucket, newTotal);
+ if (newTotal > highScore) {
+ highScore = newTotal;
+ // All the colors in the winning bucket are very similar. Last in wins.
+ bestColor = rgb;
}
}
}