// // 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.Reflection; using System.Threading.Tasks; using 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(Color), i=> new ColorPixelAccessor(i) } }; } /// /// Gets the current bootstrapper instance. /// public static Bootstrapper Instance = Lazy.Value; /// /// Gets the collection of supported /// public IReadOnlyCollection ImageFormats => new ReadOnlyCollection(this.imageFormats); /// /// Gets the collection of supported pixel accessors /// public IReadOnlyDictionary> PixelAccessors => new ReadOnlyDictionary>(this.pixelAccessors); /// /// Gets or sets the global parallel options for processing tasks in parallel. /// public ParallelOptions ParallelOptions { get; set; } = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; /// /// Adds a new to the collection of supported image formats. /// /// The new format to add. public void AddImageFormat(IImageFormat format) { this.imageFormats.Add(format); } /// /// Adds a pixel accessor for the given pixel format. /// /// The packed format type, must implement /// The function to return a new instance of the pixel accessor. public void AddPixelAccessor(Type packedType, Func initializer) { if (!typeof(IPackedVector).GetTypeInfo().IsAssignableFrom(packedType.GetTypeInfo())) { throw new ArgumentException($"Type {packedType} must implement {nameof(IPackedVector)}"); } this.pixelAccessors.Add(packedType, initializer); } /// /// Gets an instance of the correct for the packed vector. /// /// The type of pixel data. /// The packed format. long, float. /// The image /// The public IPixelAccessor GetPixelAccessor(IImageBase image) where T : IPackedVector where TP : struct { Type packed = typeof(T); if (this.pixelAccessors.ContainsKey(packed)) { return (IPixelAccessor)this.pixelAccessors[packed].Invoke(image); } throw new NotSupportedException($"PixelAccessor cannot be loaded for {packed}:"); } } }