2017-05-09 19:53:46 +00:00
|
|
|
using System;
|
2019-01-13 19:16:43 +00:00
|
|
|
using System.Collections.Generic;
|
2017-05-09 19:53:46 +00:00
|
|
|
using System.IO;
|
2019-01-13 19:16:43 +00:00
|
|
|
using MediaBrowser.Common.Configuration;
|
2017-05-09 19:53:46 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
2019-01-13 19:16:43 +00:00
|
|
|
using SkiaSharp;
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2019-01-20 03:38:41 +00:00
|
|
|
namespace Emby.Drawing
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
|
|
|
public class StripCollageBuilder
|
|
|
|
{
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
|
|
|
public StripCollageBuilder(IApplicationPaths appPaths, IFileSystem fileSystem)
|
|
|
|
{
|
|
|
|
_appPaths = appPaths;
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
}
|
|
|
|
|
2017-05-10 04:49:11 +00:00
|
|
|
public static SKEncodedImageFormat GetEncodedFormat(string outputPath)
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
if (outputPath == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(outputPath));
|
|
|
|
}
|
|
|
|
|
2017-05-09 19:53:46 +00:00
|
|
|
var ext = Path.GetExtension(outputPath).ToLower();
|
|
|
|
|
|
|
|
if (ext == ".jpg" || ext == ".jpeg")
|
|
|
|
return SKEncodedImageFormat.Jpeg;
|
|
|
|
|
|
|
|
if (ext == ".webp")
|
|
|
|
return SKEncodedImageFormat.Webp;
|
|
|
|
|
|
|
|
if (ext == ".gif")
|
|
|
|
return SKEncodedImageFormat.Gif;
|
|
|
|
|
|
|
|
if (ext == ".bmp")
|
|
|
|
return SKEncodedImageFormat.Bmp;
|
|
|
|
|
|
|
|
// default to png
|
|
|
|
return SKEncodedImageFormat.Png;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void BuildPosterCollage(string[] paths, string outputPath, int width, int height)
|
|
|
|
{
|
2017-05-10 04:49:11 +00:00
|
|
|
// @todo
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void BuildSquareCollage(string[] paths, string outputPath, int width, int height)
|
|
|
|
{
|
|
|
|
using (var bitmap = BuildSquareCollageBitmap(paths, width, height))
|
|
|
|
{
|
|
|
|
using (var outputStream = new SKFileWStream(outputPath))
|
|
|
|
{
|
2019-01-03 18:11:18 +00:00
|
|
|
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels()))
|
|
|
|
{
|
|
|
|
pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
|
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void BuildThumbCollage(string[] paths, string outputPath, int width, int height)
|
|
|
|
{
|
|
|
|
using (var bitmap = BuildThumbCollageBitmap(paths, width, height))
|
|
|
|
{
|
|
|
|
using (var outputStream = new SKFileWStream(outputPath))
|
|
|
|
{
|
2019-01-03 18:11:18 +00:00
|
|
|
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels()))
|
|
|
|
{
|
|
|
|
pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
|
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 04:49:11 +00:00
|
|
|
private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height)
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2017-05-10 04:49:11 +00:00
|
|
|
var bitmap = new SKBitmap(width, height);
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2017-05-10 04:49:11 +00:00
|
|
|
using (var canvas = new SKCanvas(bitmap))
|
|
|
|
{
|
|
|
|
canvas.Clear(SKColors.Black);
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2017-05-12 04:57:09 +00:00
|
|
|
// determine sizes for each image that will composited into the final image
|
|
|
|
var iSlice = Convert.ToInt32(width * 0.23475);
|
2017-05-10 04:49:11 +00:00
|
|
|
int iTrans = Convert.ToInt32(height * .25);
|
|
|
|
int iHeight = Convert.ToInt32(height * .70);
|
|
|
|
var horizontalImagePadding = Convert.ToInt32(width * 0.0125);
|
|
|
|
var verticalSpacing = Convert.ToInt32(height * 0.01111111111111111111111111111111);
|
|
|
|
int imageIndex = 0;
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2017-05-10 04:49:11 +00:00
|
|
|
for (int i = 0; i < 4; i++)
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2019-01-03 17:59:12 +00:00
|
|
|
using (var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex))
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
imageIndex = newIndex;
|
|
|
|
|
|
|
|
if (currentBitmap == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-12 04:57:09 +00:00
|
|
|
// resize to the same aspect as the original
|
2017-05-10 04:49:11 +00:00
|
|
|
int iWidth = (int)Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height);
|
|
|
|
using (var resizeBitmap = new SKBitmap(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2019-01-03 17:59:12 +00:00
|
|
|
currentBitmap.ScalePixels(resizeBitmap, SKFilterQuality.High);
|
2018-12-13 21:34:28 +00:00
|
|
|
// crop image
|
2017-05-09 19:53:46 +00:00
|
|
|
int ix = (int)Math.Abs((iWidth - iSlice) / 2);
|
2017-05-10 04:49:11 +00:00
|
|
|
using (var image = SKImage.FromBitmap(resizeBitmap))
|
2018-12-13 21:34:28 +00:00
|
|
|
using (var subset = image.Subset(SKRectI.Create(ix, 0, iSlice, iHeight)))
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2018-12-13 21:34:28 +00:00
|
|
|
// draw image onto canvas
|
|
|
|
canvas.DrawImage(subset ?? image, (horizontalImagePadding * (i + 1)) + (iSlice * i), verticalSpacing);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
if (subset == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// create reflection of image below the drawn image
|
|
|
|
using (var croppedBitmap = SKBitmap.FromImage(subset))
|
|
|
|
using (var reflectionBitmap = new SKBitmap(croppedBitmap.Width, croppedBitmap.Height / 2, croppedBitmap.ColorType, croppedBitmap.AlphaType))
|
|
|
|
{
|
|
|
|
// resize to half height
|
2019-01-03 17:59:12 +00:00
|
|
|
currentBitmap.ScalePixels(reflectionBitmap, SKFilterQuality.High);
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
using (var flippedBitmap = new SKBitmap(reflectionBitmap.Width, reflectionBitmap.Height, reflectionBitmap.ColorType, reflectionBitmap.AlphaType))
|
|
|
|
using (var flippedCanvas = new SKCanvas(flippedBitmap))
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2018-12-13 21:34:28 +00:00
|
|
|
// flip image vertically
|
|
|
|
var matrix = SKMatrix.MakeScale(1, -1);
|
|
|
|
matrix.SetScaleTranslate(1, -1, 0, flippedBitmap.Height);
|
|
|
|
flippedCanvas.SetMatrix(matrix);
|
|
|
|
flippedCanvas.DrawBitmap(reflectionBitmap, 0, 0);
|
|
|
|
flippedCanvas.ResetMatrix();
|
|
|
|
|
|
|
|
// create gradient to make image appear as a reflection
|
|
|
|
var remainingHeight = height - (iHeight + (2 * verticalSpacing));
|
|
|
|
flippedCanvas.ClipRect(SKRect.Create(reflectionBitmap.Width, remainingHeight));
|
|
|
|
using (var gradient = new SKPaint())
|
2017-05-10 04:49:11 +00:00
|
|
|
{
|
2018-12-13 21:34:28 +00:00
|
|
|
gradient.IsAntialias = true;
|
|
|
|
gradient.BlendMode = SKBlendMode.SrcOver;
|
|
|
|
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);
|
|
|
|
flippedCanvas.DrawPaint(gradient);
|
2017-05-10 04:49:11 +00:00
|
|
|
}
|
2018-12-13 21:34:28 +00:00
|
|
|
|
|
|
|
// finally draw reflection onto canvas
|
|
|
|
canvas.DrawBitmap(flippedBitmap, (horizontalImagePadding * (i + 1)) + (iSlice * i), iHeight + (2 * verticalSpacing));
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
private SKBitmap GetNextValidImage(string[] paths, int currentIndex, out int newIndex)
|
|
|
|
{
|
2019-01-13 20:37:13 +00:00
|
|
|
var imagesTested = new Dictionary<int, int>();
|
2018-09-12 17:26:21 +00:00
|
|
|
SKBitmap bitmap = null;
|
|
|
|
|
|
|
|
while (imagesTested.Count < paths.Length)
|
|
|
|
{
|
|
|
|
if (currentIndex >= paths.Length)
|
|
|
|
{
|
|
|
|
currentIndex = 0;
|
|
|
|
}
|
|
|
|
|
2019-01-13 20:37:13 +00:00
|
|
|
bitmap = SkiaEncoder.Decode(paths[currentIndex], false, _fileSystem, null, out var origin);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
imagesTested[currentIndex] = 0;
|
2017-05-10 04:49:11 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
currentIndex++;
|
2017-05-10 04:49:11 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (bitmap != null)
|
|
|
|
{
|
|
|
|
break;
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
2017-05-10 04:49:11 +00:00
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
newIndex = currentIndex;
|
2017-05-10 04:49:11 +00:00
|
|
|
return bitmap;
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private SKBitmap BuildSquareCollageBitmap(string[] paths, int width, int height)
|
|
|
|
{
|
|
|
|
var bitmap = new SKBitmap(width, height);
|
|
|
|
var imageIndex = 0;
|
|
|
|
var cellWidth = width / 2;
|
|
|
|
var cellHeight = height / 2;
|
|
|
|
|
|
|
|
using (var canvas = new SKCanvas(bitmap))
|
|
|
|
{
|
|
|
|
for (var x = 0; x < 2; x++)
|
|
|
|
{
|
|
|
|
for (var y = 0; y < 2; y++)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2019-01-03 17:59:12 +00:00
|
|
|
using (var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex))
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
imageIndex = newIndex;
|
|
|
|
|
|
|
|
if (currentBitmap == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-09 19:53:46 +00:00
|
|
|
using (var resizedBitmap = new SKBitmap(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
|
|
|
|
{
|
|
|
|
// scale image
|
2019-01-03 17:59:12 +00:00
|
|
|
currentBitmap.ScalePixels(resizedBitmap, SKFilterQuality.High);
|
2017-05-10 04:49:11 +00:00
|
|
|
|
2017-05-09 19:53:46 +00:00
|
|
|
// draw this image into the strip at the next position
|
|
|
|
var xPos = x * cellWidth;
|
|
|
|
var yPos = y * cellHeight;
|
|
|
|
canvas.DrawBitmap(resizedBitmap, xPos, yPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 21:34:28 +00:00
|
|
|
}
|