2021-08-15 14:32:08 -04:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using SkiaSharp;
|
|
|
|
|
|
|
|
namespace Jellyfin.Drawing.Skia
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Used to build the splashscreen.
|
|
|
|
/// </summary>
|
|
|
|
public class SplashscreenBuilder
|
|
|
|
{
|
2021-08-16 06:26:41 -04:00
|
|
|
private const int FinalWidth = 1920;
|
|
|
|
private const int FinalHeight = 1080;
|
|
|
|
// generated collage resolution should be higher than the final resolution
|
|
|
|
private const int WallWidth = FinalWidth * 3;
|
|
|
|
private const int WallHeight = FinalHeight * 2;
|
2021-08-15 14:32:08 -04:00
|
|
|
private const int Rows = 6;
|
|
|
|
private const int Spacing = 20;
|
|
|
|
|
|
|
|
private readonly SkiaEncoder _skiaEncoder;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="SplashscreenBuilder"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="skiaEncoder">The SkiaEncoder.</param>
|
|
|
|
public SplashscreenBuilder(SkiaEncoder skiaEncoder)
|
|
|
|
{
|
|
|
|
_skiaEncoder = skiaEncoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generate a splashscreen.
|
|
|
|
/// </summary>
|
2021-08-17 12:11:38 -04:00
|
|
|
/// <param name="posters">The poster paths.</param>
|
2022-01-10 10:25:46 -05:00
|
|
|
/// <param name="backdrops">The landscape paths.</param>
|
2021-08-17 12:11:38 -04:00
|
|
|
/// <param name="outputPath">The output path.</param>
|
2022-01-10 10:25:46 -05:00
|
|
|
public void GenerateSplash(IReadOnlyList<string> posters, IReadOnlyList<string> backdrops, string outputPath)
|
2021-08-15 14:32:08 -04:00
|
|
|
{
|
2022-01-10 10:25:46 -05:00
|
|
|
using var wall = GenerateCollage(posters, backdrops);
|
|
|
|
using var transformed = Transform3D(wall);
|
2021-08-15 14:32:08 -04:00
|
|
|
|
2021-08-17 12:11:38 -04:00
|
|
|
using var outputStream = new SKFileWStream(outputPath);
|
2021-08-16 06:26:41 -04:00
|
|
|
using var pixmap = new SKPixmap(new SKImageInfo(FinalWidth, FinalHeight), transformed.GetPixels());
|
2021-08-17 12:11:38 -04:00
|
|
|
pixmap.Encode(outputStream, StripCollageBuilder.GetEncodedFormat(outputPath), 90);
|
2021-08-15 14:32:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generates a collage of posters and landscape pictures.
|
|
|
|
/// </summary>
|
2021-08-17 12:11:38 -04:00
|
|
|
/// <param name="posters">The poster paths.</param>
|
2022-01-10 10:25:46 -05:00
|
|
|
/// <param name="backdrops">The landscape paths.</param>
|
2021-08-15 14:32:08 -04:00
|
|
|
/// <returns>The created collage as a bitmap.</returns>
|
2022-01-10 10:25:46 -05:00
|
|
|
private SKBitmap GenerateCollage(IReadOnlyList<string> posters, IReadOnlyList<string> backdrops)
|
2021-08-15 14:32:08 -04:00
|
|
|
{
|
|
|
|
var posterIndex = 0;
|
|
|
|
var backdropIndex = 0;
|
|
|
|
|
2021-08-16 06:26:41 -04:00
|
|
|
var bitmap = new SKBitmap(WallWidth, WallHeight);
|
2021-08-15 14:32:08 -04:00
|
|
|
using var canvas = new SKCanvas(bitmap);
|
|
|
|
canvas.Clear(SKColors.Black);
|
|
|
|
|
2021-08-16 06:26:41 -04:00
|
|
|
int posterHeight = WallHeight / 6;
|
2021-08-15 14:32:08 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < Rows; i++)
|
|
|
|
{
|
2022-01-10 12:59:32 -05:00
|
|
|
int imageCounter = Random.Shared.Next(0, 5);
|
2021-08-15 14:32:08 -04:00
|
|
|
int currentWidthPos = i * 75;
|
|
|
|
int currentHeight = i * (posterHeight + Spacing);
|
|
|
|
|
2021-08-16 06:26:41 -04:00
|
|
|
while (currentWidthPos < WallWidth)
|
2021-08-15 14:32:08 -04:00
|
|
|
{
|
|
|
|
SKBitmap? currentImage;
|
|
|
|
|
|
|
|
switch (imageCounter)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2021-08-17 12:11:38 -04:00
|
|
|
currentImage = SkiaHelper.GetNextValidImage(_skiaEncoder, posters, posterIndex, out int newPosterIndex);
|
2021-08-15 14:32:08 -04:00
|
|
|
posterIndex = newPosterIndex;
|
|
|
|
break;
|
|
|
|
default:
|
2022-01-10 10:25:46 -05:00
|
|
|
currentImage = SkiaHelper.GetNextValidImage(_skiaEncoder, backdrops, backdropIndex, out int newBackdropIndex);
|
2021-08-15 14:32:08 -04:00
|
|
|
backdropIndex = newBackdropIndex;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-12-05 09:00:20 -05:00
|
|
|
if (currentImage is null)
|
2021-08-15 14:32:08 -04:00
|
|
|
{
|
|
|
|
throw new ArgumentException("Not enough valid pictures provided to create a splashscreen!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// resize to the same aspect as the original
|
|
|
|
var imageWidth = Math.Abs(posterHeight * currentImage.Width / currentImage.Height);
|
|
|
|
using var resizedBitmap = new SKBitmap(imageWidth, posterHeight);
|
|
|
|
currentImage.ScalePixels(resizedBitmap, SKFilterQuality.High);
|
|
|
|
|
|
|
|
// draw on canvas
|
|
|
|
canvas.DrawBitmap(resizedBitmap, currentWidthPos, currentHeight);
|
|
|
|
|
|
|
|
currentWidthPos += imageWidth + Spacing;
|
|
|
|
|
|
|
|
currentImage.Dispose();
|
|
|
|
|
|
|
|
if (imageCounter >= 4)
|
|
|
|
{
|
|
|
|
imageCounter = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
imageCounter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Transform the collage in 3D space.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="input">The bitmap to transform.</param>
|
|
|
|
/// <returns>The transformed image.</returns>
|
|
|
|
private SKBitmap Transform3D(SKBitmap input)
|
|
|
|
{
|
2021-08-16 06:26:41 -04:00
|
|
|
var bitmap = new SKBitmap(FinalWidth, FinalHeight);
|
2021-08-15 14:32:08 -04:00
|
|
|
using var canvas = new SKCanvas(bitmap);
|
|
|
|
canvas.Clear(SKColors.Black);
|
|
|
|
var matrix = new SKMatrix
|
|
|
|
{
|
|
|
|
ScaleX = 0.324108899f,
|
|
|
|
ScaleY = 0.563934922f,
|
|
|
|
SkewX = -0.244337708f,
|
|
|
|
SkewY = 0.0377609022f,
|
|
|
|
TransX = 42.0407715f,
|
|
|
|
TransY = -198.104706f,
|
|
|
|
Persp0 = -9.08959337E-05f,
|
|
|
|
Persp1 = 6.85242048E-05f,
|
|
|
|
Persp2 = 0.988209724f
|
|
|
|
};
|
|
|
|
|
|
|
|
canvas.SetMatrix(matrix);
|
|
|
|
canvas.DrawBitmap(input, 0, 0);
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|