sketch icon indicating copy to clipboard operation
sketch copied to clipboard

Blurhash support, contribution request

Open 0sten opened this issue 5 months ago • 9 comments

@panpf would you consider merging a contribution which adds a blurhash support?

0sten avatar Jul 18 '25 12:07 0sten

I'll consider it, please submit your merge request.

panpf avatar Jul 18 '25 16:07 panpf

All the changes I needed to make have been made in the dev-blurhash branch.

There are several important changes that you need to be aware of:

  1. Removed dependency on compose and related remember functions, because this module also needs to consider Android view scenarios
  2. The newBlurHashUri function adds url encoding to prevent the blurhash string from destroying the uri structure.
  3. The cache key in BlurHashStateImage is spliced with the bitmap size, and asynchronous decoding is removed. Asynchronous decoding cannot be used here. For specific reasons, please see the comments in the source code.

I still have a few questions about BlurHashUtil :

  1. The source code has been significantly modified compared to cbeyls. You may not be able to upgrade from cbeyls in the future. I hope it remains completely consistent. If you must modify it, you can keep most of the code consistent and only modify the necessary parts.
  2. The outputheight parameter in BlurHashUtil:163 is not used. You need to check whether there is a problem here.

panpf avatar Aug 13 '25 09:08 panpf

2 - outputwidth, outputheight and output (to decode smaller blurhash into bigger canvas) aren't needed since you suggested not to use sharing bitmaps, so could be removed. https://github.com/panpf/sketch/pull/265

1 - Beside the point above, I'd leave the code as it is now. Both have same extracted helping functions. In case new version of an algo is introduced, and modifying current algo won't be obvious, they could be easily compared in performance since have same set of input arguments (except original algo immediately sets pixels from array into bitmap).

0sten avatar Aug 13 '25 19:08 0sten

The dev branch has been updated and the relevant documentation is ready.

https://github.com/panpf/sketch/blob/dev/docs/blurhash.md

Please check if there are any problems.

panpf avatar Aug 14 '25 10:08 panpf

I see you switched to use ? as blurhash and size separator in blurhash uri, it seems there is an issue with parsing blurhash string uri in this case since question mark could be a part of blurhash string. For instance UHF5?xYk^6#M@-5b,1J5@[or[k6.};FxngOZ.

Image

0sten avatar Aug 14 '25 20:08 0sten

Yes, this is the rule of the URI protocol, the path and query must be separated by a question mark.

Image

So I uri-encoded blurhash in the newBlurHashUri function.

https://github.com/panpf/sketch/blob/d7a0f4dff2556b08207580403de3e621e95a4f43/sketch-blurhash/src/commonMain/kotlin/com/github/panpf/sketch/fetch/BlurHashUriFetcher.kt#L47

panpf avatar Aug 15 '25 01:08 panpf

Added in 4.4.0-alpha01.

panpf avatar Aug 15 '25 08:08 panpf

@0sten Version 4.4.0-alpha01 with blurhash has been out for a while now, and I wanted to know if you’ve been using or testing it. I’d love to hear your feedback.

panpf avatar Sep 10 '25 11:09 panpf

It works fine for my app, thanks! that's how I use it in grid's code:

AsyncImage(
    uri = uploadPresentable.imageThumbUri.uri,
    contentDescription = "uploadId= ${uploadPresentable.upload.uploadId}",
    modifier = Modifier.matchParentSize(),
    contentScale = ContentScale.Crop,
    state = rememberAsyncImageState(ComposableImageOptions {
        if (uploadPresentable.upload.meta.blurhash?.isNotEmpty() == true) {
            val blurhashSize = BLURHASH_IN_GRID_SIZE
            val blurHashStateImage = BlurHashStateImage(
                uploadPresentable.upload.meta.blurhash,
                blurhashSize
            )
            placeholder(blurHashStateImage)
            error(blurHashStateImage)
            fallback(blurHashStateImage)
        }
    })
)

https://github.com/user-attachments/assets/6e2424ee-9952-404f-a099-1b0e86d73a16

0sten avatar Sep 10 '25 17:09 0sten