StripCollageBuilder.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using SkiaSharp;
  2. using MediaBrowser.Common.Configuration;
  3. using System;
  4. using System.IO;
  5. using MediaBrowser.Model.IO;
  6. namespace Emby.Drawing.Skia
  7. {
  8. public class StripCollageBuilder
  9. {
  10. private readonly IApplicationPaths _appPaths;
  11. private readonly IFileSystem _fileSystem;
  12. public StripCollageBuilder(IApplicationPaths appPaths, IFileSystem fileSystem)
  13. {
  14. _appPaths = appPaths;
  15. _fileSystem = fileSystem;
  16. }
  17. public static SKEncodedImageFormat GetEncodedFormat(string outputPath)
  18. {
  19. var ext = Path.GetExtension(outputPath).ToLower();
  20. if (ext == ".jpg" || ext == ".jpeg")
  21. return SKEncodedImageFormat.Jpeg;
  22. if (ext == ".webp")
  23. return SKEncodedImageFormat.Webp;
  24. if (ext == ".gif")
  25. return SKEncodedImageFormat.Gif;
  26. if (ext == ".bmp")
  27. return SKEncodedImageFormat.Bmp;
  28. // default to png
  29. return SKEncodedImageFormat.Png;
  30. }
  31. public void BuildPosterCollage(string[] paths, string outputPath, int width, int height)
  32. {
  33. // @todo
  34. }
  35. public void BuildSquareCollage(string[] paths, string outputPath, int width, int height)
  36. {
  37. using (var bitmap = BuildSquareCollageBitmap(paths, width, height))
  38. {
  39. using (var outputStream = new SKFileWStream(outputPath))
  40. {
  41. bitmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
  42. }
  43. }
  44. }
  45. public void BuildThumbCollage(string[] paths, string outputPath, int width, int height)
  46. {
  47. using (var bitmap = BuildThumbCollageBitmap(paths, width, height))
  48. {
  49. using (var outputStream = new SKFileWStream(outputPath))
  50. {
  51. bitmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
  52. }
  53. }
  54. }
  55. private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height)
  56. {
  57. var bitmap = new SKBitmap(width, height);
  58. using (var canvas = new SKCanvas(bitmap))
  59. {
  60. canvas.Clear(SKColors.Black);
  61. var iSlice = Convert.ToInt32(width * 0.24125);
  62. int iTrans = Convert.ToInt32(height * .25);
  63. int iHeight = Convert.ToInt32(height * .70);
  64. var horizontalImagePadding = Convert.ToInt32(width * 0.0125);
  65. var verticalSpacing = Convert.ToInt32(height * 0.01111111111111111111111111111111);
  66. int imageIndex = 0;
  67. for (int i = 0; i < 4; i++)
  68. {
  69. using (var currentBitmap = SKBitmap.Decode(paths[imageIndex]))
  70. {
  71. int iWidth = (int)Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height);
  72. using (var resizeBitmap = new SKBitmap(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
  73. {
  74. currentBitmap.Resize(resizeBitmap, SKBitmapResizeMethod.Lanczos3);
  75. int ix = (int)Math.Abs((iWidth - iSlice) / 2);
  76. using (var image = SKImage.FromBitmap(resizeBitmap))
  77. {
  78. using (var subset = image.Subset(SKRectI.Create(ix, 0, iSlice, iHeight)))
  79. {
  80. canvas.DrawImage(subset, (horizontalImagePadding * (i + 1)) + (iSlice * i), 0);
  81. using (var croppedBitmap = SKBitmap.FromImage(subset))
  82. {
  83. using (var flipped = new SKBitmap(croppedBitmap.Width, croppedBitmap.Height / 2, croppedBitmap.ColorType, croppedBitmap.AlphaType))
  84. {
  85. croppedBitmap.Resize(flipped, SKBitmapResizeMethod.Lanczos3);
  86. using (var gradient = new SKPaint())
  87. {
  88. var matrix = SKMatrix.MakeScale(1, -1);
  89. matrix.SetScaleTranslate(1, -1, 0, flipped.Height);
  90. gradient.Shader = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(0, flipped.Height), new[] { new SKColor(0, 0, 0, 0), SKColors.Black }, null, SKShaderTileMode.Clamp, matrix);
  91. canvas.DrawBitmap(flipped, (horizontalImagePadding * (i + 1)) + (iSlice * i), iHeight + verticalSpacing, gradient);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. imageIndex++;
  100. if (imageIndex >= paths.Length)
  101. imageIndex = 0;
  102. }
  103. }
  104. return bitmap;
  105. }
  106. private SKBitmap BuildSquareCollageBitmap(string[] paths, int width, int height)
  107. {
  108. var bitmap = new SKBitmap(width, height);
  109. var imageIndex = 0;
  110. var cellWidth = width / 2;
  111. var cellHeight = height / 2;
  112. using (var canvas = new SKCanvas(bitmap))
  113. {
  114. for (var x = 0; x < 2; x++)
  115. {
  116. for (var y = 0; y < 2; y++)
  117. {
  118. using (var currentBitmap = SKBitmap.Decode(paths[imageIndex]))
  119. {
  120. using (var resizedBitmap = new SKBitmap(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
  121. {
  122. // scale image
  123. currentBitmap.Resize(resizedBitmap, SKBitmapResizeMethod.Lanczos3);
  124. // draw this image into the strip at the next position
  125. var xPos = x * cellWidth;
  126. var yPos = y * cellHeight;
  127. canvas.DrawBitmap(resizedBitmap, xPos, yPos);
  128. }
  129. }
  130. imageIndex++;
  131. if (imageIndex >= paths.Length)
  132. imageIndex = 0;
  133. }
  134. }
  135. }
  136. return bitmap;
  137. }
  138. }
  139. }