diff --git a/library/src/main/java/com/davemorrissey/labs/subscaleview/SubsamplingScaleImageView.java b/library/src/main/java/com/davemorrissey/labs/subscaleview/SubsamplingScaleImageView.java index 1303d137..8ee1e987 100755 --- a/library/src/main/java/com/davemorrissey/labs/subscaleview/SubsamplingScaleImageView.java +++ b/library/src/main/java/com/davemorrissey/labs/subscaleview/SubsamplingScaleImageView.java @@ -171,6 +171,8 @@ public class SubsamplingScaleImageView extends View { private int minimumScaleType = SCALE_TYPE_CENTER_INSIDE; // Whether to crop borders. private boolean cropBorders = false; + // Whether to decode to hardware bitmap + private boolean hardwareConfig = true; private int maxTileWidth = TILE_SIZE_AUTO; private int maxTileHeight = TILE_SIZE_AUTO; // An executor service for loading of images @@ -1541,6 +1543,15 @@ public void setCropBorders(boolean cropBorders) { this.cropBorders = cropBorders; } + /** + * Set if we want to use hardware bitmap config for decoded bitmap regions. + * + * @param hardwareConfig Whether to use hardware bitmap config. + */ + public void setHardwareConfig(boolean hardwareConfig) { + this.hardwareConfig = hardwareConfig; + } + /** * By default the View automatically calculates the optimal tile size. Set this to override this, and force an upper limit to the dimensions of the generated tiles. Passing {@link #TILE_SIZE_AUTO} will re-enable the default behaviour. * @@ -2760,7 +2771,7 @@ protected int[] doInBackground(Void... params) { InputProvider provider = providerRef.get(); if (context != null && view != null && provider == view.provider) { view.debug("TilesInitTask.doInBackground"); - decoder = new Decoder(view.cropBorders, view.displayProfile.toByteArray()); + decoder = new Decoder(view.cropBorders, view.hardwareConfig, view.displayProfile.toByteArray()); Point dimensions = decoder.init(context, provider); int sWidth = dimensions.x; int sHeight = dimensions.y; diff --git a/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/Decoder.kt b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/Decoder.kt index 85e375e4..bba57a4f 100755 --- a/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/Decoder.kt +++ b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/Decoder.kt @@ -12,6 +12,7 @@ import tachiyomi.decoder.ImageDecoder.Companion.newInstance class Decoder( private val cropBorders: Boolean, + private val hardwareConfig: Boolean, private val displayProfile: ByteArray, ) : ImageRegionDecoder { @@ -53,11 +54,16 @@ class Decoder( * @return The decoded region. It is safe to return null if decoding fails. */ override fun decodeRegion(sRect: Rect, sampleSize: Int): Bitmap { - val bitmap = decoder?.decode(sRect, sampleSize) - if (Build.VERSION.SDK_INT >= 26) { - bitmap?.copy(Bitmap.Config.HARDWARE, false)?.let { return it; } + var bitmap = decoder?.decode(sRect, sampleSize) + check(bitmap != null) { "Failed to decode region" } + if (hardwareConfig && Build.VERSION.SDK_INT >= 26) { + val hwBitmap = bitmap.copy(Bitmap.Config.HARDWARE, false) + if (hwBitmap != null) { + bitmap.recycle() + bitmap = hwBitmap + } } - return bitmap ?: error("Null region bitmap") + return bitmap } /**