1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
/* 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(int argc, char * argv []) {
FILE* ifp;
bit* prevrow;
bit* thisrow;
bit* nextrow;
bit* temprow;
bit* newrow;
int rows, cols, row;
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 );
}
|