jellyfin-server/Emby.Drawing/ImageMagick/StripCollageBuilder.cs

519 lines
26 KiB
C#
Raw Normal View History

2015-03-13 20:09:07 +00:00
using ImageMagickSharp;
using MediaBrowser.Common.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
2015-04-08 14:38:02 +00:00
namespace Emby.Drawing.ImageMagick
2015-03-13 20:09:07 +00:00
{
public class StripCollageBuilder
{
private readonly IApplicationPaths _appPaths;
public StripCollageBuilder(IApplicationPaths appPaths)
{
_appPaths = appPaths;
}
2015-04-08 14:38:02 +00:00
public void BuildPosterCollage(IEnumerable<string> paths, string outputPath, int width, int height, string text)
2015-04-02 16:58:52 +00:00
{
2015-04-08 14:38:02 +00:00
if (!string.IsNullOrWhiteSpace(text))
2015-04-02 16:58:52 +00:00
{
2015-04-02 21:01:42 +00:00
using (var wand = BuildPosterCollageWandWithText(paths, text, width, height))
2015-04-02 17:44:44 +00:00
{
2015-04-08 14:38:02 +00:00
wand.SaveImage(outputPath);
2015-04-02 17:44:44 +00:00
}
}
2015-04-08 14:38:02 +00:00
else
2015-04-02 21:01:42 +00:00
{
2015-04-08 14:38:02 +00:00
using (var wand = BuildPosterCollageWand(paths, width, height))
{
wand.SaveImage(outputPath);
}
2015-04-02 21:01:42 +00:00
}
}
2015-04-08 14:38:02 +00:00
public void BuildSquareCollage(IEnumerable<string> paths, string outputPath, int width, int height, string text)
2015-04-02 21:01:42 +00:00
{
2015-04-08 14:38:02 +00:00
if (!string.IsNullOrWhiteSpace(text))
2015-04-02 17:44:44 +00:00
{
2015-04-02 21:01:42 +00:00
using (var wand = BuildSquareCollageWandWithText(paths, text, width, height))
2015-04-02 17:44:44 +00:00
{
2015-04-08 14:38:02 +00:00
wand.SaveImage(outputPath);
2015-04-02 17:44:44 +00:00
}
2015-04-02 16:58:52 +00:00
}
2015-04-08 14:38:02 +00:00
else
2015-04-02 21:01:42 +00:00
{
2015-04-08 14:38:02 +00:00
using (var wand = BuildSquareCollageWand(paths, width, height))
{
wand.SaveImage(outputPath);
}
2015-04-02 21:01:42 +00:00
}
2015-04-02 16:58:52 +00:00
}
2015-04-08 14:38:02 +00:00
public void BuildThumbCollage(IEnumerable<string> paths, string outputPath, int width, int height, string text)
2015-03-13 20:09:07 +00:00
{
2015-04-08 14:38:02 +00:00
if (!string.IsNullOrWhiteSpace(text))
2015-04-02 17:44:44 +00:00
{
using (var wand = BuildThumbCollageWandWithText(paths, text, width, height))
{
2015-04-08 14:38:02 +00:00
wand.SaveImage(outputPath);
2015-04-02 17:44:44 +00:00
}
}
2015-04-08 14:38:02 +00:00
else
2015-03-13 20:09:07 +00:00
{
2015-04-08 14:38:02 +00:00
using (var wand = BuildThumbCollageWand(paths, width, height))
{
wand.SaveImage(outputPath);
}
2015-03-13 20:09:07 +00:00
}
}
2015-03-14 04:50:23 +00:00
private string[] ProjectPaths(IEnumerable<string> paths, int count)
2015-03-13 20:09:07 +00:00
{
var clone = paths.ToList();
var list = new List<string>();
while (list.Count < count)
{
2015-03-14 04:50:23 +00:00
foreach (var path in clone)
{
list.Add(path);
if (list.Count >= count)
{
break;
}
}
2015-03-13 20:09:07 +00:00
}
2015-03-14 04:50:23 +00:00
return list.Take(count).ToArray();
2015-03-13 20:09:07 +00:00
}
2015-03-14 04:50:23 +00:00
private MagickWand BuildThumbCollageWandWithText(IEnumerable<string> paths, string text, int width, int height)
2015-03-13 20:09:07 +00:00
{
2015-03-14 04:50:23 +00:00
var inputPaths = ProjectPaths(paths, 8);
using (var wandImages = new MagickWand(inputPaths))
2015-03-13 20:09:07 +00:00
{
var wand = new MagickWand(width, height);
2015-03-14 04:50:23 +00:00
wand.OpenImage("gradient:#111111-#111111");
2015-03-13 20:09:07 +00:00
using (var draw = new DrawingWand())
{
using (var fcolor = new PixelWand(ColorName.White))
{
draw.FillColor = fcolor;
draw.Font = MontserratLightFont;
2015-04-02 17:44:44 +00:00
draw.FontSize = 60;
2015-03-13 20:09:07 +00:00
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())
{
2015-04-06 20:55:37 +00:00
using (var blackPixelWand = new PixelWand(ColorName.Black))
2015-03-13 20:09:07 +00:00
{
2015-04-06 20:55:37 +00:00
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));
}
}
2015-03-13 20:09:07 +00:00
}
}
}
}
return wand;
}
}
2015-04-02 21:01:42 +00:00
private MagickWand BuildPosterCollageWand(IEnumerable<string> paths, int width, int height)
{
2015-04-03 02:54:40 +00:00
var inputPaths = ProjectPaths(paths, 4);
2015-04-02 21:01:42 +00:00
using (var wandImages = new MagickWand(inputPaths))
{
var wand = new MagickWand(width, height);
wand.OpenImage("gradient:#111111-#111111");
using (var draw = new DrawingWand())
{
2015-04-03 02:54:40 +00:00
var iSlice = Convert.ToInt32(width * 0.225);
2015-04-02 21:01:42 +00:00
int iTrans = Convert.ToInt32(height * .25);
int iHeight = Convert.ToInt32(height * .65);
2015-04-03 02:54:40 +00:00
var horizontalImagePadding = Convert.ToInt32(width * 0.0275);
2015-04-02 21:01:42 +00:00
foreach (var element in wandImages.ImageList)
{
2015-04-06 20:55:37 +00:00
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);
}
2015-04-02 21:01:42 +00:00
}
wandImages.SetFirstIterator();
using (var wandList = wandImages.AppendImages())
{
wandList.CurrentImage.TrimImage(1);
using (var mwr = wandList.CloneMagickWand())
{
2015-04-06 20:55:37 +00:00
using (var blackPixelWand = new PixelWand(ColorName.Black))
2015-04-02 21:01:42 +00:00
{
2015-04-06 20:55:37 +00:00
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));
}
}
2015-04-02 21:01:42 +00:00
}
}
}
}
return wand;
}
}
private MagickWand BuildPosterCollageWandWithText(IEnumerable<string> paths, string label, int width, int height)
{
2015-04-03 02:54:40 +00:00
var inputPaths = ProjectPaths(paths, 4);
2015-04-02 21:01:42 +00:00
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);
2015-04-03 02:54:40 +00:00
var iSlice = Convert.ToInt32(width * 0.225);
2015-04-02 21:01:42 +00:00
int iTrans = Convert.ToInt32(height * 0.2);
int iHeight = Convert.ToInt32(height * 0.46296296296296296296296296296296);
2015-04-03 02:54:40 +00:00
var horizontalImagePadding = Convert.ToInt32(width * 0.0275);
2015-04-02 21:01:42 +00:00
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())
{
2015-04-06 20:55:37 +00:00
using (var blackPixelWand = new PixelWand(ColorName.Black))
2015-04-02 21:01:42 +00:00
{
2015-04-06 20:55:37 +00:00
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));
}
}
2015-04-02 21:01:42 +00:00
}
}
}
}
return wand;
}
}
2015-03-14 04:50:23 +00:00
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);
2015-03-26 04:44:24 +00:00
int iHeight = Convert.ToInt32(height * .62);
2015-03-14 04:50:23 +00:00
var horizontalImagePadding = Convert.ToInt32(width * 0.0125);
foreach (var element in wandImages.ImageList)
{
2015-04-06 20:55:37 +00:00
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);
}
2015-03-14 04:50:23 +00:00
}
wandImages.SetFirstIterator();
using (var wandList = wandImages.AppendImages())
{
wandList.CurrentImage.TrimImage(1);
using (var mwr = wandList.CloneMagickWand())
{
2015-04-06 20:55:37 +00:00
using (var blackPixelWand = new PixelWand(ColorName.Black))
2015-03-14 04:50:23 +00:00
{
2015-04-06 20:55:37 +00:00
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));
}
}
2015-03-14 04:50:23 +00:00
}
}
}
}
return wand;
}
}
2015-04-02 16:58:52 +00:00
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())
{
2015-04-02 21:01:42 +00:00
var iSlice = Convert.ToInt32(width * .225);
2015-04-02 16:58:52 +00:00
int iTrans = Convert.ToInt32(height * .25);
2015-04-02 21:01:42 +00:00
int iHeight = Convert.ToInt32(height * .63);
2015-04-02 17:44:44 +00:00
var horizontalImagePadding = Convert.ToInt32(width * 0.02);
2015-04-02 16:58:52 +00:00
foreach (var element in wandImages.ImageList)
{
2015-04-06 20:55:37 +00:00
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);
}
2015-04-02 16:58:52 +00:00
}
wandImages.SetFirstIterator();
using (var wandList = wandImages.AppendImages())
{
wandList.CurrentImage.TrimImage(1);
using (var mwr = wandList.CloneMagickWand())
{
2015-04-06 20:55:37 +00:00
using (var blackPixelWand = new PixelWand(ColorName.Black))
2015-04-02 16:58:52 +00:00
{
2015-04-06 20:55:37 +00:00
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));
}
}
2015-04-02 16:58:52 +00:00
}
}
}
}
return wand;
}
}
2015-04-02 17:44:44 +00:00
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);
2015-04-02 21:01:42 +00:00
var iSlice = Convert.ToInt32(width * .225);
2015-04-02 17:44:44 +00:00
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())
{
2015-04-06 20:55:37 +00:00
using (var blackPixelWand = new PixelWand(ColorName.Black))
2015-04-02 17:44:44 +00:00
{
2015-04-06 20:55:37 +00:00
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));
}
}
2015-04-02 17:44:44 +00:00
}
}
}
}
return wand;
}
}
2015-03-13 20:09:07 +00:00
private string MontserratLightFont
{
get { return PlayedIndicatorDrawer.ExtractFont("MontserratLight.otf", _appPaths); }
}
}
}