From 1fd361a1ea06e44286c213ca1f814f49306fdc43 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sat, 19 Aug 2006 03:12:28 +0000 Subject: Create Subversion repository git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- editor/pbmlife.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 editor/pbmlife.c (limited to 'editor/pbmlife.c') diff --git a/editor/pbmlife.c b/editor/pbmlife.c new file mode 100644 index 00000000..be34cc69 --- /dev/null +++ b/editor/pbmlife.c @@ -0,0 +1,114 @@ +/* pbmlife.c - read a portable bitmap and apply Conway's rules of Life to it +** +** Copyright (C) 1988,1 1991 by Jef Poskanzer. +** +** Permission to use, copy, modify, and distribute this software and its +** documentation for any purpose and without fee is hereby granted, provided +** that the above copyright notice appear in all copies and that both that +** copyright notice and this permission notice appear in supporting +** documentation. This software is provided "as is" without express or +** implied warranty. +*/ + +#include "pbm.h" + +int +main( argc, argv ) +int argc; +char* argv[]; + { + FILE* ifp; + bit* prevrow; + bit* thisrow; + bit* nextrow; + bit* temprow; + register bit* newrow; + int rows, cols, row; + register int col, count; + int format; + + + pbm_init( &argc, argv ); + + if ( argc > 2 ) + pm_usage( "[pbmfile]" ); + + if ( argc == 2 ) + ifp = pm_openr( argv[1] ); + else + ifp = stdin; + + pbm_readpbminit( ifp, &cols, &rows, &format ); + prevrow = pbm_allocrow( cols ); + thisrow = pbm_allocrow( cols ); + nextrow = pbm_allocrow( cols ); + + pbm_writepbminit( stdout, cols, rows, 0 ); + newrow = pbm_allocrow( cols ); + + pbm_readpbmrow( ifp, nextrow, cols, format ); + + for ( row = 0; row < rows; ++row ) + { + temprow = prevrow; + prevrow = thisrow; + thisrow = nextrow; + nextrow = temprow; + if ( row < rows - 1 ) + pbm_readpbmrow( ifp, nextrow, cols, format ); + + for ( col = 0; col < cols; ++col ) + { + /* Check the neighborhood, with an unrolled double loop. */ + count = 0; + if ( row > 0 ) + { + /* upper left */ + if ( col > 0 && prevrow[col - 1] == PBM_WHITE ) + ++count; + /* upper center */ + if ( prevrow[col] == PBM_WHITE ) + ++count; + /* upper right */ + if ( col < cols - 1 && prevrow[col + 1] == PBM_WHITE ) + ++count; + } + /* left */ + if ( col > 0 && thisrow[col - 1] == PBM_WHITE ) + ++count; + /* right */ + if ( col < cols - 1 && thisrow[col + 1] == PBM_WHITE ) + ++count; + if ( row < rows - 1 ) + { + /* lower left */ + if ( col > 0 && nextrow[col - 1] == PBM_WHITE ) + ++count; + /* lower center */ + if ( nextrow[col] == PBM_WHITE ) + ++count; + /* lower right */ + if ( col < cols - 1 && nextrow[col + 1] == PBM_WHITE ) + ++count; + } + + /* And compute the new value. */ + if ( thisrow[col] == PBM_WHITE ) + if ( count == 2 || count == 3 ) + newrow[col] = PBM_WHITE; + else + newrow[col] = PBM_BLACK; + else + if ( count == 3 ) + newrow[col] = PBM_WHITE; + else + newrow[col] = PBM_BLACK; + } + pbm_writepbmrow( stdout, newrow, cols, 0 ); + } + + pm_close( ifp ); + pm_close( stdout ); + + exit( 0 ); + } -- cgit 1.4.1