mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.3 KiB
72 lines
2.3 KiB
// <copyright file="FileProvider.cs" company="James Jackson-South">
|
|
// Copyright (c) James Jackson-South and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
// </copyright>
|
|
|
|
namespace ImageSharp.Tests
|
|
{
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using Xunit.Abstractions;
|
|
|
|
public abstract partial class TestImageProvider<TColor>
|
|
where TColor : struct, IPixel<TColor>
|
|
{
|
|
private class FileProvider : TestImageProvider<TColor>, IXunitSerializable
|
|
{
|
|
// Need PixelTypes in the dictionary key, because result images of TestImageProvider<TColor>.FileProvider
|
|
// are shared between PixelTypes.Color & PixelTypes.StandardImageClass
|
|
private class Key : Tuple<PixelTypes, string>
|
|
{
|
|
public Key(PixelTypes item1, string item2)
|
|
: base(item1, item2)
|
|
{
|
|
}
|
|
}
|
|
|
|
private static ConcurrentDictionary<Key, Image<TColor>> cache =
|
|
new ConcurrentDictionary<Key, Image<TColor>>();
|
|
|
|
private string filePath;
|
|
|
|
public FileProvider(string filePath)
|
|
{
|
|
this.filePath = filePath;
|
|
}
|
|
|
|
public FileProvider()
|
|
{
|
|
}
|
|
|
|
public override string SourceFileOrDescription => this.filePath;
|
|
|
|
public override Image<TColor> GetImage()
|
|
{
|
|
Key key = new Key(this.PixelType, this.filePath);
|
|
|
|
Image<TColor> cachedImage = cache.GetOrAdd(
|
|
key,
|
|
fn =>
|
|
{
|
|
TestFile testFile = TestFile.Create(this.filePath);
|
|
return this.Factory.CreateImage(testFile.Bytes);
|
|
});
|
|
|
|
return this.Factory.CreateImage(cachedImage);
|
|
}
|
|
|
|
public override void Deserialize(IXunitSerializationInfo info)
|
|
{
|
|
this.filePath = info.GetValue<string>("path");
|
|
|
|
base.Deserialize(info); // must be called last
|
|
}
|
|
|
|
public override void Serialize(IXunitSerializationInfo info)
|
|
{
|
|
base.Serialize(info);
|
|
info.AddValue("path", this.filePath);
|
|
}
|
|
}
|
|
}
|
|
}
|