aboutsummaryrefslogtreecommitdiff
path: root/Emby.Drawing/ImageMagick
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2015-04-14 00:43:41 -0400
committerLuke <luke.pulverenti@gmail.com>2015-04-14 00:43:41 -0400
commit935de313d58c7a7ba792345c16cfd1c1aad09a78 (patch)
tree2e9334986de5864b00d4901f031b5de6a970305e /Emby.Drawing/ImageMagick
parent71a4f2761e784513ae2f3dda03aa549903808ebb (diff)
parentbd253399c2f1913c544c93fd6927dee37f8add2f (diff)
Merge pull request #1079 from MediaBrowser/dev
3.0.5582.0
Diffstat (limited to 'Emby.Drawing/ImageMagick')
-rw-r--r--Emby.Drawing/ImageMagick/ImageMagickEncoder.cs229
-rw-r--r--Emby.Drawing/ImageMagick/PercentPlayedDrawer.cs40
-rw-r--r--Emby.Drawing/ImageMagick/PlayedIndicatorDrawer.cs86
-rw-r--r--Emby.Drawing/ImageMagick/StripCollageBuilder.cs518
-rw-r--r--Emby.Drawing/ImageMagick/UnplayedCountIndicator.cs70
5 files changed, 943 insertions, 0 deletions
diff --git a/Emby.Drawing/ImageMagick/ImageMagickEncoder.cs b/Emby.Drawing/ImageMagick/ImageMagickEncoder.cs
new file mode 100644
index 000000000..3d6cdd03d
--- /dev/null
+++ b/Emby.Drawing/ImageMagick/ImageMagickEncoder.cs
@@ -0,0 +1,229 @@
+using ImageMagickSharp;
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Controller.Drawing;
+using MediaBrowser.Model.Drawing;
+using MediaBrowser.Model.Logging;
+using System;
+using System.IO;
+
+namespace Emby.Drawing.ImageMagick
+{
+ public class ImageMagickEncoder : IImageEncoder
+ {
+ private readonly ILogger _logger;
+ private readonly IApplicationPaths _appPaths;
+
+ public ImageMagickEncoder(ILogger logger, IApplicationPaths appPaths)
+ {
+ _logger = logger;
+ _appPaths = appPaths;
+
+ LogImageMagickVersion();
+ }
+
+ public string[] SupportedInputFormats
+ {
+ get
+ {
+ // Some common file name extensions for RAW picture files include: .cr2, .crw, .dng, .nef, .orf, .rw2, .pef, .arw, .sr2, .srf, and .tif.
+ return new[]
+ {
+ "tiff",
+ "jpeg",
+ "jpg",
+ "png",
+ "aiff",
+ "cr2",
+ "crw",
+ "dng",
+ "nef",
+ "orf",
+ "pef",
+ "arw",
+ "webp",
+ "gif",
+ "bmp"
+ };
+ }
+ }
+
+ public ImageFormat[] SupportedOutputFormats
+ {
+ get
+ {
+ if (_webpAvailable)
+ {
+ return new[] { ImageFormat.Webp, ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png };
+ }
+ return new[] { ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png };
+ }
+ }
+
+ private void LogImageMagickVersion()
+ {
+ _logger.Info("ImageMagick version: " + Wand.VersionString);
+ TestWebp();
+ }
+
+ private bool _webpAvailable = true;
+ private void TestWebp()
+ {
+ try
+ {
+ var tmpPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".webp");
+ Directory.CreateDirectory(Path.GetDirectoryName(tmpPath));
+
+ using (var wand = new MagickWand(1, 1, new PixelWand("none", 1)))
+ {
+ wand.SaveImage(tmpPath);
+ }
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error loading webp: ", ex);
+ _webpAvailable = false;
+ }
+ }
+
+ public void CropWhiteSpace(string inputPath, string outputPath)
+ {
+ CheckDisposed();
+
+ using (var wand = new MagickWand(inputPath))
+ {
+ wand.CurrentImage.TrimImage(10);
+ wand.SaveImage(outputPath);
+ }
+ }
+
+ public ImageSize GetImageSize(string path)
+ {
+ CheckDisposed();
+
+ using (var wand = new MagickWand())
+ {
+ wand.PingImage(path);
+ var img = wand.CurrentImage;
+
+ return new ImageSize
+ {
+ Width = img.Width,
+ Height = img.Height
+ };
+ }
+ }
+
+ public void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options)
+ {
+ if (string.IsNullOrWhiteSpace(options.BackgroundColor))
+ {
+ using (var originalImage = new MagickWand(inputPath))
+ {
+ originalImage.CurrentImage.ResizeImage(width, height);
+
+ DrawIndicator(originalImage, width, height, options);
+
+ originalImage.CurrentImage.CompressionQuality = quality;
+
+ originalImage.SaveImage(outputPath);
+ }
+ }
+ else
+ {
+ using (var wand = new MagickWand(width, height, options.BackgroundColor))
+ {
+ using (var originalImage = new MagickWand(inputPath))
+ {
+ originalImage.CurrentImage.ResizeImage(width, height);
+
+ wand.CurrentImage.CompositeImage(originalImage, CompositeOperator.OverCompositeOp, 0, 0);
+ DrawIndicator(wand, width, height, options);
+
+ wand.CurrentImage.CompressionQuality = quality;
+
+ wand.SaveImage(outputPath);
+ }
+ }
+ }
+ }
+
+ /// <summary>
+ /// Draws the indicator.
+ /// </summary>
+ /// <param name="wand">The wand.</param>
+ /// <param name="imageWidth">Width of the image.</param>
+ /// <param name="imageHeight">Height of the image.</param>
+ /// <param name="options">The options.</param>
+ private void DrawIndicator(MagickWand wand, int imageWidth, int imageHeight, ImageProcessingOptions options)
+ {
+ if (!options.AddPlayedIndicator && !options.UnplayedCount.HasValue && options.PercentPlayed.Equals(0))
+ {
+ return;
+ }
+
+ try
+ {
+ if (options.AddPlayedIndicator)
+ {
+ var currentImageSize = new ImageSize(imageWidth, imageHeight);
+
+ new PlayedIndicatorDrawer(_appPaths).DrawPlayedIndicator(wand, currentImageSize);
+ }
+ else if (options.UnplayedCount.HasValue)
+ {
+ var currentImageSize = new ImageSize(imageWidth, imageHeight);
+
+ new UnplayedCountIndicator(_appPaths).DrawUnplayedCountIndicator(wand, currentImageSize, options.UnplayedCount.Value);
+ }
+
+ if (options.PercentPlayed > 0)
+ {
+ new PercentPlayedDrawer().Process(wand, options.PercentPlayed);
+ }
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error drawing indicator overlay", ex);
+ }
+ }
+
+ public void CreateImageCollage(ImageCollageOptions options)
+ {
+ double ratio = options.Width;
+ ratio /= options.Height;
+
+ if (ratio >= 1.4)
+ {
+ new StripCollageBuilder(_appPaths).BuildThumbCollage(options.InputPaths, options.OutputPath, options.Width, options.Height, options.Text);
+ }
+ else if (ratio >= .9)
+ {
+ new StripCollageBuilder(_appPaths).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height, options.Text);
+ }
+ else
+ {
+ new StripCollageBuilder(_appPaths).BuildPosterCollage(options.InputPaths, options.OutputPath, options.Width, options.Height, options.Text);
+ }
+ }
+
+ public string Name
+ {
+ get { return "ImageMagick"; }
+ }
+
+ private bool _disposed;
+ public void Dispose()
+ {
+ _disposed = true;
+ Wand.CloseEnvironment();
+ }
+
+ private void CheckDisposed()
+ {
+ if (_disposed)
+ {
+ throw new ObjectDisposedException(GetType().Name);
+ }
+ }
+ }
+}
diff --git a/Emby.Drawing/ImageMagick/PercentPlayedDrawer.cs b/Emby.Drawing/ImageMagick/PercentPlayedDrawer.cs
new file mode 100644
index 000000000..90f9d5609
--- /dev/null
+++ b/Emby.Drawing/ImageMagick/PercentPlayedDrawer.cs
@@ -0,0 +1,40 @@
+using ImageMagickSharp;
+using System;
+
+namespace Emby.Drawing.ImageMagick
+{
+ public class PercentPlayedDrawer
+ {
+ private const int IndicatorHeight = 8;
+
+ public void Process(MagickWand wand, double percent)
+ {
+ var currentImage = wand.CurrentImage;
+ var height = currentImage.Height;
+
+ using (var draw = new DrawingWand())
+ {
+ using (PixelWand pixel = new PixelWand())
+ {
+ var endX = currentImage.Width - 1;
+ var endY = height - 1;
+
+ pixel.Color = "black";
+ pixel.Opacity = 0.4;
+ draw.FillColor = pixel;
+ draw.DrawRectangle(0, endY - IndicatorHeight, endX, endY);
+
+ double foregroundWidth = endX;
+ foregroundWidth *= percent;
+ foregroundWidth /= 100;
+
+ pixel.Color = "#52B54B";
+ pixel.Opacity = 0;
+ draw.FillColor = pixel;
+ draw.DrawRectangle(0, endY - IndicatorHeight, Convert.ToInt32(Math.Round(foregroundWidth)), endY);
+ wand.CurrentImage.DrawImage(draw);
+ }
+ }
+ }
+ }
+}
diff --git a/Emby.Drawing/ImageMagick/PlayedIndicatorDrawer.cs b/Emby.Drawing/ImageMagick/PlayedIndicatorDrawer.cs
new file mode 100644
index 000000000..5eeb15771
--- /dev/null
+++ b/Emby.Drawing/ImageMagick/PlayedIndicatorDrawer.cs
@@ -0,0 +1,86 @@
+using ImageMagickSharp;
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Model.Drawing;
+using System;
+using System.IO;
+
+namespace Emby.Drawing.ImageMagick
+{
+ public class PlayedIndicatorDrawer
+ {
+ private const int FontSize = 52;
+ private const int OffsetFromTopRightCorner = 38;
+
+ private readonly IApplicationPaths _appPaths;
+
+ public PlayedIndicatorDrawer(IApplicationPaths appPaths)
+ {
+ _appPaths = appPaths;
+ }
+
+ public void DrawPlayedIndicator(MagickWand wand, ImageSize imageSize)
+ {
+ var x = imageSize.Width - OffsetFromTopRightCorner;
+
+ using (var draw = new DrawingWand())
+ {
+ using (PixelWand pixel = new PixelWand())
+ {
+ pixel.Color = "#52B54B";
+ pixel.Opacity = 0.2;
+ draw.FillColor = pixel;
+ draw.DrawCircle(x, OffsetFromTopRightCorner, x - 20, OffsetFromTopRightCorner - 20);
+
+ pixel.Opacity = 0;
+ pixel.Color = "white";
+ draw.FillColor = pixel;
+ draw.Font = ExtractFont("webdings.ttf", _appPaths);
+ draw.FontSize = FontSize;
+ draw.FontStyle = FontStyleType.NormalStyle;
+ draw.TextAlignment = TextAlignType.CenterAlign;
+ draw.FontWeight = FontWeightType.RegularStyle;
+ draw.TextAntialias = true;
+ draw.DrawAnnotation(x + 4, OffsetFromTopRightCorner + 14, "a");
+
+ draw.FillColor = pixel;
+ wand.CurrentImage.DrawImage(draw);
+ }
+ }
+ }
+
+ internal static string ExtractFont(string name, IApplicationPaths paths)
+ {
+ var filePath = Path.Combine(paths.ProgramDataPath, "fonts", name);
+
+ if (File.Exists(filePath))
+ {
+ return filePath;
+ }
+
+ var namespacePath = typeof(PlayedIndicatorDrawer).Namespace + ".fonts." + name;
+ var tempPath = Path.Combine(paths.TempDirectory, Guid.NewGuid().ToString("N") + ".ttf");
+ Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
+
+ using (var stream = typeof(PlayedIndicatorDrawer).Assembly.GetManifestResourceStream(namespacePath))
+ {
+ using (var fileStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.Read))
+ {
+ stream.CopyTo(fileStream);
+ }
+ }
+
+ Directory.CreateDirectory(Path.GetDirectoryName(filePath));
+
+ try
+ {
+ File.Copy(tempPath, filePath, false);
+ }
+ catch (IOException)
+ {
+
+ }
+
+ return tempPath;
+ }
+ }
+}
diff --git a/Emby.Drawing/ImageMagick/StripCollageBuilder.cs b/Emby.Drawing/ImageMagick/StripCollageBuilder.cs
new file mode 100644
index 000000000..7cdd0077d
--- /dev/null
+++ b/Emby.Drawing/ImageMagick/StripCollageBuilder.cs
@@ -0,0 +1,518 @@
+using ImageMagickSharp;
+using MediaBrowser.Common.Configuration;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Emby.Drawing.ImageMagick
+{
+ public class StripCollageBuilder
+ {
+ private readonly IApplicationPaths _appPaths;
+
+ public StripCollageBuilder(IApplicationPaths appPaths)
+ {
+ _appPaths = appPaths;
+ }
+
+ public void BuildPosterCollage(IEnumerable<string> paths, string outputPath, int width, int height, string text)
+ {
+ if (!string.IsNullOrWhiteSpace(text))
+ {
+ using (var wand = BuildPosterCollageWandWithText(paths, text, width, height))
+ {
+ wand.SaveImage(outputPath);
+ }
+ }
+ else
+ {
+ using (var wand = BuildPosterCollageWand(paths, width, height))
+ {
+ wand.SaveImage(outputPath);
+ }
+ }
+ }
+
+ public void BuildSquareCollage(IEnumerable<string> paths, string outputPath, int width, int height, string text)
+ {
+ if (!string.IsNullOrWhiteSpace(text))
+ {
+ using (var wand = BuildSquareCollageWandWithText(paths, text, width, height))
+ {
+ wand.SaveImage(outputPath);
+ }
+ }
+ else
+ {
+ using (var wand = BuildSquareCollageWand(paths, width, height))
+ {
+ wand.SaveImage(outputPath);
+ }
+ }
+ }
+
+ public void BuildThumbCollage(IEnumerable<string> paths, string outputPath, int width, int height, string text)
+ {
+ if (!string.IsNullOrWhiteSpace(text))
+ {
+ using (var wand = BuildThumbCollageWandWithText(paths, text, width, height))
+ {
+ wand.SaveImage(outputPath);
+ }
+ }
+ else
+ {
+ using (var wand = BuildThumbCollageWand(paths, width, height))
+ {
+ wand.SaveImage(outputPath);
+ }
+ }
+ }
+
+ internal static string[] ProjectPaths(IEnumerable<string> paths, int count)
+ {
+ var clone = paths.ToList();
+ var list = new List<string>();
+
+ while (list.Count < count)
+ {
+ foreach (var path in clone)
+ {
+ list.Add(path);
+
+ if (list.Count >= count)
+ {
+ break;
+ }
+ }
+ }
+
+ return list.Take(count).ToArray();
+ }
+
+ private MagickWand BuildThumbCollageWandWithText(IEnumerable<string> paths, string text, int width, int height)
+ {
+ var inputPaths = ProjectPaths(paths, 8);
+ using (var wandImages = new MagickWand(inputPaths))
+ {
+ var wand = new MagickWand(width, height);
+ wand.OpenImage("gradient:#111111-#111111");
+ using (var draw = new DrawingWand())
+ {
+ using (var fcolor = new PixelWand(ColorName.White))
+ {
+ draw.FillColor = fcolor;
+ draw.Font = MontserratLightFont;
+ draw.FontSize = 60;
+ draw.FontWeight = FontWeightType.LightStyle;
+ draw.TextAntialias = true;
+ }
+
+ var fontMetrics = wand.QueryFontMetrics(draw, text);
+ var textContainerY = Convert.ToInt32(height * .165);
+ wand.CurrentImage.AnnotateImage(draw, (width - fontMetrics.TextWidth) / 2, textContainerY, 0.0, text);
+
+ var iSlice = Convert.ToInt32(width * .1166666667);
+ int iTrans = Convert.ToInt32(height * 0.2);
+ int iHeight = Convert.ToInt32(height * 0.46296296296296296296296296296296);
+ var horizontalImagePadding = Convert.ToInt32(width * 0.0125);
+
+ foreach (var element in wandImages.ImageList)
+ {
+ int iWidth = (int)Math.Abs(iHeight * element.Width / element.Height);
+ element.Gravity = GravityType.CenterGravity;
+ element.BackgroundColor = new PixelWand("none", 1);
+ element.ResizeImage(iWidth, iHeight, FilterTypes.LanczosFilter);
+ int ix = (int)Math.Abs((iWidth - iSlice) / 2);
+ element.CropImage(iSlice, iHeight, ix, 0);
+
+ element.ExtentImage(iSlice, iHeight, 0 - horizontalImagePadding, 0);
+ }
+
+ wandImages.SetFirstIterator();
+ using (var wandList = wandImages.AppendImages())
+ {
+ wandList.CurrentImage.TrimImage(1);
+ using (var mwr = wandList.CloneMagickWand())
+ {
+ using (var blackPixelWand = new PixelWand(ColorName.Black))
+ {
+ using (var greyPixelWand = new PixelWand(ColorName.Grey70))
+ {
+ mwr.CurrentImage.ResizeImage(wandList.CurrentImage.Width, (wandList.CurrentImage.Height / 2), FilterTypes.LanczosFilter, 1);
+ mwr.CurrentImage.FlipImage();
+
+ mwr.CurrentImage.AlphaChannel = AlphaChannelType.DeactivateAlphaChannel;
+ mwr.CurrentImage.ColorizeImage(blackPixelWand, greyPixelWand);
+
+ using (var mwg = new MagickWand(wandList.CurrentImage.Width, iTrans))
+ {
+ mwg.OpenImage("gradient:black-none");
+ var verticalSpacing = Convert.ToInt32(height * 0.01111111111111111111111111111111);
+ mwr.CurrentImage.CompositeImage(mwg, CompositeOperator.DstInCompositeOp, 0, verticalSpacing);
+
+ wandList.AddImage(mwr);
+ int ex = (int)(wand.CurrentImage.Width - mwg.CurrentImage.Width) / 2;
+ wand.CurrentImage.CompositeImage(wandList.AppendImages(true), CompositeOperator.AtopCompositeOp, ex, Convert.ToInt32(height * 0.26851851851851851851851851851852));
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return wand;
+ }
+ }
+
+ private MagickWand BuildPosterCollageWand(IEnumerable<string> paths, int width, int height)
+ {
+ var inputPaths = ProjectPaths(paths, 4);
+ using (var wandImages = new MagickWand(inputPaths))
+ {
+ var wand = new MagickWand(width, height);
+ wand.OpenImage("gradient:#111111-#111111");
+ using (var draw = new DrawingWand())
+ {
+ var iSlice = Convert.ToInt32(width * 0.225);
+ int iTrans = Convert.ToInt32(height * .25);
+ int iHeight = Convert.ToInt32(height * .65);
+ var horizontalImagePadding = Convert.ToInt32(width * 0.0275);
+
+ foreach (var element in wandImages.ImageList)
+ {
+ using (var blackPixelWand = new PixelWand(ColorName.Black))
+ {
+ int iWidth = (int)Math.Abs(iHeight * element.Width / element.Height);
+ element.Gravity = GravityType.CenterGravity;
+ element.BackgroundColor = blackPixelWand;
+ element.ResizeImage(iWidth, iHeight, FilterTypes.LanczosFilter);
+ int ix = (int)Math.Abs((iWidth - iSlice) / 2);
+ element.CropImage(iSlice, iHeight, ix, 0);
+
+ element.ExtentImage(iSlice, iHeight, 0 - horizontalImagePadding, 0);
+ }
+ }
+
+ wandImages.SetFirstIterator();
+ using (var wandList = wandImages.AppendImages())
+ {
+ wandList.CurrentImage.TrimImage(1);
+ using (var mwr = wandList.CloneMagickWand())
+ {
+ using (var blackPixelWand = new PixelWand(ColorName.Black))
+ {
+ using (var greyPixelWand = new PixelWand(ColorName.Grey70))
+ {
+ mwr.CurrentImage.ResizeImage(wandList.CurrentImage.Width, (wandList.CurrentImage.Height / 2), FilterTypes.LanczosFilter, 1);
+ mwr.CurrentImage.FlipImage();
+
+ mwr.CurrentImage.AlphaChannel = AlphaChannelType.DeactivateAlphaChannel;
+ mwr.CurrentImage.ColorizeImage(blackPixelWand, greyPixelWand);
+
+ using (var mwg = new MagickWand(wandList.CurrentImage.Width, iTrans))
+ {
+ mwg.OpenImage("gradient:black-none");
+ var verticalSpacing = Convert.ToInt32(height * 0.01111111111111111111111111111111);
+ mwr.CurrentImage.CompositeImage(mwg, CompositeOperator.CopyOpacityCompositeOp, 0, verticalSpacing);
+
+ wandList.AddImage(mwr);
+ int ex = (int)(wand.CurrentImage.Width - mwg.CurrentImage.Width) / 2;
+ wand.CurrentImage.CompositeImage(wandList.AppendImages(true), CompositeOperator.AtopCompositeOp, ex, Convert.ToInt32(height * .05));
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return wand;
+ }
+ }
+
+ private MagickWand BuildPosterCollageWandWithText(IEnumerable<string> paths, string label, int width, int height)
+ {
+ var inputPaths = ProjectPaths(paths, 4);
+ using (var wandImages = new MagickWand(inputPaths))
+ {
+ var wand = new MagickWand(width, height);
+ wand.OpenImage("gradient:#111111-#111111");
+ using (var draw = new DrawingWand())
+ {
+ using (var fcolor = new PixelWand(ColorName.White))
+ {
+ draw.FillColor = fcolor;
+ draw.Font = MontserratLightFont;
+ draw.FontSize = 60;
+ draw.FontWeight = FontWeightType.LightStyle;
+ draw.TextAntialias = true;
+ }
+
+ var fontMetrics = wand.QueryFontMetrics(draw, label);
+ var textContainerY = Convert.ToInt32(height * .165);
+ wand.CurrentImage.AnnotateImage(draw, (width - fontMetrics.TextWidth) / 2, textContainerY, 0.0, label);
+
+ var iSlice = Convert.ToInt32(width * 0.225);
+ int iTrans = Convert.ToInt32(height * 0.2);
+ int iHeight = Convert.ToInt32(height * 0.46296296296296296296296296296296);
+ var horizontalImagePadding = Convert.ToInt32(width * 0.0275);
+
+ foreach (var element in wandImages.ImageList)
+ {
+ int iWidth = (int)Math.Abs(iHeight * element.Width / element.Height);
+ element.Gravity = GravityType.CenterGravity;
+ element.BackgroundColor = new PixelWand("none", 1);
+ element.ResizeImage(iWidth, iHeight, FilterTypes.LanczosFilter);
+ int ix = (int)Math.Abs((iWidth - iSlice) / 2);
+ element.CropImage(iSlice, iHeight, ix, 0);
+
+ element.ExtentImage(iSlice, iHeight, 0 - horizontalImagePadding, 0);
+ }
+
+ wandImages.SetFirstIterator();
+ using (var wandList = wandImages.AppendImages())
+ {
+ wandList.CurrentImage.TrimImage(1);
+ using (var mwr = wandList.CloneMagickWand())
+ {
+ using (var blackPixelWand = new PixelWand(ColorName.Black))
+ {
+ using (var greyPixelWand = new PixelWand(ColorName.Grey70))
+ {
+ mwr.CurrentImage.ResizeImage(wandList.CurrentImage.Width, (wandList.CurrentImage.Height / 2), FilterTypes.LanczosFilter, 1);
+ mwr.CurrentImage.FlipImage();
+
+ mwr.CurrentImage.AlphaChannel = AlphaChannelType.DeactivateAlphaChannel;
+ mwr.CurrentImage.ColorizeImage(blackPixelWand, greyPixelWand);
+
+ using (var mwg = new MagickWand(wandList.CurrentImage.Width, iTrans))
+ {
+ mwg.OpenImage("gradient:black-none");
+ var verticalSpacing = Convert.ToInt32(height * 0.01111111111111111111111111111111);
+ mwr.CurrentImage.CompositeImage(mwg, CompositeOperator.DstInCompositeOp, 0, verticalSpacing);
+
+ wandList.AddImage(mwr);
+ int ex = (int)(wand.CurrentImage.Width - mwg.CurrentImage.Width) / 2;
+ wand.CurrentImage.CompositeImage(wandList.AppendImages(true), CompositeOperator.AtopCompositeOp, ex, Convert.ToInt32(height * 0.26851851851851851851851851851852));
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return wand;
+ }
+ }
+
+ private MagickWand BuildThumbCollageWand(IEnumerable<string> paths, int width, int height)
+ {
+ var inputPaths = ProjectPaths(paths, 8);
+ using (var wandImages = new MagickWand(inputPaths))
+ {
+ var wand = new MagickWand(width, height);
+ wand.OpenImage("gradient:#111111-#111111");
+ using (var draw = new DrawingWand())
+ {
+ var iSlice = Convert.ToInt32(width * .1166666667);
+ int iTrans = Convert.ToInt32(height * .25);
+ int iHeight = Convert.ToInt32(height * .62);
+ var horizontalImagePadding = Convert.ToInt32(width * 0.0125);
+
+ foreach (var element in wandImages.ImageList)
+ {
+ using (var blackPixelWand = new PixelWand(ColorName.Black))
+ {
+ int iWidth = (int)Math.Abs(iHeight * element.Width / element.Height);
+ element.Gravity = GravityType.CenterGravity;
+ element.BackgroundColor = blackPixelWand;
+ element.ResizeImage(iWidth, iHeight, FilterTypes.LanczosFilter);
+ int ix = (int)Math.Abs((iWidth - iSlice) / 2);
+ element.CropImage(iSlice, iHeight, ix, 0);
+
+ element.ExtentImage(iSlice, iHeight, 0 - horizontalImagePadding, 0);
+ }
+ }
+
+ wandImages.SetFirstIterator();
+ using (var wandList = wandImages.AppendImages())
+ {
+ wandList.CurrentImage.TrimImage(1);
+ using (var mwr = wandList.CloneMagickWand())
+ {
+ using (var blackPixelWand = new PixelWand(ColorName.Black))
+ {
+ using (var greyPixelWand = new PixelWand(ColorName.Grey70))
+ {
+ mwr.CurrentImage.ResizeImage(wandList.CurrentImage.Width, (wandList.CurrentImage.Height / 2), FilterTypes.LanczosFilter, 1);
+ mwr.CurrentImage.FlipImage();
+
+ mwr.CurrentImage.AlphaChannel = AlphaChannelType.DeactivateAlphaChannel;
+ mwr.CurrentImage.ColorizeImage(blackPixelWand, greyPixelWand);
+
+ using (var mwg = new MagickWand(wandList.CurrentImage.Width, iTrans))
+ {
+ mwg.OpenImage("gradient:black-none");
+ var verticalSpacing = Convert.ToInt32(height * 0.01111111111111111111111111111111);
+ mwr.CurrentImage.CompositeImage(mwg, CompositeOperator.CopyOpacityCompositeOp, 0, verticalSpacing);
+
+ wandList.AddImage(mwr);
+ int ex = (int)(wand.CurrentImage.Width - mwg.CurrentImage.Width) / 2;
+ wand.CurrentImage.CompositeImage(wandList.AppendImages(true), CompositeOperator.AtopCompositeOp, ex, Convert.ToInt32(height * .085));
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return wand;
+ }
+ }
+
+ private MagickWand BuildSquareCollageWand(IEnumerable<string> paths, int width, int height)
+ {
+ var inputPaths = ProjectPaths(paths, 4);
+ using (var wandImages = new MagickWand(inputPaths))
+ {
+ var wand = new MagickWand(width, height);
+ wand.OpenImage("gradient:#111111-#111111");
+ using (var draw = new DrawingWand())
+ {
+ var iSlice = Convert.ToInt32(width * .225);
+ int iTrans = Convert.ToInt32(height * .25);
+ int iHeight = Convert.ToInt32(height * .63);
+ var horizontalImagePadding = Convert.ToInt32(width * 0.02);
+
+ foreach (var element in wandImages.ImageList)
+ {
+ using (var blackPixelWand = new PixelWand(ColorName.Black))
+ {
+ int iWidth = (int)Math.Abs(iHeight * element.Width / element.Height);
+ element.Gravity = GravityType.CenterGravity;
+ element.BackgroundColor = blackPixelWand;
+ element.ResizeImage(iWidth, iHeight, FilterTypes.LanczosFilter);
+ int ix = (int)Math.Abs((iWidth - iSlice) / 2);
+ element.CropImage(iSlice, iHeight, ix, 0);
+
+ element.ExtentImage(iSlice, iHeight, 0 - horizontalImagePadding, 0);
+ }
+ }
+
+ wandImages.SetFirstIterator();
+ using (var wandList = wandImages.AppendImages())
+ {
+ wandList.CurrentImage.TrimImage(1);
+ using (var mwr = wandList.CloneMagickWand())
+ {
+ using (var blackPixelWand = new PixelWand(ColorName.Black))
+ {
+ using (var greyPixelWand = new PixelWand(ColorName.Grey70))
+ {
+ mwr.CurrentImage.ResizeImage(wandList.CurrentImage.Width, (wandList.CurrentImage.Height / 2), FilterTypes.LanczosFilter, 1);
+ mwr.CurrentImage.FlipImage();
+
+ mwr.CurrentImage.AlphaChannel = AlphaChannelType.DeactivateAlphaChannel;
+ mwr.CurrentImage.ColorizeImage(blackPixelWand, greyPixelWand);
+
+ using (var mwg = new MagickWand(wandList.CurrentImage.Width, iTrans))
+ {
+ mwg.OpenImage("gradient:black-none");
+ var verticalSpacing = Convert.ToInt32(height * 0.01111111111111111111111111111111);
+ mwr.CurrentImage.CompositeImage(mwg, CompositeOperator.CopyOpacityCompositeOp, 0, verticalSpacing);
+
+ wandList.AddImage(mwr);
+ int ex = (int)(wand.CurrentImage.Width - mwg.CurrentImage.Width) / 2;
+ wand.CurrentImage.CompositeImage(wandList.AppendImages(true), CompositeOperator.AtopCompositeOp, ex, Convert.ToInt32(height * .07));
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return wand;
+ }
+ }
+
+ private MagickWand BuildSquareCollageWandWithText(IEnumerable<string> paths, string label, int width, int height)
+ {
+ var inputPaths = ProjectPaths(paths, 4);
+ using (var wandImages = new MagickWand(inputPaths))
+ {
+ var wand = new MagickWand(width, height);
+ wand.OpenImage("gradient:#111111-#111111");
+ using (var draw = new DrawingWand())
+ {
+ using (var fcolor = new PixelWand(ColorName.White))
+ {
+ draw.FillColor = fcolor;
+ draw.Font = MontserratLightFont;
+ draw.FontSize = 60;
+ draw.FontWeight = FontWeightType.LightStyle;
+ draw.TextAntialias = true;
+ }
+
+ var fontMetrics = wand.QueryFontMetrics(draw, label);
+ var textContainerY = Convert.ToInt32(height * .165);
+ wand.CurrentImage.AnnotateImage(draw, (width - fontMetrics.TextWidth) / 2, textContainerY, 0.0, label);
+
+ var iSlice = Convert.ToInt32(width * .225);
+ int iTrans = Convert.ToInt32(height * 0.2);
+ int iHeight = Convert.ToInt32(height * 0.46296296296296296296296296296296);
+ var horizontalImagePadding = Convert.ToInt32(width * 0.02);
+
+ foreach (var element in wandImages.ImageList)
+ {
+ int iWidth = (int)Math.Abs(iHeight * element.Width / element.Height);
+ element.Gravity = GravityType.CenterGravity;
+ element.BackgroundColor = new PixelWand("none", 1);
+ element.ResizeImage(iWidth, iHeight, FilterTypes.LanczosFilter);
+ int ix = (int)Math.Abs((iWidth - iSlice) / 2);
+ element.CropImage(iSlice, iHeight, ix, 0);
+
+ element.ExtentImage(iSlice, iHeight, 0 - horizontalImagePadding, 0);
+ }
+
+ wandImages.SetFirstIterator();
+ using (var wandList = wandImages.AppendImages())
+ {
+ wandList.CurrentImage.TrimImage(1);
+ using (var mwr = wandList.CloneMagickWand())
+ {
+ using (var blackPixelWand = new PixelWand(ColorName.Black))
+ {
+ using (var greyPixelWand = new PixelWand(ColorName.Grey70))
+ {
+ mwr.CurrentImage.ResizeImage(wandList.CurrentImage.Width, (wandList.CurrentImage.Height / 2), FilterTypes.LanczosFilter, 1);
+ mwr.CurrentImage.FlipImage();
+
+ mwr.CurrentImage.AlphaChannel = AlphaChannelType.DeactivateAlphaChannel;
+ mwr.CurrentImage.ColorizeImage(blackPixelWand, greyPixelWand);
+
+ using (var mwg = new MagickWand(wandList.CurrentImage.Width, iTrans))
+ {
+ mwg.OpenImage("gradient:black-none");
+ var verticalSpacing = Convert.ToInt32(height * 0.01111111111111111111111111111111);
+ mwr.CurrentImage.CompositeImage(mwg, CompositeOperator.DstInCompositeOp, 0, verticalSpacing);
+
+ wandList.AddImage(mwr);
+ int ex = (int)(wand.CurrentImage.Width - mwg.CurrentImage.Width) / 2;
+ wand.CurrentImage.CompositeImage(wandList.AppendImages(true), CompositeOperator.AtopCompositeOp, ex, Convert.ToInt32(height * 0.26851851851851851851851851851852));
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return wand;
+ }
+ }
+
+ private string MontserratLightFont
+ {
+ get { return PlayedIndicatorDrawer.ExtractFont("MontserratLight.otf", _appPaths); }
+ }
+ }
+}
diff --git a/Emby.Drawing/ImageMagick/UnplayedCountIndicator.cs b/Emby.Drawing/ImageMagick/UnplayedCountIndicator.cs
new file mode 100644
index 000000000..dd25004d6
--- /dev/null
+++ b/Emby.Drawing/ImageMagick/UnplayedCountIndicator.cs
@@ -0,0 +1,70 @@
+using ImageMagickSharp;
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Model.Drawing;
+using System.Globalization;
+
+namespace Emby.Drawing.ImageMagick
+{
+ public class UnplayedCountIndicator
+ {
+ private const int OffsetFromTopRightCorner = 38;
+
+ private readonly IApplicationPaths _appPaths;
+
+ public UnplayedCountIndicator(IApplicationPaths appPaths)
+ {
+ _appPaths = appPaths;
+ }
+
+ public void DrawUnplayedCountIndicator(MagickWand wand, ImageSize imageSize, int count)
+ {
+ var x = imageSize.Width - OffsetFromTopRightCorner;
+ var text = count.ToString(CultureInfo.InvariantCulture);
+
+ using (var draw = new DrawingWand())
+ {
+ using (PixelWand pixel = new PixelWand())
+ {
+ pixel.Color = "#52B54B";
+ pixel.Opacity = 0.2;
+ draw.FillColor = pixel;
+ draw.DrawCircle(x, OffsetFromTopRightCorner, x - 20, OffsetFromTopRightCorner - 20);
+
+ pixel.Opacity = 0;
+ pixel.Color = "white";
+ draw.FillColor = pixel;
+ draw.Font = PlayedIndicatorDrawer.ExtractFont("robotoregular.ttf", _appPaths);
+ draw.FontStyle = FontStyleType.NormalStyle;
+ draw.TextAlignment = TextAlignType.CenterAlign;
+ draw.FontWeight = FontWeightType.RegularStyle;
+ draw.TextAntialias = true;
+
+ var fontSize = 30;
+ var y = OffsetFromTopRightCorner + 11;
+
+ if (text.Length == 1)
+ {
+ x += 1;
+ }
+ else if (text.Length == 2)
+ {
+ x += 1;
+ }
+ else if (text.Length >= 3)
+ {
+ //x += 1;
+ y -= 2;
+ fontSize = 24;
+ }
+
+ draw.FontSize = fontSize;
+ draw.DrawAnnotation(x, y, text);
+
+ draw.FillColor = pixel;
+ wand.CurrentImage.DrawImage(draw);
+ }
+
+ }
+ }
+ }
+}