StripCollageBuilder.cs 8.9 KB

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