aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlbert <bassmann1911@gmail.com>2026-09-02 21:41:36 +0200
committerAlbert <bassmann1911@gmail.com>2026-09-02 21:41:36 +0200
commit064de2172ebdad0b9dfc946fed8e95f59efb9b33 (patch)
tree97bcd3a30c02b17fc1a5938556df0ac59c5eb714 /src
parentb3766b00d4c5ae38589774b30f4f1e0579a9619f (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 'src')
-rw-r--r--src/Jellyfin.Drawing.Skia/SkiaEncoder.cs94
1 files changed, 78 insertions, 16 deletions
diff --git a/src/Jellyfin.Drawing.Skia/SkiaEncoder.cs b/src/Jellyfin.Drawing.Skia/SkiaEncoder.cs
index 3e353db8de..4cdff055f4 100644
--- a/src/Jellyfin.Drawing.Skia/SkiaEncoder.cs
+++ b/src/Jellyfin.Drawing.Skia/SkiaEncoder.cs
@@ -1,8 +1,10 @@
using System;
+using System.Buffers;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
+using System.Runtime.InteropServices;
using BlurHashSharp.SkiaSharp;
using Jellyfin.Extensions;
using MediaBrowser.Common.Configuration;
@@ -22,22 +24,15 @@ namespace Jellyfin.Drawing.Skia;
public class SkiaEncoder : IImageEncoder
{
private const string SvgFormat = "svg";
+
+ // The light sharpening kernel applied after resizing, see ResizeImage.
+ private const float SharpenCenterWeight = 1.4f;
+ private const float SharpenNeighborWeight = -0.1f;
+
private static readonly HashSet<string> _transparentImageTypes = new(StringComparer.OrdinalIgnoreCase) { ".png", ".gif", ".webp" };
private readonly ILogger<SkiaEncoder> _logger;
private readonly IApplicationPaths _appPaths;
private static readonly SKTypeface?[] _typefaces = InitializeTypefaces();
- private static readonly SKImageFilter _imageFilter = SKImageFilter.CreateMatrixConvolution(
- new SKSizeI(3, 3),
- [
- 0, -.1f, 0,
- -.1f, 1.4f, -.1f,
- 0, -.1f, 0
- ],
- 1f,
- 0f,
- new SKPointI(1, 1),
- SKShaderTileMode.Clamp,
- true);
/// <summary>
/// The default sampling options, equivalent to old high quality filter settings when upscaling.
@@ -561,8 +556,8 @@ public class SkiaEncoder : IImageEncoder
/// <returns>The resized image.</returns>
internal static SKImage ResizeImage(SKBitmap source, SKImageInfo targetInfo, bool isAntialias = false, bool isDither = false)
{
- using var surface = SKSurface.Create(targetInfo);
- using var canvas = surface.Canvas;
+ using var target = new SKBitmap(targetInfo);
+ using var canvas = new SKCanvas(target);
using var paint = new SKPaint();
paint.IsAntialias = isAntialias;
paint.IsDither = isDither;
@@ -574,7 +569,6 @@ public class SkiaEncoder : IImageEncoder
? DefaultSamplingOptions
: UpscaleSamplingOptions;
- paint.ImageFilter = _imageFilter;
canvas.DrawBitmap(
source,
SKRect.Create(0, 0, source.Width, source.Height),
@@ -582,7 +576,75 @@ public class SkiaEncoder : IImageEncoder
samplingOptions,
paint);
- return surface.Snapshot();
+ SharpenInPlace(target);
+
+ return SKImage.FromBitmap(target);
+ }
+
+ /// <summary>
+ /// Applies the light 3x3 sharpening kernel to the bitmap in place.
+ ///
+ /// This is equivalent to the SKImageFilter.CreateMatrixConvolution paint filter that
+ /// was previously part of the resize draw call. Since the SkiaSharp 3 update that
+ /// filter no longer has a fast CPU path and takes multiple seconds per image on the
+ /// software rasterizer, so the same kernel is applied directly instead.
+ /// </summary>
+ /// <param name="bitmap">The bitmap to sharpen. Must use a color type with four bytes per pixel; other color types are returned unchanged.</param>
+ internal static void SharpenInPlace(SKBitmap bitmap)
+ {
+ if (bitmap.BytesPerPixel != 4)
+ {
+ return;
+ }
+
+ var width = bitmap.Width;
+ var height = bitmap.Height;
+ var stride = bitmap.RowBytes;
+ var pixels = bitmap.GetPixels();
+ if (width == 0 || height == 0 || pixels == IntPtr.Zero)
+ {
+ return;
+ }
+
+ var length = stride * height;
+ var source = ArrayPool<byte>.Shared.Rent(length);
+ var result = ArrayPool<byte>.Shared.Rent(length);
+ try
+ {
+ Marshal.Copy(pixels, source, 0, length);
+
+ for (var y = 0; y < height; y++)
+ {
+ // The kernel clamps at the edges: out-of-bounds taps reuse the edge pixel.
+ var row = y * stride;
+ var up = y == 0 ? row : row - stride;
+ var down = y == height - 1 ? row : row + stride;
+
+ for (var x = 0; x < width; x++)
+ {
+ var col = x * 4;
+ var left = x == 0 ? col : col - 4;
+ var right = x == width - 1 ? col : col + 4;
+
+ for (var channel = 0; channel < 4; channel++)
+ {
+ var value = (SharpenCenterWeight * source[row + col + channel])
+ + (SharpenNeighborWeight * (source[up + col + channel]
+ + source[down + col + channel]
+ + source[row + left + channel]
+ + source[row + right + channel]));
+ result[row + col + channel] = (byte)Math.Clamp((int)(value + 0.5f), 0, 255);
+ }
+ }
+ }
+
+ Marshal.Copy(result, 0, pixels, length);
+ }
+ finally
+ {
+ ArrayPool<byte>.Shared.Return(source);
+ ArrayPool<byte>.Shared.Return(result);
+ }
}
/// <inheritdoc/>