Browse Source

Fix cache key resolution

pull/2180/head
James Jackson-South 4 years ago
parent
commit
8d50996ce7
  1. 2
      src/ImageSharp/Formats/Webp/WebpDecoderCore.cs
  2. 50
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs

2
src/ImageSharp/Formats/Webp/WebpDecoderCore.cs

@ -279,7 +279,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
/// <param name="features">The webp features.</param> /// <param name="features">The webp features.</param>
private void ParseOptionalChunks(WebpFeatures features) private void ParseOptionalChunks(WebpFeatures features)
{ {
if (this.skipMetadata || (features.ExifProfile == false && features.XmpMetaData == false)) if (this.skipMetadata || (!features.ExifProfile && !features.XmpMetaData))
{ {
return; return;
} }

50
tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs

@ -28,38 +28,68 @@ namespace SixLabors.ImageSharp.Tests
private readonly Dictionary<string, object> decoderParameters; private readonly Dictionary<string, object> decoderParameters;
public Key(PixelTypes pixelType, string filePath, IImageDecoder customDecoder) public Key(
PixelTypes pixelType,
string filePath,
IImageDecoder customDecoder,
DecoderOptions options,
ISpecializedDecoderOptions specialized)
{ {
Type customType = customDecoder?.GetType(); Type customType = customDecoder?.GetType();
this.commonValues = new Tuple<PixelTypes, string, Type>( this.commonValues = new Tuple<PixelTypes, string, Type>(
pixelType, pixelType,
filePath, filePath,
customType); customType);
this.decoderParameters = GetDecoderParameters(customDecoder); this.decoderParameters = GetDecoderParameters(options, specialized);
} }
private static Dictionary<string, object> GetDecoderParameters(IImageDecoder customDecoder) private static Dictionary<string, object> GetDecoderParameters(
DecoderOptions options,
ISpecializedDecoderOptions specialized)
{ {
Type type = customDecoder.GetType(); Type type = options.GetType();
var data = new Dictionary<string, object>(); var data = new Dictionary<string, object>();
while (type != null && type != typeof(object)) while (type != null && type != typeof(object))
{ {
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo p in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
foreach (PropertyInfo p in properties)
{ {
string key = $"{type.FullName}.{p.Name}"; string key = $"{type.FullName}.{p.Name}";
object value = p.GetValue(customDecoder); data[key] = p.GetValue(options);
data[key] = value;
} }
type = type.GetTypeInfo().BaseType; type = type.GetTypeInfo().BaseType;
} }
GetSpecializedDecoderParameters(data, specialized);
return data; return data;
} }
private static void GetSpecializedDecoderParameters(
Dictionary<string, object> data,
ISpecializedDecoderOptions options)
{
if (options is null)
{
return;
}
Type type = options.GetType();
while (type != null && type != typeof(object))
{
foreach (PropertyInfo p in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
string key = $"{type.FullName}.{p.Name}";
data[key] = p.GetValue(options);
}
type = type.GetTypeInfo().BaseType;
}
}
public bool Equals(Key other) public bool Equals(Key other)
{ {
if (other is null) if (other is null)
@ -165,7 +195,7 @@ namespace SixLabors.ImageSharp.Tests
return this.DecodeImage(decoder, options); return this.DecodeImage(decoder, options);
} }
var key = new Key(this.PixelType, this.FilePath, decoder); var key = new Key(this.PixelType, this.FilePath, decoder, options, null);
Image<TPixel> cachedImage = Cache.GetOrAdd(key, _ => this.DecodeImage(decoder, options)); Image<TPixel> cachedImage = Cache.GetOrAdd(key, _ => this.DecodeImage(decoder, options));
return cachedImage.Clone(this.Configuration); return cachedImage.Clone(this.Configuration);
@ -202,7 +232,7 @@ namespace SixLabors.ImageSharp.Tests
return this.DecodeImage(decoder, options); return this.DecodeImage(decoder, options);
} }
var key = new Key(this.PixelType, this.FilePath, decoder); var key = new Key(this.PixelType, this.FilePath, decoder, options.GeneralOptions, options);
Image<TPixel> cachedImage = Cache.GetOrAdd(key, _ => this.DecodeImage(decoder, options)); Image<TPixel> cachedImage = Cache.GetOrAdd(key, _ => this.DecodeImage(decoder, options));
return cachedImage.Clone(this.Configuration); return cachedImage.Clone(this.Configuration);

Loading…
Cancel
Save