2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2019-02-06 20:31:41 +00:00
|
|
|
using System.Collections.Generic;
|
2019-01-13 19:16:43 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2017-05-09 19:53:46 +00:00
|
|
|
using MediaBrowser.Controller.Drawing;
|
2019-01-13 19:16:43 +00:00
|
|
|
using MediaBrowser.Controller.Extensions;
|
2017-05-09 19:53:46 +00:00
|
|
|
using MediaBrowser.Model.Drawing;
|
2019-01-13 19:16:43 +00:00
|
|
|
using MediaBrowser.Model.Globalization;
|
2018-12-13 13:18:25 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2017-05-09 19:53:46 +00:00
|
|
|
using SkiaSharp;
|
2019-09-15 04:27:42 +00:00
|
|
|
using static Jellyfin.Drawing.Skia.SkiaHelper;
|
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>
|
|
|
|
/// Image encoder that uses <see cref="SkiaSharp"/> to manipulate images.
|
|
|
|
/// </summary>
|
2017-05-09 19:53:46 +00:00
|
|
|
public class SkiaEncoder : IImageEncoder
|
|
|
|
{
|
|
|
|
private readonly ILogger _logger;
|
2019-06-09 21:51:52 +00:00
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
private readonly ILocalizationManager _localizationManager;
|
|
|
|
|
|
|
|
private static readonly HashSet<string> _transparentImageTypes
|
|
|
|
= new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".png", ".gif", ".webp" };
|
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="SkiaEncoder"/> class.
|
|
|
|
/// </summary>
|
2019-12-14 10:04:22 +00:00
|
|
|
/// <param name="logger">The application logger.</param>
|
|
|
|
/// <param name="appPaths">The application paths.</param>
|
|
|
|
/// <param name="localizationManager">The application localization manager.</param>
|
2019-01-17 22:55:05 +00:00
|
|
|
public SkiaEncoder(
|
2019-06-09 21:51:52 +00:00
|
|
|
ILogger<SkiaEncoder> logger,
|
2019-01-17 22:55:05 +00:00
|
|
|
IApplicationPaths appPaths,
|
|
|
|
ILocalizationManager localizationManager)
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2019-06-09 21:51:52 +00:00
|
|
|
_logger = logger;
|
2017-05-09 19:53:46 +00:00
|
|
|
_appPaths = appPaths;
|
2017-11-01 19:45:10 +00:00
|
|
|
_localizationManager = localizationManager;
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 19:57:23 +00:00
|
|
|
/// <inheritdoc/>
|
2019-06-09 21:51:52 +00:00
|
|
|
public string Name => "Skia";
|
|
|
|
|
2019-12-13 19:57:23 +00:00
|
|
|
/// <inheritdoc/>
|
2019-06-09 21:51:52 +00:00
|
|
|
public bool SupportsImageCollageCreation => true;
|
|
|
|
|
2019-12-13 19:57:23 +00:00
|
|
|
/// <inheritdoc/>
|
2019-06-09 21:51:52 +00:00
|
|
|
public bool SupportsImageEncoding => true;
|
|
|
|
|
2019-12-13 19:57:23 +00:00
|
|
|
/// <inheritdoc/>
|
2019-02-06 20:31:41 +00:00
|
|
|
public IReadOnlyCollection<string> SupportedInputFormats =>
|
|
|
|
new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2019-01-13 20:31:14 +00:00
|
|
|
"jpeg",
|
|
|
|
"jpg",
|
|
|
|
"png",
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
"dng",
|
|
|
|
|
|
|
|
"webp",
|
|
|
|
"gif",
|
|
|
|
"bmp",
|
|
|
|
"ico",
|
|
|
|
"astc",
|
|
|
|
"ktx",
|
|
|
|
"pkm",
|
|
|
|
"wbmp",
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
// Are all of these supported? https://github.com/google/skia/blob/master/infra/bots/recipes/test.py#L454
|
|
|
|
|
|
|
|
// working on windows at least
|
|
|
|
"cr2",
|
|
|
|
"nef",
|
|
|
|
"arw"
|
|
|
|
};
|
|
|
|
|
2019-12-13 19:57:23 +00:00
|
|
|
/// <inheritdoc/>
|
2019-02-06 20:31:41 +00:00
|
|
|
public IReadOnlyCollection<ImageFormat> SupportedOutputFormats
|
|
|
|
=> new HashSet<ImageFormat>() { ImageFormat.Webp, ImageFormat.Jpg, ImageFormat.Png };
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2019-06-09 21:51:52 +00:00
|
|
|
/// <summary>
|
2019-12-14 10:04:22 +00:00
|
|
|
/// Test to determine if the native lib is available.
|
2019-06-09 21:51:52 +00:00
|
|
|
/// </summary>
|
|
|
|
public static void TestSkia()
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2017-06-24 18:29:23 +00:00
|
|
|
// test an operation that requires the native library
|
|
|
|
SKPMColor.PreMultiply(SKColors.Black);
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 03:40:36 +00:00
|
|
|
private static bool IsTransparent(SKColor color)
|
2019-01-26 20:10:19 +00:00
|
|
|
=> (color.Red == 255 && color.Green == 255 && color.Blue == 255) || color.Alpha == 0;
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2019-12-13 19:57:23 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Convert a <see cref="ImageFormat"/> to a <see cref="SKEncodedImageFormat"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="selectedFormat">The format to convert.</param>
|
|
|
|
/// <returns>The converted format.</returns>
|
2017-05-09 19:53:46 +00:00
|
|
|
public static SKEncodedImageFormat GetImageFormat(ImageFormat selectedFormat)
|
|
|
|
{
|
|
|
|
switch (selectedFormat)
|
|
|
|
{
|
|
|
|
case ImageFormat.Bmp:
|
|
|
|
return SKEncodedImageFormat.Bmp;
|
|
|
|
case ImageFormat.Jpg:
|
|
|
|
return SKEncodedImageFormat.Jpeg;
|
|
|
|
case ImageFormat.Gif:
|
|
|
|
return SKEncodedImageFormat.Gif;
|
|
|
|
case ImageFormat.Webp:
|
|
|
|
return SKEncodedImageFormat.Webp;
|
|
|
|
default:
|
|
|
|
return SKEncodedImageFormat.Png;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 03:40:36 +00:00
|
|
|
private static bool IsTransparentRow(SKBitmap bmp, int row)
|
2017-05-10 04:49:11 +00:00
|
|
|
{
|
|
|
|
for (var i = 0; i < bmp.Width; ++i)
|
|
|
|
{
|
2017-08-16 03:40:36 +00:00
|
|
|
if (!IsTransparent(bmp.GetPixel(i, row)))
|
2017-05-10 04:49:11 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-06-09 21:51:52 +00:00
|
|
|
|
2017-05-10 04:49:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-16 03:40:36 +00:00
|
|
|
private static bool IsTransparentColumn(SKBitmap bmp, int col)
|
2017-05-10 04:49:11 +00:00
|
|
|
{
|
|
|
|
for (var i = 0; i < bmp.Height; ++i)
|
|
|
|
{
|
2017-08-16 03:40:36 +00:00
|
|
|
if (!IsTransparent(bmp.GetPixel(col, i)))
|
2017-05-10 04:49:11 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-06-09 21:51:52 +00:00
|
|
|
|
2017-05-10 04:49:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private SKBitmap CropWhiteSpace(SKBitmap bitmap)
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2017-05-10 04:49:11 +00:00
|
|
|
var topmost = 0;
|
|
|
|
for (int row = 0; row < bitmap.Height; ++row)
|
|
|
|
{
|
2017-08-16 03:40:36 +00:00
|
|
|
if (IsTransparentRow(bitmap, row))
|
2019-01-26 20:10:19 +00:00
|
|
|
{
|
2017-06-04 18:31:40 +00:00
|
|
|
topmost = row + 1;
|
2019-01-26 20:10:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-05-10 04:49:11 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 18:31:40 +00:00
|
|
|
int bottommost = bitmap.Height;
|
2017-05-10 04:49:11 +00:00
|
|
|
for (int row = bitmap.Height - 1; row >= 0; --row)
|
|
|
|
{
|
2017-08-16 03:40:36 +00:00
|
|
|
if (IsTransparentRow(bitmap, row))
|
2019-01-26 20:10:19 +00:00
|
|
|
{
|
2017-05-10 04:49:11 +00:00
|
|
|
bottommost = row;
|
2019-01-26 20:10:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-05-10 04:49:11 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 18:31:40 +00:00
|
|
|
int leftmost = 0, rightmost = bitmap.Width;
|
2017-05-10 04:49:11 +00:00
|
|
|
for (int col = 0; col < bitmap.Width; ++col)
|
|
|
|
{
|
2017-08-16 03:40:36 +00:00
|
|
|
if (IsTransparentColumn(bitmap, col))
|
2019-01-26 20:10:19 +00:00
|
|
|
{
|
2017-06-04 18:31:40 +00:00
|
|
|
leftmost = col + 1;
|
2019-01-26 20:10:19 +00:00
|
|
|
}
|
2017-05-10 04:49:11 +00:00
|
|
|
else
|
2019-01-26 20:10:19 +00:00
|
|
|
{
|
2017-05-10 04:49:11 +00:00
|
|
|
break;
|
2019-01-26 20:10:19 +00:00
|
|
|
}
|
2017-05-10 04:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int col = bitmap.Width - 1; col >= 0; --col)
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2017-08-16 03:40:36 +00:00
|
|
|
if (IsTransparentColumn(bitmap, col))
|
2019-01-26 20:10:19 +00:00
|
|
|
{
|
2017-05-10 04:49:11 +00:00
|
|
|
rightmost = col;
|
2019-01-26 20:10:19 +00:00
|
|
|
}
|
2017-05-10 04:49:11 +00:00
|
|
|
else
|
2019-01-26 20:10:19 +00:00
|
|
|
{
|
2017-05-10 04:49:11 +00:00
|
|
|
break;
|
2019-01-26 20:10:19 +00:00
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
2017-05-09 20:18:02 +00:00
|
|
|
|
2017-05-10 04:49:11 +00:00
|
|
|
var newRect = SKRectI.Create(leftmost, topmost, rightmost - leftmost, bottommost - topmost);
|
|
|
|
|
|
|
|
using (var image = SKImage.FromBitmap(bitmap))
|
2018-12-13 21:34:28 +00:00
|
|
|
using (var subset = image.Subset(newRect))
|
2017-05-10 04:49:11 +00:00
|
|
|
{
|
2018-12-13 21:34:28 +00:00
|
|
|
return SKBitmap.FromImage(subset);
|
2017-05-10 04:49:11 +00:00
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
2019-09-15 04:27:42 +00:00
|
|
|
/// <inheritdoc />
|
2019-12-14 14:47:35 +00:00
|
|
|
/// <exception cref="ArgumentNullException">The path is null.</exception>
|
|
|
|
/// <exception cref="FileNotFoundException">The path is not valid.</exception>
|
|
|
|
/// <exception cref="SkiaCodecException">The file at the specified path could not be used to generate a codec.</exception>
|
2019-01-26 12:16:47 +00:00
|
|
|
public ImageDimensions GetImageSize(string path)
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2019-09-15 04:27:42 +00:00
|
|
|
if (path == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(path));
|
|
|
|
}
|
|
|
|
|
2019-06-23 14:13:50 +00:00
|
|
|
if (!File.Exists(path))
|
|
|
|
{
|
|
|
|
throw new FileNotFoundException("File not found", path);
|
|
|
|
}
|
|
|
|
|
2019-09-15 04:27:42 +00:00
|
|
|
using (var codec = SKCodec.Create(path, out SKCodecResult result))
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2019-09-15 04:27:42 +00:00
|
|
|
EnsureSuccess(result);
|
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
var info = codec.Info;
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2019-01-26 12:16:47 +00:00
|
|
|
return new ImageDimensions(info.Width, info.Height);
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 19:45:10 +00:00
|
|
|
private static bool HasDiacritics(string text)
|
2019-01-26 20:10:19 +00:00
|
|
|
=> !string.Equals(text, text.RemoveDiacritics(), StringComparison.Ordinal);
|
2017-11-01 19:45:10 +00:00
|
|
|
|
2019-06-09 21:51:52 +00:00
|
|
|
private bool RequiresSpecialCharacterHack(string path)
|
2017-11-01 19:45:10 +00:00
|
|
|
{
|
|
|
|
if (_localizationManager.HasUnicodeCategory(path, UnicodeCategory.OtherLetter))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasDiacritics(path))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-09 21:51:52 +00:00
|
|
|
private string NormalizePath(string path)
|
2017-11-01 19:45:10 +00:00
|
|
|
{
|
|
|
|
if (!RequiresSpecialCharacterHack(path))
|
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tempPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + Path.GetExtension(path) ?? string.Empty);
|
|
|
|
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
|
2019-01-26 21:31:59 +00:00
|
|
|
File.Copy(path, tempPath, true);
|
2017-11-01 19:45:10 +00:00
|
|
|
|
|
|
|
return tempPath;
|
|
|
|
}
|
|
|
|
|
2018-12-14 16:46:43 +00:00
|
|
|
private static SKEncodedOrigin GetSKEncodedOrigin(ImageOrientation? orientation)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
if (!orientation.HasValue)
|
|
|
|
{
|
2018-12-13 21:34:28 +00:00
|
|
|
return SKEncodedOrigin.TopLeft;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (orientation.Value)
|
|
|
|
{
|
|
|
|
case ImageOrientation.TopRight:
|
2018-12-13 21:34:28 +00:00
|
|
|
return SKEncodedOrigin.TopRight;
|
2018-09-12 17:26:21 +00:00
|
|
|
case ImageOrientation.RightTop:
|
2018-12-13 21:34:28 +00:00
|
|
|
return SKEncodedOrigin.RightTop;
|
2018-09-12 17:26:21 +00:00
|
|
|
case ImageOrientation.RightBottom:
|
2018-12-13 21:34:28 +00:00
|
|
|
return SKEncodedOrigin.RightBottom;
|
2018-09-12 17:26:21 +00:00
|
|
|
case ImageOrientation.LeftTop:
|
2018-12-13 21:34:28 +00:00
|
|
|
return SKEncodedOrigin.LeftTop;
|
2018-09-12 17:26:21 +00:00
|
|
|
case ImageOrientation.LeftBottom:
|
2018-12-13 21:34:28 +00:00
|
|
|
return SKEncodedOrigin.LeftBottom;
|
2018-09-12 17:26:21 +00:00
|
|
|
case ImageOrientation.BottomRight:
|
2018-12-13 21:34:28 +00:00
|
|
|
return SKEncodedOrigin.BottomRight;
|
2018-09-12 17:26:21 +00:00
|
|
|
case ImageOrientation.BottomLeft:
|
2018-12-13 21:34:28 +00:00
|
|
|
return SKEncodedOrigin.BottomLeft;
|
2018-09-12 17:26:21 +00:00
|
|
|
default:
|
2018-12-13 21:34:28 +00:00
|
|
|
return SKEncodedOrigin.TopLeft;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 10:04:22 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Decode an image.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The filepath of the image to decode.</param>
|
|
|
|
/// <param name="forceCleanBitmap">Whether to force clean the bitmap.</param>
|
|
|
|
/// <param name="orientation">The orientation of the image.</param>
|
|
|
|
/// <param name="origin">The detected origin of the image.</param>
|
|
|
|
/// <returns>The resulting bitmap of the image.</returns>
|
2019-06-09 21:51:52 +00:00
|
|
|
internal SKBitmap Decode(string path, bool forceCleanBitmap, ImageOrientation? orientation, out SKEncodedOrigin origin)
|
2017-05-10 19:56:59 +00:00
|
|
|
{
|
2019-01-26 21:59:53 +00:00
|
|
|
if (!File.Exists(path))
|
2017-11-01 19:45:10 +00:00
|
|
|
{
|
|
|
|
throw new FileNotFoundException("File not found", path);
|
|
|
|
}
|
|
|
|
|
2019-06-09 21:51:52 +00:00
|
|
|
var requiresTransparencyHack = _transparentImageTypes.Contains(Path.GetExtension(path));
|
2017-05-11 03:23:16 +00:00
|
|
|
|
2017-05-17 18:18:18 +00:00
|
|
|
if (requiresTransparencyHack || forceCleanBitmap)
|
2017-05-10 19:56:59 +00:00
|
|
|
{
|
2019-06-09 21:51:52 +00:00
|
|
|
using (var stream = new SKFileStream(NormalizePath(path)))
|
2018-12-13 21:34:28 +00:00
|
|
|
using (var codec = SKCodec.Create(stream))
|
2017-05-11 03:23:16 +00:00
|
|
|
{
|
2018-12-13 21:34:28 +00:00
|
|
|
if (codec == null)
|
2017-08-31 03:49:38 +00:00
|
|
|
{
|
2018-12-14 16:46:43 +00:00
|
|
|
origin = GetSKEncodedOrigin(orientation);
|
2018-12-13 21:34:28 +00:00
|
|
|
return null;
|
|
|
|
}
|
2017-08-31 03:49:38 +00:00
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
// create the bitmap
|
|
|
|
var bitmap = new SKBitmap(codec.Info.Width, codec.Info.Height, !requiresTransparencyHack);
|
2017-05-10 19:56:59 +00:00
|
|
|
|
2019-01-21 19:18:52 +00:00
|
|
|
// decode
|
2019-02-13 15:44:58 +00:00
|
|
|
_ = codec.GetPixels(bitmap.Info, bitmap.GetPixels());
|
2017-06-09 19:24:31 +00:00
|
|
|
|
2019-01-21 19:18:52 +00:00
|
|
|
origin = codec.EncodedOrigin;
|
2018-12-13 21:34:28 +00:00
|
|
|
|
|
|
|
return bitmap;
|
2017-05-11 03:23:16 +00:00
|
|
|
}
|
2017-05-10 19:56:59 +00:00
|
|
|
}
|
2017-05-11 03:23:16 +00:00
|
|
|
|
2019-06-09 21:51:52 +00:00
|
|
|
var resultBitmap = SKBitmap.Decode(NormalizePath(path));
|
2017-05-17 18:18:18 +00:00
|
|
|
|
2017-05-18 21:05:47 +00:00
|
|
|
if (resultBitmap == null)
|
|
|
|
{
|
2019-06-09 21:51:52 +00:00
|
|
|
return Decode(path, true, orientation, out origin);
|
2017-05-18 21:05:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 18:18:18 +00:00
|
|
|
// If we have to resize these they often end up distorted
|
|
|
|
if (resultBitmap.ColorType == SKColorType.Gray8)
|
|
|
|
{
|
|
|
|
using (resultBitmap)
|
|
|
|
{
|
2019-06-09 21:51:52 +00:00
|
|
|
return Decode(path, true, orientation, out origin);
|
2017-05-17 18:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
origin = SKEncodedOrigin.TopLeft;
|
2017-05-17 18:18:18 +00:00
|
|
|
return resultBitmap;
|
2017-05-10 19:56:59 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
private SKBitmap GetBitmap(string path, bool cropWhitespace, bool forceAnalyzeBitmap, ImageOrientation? orientation, out SKEncodedOrigin origin)
|
2017-05-10 04:49:11 +00:00
|
|
|
{
|
|
|
|
if (cropWhitespace)
|
|
|
|
{
|
2019-06-09 21:51:52 +00:00
|
|
|
using (var bitmap = Decode(path, forceAnalyzeBitmap, orientation, out origin))
|
2017-05-10 04:49:11 +00:00
|
|
|
{
|
|
|
|
return CropWhiteSpace(bitmap);
|
|
|
|
}
|
2017-05-11 03:23:16 +00:00
|
|
|
}
|
2017-05-10 04:49:11 +00:00
|
|
|
|
2019-06-09 21:51:52 +00:00
|
|
|
return Decode(path, forceAnalyzeBitmap, orientation, out origin);
|
2017-06-09 19:24:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private SKBitmap GetBitmap(string path, bool cropWhitespace, bool autoOrient, ImageOrientation? orientation)
|
2017-06-09 19:24:31 +00:00
|
|
|
{
|
2018-12-13 21:34:28 +00:00
|
|
|
SKEncodedOrigin origin;
|
2017-06-09 19:24:31 +00:00
|
|
|
|
|
|
|
if (autoOrient)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var bitmap = GetBitmap(path, cropWhitespace, true, orientation, out origin);
|
2017-06-09 19:24:31 +00:00
|
|
|
|
2019-01-26 20:10:19 +00:00
|
|
|
if (bitmap != null && origin != SKEncodedOrigin.TopLeft)
|
2017-06-09 19:24:31 +00:00
|
|
|
{
|
2019-01-26 20:10:19 +00:00
|
|
|
using (bitmap)
|
2017-06-09 19:24:31 +00:00
|
|
|
{
|
2019-01-26 20:10:19 +00:00
|
|
|
return OrientImage(bitmap, origin);
|
2017-06-09 19:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
return GetBitmap(path, cropWhitespace, false, orientation, out origin);
|
2017-06-09 19:24:31 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
private SKBitmap OrientImage(SKBitmap bitmap, SKEncodedOrigin origin)
|
2017-09-02 05:33:04 +00:00
|
|
|
{
|
|
|
|
switch (origin)
|
|
|
|
{
|
2018-12-13 21:34:28 +00:00
|
|
|
case SKEncodedOrigin.TopRight:
|
2017-09-02 05:33:04 +00:00
|
|
|
{
|
|
|
|
var rotated = new SKBitmap(bitmap.Width, bitmap.Height);
|
|
|
|
using (var surface = new SKCanvas(rotated))
|
|
|
|
{
|
|
|
|
surface.Translate(rotated.Width, 0);
|
|
|
|
surface.Scale(-1, 1);
|
|
|
|
surface.DrawBitmap(bitmap, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rotated;
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
case SKEncodedOrigin.BottomRight:
|
2017-09-02 05:33:04 +00:00
|
|
|
{
|
|
|
|
var rotated = new SKBitmap(bitmap.Width, bitmap.Height);
|
|
|
|
using (var surface = new SKCanvas(rotated))
|
|
|
|
{
|
2019-01-26 20:10:19 +00:00
|
|
|
float px = (float)bitmap.Width / 2;
|
|
|
|
float py = (float)bitmap.Height / 2;
|
2017-09-02 05:33:04 +00:00
|
|
|
|
|
|
|
surface.RotateDegrees(180, px, py);
|
|
|
|
surface.DrawBitmap(bitmap, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rotated;
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
case SKEncodedOrigin.BottomLeft:
|
2017-09-02 05:33:04 +00:00
|
|
|
{
|
|
|
|
var rotated = new SKBitmap(bitmap.Width, bitmap.Height);
|
|
|
|
using (var surface = new SKCanvas(rotated))
|
|
|
|
{
|
2019-01-26 20:10:19 +00:00
|
|
|
float px = (float)bitmap.Width / 2;
|
2017-09-02 05:33:04 +00:00
|
|
|
|
2019-01-26 20:10:19 +00:00
|
|
|
float py = (float)bitmap.Height / 2;
|
2017-09-02 05:33:04 +00:00
|
|
|
|
|
|
|
surface.Translate(rotated.Width, 0);
|
|
|
|
surface.Scale(-1, 1);
|
|
|
|
|
|
|
|
surface.RotateDegrees(180, px, py);
|
|
|
|
surface.DrawBitmap(bitmap, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rotated;
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
case SKEncodedOrigin.LeftTop:
|
2017-09-02 05:33:04 +00:00
|
|
|
{
|
2017-09-02 05:44:47 +00:00
|
|
|
// TODO: Remove dual canvases, had trouble with flipping
|
|
|
|
using (var rotated = new SKBitmap(bitmap.Height, bitmap.Width))
|
2017-09-02 05:33:04 +00:00
|
|
|
{
|
2017-09-02 05:44:47 +00:00
|
|
|
using (var surface = new SKCanvas(rotated))
|
|
|
|
{
|
|
|
|
surface.Translate(rotated.Width, 0);
|
2017-09-02 05:33:04 +00:00
|
|
|
|
2017-09-02 05:44:47 +00:00
|
|
|
surface.RotateDegrees(90);
|
|
|
|
|
|
|
|
surface.DrawBitmap(bitmap, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
var flippedBitmap = new SKBitmap(rotated.Width, rotated.Height);
|
|
|
|
using (var flippedCanvas = new SKCanvas(flippedBitmap))
|
|
|
|
{
|
|
|
|
flippedCanvas.Translate(flippedBitmap.Width, 0);
|
|
|
|
flippedCanvas.Scale(-1, 1);
|
|
|
|
flippedCanvas.DrawBitmap(rotated, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return flippedBitmap;
|
|
|
|
}
|
2017-09-02 05:33:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
case SKEncodedOrigin.RightTop:
|
2017-09-02 05:33:04 +00:00
|
|
|
{
|
|
|
|
var rotated = new SKBitmap(bitmap.Height, bitmap.Width);
|
|
|
|
using (var surface = new SKCanvas(rotated))
|
|
|
|
{
|
|
|
|
surface.Translate(rotated.Width, 0);
|
|
|
|
surface.RotateDegrees(90);
|
|
|
|
surface.DrawBitmap(bitmap, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rotated;
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
case SKEncodedOrigin.RightBottom:
|
2017-09-02 05:33:04 +00:00
|
|
|
{
|
2017-09-02 05:44:47 +00:00
|
|
|
// TODO: Remove dual canvases, had trouble with flipping
|
|
|
|
using (var rotated = new SKBitmap(bitmap.Height, bitmap.Width))
|
2017-09-02 05:33:04 +00:00
|
|
|
{
|
2017-09-02 05:44:47 +00:00
|
|
|
using (var surface = new SKCanvas(rotated))
|
|
|
|
{
|
|
|
|
surface.Translate(0, rotated.Height);
|
|
|
|
surface.RotateDegrees(270);
|
|
|
|
surface.DrawBitmap(bitmap, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
var flippedBitmap = new SKBitmap(rotated.Width, rotated.Height);
|
|
|
|
using (var flippedCanvas = new SKCanvas(flippedBitmap))
|
|
|
|
{
|
|
|
|
flippedCanvas.Translate(flippedBitmap.Width, 0);
|
|
|
|
flippedCanvas.Scale(-1, 1);
|
|
|
|
flippedCanvas.DrawBitmap(rotated, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return flippedBitmap;
|
2017-09-02 05:33:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
case SKEncodedOrigin.LeftBottom:
|
2017-09-02 05:33:04 +00:00
|
|
|
{
|
|
|
|
var rotated = new SKBitmap(bitmap.Height, bitmap.Width);
|
|
|
|
using (var surface = new SKCanvas(rotated))
|
|
|
|
{
|
|
|
|
surface.Translate(0, rotated.Height);
|
|
|
|
surface.RotateDegrees(270);
|
|
|
|
surface.DrawBitmap(bitmap, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rotated;
|
|
|
|
}
|
|
|
|
|
2019-01-26 20:10:19 +00:00
|
|
|
default: return bitmap;
|
2017-06-09 19:24:31 +00:00
|
|
|
}
|
2017-05-10 04:49:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 19:57:23 +00:00
|
|
|
/// <inheritdoc/>
|
2017-06-09 19:24:31 +00:00
|
|
|
public string EncodeImage(string inputPath, DateTime dateModified, string outputPath, bool autoOrient, ImageOrientation? orientation, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat)
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2017-05-09 20:18:02 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(inputPath))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(inputPath));
|
2017-05-09 20:18:02 +00:00
|
|
|
}
|
2019-01-26 20:10:19 +00:00
|
|
|
|
2017-05-09 20:18:02 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(inputPath))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(outputPath));
|
2017-05-09 20:18:02 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 04:49:11 +00:00
|
|
|
var skiaOutputFormat = GetImageFormat(selectedOutputFormat);
|
|
|
|
|
|
|
|
var hasBackgroundColor = !string.IsNullOrWhiteSpace(options.BackgroundColor);
|
|
|
|
var hasForegroundColor = !string.IsNullOrWhiteSpace(options.ForegroundLayer);
|
|
|
|
var blur = options.Blur ?? 0;
|
2017-05-11 03:23:16 +00:00
|
|
|
var hasIndicator = options.AddPlayedIndicator || options.UnplayedCount.HasValue || !options.PercentPlayed.Equals(0);
|
2017-05-10 04:49:11 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
using (var bitmap = GetBitmap(inputPath, options.CropWhiteSpace, autoOrient, orientation))
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2017-05-17 18:18:18 +00:00
|
|
|
if (bitmap == null)
|
|
|
|
{
|
2019-12-13 20:17:05 +00:00
|
|
|
throw new ArgumentOutOfRangeException($"Skia unable to read image {inputPath}");
|
2017-05-17 18:18:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 12:16:47 +00:00
|
|
|
var originalImageSize = new ImageDimensions(bitmap.Width, bitmap.Height);
|
2017-05-17 18:18:18 +00:00
|
|
|
|
2019-01-26 20:10:19 +00:00
|
|
|
if (!options.CropWhiteSpace
|
|
|
|
&& options.HasDefaultOptions(inputPath, originalImageSize)
|
|
|
|
&& !autoOrient)
|
2017-05-15 02:27:58 +00:00
|
|
|
{
|
2017-05-17 18:18:18 +00:00
|
|
|
// Just spit out the original file if all the options are default
|
|
|
|
return inputPath;
|
2017-05-15 02:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var newImageSize = ImageHelper.GetNewImageSize(options, originalImageSize);
|
|
|
|
|
2019-01-26 12:16:47 +00:00
|
|
|
var width = newImageSize.Width;
|
|
|
|
var height = newImageSize.Height;
|
2017-05-15 02:27:58 +00:00
|
|
|
|
2019-01-26 20:10:19 +00:00
|
|
|
using (var resizedBitmap = new SKBitmap(width, height, bitmap.ColorType, bitmap.AlphaType))
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
|
|
|
// scale image
|
2019-01-03 17:59:12 +00:00
|
|
|
bitmap.ScalePixels(resizedBitmap, SKFilterQuality.High);
|
2017-05-10 04:49:11 +00:00
|
|
|
|
|
|
|
// If all we're doing is resizing then we can stop now
|
|
|
|
if (!hasBackgroundColor && !hasForegroundColor && blur == 0 && !hasIndicator)
|
|
|
|
{
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
|
2017-05-10 04:49:11 +00:00
|
|
|
using (var outputStream = new SKFileWStream(outputPath))
|
2019-01-26 20:10:19 +00:00
|
|
|
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), resizedBitmap.GetPixels()))
|
2017-05-10 04:49:11 +00:00
|
|
|
{
|
2019-01-26 20:10:19 +00:00
|
|
|
pixmap.Encode(outputStream, skiaOutputFormat, quality);
|
|
|
|
return outputPath;
|
2017-05-10 04:49:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
// create bitmap to use for canvas drawing used to draw into bitmap
|
2019-12-14 10:04:22 +00:00
|
|
|
using (var saveBitmap = new SKBitmap(width, height)) // , bitmap.ColorType, bitmap.AlphaType))
|
2018-12-13 21:34:28 +00:00
|
|
|
using (var canvas = new SKCanvas(saveBitmap))
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2018-12-13 21:34:28 +00:00
|
|
|
// set background color if present
|
|
|
|
if (hasBackgroundColor)
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2018-12-13 21:34:28 +00:00
|
|
|
canvas.Clear(SKColor.Parse(options.BackgroundColor));
|
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
// Add blur if option is present
|
|
|
|
if (blur > 0)
|
|
|
|
{
|
|
|
|
// create image from resized bitmap to apply blur
|
|
|
|
using (var paint = new SKPaint())
|
|
|
|
using (var filter = SKImageFilter.CreateBlur(blur, blur))
|
2017-05-09 19:53:46 +00:00
|
|
|
{
|
2018-12-13 21:34:28 +00:00
|
|
|
paint.ImageFilter = filter;
|
|
|
|
canvas.DrawBitmap(resizedBitmap, SKRect.Create(width, height), paint);
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
2018-12-13 21:34:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// draw resized bitmap onto canvas
|
|
|
|
canvas.DrawBitmap(resizedBitmap, SKRect.Create(width, height));
|
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
// If foreground layer present then draw
|
|
|
|
if (hasForegroundColor)
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
if (!double.TryParse(options.ForegroundLayer, out double opacity))
|
2018-12-14 16:46:43 +00:00
|
|
|
{
|
|
|
|
opacity = .4;
|
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
canvas.DrawColor(new SKColor(0, 0, 0, (byte)((1 - opacity) * 0xFF)), SKBlendMode.SrcOver);
|
2018-12-13 21:34:28 +00:00
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2018-12-13 21:34:28 +00:00
|
|
|
if (hasIndicator)
|
|
|
|
{
|
|
|
|
DrawIndicator(canvas, width, height, options);
|
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
|
2018-12-13 21:34:28 +00:00
|
|
|
using (var outputStream = new SKFileWStream(outputPath))
|
|
|
|
{
|
2019-01-03 18:11:18 +00:00
|
|
|
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), saveBitmap.GetPixels()))
|
|
|
|
{
|
|
|
|
pixmap.Encode(outputStream, skiaOutputFormat, quality);
|
|
|
|
}
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-14 10:04:22 +00:00
|
|
|
|
2017-05-17 18:18:18 +00:00
|
|
|
return outputPath;
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 19:57:23 +00:00
|
|
|
/// <inheritdoc/>
|
2017-05-09 19:53:46 +00:00
|
|
|
public void CreateImageCollage(ImageCollageOptions options)
|
|
|
|
{
|
2019-01-26 20:10:19 +00:00
|
|
|
double ratio = (double)options.Width / options.Height;
|
2017-05-09 19:53:46 +00:00
|
|
|
|
|
|
|
if (ratio >= 1.4)
|
|
|
|
{
|
2019-06-09 21:51:52 +00:00
|
|
|
new StripCollageBuilder(this).BuildThumbCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
else if (ratio >= .9)
|
|
|
|
{
|
2019-06-09 21:51:52 +00:00
|
|
|
new StripCollageBuilder(this).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-26 20:10:19 +00:00
|
|
|
// TODO: Create Poster collage capability
|
2019-06-09 21:51:52 +00:00
|
|
|
new StripCollageBuilder(this).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DrawIndicator(SKCanvas canvas, int imageWidth, int imageHeight, ImageProcessingOptions options)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-01-26 12:16:47 +00:00
|
|
|
var currentImageSize = new ImageDimensions(imageWidth, imageHeight);
|
2017-05-09 19:53:46 +00:00
|
|
|
|
|
|
|
if (options.AddPlayedIndicator)
|
|
|
|
{
|
2019-01-20 13:25:13 +00:00
|
|
|
PlayedIndicatorDrawer.DrawPlayedIndicator(canvas, currentImageSize);
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
else if (options.UnplayedCount.HasValue)
|
|
|
|
{
|
2019-01-20 13:25:13 +00:00
|
|
|
UnplayedCountIndicator.DrawUnplayedCountIndicator(canvas, currentImageSize, options.UnplayedCount.Value);
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.PercentPlayed > 0)
|
|
|
|
{
|
2019-01-20 13:25:13 +00:00
|
|
|
PercentPlayedDrawer.Process(canvas, currentImageSize, options.PercentPlayed);
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
_logger.LogError(ex, "Error drawing indicator overlay");
|
2017-05-09 19:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-14 16:46:43 +00:00
|
|
|
}
|