SplashscreenBuilder.cs 5.4 KB

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