w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

pbmlife.c
Go to the documentation of this file.
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
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 
45 
47  newrow = pbm_allocrow( cols );
48 
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 )
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  }
#define count(a)
Definition: aptex-macros.h:781
int col
Definition: gsftopk.c:443
void exit()
void pm_usage(char *usage)
Definition: libpbm1.c:343
void pbm_init(int *argcP, argv)
Definition: libpbm1.c:332
FILE * pm_openr(char *name)
Definition: libpbm1.c:600
void pbm_readpbminit(FILE *file, int *colsP, int *rowsP, int *formatP)
Definition: libpbm2.c:62
void pbm_readpbmrow(FILE *file, bit *bitrow, int cols, int format)
Definition: libpbm2.c:82
void pbm_writepbminit(FILE *file, int cols, int rows, int forceplain)
Definition: libpbm3.c:22
void pbm_writepbmrow(FILE *file, bit *bitrow, int cols, int forceplain)
Definition: libpbm3.c:95
#define pbm_allocrow(cols)
Definition: pbm.h:36
unsigned char bit
Definition: pbm.h:9
#define PBM_BLACK
Definition: pbm.h:11
#define PBM_WHITE
Definition: pbm.h:10
static int rows
Definition: pbmclean.c:15
static int format
Definition: pbmclean.c:15
#define thisrow
Definition: pbmclean.c:18
void nextrow()
int main(int argc, argv)
Definition: pbmlife.c:16
static int cols
Definition: pbmmask.c:21
#define pm_close(file)
Definition: png22pnm.c:120
static int row
Definition: ps2pk.c:587
#define FILE
Definition: t1stdio.h:34
static int prevrow
Definition: tex4ht.c:1078
FILE * ifp
Definition: t1asm.c:88
#define argv
Definition: xmain.c:270
#define argc
Definition: xmain.c:269