If I'm right, this looks like it's used to detect slopes or something? It's not a perfect method... and seems like it would only work in a very limited use case.
The idea looks to be pretty simple. Let's say you have a perfectly diagonal slope:
1 2 3 4 5 6 7

\ O
\
\
\
\
\
C \
 
With the 'C' part being "closed" and the 'O' part being open.
The routine looks at a 7x7 pixel square. 'avg' starts at the center point of the square... and the routine loops through the square with 'w' and 'h' represending each pixel's distance from the center.
Every time a pixel is solid... 'avg' gets pushed in the opposite direction. In the case of my simple example slope... 'avg' would get pushed very far up and to the right, into the "open space". It will get pushed equally far in both x and y because the number of solid pixels on the X and Y axis are equal.
For a shallower slope...such as this:
'avg' will get pushed more "up" than right... because there are fewer solid pixels on the left side of the square.
Lastly, avg get normalized presumably to make it a unit vector. At which point you have a reasonably decent estimate of the surface normal.