SplashscreenBuilder.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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="backdrop">The landscape paths.</param>
  32. /// <param name="outputPath">The output path.</param>
  33. public void GenerateSplash(IReadOnlyList<string> posters, IReadOnlyList<string> backdrop, string outputPath)
  34. {
  35. var wall = GenerateCollage(posters, backdrop);
  36. 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="backdrop">The landscape paths.</param>
  46. /// <returns>The created collage as a bitmap.</returns>
  47. private SKBitmap GenerateCollage(IReadOnlyList<string> posters, IReadOnlyList<string> backdrop)
  48. {
  49. var random = new Random();
  50. var posterIndex = 0;
  51. var backdropIndex = 0;
  52. var bitmap = new SKBitmap(WallWidth, WallHeight);
  53. using var canvas = new SKCanvas(bitmap);
  54. canvas.Clear(SKColors.Black);
  55. int posterHeight = WallHeight / 6;
  56. for (int i = 0; i < Rows; i++)
  57. {
  58. int imageCounter = random.Next(0, 5);
  59. int currentWidthPos = i * 75;
  60. int currentHeight = i * (posterHeight + Spacing);
  61. while (currentWidthPos < WallWidth)
  62. {
  63. SKBitmap? currentImage;
  64. switch (imageCounter)
  65. {
  66. case 0:
  67. case 2:
  68. case 3:
  69. currentImage = SkiaHelper.GetNextValidImage(_skiaEncoder, posters, posterIndex, out int newPosterIndex);
  70. posterIndex = newPosterIndex;
  71. break;
  72. default:
  73. currentImage = SkiaHelper.GetNextValidImage(_skiaEncoder, backdrop, backdropIndex, out int newBackdropIndex);
  74. backdropIndex = newBackdropIndex;
  75. break;
  76. }
  77. if (currentImage == null)
  78. {
  79. throw new ArgumentException("Not enough valid pictures provided to create a splashscreen!");
  80. }
  81. // resize to the same aspect as the original
  82. var imageWidth = Math.Abs(posterHeight * currentImage.Width / currentImage.Height);
  83. using var resizedBitmap = new SKBitmap(imageWidth, posterHeight);
  84. currentImage.ScalePixels(resizedBitmap, SKFilterQuality.High);
  85. // draw on canvas
  86. canvas.DrawBitmap(resizedBitmap, currentWidthPos, currentHeight);
  87. currentWidthPos += imageWidth + Spacing;
  88. currentImage.Dispose();
  89. if (imageCounter >= 4)
  90. {
  91. imageCounter = 0;
  92. }
  93. else
  94. {
  95. imageCounter++;
  96. }
  97. }
  98. }
  99. return bitmap;
  100. }
  101. /// <summary>
  102. /// Transform the collage in 3D space.
  103. /// </summary>
  104. /// <param name="input">The bitmap to transform.</param>
  105. /// <returns>The transformed image.</returns>
  106. private SKBitmap Transform3D(SKBitmap input)
  107. {
  108. var bitmap = new SKBitmap(FinalWidth, FinalHeight);
  109. using var canvas = new SKCanvas(bitmap);
  110. canvas.Clear(SKColors.Black);
  111. var matrix = new SKMatrix
  112. {
  113. ScaleX = 0.324108899f,
  114. ScaleY = 0.563934922f,
  115. SkewX = -0.244337708f,
  116. SkewY = 0.0377609022f,
  117. TransX = 42.0407715f,
  118. TransY = -198.104706f,
  119. Persp0 = -9.08959337E-05f,
  120. Persp1 = 6.85242048E-05f,
  121. Persp2 = 0.988209724f
  122. };
  123. canvas.SetMatrix(matrix);
  124. canvas.DrawBitmap(input, 0, 0);
  125. return bitmap;
  126. }
  127. }
  128. }