// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageProcessorCore { using System; /// /// The base class of all images. Encapsulates the basic properties and methods /// required to manipulate images. /// public abstract class ImageBase { /// /// The array of pixels. /// private float[] pixelsArray; /// /// Initializes a new instance of the class. /// protected ImageBase() { } /// /// Initializes a new instance of the class. /// /// The width of the image in pixels. /// The height of the image in pixels. /// /// Thrown if either or are less than or equal to 0. /// protected ImageBase(int width, int height) { Guard.MustBeGreaterThan(width, 0, nameof(width)); Guard.MustBeGreaterThan(height, 0, nameof(height)); this.Width = width; this.Height = height; // Assign the pointer and pixels. this.pixelsArray = new float[width * height * 4]; } /// /// Initializes a new instance of the class. /// /// /// The other to create this instance from. /// /// /// Thrown if the given is null. /// protected ImageBase(ImageBase other) { Guard.NotNull(other, nameof(other), "Other image cannot be null."); this.Width = other.Width; this.Height = other.Height; this.Quality = other.Quality; this.FrameDelay = other.FrameDelay; // Copy the pixels. this.pixelsArray = new float[this.Width * this.Height * 4]; Array.Copy(other.pixelsArray, this.pixelsArray, other.pixelsArray.Length); } /// /// Gets or sets the maximum allowable width in pixels. /// public static int MaxWidth { get; set; } = int.MaxValue; /// /// Gets or sets the maximum allowable height in pixels. /// public static int MaxHeight { get; set; } = int.MaxValue; /// /// Gets the image pixels as byte array. /// /// /// The returned array has a length of Width * Height * 4 bytes /// and stores the red, the green, the blue, and the alpha value for /// each pixel in this order. /// public float[] Pixels => this.pixelsArray; /// /// Gets the width in pixels. /// public int Width { get; private set; } /// /// Gets the height in pixels. /// public int Height { get; private set; } /// /// Gets the pixel ratio made up of the width and height. /// public double PixelRatio => (double)this.Width / this.Height; /// /// Gets the representing the bounds of the image. /// public Rectangle Bounds => new Rectangle(0, 0, this.Width, this.Height); /// /// Gets or sets th quality of the image. This affects the output quality of lossy image formats. /// public int Quality { get; set; } /// /// Gets or sets the frame delay for animated images. /// If not 0, this field specifies the number of hundredths (1/100) of a second to /// wait before continuing with the processing of the Data Stream. /// The clock starts ticking immediately after the graphic is rendered. /// public int FrameDelay { get; set; } /// /// Sets the pixel array of the image to the given value. /// /// The new width of the image. Must be greater than zero. /// The new height of the image. Must be greater than zero. /// /// The array with colors. Must be a multiple of four times the width and height. /// /// /// Thrown if either or are less than or equal to 0. /// /// /// Thrown if the length is not equal to Width * Height * 4. /// public void SetPixels(int width, int height, float[] pixels) { if (width <= 0) { throw new ArgumentOutOfRangeException(nameof(width), "Width must be greater than or equals than zero."); } if (height <= 0) { throw new ArgumentOutOfRangeException(nameof(height), "Height must be greater than or equal than zero."); } if (pixels.Length != width * height * 4) { throw new ArgumentException("Pixel array must have the length of Width * Height * 4."); } this.Width = width; this.Height = height; this.pixelsArray = pixels; } /// /// Sets the pixel array of the image to the given value, creating a copy of /// the original pixels. /// /// The new width of the image. Must be greater than zero. /// The new height of the image. Must be greater than zero. /// /// The array with colors. Must be a multiple of four times the width and height. /// /// /// Thrown if either or are less than or equal to 0. /// /// /// Thrown if the length is not equal to Width * Height * 4. /// public void ClonePixels(int width, int height, float[] pixels) { if (width <= 0) { throw new ArgumentOutOfRangeException(nameof(width), "Width must be greater than or equals than zero."); } if (height <= 0) { throw new ArgumentOutOfRangeException(nameof(height), "Height must be greater than or equal than zero."); } if (pixels.Length != width * height * 4) { throw new ArgumentException("Pixel array must have the length of Width * Height * 4."); } this.Width = width; this.Height = height; // Copy the pixels. this.pixelsArray = new float[pixels.Length]; Array.Copy(pixels, this.pixelsArray, pixels.Length); } /// /// Locks the image providing access to the pixels. /// /// It is imperative that the accessor is correctly disposed off after use. /// /// /// The public PixelAccessor Lock() { return new PixelAccessor(this); } } }