aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Drawing.Skia
diff options
context:
space:
mode:
authorJesús Higueras <jesus@dabbleam.com>2020-03-23 20:05:49 +0100
committerVasily <just.one.man@yandex.ru>2020-05-18 23:21:37 +0300
commitb9fc0d26287e46017515e4ac3e569ca2c60f622f (patch)
tree7168d3272a2ead08c2cb6171d1d2f9dc794df262 /Jellyfin.Drawing.Skia
parent2d2c1d94733a1959f84c1cc26f8051d0be35cfb4 (diff)
Add BlurHash support to backend
Diffstat (limited to 'Jellyfin.Drawing.Skia')
-rw-r--r--Jellyfin.Drawing.Skia/Jellyfin.Drawing.Skia.csproj2
-rw-r--r--Jellyfin.Drawing.Skia/SkiaEncoder.cs48
2 files changed, 49 insertions, 1 deletions
diff --git a/Jellyfin.Drawing.Skia/Jellyfin.Drawing.Skia.csproj b/Jellyfin.Drawing.Skia/Jellyfin.Drawing.Skia.csproj
index a6e1f490a..9f0e3a004 100644
--- a/Jellyfin.Drawing.Skia/Jellyfin.Drawing.Skia.csproj
+++ b/Jellyfin.Drawing.Skia/Jellyfin.Drawing.Skia.csproj
@@ -21,6 +21,8 @@
<PackageReference Include="SkiaSharp" Version="1.68.1" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="1.68.1" />
<PackageReference Include="Jellyfin.SkiaSharp.NativeAssets.LinuxArm" Version="1.68.1" />
+
+ <PackageReference Include="Blurhash.Core" Version="*" />
</ItemGroup>
<ItemGroup>
diff --git a/Jellyfin.Drawing.Skia/SkiaEncoder.cs b/Jellyfin.Drawing.Skia/SkiaEncoder.cs
index 5c7462ee2..1d7307863 100644
--- a/Jellyfin.Drawing.Skia/SkiaEncoder.cs
+++ b/Jellyfin.Drawing.Skia/SkiaEncoder.cs
@@ -1,7 +1,11 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+using Blurhash.Core;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Extensions;
@@ -15,7 +19,7 @@ namespace Jellyfin.Drawing.Skia
/// <summary>
/// Image encoder that uses <see cref="SkiaSharp"/> to manipulate images.
/// </summary>
- public class SkiaEncoder : IImageEncoder
+ public class SkiaEncoder : CoreEncoder, IImageEncoder
{
private static readonly HashSet<string> _transparentImageTypes
= new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".png", ".gif", ".webp" };
@@ -229,6 +233,48 @@ namespace Jellyfin.Drawing.Skia
}
}
+ /// <inheritdoc />
+ /// <exception cref="ArgumentNullException">The path is null.</exception>
+ /// <exception cref="FileNotFoundException">The path is not valid.</exception>
+ /// <exception cref="SkiaCodecException">The file at the specified path could not be used to generate a codec.</exception>
+ [SuppressMessage("Microsoft.Performance", "CA1814:PreferJaggedArraysOverMultidimensional")]
+ public string GetImageHash(string path)
+ {
+ if (path == null)
+ {
+ throw new ArgumentNullException(nameof(path));
+ }
+
+ if (!File.Exists(path))
+ {
+ throw new FileNotFoundException("File not found", path);
+ }
+
+ using (var bitmap = GetBitmap(path, false, false, null))
+ {
+ if (bitmap == null)
+ {
+ throw new ArgumentOutOfRangeException($"Skia unable to read image {path}");
+ }
+
+ var width = bitmap.Width;
+ var height = bitmap.Height;
+ var pixels = new Pixel[width, height];
+ Parallel.ForEach(Enumerable.Range(0, height), y =>
+ {
+ for (var x = 0; x < width; x++)
+ {
+ var color = bitmap.GetPixel(x, y);
+ pixels[x, y].Red = MathUtils.SRgbToLinear(color.Red);
+ pixels[x, y].Green = MathUtils.SRgbToLinear(color.Green);
+ pixels[x, y].Blue = MathUtils.SRgbToLinear(color.Blue);
+ }
+ });
+
+ return CoreEncode(pixels, 4, 4);
+ }
+ }
+
private static bool HasDiacritics(string text)
=> !string.Equals(text, text.RemoveDiacritics(), StringComparison.Ordinal);