Skip to content

Commit

Permalink
BitmapCalculator: fix use of a recycled bitmap
Browse files Browse the repository at this point in the history
This commit fixes this warning message which happens with the HSV
color picker:

 "Called getWidth() on a recycle()'d bitmap! This is undefined behavior!"

It is likely due to the fact that createScaledBitmap() will usually
return a new bitmap, but will return the source bitmap if the dimensions
are the same.

This commit makes sure that scaleBitmap() will always return a new
bitmap.
  • Loading branch information
sderr committed May 7, 2024
1 parent 775f779 commit 94fcc51
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ internal object BitmapCalculator {
* corresponding dimension of the target size.
*/
internal fun scaleBitmap(bitmap: Bitmap, targetSize: IntSize): Bitmap {
return Bitmap.createScaledBitmap(
bitmap,
targetSize.width,
targetSize.height,
false,
)
val sameSize = (targetSize.height == bitmap.height && targetSize.width == bitmap.width)
return if (sameSize) {
bitmap.copy(bitmap.config, bitmap.isMutable)
} else {
Bitmap.createScaledBitmap(
bitmap,
targetSize.width,
targetSize.height,
false,
)
}
}

/**
Expand Down

0 comments on commit 94fcc51

Please sign in to comment.