StripCollageBuilder.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. public void BuildThumbCollage(string[] paths, string outputPath, int width, int height)
  75. {
  76. using var bitmap = BuildThumbCollageBitmap(paths, width, height);
  77. using var outputStream = new SKFileWStream(outputPath);
  78. using var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels());
  79. pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
  80. }
  81. private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height)
  82. {
  83. var bitmap = new SKBitmap(width, height);
  84. using var canvas = new SKCanvas(bitmap);
  85. canvas.Clear(SKColors.Black);
  86. // number of images used in the thumbnail
  87. var iCount = 3;
  88. // determine sizes for each image that will composited into the final image
  89. var iSlice = Convert.ToInt32(width / iCount);
  90. int iHeight = Convert.ToInt32(height * 1.00);
  91. int imageIndex = 0;
  92. for (int i = 0; i < iCount; i++)
  93. {
  94. using var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex);
  95. imageIndex = newIndex;
  96. if (currentBitmap == null)
  97. {
  98. continue;
  99. }
  100. // resize to the same aspect as the original
  101. int iWidth = Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height);
  102. using var resizedImage = SkiaEncoder.ResizeImage(currentBitmap, new SKImageInfo(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType, currentBitmap.ColorSpace));
  103. // crop image
  104. int ix = Math.Abs((iWidth - iSlice) / 2);
  105. using var subset = resizedImage.Subset(SKRectI.Create(ix, 0, iSlice, iHeight));
  106. // draw image onto canvas
  107. canvas.DrawImage(subset ?? resizedImage, iSlice * i, 0);
  108. }
  109. return bitmap;
  110. }
  111. private SKBitmap? GetNextValidImage(string[] paths, int currentIndex, out int newIndex)
  112. {
  113. var imagesTested = new Dictionary<int, int>();
  114. SKBitmap? bitmap = null;
  115. while (imagesTested.Count < paths.Length)
  116. {
  117. if (currentIndex >= paths.Length)
  118. {
  119. currentIndex = 0;
  120. }
  121. bitmap = _skiaEncoder.Decode(paths[currentIndex], false, null, out _);
  122. imagesTested[currentIndex] = 0;
  123. currentIndex++;
  124. if (bitmap != null)
  125. {
  126. break;
  127. }
  128. }
  129. newIndex = currentIndex;
  130. return bitmap;
  131. }
  132. private SKBitmap BuildSquareCollageBitmap(string[] paths, int width, int height)
  133. {
  134. var bitmap = new SKBitmap(width, height);
  135. var imageIndex = 0;
  136. var cellWidth = width / 2;
  137. var cellHeight = height / 2;
  138. using var canvas = new SKCanvas(bitmap);
  139. for (var x = 0; x < 2; x++)
  140. {
  141. for (var y = 0; y < 2; y++)
  142. {
  143. using var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex);
  144. imageIndex = newIndex;
  145. if (currentBitmap == null)
  146. {
  147. continue;
  148. }
  149. // Scale image. The FromBitmap creates a copy
  150. var imageInfo = new SKImageInfo(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType, currentBitmap.ColorSpace);
  151. using var resizedBitmap = SKBitmap.FromImage(SkiaEncoder.ResizeImage(currentBitmap, imageInfo));
  152. // draw this image into the strip at the next position
  153. var xPos = x * cellWidth;
  154. var yPos = y * cellHeight;
  155. canvas.DrawBitmap(resizedBitmap, xPos, yPos);
  156. }
  157. }
  158. return bitmap;
  159. }
  160. }
  161. }