-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wave goodbye to material-color-utilities-python #361
Conversation
The color generation is faster, but not the color picking? This PR makes the process for large images much slower. Try setting this very large AI-generated and -upscaled image as wallpaper and you'll see... For me it takes only 1s currently but with this PR it takes 20s |
I haven't tested that for large 4K images. |
I recommend to have a look at android's compression algorithm: // Maximum size that a bitmap can have to keep our calculations valid
private static final int MAX_BITMAP_SIZE = 112;
// Even though we have a maximum size, we'll mainly match bitmap sizes
// using the area instead. This way our comparisons are aspect ratio independent.
private static final int MAX_WALLPAPER_EXTRACTION_AREA = MAX_BITMAP_SIZE * MAX_BITMAP_SIZE; if (bitmapArea > MAX_WALLPAPER_EXTRACTION_AREA) {
shouldRecycle = true;
Size optimalSize = calculateOptimalSize(bitmap.getWidth(), bitmap.getHeight());
bitmap = Bitmap.createScaledBitmap(bitmap, optimalSize.getWidth(),
optimalSize.getHeight(), false /* filter */);
} private static Size calculateOptimalSize(int width, int height) {
// Calculate how big the bitmap needs to be.
// This avoids unnecessary processing and allocation inside Palette.
final int requestedArea = width * height;
double scale = 1;
if (requestedArea > MAX_WALLPAPER_EXTRACTION_AREA) {
scale = Math.sqrt(MAX_WALLPAPER_EXTRACTION_AREA / (double) requestedArea);
}
int newWidth = (int) (width * scale);
int newHeight = (int) (height * scale);
// Dealing with edge cases of the drawable being too wide or too tall.
// Width or height would end up being 0, in this case we'll set it to 1.
if (newWidth == 0) {
newWidth = 1;
}
if (newHeight == 0) {
newHeight = 1;
}
return new Size(newWidth, newHeight);
} (That bitmap is used for generating colors) |
Hmm, This algorithm is much better, as it will handle any arbitrary image aspect ratio and still ensure that the resized area is always the same. The previous algorithm will yield a high bitmap for arbitrary non-standard image aspect ratios (like panaromas). Perhaps we should always crop the image to the respective screen resolution and then generate colors. I will consider this algorithm and push a new commit. end-4 dont merge this right now. Thanks a lot T-Dynamos for the recommendation 💙 |
Added new algo in 7d50e43 |
Performance is good now |
Removing dependency material-color-utilities-python in favor of materialyoucolor-python for picking colors from image, as this issue is fixed, and the algorithm is faster too.