Originally Posted by
choloboy
... what approach would best...
I wouldn't worry about finding the "best" approach. Just think about
an approach.
For example:
Suppose you have a two-dimension array of ints named pixelArray.
Declare an int named numRows and set it equal to pixelArray.length;
Declare an int named numCols and set it equal to pixelArray[0].length;
First of all make sure that numRows and numCols are both even numbers.
(Use numRows%2 and numCols%2 to determine that.)
Then make nested loops that go through the rows and columns of pixelArray two at a time:
for (int i = 0; i < numRows; /* What goes here to make it step through the rows two at a time? */) {
for (int j = 0; j < numCols; /* What goes here to make it step through the columns two at a time? */) {
//
// Do the Good Stuff here
//
}
}
Now, what is the Good Stuff?
Declare an int variable named sum, or some such thing.
In the innermost loop, calculate it by adding the values of the
elements of the 2x2 sub array:
pixelArray[i][j] +
pixelArray[i][j+1] +
pixelArray[i+1][j] +
pixelArray[i+1][j+1]
Declare an int variable named avg or some such thing
Set the value of avg equal to Math.round(sum/4.0f) to get the
int value closest to the average value of the four pixels.
Copy this average value back into the pixelArray elements
that were used to calculate the sum.
Cheers!
Z