This isn't right. If the image is all black, then values[0] will equal the number of pixels in the image and sum will never be 0.

In this case, I'd worry about an image that is all white.  Then values[0] through values[254] will all be zero.  And values[255] will be the number of pixels.  In that case we'll return 255 for both the max and the min.  Does that cause a divide by zero error in the shader?

If so, I'd handle that case above.