Advertisement
Guest User

Untitled

a guest
Jul 26th, 2013
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. // Count the number of Alive pixels around the pixel at (x, y).
  2. int countNeighbours(CImg<unsigned char> &image, int x, int y)
  3. {
  4.     int count = 0;
  5.     for (int x0 = x -1; x0 < x + 2; x0++)
  6.     {
  7.         if (x < 0 || x >= image.width())
  8.             continue; // Skip this loop if out of range.
  9.         for (int y0 = y -1; y0 < y +2; y0++)
  10.         {
  11.             if (y < 0 || y >= image.height())
  12.                 continue; // Skip this loop if out of range.
  13.  
  14.             if(!(x0 == x && y0 == y))
  15.             { // Check we're not on current pixel.
  16.                 count += image(x0, y0, 0);
  17.             }
  18.         }
  19.     }
  20.     // Count number of living around pixel and return.
  21.     // Divide by 255 because each time we increment count using image(x,y,0)
  22.     // if it is white the number returned will be 255.
  23.     // Do 8 - (ans) because white = dead.
  24.     return(8 - (count/255));
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement