You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
228 lines
9.5 KiB
228 lines
9.5 KiB
8 years ago
|
using System;
|
||
6 years ago
|
using System.Collections.Generic;
|
||
8 years ago
|
using System.IO;
|
||
6 years ago
|
using MediaBrowser.Common.Configuration;
|
||
8 years ago
|
using MediaBrowser.Model.IO;
|
||
6 years ago
|
using SkiaSharp;
|
||
8 years ago
|
|
||
6 years ago
|
namespace Jellyfin.Drawing.Skia
|
||
8 years ago
|
{
|
||
|
public class StripCollageBuilder
|
||
|
{
|
||
|
private readonly IApplicationPaths _appPaths;
|
||
|
private readonly IFileSystem _fileSystem;
|
||
|
|
||
|
public StripCollageBuilder(IApplicationPaths appPaths, IFileSystem fileSystem)
|
||
|
{
|
||
|
_appPaths = appPaths;
|
||
|
_fileSystem = fileSystem;
|
||
|
}
|
||
|
|
||
8 years ago
|
public static SKEncodedImageFormat GetEncodedFormat(string outputPath)
|
||
8 years ago
|
{
|
||
6 years ago
|
if (outputPath == null)
|
||
|
{
|
||
|
throw new ArgumentNullException(nameof(outputPath));
|
||
|
}
|
||
|
|
||
8 years ago
|
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 BuildSquareCollage(string[] paths, string outputPath, int width, int height)
|
||
|
{
|
||
|
using (var bitmap = BuildSquareCollageBitmap(paths, width, height))
|
||
6 years ago
|
using (var outputStream = new SKFileWStream(outputPath))
|
||
8 years ago
|
{
|
||
6 years ago
|
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels()))
|
||
8 years ago
|
{
|
||
6 years ago
|
pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void BuildThumbCollage(string[] paths, string outputPath, int width, int height)
|
||
|
{
|
||
|
using (var bitmap = BuildThumbCollageBitmap(paths, width, height))
|
||
|
{
|
||
|
using (var outputStream = new SKFileWStream(outputPath))
|
||
|
{
|
||
6 years ago
|
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels()))
|
||
|
{
|
||
|
pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
|
||
|
}
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height)
|
||
8 years ago
|
{
|
||
8 years ago
|
var bitmap = new SKBitmap(width, height);
|
||
8 years ago
|
|
||
8 years ago
|
using (var canvas = new SKCanvas(bitmap))
|
||
|
{
|
||
|
canvas.Clear(SKColors.Black);
|
||
8 years ago
|
|
||
8 years ago
|
// determine sizes for each image that will composited into the final image
|
||
|
var iSlice = Convert.ToInt32(width * 0.23475);
|
||
8 years ago
|
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;
|
||
8 years ago
|
|
||
8 years ago
|
for (int i = 0; i < 4; i++)
|
||
8 years ago
|
{
|
||
6 years ago
|
|
||
6 years ago
|
using (var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex))
|
||
8 years ago
|
{
|
||
6 years ago
|
imageIndex = newIndex;
|
||
|
|
||
|
if (currentBitmap == null)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
8 years ago
|
// resize to the same aspect as the original
|
||
8 years ago
|
int iWidth = (int)Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height);
|
||
|
using (var resizeBitmap = new SKBitmap(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
|
||
8 years ago
|
{
|
||
6 years ago
|
currentBitmap.ScalePixels(resizeBitmap, SKFilterQuality.High);
|
||
6 years ago
|
// crop image
|
||
8 years ago
|
int ix = (int)Math.Abs((iWidth - iSlice) / 2);
|
||
8 years ago
|
using (var image = SKImage.FromBitmap(resizeBitmap))
|
||
6 years ago
|
using (var subset = image.Subset(SKRectI.Create(ix, 0, iSlice, iHeight)))
|
||
8 years ago
|
{
|
||
6 years ago
|
// draw image onto canvas
|
||
|
canvas.DrawImage(subset ?? image, (horizontalImagePadding * (i + 1)) + (iSlice * i), verticalSpacing);
|
||
6 years ago
|
|
||
6 years ago
|
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
|
||
6 years ago
|
currentBitmap.ScalePixels(reflectionBitmap, SKFilterQuality.High);
|
||
8 years ago
|
|
||
6 years ago
|
using (var flippedBitmap = new SKBitmap(reflectionBitmap.Width, reflectionBitmap.Height, reflectionBitmap.ColorType, reflectionBitmap.AlphaType))
|
||
|
using (var flippedCanvas = new SKCanvas(flippedBitmap))
|
||
8 years ago
|
{
|
||
6 years ago
|
// 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())
|
||
8 years ago
|
{
|
||
6 years ago
|
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);
|
||
8 years ago
|
}
|
||
6 years ago
|
|
||
|
// finally draw reflection onto canvas
|
||
|
canvas.DrawBitmap(flippedBitmap, (horizontalImagePadding * (i + 1)) + (iSlice * i), iHeight + (2 * verticalSpacing));
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return bitmap;
|
||
|
}
|
||
|
|
||
|
private SKBitmap GetNextValidImage(string[] paths, int currentIndex, out int newIndex)
|
||
|
{
|
||
6 years ago
|
var imagesTested = new Dictionary<int, int>();
|
||
6 years ago
|
SKBitmap bitmap = null;
|
||
|
|
||
|
while (imagesTested.Count < paths.Length)
|
||
|
{
|
||
|
if (currentIndex >= paths.Length)
|
||
|
{
|
||
|
currentIndex = 0;
|
||
|
}
|
||
|
|
||
6 years ago
|
bitmap = SkiaEncoder.Decode(paths[currentIndex], false, _fileSystem, null, out var origin);
|
||
6 years ago
|
|
||
|
imagesTested[currentIndex] = 0;
|
||
8 years ago
|
|
||
6 years ago
|
currentIndex++;
|
||
8 years ago
|
|
||
6 years ago
|
if (bitmap != null)
|
||
|
{
|
||
|
break;
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
6 years ago
|
newIndex = currentIndex;
|
||
8 years ago
|
return bitmap;
|
||
8 years ago
|
}
|
||
|
|
||
|
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++)
|
||
|
{
|
||
6 years ago
|
|
||
6 years ago
|
using (var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex))
|
||
8 years ago
|
{
|
||
6 years ago
|
imageIndex = newIndex;
|
||
|
|
||
|
if (currentBitmap == null)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
8 years ago
|
using (var resizedBitmap = new SKBitmap(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
|
||
|
{
|
||
|
// scale image
|
||
6 years ago
|
currentBitmap.ScalePixels(resizedBitmap, SKFilterQuality.High);
|
||
8 years ago
|
|
||
8 years ago
|
// 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;
|
||
|
}
|
||
|
}
|
||
6 years ago
|
}
|