mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-03-28 18:51:45 +01:00
Fixes blurhash rendering
This commit is contained in:
parent
e501101062
commit
15b527c58d
@ -106,6 +106,7 @@ fun VideoDisplay(
|
|||||||
authorName = note.author?.toBestDisplayName(),
|
authorName = note.author?.toBestDisplayName(),
|
||||||
artworkUri = imeta.image.firstOrNull(),
|
artworkUri = imeta.image.firstOrNull(),
|
||||||
mimeType = imeta.mimeType,
|
mimeType = imeta.mimeType,
|
||||||
|
blurhash = imeta.blurhash,
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -20,18 +20,20 @@
|
|||||||
*/
|
*/
|
||||||
package com.vitorpamplona.amethyst.commons.preview
|
package com.vitorpamplona.amethyst.commons.preview
|
||||||
|
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
import junit.framework.TestCase.assertTrue
|
import junit.framework.TestCase.assertTrue
|
||||||
import org.junit.Assert
|
import org.junit.Assert
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4::class)
|
||||||
class BlurhashTest {
|
class BlurhashTest {
|
||||||
val warmHex = "[45#Y7_2^-xt%OSb%4S0-qt0xbotaRInV|M{RlD~M{M_IVIUNHM{M{M{M{RjNGRkoyj]o[t8tPt8"
|
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"
|
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
|
@Test
|
||||||
fun testAspectRatioWarm() {
|
fun testAspectRatioWarm() {
|
||||||
Assert.assertEquals(0.44444445f, BlurHashDecoderOld.aspectRatio(warmHex)!!, 0.001f)
|
|
||||||
Assert.assertEquals(0.44444445f, BlurHashDecoder.aspectRatio(warmHex)!!, 0.001f)
|
Assert.assertEquals(0.44444445f, BlurHashDecoder.aspectRatio(warmHex)!!, 0.001f)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,9 +47,18 @@ class BlurhashTest {
|
|||||||
assertTrue(bmp1!!.sameAs(bmp2!!))
|
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
|
@Test
|
||||||
fun testAspectRatioTest() {
|
fun testAspectRatioTest() {
|
||||||
Assert.assertEquals(1.0f, BlurHashDecoderOld.aspectRatio(testHex)!!)
|
|
||||||
Assert.assertEquals(1.0f, BlurHashDecoder.aspectRatio(testHex)!!)
|
Assert.assertEquals(1.0f, BlurHashDecoder.aspectRatio(testHex)!!)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@
|
|||||||
package com.vitorpamplona.amethyst.commons.preview
|
package com.vitorpamplona.amethyst.commons.preview
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.graphics.Color
|
|
||||||
import kotlin.math.cos
|
import kotlin.math.cos
|
||||||
import kotlin.math.pow
|
import kotlin.math.pow
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
@ -60,6 +59,28 @@ object BlurHashDecoder {
|
|||||||
return numCompX.toFloat() / numCompY.toFloat()
|
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.
|
* Decode a blur hash into a new bitmap.
|
||||||
*
|
*
|
||||||
@ -75,24 +96,14 @@ object BlurHashDecoder {
|
|||||||
if (blurHash == null || blurHash.length < 6) {
|
if (blurHash == null || blurHash.length < 6) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
val numCompEnc = decode83At(blurHash, 0)
|
val (numCompX, numCompY) = computeNumComponets(blurHash)
|
||||||
val numCompX = (numCompEnc % 9) + 1
|
|
||||||
val numCompY = (numCompEnc / 9) + 1
|
|
||||||
if (blurHash.length != 4 + 2 * numCompX * numCompY) {
|
if (blurHash.length != 4 + 2 * numCompX * numCompY) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
val height = (100 * (1 / (numCompX.toFloat() / numCompY.toFloat()))).roundToInt()
|
val height = (width * (1 / (numCompX.toFloat() / numCompY.toFloat()))).roundToInt()
|
||||||
val maxAc = (decode83At(blurHash, 1) + 1) / 166f
|
val colors = computeColors(numCompX, numCompY, blurHash)
|
||||||
|
val imageArray = composeImageArray(width, height, numCompX, numCompY, colors, useCache)
|
||||||
val colors =
|
return Bitmap.createBitmap(imageArray, width, height, Bitmap.Config.ARGB_8888)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun decode83At(
|
private fun decode83At(
|
||||||
@ -149,14 +160,14 @@ object BlurHashDecoder {
|
|||||||
|
|
||||||
private fun signedPow2(value: Float) = value.pow(2f).withSign(value)
|
private fun signedPow2(value: Float) = value.pow(2f).withSign(value)
|
||||||
|
|
||||||
private fun composeBitmap(
|
private fun composeImageArray(
|
||||||
width: Int,
|
width: Int,
|
||||||
height: Int,
|
height: Int,
|
||||||
numCompX: Int,
|
numCompX: Int,
|
||||||
numCompY: Int,
|
numCompY: Int,
|
||||||
colors: Array<FloatArray>,
|
colors: Array<FloatArray>,
|
||||||
useCache: Boolean,
|
useCache: Boolean,
|
||||||
): Bitmap {
|
): IntArray {
|
||||||
// use an array for better performance when writing pixel colors
|
// use an array for better performance when writing pixel colors
|
||||||
val imageArray = IntArray(width * height)
|
val imageArray = IntArray(width * height)
|
||||||
val calculateCosX = !useCache || !cacheCosinesX.containsKey(width * numCompX)
|
val calculateCosX = !useCache || !cacheCosinesX.containsKey(width * numCompX)
|
||||||
@ -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(
|
private fun getArrayForCosinesY(
|
||||||
calculate: Boolean,
|
calculate: Boolean,
|
||||||
height: Int,
|
height: Int,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user