jellyfin/Emby.Drawing/ImageMagick/StripCollageBuilder.cs

201 lines
9.3 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;
2015-10-04 04:23:11 +00:00
using CommonIO;
2015-03-13 20:09:07 +00:00
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;
2016-06-18 17:26:57 +00:00
private readonly IFileSystem _fileSystem;
2015-03-13 20:09:07 +00:00
2016-06-18 17:26:57 +00:00
public StripCollageBuilder(IApplicationPaths appPaths, IFileSystem fileSystem)
2015-03-13 20:09:07 +00:00
{
_appPaths = appPaths;
2016-06-18 17:26:57 +00:00
_fileSystem = fileSystem;
2015-03-13 20:09:07 +00:00
}
2016-06-18 17:26:57 +00:00
public void BuildPosterCollage(List<string> paths, string outputPath, int width, int height)
2015-04-02 16:58:52 +00:00
{
2016-06-18 17:26:57 +00:00
using (var wand = BuildPosterCollageWand(paths, width, height))
2015-04-02 16:58:52 +00:00
{
2016-06-18 17:26:57 +00:00
wand.SaveImage(outputPath);
2015-04-02 21:01:42 +00:00
}
}
2016-06-18 17:26:57 +00:00
public void BuildSquareCollage(List<string> paths, string outputPath, int width, int height)
2015-04-02 21:01:42 +00:00
{
2016-06-18 17:26:57 +00:00
using (var wand = BuildSquareCollageWand(paths, width, height))
2015-04-02 17:44:44 +00:00
{
2016-06-18 17:26:57 +00:00
wand.SaveImage(outputPath);
2015-04-02 21:01:42 +00:00
}
2015-04-02 16:58:52 +00:00
}
2016-06-18 17:26:57 +00:00
public void BuildThumbCollage(List<string> paths, string outputPath, int width, int height)
2015-03-13 20:09:07 +00:00
{
2016-06-18 17:26:57 +00:00
using (var wand = BuildThumbCollageWand(paths, width, height))
2015-04-02 17:44:44 +00:00
{
2016-06-18 17:26:57 +00:00
wand.SaveImage(outputPath);
2015-03-13 20:09:07 +00:00
}
}
2015-05-11 16:32:15 +00:00
private MagickWand BuildPosterCollageWand(List<string> paths, int width, int height)
2015-04-02 21:01:42 +00:00
{
2015-10-08 19:12:53 +00:00
var inputPaths = ImageHelpers.ProjectPaths(paths, 3);
2015-05-11 16:32:15 +00:00
using (var wandImages = new MagickWand(inputPaths.ToArray()))
2015-04-02 21:01:42 +00:00
{
var wand = new MagickWand(width, height);
wand.OpenImage("gradient:#111111-#111111");
using (var draw = new DrawingWand())
{
2015-10-08 19:12:53 +00:00
var iSlice = Convert.ToInt32(width * 0.3);
2015-04-02 21:01:42 +00:00
int iTrans = Convert.ToInt32(height * .25);
int iHeight = Convert.ToInt32(height * .65);
2015-10-08 19:12:53 +00:00
var horizontalImagePadding = Convert.ToInt32(width * 0.0366);
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;
}
}
2015-05-11 16:32:15 +00:00
private MagickWand BuildThumbCollageWand(List<string> paths, int width, int height)
2015-03-14 04:50:23 +00:00
{
2015-07-29 20:31:15 +00:00
var inputPaths = ImageHelpers.ProjectPaths(paths, 4);
2015-05-11 16:32:15 +00:00
using (var wandImages = new MagickWand(inputPaths.ToArray()))
2015-03-14 04:50:23 +00:00
{
var wand = new MagickWand(width, height);
wand.OpenImage("gradient:#111111-#111111");
using (var draw = new DrawingWand())
{
var iSlice = Convert.ToInt32(width * 0.24125);
2015-03-14 04:50:23 +00:00
int iTrans = Convert.ToInt32(height * .25);
int iHeight = Convert.ToInt32(height * .70);
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 * .045));
2015-04-06 20:55:37 +00:00
}
}
2015-03-14 04:50:23 +00:00
}
}
}
}
return wand;
}
}
2015-05-11 16:32:15 +00:00
private MagickWand BuildSquareCollageWand(List<string> paths, int width, int height)
2015-04-02 17:44:44 +00:00
{
2015-05-11 16:32:15 +00:00
var inputPaths = ImageHelpers.ProjectPaths(paths, 4);
2016-06-18 17:26:57 +00:00
var outputWand = new MagickWand(width, height, new PixelWand("none", 1));
var imageIndex = 0;
var cellWidth = width/2;
var cellHeight = height/2;
for (var x = 0; x < 2; x++)
2015-04-02 17:44:44 +00:00
{
2016-06-18 17:26:57 +00:00
for (var y = 0; y < 2; y++)
2015-04-02 17:44:44 +00:00
{
2016-06-18 17:26:57 +00:00
using (var temp = new MagickWand(inputPaths[imageIndex]))
2015-04-02 17:44:44 +00:00
{
2016-06-18 17:26:57 +00:00
temp.CurrentImage.ScaleImage(cellWidth, cellHeight);
// draw this image into the strip at the next position
var xPos = x*cellWidth;
var yPos = y*cellHeight;
outputWand.CurrentImage.CompositeImage(temp, CompositeOperator.OverCompositeOp, xPos, yPos);
2015-04-02 17:44:44 +00:00
}
2016-06-18 17:26:57 +00:00
imageIndex++;
2015-04-02 17:44:44 +00:00
}
}
2016-06-18 17:26:57 +00:00
return outputWand;
2015-03-13 20:09:07 +00:00
}
}
}