Browse Source

Avoid activator

Former-commit-id: 369a59330765de72829d5b2e6eec83cdb18a3f03
Former-commit-id: 8611d5b3aebe7f155a80598eeff86c707c614e64
Former-commit-id: 862e506ccc0485d075b4afa2c514c6619823f1f5
af/merge-core
James Jackson-South 10 years ago
parent
commit
1d563ac393
  1. 48
      src/ImageProcessorCore/Bootstrapper.cs
  2. 8
      tests/ImageProcessorCore.Benchmarks/Samplers/Resize.cs

48
src/ImageProcessorCore/Bootstrapper.cs

@ -8,7 +8,6 @@ namespace ImageProcessorCore
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Text;
using ImageProcessorCore.Formats; using ImageProcessorCore.Formats;
@ -28,7 +27,7 @@ namespace ImageProcessorCore
/// </summary> /// </summary>
private readonly List<IImageFormat> imageFormats; private readonly List<IImageFormat> imageFormats;
private readonly Dictionary<Type, Type> pixelAccessors; private readonly Dictionary<Type, Func<IImageBase, IPixelAccessor>> pixelAccessors;
/// <summary> /// <summary>
/// Prevents a default instance of the <see cref="Bootstrapper"/> class from being created. /// Prevents a default instance of the <see cref="Bootstrapper"/> class from being created.
@ -43,9 +42,9 @@ namespace ImageProcessorCore
//new GifFormat() //new GifFormat()
}; };
this.pixelAccessors = new Dictionary<Type, Type> this.pixelAccessors = new Dictionary<Type, Func<IImageBase, IPixelAccessor>>
{ {
{ typeof(Bgra32), typeof(Bgra32PixelAccessor) } { typeof(Bgra32), i=> new Bgra32PixelAccessor(i) }
}; };
} }
@ -80,47 +79,10 @@ namespace ImageProcessorCore
Type packed = typeof(TPackedVector); Type packed = typeof(TPackedVector);
if (this.pixelAccessors.ContainsKey(packed)) if (this.pixelAccessors.ContainsKey(packed))
{ {
// TODO: Double check this. It should work... return this.pixelAccessors[packed].Invoke(image);
//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);
} }
throw new NotSupportedException(stringBuilder.ToString()); throw new NotSupportedException($"PixelAccessor cannot be loaded for {packed}:");
} }
///// <summary>
///// Gets an instance of the correct <see cref="IPixelAccessor"/> for the packed vector.
///// </summary>
///// <typeparam name="TPackedVector">The type of pixel data.</typeparam>
///// <param name="image">The image</param>
///// <returns>The <see cref="IPixelAccessor"/></returns>
//public IPixelAccessor GetPixelAccessor<TPackedVector>(ImageFrame<TPackedVector> 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());
//}
} }
} }

8
tests/ImageProcessorCore.Benchmarks/Samplers/Resize.cs

@ -11,9 +11,9 @@
[Benchmark(Baseline = true, Description = "System.Drawing Resize")] [Benchmark(Baseline = true, Description = "System.Drawing Resize")]
public Size ResizeSystemDrawing() 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)) using (Graphics graphics = Graphics.FromImage(destination))
{ {
@ -31,8 +31,8 @@
[Benchmark(Description = "ImageProcessorCore Resize")] [Benchmark(Description = "ImageProcessorCore Resize")]
public CoreSize ResizeCore() public CoreSize ResizeCore()
{ {
Image<Bgra32> image = new Image<Bgra32>(400, 400); Image<Bgra32> image = new Image<Bgra32>(2000, 2000);
image.Resize(100, 100); image.Resize(400, 400);
return new CoreSize(image.Width, image.Height); return new CoreSize(image.Width, image.Height);
} }
} }

Loading…
Cancel
Save