home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmlife.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  2.7 KB  |  114 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. void
  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.     pbm_init( &argc, argv );
  31.  
  32.     if ( argc > 2 )
  33.     pm_usage( "[pbmfile]" );
  34.  
  35.     if ( argc == 2 )
  36.     ifp = pm_openr( argv[1] );
  37.     else
  38.     ifp = stdin;
  39.  
  40.     pbm_readpbminit( ifp, &cols, &rows, &format );
  41.     prevrow = pbm_allocrow( cols );
  42.     thisrow = pbm_allocrow( cols );
  43.     nextrow = pbm_allocrow( cols );
  44.  
  45.     pbm_writepbminit( stdout, cols, rows, 0 );
  46.     newrow = pbm_allocrow( cols );
  47.  
  48.     pbm_readpbmrow( ifp, nextrow, cols, format );
  49.  
  50.     for ( row = 0; row < rows; ++row )
  51.     {
  52.     temprow = prevrow;
  53.     prevrow = thisrow;
  54.     thisrow = nextrow;
  55.     nextrow = temprow;
  56.     if ( row < rows - 1 )
  57.         pbm_readpbmrow( ifp, nextrow, cols, format );
  58.  
  59.         for ( col = 0; col < cols; ++col )
  60.         {
  61.         /* Check the neighborhood, with an unrolled double loop. */
  62.         count = 0;
  63.         if ( row > 0 )
  64.         {
  65.         /* upper left */
  66.         if ( col > 0 && prevrow[col - 1] == PBM_WHITE )
  67.             ++count;
  68.         /* upper center */
  69.         if ( prevrow[col] == PBM_WHITE )
  70.             ++count;
  71.         /* upper right */
  72.         if ( col < cols - 1 && prevrow[col + 1] == PBM_WHITE )
  73.             ++count;
  74.         }
  75.         /* left */
  76.         if ( col > 0 && thisrow[col - 1] == PBM_WHITE )
  77.         ++count;
  78.         /* right */
  79.         if ( col < cols - 1 && thisrow[col + 1] == PBM_WHITE )
  80.         ++count;
  81.         if ( row < rows - 1 )
  82.         {
  83.         /* lower left */
  84.         if ( col > 0 && nextrow[col - 1] == PBM_WHITE )
  85.             ++count;
  86.         /* lower center */
  87.         if ( nextrow[col] == PBM_WHITE )
  88.             ++count;
  89.         /* lower right */
  90.         if ( col < cols - 1 && nextrow[col + 1] == PBM_WHITE )
  91.             ++count;
  92.         }
  93.  
  94.         /* And compute the new value. */
  95.         if ( thisrow[col] == PBM_WHITE )
  96.         if ( count == 2 || count == 3 )
  97.             newrow[col] = PBM_WHITE;
  98.         else
  99.             newrow[col] = PBM_BLACK;
  100.         else
  101.         if ( count == 3 )
  102.             newrow[col] = PBM_WHITE;
  103.         else
  104.             newrow[col] = PBM_BLACK;
  105.         }
  106.     pbm_writepbmrow( stdout, newrow, cols, 0 );
  107.     }
  108.  
  109.     pm_close( ifp );
  110.     pm_close( stdout );
  111.  
  112.     exit( 0 );
  113.     }
  114.