// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Processing.Processors { using System; /// /// Provides methods to allow the cropping of an image to preserve areas of highest /// entropy. /// /// The pixel format. public class EntropyCropProcessor : ImageProcessor where TColor : struct, IPackedPixel, IEquatable { /// /// Initializes a new instance of the class. /// /// The threshold to split the image. Must be between 0 and 1. /// /// is less than 0 or is greater than 1. /// public EntropyCropProcessor(float threshold) { Guard.MustBeBetweenOrEqualTo(threshold, 0, 1, nameof(threshold)); this.Value = threshold; } /// /// Gets the threshold value. /// public float Value { get; } /// protected override void OnApply(ImageBase source, Rectangle sourceRectangle) { using (ImageBase temp = new Image(source)) { // Detect the edges. new SobelProcessor().Apply(temp, sourceRectangle); // Apply threshold binarization filter. new BinaryThresholdProcessor(this.Value).Apply(temp, sourceRectangle); // Search for the first white pixels Rectangle rectangle = ImageMaths.GetFilteredBoundingRectangle(temp, 0); if (rectangle == sourceRectangle) { return; } new CropProcessor(rectangle).Apply(source, sourceRectangle); } } } }