StripCollageBuilder.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using SkiaSharp;
  5. namespace Jellyfin.Drawing.Skia
  6. {
  7. public class StripCollageBuilder
  8. {
  9. private readonly SkiaEncoder _skiaEncoder;
  10. public StripCollageBuilder(SkiaEncoder skiaEncoder)
  11. {
  12. _skiaEncoder = skiaEncoder;
  13. }
  14. public static SKEncodedImageFormat GetEncodedFormat(string outputPath)
  15. {
  16. if (outputPath == null)
  17. {
  18. throw new ArgumentNullException(nameof(outputPath));
  19. }
  20. var ext = Path.GetExtension(outputPath);
  21. if (string.Equals(ext, ".jpg", StringComparison.OrdinalIgnoreCase)
  22. || string.Equals(ext, ".jpeg", StringComparison.OrdinalIgnoreCase))
  23. {
  24. return SKEncodedImageFormat.Jpeg;
  25. }
  26. if (string.Equals(ext, ".webp", StringComparison.OrdinalIgnoreCase))
  27. {
  28. return SKEncodedImageFormat.Webp;
  29. }
  30. if (string.Equals(ext, ".gif", StringComparison.OrdinalIgnoreCase))
  31. {
  32. return SKEncodedImageFormat.Gif;
  33. }
  34. if (string.Equals(ext, ".bmp", StringComparison.OrdinalIgnoreCase))
  35. {
  36. return SKEncodedImageFormat.Bmp;
  37. }
  38. // default to png
  39. return SKEncodedImageFormat.Png;
  40. }
  41. public void BuildSquareCollage(string[] paths, string outputPath, int width, int height)
  42. {
  43. using (var bitmap = BuildSquareCollageBitmap(paths, width, height))
  44. using (var outputStream = new SKFileWStream(outputPath))
  45. using (var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels()))
  46. {
  47. pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
  48. }
  49. }
  50. public void BuildThumbCollage(string[] paths, string outputPath, int width, int height)
  51. {
  52. using (var bitmap = BuildThumbCollageBitmap(paths, width, height))
  53. using (var outputStream = new SKFileWStream(outputPath))
  54. using (var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels()))
  55. {
  56. pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
  57. }
  58. }
  59. private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height)
  60. {
  61. var bitmap = new SKBitmap(width, height);
  62. using (var canvas = new SKCanvas(bitmap))
  63. {
  64. canvas.Clear(SKColors.Black);
  65. // number of images used in the thumbnail
  66. var iCount = 3;
  67. // determine sizes for each image that will composited into the final image
  68. var iSlice = Convert.ToInt32(width / iCount);
  69. int iHeight = Convert.ToInt32(height * 1.00);
  70. int imageIndex = 0;
  71. for (int i = 0; i < iCount; i++)
  72. {
  73. using (var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex))
  74. {
  75. imageIndex = newIndex;
  76. if (currentBitmap == null)
  77. {
  78. continue;
  79. }
  80. // resize to the same aspect as the original
  81. int iWidth = (int)Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height);
  82. using (var resizeBitmap = new SKBitmap(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
  83. {
  84. currentBitmap.ScalePixels(resizeBitmap, SKFilterQuality.High);
  85. // crop image
  86. int ix = (int)Math.Abs((iWidth - iSlice) / 2);
  87. using (var image = SKImage.FromBitmap(resizeBitmap))
  88. using (var subset = image.Subset(SKRectI.Create(ix, 0, iSlice, iHeight)))
  89. {
  90. // draw image onto canvas
  91. canvas.DrawImage(subset ?? image, iSlice * i, 0);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. return bitmap;
  98. }
  99. private SKBitmap GetNextValidImage(string[] paths, int currentIndex, out int newIndex)
  100. {
  101. var imagesTested = new Dictionary<int, int>();
  102. SKBitmap bitmap = null;
  103. while (imagesTested.Count < paths.Length)
  104. {
  105. if (currentIndex >= paths.Length)
  106. {
  107. currentIndex = 0;
  108. }
  109. bitmap = _skiaEncoder.Decode(paths[currentIndex], false, null, out var origin);
  110. imagesTested[currentIndex] = 0;
  111. currentIndex++;
  112. if (bitmap != null)
  113. {
  114. break;
  115. }
  116. }
  117. newIndex = currentIndex;
  118. return bitmap;
  119. }
  120. private SKBitmap BuildSquareCollageBitmap(string[] paths, int width, int height)
  121. {
  122. var bitmap = new SKBitmap(width, height);
  123. var imageIndex = 0;
  124. var cellWidth = width / 2;
  125. var cellHeight = height / 2;
  126. using (var canvas = new SKCanvas(bitmap))
  127. {
  128. for (var x = 0; x < 2; x++)
  129. {
  130. for (var y = 0; y < 2; y++)
  131. {
  132. using (var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex))
  133. {
  134. imageIndex = newIndex;
  135. if (currentBitmap == null)
  136. {
  137. continue;
  138. }
  139. using (var resizedBitmap = new SKBitmap(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
  140. {
  141. // scale image
  142. currentBitmap.ScalePixels(resizedBitmap, SKFilterQuality.High);
  143. // draw this image into the strip at the next position
  144. var xPos = x * cellWidth;
  145. var yPos = y * cellHeight;
  146. canvas.DrawBitmap(resizedBitmap, xPos, yPos);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. return bitmap;
  153. }
  154. }
  155. }