Merge pull request #4988 from Bond-009/crop

This commit is contained in:
Bond-009 2021-04-10 02:57:43 +02:00 committed by GitHub
commit b2fbf97abd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 6 additions and 92 deletions

View File

@ -181,11 +181,6 @@ namespace Emby.Drawing
{ {
if (!File.Exists(cacheFilePath)) if (!File.Exists(cacheFilePath))
{ {
if (options.CropWhiteSpace && !SupportsTransparency(originalImagePath))
{
options.CropWhiteSpace = false;
}
string resultPath = _imageEncoder.EncodeImage(originalImagePath, dateModified, cacheFilePath, autoOrient, orientation, quality, options, outputFormat); string resultPath = _imageEncoder.EncodeImage(originalImagePath, dateModified, cacheFilePath, autoOrient, orientation, quality, options, outputFormat);
if (string.Equals(resultPath, originalImagePath, StringComparison.OrdinalIgnoreCase)) if (string.Equals(resultPath, originalImagePath, StringComparison.OrdinalIgnoreCase))

View File

@ -1695,7 +1695,7 @@ namespace Jellyfin.Api.Controllers
int? width, int? width,
int? height, int? height,
int? quality, int? quality,
bool? cropWhitespace, bool? cropWhitespace, // TODO: Remove
bool? addPlayedIndicator, bool? addPlayedIndicator,
int? blur, int? blur,
string? backgroundColor, string? backgroundColor,
@ -1770,7 +1770,6 @@ namespace Jellyfin.Api.Controllers
backgroundColor, backgroundColor,
foregroundLayer, foregroundLayer,
imageInfo, imageInfo,
cropWhitespace.Value,
outputFormats, outputFormats,
cacheDuration, cacheDuration,
responseHeaders, responseHeaders,
@ -1869,7 +1868,6 @@ namespace Jellyfin.Api.Controllers
string? backgroundColor, string? backgroundColor,
string? foregroundLayer, string? foregroundLayer,
ItemImageInfo imageInfo, ItemImageInfo imageInfo,
bool cropWhitespace,
IReadOnlyCollection<ImageFormat> supportedFormats, IReadOnlyCollection<ImageFormat> supportedFormats,
TimeSpan? cacheDuration, TimeSpan? cacheDuration,
IDictionary<string, string> headers, IDictionary<string, string> headers,
@ -1882,7 +1880,6 @@ namespace Jellyfin.Api.Controllers
var options = new ImageProcessingOptions var options = new ImageProcessingOptions
{ {
CropWhiteSpace = cropWhitespace,
Height = height, Height = height,
ImageIndex = index ?? 0, ImageIndex = index ?? 0,
Image = imageInfo, Image = imageInfo,

View File

@ -91,9 +91,6 @@ namespace Jellyfin.Drawing.Skia
} }
} }
private static bool IsTransparent(SKColor color)
=> (color.Red == 255 && color.Green == 255 && color.Blue == 255) || color.Alpha == 0;
/// <summary> /// <summary>
/// Convert a <see cref="ImageFormat"/> to a <see cref="SKEncodedImageFormat"/>. /// Convert a <see cref="ImageFormat"/> to a <see cref="SKEncodedImageFormat"/>.
/// </summary> /// </summary>
@ -111,65 +108,6 @@ namespace Jellyfin.Drawing.Skia
}; };
} }
private static bool IsTransparentRow(SKBitmap bmp, int row)
{
for (var i = 0; i < bmp.Width; ++i)
{
if (!IsTransparent(bmp.GetPixel(i, row)))
{
return false;
}
}
return true;
}
private static bool IsTransparentColumn(SKBitmap bmp, int col)
{
for (var i = 0; i < bmp.Height; ++i)
{
if (!IsTransparent(bmp.GetPixel(col, i)))
{
return false;
}
}
return true;
}
private SKBitmap CropWhiteSpace(SKBitmap bitmap)
{
var topmost = 0;
while (topmost < bitmap.Height && IsTransparentRow(bitmap, topmost))
{
topmost++;
}
int bottommost = bitmap.Height;
while (bottommost >= 0 && IsTransparentRow(bitmap, bottommost - 1))
{
bottommost--;
}
var leftmost = 0;
while (leftmost < bitmap.Width && IsTransparentColumn(bitmap, leftmost))
{
leftmost++;
}
var rightmost = bitmap.Width;
while (rightmost >= 0 && IsTransparentColumn(bitmap, rightmost - 1))
{
rightmost--;
}
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);
}
/// <inheritdoc /> /// <inheritdoc />
/// <exception cref="ArgumentNullException">The path is null.</exception> /// <exception cref="ArgumentNullException">The path is null.</exception>
/// <exception cref="FileNotFoundException">The path is not valid.</exception> /// <exception cref="FileNotFoundException">The path is not valid.</exception>
@ -312,22 +250,11 @@ namespace Jellyfin.Drawing.Skia
return resultBitmap; return resultBitmap;
} }
private SKBitmap? GetBitmap(string path, bool cropWhitespace, bool forceAnalyzeBitmap, ImageOrientation? orientation, out SKEncodedOrigin origin) private SKBitmap? GetBitmap(string path, bool autoOrient, ImageOrientation? orientation)
{
if (cropWhitespace)
{
using var bitmap = Decode(path, forceAnalyzeBitmap, orientation, out origin);
return bitmap == null ? null : CropWhiteSpace(bitmap);
}
return Decode(path, forceAnalyzeBitmap, orientation, out origin);
}
private SKBitmap? GetBitmap(string path, bool cropWhitespace, bool autoOrient, ImageOrientation? orientation)
{ {
if (autoOrient) if (autoOrient)
{ {
var bitmap = GetBitmap(path, cropWhitespace, true, orientation, out var origin); var bitmap = Decode(path, true, orientation, out var origin);
if (bitmap != null && origin != SKEncodedOrigin.TopLeft) if (bitmap != null && origin != SKEncodedOrigin.TopLeft)
{ {
@ -340,7 +267,7 @@ namespace Jellyfin.Drawing.Skia
return bitmap; return bitmap;
} }
return GetBitmap(path, cropWhitespace, false, orientation, out _); return Decode(path, false, orientation, out _);
} }
private SKBitmap OrientImage(SKBitmap bitmap, SKEncodedOrigin origin) private SKBitmap OrientImage(SKBitmap bitmap, SKEncodedOrigin origin)
@ -461,7 +388,7 @@ namespace Jellyfin.Drawing.Skia
var blur = options.Blur ?? 0; var blur = options.Blur ?? 0;
var hasIndicator = options.AddPlayedIndicator || options.UnplayedCount.HasValue || !options.PercentPlayed.Equals(0); var hasIndicator = options.AddPlayedIndicator || options.UnplayedCount.HasValue || !options.PercentPlayed.Equals(0);
using var bitmap = GetBitmap(inputPath, options.CropWhiteSpace, autoOrient, orientation); using var bitmap = GetBitmap(inputPath, autoOrient, orientation);
if (bitmap == null) if (bitmap == null)
{ {
throw new InvalidDataException($"Skia unable to read image {inputPath}"); throw new InvalidDataException($"Skia unable to read image {inputPath}");
@ -469,9 +396,7 @@ namespace Jellyfin.Drawing.Skia
var originalImageSize = new ImageDimensions(bitmap.Width, bitmap.Height); var originalImageSize = new ImageDimensions(bitmap.Width, bitmap.Height);
if (!options.CropWhiteSpace if (options.HasDefaultOptions(inputPath, originalImageSize) && !autoOrient)
&& options.HasDefaultOptions(inputPath, originalImageSize)
&& !autoOrient)
{ {
// Just spit out the original file if all the options are default // Just spit out the original file if all the options are default
return inputPath; return inputPath;

View File

@ -24,8 +24,6 @@ namespace MediaBrowser.Controller.Drawing
public int ImageIndex { get; set; } public int ImageIndex { get; set; }
public bool CropWhiteSpace { get; set; }
public int? Width { get; set; } public int? Width { get; set; }
public int? Height { get; set; } public int? Height { get; set; }
@ -106,7 +104,6 @@ namespace MediaBrowser.Controller.Drawing
PercentPlayed.Equals(0) && PercentPlayed.Equals(0) &&
!UnplayedCount.HasValue && !UnplayedCount.HasValue &&
!Blur.HasValue && !Blur.HasValue &&
!CropWhiteSpace &&
string.IsNullOrEmpty(BackgroundColor) && string.IsNullOrEmpty(BackgroundColor) &&
string.IsNullOrEmpty(ForegroundLayer); string.IsNullOrEmpty(ForegroundLayer);
} }