Counting pixels in a picture

I’ve got a picture with only two colours: white an black.
I need to count how many black pixels are there in the picture.

(both OSX and Windows)
Is there any system API to do this?
Have you any special fast algorithm?


Could be tricky… Xojo can tend to mess about with the image when it opens.
You might find you get some greys that you didnt expect.

Using native code, its generallly held that the fastest method of looking at an image pixel by pixel is the rgbsurface property

dim x,y as integer
dim h,w as integer
dim rgb as rgbsurface
dim cnt as integer =0
h= img.height -1
w= img.width-1

rgb = img.rgbsurface
for x = 0 to w
for y = 0 to h
cnt = cnt + rgb.pixel(x,y).red =0   
// or maybe <20 if you want to catch greys too..

It may go faster if you nest y inside x… havent tried it myself.

If the pixel values could be translated into a memoryblock it might be faster to traverse one long list rather than nested loops.

Thanks Jeff for your hints.
I’ll apply them and comment it if something interesting happens.