Skip to content

Commit

Permalink
improve skia error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
LukePulverenti committed Sep 20, 2018
1 parent ed925c3 commit 042b15c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
51 changes: 36 additions & 15 deletions Emby.Drawing.Skia/SkiaEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,12 @@ private SKBitmap CropWhiteSpace(SKBitmap bitmap)

public ImageSize GetImageSize(string path)
{
using (var s = new SKFileStream(path))
if (!_fileSystem.FileExists(path))
{
throw new FileNotFoundException("File not found", path);
}

using (var s = new SKFileStream(NormalizePath(path, _fileSystem)))
{
using (var codec = SKCodec.Create(s))
{
Expand Down Expand Up @@ -263,7 +268,7 @@ private static SKCodecOrigin GetSKCodecOrigin(ImageOrientation? orientation)
}

private static string[] TransparentImageTypes = new string[] { ".png", ".gif", ".webp" };
internal static SKBitmap Decode(string path, bool forceCleanBitmap, IFileSystem fileSystem, ImageOrientation? orientation, out SKCodecOrigin origin)
internal static SKBitmap Decode(string path, bool forceCleanBitmap, IFileSystem fileSystem, ILogger logger, ImageOrientation? orientation, out SKCodecOrigin origin)
{
if (!fileSystem.FileExists(path))
{
Expand All @@ -272,9 +277,12 @@ internal static SKBitmap Decode(string path, bool forceCleanBitmap, IFileSystem

var requiresTransparencyHack = TransparentImageTypes.Contains(Path.GetExtension(path) ?? string.Empty);

var normalizedPath = NormalizePath(path, fileSystem);

if (requiresTransparencyHack || forceCleanBitmap)
{
using (var stream = new SKFileStream(NormalizePath(path, fileSystem)))
//logger.Debug("Opening {0} for decoding with forceCleanBitmap", normalizedPath);
using (var stream = new SKFileStream(normalizedPath))
{
using (var codec = SKCodec.Create(stream))
{
Expand All @@ -287,11 +295,17 @@ internal static SKBitmap Decode(string path, bool forceCleanBitmap, IFileSystem
// create the bitmap
var bitmap = new SKBitmap(codec.Info.Width, codec.Info.Height, !requiresTransparencyHack);

if (bitmap != null)
if (bitmap != null && !bitmap.IsNull && !bitmap.IsEmpty)
{
// decode
codec.GetPixels(bitmap.Info, bitmap.GetPixels());

var result = codec.GetPixels(bitmap.Info, bitmap.GetPixels());

if (result != SKCodecResult.Success)
{
origin = GetSKCodecOrigin(orientation);
return null;
}

origin = codec.Origin;
}
else
Expand All @@ -304,19 +318,26 @@ internal static SKBitmap Decode(string path, bool forceCleanBitmap, IFileSystem
}
}

var resultBitmap = SKBitmap.Decode(NormalizePath(path, fileSystem));
//logger.Debug("Opening {0} for decoding without forceCleanBitmap", normalizedPath);
var resultBitmap = SKBitmap.Decode(normalizedPath);

if (resultBitmap == null || resultBitmap.IsNull || resultBitmap.IsEmpty)
{
return Decode(path, true, fileSystem, logger, orientation, out origin);
}

if (resultBitmap == null)
if (resultBitmap.IsNull || resultBitmap.IsEmpty)
{
return Decode(path, true, fileSystem, orientation, out origin);
origin = GetSKCodecOrigin(orientation);
return null;
}

// If we have to resize these they often end up distorted
if (resultBitmap.ColorType == SKColorType.Gray8)
{
using (resultBitmap)
{
return Decode(path, true, fileSystem, orientation, out origin);
return Decode(path, true, fileSystem, logger, orientation, out origin);
}
}

Expand All @@ -328,13 +349,13 @@ private SKBitmap GetBitmap(string path, bool cropWhitespace, bool forceAnalyzeBi
{
if (cropWhitespace)
{
using (var bitmap = Decode(path, forceAnalyzeBitmap, _fileSystem, orientation, out origin))
using (var bitmap = Decode(path, forceAnalyzeBitmap, _fileSystem, _logger, orientation, out origin))
{
return CropWhiteSpace(bitmap);
}
}

return Decode(path, forceAnalyzeBitmap, _fileSystem, orientation, out origin);
return Decode(path, forceAnalyzeBitmap, _fileSystem, _logger, orientation, out origin);
}

private SKBitmap GetBitmap(string path, bool cropWhitespace, bool autoOrient, ImageOrientation? orientation)
Expand Down Expand Up @@ -633,16 +654,16 @@ public void CreateImageCollage(ImageCollageOptions options)

if (ratio >= 1.4)
{
new StripCollageBuilder(_appPaths, _fileSystem).BuildThumbCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
new StripCollageBuilder(_appPaths, _fileSystem, _logger).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);
new StripCollageBuilder(_appPaths, _fileSystem, _logger).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
}
else
{
// @todo create Poster collage capability
new StripCollageBuilder(_appPaths, _fileSystem).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
new StripCollageBuilder(_appPaths, _fileSystem, _logger).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
}
}

Expand Down
7 changes: 5 additions & 2 deletions Emby.Drawing.Skia/StripCollageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
using System.IO;
using MediaBrowser.Model.IO;
using System.Collections.Generic;
using MediaBrowser.Model.Logging;

namespace Emby.Drawing.Skia
{
public class StripCollageBuilder
{
private readonly IApplicationPaths _appPaths;
private readonly IFileSystem _fileSystem;
private readonly ILogger _logger;

public StripCollageBuilder(IApplicationPaths appPaths, IFileSystem fileSystem)
public StripCollageBuilder(IApplicationPaths appPaths, IFileSystem fileSystem, ILogger logger)
{
_appPaths = appPaths;
_fileSystem = fileSystem;
_logger = logger;
}

public static SKEncodedImageFormat GetEncodedFormat(string outputPath)
Expand Down Expand Up @@ -173,7 +176,7 @@ private SKBitmap GetNextValidImage(string[] paths, int currentIndex, out int new
}

SKCodecOrigin origin;
bitmap = SkiaEncoder.Decode(paths[currentIndex], false, _fileSystem, null, out origin);
bitmap = SkiaEncoder.Decode(paths[currentIndex], false, _fileSystem, _logger, null, out origin);

imagesTested[currentIndex] = 0;

Expand Down

0 comments on commit 042b15c

Please sign in to comment.