aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Drawing.Skia/SkiaEncoder.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Jellyfin.Drawing.Skia/SkiaEncoder.cs')
-rw-r--r--Jellyfin.Drawing.Skia/SkiaEncoder.cs76
1 files changed, 47 insertions, 29 deletions
diff --git a/Jellyfin.Drawing.Skia/SkiaEncoder.cs b/Jellyfin.Drawing.Skia/SkiaEncoder.cs
index a67118f18..ba9a5809f 100644
--- a/Jellyfin.Drawing.Skia/SkiaEncoder.cs
+++ b/Jellyfin.Drawing.Skia/SkiaEncoder.cs
@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
+using BlurHashSharp.SkiaSharp;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Extensions;
@@ -20,7 +21,7 @@ namespace Jellyfin.Drawing.Skia
private static readonly HashSet<string> _transparentImageTypes
= new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".png", ".gif", ".webp" };
- private readonly ILogger _logger;
+ private readonly ILogger<SkiaEncoder> _logger;
private readonly IApplicationPaths _appPaths;
/// <summary>
@@ -52,9 +53,7 @@ namespace Jellyfin.Drawing.Skia
"jpeg",
"jpg",
"png",
-
"dng",
-
"webp",
"gif",
"bmp",
@@ -63,10 +62,8 @@ namespace Jellyfin.Drawing.Skia
"ktx",
"pkm",
"wbmp",
-
- // TODO
- // Are all of these supported? https://github.com/google/skia/blob/master/infra/bots/recipes/test.py#L454
-
+ // TODO: check if these are supported on multiple platforms
+ // https://github.com/google/skia/blob/master/infra/bots/recipes/test.py#L454
// working on windows at least
"cr2",
"nef",
@@ -78,12 +75,21 @@ namespace Jellyfin.Drawing.Skia
=> new HashSet<ImageFormat>() { ImageFormat.Webp, ImageFormat.Jpg, ImageFormat.Png };
/// <summary>
- /// Test to determine if the native lib is available.
+ /// Check if the native lib is available.
/// </summary>
- public static void TestSkia()
+ /// <returns>True if the native lib is available, otherwise false.</returns>
+ public static bool IsNativeLibAvailable()
{
- // test an operation that requires the native library
- SKPMColor.PreMultiply(SKColors.Black);
+ try
+ {
+ // test an operation that requires the native library
+ SKPMColor.PreMultiply(SKColors.Black);
+ return true;
+ }
+ catch (Exception)
+ {
+ return false;
+ }
}
private static bool IsTransparent(SKColor color)
@@ -205,11 +211,6 @@ namespace Jellyfin.Drawing.Skia
/// <exception cref="SkiaCodecException">The file at the specified path could not be used to generate a codec.</exception>
public ImageDimensions GetImageSize(string path)
{
- if (path == null)
- {
- throw new ArgumentNullException(nameof(path));
- }
-
if (!File.Exists(path))
{
throw new FileNotFoundException("File not found", path);
@@ -225,6 +226,20 @@ 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>
+ public string GetImageBlurHash(int xComp, int yComp, string path)
+ {
+ if (path == null)
+ {
+ throw new ArgumentNullException(nameof(path));
+ }
+
+ return BlurHashEncoder.Encode(xComp, yComp, path);
+ }
+
private static bool HasDiacritics(string text)
=> !string.Equals(text, text.RemoveDiacritics(), StringComparison.Ordinal);
@@ -253,7 +268,7 @@ namespace Jellyfin.Drawing.Skia
return path;
}
- var tempPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + Path.GetExtension(path) ?? string.Empty);
+ var tempPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + Path.GetExtension(path));
Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
File.Copy(path, tempPath, true);
@@ -297,7 +312,7 @@ namespace Jellyfin.Drawing.Skia
/// <param name="orientation">The orientation of the image.</param>
/// <param name="origin">The detected origin of the image.</param>
/// <returns>The resulting bitmap of the image.</returns>
- internal SKBitmap Decode(string path, bool forceCleanBitmap, ImageOrientation? orientation, out SKEncodedOrigin origin)
+ internal SKBitmap? Decode(string path, bool forceCleanBitmap, ImageOrientation? orientation, out SKEncodedOrigin origin)
{
if (!File.Exists(path))
{
@@ -348,12 +363,17 @@ namespace Jellyfin.Drawing.Skia
return resultBitmap;
}
- private SKBitmap GetBitmap(string path, bool cropWhitespace, bool forceAnalyzeBitmap, ImageOrientation? orientation, out SKEncodedOrigin origin)
+ private SKBitmap? GetBitmap(string path, bool cropWhitespace, bool forceAnalyzeBitmap, ImageOrientation? orientation, out SKEncodedOrigin origin)
{
if (cropWhitespace)
{
using (var bitmap = Decode(path, forceAnalyzeBitmap, orientation, out origin))
{
+ if (bitmap == null)
+ {
+ return null;
+ }
+
return CropWhiteSpace(bitmap);
}
}
@@ -361,13 +381,11 @@ namespace Jellyfin.Drawing.Skia
return Decode(path, forceAnalyzeBitmap, orientation, out origin);
}
- private SKBitmap GetBitmap(string path, bool cropWhitespace, bool autoOrient, ImageOrientation? orientation)
+ private SKBitmap? GetBitmap(string path, bool cropWhitespace, bool autoOrient, ImageOrientation? orientation)
{
- SKEncodedOrigin origin;
-
if (autoOrient)
{
- var bitmap = GetBitmap(path, cropWhitespace, true, orientation, out origin);
+ var bitmap = GetBitmap(path, cropWhitespace, true, orientation, out var origin);
if (bitmap != null && origin != SKEncodedOrigin.TopLeft)
{
@@ -380,7 +398,7 @@ namespace Jellyfin.Drawing.Skia
return bitmap;
}
- return GetBitmap(path, cropWhitespace, false, orientation, out origin);
+ return GetBitmap(path, cropWhitespace, false, orientation, out _);
}
private SKBitmap OrientImage(SKBitmap bitmap, SKEncodedOrigin origin)
@@ -517,14 +535,14 @@ namespace Jellyfin.Drawing.Skia
/// <inheritdoc/>
public string EncodeImage(string inputPath, DateTime dateModified, string outputPath, bool autoOrient, ImageOrientation? orientation, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat)
{
- if (string.IsNullOrWhiteSpace(inputPath))
+ if (inputPath.Length == 0)
{
- throw new ArgumentNullException(nameof(inputPath));
+ throw new ArgumentException("String can't be empty.", nameof(inputPath));
}
- if (string.IsNullOrWhiteSpace(inputPath))
+ if (outputPath.Length == 0)
{
- throw new ArgumentNullException(nameof(outputPath));
+ throw new ArgumentException("String can't be empty.", nameof(outputPath));
}
var skiaOutputFormat = GetImageFormat(selectedOutputFormat);
@@ -538,7 +556,7 @@ namespace Jellyfin.Drawing.Skia
{
if (bitmap == null)
{
- throw new ArgumentOutOfRangeException($"Skia unable to read image {inputPath}");
+ throw new InvalidDataException($"Skia unable to read image {inputPath}");
}
var originalImageSize = new ImageDimensions(bitmap.Width, bitmap.Height);