I don't know what your setPixel() and getPixel() functions are, or why you're incrementing a counter variable that's apparently not used for anything. It's also unclear whether you're trying to process color images, since the comment suggests yes, but the code suggests no. If you're trying to incorporate color into a dithering process, there are trivial ways that generally produce poor results, and there are more significantly more complicated ways (that I don't have direct examples for).
You might want to start here. This discusses basic noise dithers, halftoning, ordered dithers and error diffusion dithers.
The code that Cris uses is largely MATLAB, but some of the tools are from DIPimage.
MIMT (also on the FEX) has functions for doing various dithers, including some of those listed above. Other than some input/output handling and avoidance of DIPimage and IPT dependencies, I don't recall that the core code is much different. inpict = imread('lena.tif');
inpict = rgb2gray(inpict);
inpict = inpict(128:384,128:384);
Dwhite = noisedither(inpict,'white');
Dblue = noisedither(inpict,'blue');
Dordered = orddither(inpict);
Derrdiff = zfdither(inpict);
Regarding color, the simple way is to just do the comparison on each of the three RGB channels independently. This results in an 8-color image that's generally pretty terrible. The only colors you get are black, white, and 6 primary and secondary colors. You can't apply a colormap or anything. This also kind of defeats the point of error diffusion. For an example of how the simple method is done, you can look at how orddither() does it (it's just a simple loop at the end).
Dordered = orddither(inpict,16,'color');
This isn't the method used by rgb2ind() with the 'dither' flag. That requires color quantization and it requires the index assignment to be incorporated into the error diffusion process. I don't have an example for that.
Due to a difference in intent and scope, the MIMT halftoning tools are more complicated (and cumbersome) than what Cris describes in the blog. You'd be better off just using his examples.