StripCollageBuilder.cs 9.7 KB

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