Repository: woltapp/blurhash
File: Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt (Kotlin)

Code before:
package com.wolt.blurhashkt

import android.graphics.Bitmap
import android.graphics.Color
import java.util.*
import kotlin.math.cos
import kotlin.math.pow
import kotlin.math.withSign

// this is to optimize the number of calculations for "Math.cos()",
// is is slow and for many images with same size it can be cached, improving performance.
// the improvement can be noticed with images bigger than 80x80
private const val USE_CACHE_FOR_MATH_COS = true

object BlurHashDecoder {

    private val cacheCosinesX = HashMap<Int, DoubleArray>()
    private val cacheCosinesY = HashMap<Int, DoubleArray>()

    fun decode(blurHash: String?, width: Int, height: Int, punch: Float = 1f): Bitmap? {
        if (blurHash == null || blurHash.length < 6) {
            return null
        }
        val numCompEnc = decode83(blurHash, 0, 1)
        val numCompX = (numCompEnc % 9) + 1
        val numCompY = (numCompEnc / 9) + 1
        if (blurHash.length != 4 + 2 * numCompX * numCompY) {
            return null
        }
        val maxAcEnc = decode83(blurHash, 1, 2)
        val maxAc = (maxAcEnc + 1) / 166f
        val colors = Array(numCompX * numCompY) { i ->
            if (i == 0) {
                val colorEnc = decode83(blurHash, 2, 6)
                decodeDc(colorEnc)
            } else {
                val from = 4 + i * 2

Review comment (performance):
Why would you decode to an image that big? From the README:
> We usually decode placeholders that are 32 or even 20 pixels wide, and then let the UI layer scale them up

I would love to see a more elaborate benchmark that shows its faster, because that caching mechanism adds overhead as well and may led to cache misses. Is calculating cosine really that expensive?

Code after:
package com.wolt.blurhashkt

import android.graphics.Bitmap
import android.graphics.Color
import kotlin.math.cos
import kotlin.math.pow
import kotlin.math.withSign

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<Int, DoubleArray>()
    private val cacheCosinesY = HashMap<Int, DoubleArray>()

    /**
     * Clear calculations stored in memory cache.
     * The cache is not big, but will increase when many image sizes are used,
     * if the app needs memory it is recommended to clear it.
     */
    fun clearCache() {
        cacheCosinesX.clear()
        cacheCosinesY.clear()
    }

    /**
     * Decode a blur hash into a new bitmap.
     *
     * @param useCache use in memory cache for the calculated math, reused by images with same size.
     *                 if the cache does not exist yet it will be created and populated with new calculations.
     *                 By default it is true.
     */
    fun decode(blurHash: String?, width: Int, height: Int, punch: Float = 1f, useCache: Boolean = true): Bitmap? {
        if (blurHash == null || blurHash.length < 6) {
            return null
        }