feat: add ThumbHash support alongside BlurHash across events, uploads, and UI

Adds a parallel ThumbHash placeholder everywhere BlurHash is already used.
Remote events now carry both hashes; renderers prefer thumbhash when
available and fall back to blurhash. The MIP-04 `thumbhash` imeta field
that was previously reserved-but-unused is now wired end-to-end.

- New ThumbHash encoder/decoder in commons/commonMain (no new Gradle dep)
- New ThumbhashTag and thumbhash accessors/builders across NIP-17, 68,
  71, 94, 95, 99, the experimental profile gallery, and MIP-04
- RichTextParser + MediaContentModels carry a thumbhash field alongside
  blurhash so every downstream composable can pick its preferred placeholder
- New ThumbHashFetcher (Android + Desktop) registered with Coil, plus a
  small placeholderModel(thumbhash, blurhash) helper centralising the
  "prefer thumbhash, fall back to blurhash" rule
- Rename BlurhashMetadataCalculator -> PreviewMetadataCalculator; the
  calculator decodes the bitmap / video thumbnail once and runs both
  encoders on the same pixels to keep upload cost flat
- DesktopMediaMetadata, MediaUploadResult, and FileHeader now surface
  both hashes through the NIP-96, Blossom, NIP-95, MIP-04, NIP-17 DM,
  Classifieds, picture, video, and long-form upload paths
- Round-trip unit test for the ThumbHash port
This commit is contained in:
Claude
2026-04-20 00:12:28 +00:00
parent 2d5097e8b0
commit b2f297b3bb
68 changed files with 1203 additions and 120 deletions
@@ -60,8 +60,10 @@ object DesktopImageLoaderSetup {
add(SkiaGifDecoder.Factory())
add(DesktopBase64Fetcher.Factory)
add(DesktopBlurHashFetcher.Factory)
add(DesktopThumbHashFetcher.Factory)
add(DesktopBase64Fetcher.BKeyer)
add(DesktopBlurHashFetcher.BKeyer)
add(DesktopThumbHashFetcher.TKeyer)
}.build()
private fun newMemoryCache(): MemoryCache {
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.desktop.service.images
import androidx.compose.runtime.Stable
import coil3.ImageLoader
import coil3.asImage
import coil3.decode.DataSource
import coil3.fetch.FetchResult
import coil3.fetch.Fetcher
import coil3.fetch.ImageFetchResult
import coil3.key.Keyer
import coil3.request.Options
import com.vitorpamplona.amethyst.commons.blurhash.toBufferedImage
import com.vitorpamplona.amethyst.commons.thumbhash.ThumbHashDecoder
data class ThumbhashWrapper(
val thumbhash: String,
)
@Stable
class DesktopThumbHashFetcher(
private val data: ThumbhashWrapper,
) : Fetcher {
override suspend fun fetch(): FetchResult? {
val hash = data.thumbhash
val platformImage = ThumbHashDecoder.decodeKeepAspectRatio(hash, 25) ?: return null
val bufferedImage = platformImage.toBufferedImage()
val bitmap = bufferedImageToSkiaBitmap(bufferedImage)
return ImageFetchResult(
image = bitmap.asImage(true),
isSampled = false,
dataSource = DataSource.MEMORY,
)
}
object Factory : Fetcher.Factory<ThumbhashWrapper> {
override fun create(
data: ThumbhashWrapper,
options: Options,
imageLoader: ImageLoader,
): Fetcher = DesktopThumbHashFetcher(data)
}
object TKeyer : Keyer<ThumbhashWrapper> {
override fun key(
data: ThumbhashWrapper,
options: Options,
): String = data.thumbhash
}
}
/**
* Pick the best Coil model for a media placeholder on Desktop.
*
* Prefers [ThumbhashWrapper] when a thumbhash is available (better quality, preserves aspect ratio
* and alpha) and falls back to [BlurhashWrapper] when only a blurhash is present.
*/
fun placeholderModel(
thumbhash: String?,
blurhash: String?,
): Any? =
when {
!thumbhash.isNullOrEmpty() -> ThumbhashWrapper(thumbhash)
!blurhash.isNullOrEmpty() -> BlurhashWrapper(blurhash)
else -> null
}
@@ -22,6 +22,7 @@ package com.vitorpamplona.amethyst.desktop.service.upload
import com.vitorpamplona.amethyst.commons.blurhash.toBlurhash
import com.vitorpamplona.amethyst.commons.blurhash.toPlatformImage
import com.vitorpamplona.amethyst.commons.thumbhash.toThumbhash
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import com.vitorpamplona.quartz.utils.sha256.sha256
import java.io.File
@@ -34,6 +35,7 @@ data class MediaMetadata(
val width: Int? = null,
val height: Int? = null,
val blurhash: String? = null,
val thumbhash: String? = null,
)
object DesktopMediaMetadata {
@@ -44,6 +46,7 @@ object DesktopMediaMetadata {
var width: Int? = null
var height: Int? = null
var blurhash: String? = null
var thumbhash: String? = null
if (mimeType.startsWith("image/")) {
try {
@@ -51,7 +54,9 @@ object DesktopMediaMetadata {
if (image != null) {
width = image.width
height = image.height
blurhash = image.toPlatformImage().toBlurhash()
val platformImage = image.toPlatformImage()
blurhash = runCatching { platformImage.toBlurhash() }.getOrNull()
thumbhash = runCatching { platformImage.toThumbhash() }.getOrNull()
}
} catch (_: Exception) {
}
@@ -64,6 +69,7 @@ object DesktopMediaMetadata {
width = width,
height = height,
blurhash = blurhash,
thumbhash = thumbhash,
)
}
@@ -358,6 +358,7 @@ private fun buildIMetaTags(results: List<UploadResult>): List<IMetaTag> =
props["dim"] = listOf("${meta.width}x${meta.height}")
}
meta.blurhash?.let { props["blurhash"] = listOf(it) }
meta.thumbhash?.let { props["thumbhash"] = listOf(it) }
IMetaTag(url = url, properties = props)
}
@@ -460,6 +461,7 @@ private fun buildPictureMetas(results: List<UploadResult>): List<com.vitorpamplo
},
hash = meta.sha256,
size = meta.size.toInt(),
thumbhash = meta.thumbhash,
)
}
@@ -825,6 +825,7 @@ private suspend fun sendEncryptedFiles(
null
},
blurhash = result.metadata.blurhash,
thumbhash = result.metadata.thumbhash,
originalHash = result.metadata.sha256,
)
account.sendNip17EncryptedFile(template)