// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; using System.Collections.Generic; using System.Linq; using System.Text; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using SixLabors.Primitives; namespace SixLabors.ImageSharp.Tests { internal class FakeImageOperationsProvider : IImageProcessingContextFactory { private List ImageOperators = new List(); public bool HasCreated(Image source) where TPixel : struct, IPixel { return Created(source).Any(); } public IEnumerable> Created(Image source) where TPixel : struct, IPixel { return this.ImageOperators.OfType>() .Where(x => x.source == source); } public IEnumerable.AppliedOpperation> AppliedOperations(Image source) where TPixel : struct, IPixel { return Created(source) .SelectMany(x => x.applied); } public IInternalImageProcessingContext CreateImageProcessingContext(Image source, bool mutate) where TPixel : struct, IPixel { var op = new FakeImageOperations(source, mutate); this.ImageOperators.Add(op); return op; } public class FakeImageOperations : IInternalImageProcessingContext where TPixel : struct, IPixel { public Image source; public List applied = new List(); public bool mutate; public FakeImageOperations(Image source, bool mutate) { this.mutate = mutate; if (mutate) { this.source = source; } else { this.source = source?.Clone(); } } public Image Apply() { return source; } public IImageProcessingContext ApplyProcessor(IImageProcessor processor, Rectangle rectangle) { applied.Add(new AppliedOpperation { Processor = processor, Rectangle = rectangle }); return this; } public IImageProcessingContext ApplyProcessor(IImageProcessor processor) { applied.Add(new AppliedOpperation { Processor = processor }); return this; } public MemoryManager MemoryManager => this.source.GetConfiguration().MemoryManager; public struct AppliedOpperation { public Rectangle? Rectangle { get; set; } public IImageProcessor Processor { get; set; } } } } }