Calculate a binned histogram, works for 8bpp grayscale and 16bpp grayscale.