Repository navigation
Scale images down only once in the SkiaSharp encoder - #164
Open
GhostPratt wants to merge 1 commit into
Open
GhostPratt wants to merge 1 commit into
GhostPratt wants to merge 1 commit into
Conversation
The size limited Encode overloads applied the scale factor to a bitmap the codec had already shrunk, so extreme aspect ratios ended up asking for a 0 pixel side and crashed on a null Resize result.
7 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The size limited
Encodeoverloads inBlurHashSharp.SkiaSharpshrink the image twice: they ask the codec to decode at the scaled size, then apply the same scale factor again to the bitmap they got back. For a wide WebP logo like 4319x481 with a 128x128 limit, the codec already returns 128x14, the second pass asks for 4x0,SKBitmap.Resizereturns null andEncodeInternalthrows aNullReferenceException. Jellyfin hits this on every library scan for some logos, see jellyfin/jellyfin#12106.The target size is now computed once from the codec's original dimensions, so the bitmap handed to the core encoder is the same size no matter how much of the scaling the codec did itself. Each side is clamped to at least 1 so a 10000x1 image can't round down to a zero height either.
Decisions worth knowing
The decode and shrink logic moved into an internal
DecodeScaledthat returns the bitmap. That lets the tests assert the exact size (128x14 for WebP, PNG and JPEG alike) instead of comparing hashes, since lossy WebP and PNG hash a few characters apart and there's no sane tolerance for a blurhash string. Hence theInternalsVisibleToin the csproj.The resize is skipped when the codec already delivered the target size, which is what WebP does.
Hashes for images above the limit in formats whose codec scales (WebP, JPEG) will change, since they were previously computed from an image much smaller than requested (16x16 for a 1000x1000 WebP). PNG output and anything within the limit is unchanged.
The clamp lives in the SkiaSharp encoder, not in
ScaleHelper, which keeps returning plain rounded values.Review guide
src/BlurHashSharp.SkiaSharp/BlurHashEncoder.csis the whole change. Worth a close look: the early return for images within the limit (same path as before), and thatGetScaleDimensionsis now fed the original width and height.tests/BlurHashSharp.SkiaSharp.Tests/BlurHashEncoderScalingTests.csgenerates its images at run time with SkiaSharp, so it needs noFFMPEG_SAMPLES_PATHand no fixtures. The csproj change is mechanical.Known gaps
CI runs
dotnet test --filter 'BlurHashSharp.Tests', which only matches the core project, so the new tests won't run there. Happy to widen the filter here or in a follow up if you want.#155 touches the same method, so whichever lands second needs a small rebase.