// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System.Collections.Generic; using System.Linq; using SixLabors.ImageSharp.Advanced; using SixLabors.Memory; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors; 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.AppliedOperation> 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 { private bool mutate; public FakeImageOperations(Image source, bool mutate) { this.mutate = mutate; this.Source = mutate ? source : source?.Clone(); } public Image Source { get; } public List Applied { get; } = new List(); public MemoryAllocator MemoryAllocator => this.Source.GetConfiguration().MemoryAllocator; public Image Apply() { return this.Source; } public Size GetCurrentSize() { return this.Source.Size(); } public IImageProcessingContext ApplyProcessor(IImageProcessor processor, Rectangle rectangle) { this.Applied.Add(new AppliedOperation { Processor = processor, Rectangle = rectangle }); return this; } public IImageProcessingContext ApplyProcessor(IImageProcessor processor) { this.Applied.Add(new AppliedOperation { Processor = processor }); return this; } public struct AppliedOperation { public Rectangle? Rectangle { get; set; } public IImageProcessor Processor { get; set; } } } } }