// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Encapsulates the properties required to crop an image. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Imaging { using System; /// /// Encapsulates the properties required to crop an image. /// public class CropLayer { /// /// Initializes a new instance of the class. /// /// /// The left coordinate of the crop layer. /// /// /// The top coordinate of the crop layer. /// /// /// The right coordinate of the crop layer. /// /// /// The bottom coordinate of the crop layer. /// /// /// The . /// /// /// If the is set to CropMode.Percentage then the four coordinates /// become percentages to reduce from each edge. /// public CropLayer(float left, float top, float right, float bottom, CropMode cropMode = CropMode.Percentage) { if (left < 0 || top < 0 || right < 0 || bottom < 0) { throw new ArgumentOutOfRangeException(); } this.Left = left; this.Top = top; this.Right = right; this.Bottom = bottom; this.CropMode = cropMode; } /// /// Gets or sets the left coordinate of the crop layer. /// public float Left { get; set; } /// /// Gets or sets the top coordinate of the crop layer. /// public float Top { get; set; } /// /// Gets or sets the right coordinate of the crop layer. /// public float Right { get; set; } /// /// Gets or sets the bottom coordinate of the crop layer. /// public float Bottom { get; set; } /// /// Gets or sets the . /// public CropMode CropMode { get; set; } /// /// Determines whether the specified , is /// equal to this instance. /// /// /// The to compare with this instance. /// /// /// true if the specified is equal to /// this instance; otherwise, false. /// public override bool Equals(object obj) { CropLayer cropLayer = obj as CropLayer; if (cropLayer == null) { return false; } // Define the tolerance for variation in their values return Math.Abs(this.Top - cropLayer.Top) <= Math.Abs(this.Top * .0001) && Math.Abs(this.Right - cropLayer.Right) <= Math.Abs(this.Right * .0001) && Math.Abs(this.Bottom - cropLayer.Bottom) <= Math.Abs(this.Bottom * .0001) && Math.Abs(this.Left - cropLayer.Left) <= Math.Abs(this.Left * .0001) && this.CropMode.Equals(cropLayer.CropMode); } /// /// Serves as a hash function for a particular type. /// /// /// A hash code for the current . /// public override int GetHashCode() { unchecked { int hashCode = this.Left.GetHashCode(); hashCode = (hashCode * 397) ^ this.Top.GetHashCode(); hashCode = (hashCode * 397) ^ this.Right.GetHashCode(); hashCode = (hashCode * 397) ^ this.Bottom.GetHashCode(); hashCode = (hashCode * 397) ^ (int)this.CropMode; return hashCode; } } } }