using DlibDotNet;
using DlibDotNet.Extensions;
using System.Drawing;
using View_by_Distance.Shared.Models.Stateless;
namespace View_by_Distance.FaceRecognitionDotNet.Models;
///
/// Represents a image data. This class cannot be inherited.
///
public sealed class Image : DisposableObject
{
#region Fields
#endregion
#region Constructors
internal Image(MatrixBase matrix, Mode mode)
{
Matrix = matrix;
Mode = mode;
}
#endregion
#region Properties
///
/// Gets the height of the image.
///
/// This object is disposed.
public int Height
{
get
{
ThrowIfDisposed();
return Matrix.Rows;
}
}
internal MatrixBase Matrix { get; private set; }
internal Mode Mode { get; }
///
/// Gets the width of the image.
///
/// This object is disposed.
public int Width
{
get
{
ThrowIfDisposed();
return Matrix.Columns;
}
}
#endregion
#region Methods
///
/// Saves this to the specified file.
///
/// A string that contains the name of the file to which to save this .
/// The for this .
/// is null.
/// This object is disposed.
public void Save(string fileName, ImageFormat format)
{
if (fileName == null)
throw new NullReferenceException(nameof(fileName));
ThrowIfDisposed();
string? directory = Path.GetDirectoryName(fileName);
if (!Directory.Exists(directory) && !string.IsNullOrWhiteSpace(directory))
_ = Directory.CreateDirectory(directory);
switch (format)
{
case ImageFormat.Bmp:
DlibDotNet.Dlib.SaveBmp(Matrix, fileName);
break;
case ImageFormat.Jpeg:
DlibDotNet.Dlib.SaveJpeg(Matrix, fileName);
break;
case ImageFormat.Png:
DlibDotNet.Dlib.SavePng(Matrix, fileName);
break;
default:
break;
}
}
///
/// Converts this to a GDI+ .
///
/// A that represents the converted .
/// This object is disposed.
/// A Greyscale image is not supported.
public Bitmap ToBitmap()
{
ThrowIfDisposed();
if (Mode == Mode.Greyscale)
throw new NotSupportedException();
return ((Matrix)Matrix).ToBitmap();
}
#region Overrides
///
/// Releases all unmanaged resources.
///
protected override void DisposeUnmanaged()
{
base.DisposeUnmanaged();
Matrix?.Dispose();
}
#endregion
#endregion
}