This commit is contained in:
Bond_009 2019-01-26 21:10:19 +01:00
parent ce11869a1a
commit bb056f4b59

View File

@ -72,16 +72,11 @@ namespace Jellyfin.Drawing.Skia
_logger.LogInformation("SkiaSharp version: " + GetVersion());
}
public static string GetVersion()
{
return typeof(SKBitmap).GetTypeInfo().Assembly.GetName().Version.ToString();
}
public static Version GetVersion()
=> typeof(SKBitmap).GetTypeInfo().Assembly.GetName().Version;
private static bool IsTransparent(SKColor color)
{
return (color.Red == 255 && color.Green == 255 && color.Blue == 255) || color.Alpha == 0;
}
=> (color.Red == 255 && color.Green == 255 && color.Blue == 255) || color.Alpha == 0;
public static SKEncodedImageFormat GetImageFormat(ImageFormat selectedFormat)
{
@ -130,34 +125,52 @@ namespace Jellyfin.Drawing.Skia
for (int row = 0; row < bitmap.Height; ++row)
{
if (IsTransparentRow(bitmap, row))
{
topmost = row + 1;
else break;
}
else
{
break;
}
}
int bottommost = bitmap.Height;
for (int row = bitmap.Height - 1; row >= 0; --row)
{
if (IsTransparentRow(bitmap, row))
{
bottommost = row;
else break;
}
else
{
break;
}
}
int leftmost = 0, rightmost = bitmap.Width;
for (int col = 0; col < bitmap.Width; ++col)
{
if (IsTransparentColumn(bitmap, col))
{
leftmost = col + 1;
}
else
{
break;
}
}
for (int col = bitmap.Width - 1; col >= 0; --col)
{
if (IsTransparentColumn(bitmap, col))
{
rightmost = col;
}
else
{
break;
}
}
var newRect = SKRectI.Create(leftmost, topmost, rightmost - leftmost, bottommost - topmost);
@ -180,9 +193,7 @@ namespace Jellyfin.Drawing.Skia
}
private static bool HasDiacritics(string text)
{
return !string.Equals(text, text.RemoveDiacritics(), StringComparison.Ordinal);
}
=> !string.Equals(text, text.RemoveDiacritics(), StringComparison.Ordinal);
private static bool RequiresSpecialCharacterHack(string path)
{
@ -243,6 +254,7 @@ namespace Jellyfin.Drawing.Skia
}
private static string[] TransparentImageTypes = new string[] { ".png", ".gif", ".webp" };
internal static SKBitmap Decode(string path, bool forceCleanBitmap, IFileSystem fileSystem, ImageOrientation? orientation, out SKEncodedOrigin origin)
{
if (!fileSystem.FileExists(path))
@ -267,7 +279,7 @@ namespace Jellyfin.Drawing.Skia
var bitmap = new SKBitmap(codec.Info.Width, codec.Info.Height, !requiresTransparencyHack);
// decode
codec.GetPixels(bitmap.Info, bitmap.GetPixels());
var _ = codec.GetPixels(bitmap.Info, bitmap.GetPixels());
origin = codec.EncodedOrigin;
@ -316,16 +328,13 @@ namespace Jellyfin.Drawing.Skia
{
var bitmap = GetBitmap(path, cropWhitespace, true, orientation, out origin);
if (bitmap != null)
{
if (origin != SKEncodedOrigin.TopLeft)
if (bitmap != null && origin != SKEncodedOrigin.TopLeft)
{
using (bitmap)
{
return OrientImage(bitmap, origin);
}
}
}
return bitmap;
}
@ -347,7 +356,6 @@ namespace Jellyfin.Drawing.Skia
switch (origin)
{
case SKEncodedOrigin.TopRight:
{
var rotated = new SKBitmap(bitmap.Width, bitmap.Height);
@ -366,11 +374,8 @@ namespace Jellyfin.Drawing.Skia
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;
float px = (float)bitmap.Width / 2;
float py = (float)bitmap.Height / 2;
surface.RotateDegrees(180, px, py);
surface.DrawBitmap(bitmap, 0, 0);
@ -384,11 +389,9 @@ namespace Jellyfin.Drawing.Skia
var rotated = new SKBitmap(bitmap.Width, bitmap.Height);
using (var surface = new SKCanvas(rotated))
{
float px = bitmap.Width;
px /= 2;
float px = (float)bitmap.Width / 2;
float py = bitmap.Height;
py /= 2;
float py = (float)bitmap.Height / 2;
surface.Translate(rotated.Width, 0);
surface.Scale(-1, 1);
@ -412,7 +415,6 @@ namespace Jellyfin.Drawing.Skia
surface.RotateDegrees(90);
surface.DrawBitmap(bitmap, 0, 0);
}
var flippedBitmap = new SKBitmap(rotated.Width, rotated.Height);
@ -477,8 +479,7 @@ namespace Jellyfin.Drawing.Skia
return rotated;
}
default:
return bitmap;
default: return bitmap;
}
}
@ -488,6 +489,7 @@ namespace Jellyfin.Drawing.Skia
{
throw new ArgumentNullException(nameof(inputPath));
}
if (string.IsNullOrWhiteSpace(inputPath))
{
throw new ArgumentNullException(nameof(outputPath));
@ -507,11 +509,11 @@ namespace Jellyfin.Drawing.Skia
throw new ArgumentOutOfRangeException(string.Format("Skia unable to read image {0}", inputPath));
}
//_logger.LogInformation("Color type {0}", bitmap.Info.ColorType);
var originalImageSize = new ImageDimensions(bitmap.Width, bitmap.Height);
if (!options.CropWhiteSpace && options.HasDefaultOptions(inputPath, originalImageSize) && !autoOrient)
if (!options.CropWhiteSpace
&& options.HasDefaultOptions(inputPath, originalImageSize)
&& !autoOrient)
{
// Just spit out the original file if all the options are default
return inputPath;
@ -522,7 +524,7 @@ namespace Jellyfin.Drawing.Skia
var width = newImageSize.Width;
var height = newImageSize.Height;
using (var resizedBitmap = new SKBitmap(width, height))//, bitmap.ColorType, bitmap.AlphaType))
using (var resizedBitmap = new SKBitmap(width, height, bitmap.ColorType, bitmap.AlphaType))
{
// scale image
bitmap.ScalePixels(resizedBitmap, SKFilterQuality.High);
@ -532,14 +534,12 @@ namespace Jellyfin.Drawing.Skia
{
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(outputPath));
using (var outputStream = new SKFileWStream(outputPath))
{
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), resizedBitmap.GetPixels()))
{
pixmap.Encode(outputStream, skiaOutputFormat, quality);
return outputPath;
}
}
}
// create bitmap to use for canvas drawing used to draw into bitmap
using (var saveBitmap = new SKBitmap(width, height))//, bitmap.ColorType, bitmap.AlphaType))
@ -600,8 +600,7 @@ namespace Jellyfin.Drawing.Skia
public void CreateImageCollage(ImageCollageOptions options)
{
double ratio = options.Width;
ratio /= options.Height;
double ratio = (double)options.Width / options.Height;
if (ratio >= 1.4)
{
@ -613,7 +612,7 @@ namespace Jellyfin.Drawing.Skia
}
else
{
// @todo create Poster collage capability
// TODO: Create Poster collage capability
new StripCollageBuilder(_appPaths, _fileSystem).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
}
}