2017-05-09 12:53:46 -07:00
|
|
|
using System;
|
2019-01-13 12:16:43 -07:00
|
|
|
using System.Collections.Generic;
|
2017-05-09 12:53:46 -07:00
|
|
|
using System.IO;
|
2020-12-25 10:24:25 -07:00
|
|
|
using System.Text.RegularExpressions;
|
2019-01-13 12:16:43 -07:00
|
|
|
using SkiaSharp;
|
2017-05-09 12:53:46 -07:00
|
|
|
|
2019-01-26 12:43:13 -07:00
|
|
|
namespace Jellyfin.Drawing.Skia
|
2017-05-09 12:53:46 -07:00
|
|
|
{
|
2019-12-13 12:57:23 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Used to build collages of multiple images arranged in vertical strips.
|
|
|
|
/// </summary>
|
2017-05-09 12:53:46 -07:00
|
|
|
public class StripCollageBuilder
|
|
|
|
{
|
2019-06-09 14:51:52 -07:00
|
|
|
private readonly SkiaEncoder _skiaEncoder;
|
2017-05-09 12:53:46 -07:00
|
|
|
|
2019-12-13 12:57:23 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="StripCollageBuilder"/> class.
|
|
|
|
/// </summary>
|
2019-12-14 03:04:22 -07:00
|
|
|
/// <param name="skiaEncoder">The encoder to use for building collages.</param>
|
2019-06-09 14:51:52 -07:00
|
|
|
public StripCollageBuilder(SkiaEncoder skiaEncoder)
|
2017-05-09 12:53:46 -07:00
|
|
|
{
|
2019-06-09 14:51:52 -07:00
|
|
|
_skiaEncoder = skiaEncoder;
|
2017-05-09 12:53:46 -07:00
|
|
|
}
|
|
|
|
|
2019-12-13 12:57:23 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Check which format an image has been encoded with using its filename extension.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="outputPath">The path to the image to get the format for.</param>
|
|
|
|
/// <returns>The image format.</returns>
|
2017-05-09 21:49:11 -07:00
|
|
|
public static SKEncodedImageFormat GetEncodedFormat(string outputPath)
|
2017-05-09 12:53:46 -07:00
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
if (outputPath == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(outputPath));
|
|
|
|
}
|
|
|
|
|
2019-06-09 14:51:52 -07:00
|
|
|
var ext = Path.GetExtension(outputPath);
|
2017-05-09 12:53:46 -07:00
|
|
|
|
2019-06-09 14:51:52 -07:00
|
|
|
if (string.Equals(ext, ".jpg", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| string.Equals(ext, ".jpeg", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2017-05-09 12:53:46 -07:00
|
|
|
return SKEncodedImageFormat.Jpeg;
|
2019-06-09 14:51:52 -07:00
|
|
|
}
|
2017-05-09 12:53:46 -07:00
|
|
|
|
2019-06-09 14:51:52 -07:00
|
|
|
if (string.Equals(ext, ".webp", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2017-05-09 12:53:46 -07:00
|
|
|
return SKEncodedImageFormat.Webp;
|
2019-06-09 14:51:52 -07:00
|
|
|
}
|
2017-05-09 12:53:46 -07:00
|
|
|
|
2019-06-09 14:51:52 -07:00
|
|
|
if (string.Equals(ext, ".gif", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2017-05-09 12:53:46 -07:00
|
|
|
return SKEncodedImageFormat.Gif;
|
2019-06-09 14:51:52 -07:00
|
|
|
}
|
2017-05-09 12:53:46 -07:00
|
|
|
|
2019-06-09 14:51:52 -07:00
|
|
|
if (string.Equals(ext, ".bmp", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2017-05-09 12:53:46 -07:00
|
|
|
return SKEncodedImageFormat.Bmp;
|
2019-06-09 14:51:52 -07:00
|
|
|
}
|
2017-05-09 12:53:46 -07:00
|
|
|
|
|
|
|
// default to png
|
|
|
|
return SKEncodedImageFormat.Png;
|
|
|
|
}
|
|
|
|
|
2019-12-13 12:57:23 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Create a square collage.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="paths">The paths of the images to use in the collage.</param>
|
|
|
|
/// <param name="outputPath">The path at which to place the resulting collage image.</param>
|
|
|
|
/// <param name="width">The desired width of the collage.</param>
|
|
|
|
/// <param name="height">The desired height of the collage.</param>
|
2021-05-11 04:55:46 -07:00
|
|
|
public void BuildSquareCollage(IReadOnlyList<string> paths, string outputPath, int width, int height)
|
2017-05-09 12:53:46 -07:00
|
|
|
{
|
2020-07-19 11:12:53 -07:00
|
|
|
using var bitmap = BuildSquareCollageBitmap(paths, width, height);
|
|
|
|
using var outputStream = new SKFileWStream(outputPath);
|
|
|
|
using var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels());
|
|
|
|
pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
|
2017-05-09 12:53:46 -07:00
|
|
|
}
|
|
|
|
|
2019-12-13 12:57:23 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Create a thumb collage.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="paths">The paths of the images to use in the collage.</param>
|
|
|
|
/// <param name="outputPath">The path at which to place the resulting image.</param>
|
|
|
|
/// <param name="width">The desired width of the collage.</param>
|
|
|
|
/// <param name="height">The desired height of the collage.</param>
|
2020-08-21 10:53:55 -07:00
|
|
|
/// <param name="libraryName">The name of the library to draw on the collage.</param>
|
2021-05-11 04:55:46 -07:00
|
|
|
public void BuildThumbCollage(IReadOnlyList<string> paths, string outputPath, int width, int height, string? libraryName)
|
2017-05-09 12:53:46 -07:00
|
|
|
{
|
2020-08-21 10:53:55 -07:00
|
|
|
using var bitmap = BuildThumbCollageBitmap(paths, width, height, libraryName);
|
2020-07-19 11:12:53 -07:00
|
|
|
using var outputStream = new SKFileWStream(outputPath);
|
|
|
|
using var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels());
|
|
|
|
pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
|
2017-05-09 12:53:46 -07:00
|
|
|
}
|
|
|
|
|
2021-05-11 04:55:46 -07:00
|
|
|
private SKBitmap BuildThumbCollageBitmap(IReadOnlyList<string> paths, int width, int height, string? libraryName)
|
2017-05-09 12:53:46 -07:00
|
|
|
{
|
2017-05-09 21:49:11 -07:00
|
|
|
var bitmap = new SKBitmap(width, height);
|
2017-05-09 12:53:46 -07:00
|
|
|
|
2020-07-19 11:12:53 -07:00
|
|
|
using var canvas = new SKCanvas(bitmap);
|
|
|
|
canvas.Clear(SKColors.Black);
|
2017-05-09 12:53:46 -07:00
|
|
|
|
2020-08-21 10:53:55 -07:00
|
|
|
using var backdrop = GetNextValidImage(paths, 0, out _);
|
|
|
|
if (backdrop == null)
|
2020-07-19 11:12:53 -07:00
|
|
|
{
|
2020-08-21 10:53:55 -07:00
|
|
|
return bitmap;
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
2020-08-21 10:53:55 -07:00
|
|
|
// resize to the same aspect as the original
|
|
|
|
var backdropHeight = Math.Abs(width * backdrop.Height / backdrop.Width);
|
|
|
|
using var residedBackdrop = SkiaEncoder.ResizeImage(backdrop, new SKImageInfo(width, backdropHeight, backdrop.ColorType, backdrop.AlphaType, backdrop.ColorSpace));
|
|
|
|
// draw the backdrop
|
|
|
|
canvas.DrawImage(residedBackdrop, 0, 0);
|
|
|
|
|
|
|
|
// draw shadow rectangle
|
|
|
|
var paintColor = new SKPaint
|
|
|
|
{
|
|
|
|
Color = SKColors.Black.WithAlpha(0x78),
|
|
|
|
Style = SKPaintStyle.Fill
|
|
|
|
};
|
|
|
|
canvas.DrawRect(0, 0, width, height, paintColor);
|
|
|
|
|
2020-12-25 10:24:25 -07:00
|
|
|
var typeFace = SKTypeface.FromFamilyName("sans-serif", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright);
|
|
|
|
|
|
|
|
// use the system fallback to find a typeface for the given CJK character
|
|
|
|
var nonCjkPattern = @"[^\p{IsCJKUnifiedIdeographs}\p{IsCJKUnifiedIdeographsExtensionA}\p{IsKatakana}\p{IsHiragana}\p{IsHangulSyllables}\p{IsHangulJamo}]";
|
|
|
|
var filteredName = Regex.Replace(libraryName ?? string.Empty, nonCjkPattern, string.Empty);
|
|
|
|
if (!string.IsNullOrEmpty(filteredName))
|
|
|
|
{
|
|
|
|
typeFace = SKFontManager.Default.MatchCharacter(null, SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright, null, filteredName[0]);
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:53:55 -07:00
|
|
|
// draw library name
|
|
|
|
var textPaint = new SKPaint
|
|
|
|
{
|
|
|
|
Color = SKColors.White,
|
|
|
|
Style = SKPaintStyle.Fill,
|
2020-08-21 11:36:56 -07:00
|
|
|
TextSize = 112,
|
2020-08-21 10:53:55 -07:00
|
|
|
TextAlign = SKTextAlign.Center,
|
2020-12-25 10:24:25 -07:00
|
|
|
Typeface = typeFace,
|
2020-08-21 10:53:55 -07:00
|
|
|
IsAntialias = true
|
|
|
|
};
|
2020-08-22 05:31:28 -07:00
|
|
|
|
|
|
|
// scale down text to 90% of the width if text is larger than 95% of the width
|
|
|
|
var textWidth = textPaint.MeasureText(libraryName);
|
|
|
|
if (textWidth > width * 0.95)
|
|
|
|
{
|
|
|
|
textPaint.TextSize = 0.9f * width * textPaint.TextSize / textWidth;
|
|
|
|
}
|
|
|
|
|
2020-08-21 11:36:56 -07:00
|
|
|
canvas.DrawText(libraryName, width / 2f, (height / 2f) + (textPaint.FontMetrics.XHeight / 2), textPaint);
|
2020-08-21 10:53:55 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2021-05-11 04:55:46 -07:00
|
|
|
private SKBitmap? GetNextValidImage(IReadOnlyList<string> paths, int currentIndex, out int newIndex)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2019-01-13 13:37:13 -07:00
|
|
|
var imagesTested = new Dictionary<int, int>();
|
2020-04-05 12:19:04 -07:00
|
|
|
SKBitmap? bitmap = null;
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2021-05-11 04:55:46 -07:00
|
|
|
while (imagesTested.Count < paths.Count)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2021-05-11 04:55:46 -07:00
|
|
|
if (currentIndex >= paths.Count)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
|
|
|
currentIndex = 0;
|
|
|
|
}
|
|
|
|
|
2020-04-05 12:19:04 -07:00
|
|
|
bitmap = _skiaEncoder.Decode(paths[currentIndex], false, null, out _);
|
2018-09-12 10:26:21 -07:00
|
|
|
|
|
|
|
imagesTested[currentIndex] = 0;
|
2017-05-09 21:49:11 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
currentIndex++;
|
2017-05-09 21:49:11 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (bitmap != null)
|
|
|
|
{
|
|
|
|
break;
|
2017-05-09 12:53:46 -07:00
|
|
|
}
|
2017-05-09 21:49:11 -07:00
|
|
|
}
|
2017-05-09 12:53:46 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
newIndex = currentIndex;
|
2017-05-09 21:49:11 -07:00
|
|
|
return bitmap;
|
2017-05-09 12:53:46 -07:00
|
|
|
}
|
|
|
|
|
2021-05-11 04:55:46 -07:00
|
|
|
private SKBitmap BuildSquareCollageBitmap(IReadOnlyList<string> paths, int width, int height)
|
2017-05-09 12:53:46 -07:00
|
|
|
{
|
|
|
|
var bitmap = new SKBitmap(width, height);
|
|
|
|
var imageIndex = 0;
|
|
|
|
var cellWidth = width / 2;
|
|
|
|
var cellHeight = height / 2;
|
|
|
|
|
2020-07-19 11:12:53 -07:00
|
|
|
using var canvas = new SKCanvas(bitmap);
|
|
|
|
for (var x = 0; x < 2; x++)
|
2017-05-09 12:53:46 -07:00
|
|
|
{
|
2020-07-19 11:12:53 -07:00
|
|
|
for (var y = 0; y < 2; y++)
|
2017-05-09 12:53:46 -07:00
|
|
|
{
|
2020-07-19 11:12:53 -07:00
|
|
|
using var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex);
|
|
|
|
imageIndex = newIndex;
|
|
|
|
|
|
|
|
if (currentBitmap == null)
|
2017-05-09 12:53:46 -07:00
|
|
|
{
|
2020-07-19 11:12:53 -07:00
|
|
|
continue;
|
2017-05-09 12:53:46 -07:00
|
|
|
}
|
2020-07-19 11:12:53 -07:00
|
|
|
|
2020-07-31 12:20:05 -07:00
|
|
|
// Scale image. The FromBitmap creates a copy
|
2020-08-02 03:43:25 -07:00
|
|
|
var imageInfo = new SKImageInfo(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType, currentBitmap.ColorSpace);
|
2020-08-09 13:59:31 -07:00
|
|
|
using var resizedBitmap = SKBitmap.FromImage(SkiaEncoder.ResizeImage(currentBitmap, imageInfo));
|
2020-07-19 11:12:53 -07:00
|
|
|
|
|
|
|
// draw this image into the strip at the next position
|
|
|
|
var xPos = x * cellWidth;
|
|
|
|
var yPos = y * cellHeight;
|
|
|
|
canvas.DrawBitmap(resizedBitmap, xPos, yPos);
|
2017-05-09 12:53:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 14:34:28 -07:00
|
|
|
}
|