From b04b6298e028bb8caddea92fca8acdb5f3471912 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 8 Jul 2016 19:49:39 +1000 Subject: [PATCH] IPixelAcessor Former-commit-id: 5b04e85fc18ebe98bd122766c58379a1666d0cbb Former-commit-id: 6e626d6a6e55aa75922a3c1aeeb4094390b4e185 Former-commit-id: f5293704eec3ece4aceaebff57218bce383d02e8 --- src/ImageProcessorCore/Bootstrapper.cs | 4 +-- .../Formats/Bmp/BmpEncoderCore.cs | 9 ++++-- src/ImageProcessorCore/IImageBase.cs | 2 +- src/ImageProcessorCore/Image.cs | 2 +- src/ImageProcessorCore/ImageBase.cs | 2 +- src/ImageProcessorCore/ImageFrame.cs | 2 +- .../PixelAccessor/Bgra32PixelAccessor.cs | 6 ++-- .../PixelAccessor/IPixelAccessor.cs | 28 +++++++++++-------- .../Samplers/Processors/ResizeProcessor.cs | 10 +++---- .../Image/GetSetPixel.cs | 2 +- 10 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/ImageProcessorCore/Bootstrapper.cs b/src/ImageProcessorCore/Bootstrapper.cs index 1601f8d892..36e53ce51b 100644 --- a/src/ImageProcessorCore/Bootstrapper.cs +++ b/src/ImageProcessorCore/Bootstrapper.cs @@ -73,13 +73,13 @@ namespace ImageProcessorCore /// The type of pixel data. /// The image /// The - public IPixelAccessor GetPixelAccessor(IImageBase image) + public IPixelAccessor GetPixelAccessor(IImageBase image) where TPackedVector : IPackedVector, new() { Type packed = typeof(TPackedVector); if (this.pixelAccessors.ContainsKey(packed)) { - return this.pixelAccessors[packed].Invoke(image); + return (IPixelAccessor)this.pixelAccessors[packed].Invoke(image); } throw new NotSupportedException($"PixelAccessor cannot be loaded for {packed}:"); diff --git a/src/ImageProcessorCore/Formats/Bmp/BmpEncoderCore.cs b/src/ImageProcessorCore/Formats/Bmp/BmpEncoderCore.cs index a0d321ecb9..fae92c0f12 100644 --- a/src/ImageProcessorCore/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageProcessorCore/Formats/Bmp/BmpEncoderCore.cs @@ -137,7 +137,7 @@ namespace ImageProcessorCore.Formats amount = 4 - amount; } - using (IPixelAccessor pixels = image.Lock()) + using (IPixelAccessor pixels = image.Lock()) { switch (this.bmpBitsPerPixel) { @@ -155,10 +155,12 @@ namespace ImageProcessorCore.Formats /// /// Writes the 32bit color palette to the stream. /// + /// The type of pixels contained within the image. /// The containing the stream to write to. /// The containing pixel data. /// The amount to pad each row by. - private void Write32bit(EndianBinaryWriter writer, IPixelAccessor pixels, int amount) + private void Write32bit(EndianBinaryWriter writer, IPixelAccessor pixels, int amount) + where TPackedVector : IPackedVector, new() { for (int y = pixels.Height - 1; y >= 0; y--) { @@ -183,7 +185,8 @@ namespace ImageProcessorCore.Formats /// The containing the stream to write to. /// The containing pixel data. /// The amount to pad each row by. - private void Write24bit(EndianBinaryWriter writer, IPixelAccessor pixels, int amount) + private void Write24bit(EndianBinaryWriter writer, IPixelAccessor pixels, int amount) + where TPackedVector : IPackedVector, new() { for (int y = pixels.Height - 1; y >= 0; y--) { diff --git a/src/ImageProcessorCore/IImageBase.cs b/src/ImageProcessorCore/IImageBase.cs index cc383cb258..5a61f76701 100644 --- a/src/ImageProcessorCore/IImageBase.cs +++ b/src/ImageProcessorCore/IImageBase.cs @@ -7,7 +7,7 @@ namespace ImageProcessorCore { TPackedVector[] Pixels { get; } void ClonePixels(int width, int height, TPackedVector[] pixels); - IPixelAccessor Lock(); + IPixelAccessor Lock(); void SetPixels(int width, int height, TPackedVector[] pixels); } diff --git a/src/ImageProcessorCore/Image.cs b/src/ImageProcessorCore/Image.cs index caa333e9c0..a672f6a250 100644 --- a/src/ImageProcessorCore/Image.cs +++ b/src/ImageProcessorCore/Image.cs @@ -184,7 +184,7 @@ namespace ImageProcessorCore public IImageFormat CurrentImageFormat { get; internal set; } /// - public override IPixelAccessor Lock() + public override IPixelAccessor Lock() { return Bootstrapper.Instance.GetPixelAccessor(this); } diff --git a/src/ImageProcessorCore/ImageBase.cs b/src/ImageProcessorCore/ImageBase.cs index 15fd9b5d3f..0c0bb6fac6 100644 --- a/src/ImageProcessorCore/ImageBase.cs +++ b/src/ImageProcessorCore/ImageBase.cs @@ -196,6 +196,6 @@ namespace ImageProcessorCore /// /// /// The - public abstract IPixelAccessor Lock(); + public abstract IPixelAccessor Lock(); } } diff --git a/src/ImageProcessorCore/ImageFrame.cs b/src/ImageProcessorCore/ImageFrame.cs index d170778e41..75d755f5fc 100644 --- a/src/ImageProcessorCore/ImageFrame.cs +++ b/src/ImageProcessorCore/ImageFrame.cs @@ -33,7 +33,7 @@ namespace ImageProcessorCore } /// - public override IPixelAccessor Lock() + public override IPixelAccessor Lock() { return Bootstrapper.Instance.GetPixelAccessor(this); } diff --git a/src/ImageProcessorCore/PixelAccessor/Bgra32PixelAccessor.cs b/src/ImageProcessorCore/PixelAccessor/Bgra32PixelAccessor.cs index e5f090e417..f9fa7005bc 100644 --- a/src/ImageProcessorCore/PixelAccessor/Bgra32PixelAccessor.cs +++ b/src/ImageProcessorCore/PixelAccessor/Bgra32PixelAccessor.cs @@ -15,7 +15,7 @@ namespace ImageProcessorCore /// The image data is always stored in format, where the blue, green, red, and /// alpha values are 8 bit unsigned bytes. /// - public sealed unsafe class Bgra32PixelAccessor : IPixelAccessor + public sealed unsafe class Bgra32PixelAccessor : IPixelAccessor { /// @@ -90,7 +90,7 @@ namespace ImageProcessorCore /// than zero and smaller than the width of the pixel. /// /// The at the specified position. - public IPackedVector this[int x, int y] + public Bgra32 this[int x, int y] { get { @@ -121,7 +121,7 @@ namespace ImageProcessorCore throw new ArgumentOutOfRangeException(nameof(y), "Value cannot be less than zero or greater than the bitmap height."); } #endif - *(this.pixelsBase + ((y * this.Width) + x)) = (Bgra32)value; + *(this.pixelsBase + ((y * this.Width) + x)) = value; } } diff --git a/src/ImageProcessorCore/PixelAccessor/IPixelAccessor.cs b/src/ImageProcessorCore/PixelAccessor/IPixelAccessor.cs index cf3c2cf539..7eac9fd030 100644 --- a/src/ImageProcessorCore/PixelAccessor/IPixelAccessor.cs +++ b/src/ImageProcessorCore/PixelAccessor/IPixelAccessor.cs @@ -10,18 +10,9 @@ namespace ImageProcessorCore /// /// Encapsulates properties to provides per-pixel access to an images pixels. /// - public interface IPixelAccessor : IDisposable + public interface IPixelAccessor : IPixelAccessor + where TPackedVector : IPackedVector, new() { - /// - /// Gets the width of the image in pixels. - /// - int Width { get; } - - /// - /// Gets the height of the image in pixels. - /// - int Height { get; } - /// /// Gets or sets the pixel at the specified position. /// @@ -34,10 +25,23 @@ namespace ImageProcessorCore /// than zero and smaller than the width of the pixel. /// /// The at the specified position. - IPackedVector this[int x, int y] + TPackedVector this[int x, int y] { get; set; } } + + public interface IPixelAccessor : IDisposable + { + /// + /// Gets the width of the image in pixels. + /// + int Width { get; } + + /// + /// Gets the height of the image in pixels. + /// + int Height { get; } + } } diff --git a/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs b/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs index ce713797b5..11b397d745 100644 --- a/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs +++ b/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs @@ -78,8 +78,8 @@ namespace ImageProcessorCore.Processors float widthFactor = sourceRectangle.Width / (float)targetRectangle.Width; float heightFactor = sourceRectangle.Height / (float)targetRectangle.Height; - using (IPixelAccessor sourcePixels = source.Lock()) - using (IPixelAccessor targetPixels = target.Lock()) + using (IPixelAccessor sourcePixels = source.Lock()) + using (IPixelAccessor targetPixels = target.Lock()) { Parallel.For( startY, @@ -115,9 +115,9 @@ namespace ImageProcessorCore.Processors // First process the columns. Since we are not using multiple threads startY and endY // are the upper and lower bounds of the source rectangle. Image firstPass = new Image(target.Width, source.Height); - using (IPixelAccessor sourcePixels = source.Lock()) - using (IPixelAccessor firstPassPixels = firstPass.Lock()) - using (IPixelAccessor targetPixels = target.Lock()) + using (IPixelAccessor sourcePixels = source.Lock()) + using (IPixelAccessor firstPassPixels = firstPass.Lock()) + using (IPixelAccessor targetPixels = target.Lock()) { Parallel.For( 0, diff --git a/tests/ImageProcessorCore.Benchmarks/Image/GetSetPixel.cs b/tests/ImageProcessorCore.Benchmarks/Image/GetSetPixel.cs index 98bcf80493..d900377cda 100644 --- a/tests/ImageProcessorCore.Benchmarks/Image/GetSetPixel.cs +++ b/tests/ImageProcessorCore.Benchmarks/Image/GetSetPixel.cs @@ -24,7 +24,7 @@ public Bgra32 ResizeCore() { Image image = new Image(400, 400); - using (IPixelAccessor imagePixels = image.Lock()) + using (IPixelAccessor imagePixels = image.Lock()) { imagePixels[200, 200] = new Bgra32(1, 1, 1, 1); return (Bgra32)imagePixels[200, 200];