// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Tests { using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using ImageSharp.Formats; using ImageSharp.PixelFormats; /// /// A test image file. /// public class TestFile { /// /// The test file cache. /// private static readonly ConcurrentDictionary Cache = new ConcurrentDictionary(); /// /// The "Formats" directory, as lazy value /// // ReSharper disable once InconsistentNaming private static readonly Lazy inputImagesDirectory = new Lazy(() => TestEnvironment.InputImagesDirectoryFullPath); /// /// The image (lazy initialized value) /// private Image image; /// /// The image bytes /// private byte[] bytes; /// /// The file. /// private readonly string file; /// /// Initializes a new instance of the class. /// /// The file. private TestFile(string file) { this.file = file; } /// /// Gets the image bytes. /// public byte[] Bytes => this.bytes ?? (this.bytes = File.ReadAllBytes(this.file)); /// /// The file name. /// public string FilePath => this.file; /// /// The file name. /// public string FileName => Path.GetFileName(this.file); /// /// The file name without extension. /// public string FileNameWithoutExtension => Path.GetFileNameWithoutExtension(this.file); /// /// Gets the image with lazy initialization. /// private Image Image => this.image ?? (this.image = ImageSharp.Image.Load(this.Bytes)); /// /// private static string InputImagesDirectory => inputImagesDirectory.Value; /// /// Gets the full qualified path to the input test file. /// /// /// The file path. /// /// /// The . /// public static string GetInputFileFullPath(string file) { return Path.Combine(InputImagesDirectory, file); } /// /// Creates a new test file or returns one from the cache. /// /// The file path. /// /// The . /// public static TestFile Create(string file) { return Cache.GetOrAdd(file, (string fileName) => new TestFile(GetInputFileFullPath(file))); } /// /// Gets the file name. /// /// The value. /// /// The . /// public string GetFileName(object value) { return $"{this.FileNameWithoutExtension}-{value}{Path.GetExtension(this.file)}"; } /// /// Gets the file name without extension. /// /// The value. /// /// The . /// public string GetFileNameWithoutExtension(object value) { return this.FileNameWithoutExtension + "-" + value; } /// /// Creates a new image. /// /// /// The . /// public Image CreateImage() { return this.Image.Clone(); } /// /// Creates a new image. /// /// /// The . /// public Image CreateImage(IImageDecoder decoder) { return ImageSharp.Image.Load(this.Image.Configuration, this.Bytes, decoder); } } }