2017-05-09 15:53:46 -04:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
|
|
|
|
using MediaBrowser.Model.Drawing;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using SkiaSharp;
|
|
|
|
|
using System;
|
2017-05-10 23:23:16 -04:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2017-05-09 15:53:46 -04:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-11-01 15:45:10 -04:00
|
|
|
|
using MediaBrowser.Controller.Extensions;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using MediaBrowser.Model.Globalization;
|
2017-05-09 15:53:46 -04:00
|
|
|
|
|
|
|
|
|
namespace Emby.Drawing.Skia
|
|
|
|
|
{
|
|
|
|
|
public class SkiaEncoder : IImageEncoder
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2017-11-01 15:45:10 -04:00
|
|
|
|
private static IApplicationPaths _appPaths;
|
2017-05-09 15:53:46 -04:00
|
|
|
|
private readonly Func<IHttpClient> _httpClientFactory;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2017-11-01 15:45:10 -04:00
|
|
|
|
private static ILocalizationManager _localizationManager;
|
2017-05-09 15:53:46 -04:00
|
|
|
|
|
2017-11-01 15:45:10 -04:00
|
|
|
|
public SkiaEncoder(ILogger logger, IApplicationPaths appPaths, Func<IHttpClient> httpClientFactory, IFileSystem fileSystem, ILocalizationManager localizationManager)
|
2017-05-09 15:53:46 -04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
_httpClientFactory = httpClientFactory;
|
|
|
|
|
_fileSystem = fileSystem;
|
2017-11-01 15:45:10 -04:00
|
|
|
|
_localizationManager = localizationManager;
|
2017-05-09 15:53:46 -04:00
|
|
|
|
|
|
|
|
|
LogVersion();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string[] SupportedInputFormats
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// Some common file name extensions for RAW picture files include: .cr2, .crw, .dng, .nef, .orf, .rw2, .pef, .arw, .sr2, .srf, and .tif.
|
|
|
|
|
return new[]
|
|
|
|
|
{
|
|
|
|
|
"jpeg",
|
|
|
|
|
"jpg",
|
|
|
|
|
"png",
|
2017-09-22 16:33:01 -04:00
|
|
|
|
|
2017-05-09 15:53:46 -04:00
|
|
|
|
"dng",
|
2017-09-22 16:33:01 -04:00
|
|
|
|
|
2017-05-09 15:53:46 -04:00
|
|
|
|
"webp",
|
|
|
|
|
"gif",
|
|
|
|
|
"bmp",
|
|
|
|
|
"ico",
|
|
|
|
|
"astc",
|
|
|
|
|
"ktx",
|
|
|
|
|
"pkm",
|
2017-09-09 14:19:02 -04:00
|
|
|
|
"wbmp",
|
|
|
|
|
|
|
|
|
|
// working on windows at least
|
2017-09-28 13:01:43 -04:00
|
|
|
|
"cr2",
|
|
|
|
|
"nef"
|
2017-05-09 15:53:46 -04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ImageFormat[] SupportedOutputFormats
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return new[] { ImageFormat.Webp, ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png, ImageFormat.Bmp };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LogVersion()
|
|
|
|
|
{
|
2017-06-24 14:29:23 -04:00
|
|
|
|
// test an operation that requires the native library
|
|
|
|
|
SKPMColor.PreMultiply(SKColors.Black);
|
|
|
|
|
|
2017-05-09 15:53:46 -04:00
|
|
|
|
_logger.Info("SkiaSharp version: " + GetVersion());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetVersion()
|
|
|
|
|
{
|
2017-06-24 14:29:23 -04:00
|
|
|
|
return typeof(SKBitmap).GetTypeInfo().Assembly.GetName().Version.ToString();
|
2017-05-10 00:49:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 23:40:36 -04:00
|
|
|
|
private static bool IsTransparent(SKColor color)
|
2017-05-10 00:49:11 -04:00
|
|
|
|
{
|
2017-09-02 01:33:04 -04:00
|
|
|
|
|
2017-05-10 00:49:11 -04:00
|
|
|
|
return (color.Red == 255 && color.Green == 255 && color.Blue == 255) || color.Alpha == 0;
|
2017-05-09 15:53:46 -04: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-15 23:40:36 -04:00
|
|
|
|
private static bool IsTransparentRow(SKBitmap bmp, int row)
|
2017-05-10 00:49:11 -04:00
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < bmp.Width; ++i)
|
|
|
|
|
{
|
2017-08-15 23:40:36 -04:00
|
|
|
|
if (!IsTransparent(bmp.GetPixel(i, row)))
|
2017-05-10 00:49:11 -04:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 23:40:36 -04:00
|
|
|
|
private static bool IsTransparentColumn(SKBitmap bmp, int col)
|
2017-05-10 00:49:11 -04:00
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < bmp.Height; ++i)
|
|
|
|
|
{
|
2017-08-15 23:40:36 -04:00
|
|
|
|
if (!IsTransparent(bmp.GetPixel(col, i)))
|
2017-05-10 00:49:11 -04:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SKBitmap CropWhiteSpace(SKBitmap bitmap)
|
2017-05-09 15:53:46 -04:00
|
|
|
|
{
|
2017-05-10 00:49:11 -04:00
|
|
|
|
var topmost = 0;
|
|
|
|
|
for (int row = 0; row < bitmap.Height; ++row)
|
|
|
|
|
{
|
2017-08-15 23:40:36 -04:00
|
|
|
|
if (IsTransparentRow(bitmap, row))
|
2017-06-04 14:31:40 -04:00
|
|
|
|
topmost = row + 1;
|
2017-05-10 00:49:11 -04:00
|
|
|
|
else break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-04 14:31:40 -04:00
|
|
|
|
int bottommost = bitmap.Height;
|
2017-05-10 00:49:11 -04:00
|
|
|
|
for (int row = bitmap.Height - 1; row >= 0; --row)
|
|
|
|
|
{
|
2017-08-15 23:40:36 -04:00
|
|
|
|
if (IsTransparentRow(bitmap, row))
|
2017-05-10 00:49:11 -04:00
|
|
|
|
bottommost = row;
|
|
|
|
|
else break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-04 14:31:40 -04:00
|
|
|
|
int leftmost = 0, rightmost = bitmap.Width;
|
2017-05-10 00:49:11 -04:00
|
|
|
|
for (int col = 0; col < bitmap.Width; ++col)
|
|
|
|
|
{
|
2017-08-15 23:40:36 -04:00
|
|
|
|
if (IsTransparentColumn(bitmap, col))
|
2017-06-04 14:31:40 -04:00
|
|
|
|
leftmost = col + 1;
|
2017-05-10 00:49:11 -04:00
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int col = bitmap.Width - 1; col >= 0; --col)
|
2017-05-09 15:53:46 -04:00
|
|
|
|
{
|
2017-08-15 23:40:36 -04:00
|
|
|
|
if (IsTransparentColumn(bitmap, col))
|
2017-05-10 00:49:11 -04:00
|
|
|
|
rightmost = col;
|
|
|
|
|
else
|
|
|
|
|
break;
|
2017-05-09 15:53:46 -04:00
|
|
|
|
}
|
2017-05-09 16:18:02 -04:00
|
|
|
|
|
2017-05-10 00:49:11 -04:00
|
|
|
|
var newRect = SKRectI.Create(leftmost, topmost, rightmost - leftmost, bottommost - topmost);
|
|
|
|
|
|
|
|
|
|
using (var image = SKImage.FromBitmap(bitmap))
|
|
|
|
|
{
|
|
|
|
|
using (var subset = image.Subset(newRect))
|
|
|
|
|
{
|
|
|
|
|
return SKBitmap.FromImage(subset);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-09 15:53:46 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ImageSize GetImageSize(string path)
|
|
|
|
|
{
|
|
|
|
|
using (var s = new SKFileStream(path))
|
|
|
|
|
{
|
|
|
|
|
using (var codec = SKCodec.Create(s))
|
|
|
|
|
{
|
|
|
|
|
var info = codec.Info;
|
|
|
|
|
|
|
|
|
|
return new ImageSize
|
|
|
|
|
{
|
|
|
|
|
Width = info.Width,
|
|
|
|
|
Height = info.Height
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-01 15:45:10 -04:00
|
|
|
|
private static bool HasDiacritics(string text)
|
|
|
|
|
{
|
|
|
|
|
return !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);
|
|
|
|
|
|
|
|
|
|
fileSystem.CopyFile(path, tempPath, true);
|
|
|
|
|
|
|
|
|
|
return tempPath;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-01 21:22:23 -04:00
|
|
|
|
private static string[] TransparentImageTypes = new string[] { ".png", ".gif", ".webp" };
|
2017-11-01 15:45:10 -04:00
|
|
|
|
internal static SKBitmap Decode(string path, bool forceCleanBitmap, IFileSystem fileSystem, out SKCodecOrigin origin)
|
2017-05-10 15:56:59 -04:00
|
|
|
|
{
|
2017-11-01 15:45:10 -04:00
|
|
|
|
if (!fileSystem.FileExists(path))
|
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException("File not found", path);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-10 23:23:16 -04:00
|
|
|
|
var requiresTransparencyHack = TransparentImageTypes.Contains(Path.GetExtension(path) ?? string.Empty);
|
|
|
|
|
|
2017-05-17 14:18:18 -04:00
|
|
|
|
if (requiresTransparencyHack || forceCleanBitmap)
|
2017-05-10 15:56:59 -04:00
|
|
|
|
{
|
2017-11-01 15:45:10 -04:00
|
|
|
|
using (var stream = new SKFileStream(NormalizePath(path, fileSystem)))
|
2017-05-10 23:23:16 -04:00
|
|
|
|
{
|
2017-09-05 15:49:02 -04:00
|
|
|
|
using (var codec = SKCodec.Create(stream))
|
2017-08-30 23:49:38 -04:00
|
|
|
|
{
|
2017-09-05 15:49:02 -04:00
|
|
|
|
if (codec == null)
|
|
|
|
|
{
|
|
|
|
|
origin = SKCodecOrigin.TopLeft;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-08-30 23:49:38 -04:00
|
|
|
|
|
2017-09-05 15:49:02 -04:00
|
|
|
|
// create the bitmap
|
|
|
|
|
var bitmap = new SKBitmap(codec.Info.Width, codec.Info.Height, !requiresTransparencyHack);
|
2017-05-10 15:56:59 -04:00
|
|
|
|
|
2017-09-05 15:49:02 -04:00
|
|
|
|
if (bitmap != null)
|
|
|
|
|
{
|
|
|
|
|
// decode
|
|
|
|
|
codec.GetPixels(bitmap.Info, bitmap.GetPixels());
|
2017-08-30 23:49:38 -04:00
|
|
|
|
|
2017-09-05 15:49:02 -04:00
|
|
|
|
origin = codec.Origin;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
origin = SKCodecOrigin.TopLeft;
|
|
|
|
|
}
|
2017-06-09 15:24:31 -04:00
|
|
|
|
|
2017-09-05 15:49:02 -04:00
|
|
|
|
return bitmap;
|
|
|
|
|
}
|
2017-05-10 23:23:16 -04:00
|
|
|
|
}
|
2017-05-10 15:56:59 -04:00
|
|
|
|
}
|
2017-05-10 23:23:16 -04:00
|
|
|
|
|
2017-11-01 15:45:10 -04:00
|
|
|
|
var resultBitmap = SKBitmap.Decode(NormalizePath(path, fileSystem));
|
2017-05-17 14:18:18 -04:00
|
|
|
|
|
2017-05-18 17:05:47 -04:00
|
|
|
|
if (resultBitmap == null)
|
|
|
|
|
{
|
2017-11-01 15:45:10 -04:00
|
|
|
|
return Decode(path, true, fileSystem, out origin);
|
2017-05-18 17:05:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-17 14:18:18 -04:00
|
|
|
|
// If we have to resize these they often end up distorted
|
|
|
|
|
if (resultBitmap.ColorType == SKColorType.Gray8)
|
|
|
|
|
{
|
|
|
|
|
using (resultBitmap)
|
|
|
|
|
{
|
2017-11-01 15:45:10 -04:00
|
|
|
|
return Decode(path, true, fileSystem, out origin);
|
2017-05-17 14:18:18 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-09 15:24:31 -04:00
|
|
|
|
origin = SKCodecOrigin.TopLeft;
|
2017-05-17 14:18:18 -04:00
|
|
|
|
return resultBitmap;
|
2017-05-10 15:56:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-09 15:24:31 -04:00
|
|
|
|
private SKBitmap GetBitmap(string path, bool cropWhitespace, bool forceAnalyzeBitmap, out SKCodecOrigin origin)
|
2017-05-10 00:49:11 -04:00
|
|
|
|
{
|
|
|
|
|
if (cropWhitespace)
|
|
|
|
|
{
|
2017-11-01 15:45:10 -04:00
|
|
|
|
using (var bitmap = Decode(path, forceAnalyzeBitmap, _fileSystem, out origin))
|
2017-05-10 00:49:11 -04:00
|
|
|
|
{
|
|
|
|
|
return CropWhiteSpace(bitmap);
|
|
|
|
|
}
|
2017-05-10 23:23:16 -04:00
|
|
|
|
}
|
2017-05-10 00:49:11 -04:00
|
|
|
|
|
2017-11-01 15:45:10 -04:00
|
|
|
|
return Decode(path, forceAnalyzeBitmap, _fileSystem, out origin);
|
2017-06-09 15:24:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 23:49:38 -04:00
|
|
|
|
private SKBitmap GetBitmap(string path, bool cropWhitespace, bool autoOrient)
|
2017-06-09 15:24:31 -04:00
|
|
|
|
{
|
|
|
|
|
SKCodecOrigin origin;
|
|
|
|
|
|
|
|
|
|
if (autoOrient)
|
|
|
|
|
{
|
|
|
|
|
var bitmap = GetBitmap(path, cropWhitespace, true, out origin);
|
|
|
|
|
|
2017-08-30 23:49:38 -04:00
|
|
|
|
if (bitmap != null)
|
2017-06-09 15:24:31 -04:00
|
|
|
|
{
|
2017-08-30 23:49:38 -04:00
|
|
|
|
if (origin != SKCodecOrigin.TopLeft)
|
2017-06-09 15:24:31 -04:00
|
|
|
|
{
|
2017-08-30 23:49:38 -04:00
|
|
|
|
using (bitmap)
|
|
|
|
|
{
|
2017-09-02 01:33:04 -04:00
|
|
|
|
return OrientImage(bitmap, origin);
|
2017-08-30 23:49:38 -04:00
|
|
|
|
}
|
2017-06-09 15:24:31 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return GetBitmap(path, cropWhitespace, false, out origin);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-02 01:33:04 -04:00
|
|
|
|
private SKBitmap OrientImage(SKBitmap bitmap, SKCodecOrigin origin)
|
|
|
|
|
{
|
|
|
|
|
//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)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
case SKCodecOrigin.TopRight:
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SKCodecOrigin.BottomRight:
|
|
|
|
|
{
|
|
|
|
|
var rotated = new SKBitmap(bitmap.Width, bitmap.Height);
|
|
|
|
|
using (var surface = new SKCanvas(rotated))
|
|
|
|
|
{
|
|
|
|
|
float px = bitmap.Width;
|
|
|
|
|
px /= 2;
|
|
|
|
|
|
|
|
|
|
float py = bitmap.Height;
|
|
|
|
|
py /= 2;
|
|
|
|
|
|
|
|
|
|
surface.RotateDegrees(180, px, py);
|
|
|
|
|
surface.DrawBitmap(bitmap, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rotated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SKCodecOrigin.BottomLeft:
|
|
|
|
|
{
|
|
|
|
|
var rotated = new SKBitmap(bitmap.Width, bitmap.Height);
|
|
|
|
|
using (var surface = new SKCanvas(rotated))
|
|
|
|
|
{
|
|
|
|
|
float px = bitmap.Width;
|
|
|
|
|
px /= 2;
|
|
|
|
|
|
|
|
|
|
float py = bitmap.Height;
|
|
|
|
|
py /= 2;
|
|
|
|
|
|
|
|
|
|
surface.Translate(rotated.Width, 0);
|
|
|
|
|
surface.Scale(-1, 1);
|
|
|
|
|
|
|
|
|
|
surface.RotateDegrees(180, px, py);
|
|
|
|
|
surface.DrawBitmap(bitmap, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rotated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SKCodecOrigin.LeftTop:
|
|
|
|
|
{
|
2017-09-02 01:44:47 -04:00
|
|
|
|
// TODO: Remove dual canvases, had trouble with flipping
|
|
|
|
|
using (var rotated = new SKBitmap(bitmap.Height, bitmap.Width))
|
2017-09-02 01:33:04 -04:00
|
|
|
|
{
|
2017-09-02 01:44:47 -04:00
|
|
|
|
using (var surface = new SKCanvas(rotated))
|
|
|
|
|
{
|
|
|
|
|
surface.Translate(rotated.Width, 0);
|
2017-09-02 01:33:04 -04:00
|
|
|
|
|
2017-09-02 01:44:47 -04: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 01:33:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SKCodecOrigin.RightTop:
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SKCodecOrigin.RightBottom:
|
|
|
|
|
{
|
2017-09-02 01:44:47 -04:00
|
|
|
|
// TODO: Remove dual canvases, had trouble with flipping
|
|
|
|
|
using (var rotated = new SKBitmap(bitmap.Height, bitmap.Width))
|
2017-09-02 01:33:04 -04:00
|
|
|
|
{
|
2017-09-02 01:44:47 -04: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 01:33:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SKCodecOrigin.LeftBottom:
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
2017-09-02 15:22:19 -04:00
|
|
|
|
return bitmap;
|
2017-06-09 15:24:31 -04:00
|
|
|
|
}
|
2017-05-10 00:49:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-09 15:24:31 -04:00
|
|
|
|
public string EncodeImage(string inputPath, DateTime dateModified, string outputPath, bool autoOrient, ImageOrientation? orientation, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat)
|
2017-05-09 15:53:46 -04:00
|
|
|
|
{
|
2017-05-09 16:18:02 -04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(inputPath))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("inputPath");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrWhiteSpace(inputPath))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("outputPath");
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-10 00:49:11 -04:00
|
|
|
|
var skiaOutputFormat = GetImageFormat(selectedOutputFormat);
|
|
|
|
|
|
|
|
|
|
var hasBackgroundColor = !string.IsNullOrWhiteSpace(options.BackgroundColor);
|
|
|
|
|
var hasForegroundColor = !string.IsNullOrWhiteSpace(options.ForegroundLayer);
|
|
|
|
|
var blur = options.Blur ?? 0;
|
2017-05-10 23:23:16 -04:00
|
|
|
|
var hasIndicator = options.AddPlayedIndicator || options.UnplayedCount.HasValue || !options.PercentPlayed.Equals(0);
|
2017-05-10 00:49:11 -04:00
|
|
|
|
|
2017-08-30 23:49:38 -04:00
|
|
|
|
using (var bitmap = GetBitmap(inputPath, options.CropWhiteSpace, autoOrient))
|
2017-05-09 15:53:46 -04:00
|
|
|
|
{
|
2017-05-17 14:18:18 -04:00
|
|
|
|
if (bitmap == null)
|
|
|
|
|
{
|
2017-08-30 23:49:38 -04:00
|
|
|
|
throw new ArgumentOutOfRangeException(string.Format("Skia unable to read image {0}", inputPath));
|
2017-05-17 14:18:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//_logger.Info("Color type {0}", bitmap.Info.ColorType);
|
|
|
|
|
|
|
|
|
|
var originalImageSize = new ImageSize(bitmap.Width, bitmap.Height);
|
|
|
|
|
|
2017-06-09 15:24:31 -04:00
|
|
|
|
if (!options.CropWhiteSpace && options.HasDefaultOptions(inputPath, originalImageSize) && !autoOrient)
|
2017-05-14 22:27:58 -04:00
|
|
|
|
{
|
2017-05-17 14:18:18 -04:00
|
|
|
|
// Just spit out the original file if all the options are default
|
|
|
|
|
return inputPath;
|
2017-05-14 22:27:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newImageSize = ImageHelper.GetNewImageSize(options, originalImageSize);
|
|
|
|
|
|
|
|
|
|
var width = Convert.ToInt32(Math.Round(newImageSize.Width));
|
|
|
|
|
var height = Convert.ToInt32(Math.Round(newImageSize.Height));
|
|
|
|
|
|
2017-05-10 23:23:16 -04:00
|
|
|
|
using (var resizedBitmap = new SKBitmap(width, height))//, bitmap.ColorType, bitmap.AlphaType))
|
2017-05-09 15:53:46 -04:00
|
|
|
|
{
|
|
|
|
|
// scale image
|
2017-05-10 15:57:55 -04:00
|
|
|
|
var resizeMethod = SKBitmapResizeMethod.Lanczos3;
|
2017-05-10 00:49:11 -04:00
|
|
|
|
|
|
|
|
|
bitmap.Resize(resizedBitmap, resizeMethod);
|
|
|
|
|
|
|
|
|
|
// If all we're doing is resizing then we can stop now
|
|
|
|
|
if (!hasBackgroundColor && !hasForegroundColor && blur == 0 && !hasIndicator)
|
|
|
|
|
{
|
|
|
|
|
using (var outputStream = new SKFileWStream(outputPath))
|
|
|
|
|
{
|
|
|
|
|
resizedBitmap.Encode(outputStream, skiaOutputFormat, quality);
|
2017-05-17 14:18:18 -04:00
|
|
|
|
return outputPath;
|
2017-05-10 00:49:11 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-09 15:53:46 -04:00
|
|
|
|
|
|
|
|
|
// create bitmap to use for canvas drawing
|
2017-05-10 23:23:16 -04:00
|
|
|
|
using (var saveBitmap = new SKBitmap(width, height))//, bitmap.ColorType, bitmap.AlphaType))
|
2017-05-09 15:53:46 -04:00
|
|
|
|
{
|
|
|
|
|
// create canvas used to draw into bitmap
|
|
|
|
|
using (var canvas = new SKCanvas(saveBitmap))
|
|
|
|
|
{
|
|
|
|
|
// set background color if present
|
2017-05-10 00:49:11 -04:00
|
|
|
|
if (hasBackgroundColor)
|
2017-05-09 15:53:46 -04:00
|
|
|
|
{
|
|
|
|
|
canvas.Clear(SKColor.Parse(options.BackgroundColor));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add blur if option is present
|
2017-05-10 00:49:11 -04:00
|
|
|
|
if (blur > 0)
|
2017-05-09 15:53:46 -04:00
|
|
|
|
{
|
|
|
|
|
using (var paint = new SKPaint())
|
|
|
|
|
{
|
|
|
|
|
// create image from resized bitmap to apply blur
|
2017-05-10 23:23:16 -04:00
|
|
|
|
using (var filter = SKImageFilter.CreateBlur(blur, blur))
|
2017-05-09 15:53:46 -04:00
|
|
|
|
{
|
|
|
|
|
paint.ImageFilter = filter;
|
|
|
|
|
canvas.DrawBitmap(resizedBitmap, SKRect.Create(width, height), paint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// draw resized bitmap onto canvas
|
|
|
|
|
canvas.DrawBitmap(resizedBitmap, SKRect.Create(width, height));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If foreground layer present then draw
|
2017-05-10 00:49:11 -04:00
|
|
|
|
if (hasForegroundColor)
|
2017-05-09 15:53:46 -04:00
|
|
|
|
{
|
|
|
|
|
Double opacity;
|
|
|
|
|
if (!Double.TryParse(options.ForegroundLayer, out opacity)) opacity = .4;
|
|
|
|
|
|
2017-05-10 23:23:16 -04:00
|
|
|
|
canvas.DrawColor(new SKColor(0, 0, 0, (Byte)((1 - opacity) * 0xFF)), SKBlendMode.SrcOver);
|
2017-05-09 15:53:46 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-10 00:49:11 -04:00
|
|
|
|
if (hasIndicator)
|
|
|
|
|
{
|
|
|
|
|
DrawIndicator(canvas, width, height, options);
|
|
|
|
|
}
|
2017-05-09 15:53:46 -04:00
|
|
|
|
|
|
|
|
|
using (var outputStream = new SKFileWStream(outputPath))
|
|
|
|
|
{
|
2017-05-10 00:49:11 -04:00
|
|
|
|
saveBitmap.Encode(outputStream, skiaOutputFormat, quality);
|
2017-05-09 15:53:46 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-17 14:18:18 -04:00
|
|
|
|
return outputPath;
|
2017-05-09 15:53:46 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CreateImageCollage(ImageCollageOptions options)
|
|
|
|
|
{
|
|
|
|
|
double ratio = options.Width;
|
|
|
|
|
ratio /= options.Height;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2017-05-10 00:49:11 -04:00
|
|
|
|
// @todo create Poster collage capability
|
|
|
|
|
new StripCollageBuilder(_appPaths, _fileSystem).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
|
2017-05-09 15:53:46 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DrawIndicator(SKCanvas canvas, int imageWidth, int imageHeight, ImageProcessingOptions options)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var currentImageSize = new ImageSize(imageWidth, imageHeight);
|
|
|
|
|
|
|
|
|
|
if (options.AddPlayedIndicator)
|
|
|
|
|
{
|
|
|
|
|
var task = new PlayedIndicatorDrawer(_appPaths, _httpClientFactory(), _fileSystem).DrawPlayedIndicator(canvas, currentImageSize);
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
}
|
|
|
|
|
else if (options.UnplayedCount.HasValue)
|
|
|
|
|
{
|
|
|
|
|
new UnplayedCountIndicator(_appPaths, _httpClientFactory(), _fileSystem).DrawUnplayedCountIndicator(canvas, currentImageSize, options.UnplayedCount.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.PercentPlayed > 0)
|
|
|
|
|
{
|
|
|
|
|
new PercentPlayedDrawer().Process(canvas, currentImageSize, options.PercentPlayed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error drawing indicator overlay", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "Skia"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SupportsImageCollageCreation
|
|
|
|
|
{
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SupportsImageEncoding
|
|
|
|
|
{
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-10 00:49:11 -04:00
|
|
|
|
}
|