From aaaacf12ec153b8725b87b8cdd1a1995d20ee169 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 8 Jul 2016 10:25:28 +1000 Subject: [PATCH] Avoid activator Former-commit-id: 369a59330765de72829d5b2e6eec83cdb18a3f03 Former-commit-id: 8611d5b3aebe7f155a80598eeff86c707c614e64 Former-commit-id: 862e506ccc0485d075b4afa2c514c6619823f1f5 --- src/ImageProcessorCore/Bootstrapper.cs | 48 ++----------------- .../Samplers/Resize.cs | 8 ++-- 2 files changed, 9 insertions(+), 47 deletions(-) diff --git a/src/ImageProcessorCore/Bootstrapper.cs b/src/ImageProcessorCore/Bootstrapper.cs index 7a7d1892f..1601f8d89 100644 --- a/src/ImageProcessorCore/Bootstrapper.cs +++ b/src/ImageProcessorCore/Bootstrapper.cs @@ -8,7 +8,6 @@ namespace ImageProcessorCore using System; using System.Collections.Generic; using System.Collections.ObjectModel; - using System.Text; using ImageProcessorCore.Formats; @@ -28,7 +27,7 @@ namespace ImageProcessorCore /// private readonly List imageFormats; - private readonly Dictionary pixelAccessors; + private readonly Dictionary> pixelAccessors; /// /// Prevents a default instance of the class from being created. @@ -43,9 +42,9 @@ namespace ImageProcessorCore //new GifFormat() }; - this.pixelAccessors = new Dictionary + this.pixelAccessors = new Dictionary> { - { typeof(Bgra32), typeof(Bgra32PixelAccessor) } + { typeof(Bgra32), i=> new Bgra32PixelAccessor(i) } }; } @@ -80,47 +79,10 @@ namespace ImageProcessorCore Type packed = typeof(TPackedVector); if (this.pixelAccessors.ContainsKey(packed)) { - // TODO: Double check this. It should work... - - //return new Bgra32PixelAccessor(image); - 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); + return this.pixelAccessors[packed].Invoke(image); } - throw new NotSupportedException(stringBuilder.ToString()); + throw new NotSupportedException($"PixelAccessor cannot be loaded for {packed}:"); } - - ///// - ///// 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, new() - //{ - // 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()); - //} } } diff --git a/tests/ImageProcessorCore.Benchmarks/Samplers/Resize.cs b/tests/ImageProcessorCore.Benchmarks/Samplers/Resize.cs index 21a9033e0..cbc90c726 100644 --- a/tests/ImageProcessorCore.Benchmarks/Samplers/Resize.cs +++ b/tests/ImageProcessorCore.Benchmarks/Samplers/Resize.cs @@ -11,9 +11,9 @@ [Benchmark(Baseline = true, Description = "System.Drawing Resize")] public Size ResizeSystemDrawing() { - using (Bitmap source = new Bitmap(400, 400)) + using (Bitmap source = new Bitmap(2000, 2000)) { - using (Bitmap destination = new Bitmap(100, 100)) + using (Bitmap destination = new Bitmap(400, 400)) { using (Graphics graphics = Graphics.FromImage(destination)) { @@ -31,8 +31,8 @@ [Benchmark(Description = "ImageProcessorCore Resize")] public CoreSize ResizeCore() { - Image image = new Image(400, 400); - image.Resize(100, 100); + Image image = new Image(2000, 2000); + image.Resize(400, 400); return new CoreSize(image.Width, image.Height); } }