StripCollageBuilder.cs 9.6 KB

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