//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageProcessorCore
{
using System;
using System.Diagnostics;
///
/// The base class of all images. Encapsulates the basic properties and methods required to manipulate
/// images in different pixel formats.
///
/// The pixel format.
/// The packed format. long, float.
[DebuggerDisplay("Image: {Width}x{Height}")]
public abstract class ImageBase : IImageBase
where T : IPackedVector
where TP : struct
{
///
/// 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;
this.Pixels = new T[width * height];
}
///
/// 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.Pixels = new T[this.Width * this.Height];
Array.Copy(other.Pixels, this.Pixels, other.Pixels.Length);
}
///
public int MaxWidth { get; set; } = int.MaxValue;
///
public int MaxHeight { get; set; } = int.MaxValue;
///
public T[] Pixels { get; private set; }
///
public int Width { get; private set; }
///
public int Height { get; private set; }
///
public double PixelRatio => (double)this.Width / this.Height;
///
public Rectangle Bounds => new Rectangle(0, 0, this.Width, this.Height);
///
public int Quality { get; set; }
///
public int FrameDelay { get; set; }
///
public void SetPixels(int width, int height, T[] 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)
{
throw new ArgumentException("Pixel array must have the length of Width * Height.");
}
this.Width = width;
this.Height = height;
this.Pixels = pixels;
}
///
public void ClonePixels(int width, int height, T[] 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)
{
throw new ArgumentException("Pixel array must have the length of Width * Height.");
}
this.Width = width;
this.Height = height;
// Copy the pixels.
this.Pixels = new T[pixels.Length];
Array.Copy(pixels, this.Pixels, pixels.Length);
}
///
public abstract IPixelAccessor Lock();
}
}