Skip to content

Commit

Permalink
Fixes blurhash rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed Dec 6, 2024
1 parent e501101 commit 15b527c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ fun VideoDisplay(
authorName = note.author?.toBestDisplayName(),
artworkUri = imeta.image.firstOrNull(),
mimeType = imeta.mimeType,
blurhash = imeta.blurhash,
)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@
*/
package com.vitorpamplona.amethyst.commons.preview

import androidx.test.ext.junit.runners.AndroidJUnit4
import junit.framework.TestCase.assertTrue
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.math.roundToInt

@RunWith(AndroidJUnit4::class)
class BlurhashTest {
val warmHex = "[45#Y7_2^-xt%OSb%4S0-qt0xbotaRInV|M{RlD~M{M_IVIUNHM{M{M{M{RjNGRkoyj]o[t8tPt8"
val testHex = "|NHL-]~pabocs+jDM{j?of4T9ZR+WBWZbdR-WCog04ITn\$t6t6t6t6oJoLZ}?bIUWBs:M{WCogRjs:s+o#R+WBoft7axWBx]IV%LogM{t5xaWBay%KRjxus.WCNGWWt7j[j]s+R-S5ofjYV@j[ofD%t8RPoJt7t7R*WCof"

@Test
fun testAspectRatioWarm() {
Assert.assertEquals(0.44444445f, BlurHashDecoderOld.aspectRatio(warmHex)!!, 0.001f)
Assert.assertEquals(0.44444445f, BlurHashDecoder.aspectRatio(warmHex)!!, 0.001f)
}

Expand All @@ -45,9 +47,18 @@ class BlurhashTest {
assertTrue(bmp1!!.sameAs(bmp2!!))
}

@Test
fun testDecoderWarm25Pixels() {
val aspectRatio = BlurHashDecoder.aspectRatio(warmHex) ?: 1.0f

val bmp1 = BlurHashDecoderOld.decode(warmHex, 25, (25 * (1 / aspectRatio)).roundToInt())
val bmp2 = BlurHashDecoder.decodeKeepAspectRatio(warmHex, 25)

assertTrue(bmp1!!.sameAs(bmp2!!))
}

@Test
fun testAspectRatioTest() {
Assert.assertEquals(1.0f, BlurHashDecoderOld.aspectRatio(testHex)!!)
Assert.assertEquals(1.0f, BlurHashDecoder.aspectRatio(testHex)!!)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package com.vitorpamplona.amethyst.commons.preview

import android.graphics.Bitmap
import android.graphics.Color
import kotlin.math.cos
import kotlin.math.pow
import kotlin.math.roundToInt
Expand Down Expand Up @@ -60,6 +59,28 @@ object BlurHashDecoder {
return numCompX.toFloat() / numCompY.toFloat()
}

fun computeColors(
numCompX: Int,
numCompY: Int,
blurHash: String,
): Array<FloatArray> {
val maxAc = (decode83At(blurHash, 1) + 1) / 166f
return Array(numCompX * numCompY) { i ->
if (i == 0) {
decodeDc(decode83(blurHash, 2, 6))
} else {
decodeAc(decode83Fixed2(blurHash, 4 + i * 2), maxAc)
}
}
}

fun computeNumComponets(blurHash: String): Pair<Int, Int> {
val numCompEnc = decode83At(blurHash, 0)
val numCompX = (numCompEnc % 9) + 1
val numCompY = (numCompEnc / 9) + 1
return Pair(numCompX, numCompY)
}

/**
* Decode a blur hash into a new bitmap.
*
Expand All @@ -75,24 +96,14 @@ object BlurHashDecoder {
if (blurHash == null || blurHash.length < 6) {
return null
}
val numCompEnc = decode83At(blurHash, 0)
val numCompX = (numCompEnc % 9) + 1
val numCompY = (numCompEnc / 9) + 1
val (numCompX, numCompY) = computeNumComponets(blurHash)
if (blurHash.length != 4 + 2 * numCompX * numCompY) {
return null
}
val height = (100 * (1 / (numCompX.toFloat() / numCompY.toFloat()))).roundToInt()
val maxAc = (decode83At(blurHash, 1) + 1) / 166f

val colors =
Array(numCompX * numCompY) { i ->
if (i == 0) {
decodeDc(decode83(blurHash, 2, 6))
} else {
decodeAc(decode83Fixed2(blurHash, 4 + i * 2), maxAc)
}
}
return composeBitmap(width, height, numCompX, numCompY, colors, useCache)
val height = (width * (1 / (numCompX.toFloat() / numCompY.toFloat()))).roundToInt()
val colors = computeColors(numCompX, numCompY, blurHash)
val imageArray = composeImageArray(width, height, numCompX, numCompY, colors, useCache)
return Bitmap.createBitmap(imageArray, width, height, Bitmap.Config.ARGB_8888)
}

private fun decode83At(
Expand Down Expand Up @@ -149,14 +160,14 @@ object BlurHashDecoder {

private fun signedPow2(value: Float) = value.pow(2f).withSign(value)

private fun composeBitmap(
private fun composeImageArray(
width: Int,
height: Int,
numCompX: Int,
numCompY: Int,
colors: Array<FloatArray>,
useCache: Boolean,
): Bitmap {
): IntArray {
// use an array for better performance when writing pixel colors
val imageArray = IntArray(width * height)
val calculateCosX = !useCache || !cacheCosinesX.containsKey(width * numCompX)
Expand Down Expand Up @@ -185,12 +196,19 @@ object BlurHashDecoder {
}
}

imageArray[x + width * y] = Color.rgb(linearToSrgb(r), linearToSrgb(g), linearToSrgb(b))
imageArray[x + width * y] = rgb(linearToSrgb(r), linearToSrgb(g), linearToSrgb(b))
}
}
return Bitmap.createBitmap(imageArray, width, height, Bitmap.Config.ARGB_8888)

return imageArray
}

fun rgb(
red: Int,
green: Int,
blue: Int,
): Int = -0x1000000 or (red shl 16) or (green shl 8) or blue

private fun getArrayForCosinesY(
calculate: Boolean,
height: Int,
Expand Down

0 comments on commit 15b527c

Please sign in to comment.