home *** CD-ROM | disk | FTP | other *** search
- /*
- * genb side percent
- * This program prints out an X11 bitmap file representing a square of
- * size 'side', in which each bit has probability 'percent' of being off.
- * 'Percent' is converted using atof(), so it can be integral or floating.
- */
- #include <stdio.h>
- #include <X11/Xos.h>
-
- extern double atof();
- extern long random();
- extern time_t time();
-
- main(argc, argv)
- int argc;
- char * argv[];
- {
- int bit[8];
- int dimension;
- register int j;
- int onrow = 0;
- register long percent;
- register int word;
-
- /* no flexibility here */
- if (argc != 3)
- { /* yecch */
- (void) fprintf(stderr, "usage: genb length-of-side percent-of-bits-off\n");
- exit(1);
- } /* yecch */
-
- /* precompute bit masks; assumes array ref is faster than 1 << j in loop */
- for (j = 0; j < 8; j++)
- bit[j] = 1 << j;
- srandom((int) time((time_t *) 0));
-
- /* this many bits on a side; X11 wants bytes; round up */
- dimension = atoi(argv[1]);
- (void) printf("#define bm_width %d\n#define bm_height %d\n",
- dimension, dimension);
- (void) printf("static char bm_bits[] = {\n");
- dimension = ((dimension * dimension) + 7) / 8;
-
- /* percent is to 2**32 - 1 as argv[2] is to 100 */
- percent = (long) ( ((double) 0x7fffffff) * (atof(argv[2]) / 100.0) );
-
- /* for each byte */
- while (dimension--)
- { /* for each byte */
-
- /* use the X11 indentation standard */
- if (onrow == 0)
- fputs(" ", stdout);
-
- /* turn on bits in byte with probability percent/100 */
- for (word = 0, j = 0; j < 8; j++)
- if (random() >= percent)
- word |= bit[j];
-
- /* print out a byte */
- (void) printf("0x%02x", word);
- if (dimension)
- fputs(", ", stdout);
-
- /* use 12 bytes/row */
- if (++onrow == 12)
- { /* new row */
- fputs("\n", stdout);
- onrow = 0;
- } /* new row */
- } /* for each byte */
-
- fputs("\n};\n", stdout);
- exit(0);
- }
-
-