SplashscreenBuilder.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using MediaBrowser.Controller.Drawing;
  4. using SkiaSharp;
  5. namespace Jellyfin.Drawing.Skia
  6. {
  7. /// <summary>
  8. /// Used to build the splashscreen.
  9. /// </summary>
  10. public class SplashscreenBuilder
  11. {
  12. private const int FinalWidth = 1920;
  13. private const int FinalHeight = 1080;
  14. // generated collage resolution should be higher than the final resolution
  15. private const int WallWidth = FinalWidth * 3;
  16. private const int WallHeight = FinalHeight * 2;
  17. private const int Rows = 6;
  18. private const int Spacing = 20;
  19. private readonly SkiaEncoder _skiaEncoder;
  20. private Random? _random;
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="SplashscreenBuilder"/> class.
  23. /// </summary>
  24. /// <param name="skiaEncoder">The SkiaEncoder.</param>
  25. public SplashscreenBuilder(SkiaEncoder skiaEncoder)
  26. {
  27. _skiaEncoder = skiaEncoder;
  28. }
  29. /// <summary>
  30. /// Generate a splashscreen.
  31. /// </summary>
  32. /// <param name="options">The options to generate the splashscreen.</param>
  33. public void GenerateSplash(SplashscreenOptions options)
  34. {
  35. var wall = GenerateCollage(options.PortraitInputPaths, options.LandscapeInputPaths, options.ApplyFilter);
  36. var transformed = Transform3D(wall);
  37. using var outputStream = new SKFileWStream(options.OutputPath);
  38. using var pixmap = new SKPixmap(new SKImageInfo(FinalWidth, FinalHeight), transformed.GetPixels());
  39. pixmap.Encode(outputStream, StripCollageBuilder.GetEncodedFormat(options.OutputPath), 90);
  40. }
  41. /// <summary>
  42. /// Generates a collage of posters and landscape pictures.
  43. /// </summary>
  44. /// <param name="poster">The poster paths.</param>
  45. /// <param name="backdrop">The landscape paths.</param>
  46. /// <param name="applyFilter">Whether to apply the darkening filter.</param>
  47. /// <returns>The created collage as a bitmap.</returns>
  48. private SKBitmap GenerateCollage(IReadOnlyList<string> poster, IReadOnlyList<string> backdrop, bool applyFilter)
  49. {
  50. _random = new Random();
  51. var posterIndex = 0;
  52. var backdropIndex = 0;
  53. var bitmap = new SKBitmap(WallWidth, WallHeight);
  54. using var canvas = new SKCanvas(bitmap);
  55. canvas.Clear(SKColors.Black);
  56. int posterHeight = WallHeight / 6;
  57. for (int i = 0; i < Rows; i++)
  58. {
  59. int imageCounter = _random.Next(0, 5);
  60. int currentWidthPos = i * 75;
  61. int currentHeight = i * (posterHeight + Spacing);
  62. while (currentWidthPos < WallWidth)
  63. {
  64. SKBitmap? currentImage;
  65. switch (imageCounter)
  66. {
  67. case 0:
  68. case 2:
  69. case 3:
  70. currentImage = SkiaHelper.GetNextValidImage(_skiaEncoder, poster, posterIndex, out int newPosterIndex);
  71. posterIndex = newPosterIndex;
  72. break;
  73. default:
  74. currentImage = SkiaHelper.GetNextValidImage(_skiaEncoder, backdrop, backdropIndex, out int newBackdropIndex);
  75. backdropIndex = newBackdropIndex;
  76. break;
  77. }
  78. if (currentImage == null)
  79. {
  80. throw new ArgumentException("Not enough valid pictures provided to create a splashscreen!");
  81. }
  82. // resize to the same aspect as the original
  83. var imageWidth = Math.Abs(posterHeight * currentImage.Width / currentImage.Height);
  84. using var resizedBitmap = new SKBitmap(imageWidth, posterHeight);
  85. currentImage.ScalePixels(resizedBitmap, SKFilterQuality.High);
  86. // draw on canvas
  87. canvas.DrawBitmap(resizedBitmap, currentWidthPos, currentHeight);
  88. currentWidthPos += imageWidth + Spacing;
  89. currentImage.Dispose();
  90. if (imageCounter >= 4)
  91. {
  92. imageCounter = 0;
  93. }
  94. else
  95. {
  96. imageCounter++;
  97. }
  98. }
  99. }
  100. if (applyFilter)
  101. {
  102. var paintColor = new SKPaint
  103. {
  104. Color = SKColors.Black.WithAlpha(0x50),
  105. Style = SKPaintStyle.Fill
  106. };
  107. canvas.DrawRect(0, 0, WallWidth, WallHeight, paintColor);
  108. }
  109. return bitmap;
  110. }
  111. /// <summary>
  112. /// Transform the collage in 3D space.
  113. /// </summary>
  114. /// <param name="input">The bitmap to transform.</param>
  115. /// <returns>The transformed image.</returns>
  116. private SKBitmap Transform3D(SKBitmap input)
  117. {
  118. var bitmap = new SKBitmap(FinalWidth, FinalHeight);
  119. using var canvas = new SKCanvas(bitmap);
  120. canvas.Clear(SKColors.Black);
  121. var matrix = new SKMatrix
  122. {
  123. ScaleX = 0.324108899f,
  124. ScaleY = 0.563934922f,
  125. SkewX = -0.244337708f,
  126. SkewY = 0.0377609022f,
  127. TransX = 42.0407715f,
  128. TransY = -198.104706f,
  129. Persp0 = -9.08959337E-05f,
  130. Persp1 = 6.85242048E-05f,
  131. Persp2 = 0.988209724f
  132. };
  133. canvas.SetMatrix(matrix);
  134. canvas.DrawBitmap(input, 0, 0);
  135. return bitmap;
  136. }
  137. }
  138. }