//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageProcessorCore
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using ImageProcessorCore.Formats;
///
/// Provides initialization code which allows extending the library.
///
public class Bootstrapper
{
///
/// A new instance Initializes a new instance of the class.
/// with lazy initialization.
///
private static readonly Lazy Lazy = new Lazy(() => new Bootstrapper());
///
/// The default list of supported
///
private readonly List imageFormats;
private readonly Dictionary pixelAccessors;
///
/// Prevents a default instance of the class from being created.
///
private Bootstrapper()
{
this.imageFormats = new List
{
new BmpFormat(),
new JpegFormat(),
new PngFormat(),
new GifFormat()
};
this.pixelAccessors = new Dictionary
{
{ typeof(Bgra32), typeof(Bgra32PixelAccessor) }
};
}
///
/// Gets the current bootstrapper instance.
///
public static Bootstrapper Instance = Lazy.Value;
///
/// Gets the list of supported
///
public IReadOnlyCollection ImageFormats => new ReadOnlyCollection(this.imageFormats);
///
/// Adds a new to the collection of supported image formats.
///
/// The new format to add.
public void AddImageFormat(IImageFormat format)
{
this.imageFormats.Add(format);
}
///
/// Gets an instance of the correct for the packed vector.
///
/// The type of pixel data.
/// The image
/// The
public IPixelAccessor GetPixelAccessor(Image image)
where TPackedVector : IPackedVector
{
Type packed = typeof(TPackedVector);
if (!this.pixelAccessors.ContainsKey(packed))
{
// TODO: Double check this. It should work...
return (IPixelAccessor)Activator.CreateInstance(this.pixelAccessors[packed], image);
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("PixelAccessor cannot be loaded. Available accessors:");
foreach (Type value in this.pixelAccessors.Values)
{
stringBuilder.AppendLine("-" + value.Name);
}
throw new NotSupportedException(stringBuilder.ToString());
}
///
/// Gets an instance of the correct for the packed vector.
///
/// The type of pixel data.
/// The image
/// The
public IPixelAccessor GetPixelAccessor(ImageFrame image)
where TPackedVector : IPackedVector
{
Type packed = typeof(TPackedVector);
if (!this.pixelAccessors.ContainsKey(packed))
{
return (IPixelAccessor)Activator.CreateInstance(this.pixelAccessors[packed], image);
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("PixelAccessor cannot be loaded. Available accessors:");
foreach (Type value in this.pixelAccessors.Values)
{
stringBuilder.AppendLine("-" + value.Name);
}
throw new NotSupportedException(stringBuilder.ToString());
}
}
}