diff options
| author | Albert <bassmann1911@gmail.com> | 2026-09-02 21:41:36 +0200 |
|---|---|---|
| committer | Albert <bassmann1911@gmail.com> | 2026-09-02 21:41:36 +0200 |
| commit | 064de2172ebdad0b9dfc946fed8e95f59efb9b33 (patch) | |
| tree | 97bcd3a30c02b17fc1a5938556df0ac59c5eb714 /tests | |
| parent | b3766b00d4c5ae38589774b30f4f1e0579a9619f (diff) | |
Apply the resize sharpening kernel directly instead of via SKImageFilter
Since the SkiaSharp 3 update the MatrixConvolution image filter used in
SkiaEncoder.ResizeImage no longer has a fast CPU path: on the software
rasterizer it takes about 4.5 seconds per megapixel-sized image, which
turns every cold image request into a multi-second operation and makes
first-time loads of a library view take minutes.
Draw the resize without the paint filter and apply the identical 3x3
kernel (same weights, clamped edges, alpha included) directly on the
resized pixels instead. This drops a cold 1000x1500 -> 663x995 poster
render from ~4.6s to well under a second; the convolution pass itself
takes ~86ms. Output is visually unchanged.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/Jellyfin.Drawing.Skia.Tests/SkiaEncoderSharpenTests.cs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/Jellyfin.Drawing.Skia.Tests/SkiaEncoderSharpenTests.cs b/tests/Jellyfin.Drawing.Skia.Tests/SkiaEncoderSharpenTests.cs new file mode 100644 index 0000000000..72e555cc72 --- /dev/null +++ b/tests/Jellyfin.Drawing.Skia.Tests/SkiaEncoderSharpenTests.cs @@ -0,0 +1,73 @@ +using SkiaSharp; +using Xunit; + +namespace Jellyfin.Drawing.Skia.Tests; + +public class SkiaEncoderSharpenTests +{ + private static SKBitmap CreateBitmap(int width, int height, SKColor fill) + { + var bitmap = new SKBitmap(new SKImageInfo(width, height, SKColorType.Rgba8888, SKAlphaType.Premul)); + using var canvas = new SKCanvas(bitmap); + canvas.Clear(fill); + return bitmap; + } + + [Fact] + public void SharpenInPlace_UniformImage_IsUnchanged() + { + // 1.4 * v - 4 * 0.1 * v = v for any uniform value. + using var bitmap = CreateBitmap(8, 8, new SKColor(100, 150, 200)); + + SkiaEncoder.SharpenInPlace(bitmap); + + for (var y = 0; y < bitmap.Height; y++) + { + for (var x = 0; x < bitmap.Width; x++) + { + Assert.Equal(new SKColor(100, 150, 200), bitmap.GetPixel(x, y)); + } + } + } + + [Fact] + public void SharpenInPlace_BrightPixelOnDarkBackground_SharpensEdge() + { + using var bitmap = CreateBitmap(5, 5, new SKColor(50, 50, 50)); + bitmap.SetPixel(2, 2, new SKColor(250, 250, 250, 255)); + + SkiaEncoder.SharpenInPlace(bitmap); + + // Center: 1.4 * 250 - 0.1 * 4 * 50 = 330 -> clamped to 255. + Assert.Equal(new SKColor(255, 255, 255), bitmap.GetPixel(2, 2)); + // Direct neighbor: 1.4 * 50 - 0.1 * (250 + 3 * 50) = 30. + Assert.Equal(new SKColor(30, 30, 30), bitmap.GetPixel(1, 2)); + // Far corner is only surrounded by background: unchanged. + Assert.Equal(new SKColor(50, 50, 50), bitmap.GetPixel(0, 0)); + } + + [Fact] + public void SharpenInPlace_EdgePixels_ClampOutOfBoundsTaps() + { + // A corner pixel reuses itself for the two out-of-bounds taps: + // 1.4 * v - 0.1 * (2 * v + right + down). + using var bitmap = CreateBitmap(3, 3, new SKColor(100, 100, 100)); + bitmap.SetPixel(0, 0, new SKColor(200, 200, 200, 255)); + + SkiaEncoder.SharpenInPlace(bitmap); + + // 1.4 * 200 - 0.1 * (200 + 200 + 100 + 100) = 220. + Assert.Equal(new SKColor(220, 220, 220), bitmap.GetPixel(0, 0)); + } + + [Fact] + public void SharpenInPlace_UnsupportedColorType_IsLeftUntouched() + { + using var bitmap = new SKBitmap(new SKImageInfo(4, 4, SKColorType.Gray8, SKAlphaType.Opaque)); + bitmap.Erase(new SKColor(80, 80, 80)); + + SkiaEncoder.SharpenInPlace(bitmap); + + Assert.Equal(80, bitmap.GetPixel(1, 1).Red); + } +} |
