// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using System.Collections.Concurrent; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Tests; /// /// A test image file. /// public sealed class TestFile { /// /// The test file cache. /// private static readonly ConcurrentDictionary Cache = new(); /// /// The "Formats" directory, as lazy value /// // ReSharper disable once InconsistentNaming private static readonly Lazy InputImagesDirectoryValue = new(() => TestEnvironment.InputImagesDirectoryFullPath); /// /// The image bytes /// private byte[] bytes; /// /// Initializes a new instance of the class. /// /// The file. private TestFile(string file) => this.FullPath = file; /// /// Gets the image bytes. /// public byte[] Bytes => this.bytes ??= File.ReadAllBytes(this.FullPath); /// /// Gets the full path to file. /// public string FullPath { get; } /// /// Gets the file name. /// public string FileName => Path.GetFileName(this.FullPath); /// /// Gets the file name without extension. /// public string FileNameWithoutExtension => Path.GetFileNameWithoutExtension(this.FullPath); /// /// Gets the input image directory. /// private static string InputImagesDirectory => InputImagesDirectoryValue.Value; /// /// Gets the full qualified path to the input test file. /// /// /// The file path. /// /// /// The . /// public static string GetInputFileFullPath(string file) => Path.Combine(InputImagesDirectory, file).Replace('\\', Path.DirectorySeparatorChar); /// /// Creates a new test file or returns one from the cache. /// /// The file path. /// /// The . /// public static TestFile Create(string file) => Cache.GetOrAdd(file, (string fileName) => new TestFile(GetInputFileFullPath(fileName))); /// /// Gets the file name. /// /// The value. /// /// The . /// public string GetFileName(object value) => $"{this.FileNameWithoutExtension}-{value}{Path.GetExtension(this.FullPath)}"; /// /// Gets the file name without extension. /// /// The value. /// /// The . /// public string GetFileNameWithoutExtension(object value) => this.FileNameWithoutExtension + "-" + value; /// /// Creates a new image. /// /// /// The . /// public Image CreateRgba32Image() => Image.Load(this.Bytes); /// /// Creates a new image. /// /// The image decoder. /// /// The . /// public Image CreateRgba32Image(IImageDecoder decoder) => this.CreateRgba32Image(decoder, new DecoderOptions()); /// /// Creates a new image. /// /// The image decoder. /// The general decoder options. /// /// The . /// public Image CreateRgba32Image(IImageDecoder decoder, DecoderOptions options) { using MemoryStream stream = new(this.Bytes); return decoder.Decode(options, stream); } }