// Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors; namespace SixLabors.ImageSharp.Tests.Processing { internal class FakeImageOperationsProvider : IImageProcessingContextFactory { private readonly List imageOperators = new List(); public bool HasCreated(Image source) where TPixel : unmanaged, IPixel { return this.Created(source).Any(); } public IEnumerable> Created(Image source) where TPixel : unmanaged, IPixel { return this.imageOperators.OfType>() .Where(x => x.Source == source); } public IEnumerable.AppliedOperation> AppliedOperations(Image source) where TPixel : unmanaged, IPixel { return this.Created(source) .SelectMany(x => x.Applied); } public IInternalImageProcessingContext CreateImageProcessingContext(Configuration configuration, Image source, bool mutate) where TPixel : unmanaged, IPixel { var op = new FakeImageOperations(configuration, source, mutate); this.imageOperators.Add(op); return op; } public class FakeImageOperations : IInternalImageProcessingContext where TPixel : unmanaged, IPixel { public FakeImageOperations(Configuration configuration, Image source, bool mutate) { this.Configuration = configuration; this.Source = mutate ? source : source?.Clone(); } public Image Source { get; } public List Applied { get; } = new List(); public Configuration Configuration { get; } public IDictionary Properties { get; } = new ConcurrentDictionary(); public Image GetResultImage() { return this.Source; } public Size GetCurrentSize() { return this.Source.Size(); } public IImageProcessingContext ApplyProcessor(IImageProcessor processor, Rectangle rectangle) { this.Applied.Add(new AppliedOperation { Rectangle = rectangle, NonGenericProcessor = processor }); return this; } public IImageProcessingContext ApplyProcessor(IImageProcessor processor) { this.Applied.Add(new AppliedOperation { NonGenericProcessor = processor }); return this; } public struct AppliedOperation { public Rectangle? Rectangle { get; set; } public IImageProcessor GenericProcessor { get; set; } public IImageProcessor NonGenericProcessor { get; set; } } } } }