1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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);
}
}
|