StripCollageBuilder.cs 6.5 KB

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