From 3dbbe161c24a9edbbad82d605b463f723f14931a Mon Sep 17 00:00:00 2001 From: Fonger <5862369+Fonger@users.noreply.github.com> Date: Tue, 16 May 2023 13:12:40 +0800 Subject: [PATCH 1/2] Improve decode speed by avoiding re-calculating same cosine value --- Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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..eb45483a 100644 --- a/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt +++ b/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt @@ -114,9 +114,9 @@ object BlurHashDecoder { var g = 0f var b = 0f for (j in 0 until numCompY) { + val cosY = cosinesY.getCos(calculateCosY, j, numCompY, y, height) for (i in 0 until numCompX) { val cosX = cosinesX.getCos(calculateCosX, i, numCompX, x, width) - val cosY = cosinesY.getCos(calculateCosY, j, numCompY, y, height) val basis = (cosX * cosY).toFloat() val color = colors[j * numCompX + i] r += color[0] * basis From fbfe95fe173526c268df07c5b323c5f5f7dfaa19 Mon Sep 17 00:00:00 2001 From: Fonger <5862369+Fonger@users.noreply.github.com> Date: Tue, 16 May 2023 13:14:56 +0800 Subject: [PATCH 2/2] Improve decode speed by avoiding re-calculating same cosine value --- Swift/BlurHashDecode.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Swift/BlurHashDecode.swift b/Swift/BlurHashDecode.swift index 7fe3b398..70e8c3e8 100644 --- a/Swift/BlurHashDecode.swift +++ b/Swift/BlurHashDecode.swift @@ -37,8 +37,9 @@ extension UIImage { var b: Float = 0 for j in 0 ..< numY { + let basisY = cos(Float.pi * Float(y) * Float(j) / Float(height) for i in 0 ..< numX { - let basis = cos(Float.pi * Float(x) * Float(i) / Float(width)) * cos(Float.pi * Float(y) * Float(j) / Float(height)) + let basis = cos(Float.pi * Float(x) * Float(i) / Float(width)) * basisY) let colour = colours[i + j * numX] r += colour.0 * basis g += colour.1 * basis