Skip to content
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

Incorrect calculateInSampleSize leads to overly degraded quality #152

Closed
FlorianDenis opened this issue Jan 13, 2021 · 1 comment
Closed

Comments

@FlorianDenis
Copy link

FlorianDenis commented Jan 13, 2021

You have an issue in calculateInSampleSize where the return results does not match the comment "largest value that keeps both height and width larger than the requested height and width".

For instance for an image of size 3000x3000, if the requested size is 2000x2000, your code returns inSampleSize = 2, leading to a 1500x1500 overly degraded image being loaded in memory and then upscaled to 2000x2000

Here follows my suggestion for correction, but feel free to adapt however you want

fun calculateInSampleSize(
    options: BitmapFactory.Options,
    reqWidth: Int,
    reqHeight: Int
): Int {
    // Raw height and width of image
    var height = options.outHeight
    var width = options.outWidth
    var sampleSize = 1

    // Calculate the largest inSampleSize value that is a power of 2 and keeps both
    // height and width larger than the requested height and width.
    while(height / 2 > reqHeight || width / 2 > reqWidth) {
        sampleSize *= 2
        height /= 2
        width /= 2
    }

    return sampleSize
}
Dhaval2404 added a commit that referenced this issue May 15, 2021
@Dhaval2404
Copy link
Owner

Thank you @FlorianDenis for the feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants