From 7ef64c0787c0f98cc0273d9f20e9ed70ef33b145 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Thu, 11 Mar 2021 11:50:40 -0500 Subject: [PATCH] Move Wu check for max colors earlier. This has no impact. However, after going through the quantizers line by line for other bug fixes, after getting a break from looking at them in-depth for a couple weeks, it was much more readable/felt safer to adjust mMaxColors at the top. Bug: 182333325 Test: Verify output remains constant, atest CtsAppTestCases:android.app.cts.WallpaperColorsTest#fromDrawableTest passes. Change-Id: I561ba44bca392634a956992fd946f3310aa1c8ee --- .../internal/graphics/palette/WuQuantizer.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/core/java/com/android/internal/graphics/palette/WuQuantizer.java b/core/java/com/android/internal/graphics/palette/WuQuantizer.java index 6d08dc88cb20e..a2652ea6d5e1a 100644 --- a/core/java/com/android/internal/graphics/palette/WuQuantizer.java +++ b/core/java/com/android/internal/graphics/palette/WuQuantizer.java @@ -78,7 +78,12 @@ public class WuQuantizer implements Quantizer { // All of the sample Wu implementations are reimplementations of a snippet of C code from // the early 90s. They all cap the maximum # of colors at 256, and it is impossible to tell // if this is a requirement, a consequence of QUANT_SIZE, or arbitrary. - this.mMaxColors = Math.min(MAX_COLORS, maxColorCount); + // + // Also, the number of maximum colors should be capped at the number of pixels - otherwise, + // If extraction is run on a set of pixels whose count is less than max colors, + // then colors.length < max colors, and accesses to colors[index] throw an + // ArrayOutOfBoundsException. + this.mMaxColors = Math.min(Math.min(MAX_COLORS, maxColorCount), colors.length); Box[] cube = new Box[mMaxColors]; int red, green, blue; @@ -119,11 +124,7 @@ public class WuQuantizer implements Quantizer { } } - // If extraction is run on a set of pixels whose count is less than the - // number of max colors, then colors.length < max colors, and accesses - // to colors[index] inside the for loop throw an ArrayOutOfBoundsException. - int numColorsToCreate = (int) Math.min(mMaxColors, colors.length); - for (k = 0; k < numColorsToCreate; ++k) { + for (k = 0; k < mMaxColors; ++k) { weight = getVolume(cube[k], mWt); if (weight > 0) { red = (int) (getVolume(cube[k], mMr) / weight);