From 0cdc209c324fcf8bd93cc0c31d8eb24a3dc4189b Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 15 Jul 2016 11:52:36 +1000 Subject: [PATCH] Now faster than System.Drawing. Former-commit-id: 14ac44a018e4525cf65c19e3864840b4d6934981 Former-commit-id: b6d60bd3b96b352577dfd53a6cdd8f0d237d6a91 Former-commit-id: a0c275d2ed132c6d87f01c2469fd1b7d69e9dc62 --- src/ImageProcessorCore/PackedVector/Bgra32.cs | 122 +----------------- .../PackedVector/IPackedVector.cs | 48 ------- .../Samplers/Processors/ResizeProcessor.cs | 34 ++--- 3 files changed, 14 insertions(+), 190 deletions(-) diff --git a/src/ImageProcessorCore/PackedVector/Bgra32.cs b/src/ImageProcessorCore/PackedVector/Bgra32.cs index 3372cb283..e519a8742 100644 --- a/src/ImageProcessorCore/PackedVector/Bgra32.cs +++ b/src/ImageProcessorCore/PackedVector/Bgra32.cs @@ -10,7 +10,7 @@ namespace ImageProcessorCore using System.Runtime.InteropServices; /// - /// Packed vector type containing four 8-bit unsigned normalized values ranging from 0 to 1. + /// Packed vector type containing four 8-bit unsigned normalized values ranging from 0 to 255. /// [StructLayout(LayoutKind.Explicit)] public struct Bgra32 : IPackedVector, IEquatable @@ -124,78 +124,6 @@ namespace ImageProcessorCore return left.packedValue != right.packedValue; } - /// - public void Add(Bgra32 value) - { - this.B = Clamp(this.B + value.B); - this.G = Clamp(this.G + value.G); - this.R = Clamp(this.R + value.R); - this.A = Clamp(this.A + value.A); - } - - /// - public void Subtract(Bgra32 value) - { - this.B = Clamp(this.B - value.B); - this.G = Clamp(this.G - value.G); - this.R = Clamp(this.R - value.R); - this.A = Clamp(this.A - value.A); - } - - /// - public void Multiply(Bgra32 value) - { - this.B = Clamp(this.B * value.B); - this.G = Clamp(this.G * value.G); - this.R = Clamp(this.R * value.R); - this.A = Clamp(this.A * value.A); - } - - /// - public void Multiply(float value) - { - this.B = Clamp(this.B * value); - this.G = Clamp(this.G * value); - this.R = Clamp(this.R * value); - this.A = Clamp(this.A * value); - } - - /// - public void Multiply(double value) - { - this.B = Clamp(this.B * value); - this.G = Clamp(this.G * value); - this.R = Clamp(this.R * value); - this.A = Clamp(this.A * value); - } - - /// - public void Divide(Bgra32 value) - { - this.B = Clamp((float)this.B / value.B); - this.G = Clamp((float)this.G / value.G); - this.R = Clamp((float)this.R / value.R); - this.A = Clamp((float)this.A / value.A); - } - - /// - public void Divide(float value) - { - this.B = Clamp(this.B / value); - this.G = Clamp(this.G / value); - this.R = Clamp(this.R / value); - this.A = Clamp(this.A / value); - } - - /// - public void Divide(double value) - { - this.B = Clamp(this.B / value); - this.G = Clamp(this.G / value); - this.R = Clamp(this.R / value); - this.A = Clamp(this.A / value); - } - /// public uint PackedValue() { @@ -260,52 +188,6 @@ namespace ImageProcessorCore return this.GetHashCode(this); } - /// - /// Clamps the value to the acceptable byte range. - /// - /// The value. - /// - /// The . - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static byte Clamp(float value) - { - if (value > 255) - { - return 255; - } - - if (value < 0) - { - return 0; - } - - return (byte)value; - } - - /// - /// Clamps the value to the acceptable byte range. - /// - /// The value. - /// - /// The . - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static byte Clamp(double value) - { - if (value > 255) - { - return 255; - } - - if (value < 0) - { - return 0; - } - - return (byte)value; - } - /// /// Returns the hash code for this instance. /// @@ -321,4 +203,4 @@ namespace ImageProcessorCore return packed.packedValue.GetHashCode(); } } -} +} \ No newline at end of file diff --git a/src/ImageProcessorCore/PackedVector/IPackedVector.cs b/src/ImageProcessorCore/PackedVector/IPackedVector.cs index f130bd515..a528559ef 100644 --- a/src/ImageProcessorCore/PackedVector/IPackedVector.cs +++ b/src/ImageProcessorCore/PackedVector/IPackedVector.cs @@ -24,54 +24,6 @@ namespace ImageProcessorCore /// The . /// TP PackedValue(); - - /// - /// Adds the given to the current instance. - /// - /// The packed vector to add. - void Add(T value); - - /// - /// Subtracts the given from the current instance. - /// - /// The packed vector to subtract. - void Subtract(T value); - - /// - /// Multiplies the given current instance by given the . - /// - /// The packed vector to multiply by. - void Multiply(T value); - - /// - /// Multiplies the given current instance by given the value. - /// - /// The value to multiply by. - void Multiply(float value); - - /// - /// Multiplies the given current instance by given the value. - /// - /// The value to multiply by. - void Multiply(double value); - - /// - /// Divides the given current instance by given the . - /// - /// The packed vector to divide by. - void Divide(T value); - - /// - /// Divides the given current instance by given the value. - /// - /// The value to divide by. - void Divide(float value); - - /// - /// Divides the given current instance by given the value. - /// - /// The value to divide by. - void Divide(double value); } /// diff --git a/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs b/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs index 0fd63e4b3..0dcafd7b0 100644 --- a/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs +++ b/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs @@ -134,21 +134,14 @@ namespace ImageProcessorCore.Processors Weight[] horizontalValues = this.HorizontalWeights[offsetX].Values; // Destination color components - T destination = default(T); + Vector4 destination = Vector4.Zero; for (int i = 0; i < sum; i++) { Weight xw = horizontalValues[i]; int originX = xw.Index; - T sourceColor = sourcePixels[originX, y]; - - //Color sourceColor = compand - // ? Color.Expand(sourcePixels[originX, y]) - // : sourcePixels[originX, y]; - //destination += sourceColor * xw.Value; - - sourceColor.Multiply(xw.Value); - destination.Add(sourceColor); + Vector4 sourceColor = sourcePixels[originX, y].ToVector4(); + destination += sourceColor * (float)xw.Value; } //if (compand) @@ -156,7 +149,9 @@ namespace ImageProcessorCore.Processors // destination = Color.Compress(destination); //} - firstPassPixels[x, y] = destination; + T d = default(T); + d.PackVector(destination); + firstPassPixels[x, y] = d; } } }); @@ -177,21 +172,14 @@ namespace ImageProcessorCore.Processors for (int x = 0; x < width; x++) { // Destination color components - T destination = default(T); + Vector4 destination = Vector4.Zero; for (int i = 0; i < sum; i++) { Weight yw = verticalValues[i]; int originY = yw.Index; - T sourceColor = firstPassPixels[x, originY]; - - //Color sourceColor = compand - // ? Color.Expand(firstPassPixels[x, originY]) - // : firstPassPixels[x, originY]; - //destination += sourceColor * yw.Value; - - sourceColor.Multiply(yw.Value); - destination.Add(sourceColor); + Vector4 sourceColor = firstPassPixels[x, originY].ToVector4(); + destination += sourceColor * (float)yw.Value; } //if (compand) @@ -199,7 +187,9 @@ namespace ImageProcessorCore.Processors // destination = Color.Compress(destination); //} - targetPixels[x, y] = destination; + T d = default(T); + d.PackVector(destination); + targetPixels[x, y] = d; } }