home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pbm / pbmlife.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  3KB  |  115 lines

  1. /* pbmlife.c - read a portable bitmap and apply Conway's rules of Life to it
  2. **
  3. ** Copyright (C) 1988,1 1991 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14.  
  15. int
  16. main( argc, argv )
  17. int argc;
  18. char* argv[];
  19.     {
  20.     FILE* ifp;
  21.     bit* prevrow;
  22.     bit* thisrow;
  23.     bit* nextrow;
  24.     bit* temprow;
  25.     register bit* newrow;
  26.     int rows, cols, row;
  27.     register int col, count;
  28.     int format;
  29.  
  30.  
  31.     pbm_init( &argc, argv );
  32.  
  33.     if ( argc > 2 )
  34.     pm_usage( "[pbmfile]" );
  35.  
  36.     if ( argc == 2 )
  37.     ifp = pm_openr( argv[1] );
  38.     else
  39.     ifp = stdin;
  40.  
  41.     pbm_readpbminit( ifp, &cols, &rows, &format );
  42.     prevrow = pbm_allocrow( cols );
  43.     thisrow = pbm_allocrow( cols );
  44.     nextrow = pbm_allocrow( cols );
  45.  
  46.     pbm_writepbminit( stdout, cols, rows, 0 );
  47.     newrow = pbm_allocrow( cols );
  48.  
  49.     pbm_readpbmrow( ifp, nextrow, cols, format );
  50.  
  51.     for ( row = 0; row < rows; ++row )
  52.     {
  53.     temprow = prevrow;
  54.     prevrow = thisrow;
  55.     thisrow = nextrow;
  56.     nextrow = temprow;
  57.     if ( row < rows - 1 )
  58.         pbm_readpbmrow( ifp, nextrow, cols, format );
  59.  
  60.         for ( col = 0; col < cols; ++col )
  61.         {
  62.         /* Check the neighborhood, with an unrolled double loop. */
  63.         count = 0;
  64.         if ( row > 0 )
  65.         {
  66.         /* upper left */
  67.         if ( col > 0 && prevrow[col - 1] == PBM_WHITE )
  68.             ++count;
  69.         /* upper center */
  70.         if ( prevrow[col] == PBM_WHITE )
  71.             ++count;
  72.         /* upper right */
  73.         if ( col < cols - 1 && prevrow[col + 1] == PBM_WHITE )
  74.             ++count;
  75.         }
  76.         /* left */
  77.         if ( col > 0 && thisrow[col - 1] == PBM_WHITE )
  78.         ++count;
  79.         /* right */
  80.         if ( col < cols - 1 && thisrow[col + 1] == PBM_WHITE )
  81.         ++count;
  82.         if ( row < rows - 1 )
  83.         {
  84.         /* lower left */
  85.         if ( col > 0 && nextrow[col - 1] == PBM_WHITE )
  86.             ++count;
  87.         /* lower center */
  88.         if ( nextrow[col] == PBM_WHITE )
  89.             ++count;
  90.         /* lower right */
  91.         if ( col < cols - 1 && nextrow[col + 1] == PBM_WHITE )
  92.             ++count;
  93.         }
  94.  
  95.         /* And compute the new value. */
  96.         if ( thisrow[col] == PBM_WHITE )
  97.         if ( count == 2 || count == 3 )
  98.             newrow[col] = PBM_WHITE;
  99.         else
  100.             newrow[col] = PBM_BLACK;
  101.         else
  102.         if ( count == 3 )
  103.             newrow[col] = PBM_WHITE;
  104.         else
  105.             newrow[col] = PBM_BLACK;
  106.         }
  107.     pbm_writepbmrow( stdout, newrow, cols, 0 );
  108.     }
  109.  
  110.     pm_close( ifp );
  111.     pm_close( stdout );
  112.  
  113.     exit( 0 );
  114.     }
  115.