StripCollageBuilder.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. // determine sizes for each image that will composited into the final image
  62. var iSlice = Convert.ToInt32(width * 0.23475);
  63. int iTrans = Convert.ToInt32(height * .25);
  64. int iHeight = Convert.ToInt32(height * .70);
  65. var horizontalImagePadding = Convert.ToInt32(width * 0.0125);
  66. var verticalSpacing = Convert.ToInt32(height * 0.01111111111111111111111111111111);
  67. int imageIndex = 0;
  68. for (int i = 0; i < 4; i++)
  69. {
  70. using (var currentBitmap = SKBitmap.Decode(paths[imageIndex]))
  71. {
  72. // resize to the same aspect as the original
  73. int iWidth = (int)Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height);
  74. using (var resizeBitmap = new SKBitmap(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
  75. {
  76. currentBitmap.Resize(resizeBitmap, SKBitmapResizeMethod.Lanczos3);
  77. // determine how much to crop
  78. int ix = (int)Math.Abs((iWidth - iSlice) / 2);
  79. using (var image = SKImage.FromBitmap(resizeBitmap))
  80. {
  81. // crop image
  82. using (var subset = image.Subset(SKRectI.Create(ix, 0, iSlice, iHeight)))
  83. {
  84. // draw image onto canvas
  85. canvas.DrawImage(subset, (horizontalImagePadding * (i + 1)) + (iSlice * i), verticalSpacing);
  86. using (var croppedBitmap = SKBitmap.FromImage(subset))
  87. {
  88. // create reflection of image below the drawn image
  89. using (var reflectionBitmap = new SKBitmap(croppedBitmap.Width, croppedBitmap.Height / 2, croppedBitmap.ColorType, croppedBitmap.AlphaType))
  90. {
  91. // resize to half height
  92. croppedBitmap.Resize(reflectionBitmap, SKBitmapResizeMethod.Lanczos3);
  93. using (var flippedBitmap = new SKBitmap(reflectionBitmap.Width, reflectionBitmap.Height, reflectionBitmap.ColorType, reflectionBitmap.AlphaType))
  94. {
  95. using (var flippedCanvas = new SKCanvas(flippedBitmap))
  96. {
  97. // flip image vertically
  98. var matrix = SKMatrix.MakeScale(1, -1);
  99. matrix.SetScaleTranslate(1, -1, 0, flippedBitmap.Height);
  100. flippedCanvas.SetMatrix(matrix);
  101. flippedCanvas.DrawBitmap(reflectionBitmap, 0, 0);
  102. flippedCanvas.ResetMatrix();
  103. // create gradient to make image appear as a reflection
  104. var remainingHeight = height - (iHeight + (2 * verticalSpacing));
  105. flippedCanvas.ClipRect(SKRect.Create(reflectionBitmap.Width, remainingHeight));
  106. using (var gradient = new SKPaint())
  107. {
  108. gradient.IsAntialias = true;
  109. gradient.BlendMode = SKBlendMode.SrcOver;
  110. 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);
  111. flippedCanvas.DrawPaint(gradient);
  112. }
  113. // finally draw reflection onto canvas
  114. canvas.DrawBitmap(flippedBitmap, (horizontalImagePadding * (i + 1)) + (iSlice * i), iHeight + (2 * verticalSpacing));
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. imageIndex++;
  124. if (imageIndex >= paths.Length)
  125. imageIndex = 0;
  126. }
  127. }
  128. return bitmap;
  129. }
  130. private SKBitmap BuildSquareCollageBitmap(string[] paths, int width, int height)
  131. {
  132. var bitmap = new SKBitmap(width, height);
  133. var imageIndex = 0;
  134. var cellWidth = width / 2;
  135. var cellHeight = height / 2;
  136. using (var canvas = new SKCanvas(bitmap))
  137. {
  138. for (var x = 0; x < 2; x++)
  139. {
  140. for (var y = 0; y < 2; y++)
  141. {
  142. using (var currentBitmap = SKBitmap.Decode(paths[imageIndex]))
  143. {
  144. using (var resizedBitmap = new SKBitmap(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
  145. {
  146. // scale image
  147. currentBitmap.Resize(resizedBitmap, SKBitmapResizeMethod.Lanczos3);
  148. // draw this image into the strip at the next position
  149. var xPos = x * cellWidth;
  150. var yPos = y * cellHeight;
  151. canvas.DrawBitmap(resizedBitmap, xPos, yPos);
  152. }
  153. }
  154. imageIndex++;
  155. if (imageIndex >= paths.Length)
  156. imageIndex = 0;
  157. }
  158. }
  159. }
  160. return bitmap;
  161. }
  162. }
  163. }