Better way to average around points
I'm not into C++, but here's a suggestion for what it is worth.  Create a class that encapsulates the array, let's call it Array. Define an operator[] method that gives you the value of the element of the array at (x,y);  the function returns 0.0 if the coordinates are invalid.  Another method, n_points, returns the number of valid points around (x,y) - ie. 4, 6 or 9. Then you can compute the average using the following function, ignoring where the point (x,y) is located in the array:

float average(const Array &amp;array, int x, int y)
{
    return
        (array[x-1,y-1] + array[x,y-1] + array[x+1,y-1] +
         array[x-1,y]   + array[x,y]   + array[x+1,y] + 
         array[x-1,y+1] + array[x,y+1] + array[x+1,y+1])
        / array.n_points(x,y);
}


That won't be more efficient (the reverse probably), but it would be more readable.