Skip to content

Commit

Permalink
reduce frequency of image encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
LukePulverenti committed Mar 13, 2018
1 parent 9d85858 commit ab32b9f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Emby.Drawing.Skia/SkiaEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ImageFormat[] SupportedOutputFormats
{
get
{
return new[] { ImageFormat.Webp, ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png, ImageFormat.Bmp };
return new[] { ImageFormat.Webp, ImageFormat.Jpg, ImageFormat.Png };
}
}

Expand Down Expand Up @@ -290,7 +290,7 @@ internal static SKBitmap Decode(string path, bool forceCleanBitmap, IFileSystem
{
// decode
codec.GetPixels(bitmap.Info, bitmap.GetPixels());

origin = codec.Origin;
}
else
Expand Down
9 changes: 6 additions & 3 deletions Emby.Drawing/ImageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public ImageFormat[] GetSupportedImageOutputFormats()
return _imageEncoder.SupportedOutputFormats;
}

private readonly string[] TransparentImageTypes = new string[] { ".png", ".webp" };
private readonly string[] TransparentImageTypes = new string[] { ".png", ".webp", ".gif" };
public bool SupportsTransparency(string path)
{
return TransparentImageTypes.Contains(Path.GetExtension(path) ?? string.Empty);
Expand All @@ -196,6 +196,7 @@ public async Task<Tuple<string, string, DateTime>> ProcessImage(ImageProcessingO

var originalImagePath = originalImage.Path;
var dateModified = originalImage.DateModified;
var originalImageSize = originalImage.Width > 0 && originalImage.Height > 0 ? new ImageSize(originalImage.Width, originalImage.Height) : (ImageSize?)null;

if (!_imageEncoder.SupportsImageEncoding)
{
Expand Down Expand Up @@ -225,6 +226,8 @@ public async Task<Tuple<string, string, DateTime>> ProcessImage(ImageProcessingO
originalImagePath = tuple.Item1;
dateModified = tuple.Item2;
requiresTransparency = tuple.Item3;
// TODO: Get this info
originalImageSize = null;
}

var photo = item as Photo;
Expand All @@ -248,7 +251,7 @@ public async Task<Tuple<string, string, DateTime>> ProcessImage(ImageProcessingO
}
}

if (options.HasDefaultOptions(originalImagePath) && (!autoOrient || !options.RequiresAutoOrientation))
if (options.HasDefaultOptions(originalImagePath, originalImageSize) && (!autoOrient || !options.RequiresAutoOrientation))
{
// Just spit out the original file if all the options are default
return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
Expand Down Expand Up @@ -329,7 +332,7 @@ private ImageFormat GetOutputFormat(ImageFormat[] clientSupportedFormats, bool r
}

// If transparency is needed and webp isn't supported, than png is the only option
if (requiresTransparency)
if (requiresTransparency && clientSupportedFormats.Contains(ImageFormat.Png))
{
return ImageFormat.Png;
}
Expand Down
56 changes: 40 additions & 16 deletions MediaBrowser.Api/Images/ImageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,37 +660,61 @@ private ImageFormat[] GetOutputFormats(ImageRequest request)
private ImageFormat[] GetClientSupportedFormats()
{
//Logger.Debug("Request types: {0}", string.Join(",", Request.AcceptTypes ?? new string[] { }));
var supportsWebP = (Request.AcceptTypes ?? new string[] { }).Contains("image/webp", StringComparer.OrdinalIgnoreCase);
var supportedFormats = (Request.AcceptTypes ?? new string[] { }).Select(i => i.Split(';')[0]).ToArray();
var acceptParam = Request.QueryString["accept"];

var userAgent = Request.UserAgent ?? string.Empty;

if (!supportsWebP)
{
if (string.Equals(Request.QueryString["accept"], "webp", StringComparison.OrdinalIgnoreCase))
{
supportsWebP = true;
}
}
var supportsWebP = SupportsFormat(supportedFormats, acceptParam, "webp", false);

if (!supportsWebP)
{
var userAgent = Request.UserAgent ?? string.Empty;
if (userAgent.IndexOf("crosswalk", StringComparison.OrdinalIgnoreCase) != -1 &&
userAgent.IndexOf("android", StringComparison.OrdinalIgnoreCase) != -1)
{
supportsWebP = true;
}
}

var formats = new List<ImageFormat>(4);

if (supportsWebP)
{
// Not displaying properly on iOS
if (userAgent.IndexOf("cfnetwork", StringComparison.OrdinalIgnoreCase) == -1)
{
return new[] { ImageFormat.Webp, ImageFormat.Jpg, ImageFormat.Png };
}
formats.Add(ImageFormat.Webp);
}

if (SupportsFormat(supportedFormats, acceptParam, "jpg", true))
{
formats.Add(ImageFormat.Jpg);
}

if (SupportsFormat(supportedFormats, acceptParam, "png", true))
{
formats.Add(ImageFormat.Png);
}

if (SupportsFormat(supportedFormats, acceptParam, "gif", true))
{
formats.Add(ImageFormat.Gif);
}

return formats.ToArray();
}

private bool SupportsFormat(string[] requestAcceptTypes, string acceptParam, string format, bool acceptAll)
{
var mimeType = "image/" + format;

if (requestAcceptTypes.Contains(mimeType))
{
return true;
}

if (acceptAll && requestAcceptTypes.Contains("*/*"))
{
return true;
}

return new[] { ImageFormat.Jpg, ImageFormat.Png };
return string.Equals(Request.QueryString["accept"], format, StringComparison.OrdinalIgnoreCase);
}

/// <summary>
Expand Down
21 changes: 14 additions & 7 deletions MediaBrowser.Controller/Drawing/ImageProcessingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ImageProcessingOptions()
public string ForegroundLayer { get; set; }
public bool RequiresAutoOrientation { get; set; }

public bool HasDefaultOptions(string originalImagePath)
private bool HasDefaultOptions(string originalImagePath)
{
return HasDefaultOptionsWithoutSize(originalImagePath) &&
!Width.HasValue &&
Expand All @@ -59,34 +59,41 @@ public bool HasDefaultOptions(string originalImagePath)
!MaxHeight.HasValue;
}

public bool HasDefaultOptions(string originalImagePath, ImageSize size)
public bool HasDefaultOptions(string originalImagePath, ImageSize? size)
{
if (!size.HasValue)
{
return HasDefaultOptions(originalImagePath);
}

if (!HasDefaultOptionsWithoutSize(originalImagePath))
{
return false;
}

if (Width.HasValue && !size.Width.Equals(Width.Value))
var sizeValue = size.Value;

if (Width.HasValue && !sizeValue.Width.Equals(Width.Value))
{
return false;
}
if (Height.HasValue && !size.Height.Equals(Height.Value))
if (Height.HasValue && !sizeValue.Height.Equals(Height.Value))
{
return false;
}
if (MaxWidth.HasValue && size.Width > MaxWidth.Value)
if (MaxWidth.HasValue && sizeValue.Width > MaxWidth.Value)
{
return false;
}
if (MaxHeight.HasValue && size.Height > MaxHeight.Value)
if (MaxHeight.HasValue && sizeValue.Height > MaxHeight.Value)
{
return false;
}

return true;
}

public bool HasDefaultOptionsWithoutSize(string originalImagePath)
private bool HasDefaultOptionsWithoutSize(string originalImagePath)
{
return (Quality >= 90) &&
IsFormatSupported(originalImagePath) &&
Expand Down

0 comments on commit ab32b9f

Please sign in to comment.