StripCollageBuilder.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // determine sizes for each image that will composited into the final image
  67. var iSlice = Convert.ToInt32(width * 0.33);
  68. int iTrans = Convert.ToInt32(height * 0.25);
  69. int iHeight = Convert.ToInt32(height * 1.00);
  70. int imageIndex = 0;
  71. for (int i = 0; i < 3; 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, _fileSystem, 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. }