//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageSharp.Tests
{
using System;
using System.Collections.Concurrent;
public abstract partial class TestImageProvider
where TColor : struct, IPixel
{
private class FileProvider : TestImageProvider
{
// Need PixelTypes in the dictionary key, because result images of TestImageProvider.FileProvider
// are shared between PixelTypes.Color & PixelTypes.StandardImageClass
private class Key : Tuple
{
public Key(PixelTypes item1, string item2)
: base(item1, item2)
{
}
}
private static ConcurrentDictionary> cache =
new ConcurrentDictionary>();
private string filePath;
public FileProvider(string filePath)
{
this.filePath = filePath;
}
public override string SourceFileOrDescription => this.filePath;
public override Image GetImage()
{
Key key = new Key(this.PixelType, this.filePath);
Image cachedImage = cache.GetOrAdd(
key,
fn =>
{
TestFile testFile = TestFile.Create(this.filePath);
return this.Factory.CreateImage(testFile.Bytes);
});
return this.Factory.CreateImage(cachedImage);
}
}
}
}