generated from ZipCodeCore/OldTexasCode2
-
Notifications
You must be signed in to change notification settings - Fork 11
/
FilterExample.java
75 lines (61 loc) · 2.08 KB
/
FilterExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Mike Scott
// 2d array manipulation examples
//import
import java.awt.Color;
public class FilterExample
{
/*
*pre: image != null, image.length > 1, image[0].length > 1
* image is a rectangular matrix, neighberhoodSize > 0
*post: return a smoothed version of image
*/
public Color[][] smooth(Color[][] image, int neighberhoodSize)
{ //check precondition
assert image != null && image.length > 1 && image[0].length > 1
&& ( neighberhoodSize > 0 ) && rectangularMatrix( image )
: "Violation of precondition: smooth";
Color[][] result = new Color[image.length][image[0].length];
for(int row = 0; row < image.length; row++)
{ for(int col = 0; col < image[0].length; col++)
{ result[row][col] = aveOfNeighbors(image, row, col, neighberhoodSize);
}
}
return result;
}
// helper method that determines the average color of a neighberhood
// around a particular cell.
private Color aveOfNeighbors(Color[][] image, int row, int col, int neighberhoodSize)
{ int numNeighbors = 0;
int red = 0;
int green = 0;
int blue = 0;
for(int r = row - neighberhoodSize; r <= row + neighberhoodSize; r++)
{ for(int c = col - neighberhoodSize; c <= col + neighberhoodSize; c++)
{ if( inBounds( image, r, c ) )
{ numNeighbors++;
red += image[r][c].getRed();
green += image[r][c].getGreen();
blue += image[r][c].getBlue();
}
}
}
assert numNeighbors > 0;
return new Color( red / numNeighbors, green / numNeighbors, blue / numNeighbors );
}
//helper method to determine if given coordinates are in bounds
private boolean inBounds(Color[][] image, int row, int col)
{ return (row >= 0) && (row <= image.length) && (col >= 0)
&& (col < image[0].length);
}
//private method to ensure mat is rectangular
private boolean rectangularMatrix( Color[][] mat )
{ boolean isRectangular = true;
int row = 1;
final int COLUMNS = mat[0].length;
while( isRectangular && row < mat.length )
{ isRectangular = ( mat[row].length == COLUMNS );
row++;
}
return isRectangular;
}
}