StripCollageBuilder.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using SkiaSharp;
  2. using MediaBrowser.Common.Configuration;
  3. using System;
  4. using System.IO;
  5. using MediaBrowser.Model.IO;
  6. using System.Collections.Generic;
  7. namespace Emby.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. var ext = Path.GetExtension(outputPath).ToLower();
  21. if (ext == ".jpg" || ext == ".jpeg")
  22. return SKEncodedImageFormat.Jpeg;
  23. if (ext == ".webp")
  24. return SKEncodedImageFormat.Webp;
  25. if (ext == ".gif")
  26. return SKEncodedImageFormat.Gif;
  27. if (ext == ".bmp")
  28. return SKEncodedImageFormat.Bmp;
  29. // default to png
  30. return SKEncodedImageFormat.Png;
  31. }
  32. public void BuildPosterCollage(string[] paths, string outputPath, int width, int height)
  33. {
  34. // @todo
  35. }
  36. public void BuildSquareCollage(string[] paths, string outputPath, int width, int height)
  37. {
  38. using (var bitmap = BuildSquareCollageBitmap(paths, width, height))
  39. {
  40. using (var outputStream = new SKFileWStream(outputPath))
  41. {
  42. bitmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
  43. }
  44. }
  45. }
  46. public void BuildThumbCollage(string[] paths, string outputPath, int width, int height)
  47. {
  48. using (var bitmap = BuildThumbCollageBitmap(paths, width, height))
  49. {
  50. using (var outputStream = new SKFileWStream(outputPath))
  51. {
  52. bitmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
  53. }
  54. }
  55. }
  56. private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height)
  57. {
  58. var bitmap = new SKBitmap(width, height);
  59. using (var canvas = new SKCanvas(bitmap))
  60. {
  61. canvas.Clear(SKColors.Black);
  62. // determine sizes for each image that will composited into the final image
  63. var iSlice = Convert.ToInt32(width * 0.23475);
  64. int iTrans = Convert.ToInt32(height * .25);
  65. int iHeight = Convert.ToInt32(height * .70);
  66. var horizontalImagePadding = Convert.ToInt32(width * 0.0125);
  67. var verticalSpacing = Convert.ToInt32(height * 0.01111111111111111111111111111111);
  68. int imageIndex = 0;
  69. for (int i = 0; i < 4; i++)
  70. {
  71. int newIndex;
  72. using (var currentBitmap = GetNextValidImage(paths, imageIndex, out newIndex))
  73. {
  74. imageIndex = newIndex;
  75. if (currentBitmap == null)
  76. {
  77. continue;
  78. }
  79. // resize to the same aspect as the original
  80. int iWidth = (int)Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height);
  81. using (var resizeBitmap = new SKBitmap(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
  82. {
  83. currentBitmap.Resize(resizeBitmap, SKBitmapResizeMethod.Lanczos3);
  84. // crop image
  85. int ix = (int)Math.Abs((iWidth - iSlice) / 2);
  86. using (var image = SKImage.FromBitmap(resizeBitmap))
  87. using (var subset = image.Subset(SKRectI.Create(ix, 0, iSlice, iHeight)))
  88. {
  89. // draw image onto canvas
  90. canvas.DrawImage(subset ?? image, (horizontalImagePadding * (i + 1)) + (iSlice * i), verticalSpacing);
  91. if (subset == null)
  92. {
  93. continue;
  94. }
  95. // create reflection of image below the drawn image
  96. using (var croppedBitmap = SKBitmap.FromImage(subset))
  97. using (var reflectionBitmap = new SKBitmap(croppedBitmap.Width, croppedBitmap.Height / 2, croppedBitmap.ColorType, croppedBitmap.AlphaType))
  98. {
  99. // resize to half height
  100. croppedBitmap.Resize(reflectionBitmap, SKBitmapResizeMethod.Lanczos3);
  101. using (var flippedBitmap = new SKBitmap(reflectionBitmap.Width, reflectionBitmap.Height, reflectionBitmap.ColorType, reflectionBitmap.AlphaType))
  102. using (var flippedCanvas = new SKCanvas(flippedBitmap))
  103. {
  104. // flip image vertically
  105. var matrix = SKMatrix.MakeScale(1, -1);
  106. matrix.SetScaleTranslate(1, -1, 0, flippedBitmap.Height);
  107. flippedCanvas.SetMatrix(matrix);
  108. flippedCanvas.DrawBitmap(reflectionBitmap, 0, 0);
  109. flippedCanvas.ResetMatrix();
  110. // create gradient to make image appear as a reflection
  111. var remainingHeight = height - (iHeight + (2 * verticalSpacing));
  112. flippedCanvas.ClipRect(SKRect.Create(reflectionBitmap.Width, remainingHeight));
  113. using (var gradient = new SKPaint())
  114. {
  115. gradient.IsAntialias = true;
  116. gradient.BlendMode = SKBlendMode.SrcOver;
  117. 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);
  118. flippedCanvas.DrawPaint(gradient);
  119. }
  120. // finally draw reflection onto canvas
  121. canvas.DrawBitmap(flippedBitmap, (horizontalImagePadding * (i + 1)) + (iSlice * i), iHeight + (2 * verticalSpacing));
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. return bitmap;
  130. }
  131. private SKBitmap GetNextValidImage(string[] paths, int currentIndex, out int newIndex)
  132. {
  133. Dictionary<int, int> imagesTested = new Dictionary<int, int>();
  134. SKBitmap bitmap = null;
  135. while (imagesTested.Count < paths.Length)
  136. {
  137. if (currentIndex >= paths.Length)
  138. {
  139. currentIndex = 0;
  140. }
  141. SKEncodedOrigin origin;
  142. bitmap = SkiaEncoder.Decode(paths[currentIndex], false, _fileSystem, null, out origin);
  143. imagesTested[currentIndex] = 0;
  144. currentIndex++;
  145. if (bitmap != null)
  146. {
  147. break;
  148. }
  149. }
  150. newIndex = currentIndex;
  151. return bitmap;
  152. }
  153. private SKBitmap BuildSquareCollageBitmap(string[] paths, int width, int height)
  154. {
  155. var bitmap = new SKBitmap(width, height);
  156. var imageIndex = 0;
  157. var cellWidth = width / 2;
  158. var cellHeight = height / 2;
  159. using (var canvas = new SKCanvas(bitmap))
  160. {
  161. for (var x = 0; x < 2; x++)
  162. {
  163. for (var y = 0; y < 2; y++)
  164. {
  165. int newIndex;
  166. using (var currentBitmap = GetNextValidImage(paths, imageIndex, out newIndex))
  167. {
  168. imageIndex = newIndex;
  169. if (currentBitmap == null)
  170. {
  171. continue;
  172. }
  173. using (var resizedBitmap = new SKBitmap(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
  174. {
  175. // scale image
  176. currentBitmap.Resize(resizedBitmap, SKBitmapResizeMethod.Lanczos3);
  177. // draw this image into the strip at the next position
  178. var xPos = x * cellWidth;
  179. var yPos = y * cellHeight;
  180. canvas.DrawBitmap(resizedBitmap, xPos, yPos);
  181. }
  182. }
  183. }
  184. }
  185. }
  186. return bitmap;
  187. }
  188. }
  189. }