Browse Source

Fixing loop bounds

Former-commit-id: 39cc79e0bf142eb2d0752e4add8be9ee4fbc9bb6
af/merge-core
James South 12 years ago
parent
commit
9e64ebaf7b
  1. 65
      src/ImageProcessor/Imaging/EdgeDetection/ConvolutionFilter.cs
  2. 24
      src/ImageProcessor/Imaging/EdgeDetection/CostellaEdgeFilter.cs
  3. 2
      src/ImageProcessorConsole/Program.cs
  4. 3
      src/ImageProcessorConsole/images/output/Bikesgray.png
  5. 3
      src/ImageProcessorConsole/images/output/Valve_original_(1).PNG
  6. 3
      src/ImageProcessorConsole/images/output/circle.png
  7. 3
      src/ImageProcessorConsole/images/output/circle2.png
  8. 1
      src/ImageProcessorConsole/images/output/monster.png.REMOVED.git-id
  9. 1
      src/ImageProcessorConsole/images/output/night-bridge.png.REMOVED.git-id

65
src/ImageProcessor/Imaging/EdgeDetection/ConvolutionFilter.cs

@ -83,21 +83,19 @@ namespace ImageProcessor.Imaging.EdgeDetection
try
{
double[,] horizontalFilter = this.edgeFilter.HorizontalGradientOperator;
double[,] verticallFilter = this.edgeFilter.VerticalGradientOperator;
double[,] verticalFilter = this.edgeFilter.VerticalGradientOperator;
int filterXOffset = (horizontalFilter.GetLength(1) - 1) / 2;
int filterYOffset = (horizontalFilter.GetLength(0) - 1) / 2;
int maxWidth = width - filterXOffset;
int maxHeight = height - filterYOffset;
int kernelLength = horizontalFilter.GetLength(0);
int radius = kernelLength >> 1;
using (FastBitmap sourceBitmap = new FastBitmap(input))
{
using (FastBitmap destinationBitmap = new FastBitmap(destination))
{
// Loop through the pixels.
for (int y = filterYOffset; y < maxHeight; y++)
for (int y = 0; y < height; y++)
{
for (int x = filterXOffset; x < maxWidth; x++)
for (int x = 0; x < width; x++)
{
double rX = 0;
double rY = 0;
@ -107,23 +105,50 @@ namespace ImageProcessor.Imaging.EdgeDetection
double bY = 0;
// Apply each matrix multiplier to the color components for each pixel.
for (int fy = -1; fy < filterYOffset; fy++)
for (int fy = 0; fy < kernelLength; fy++)
{
for (int fx = -1; fx < filterXOffset; fx++)
{
Color currentColor = sourceBitmap.GetPixel(x + fx, y + fy);
double r = currentColor.R;
double g = currentColor.G;
double b = currentColor.B;
int fyr = fy - radius;
int offsetY = y + fyr;
rX += horizontalFilter[fy + 1, fx + 1] * r;
rY += verticallFilter[fy + 1, fx + 1] * r;
// Skip the current row
if (offsetY < 0)
{
continue;
}
gX += horizontalFilter[fy + 1, fx + 1] * g;
gY += verticallFilter[fy + 1, fx + 1] * g;
// Outwith the current bounds so break.
if (offsetY >= height)
{
break;
}
bX += horizontalFilter[fy + 1, fx + 1] * b;
bY += verticallFilter[fy + 1, fx + 1] * b;
for (int fx = 0; fx < kernelLength; fx++)
{
int fxr = fx - radius;
int offsetX = x + fxr;
// Skip the column
if (offsetX < 0)
{
continue;
}
if (offsetX < width)
{
Color currentColor = sourceBitmap.GetPixel(offsetX, offsetY);
double r = currentColor.R;
double g = currentColor.G;
double b = currentColor.B;
rX += horizontalFilter[fy, fx] * r;
rY += verticalFilter[fy, fx] * r;
gX += horizontalFilter[fy, fx] * g;
gY += verticalFilter[fy, fx] * g;
bX += horizontalFilter[fy, fx] * b;
bY += verticalFilter[fy, fx] * b;
}
}
}

24
src/ImageProcessor/Imaging/EdgeDetection/CostellaEdgeFilter.cs

@ -24,12 +24,12 @@ namespace ImageProcessor.Imaging.EdgeDetection
{
get
{
return new double[,]
{ { -1, -1, -1, -1, -1, },
{ -1, -1, -1, -1, -1, },
{ -1, -1, 24, -1, -1, },
{ -1, -1, -1, -1, -1, },
{ -1, -1, -1, -1, -1 }, };
//return new double[,]
//{ { -1, -1, -1, -1, -1, },
// { -1, -1, -1, -1, -1, },
// { -1, -1, 24, -1, -1, },
// { -1, -1, -1, -1, -1, },
// { -1, -1, -1, -1, -1 }, };
return new double[,]
{
{ -1, -3, 0, 3, 1 },
@ -48,12 +48,12 @@ namespace ImageProcessor.Imaging.EdgeDetection
{
get
{
return new double[,]
{ { -1, -1, -1, -1, -1, },
{ -1, -1, -1, -1, -1, },
{ -1, -1, 24, -1, -1, },
{ -1, -1, -1, -1, -1, },
{ -1, -1, -1, -1, -1 }, };
//return new double[,]
//{ { -1, -1, -1, -1, -1, },
// { -1, -1, -1, -1, -1, },
// { -1, -1, 24, -1, -1, },
// { -1, -1, -1, -1, -1, },
// { -1, -1, -1, -1, -1 }, };
return new double[,]
{
{ 1, 1, 1, 1, 1 },

2
src/ImageProcessorConsole/Program.cs

@ -77,7 +77,7 @@ namespace ImageProcessorConsole
//.Constrain(size)
//.ReplaceColor(Color.FromArgb(255, 1, 107, 165), Color.FromArgb(255, 1, 165, 13), 80)
.Resize(layer)
.DetectEdges(new CostellaEdgeFilter(), false)
.DetectEdges(new PrewittEdgeFilter(), false)
//.Filter(MatrixFilters.Comic)
//.Filter(MatrixFilters.HiSatch)
//.Pixelate(8)

3
src/ImageProcessorConsole/images/output/Bikesgray.png

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:7eb6df312d356346d0d7f5d55c657a4a3a08f11d293c93f56447137a49190d0f
size 17319

3
src/ImageProcessorConsole/images/output/Valve_original_(1).PNG

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:8d74259c087adb38c335a3162c56f1c3ae344e098f2471d79d702d80a95907a3
size 4066

3
src/ImageProcessorConsole/images/output/circle.png

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:343794bc824674c81cd8ba90acd3bb690d43c84d363e3375e8ee42207867f977
size 7011

3
src/ImageProcessorConsole/images/output/circle2.png

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:662cd66659e03b0ca18e62d2e9bdc69e62e082fad9a0316348db031f67092497
size 4407

1
src/ImageProcessorConsole/images/output/monster.png.REMOVED.git-id

@ -1 +0,0 @@
ef1fdff21802713450cf2f81321c0605962ec6f5

1
src/ImageProcessorConsole/images/output/night-bridge.png.REMOVED.git-id

@ -1 +0,0 @@
fad7e5225f13c9d35d9e877c93e2bb44da7404c0
Loading…
Cancel
Save