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