From f15fa8f79ca3427ff27ac4179d01a213d9bb112e Mon Sep 17 00:00:00 2001 From: Manbendra Date: Sat, 5 Jul 2025 10:10:31 +0530 Subject: [PATCH] making kotlin decoder thread safe --- .../java/com/wolt/blurhashkt/BlurHashDecoder.kt | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt b/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt index 3278868b..f56a593d 100644 --- a/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt +++ b/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt @@ -2,6 +2,7 @@ package com.wolt.blurhashkt import android.graphics.Bitmap import android.graphics.Color +import java.util.concurrent.ConcurrentHashMap import kotlin.math.cos import kotlin.math.pow import kotlin.math.withSign @@ -11,8 +12,8 @@ object BlurHashDecoder { // cache Math.cos() calculations to improve performance. // The number of calculations can be huge for many bitmaps: width * height * numCompX * numCompY * 2 * nBitmaps // the cache is enabled by default, it is recommended to disable it only when just a few images are displayed - private val cacheCosinesX = HashMap() - private val cacheCosinesY = HashMap() + private val cacheCosinesX = ConcurrentHashMap() + private val cacheCosinesY = ConcurrentHashMap() /** * Clear calculations stored in memory cache. @@ -127,14 +128,14 @@ object BlurHashDecoder { imageArray[x + width * y] = Color.rgb(linearToSrgb(r), linearToSrgb(g), linearToSrgb(b)) } } + cacheCosinesX[width * numCompX] = cosinesX + cacheCosinesY[height * numCompY] = cosines return Bitmap.createBitmap(imageArray, width, height, Bitmap.Config.ARGB_8888) } private fun getArrayForCosinesY(calculate: Boolean, height: Int, numCompY: Int) = when { calculate -> { - DoubleArray(height * numCompY).also { - cacheCosinesY[height * numCompY] = it - } + DoubleArray(height * numCompY) } else -> { cacheCosinesY[height * numCompY]!! @@ -143,9 +144,7 @@ object BlurHashDecoder { private fun getArrayForCosinesX(calculate: Boolean, width: Int, numCompX: Int) = when { calculate -> { - DoubleArray(width * numCompX).also { - cacheCosinesX[width * numCompX] = it - } + DoubleArray(width * numCompX) } else -> cacheCosinesX[width * numCompX]!! }