|
|
|
@ -40,7 +40,7 @@ namespace ImageSharp.Processors |
|
|
|
public float[][] KernelY { get; } |
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
protected override void Apply(ImageBase<TColor, TPacked> source, Rectangle sourceRectangle, int startY, int endY) |
|
|
|
protected override void OnApply(ImageBase<TColor, TPacked> source, Rectangle sourceRectangle) |
|
|
|
{ |
|
|
|
int kernelYHeight = this.KernelY.Length; |
|
|
|
int kernelYWidth = this.KernelY[0].Length; |
|
|
|
@ -49,11 +49,11 @@ namespace ImageSharp.Processors |
|
|
|
int radiusY = kernelYHeight >> 1; |
|
|
|
int radiusX = kernelXWidth >> 1; |
|
|
|
|
|
|
|
int sourceY = sourceRectangle.Y; |
|
|
|
int sourceBottom = sourceRectangle.Bottom; |
|
|
|
int startY = sourceRectangle.Y; |
|
|
|
int endY = sourceRectangle.Bottom; |
|
|
|
int startX = sourceRectangle.X; |
|
|
|
int endX = sourceRectangle.Right; |
|
|
|
int maxY = sourceBottom - 1; |
|
|
|
int maxY = endY - 1; |
|
|
|
int maxX = endX - 1; |
|
|
|
|
|
|
|
TColor[] target = new TColor[source.Width * source.Height]; |
|
|
|
@ -66,61 +66,58 @@ namespace ImageSharp.Processors |
|
|
|
this.ParallelOptions, |
|
|
|
y => |
|
|
|
{ |
|
|
|
if (y >= sourceY && y < sourceBottom) |
|
|
|
for (int x = startX; x < endX; x++) |
|
|
|
{ |
|
|
|
for (int x = startX; x < endX; x++) |
|
|
|
float rX = 0; |
|
|
|
float gX = 0; |
|
|
|
float bX = 0; |
|
|
|
float rY = 0; |
|
|
|
float gY = 0; |
|
|
|
float bY = 0; |
|
|
|
|
|
|
|
// Apply each matrix multiplier to the color components for each pixel.
|
|
|
|
for (int fy = 0; fy < kernelYHeight; fy++) |
|
|
|
{ |
|
|
|
float rX = 0; |
|
|
|
float gX = 0; |
|
|
|
float bX = 0; |
|
|
|
float rY = 0; |
|
|
|
float gY = 0; |
|
|
|
float bY = 0; |
|
|
|
|
|
|
|
// Apply each matrix multiplier to the color components for each pixel.
|
|
|
|
for (int fy = 0; fy < kernelYHeight; fy++) |
|
|
|
int fyr = fy - radiusY; |
|
|
|
int offsetY = y + fyr; |
|
|
|
|
|
|
|
offsetY = offsetY.Clamp(0, maxY); |
|
|
|
|
|
|
|
for (int fx = 0; fx < kernelXWidth; fx++) |
|
|
|
{ |
|
|
|
int fyr = fy - radiusY; |
|
|
|
int offsetY = y + fyr; |
|
|
|
int fxr = fx - radiusX; |
|
|
|
int offsetX = x + fxr; |
|
|
|
|
|
|
|
offsetX = offsetX.Clamp(0, maxX); |
|
|
|
|
|
|
|
Vector4 currentColor = sourcePixels[offsetX, offsetY].ToVector4(); |
|
|
|
float r = currentColor.X; |
|
|
|
float g = currentColor.Y; |
|
|
|
float b = currentColor.Z; |
|
|
|
|
|
|
|
offsetY = offsetY.Clamp(0, maxY); |
|
|
|
if (fy < kernelXHeight) |
|
|
|
{ |
|
|
|
rX += this.KernelX[fy][fx] * r; |
|
|
|
gX += this.KernelX[fy][fx] * g; |
|
|
|
bX += this.KernelX[fy][fx] * b; |
|
|
|
} |
|
|
|
|
|
|
|
for (int fx = 0; fx < kernelXWidth; fx++) |
|
|
|
if (fx < kernelYWidth) |
|
|
|
{ |
|
|
|
int fxr = fx - radiusX; |
|
|
|
int offsetX = x + fxr; |
|
|
|
|
|
|
|
offsetX = offsetX.Clamp(0, maxX); |
|
|
|
|
|
|
|
Vector4 currentColor = sourcePixels[offsetX, offsetY].ToVector4(); |
|
|
|
float r = currentColor.X; |
|
|
|
float g = currentColor.Y; |
|
|
|
float b = currentColor.Z; |
|
|
|
|
|
|
|
if (fy < kernelXHeight) |
|
|
|
{ |
|
|
|
rX += this.KernelX[fy][fx] * r; |
|
|
|
gX += this.KernelX[fy][fx] * g; |
|
|
|
bX += this.KernelX[fy][fx] * b; |
|
|
|
} |
|
|
|
|
|
|
|
if (fx < kernelYWidth) |
|
|
|
{ |
|
|
|
rY += this.KernelY[fy][fx] * r; |
|
|
|
gY += this.KernelY[fy][fx] * g; |
|
|
|
bY += this.KernelY[fy][fx] * b; |
|
|
|
} |
|
|
|
rY += this.KernelY[fy][fx] * r; |
|
|
|
gY += this.KernelY[fy][fx] * g; |
|
|
|
bY += this.KernelY[fy][fx] * b; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
float red = (float)Math.Sqrt((rX * rX) + (rY * rY)); |
|
|
|
float green = (float)Math.Sqrt((gX * gX) + (gY * gY)); |
|
|
|
float blue = (float)Math.Sqrt((bX * bX) + (bY * bY)); |
|
|
|
float red = (float)Math.Sqrt((rX * rX) + (rY * rY)); |
|
|
|
float green = (float)Math.Sqrt((gX * gX) + (gY * gY)); |
|
|
|
float blue = (float)Math.Sqrt((bX * bX) + (bY * bY)); |
|
|
|
|
|
|
|
TColor packed = default(TColor); |
|
|
|
packed.PackFromVector4(new Vector4(red, green, blue, sourcePixels[x, y].ToVector4().W)); |
|
|
|
targetPixels[x, y] = packed; |
|
|
|
} |
|
|
|
TColor packed = default(TColor); |
|
|
|
packed.PackFromVector4(new Vector4(red, green, blue, sourcePixels[x, y].ToVector4().W)); |
|
|
|
targetPixels[x, y] = packed; |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|