Merge pull request #17775 from slevin-7/fix-skia-sharpen-perf

Apply the resize sharpening kernel directly instead of via SKImageFilter
This commit is contained in:
Cody Robibero
2026-09-07 17:43:48 -04:00
committed by GitHub
2 changed files with 151 additions and 16 deletions
@@ -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);
}
}