| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using BlurHashSharp.SkiaSharp; |
| | | 7 | | using Jellyfin.Extensions; |
| | | 8 | | using MediaBrowser.Common.Configuration; |
| | | 9 | | using MediaBrowser.Common.Extensions; |
| | | 10 | | using MediaBrowser.Controller.Drawing; |
| | | 11 | | using MediaBrowser.Model.Drawing; |
| | | 12 | | using Microsoft.Extensions.Logging; |
| | | 13 | | using SkiaSharp; |
| | | 14 | | using Svg.Skia; |
| | | 15 | | |
| | | 16 | | namespace Jellyfin.Drawing.Skia; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Image encoder that uses <see cref="SkiaSharp"/> to manipulate images. |
| | | 20 | | /// </summary> |
| | | 21 | | public class SkiaEncoder : IImageEncoder |
| | | 22 | | { |
| | | 23 | | private const string SvgFormat = "svg"; |
| | 1 | 24 | | private static readonly HashSet<string> _transparentImageTypes = new(StringComparer.OrdinalIgnoreCase) { ".png", ".g |
| | | 25 | | private readonly ILogger<SkiaEncoder> _logger; |
| | | 26 | | private readonly IApplicationPaths _appPaths; |
| | | 27 | | private static readonly SKImageFilter _imageFilter; |
| | | 28 | | private static readonly SKTypeface[] _typefaces; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// The default sampling options, equivalent to old high quality filter settings when upscaling. |
| | | 32 | | /// </summary> |
| | | 33 | | public static readonly SKSamplingOptions UpscaleSamplingOptions; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// The sampling options, used for downscaling images, equivalent to old high quality filter settings when not upsca |
| | | 37 | | /// </summary> |
| | | 38 | | public static readonly SKSamplingOptions DefaultSamplingOptions; |
| | | 39 | | |
| | | 40 | | #pragma warning disable CA1810 |
| | | 41 | | static SkiaEncoder() |
| | | 42 | | #pragma warning restore CA1810 |
| | | 43 | | { |
| | 1 | 44 | | var kernel = new[] |
| | 1 | 45 | | { |
| | 1 | 46 | | 0, -.1f, 0, |
| | 1 | 47 | | -.1f, 1.4f, -.1f, |
| | 1 | 48 | | 0, -.1f, 0, |
| | 1 | 49 | | }; |
| | | 50 | | |
| | 1 | 51 | | var kernelSize = new SKSizeI(3, 3); |
| | 1 | 52 | | var kernelOffset = new SKPointI(1, 1); |
| | 1 | 53 | | _imageFilter = SKImageFilter.CreateMatrixConvolution( |
| | 1 | 54 | | kernelSize, |
| | 1 | 55 | | kernel, |
| | 1 | 56 | | 1f, |
| | 1 | 57 | | 0f, |
| | 1 | 58 | | kernelOffset, |
| | 1 | 59 | | SKShaderTileMode.Clamp, |
| | 1 | 60 | | true); |
| | | 61 | | |
| | | 62 | | // Initialize the list of typefaces |
| | | 63 | | // We have to statically build a list of typefaces because MatchCharacter only accepts a single character or cod |
| | | 64 | | // But in reality a human-readable character (grapheme cluster) could be multiple code points. For example, 🚵🏻 |
| | 1 | 65 | | _typefaces = |
| | 1 | 66 | | [ |
| | 1 | 67 | | SKFontManager.Default.MatchCharacter(null, SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant |
| | 1 | 68 | | SKFontManager.Default.MatchCharacter(null, SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant |
| | 1 | 69 | | SKFontManager.Default.MatchCharacter(null, SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant |
| | 1 | 70 | | SKFontManager.Default.MatchCharacter(null, SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant |
| | 1 | 71 | | SKFontManager.Default.MatchCharacter(null, SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant |
| | 1 | 72 | | SKFontManager.Default.MatchCharacter(null, SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant |
| | 1 | 73 | | SKFontManager.Default.MatchCharacter(null, SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant |
| | 1 | 74 | | SKTypeface.FromFamilyName("sans-serif", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Up |
| | 1 | 75 | | ]; |
| | | 76 | | |
| | | 77 | | // use cubic for upscaling |
| | 1 | 78 | | UpscaleSamplingOptions = new SKSamplingOptions(SKCubicResampler.Mitchell); |
| | | 79 | | // use bilinear for everything else |
| | 1 | 80 | | DefaultSamplingOptions = new SKSamplingOptions(SKFilterMode.Linear, SKMipmapMode.Linear); |
| | 1 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Initializes a new instance of the <see cref="SkiaEncoder"/> class. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <param name="logger">The application logger.</param> |
| | | 87 | | /// <param name="appPaths">The application paths.</param> |
| | | 88 | | public SkiaEncoder(ILogger<SkiaEncoder> logger, IApplicationPaths appPaths) |
| | | 89 | | { |
| | 21 | 90 | | _logger = logger; |
| | 21 | 91 | | _appPaths = appPaths; |
| | 21 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <inheritdoc/> |
| | 0 | 95 | | public string Name => "Skia"; |
| | | 96 | | |
| | | 97 | | /// <inheritdoc/> |
| | 0 | 98 | | public bool SupportsImageCollageCreation => true; |
| | | 99 | | |
| | | 100 | | /// <inheritdoc/> |
| | 0 | 101 | | public bool SupportsImageEncoding => true; |
| | | 102 | | |
| | | 103 | | /// <inheritdoc/> |
| | | 104 | | public IReadOnlyCollection<string> SupportedInputFormats => |
| | 0 | 105 | | new HashSet<string>(StringComparer.OrdinalIgnoreCase) |
| | 0 | 106 | | { |
| | 0 | 107 | | "jpeg", |
| | 0 | 108 | | "jpg", |
| | 0 | 109 | | "png", |
| | 0 | 110 | | "dng", |
| | 0 | 111 | | "webp", |
| | 0 | 112 | | "gif", |
| | 0 | 113 | | "bmp", |
| | 0 | 114 | | "ico", |
| | 0 | 115 | | "astc", |
| | 0 | 116 | | "ktx", |
| | 0 | 117 | | "pkm", |
| | 0 | 118 | | "wbmp", |
| | 0 | 119 | | // TODO: check if these are supported on multiple platforms |
| | 0 | 120 | | // https://github.com/google/skia/blob/master/infra/bots/recipes/test.py#L454 |
| | 0 | 121 | | // working on windows at least |
| | 0 | 122 | | "cr2", |
| | 0 | 123 | | "nef", |
| | 0 | 124 | | "arw", |
| | 0 | 125 | | SvgFormat |
| | 0 | 126 | | }; |
| | | 127 | | |
| | | 128 | | /// <inheritdoc/> |
| | | 129 | | public IReadOnlyCollection<ImageFormat> SupportedOutputFormats |
| | 0 | 130 | | => new HashSet<ImageFormat> { ImageFormat.Webp, ImageFormat.Jpg, ImageFormat.Png, ImageFormat.Svg }; |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Gets the default typeface to use. |
| | | 134 | | /// </summary> |
| | 0 | 135 | | public static SKTypeface DefaultTypeFace => _typefaces.Last(); |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Check if the native lib is available. |
| | | 139 | | /// </summary> |
| | | 140 | | /// <returns>True if the native lib is available, otherwise false.</returns> |
| | | 141 | | public static bool IsNativeLibAvailable() |
| | | 142 | | { |
| | | 143 | | try |
| | | 144 | | { |
| | | 145 | | // test an operation that requires the native library |
| | 21 | 146 | | SKPMColor.PreMultiply(SKColors.Black); |
| | 21 | 147 | | return true; |
| | | 148 | | } |
| | 0 | 149 | | catch (Exception) |
| | | 150 | | { |
| | 0 | 151 | | return false; |
| | | 152 | | } |
| | 21 | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Convert a <see cref="ImageFormat"/> to a <see cref="SKEncodedImageFormat"/>. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <param name="selectedFormat">The format to convert.</param> |
| | | 159 | | /// <returns>The converted format.</returns> |
| | | 160 | | public static SKEncodedImageFormat GetImageFormat(ImageFormat selectedFormat) |
| | | 161 | | { |
| | 0 | 162 | | return selectedFormat switch |
| | 0 | 163 | | { |
| | 0 | 164 | | ImageFormat.Bmp => SKEncodedImageFormat.Bmp, |
| | 0 | 165 | | ImageFormat.Jpg => SKEncodedImageFormat.Jpeg, |
| | 0 | 166 | | ImageFormat.Gif => SKEncodedImageFormat.Gif, |
| | 0 | 167 | | ImageFormat.Webp => SKEncodedImageFormat.Webp, |
| | 0 | 168 | | _ => SKEncodedImageFormat.Png |
| | 0 | 169 | | }; |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <inheritdoc /> |
| | | 173 | | /// <exception cref="FileNotFoundException">The path is not valid.</exception> |
| | | 174 | | public ImageDimensions GetImageSize(string path) |
| | | 175 | | { |
| | 0 | 176 | | if (!File.Exists(path)) |
| | | 177 | | { |
| | 0 | 178 | | throw new FileNotFoundException("File not found", path); |
| | | 179 | | } |
| | | 180 | | |
| | 0 | 181 | | var extension = Path.GetExtension(path.AsSpan()); |
| | 0 | 182 | | if (extension.Equals(".svg", StringComparison.OrdinalIgnoreCase)) |
| | | 183 | | { |
| | 0 | 184 | | using var svg = new SKSvg(); |
| | | 185 | | try |
| | | 186 | | { |
| | 0 | 187 | | using var picture = svg.Load(path); |
| | 0 | 188 | | if (picture is null) |
| | | 189 | | { |
| | 0 | 190 | | _logger.LogError("Unable to determine image dimensions for {FilePath}", path); |
| | 0 | 191 | | return default; |
| | | 192 | | } |
| | | 193 | | |
| | 0 | 194 | | return new ImageDimensions(Convert.ToInt32(picture.CullRect.Width), Convert.ToInt32(picture.CullRect.Hei |
| | | 195 | | } |
| | 0 | 196 | | catch (FormatException skiaColorException) |
| | | 197 | | { |
| | | 198 | | // This exception is known to be thrown on vector images that define custom styles |
| | | 199 | | // Skia SVG is not able to handle that and as the repository is quite stale and has not received updates |
| | 0 | 200 | | _logger.LogDebug(skiaColorException, "There was a issue loading the requested svg file"); |
| | 0 | 201 | | return default; |
| | | 202 | | } |
| | | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | var safePath = NormalizePath(path); |
| | 0 | 206 | | if (new FileInfo(safePath).Length == 0) |
| | | 207 | | { |
| | 0 | 208 | | _logger.LogDebug("Skip zero‑byte image {FilePath}", path); |
| | 0 | 209 | | return default; |
| | | 210 | | } |
| | | 211 | | |
| | 0 | 212 | | SKCodec? codec = null; |
| | 0 | 213 | | bool isSafePathTemp = !string.Equals(Path.GetFullPath(safePath), Path.GetFullPath(path), StringComparison.Ordina |
| | | 214 | | try |
| | | 215 | | { |
| | 0 | 216 | | codec = SKCodec.Create(safePath, out var result); |
| | 0 | 217 | | switch (result) |
| | | 218 | | { |
| | | 219 | | case SKCodecResult.Success: |
| | | 220 | | // Skia/SkiaSharp edge‑case: when the image header is parsed but the actual pixel |
| | | 221 | | // decode fails (truncated JPEG/PNG, exotic ICC/EXIF, CMYK without color‑transform, etc.) |
| | | 222 | | // `SKCodec.Create` returns a *non‑null* codec together with |
| | | 223 | | // SKCodecResult.InternalError. The header still contains valid dimensions, |
| | | 224 | | // which is all we need here – so we fall back to them instead of aborting. |
| | | 225 | | // See e.g. Skia bugs #4139, #6092. |
| | 0 | 226 | | case SKCodecResult.InternalError when codec is not null: |
| | 0 | 227 | | var info = codec.Info; |
| | 0 | 228 | | return new ImageDimensions(info.Width, info.Height); |
| | | 229 | | |
| | | 230 | | case SKCodecResult.Unimplemented: |
| | 0 | 231 | | _logger.LogDebug("Image format not supported: {FilePath}", path); |
| | 0 | 232 | | return default; |
| | | 233 | | |
| | | 234 | | default: |
| | | 235 | | { |
| | 0 | 236 | | var boundsInfo = SKBitmap.DecodeBounds(safePath); |
| | 0 | 237 | | if (boundsInfo.Width > 0 && boundsInfo.Height > 0) |
| | | 238 | | { |
| | 0 | 239 | | return new ImageDimensions(boundsInfo.Width, boundsInfo.Height); |
| | | 240 | | } |
| | | 241 | | |
| | 0 | 242 | | _logger.LogWarning( |
| | 0 | 243 | | "Unable to determine image dimensions for {FilePath}: {SkCodecResult}", |
| | 0 | 244 | | path, |
| | 0 | 245 | | result); |
| | | 246 | | |
| | 0 | 247 | | return default; |
| | | 248 | | } |
| | | 249 | | } |
| | | 250 | | } |
| | | 251 | | finally |
| | | 252 | | { |
| | | 253 | | try |
| | | 254 | | { |
| | 0 | 255 | | codec?.Dispose(); |
| | 0 | 256 | | } |
| | 0 | 257 | | catch (Exception ex) |
| | | 258 | | { |
| | 0 | 259 | | _logger.LogDebug(ex, "Error by closing codec for {FilePath}", safePath); |
| | 0 | 260 | | } |
| | | 261 | | |
| | 0 | 262 | | if (isSafePathTemp) |
| | | 263 | | { |
| | | 264 | | try |
| | | 265 | | { |
| | 0 | 266 | | if (File.Exists(safePath)) |
| | | 267 | | { |
| | 0 | 268 | | File.Delete(safePath); |
| | | 269 | | } |
| | 0 | 270 | | } |
| | 0 | 271 | | catch (Exception ex) |
| | | 272 | | { |
| | 0 | 273 | | _logger.LogDebug(ex, "Unable to remove temporary file '{TempPath}'", safePath); |
| | 0 | 274 | | } |
| | | 275 | | } |
| | 0 | 276 | | } |
| | 0 | 277 | | } |
| | | 278 | | |
| | | 279 | | /// <inheritdoc /> |
| | | 280 | | /// <exception cref="ArgumentNullException">The path is null.</exception> |
| | | 281 | | /// <exception cref="FileNotFoundException">The path is not valid.</exception> |
| | | 282 | | public string GetImageBlurHash(int xComp, int yComp, string path) |
| | | 283 | | { |
| | 0 | 284 | | ArgumentException.ThrowIfNullOrEmpty(path); |
| | | 285 | | |
| | 0 | 286 | | var extension = Path.GetExtension(path.AsSpan()).TrimStart('.'); |
| | 0 | 287 | | if (!SupportedInputFormats.Contains(extension, StringComparison.OrdinalIgnoreCase) |
| | 0 | 288 | | || extension.Equals(SvgFormat, StringComparison.OrdinalIgnoreCase)) |
| | | 289 | | { |
| | 0 | 290 | | _logger.LogDebug("Unable to compute blur hash due to unsupported format: {ImagePath}", path); |
| | 0 | 291 | | return string.Empty; |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | // Use FileStream with FileShare.Read instead of having Skia open the file to allow concurrent read access |
| | 0 | 295 | | using var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); |
| | | 296 | | // Any larger than 128x128 is too slow and there's no visually discernible difference |
| | 0 | 297 | | return BlurHashEncoder.Encode(xComp, yComp, fileStream, 128, 128); |
| | 0 | 298 | | } |
| | | 299 | | |
| | | 300 | | private bool RequiresSpecialCharacterHack(string path) |
| | | 301 | | { |
| | 0 | 302 | | for (int i = 0; i < path.Length; i++) |
| | | 303 | | { |
| | 0 | 304 | | if (char.GetUnicodeCategory(path[i]) == UnicodeCategory.OtherLetter) |
| | | 305 | | { |
| | 0 | 306 | | return true; |
| | | 307 | | } |
| | | 308 | | } |
| | | 309 | | |
| | 0 | 310 | | return path.HasDiacritics(); |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | private string NormalizePath(string path) |
| | | 314 | | { |
| | 0 | 315 | | if (!RequiresSpecialCharacterHack(path)) |
| | | 316 | | { |
| | 0 | 317 | | return path; |
| | | 318 | | } |
| | | 319 | | |
| | 0 | 320 | | var tempPath = Path.Join(_appPaths.TempDirectory, string.Concat("skia_", Guid.NewGuid().ToString(), Path.GetExte |
| | 0 | 321 | | var directory = Path.GetDirectoryName(tempPath) ?? throw new ResourceNotFoundException($"Provided path ({tempPat |
| | 0 | 322 | | Directory.CreateDirectory(directory); |
| | 0 | 323 | | File.Copy(path, tempPath, true); |
| | | 324 | | |
| | 0 | 325 | | return tempPath; |
| | | 326 | | } |
| | | 327 | | |
| | | 328 | | private static SKEncodedOrigin GetSKEncodedOrigin(ImageOrientation? orientation) |
| | | 329 | | { |
| | 0 | 330 | | if (!orientation.HasValue) |
| | | 331 | | { |
| | 0 | 332 | | return SKEncodedOrigin.Default; |
| | | 333 | | } |
| | | 334 | | |
| | 0 | 335 | | return (SKEncodedOrigin)orientation.Value; |
| | | 336 | | } |
| | | 337 | | |
| | | 338 | | /// <summary> |
| | | 339 | | /// Decode an image. |
| | | 340 | | /// </summary> |
| | | 341 | | /// <param name="path">The filepath of the image to decode.</param> |
| | | 342 | | /// <param name="forceCleanBitmap">Whether to force clean the bitmap.</param> |
| | | 343 | | /// <param name="orientation">The orientation of the image.</param> |
| | | 344 | | /// <param name="origin">The detected origin of the image.</param> |
| | | 345 | | /// <returns>The resulting bitmap of the image.</returns> |
| | | 346 | | internal SKBitmap? Decode(string path, bool forceCleanBitmap, ImageOrientation? orientation, out SKEncodedOrigin ori |
| | | 347 | | { |
| | 0 | 348 | | if (!File.Exists(path)) |
| | | 349 | | { |
| | 0 | 350 | | throw new FileNotFoundException("File not found", path); |
| | | 351 | | } |
| | | 352 | | |
| | 0 | 353 | | var requiresTransparencyHack = _transparentImageTypes.Contains(Path.GetExtension(path)); |
| | | 354 | | |
| | 0 | 355 | | if (requiresTransparencyHack || forceCleanBitmap) |
| | | 356 | | { |
| | 0 | 357 | | using SKCodec codec = SKCodec.Create(NormalizePath(path), out SKCodecResult res); |
| | 0 | 358 | | if (res != SKCodecResult.Success) |
| | | 359 | | { |
| | 0 | 360 | | origin = GetSKEncodedOrigin(orientation); |
| | 0 | 361 | | return null; |
| | | 362 | | } |
| | | 363 | | |
| | 0 | 364 | | if (codec.FrameCount != 0) |
| | | 365 | | { |
| | 0 | 366 | | throw new ArgumentException("Cannot decode images with multiple frames"); |
| | | 367 | | } |
| | | 368 | | |
| | | 369 | | // create the bitmap |
| | 0 | 370 | | SKBitmap? bitmap = null; |
| | | 371 | | try |
| | | 372 | | { |
| | 0 | 373 | | bitmap = new SKBitmap(codec.Info.Width, codec.Info.Height, !requiresTransparencyHack); |
| | | 374 | | |
| | | 375 | | // decode |
| | 0 | 376 | | _ = codec.GetPixels(bitmap.Info, bitmap.GetPixels()); |
| | | 377 | | |
| | 0 | 378 | | origin = codec.EncodedOrigin; |
| | | 379 | | |
| | 0 | 380 | | return bitmap!; |
| | | 381 | | } |
| | 0 | 382 | | catch (Exception e) |
| | | 383 | | { |
| | 0 | 384 | | _logger.LogError(e, "Detected intermediary error decoding image {0}", path); |
| | 0 | 385 | | bitmap?.Dispose(); |
| | 0 | 386 | | throw; |
| | | 387 | | } |
| | | 388 | | } |
| | | 389 | | |
| | 0 | 390 | | var resultBitmap = SKBitmap.Decode(NormalizePath(path)); |
| | | 391 | | |
| | 0 | 392 | | if (resultBitmap is null) |
| | | 393 | | { |
| | 0 | 394 | | return Decode(path, true, orientation, out origin); |
| | | 395 | | } |
| | | 396 | | |
| | | 397 | | try |
| | | 398 | | { |
| | | 399 | | // If we have to resize these they often end up distorted |
| | 0 | 400 | | if (resultBitmap.ColorType == SKColorType.Gray8) |
| | | 401 | | { |
| | 0 | 402 | | using (resultBitmap) |
| | | 403 | | { |
| | 0 | 404 | | return Decode(path, true, orientation, out origin); |
| | | 405 | | } |
| | | 406 | | } |
| | | 407 | | |
| | 0 | 408 | | origin = SKEncodedOrigin.TopLeft; |
| | 0 | 409 | | return resultBitmap; |
| | | 410 | | } |
| | 0 | 411 | | catch (Exception e) |
| | | 412 | | { |
| | 0 | 413 | | _logger.LogError(e, "Detected intermediary error decoding image {0}", path); |
| | 0 | 414 | | resultBitmap?.Dispose(); |
| | 0 | 415 | | throw; |
| | | 416 | | } |
| | 0 | 417 | | } |
| | | 418 | | |
| | | 419 | | private SKBitmap? GetBitmap(string path, bool autoOrient, ImageOrientation? orientation) |
| | | 420 | | { |
| | 0 | 421 | | if (autoOrient) |
| | | 422 | | { |
| | 0 | 423 | | var bitmap = Decode(path, true, orientation, out var origin); |
| | | 424 | | |
| | 0 | 425 | | if (bitmap is not null && origin != SKEncodedOrigin.TopLeft) |
| | | 426 | | { |
| | 0 | 427 | | using (bitmap) |
| | | 428 | | { |
| | 0 | 429 | | return OrientImage(bitmap, origin); |
| | | 430 | | } |
| | | 431 | | } |
| | | 432 | | |
| | 0 | 433 | | return bitmap; |
| | | 434 | | } |
| | | 435 | | |
| | 0 | 436 | | return Decode(path, false, orientation, out _); |
| | 0 | 437 | | } |
| | | 438 | | |
| | | 439 | | private SKBitmap? GetBitmapFromSvg(string path) |
| | | 440 | | { |
| | 0 | 441 | | if (!File.Exists(path)) |
| | | 442 | | { |
| | 0 | 443 | | throw new FileNotFoundException("File not found", path); |
| | | 444 | | } |
| | | 445 | | |
| | 0 | 446 | | using var svg = SKSvg.CreateFromFile(path); |
| | 0 | 447 | | if (svg.Drawable is null) |
| | | 448 | | { |
| | 0 | 449 | | return null; |
| | | 450 | | } |
| | | 451 | | |
| | 0 | 452 | | var width = (int)Math.Round(svg.Drawable.Bounds.Width); |
| | 0 | 453 | | var height = (int)Math.Round(svg.Drawable.Bounds.Height); |
| | | 454 | | |
| | 0 | 455 | | SKBitmap? bitmap = null; |
| | | 456 | | try |
| | | 457 | | { |
| | 0 | 458 | | bitmap = new SKBitmap(width, height); |
| | 0 | 459 | | using var canvas = new SKCanvas(bitmap); |
| | 0 | 460 | | canvas.DrawPicture(svg.Picture); |
| | 0 | 461 | | canvas.Flush(); |
| | 0 | 462 | | canvas.Save(); |
| | | 463 | | |
| | 0 | 464 | | return bitmap!; |
| | | 465 | | } |
| | 0 | 466 | | catch (Exception e) |
| | | 467 | | { |
| | 0 | 468 | | _logger.LogError(e, "Detected intermediary error extracting image {0}", path); |
| | 0 | 469 | | bitmap?.Dispose(); |
| | 0 | 470 | | throw; |
| | | 471 | | } |
| | 0 | 472 | | } |
| | | 473 | | |
| | | 474 | | private SKBitmap OrientImage(SKBitmap bitmap, SKEncodedOrigin origin) |
| | | 475 | | { |
| | 0 | 476 | | var needsFlip = origin is SKEncodedOrigin.LeftBottom or SKEncodedOrigin.LeftTop or SKEncodedOrigin.RightBottom o |
| | 0 | 477 | | SKBitmap? rotated = null; |
| | | 478 | | try |
| | | 479 | | { |
| | 0 | 480 | | rotated = needsFlip |
| | 0 | 481 | | ? new SKBitmap(bitmap.Height, bitmap.Width) |
| | 0 | 482 | | : new SKBitmap(bitmap.Width, bitmap.Height); |
| | 0 | 483 | | using var surface = new SKCanvas(rotated); |
| | 0 | 484 | | var midX = (float)rotated.Width / 2; |
| | 0 | 485 | | var midY = (float)rotated.Height / 2; |
| | | 486 | | |
| | | 487 | | switch (origin) |
| | | 488 | | { |
| | | 489 | | case SKEncodedOrigin.TopRight: |
| | 0 | 490 | | surface.Scale(-1, 1, midX, midY); |
| | 0 | 491 | | break; |
| | | 492 | | case SKEncodedOrigin.BottomRight: |
| | 0 | 493 | | surface.RotateDegrees(180, midX, midY); |
| | 0 | 494 | | break; |
| | | 495 | | case SKEncodedOrigin.BottomLeft: |
| | 0 | 496 | | surface.Scale(1, -1, midX, midY); |
| | 0 | 497 | | break; |
| | | 498 | | case SKEncodedOrigin.LeftTop: |
| | 0 | 499 | | surface.Translate(0, -rotated.Height); |
| | 0 | 500 | | surface.Scale(1, -1, midX, midY); |
| | 0 | 501 | | surface.RotateDegrees(-90); |
| | 0 | 502 | | break; |
| | | 503 | | case SKEncodedOrigin.RightTop: |
| | 0 | 504 | | surface.Translate(rotated.Width, 0); |
| | 0 | 505 | | surface.RotateDegrees(90); |
| | 0 | 506 | | break; |
| | | 507 | | case SKEncodedOrigin.RightBottom: |
| | 0 | 508 | | surface.Translate(rotated.Width, 0); |
| | 0 | 509 | | surface.Scale(1, -1, midX, midY); |
| | 0 | 510 | | surface.RotateDegrees(90); |
| | 0 | 511 | | break; |
| | | 512 | | case SKEncodedOrigin.LeftBottom: |
| | 0 | 513 | | surface.Translate(0, rotated.Height); |
| | 0 | 514 | | surface.RotateDegrees(-90); |
| | | 515 | | break; |
| | | 516 | | } |
| | | 517 | | |
| | 0 | 518 | | surface.DrawBitmap(bitmap, 0, 0, DefaultSamplingOptions); |
| | 0 | 519 | | return rotated; |
| | | 520 | | } |
| | 0 | 521 | | catch (Exception e) |
| | | 522 | | { |
| | 0 | 523 | | _logger.LogError(e, "Detected intermediary error rotating image"); |
| | 0 | 524 | | rotated?.Dispose(); |
| | 0 | 525 | | throw; |
| | | 526 | | } |
| | 0 | 527 | | } |
| | | 528 | | |
| | | 529 | | /// <summary> |
| | | 530 | | /// Resizes an image on the CPU, by utilizing a surface and canvas. |
| | | 531 | | /// |
| | | 532 | | /// The convolutional matrix kernel used in this resize function gives a (light) sharpening effect. |
| | | 533 | | /// This technique is similar to effect that can be created using for example the [Convolution matrix filter in GIMP |
| | | 534 | | /// </summary> |
| | | 535 | | /// <param name="source">The source bitmap.</param> |
| | | 536 | | /// <param name="targetInfo">This specifies the target size and other information required to create the surface.</p |
| | | 537 | | /// <param name="isAntialias">This enables anti-aliasing on the SKPaint instance.</param> |
| | | 538 | | /// <param name="isDither">This enables dithering on the SKPaint instance.</param> |
| | | 539 | | /// <returns>The resized image.</returns> |
| | | 540 | | internal static SKImage ResizeImage(SKBitmap source, SKImageInfo targetInfo, bool isAntialias = false, bool isDither |
| | | 541 | | { |
| | 0 | 542 | | using var surface = SKSurface.Create(targetInfo); |
| | 0 | 543 | | using var canvas = surface.Canvas; |
| | 0 | 544 | | using var paint = new SKPaint(); |
| | 0 | 545 | | paint.IsAntialias = isAntialias; |
| | 0 | 546 | | paint.IsDither = isDither; |
| | | 547 | | |
| | | 548 | | // Historically, kHigh implied cubic filtering, but only when upsampling. |
| | | 549 | | // If specified kHigh, and were down-sampling, Skia used to switch back to kMedium (bilinear filtering plus mipm |
| | | 550 | | // With current skia API, passing Mitchell cubic when down-sampling will cause serious quality degradation. |
| | 0 | 551 | | var samplingOptions = source.Width > targetInfo.Width || source.Height > targetInfo.Height |
| | 0 | 552 | | ? DefaultSamplingOptions |
| | 0 | 553 | | : UpscaleSamplingOptions; |
| | | 554 | | |
| | 0 | 555 | | paint.ImageFilter = _imageFilter; |
| | 0 | 556 | | canvas.DrawBitmap( |
| | 0 | 557 | | source, |
| | 0 | 558 | | SKRect.Create(0, 0, source.Width, source.Height), |
| | 0 | 559 | | SKRect.Create(0, 0, targetInfo.Width, targetInfo.Height), |
| | 0 | 560 | | samplingOptions, |
| | 0 | 561 | | paint); |
| | | 562 | | |
| | 0 | 563 | | return surface.Snapshot(); |
| | 0 | 564 | | } |
| | | 565 | | |
| | | 566 | | /// <inheritdoc/> |
| | | 567 | | public string EncodeImage(string inputPath, DateTime dateModified, string outputPath, bool autoOrient, ImageOrientat |
| | | 568 | | { |
| | 0 | 569 | | ArgumentException.ThrowIfNullOrEmpty(inputPath); |
| | 0 | 570 | | ArgumentException.ThrowIfNullOrEmpty(outputPath); |
| | | 571 | | |
| | 0 | 572 | | var inputFormat = Path.GetExtension(inputPath.AsSpan()).TrimStart('.'); |
| | 0 | 573 | | if (!SupportedInputFormats.Contains(inputFormat, StringComparison.OrdinalIgnoreCase)) |
| | | 574 | | { |
| | 0 | 575 | | _logger.LogDebug("Unable to encode image due to unsupported format: {ImagePath}", inputPath); |
| | 0 | 576 | | return inputPath; |
| | | 577 | | } |
| | | 578 | | |
| | 0 | 579 | | if (outputFormat == ImageFormat.Svg |
| | 0 | 580 | | && !inputFormat.Equals(SvgFormat, StringComparison.OrdinalIgnoreCase)) |
| | | 581 | | { |
| | 0 | 582 | | throw new ArgumentException($"Requested svg output from {inputFormat} input"); |
| | | 583 | | } |
| | | 584 | | |
| | 0 | 585 | | var skiaOutputFormat = GetImageFormat(outputFormat); |
| | | 586 | | |
| | 0 | 587 | | var hasBackgroundColor = !string.IsNullOrWhiteSpace(options.BackgroundColor); |
| | 0 | 588 | | var hasForegroundColor = !string.IsNullOrWhiteSpace(options.ForegroundLayer); |
| | 0 | 589 | | var blur = options.Blur ?? 0; |
| | 0 | 590 | | var hasIndicator = options.UnplayedCount.HasValue || !options.PercentPlayed.Equals(0); |
| | | 591 | | |
| | 0 | 592 | | using var bitmap = inputFormat.Equals(SvgFormat, StringComparison.OrdinalIgnoreCase) |
| | 0 | 593 | | ? GetBitmapFromSvg(inputPath) |
| | 0 | 594 | | : GetBitmap(inputPath, autoOrient, orientation); |
| | | 595 | | |
| | 0 | 596 | | if (bitmap is null) |
| | | 597 | | { |
| | 0 | 598 | | throw new InvalidDataException($"Skia unable to read image {inputPath}"); |
| | | 599 | | } |
| | | 600 | | |
| | 0 | 601 | | var originalImageSize = new ImageDimensions(bitmap.Width, bitmap.Height); |
| | | 602 | | |
| | 0 | 603 | | if (options.HasDefaultOptions(inputPath, originalImageSize) && !autoOrient) |
| | | 604 | | { |
| | | 605 | | // Just spit out the original file if all the options are default |
| | 0 | 606 | | return inputPath; |
| | | 607 | | } |
| | | 608 | | |
| | 0 | 609 | | var newImageSize = ImageHelper.GetNewImageSize(options, originalImageSize); |
| | | 610 | | |
| | 0 | 611 | | var width = newImageSize.Width; |
| | 0 | 612 | | var height = newImageSize.Height; |
| | | 613 | | |
| | | 614 | | // scale image (the FromImage creates a copy) |
| | 0 | 615 | | var imageInfo = new SKImageInfo(width, height, bitmap.ColorType, bitmap.AlphaType, bitmap.ColorSpace); |
| | 0 | 616 | | using var resizedImage = ResizeImage(bitmap, imageInfo); |
| | 0 | 617 | | using var resizedBitmap = SKBitmap.FromImage(resizedImage); |
| | | 618 | | |
| | | 619 | | // If all we're doing is resizing then we can stop now |
| | 0 | 620 | | if (!hasBackgroundColor && !hasForegroundColor && blur == 0 && !hasIndicator) |
| | | 621 | | { |
| | 0 | 622 | | var outputDirectory = Path.GetDirectoryName(outputPath) ?? throw new ArgumentException($"Provided path ({out |
| | 0 | 623 | | Directory.CreateDirectory(outputDirectory); |
| | 0 | 624 | | using var outputStream = new SKFileWStream(outputPath); |
| | 0 | 625 | | using var pixmap = new SKPixmap(new SKImageInfo(width, height), resizedBitmap.GetPixels()); |
| | 0 | 626 | | resizedBitmap.Encode(outputStream, skiaOutputFormat, quality); |
| | 0 | 627 | | return outputPath; |
| | | 628 | | } |
| | | 629 | | |
| | | 630 | | // create bitmap to use for canvas drawing used to draw into bitmap |
| | 0 | 631 | | using var saveBitmap = new SKBitmap(width, height); |
| | 0 | 632 | | using var canvas = new SKCanvas(saveBitmap); |
| | | 633 | | // set background color if present |
| | 0 | 634 | | if (hasBackgroundColor) |
| | | 635 | | { |
| | 0 | 636 | | canvas.Clear(SKColor.Parse(options.BackgroundColor)); |
| | | 637 | | } |
| | | 638 | | |
| | 0 | 639 | | using var paint = new SKPaint(); |
| | | 640 | | // Add blur if option is present |
| | 0 | 641 | | using var filter = blur > 0 ? SKImageFilter.CreateBlur(blur, blur) : null; |
| | 0 | 642 | | paint.ImageFilter = filter; |
| | | 643 | | |
| | | 644 | | // create image from resized bitmap to apply blur |
| | 0 | 645 | | canvas.DrawBitmap(resizedBitmap, SKRect.Create(width, height), DefaultSamplingOptions, paint); |
| | | 646 | | |
| | | 647 | | // If foreground layer present then draw |
| | 0 | 648 | | if (hasForegroundColor) |
| | | 649 | | { |
| | 0 | 650 | | if (!double.TryParse(options.ForegroundLayer, out double opacity)) |
| | | 651 | | { |
| | 0 | 652 | | opacity = .4; |
| | | 653 | | } |
| | | 654 | | |
| | 0 | 655 | | canvas.DrawColor(new SKColor(0, 0, 0, (byte)((1 - opacity) * 0xFF)), SKBlendMode.SrcOver); |
| | | 656 | | } |
| | | 657 | | |
| | 0 | 658 | | if (hasIndicator) |
| | | 659 | | { |
| | 0 | 660 | | DrawIndicator(canvas, width, height, options); |
| | | 661 | | } |
| | | 662 | | |
| | 0 | 663 | | var directory = Path.GetDirectoryName(outputPath) ?? throw new ArgumentException($"Provided path ({outputPath}) |
| | 0 | 664 | | Directory.CreateDirectory(directory); |
| | 0 | 665 | | using (var outputStream = new SKFileWStream(outputPath)) |
| | | 666 | | { |
| | 0 | 667 | | using var pixmap = new SKPixmap(new SKImageInfo(width, height), saveBitmap.GetPixels()); |
| | 0 | 668 | | pixmap.Encode(outputStream, skiaOutputFormat, quality); |
| | | 669 | | } |
| | | 670 | | |
| | 0 | 671 | | return outputPath; |
| | 0 | 672 | | } |
| | | 673 | | |
| | | 674 | | /// <inheritdoc/> |
| | | 675 | | public void CreateImageCollage(ImageCollageOptions options, string? libraryName) |
| | | 676 | | { |
| | 0 | 677 | | double ratio = (double)options.Width / options.Height; |
| | | 678 | | |
| | 0 | 679 | | if (ratio >= 1.4) |
| | | 680 | | { |
| | 0 | 681 | | new StripCollageBuilder(this).BuildThumbCollage(options.InputPaths, options.OutputPath, options.Width, optio |
| | | 682 | | } |
| | 0 | 683 | | else if (ratio >= .9) |
| | | 684 | | { |
| | 0 | 685 | | new StripCollageBuilder(this).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, opti |
| | | 686 | | } |
| | | 687 | | else |
| | | 688 | | { |
| | | 689 | | // TODO: Create Poster collage capability |
| | 0 | 690 | | new StripCollageBuilder(this).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, opti |
| | | 691 | | } |
| | 0 | 692 | | } |
| | | 693 | | |
| | | 694 | | /// <inheritdoc /> |
| | | 695 | | public void CreateSplashscreen(IReadOnlyList<string> posters, IReadOnlyList<string> backdrops) |
| | | 696 | | { |
| | | 697 | | // Only generate the splash screen if we have at least one poster and at least one backdrop/thumbnail. |
| | 17 | 698 | | if (posters.Count > 0 && backdrops.Count > 0) |
| | | 699 | | { |
| | 0 | 700 | | var splashBuilder = new SplashscreenBuilder(this, _logger); |
| | 0 | 701 | | var outputPath = Path.Combine(_appPaths.DataPath, "splashscreen.png"); |
| | 0 | 702 | | splashBuilder.GenerateSplash(posters, backdrops, outputPath); |
| | | 703 | | } |
| | 17 | 704 | | } |
| | | 705 | | |
| | | 706 | | /// <inheritdoc /> |
| | | 707 | | public int CreateTrickplayTile(ImageCollageOptions options, int quality, int imgWidth, int? imgHeight) |
| | | 708 | | { |
| | 0 | 709 | | var paths = options.InputPaths; |
| | 0 | 710 | | var tileWidth = options.Width; |
| | 0 | 711 | | var tileHeight = options.Height; |
| | | 712 | | |
| | 0 | 713 | | if (paths.Count < 1) |
| | | 714 | | { |
| | 0 | 715 | | throw new ArgumentException("InputPaths cannot be empty."); |
| | | 716 | | } |
| | 0 | 717 | | else if (paths.Count > tileWidth * tileHeight) |
| | | 718 | | { |
| | 0 | 719 | | throw new ArgumentException($"InputPaths contains more images than would fit on {tileWidth}x{tileHeight} gri |
| | | 720 | | } |
| | | 721 | | |
| | | 722 | | // If no height provided, use height of first image. |
| | 0 | 723 | | if (!imgHeight.HasValue) |
| | | 724 | | { |
| | 0 | 725 | | using var firstImg = Decode(paths[0], false, null, out _); |
| | | 726 | | |
| | 0 | 727 | | if (firstImg is null) |
| | | 728 | | { |
| | 0 | 729 | | throw new InvalidDataException("Could not decode image data."); |
| | | 730 | | } |
| | | 731 | | |
| | 0 | 732 | | if (firstImg.Width != imgWidth) |
| | | 733 | | { |
| | 0 | 734 | | throw new InvalidOperationException("Image width does not match provided width."); |
| | | 735 | | } |
| | | 736 | | |
| | 0 | 737 | | imgHeight = firstImg.Height; |
| | | 738 | | } |
| | | 739 | | |
| | | 740 | | // Make horizontal strips using every provided image. |
| | 0 | 741 | | using var tileGrid = new SKBitmap(imgWidth * tileWidth, imgHeight.Value * tileHeight); |
| | 0 | 742 | | using var canvas = new SKCanvas(tileGrid); |
| | | 743 | | |
| | 0 | 744 | | var imgIndex = 0; |
| | 0 | 745 | | for (var y = 0; y < tileHeight; y++) |
| | | 746 | | { |
| | 0 | 747 | | for (var x = 0; x < tileWidth; x++) |
| | | 748 | | { |
| | 0 | 749 | | if (imgIndex >= paths.Count) |
| | | 750 | | { |
| | | 751 | | break; |
| | | 752 | | } |
| | | 753 | | |
| | 0 | 754 | | using var img = Decode(paths[imgIndex++], false, null, out _); |
| | | 755 | | |
| | 0 | 756 | | if (img is null) |
| | | 757 | | { |
| | 0 | 758 | | throw new InvalidDataException("Could not decode image data."); |
| | | 759 | | } |
| | | 760 | | |
| | 0 | 761 | | if (img.Width != imgWidth) |
| | | 762 | | { |
| | 0 | 763 | | throw new InvalidOperationException("Image width does not match provided width."); |
| | | 764 | | } |
| | | 765 | | |
| | 0 | 766 | | if (img.Height != imgHeight) |
| | | 767 | | { |
| | 0 | 768 | | throw new InvalidOperationException("Image height does not match first image height."); |
| | | 769 | | } |
| | | 770 | | |
| | 0 | 771 | | canvas.DrawBitmap(img, x * imgWidth, y * imgHeight.Value, DefaultSamplingOptions); |
| | | 772 | | } |
| | | 773 | | } |
| | | 774 | | |
| | 0 | 775 | | using var outputStream = new SKFileWStream(options.OutputPath); |
| | 0 | 776 | | tileGrid.Encode(outputStream, SKEncodedImageFormat.Jpeg, quality); |
| | | 777 | | |
| | 0 | 778 | | return imgHeight.Value; |
| | 0 | 779 | | } |
| | | 780 | | |
| | | 781 | | private void DrawIndicator(SKCanvas canvas, int imageWidth, int imageHeight, ImageProcessingOptions options) |
| | | 782 | | { |
| | | 783 | | try |
| | | 784 | | { |
| | 0 | 785 | | var currentImageSize = new ImageDimensions(imageWidth, imageHeight); |
| | | 786 | | |
| | 0 | 787 | | if (options.UnplayedCount.HasValue) |
| | | 788 | | { |
| | 0 | 789 | | UnplayedCountIndicator.DrawUnplayedCountIndicator(canvas, currentImageSize, options.UnplayedCount.Value) |
| | | 790 | | } |
| | | 791 | | |
| | 0 | 792 | | if (options.PercentPlayed > 0) |
| | | 793 | | { |
| | 0 | 794 | | PercentPlayedDrawer.Process(canvas, currentImageSize, options.PercentPlayed); |
| | | 795 | | } |
| | 0 | 796 | | } |
| | 0 | 797 | | catch (Exception ex) |
| | | 798 | | { |
| | 0 | 799 | | _logger.LogError(ex, "Error drawing indicator overlay"); |
| | 0 | 800 | | } |
| | 0 | 801 | | } |
| | | 802 | | |
| | | 803 | | /// <summary> |
| | | 804 | | /// Return the typeface that contains the glyph for the given character. |
| | | 805 | | /// </summary> |
| | | 806 | | /// <param name="c">The text character.</param> |
| | | 807 | | /// <returns>The typeface contains the character.</returns> |
| | | 808 | | public static SKTypeface? GetFontForCharacter(string c) |
| | | 809 | | { |
| | 0 | 810 | | foreach (var typeface in _typefaces) |
| | | 811 | | { |
| | 0 | 812 | | if (typeface.ContainsGlyphs(c)) |
| | | 813 | | { |
| | 0 | 814 | | return typeface; |
| | | 815 | | } |
| | | 816 | | } |
| | | 817 | | |
| | 0 | 818 | | return null; |
| | | 819 | | } |
| | | 820 | | } |