StripCollageBuilder.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.23475);
  68. int iTrans = Convert.ToInt32(height * .25);
  69. int iHeight = Convert.ToInt32(height * .70);
  70. var horizontalImagePadding = Convert.ToInt32(width * 0.0125);
  71. var verticalSpacing = Convert.ToInt32(height * 0.01111111111111111111111111111111);
  72. int imageIndex = 0;
  73. for (int i = 0; i < 4; i++)
  74. {
  75. using (var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex))
  76. {
  77. imageIndex = newIndex;
  78. if (currentBitmap == null)
  79. {
  80. continue;
  81. }
  82. // resize to the same aspect as the original
  83. int iWidth = (int)Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height);
  84. using (var resizeBitmap = new SKBitmap(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
  85. {
  86. currentBitmap.ScalePixels(resizeBitmap, SKFilterQuality.High);
  87. // crop image
  88. int ix = (int)Math.Abs((iWidth - iSlice) / 2);
  89. using (var image = SKImage.FromBitmap(resizeBitmap))
  90. using (var subset = image.Subset(SKRectI.Create(ix, 0, iSlice, iHeight)))
  91. {
  92. // draw image onto canvas
  93. canvas.DrawImage(subset ?? image, (horizontalImagePadding * (i + 1)) + (iSlice * i), verticalSpacing);
  94. if (subset == null)
  95. {
  96. continue;
  97. }
  98. // create reflection of image below the drawn image
  99. using (var croppedBitmap = SKBitmap.FromImage(subset))
  100. using (var reflectionBitmap = new SKBitmap(croppedBitmap.Width, croppedBitmap.Height / 2, croppedBitmap.ColorType, croppedBitmap.AlphaType))
  101. {
  102. // resize to half height
  103. currentBitmap.ScalePixels(reflectionBitmap, SKFilterQuality.High);
  104. using (var flippedBitmap = new SKBitmap(reflectionBitmap.Width, reflectionBitmap.Height, reflectionBitmap.ColorType, reflectionBitmap.AlphaType))
  105. using (var flippedCanvas = new SKCanvas(flippedBitmap))
  106. {
  107. // flip image vertically
  108. var matrix = SKMatrix.MakeScale(1, -1);
  109. matrix.SetScaleTranslate(1, -1, 0, flippedBitmap.Height);
  110. flippedCanvas.SetMatrix(matrix);
  111. flippedCanvas.DrawBitmap(reflectionBitmap, 0, 0);
  112. flippedCanvas.ResetMatrix();
  113. // create gradient to make image appear as a reflection
  114. var remainingHeight = height - (iHeight + (2 * verticalSpacing));
  115. flippedCanvas.ClipRect(SKRect.Create(reflectionBitmap.Width, remainingHeight));
  116. using (var gradient = new SKPaint())
  117. {
  118. gradient.IsAntialias = true;
  119. gradient.BlendMode = SKBlendMode.SrcOver;
  120. gradient.Shader = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(0, remainingHeight), new[] { new SKColor(0, 0, 0, 128), new SKColor(0, 0, 0, 208), new SKColor(0, 0, 0, 240), new SKColor(0, 0, 0, 255) }, null, SKShaderTileMode.Clamp);
  121. flippedCanvas.DrawPaint(gradient);
  122. }
  123. // finally draw reflection onto canvas
  124. canvas.DrawBitmap(flippedBitmap, (horizontalImagePadding * (i + 1)) + (iSlice * i), iHeight + (2 * verticalSpacing));
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. return bitmap;
  133. }
  134. private SKBitmap GetNextValidImage(string[] paths, int currentIndex, out int newIndex)
  135. {
  136. var imagesTested = new Dictionary<int, int>();
  137. SKBitmap bitmap = null;
  138. while (imagesTested.Count < paths.Length)
  139. {
  140. if (currentIndex >= paths.Length)
  141. {
  142. currentIndex = 0;
  143. }
  144. bitmap = SkiaEncoder.Decode(paths[currentIndex], false, _fileSystem, null, out var origin);
  145. imagesTested[currentIndex] = 0;
  146. currentIndex++;
  147. if (bitmap != null)
  148. {
  149. break;
  150. }
  151. }
  152. newIndex = currentIndex;
  153. return bitmap;
  154. }
  155. private SKBitmap BuildSquareCollageBitmap(string[] paths, int width, int height)
  156. {
  157. var bitmap = new SKBitmap(width, height);
  158. var imageIndex = 0;
  159. var cellWidth = width / 2;
  160. var cellHeight = height / 2;
  161. using (var canvas = new SKCanvas(bitmap))
  162. {
  163. for (var x = 0; x < 2; x++)
  164. {
  165. for (var y = 0; y < 2; y++)
  166. {
  167. using (var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex))
  168. {
  169. imageIndex = newIndex;
  170. if (currentBitmap == null)
  171. {
  172. continue;
  173. }
  174. using (var resizedBitmap = new SKBitmap(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
  175. {
  176. // scale image
  177. currentBitmap.ScalePixels(resizedBitmap, SKFilterQuality.High);
  178. // draw this image into the strip at the next position
  179. var xPos = x * cellWidth;
  180. var yPos = y * cellHeight;
  181. canvas.DrawBitmap(resizedBitmap, xPos, yPos);
  182. }
  183. }
  184. }
  185. }
  186. }
  187. return bitmap;
  188. }
  189. }
  190. }