jellyfin/Jellyfin.Drawing.Skia/SkiaEncoder.cs

656 lines
24 KiB
C#
Raw Normal View History

using System;
2019-02-06 20:31:41 +00:00
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using MediaBrowser.Common.Configuration;
2017-05-09 19:53:46 +00:00
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Extensions;
2017-05-09 19:53:46 +00:00
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Globalization;
2017-05-09 19:53:46 +00:00
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;
2017-05-09 19:53:46 +00:00
using SkiaSharp;
2019-01-26 19:43:13 +00:00
namespace Jellyfin.Drawing.Skia
2017-05-09 19:53:46 +00:00
{
public class SkiaEncoder : IImageEncoder
{
private readonly ILogger _logger;
private static IApplicationPaths _appPaths;
2017-05-09 19:53:46 +00:00
private readonly IFileSystem _fileSystem;
private static ILocalizationManager _localizationManager;
2017-05-09 19:53:46 +00:00
public SkiaEncoder(
ILoggerFactory loggerFactory,
IApplicationPaths appPaths,
IFileSystem fileSystem,
ILocalizationManager localizationManager)
2017-05-09 19:53:46 +00:00
{
_logger = loggerFactory.CreateLogger("ImageEncoder");
2017-05-09 19:53:46 +00:00
_appPaths = appPaths;
_fileSystem = fileSystem;
_localizationManager = localizationManager;
2017-05-09 19:53:46 +00:00
LogVersion();
}
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
{
"jpeg",
"jpg",
"png",
2017-05-09 19:53:46 +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-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
private void LogVersion()
{
2017-06-24 18:29:23 +00:00
// test an operation that requires the native library
SKPMColor.PreMultiply(SKColors.Black);
_logger.LogInformation("SkiaSharp version: " + GetVersion());
2017-05-09 19:53:46 +00:00
}
2019-01-26 20:10:19 +00:00
public static Version GetVersion()
=> typeof(SKBitmap).GetTypeInfo().Assembly.GetName().Version;
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
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)
{
for (var i = 0; i < bmp.Width; ++i)
{
2017-08-16 03:40:36 +00:00
if (!IsTransparent(bmp.GetPixel(i, row)))
{
return false;
}
}
return true;
}
2017-08-16 03:40:36 +00:00
private static bool IsTransparentColumn(SKBitmap bmp, int col)
{
for (var i = 0; i < bmp.Height; ++i)
{
2017-08-16 03:40:36 +00:00
if (!IsTransparent(bmp.GetPixel(col, i)))
{
return false;
}
}
return true;
}
private SKBitmap CropWhiteSpace(SKBitmap bitmap)
2017-05-09 19:53:46 +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-06-04 18:31:40 +00:00
int bottommost = bitmap.Height;
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
{
bottommost = row;
2019-01-26 20:10:19 +00:00
}
else
{
break;
}
}
2017-06-04 18:31:40 +00:00
int leftmost = 0, rightmost = bitmap.Width;
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
}
else
2019-01-26 20:10:19 +00:00
{
break;
2019-01-26 20:10:19 +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
{
rightmost = col;
2019-01-26 20:10:19 +00:00
}
else
2019-01-26 20:10:19 +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
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))
{
2018-12-13 21:34:28 +00:00
return SKBitmap.FromImage(subset);
}
2017-05-09 19:53:46 +00:00
}
public ImageDimensions GetImageSize(string path)
2017-05-09 19:53:46 +00:00
{
using (var s = new SKFileStream(path))
2018-12-13 21:34:28 +00:00
using (var codec = SKCodec.Create(s))
2017-05-09 19:53:46 +00:00
{
2018-12-13 21:34:28 +00:00
var info = codec.Info;
2017-05-09 19:53:46 +00:00
return new ImageDimensions(info.Width, info.Height);
2017-05-09 19:53:46 +00:00
}
}
private static bool HasDiacritics(string text)
2019-01-26 20:10:19 +00:00
=> !string.Equals(text, text.RemoveDiacritics(), StringComparison.Ordinal);
private static bool RequiresSpecialCharacterHack(string path)
{
if (_localizationManager.HasUnicodeCategory(path, UnicodeCategory.OtherLetter))
{
return true;
}
if (HasDiacritics(path))
{
return true;
}
return false;
}
private static string NormalizePath(string path, IFileSystem fileSystem)
{
if (!RequiresSpecialCharacterHack(path))
{
return path;
}
var tempPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + Path.GetExtension(path) ?? string.Empty);
Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
2019-01-26 21:31:59 +00:00
File.Copy(path, tempPath, true);
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-02-06 20:31:41 +00:00
private static readonly HashSet<string> TransparentImageTypes
= new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".png", ".gif", ".webp" };
2019-01-26 20:10:19 +00:00
2018-12-13 21:34:28 +00:00
internal static SKBitmap Decode(string path, bool forceCleanBitmap, IFileSystem fileSystem, ImageOrientation? orientation, out SKEncodedOrigin origin)
2017-05-10 19:56:59 +00:00
{
if (!File.Exists(path))
{
throw new FileNotFoundException("File not found", path);
}
2019-02-06 20:31:41 +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
{
using (var stream = new SKFileStream(NormalizePath(path, fileSystem)))
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
// decode
2019-01-26 20:10:19 +00:00
var _ = codec.GetPixels(bitmap.Info, bitmap.GetPixels());
2017-06-09 19:24:31 +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
var resultBitmap = SKBitmap.Decode(NormalizePath(path, fileSystem));
2017-05-17 18:18:18 +00:00
2017-05-18 21:05:47 +00:00
if (resultBitmap == null)
{
2018-09-12 17:26:21 +00:00
return Decode(path, true, fileSystem, 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)
{
2018-09-12 17:26:21 +00:00
return Decode(path, true, fileSystem, 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)
{
if (cropWhitespace)
{
2018-09-12 17:26:21 +00:00
using (var bitmap = Decode(path, forceAnalyzeBitmap, _fileSystem, orientation, out origin))
{
return CropWhiteSpace(bitmap);
}
2017-05-11 03:23:16 +00:00
}
2018-09-12 17:26:21 +00:00
return Decode(path, forceAnalyzeBitmap, _fileSystem, 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
{
//var transformations = {
// 2: { rotate: 0, flip: true},
// 3: { rotate: 180, flip: false},
// 4: { rotate: 180, flip: true},
// 5: { rotate: 90, flip: true},
// 6: { rotate: 90, flip: false},
// 7: { rotate: 270, flip: true},
// 8: { rotate: 270, flip: false},
//}
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-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))
{
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))
{
throw new ArgumentNullException(nameof(outputPath));
2017-05-09 20:18:02 +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);
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)
{
2017-08-31 03:49:38 +00:00
throw new ArgumentOutOfRangeException(string.Format("Skia unable to read image {0}", inputPath));
2017-05-17 18:18:18 +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);
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
bitmap.ScalePixels(resizedBitmap, SKFilterQuality.High);
// If all we're doing is resizing then we can stop now
if (!hasBackgroundColor && !hasForegroundColor && blur == 0 && !hasIndicator)
{
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
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()))
{
2019-01-26 20:10:19 +00:00
pixmap.Encode(outputStream, skiaOutputFormat, quality);
return outputPath;
}
}
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
2017-05-11 03:23:16 +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)
{
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
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
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
2018-12-13 21:34:28 +00:00
using (var outputStream = new SKFileWStream(outputPath))
{
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), saveBitmap.GetPixels()))
{
pixmap.Encode(outputStream, skiaOutputFormat, quality);
}
2017-05-09 19:53:46 +00:00
}
}
}
}
2017-05-17 18:18:18 +00:00
return outputPath;
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)
{
new StripCollageBuilder(_appPaths, _fileSystem).BuildThumbCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
}
else if (ratio >= .9)
{
new StripCollageBuilder(_appPaths, _fileSystem).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
}
else
{
2019-01-26 20:10:19 +00:00
// TODO: Create Poster collage capability
new StripCollageBuilder(_appPaths, _fileSystem).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
{
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
}
}
public string Name => "Skia";
2017-05-09 19:53:46 +00:00
public bool SupportsImageCollageCreation => true;
2017-05-09 19:53:46 +00:00
public bool SupportsImageEncoding => true;
2017-05-09 19:53:46 +00:00
}
2018-12-14 16:46:43 +00:00
}