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