StripCollageBuilder.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. {
  66. pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
  67. }
  68. }
  69. /// <summary>
  70. /// Create a thumb collage.
  71. /// </summary>
  72. /// <param name="paths">The paths of the images to use in the collage.</param>
  73. /// <param name="outputPath">The path at which to place the resulting image.</param>
  74. /// <param name="width">The desired width of the collage.</param>
  75. /// <param name="height">The desired height of the collage.</param>
  76. public void BuildThumbCollage(string[] paths, string outputPath, int width, int height)
  77. {
  78. using (var bitmap = BuildThumbCollageBitmap(paths, width, height))
  79. using (var outputStream = new SKFileWStream(outputPath))
  80. using (var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels()))
  81. {
  82. pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
  83. }
  84. }
  85. private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height)
  86. {
  87. var bitmap = new SKBitmap(width, height);
  88. using (var canvas = new SKCanvas(bitmap))
  89. {
  90. canvas.Clear(SKColors.Black);
  91. // number of images used in the thumbnail
  92. var iCount = 3;
  93. // determine sizes for each image that will composited into the final image
  94. var iSlice = Convert.ToInt32(width / iCount);
  95. int iHeight = Convert.ToInt32(height * 1.00);
  96. int imageIndex = 0;
  97. for (int i = 0; i < iCount; i++)
  98. {
  99. using (var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex))
  100. {
  101. imageIndex = newIndex;
  102. if (currentBitmap == null)
  103. {
  104. continue;
  105. }
  106. // resize to the same aspect as the original
  107. int iWidth = Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height);
  108. using (var resizeBitmap = new SKBitmap(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
  109. {
  110. currentBitmap.ScalePixels(resizeBitmap, SKFilterQuality.High);
  111. // crop image
  112. int ix = Math.Abs((iWidth - iSlice) / 2);
  113. using (var image = SKImage.FromBitmap(resizeBitmap))
  114. using (var subset = image.Subset(SKRectI.Create(ix, 0, iSlice, iHeight)))
  115. {
  116. // draw image onto canvas
  117. canvas.DrawImage(subset ?? image, iSlice * i, 0);
  118. }
  119. }
  120. }
  121. }
  122. }
  123. return bitmap;
  124. }
  125. private SKBitmap? GetNextValidImage(string[] paths, int currentIndex, out int newIndex)
  126. {
  127. var imagesTested = new Dictionary<int, int>();
  128. SKBitmap? bitmap = null;
  129. while (imagesTested.Count < paths.Length)
  130. {
  131. if (currentIndex >= paths.Length)
  132. {
  133. currentIndex = 0;
  134. }
  135. bitmap = _skiaEncoder.Decode(paths[currentIndex], false, null, out _);
  136. imagesTested[currentIndex] = 0;
  137. currentIndex++;
  138. if (bitmap != null)
  139. {
  140. break;
  141. }
  142. }
  143. newIndex = currentIndex;
  144. return bitmap;
  145. }
  146. private SKBitmap BuildSquareCollageBitmap(string[] paths, int width, int height)
  147. {
  148. var bitmap = new SKBitmap(width, height);
  149. var imageIndex = 0;
  150. var cellWidth = width / 2;
  151. var cellHeight = height / 2;
  152. using (var canvas = new SKCanvas(bitmap))
  153. {
  154. for (var x = 0; x < 2; x++)
  155. {
  156. for (var y = 0; y < 2; y++)
  157. {
  158. using (var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex))
  159. {
  160. imageIndex = newIndex;
  161. if (currentBitmap == null)
  162. {
  163. continue;
  164. }
  165. using (var resizedBitmap = new SKBitmap(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
  166. {
  167. // scale image
  168. currentBitmap.ScalePixels(resizedBitmap, SKFilterQuality.High);
  169. // draw this image into the strip at the next position
  170. var xPos = x * cellWidth;
  171. var yPos = y * cellHeight;
  172. canvas.DrawBitmap(resizedBitmap, xPos, yPos);
  173. }
  174. }
  175. }
  176. }
  177. }
  178. return bitmap;
  179. }
  180. }
  181. }