StripCollageBuilder.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using SkiaSharp;
  5. namespace Jellyfin.Drawing.Skia
  6. {
  7. /// <summary>
  8. /// Used to build collages of multiple images arranged in vertical strips.
  9. /// </summary>
  10. public class StripCollageBuilder
  11. {
  12. private readonly SkiaEncoder _skiaEncoder;
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="StripCollageBuilder"/> class.
  15. /// </summary>
  16. /// <param name="skiaEncoder">The encoder to use for building collages.</param>
  17. public StripCollageBuilder(SkiaEncoder skiaEncoder)
  18. {
  19. _skiaEncoder = skiaEncoder;
  20. }
  21. /// <summary>
  22. /// Check which format an image has been encoded with using its filename extension.
  23. /// </summary>
  24. /// <param name="outputPath">The path to the image to get the format for.</param>
  25. /// <returns>The image format.</returns>
  26. public static SKEncodedImageFormat GetEncodedFormat(string outputPath)
  27. {
  28. if (outputPath == null)
  29. {
  30. throw new ArgumentNullException(nameof(outputPath));
  31. }
  32. var ext = Path.GetExtension(outputPath);
  33. if (string.Equals(ext, ".jpg", StringComparison.OrdinalIgnoreCase)
  34. || string.Equals(ext, ".jpeg", StringComparison.OrdinalIgnoreCase))
  35. {
  36. return SKEncodedImageFormat.Jpeg;
  37. }
  38. if (string.Equals(ext, ".webp", StringComparison.OrdinalIgnoreCase))
  39. {
  40. return SKEncodedImageFormat.Webp;
  41. }
  42. if (string.Equals(ext, ".gif", StringComparison.OrdinalIgnoreCase))
  43. {
  44. return SKEncodedImageFormat.Gif;
  45. }
  46. if (string.Equals(ext, ".bmp", StringComparison.OrdinalIgnoreCase))
  47. {
  48. return SKEncodedImageFormat.Bmp;
  49. }
  50. // default to png
  51. return SKEncodedImageFormat.Png;
  52. }
  53. /// <summary>
  54. /// Create a square collage.
  55. /// </summary>
  56. /// <param name="paths">The paths of the images to use in the collage.</param>
  57. /// <param name="outputPath">The path at which to place the resulting collage image.</param>
  58. /// <param name="width">The desired width of the collage.</param>
  59. /// <param name="height">The desired height of the collage.</param>
  60. public void BuildSquareCollage(string[] paths, string outputPath, int width, int height)
  61. {
  62. using var bitmap = BuildSquareCollageBitmap(paths, width, height);
  63. using var outputStream = new SKFileWStream(outputPath);
  64. using var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels());
  65. pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
  66. }
  67. /// <summary>
  68. /// Create a thumb collage.
  69. /// </summary>
  70. /// <param name="paths">The paths of the images to use in the collage.</param>
  71. /// <param name="outputPath">The path at which to place the resulting image.</param>
  72. /// <param name="width">The desired width of the collage.</param>
  73. /// <param name="height">The desired height of the collage.</param>
  74. /// <param name="libraryName">The name of the library to draw on the collage.</param>
  75. public void BuildThumbCollage(string[] paths, string outputPath, int width, int height, string? libraryName)
  76. {
  77. using var bitmap = BuildThumbCollageBitmap(paths, width, height, libraryName);
  78. using var outputStream = new SKFileWStream(outputPath);
  79. using var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels());
  80. pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
  81. }
  82. private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height, string? libraryName)
  83. {
  84. var bitmap = new SKBitmap(width, height);
  85. using var canvas = new SKCanvas(bitmap);
  86. canvas.Clear(SKColors.Black);
  87. using var backdrop = GetNextValidImage(paths, 0, out _);
  88. if (backdrop == null)
  89. {
  90. return bitmap;
  91. }
  92. // resize to the same aspect as the original
  93. var backdropHeight = Math.Abs(width * backdrop.Height / backdrop.Width);
  94. using var residedBackdrop = SkiaEncoder.ResizeImage(backdrop, new SKImageInfo(width, backdropHeight, backdrop.ColorType, backdrop.AlphaType, backdrop.ColorSpace));
  95. // draw the backdrop
  96. canvas.DrawImage(residedBackdrop, 0, 0);
  97. // draw shadow rectangle
  98. var paintColor = new SKPaint
  99. {
  100. Color = SKColors.Black.WithAlpha(0x78),
  101. Style = SKPaintStyle.Fill
  102. };
  103. canvas.DrawRect(0, 0, width, height, paintColor);
  104. // draw library name
  105. var textPaint = new SKPaint
  106. {
  107. Color = SKColors.White,
  108. Style = SKPaintStyle.Fill,
  109. TextSize = 112,
  110. TextAlign = SKTextAlign.Center,
  111. Typeface = SKTypeface.FromFamilyName("sans-serif", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright),
  112. IsAntialias = true
  113. };
  114. // scale down text to 90% of the width if text is larger than 95% of the width
  115. var textWidth = textPaint.MeasureText(libraryName);
  116. if (textWidth > width * 0.95)
  117. {
  118. textPaint.TextSize = 0.9f * width * textPaint.TextSize / textWidth;
  119. }
  120. canvas.DrawText(libraryName, width / 2f, (height / 2f) + (textPaint.FontMetrics.XHeight / 2), textPaint);
  121. return bitmap;
  122. }
  123. private SKBitmap? GetNextValidImage(string[] paths, int currentIndex, out int newIndex)
  124. {
  125. var imagesTested = new Dictionary<int, int>();
  126. SKBitmap? bitmap = null;
  127. while (imagesTested.Count < paths.Length)
  128. {
  129. if (currentIndex >= paths.Length)
  130. {
  131. currentIndex = 0;
  132. }
  133. bitmap = _skiaEncoder.Decode(paths[currentIndex], false, null, out _);
  134. imagesTested[currentIndex] = 0;
  135. currentIndex++;
  136. if (bitmap != null)
  137. {
  138. break;
  139. }
  140. }
  141. newIndex = currentIndex;
  142. return bitmap;
  143. }
  144. private SKBitmap BuildSquareCollageBitmap(string[] paths, int width, int height)
  145. {
  146. var bitmap = new SKBitmap(width, height);
  147. var imageIndex = 0;
  148. var cellWidth = width / 2;
  149. var cellHeight = height / 2;
  150. using var canvas = new SKCanvas(bitmap);
  151. for (var x = 0; x < 2; x++)
  152. {
  153. for (var y = 0; y < 2; y++)
  154. {
  155. using var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex);
  156. imageIndex = newIndex;
  157. if (currentBitmap == null)
  158. {
  159. continue;
  160. }
  161. // Scale image. The FromBitmap creates a copy
  162. var imageInfo = new SKImageInfo(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType, currentBitmap.ColorSpace);
  163. using var resizedBitmap = SKBitmap.FromImage(SkiaEncoder.ResizeImage(currentBitmap, imageInfo));
  164. // draw this image into the strip at the next position
  165. var xPos = x * cellWidth;
  166. var yPos = y * cellHeight;
  167. canvas.DrawBitmap(resizedBitmap, xPos, yPos);
  168. }
  169. }
  170. return bitmap;
  171. }
  172. }
  173. }