Skip to content

Scale images down only once in the SkiaSharp encoder - #164

Open
GhostPratt wants to merge 1 commit into
Bond-009:masterfrom
GhostPratt:fix/single-downscale
Open

GhostPratt wants to merge 1 commit into
Bond-009:masterfrom
GhostPratt:fix/single-downscale

Conversation

@GhostPratt

Copy link
Copy Markdown

The size limited Encode overloads in BlurHashSharp.SkiaSharp shrink 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.Resize returns null and EncodeInternal throws a NullReferenceException. 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 DecodeScaled that 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 the InternalsVisibleTo in 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.cs is the whole change. Worth a close look: the early return for images within the limit (same path as before), and that GetScaleDimensions is now fed the original width and height.

tests/BlurHashSharp.SkiaSharp.Tests/BlurHashEncoderScalingTests.cs generates its images at run time with SkiaSharp, so it needs no FFMPEG_SAMPLES_PATH and 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant