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 SkiaSharp;
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2019-01-26 19:43:13 +00:00
|
|
|
namespace Jellyfin.Drawing.Skia
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2019-12-13 19:57:23 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Used to build collages of multiple images arranged in vertical strips.
|
|
|
|
/// </summary>
|
2017-05-09 19:53:46 +00:00
|
|
|
public class StripCollageBuilder
|
|
|
|
{
|
2019-06-09 21:51:52 +00:00
|
|
|
private readonly SkiaEncoder _skiaEncoder;
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2019-12-13 19:57:23 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="StripCollageBuilder"/> class.
|
|
|
|
/// </summary>
|
2019-12-14 10:04:22 +00:00
|
|
|
/// <param name="skiaEncoder">The encoder to use for building collages.</param>
|
2019-06-09 21:51:52 +00:00
|
|
|
public StripCollageBuilder(SkiaEncoder skiaEncoder)
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2019-06-09 21:51:52 +00:00
|
|
|
_skiaEncoder = skiaEncoder;
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 19:57:23 +00: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-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));
|
|
|
|
}
|
|
|
|
|
2019-06-09 21:51:52 +00:00
|
|
|
var ext = Path.GetExtension(outputPath);
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2019-06-09 21:51:52 +00:00
|
|
|
if (string.Equals(ext, ".jpg", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| string.Equals(ext, ".jpeg", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2017-05-09 19:53:46 +00:00
|
|
|
return SKEncodedImageFormat.Jpeg;
|
2019-06-09 21:51:52 +00:00
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2019-06-09 21:51:52 +00:00
|
|
|
if (string.Equals(ext, ".webp", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2017-05-09 19:53:46 +00:00
|
|
|
return SKEncodedImageFormat.Webp;
|
2019-06-09 21:51:52 +00:00
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2019-06-09 21:51:52 +00:00
|
|
|
if (string.Equals(ext, ".gif", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2017-05-09 19:53:46 +00:00
|
|
|
return SKEncodedImageFormat.Gif;
|
2019-06-09 21:51:52 +00:00
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2019-06-09 21:51:52 +00:00
|
|
|
if (string.Equals(ext, ".bmp", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2017-05-09 19:53:46 +00:00
|
|
|
return SKEncodedImageFormat.Bmp;
|
2019-06-09 21:51:52 +00:00
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
|
|
|
|
// default to png
|
|
|
|
return SKEncodedImageFormat.Png;
|
|
|
|
}
|
|
|
|
|
2019-12-13 19:57:23 +00: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>
|
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))
|
2019-01-26 19:43:13 +00:00
|
|
|
using (var outputStream = new SKFileWStream(outputPath))
|
2019-06-09 21:51:52 +00:00
|
|
|
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels()))
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2019-06-09 21:51:52 +00:00
|
|
|
pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 19:57:23 +00: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>
|
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))
|
2019-06-09 21:51:52 +00:00
|
|
|
using (var outputStream = new SKFileWStream(outputPath))
|
|
|
|
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels()))
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2019-06-09 21:51:52 +00:00
|
|
|
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
|
|
|
|
2019-03-09 02:17:02 +00:00
|
|
|
// number of images used in the thumbnail
|
|
|
|
var iCount = 3;
|
|
|
|
|
2017-05-12 04:57:09 +00:00
|
|
|
// determine sizes for each image that will composited into the final image
|
2019-03-09 02:17:02 +00:00
|
|
|
var iSlice = Convert.ToInt32(width / iCount);
|
2019-03-08 09:39:41 +00:00
|
|
|
int iHeight = Convert.ToInt32(height * 1.00);
|
2017-05-10 04:49:11 +00:00
|
|
|
int imageIndex = 0;
|
2019-03-09 02:17:02 +00:00
|
|
|
for (int i = 0; i < iCount; i++)
|
2017-05-09 19:53:46 +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
|
2020-04-05 19:19:04 +00:00
|
|
|
int iWidth = Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height);
|
2017-05-10 04:49:11 +00:00
|
|
|
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);
|
2019-12-14 10:04:22 +00:00
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
// crop image
|
2020-04-05 19:19:04 +00:00
|
|
|
int ix = 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
|
2019-03-08 09:39:41 +00:00
|
|
|
canvas.DrawImage(subset ?? image, iSlice * i, 0);
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2020-04-05 19:19:04 +00:00
|
|
|
private SKBitmap? GetNextValidImage(string[] paths, int currentIndex, out int newIndex)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2019-01-13 20:37:13 +00:00
|
|
|
var imagesTested = new Dictionary<int, int>();
|
2020-04-05 19:19:04 +00:00
|
|
|
SKBitmap? bitmap = null;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
while (imagesTested.Count < paths.Length)
|
|
|
|
{
|
|
|
|
if (currentIndex >= paths.Length)
|
|
|
|
{
|
|
|
|
currentIndex = 0;
|
|
|
|
}
|
|
|
|
|
2020-04-05 19:19:04 +00:00
|
|
|
bitmap = _skiaEncoder.Decode(paths[currentIndex], false, null, out _);
|
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++)
|
|
|
|
{
|
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
|
|
|
}
|