home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / xstatic / part01 / xranmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-20  |  1.9 KB  |  77 lines

  1. /*
  2.  * genb side percent
  3.  *  This program prints out an X11 bitmap file representing a square of
  4.  *  size 'side', in which each bit has probability 'percent' of being off.
  5.  *  'Percent' is converted using atof(), so it can be integral or floating.
  6.  */
  7. #include <stdio.h>
  8. #include <X11/Xos.h>
  9.  
  10. extern double atof();
  11. extern long random();
  12. extern time_t time();
  13.  
  14. main(argc, argv)
  15.   int    argc;
  16.   char * argv[];
  17. {
  18.   int           bit[8];
  19.   int           dimension;
  20.   register int  j;
  21.   int           onrow = 0;
  22.   register long percent;
  23.   register int  word;
  24.  
  25.   /* no flexibility here */
  26.   if (argc != 3)
  27.   { /* yecch */
  28.     (void) fprintf(stderr, "usage: genb length-of-side percent-of-bits-off\n");
  29.     exit(1);
  30.   } /* yecch */
  31.  
  32.   /* precompute bit masks; assumes array ref is faster than 1 << j in loop */
  33.   for (j = 0; j < 8; j++)
  34.     bit[j] = 1 << j;
  35.   srandom((int) time((time_t *) 0));
  36.  
  37.   /* this many bits on a side; X11 wants bytes; round up */
  38.   dimension = atoi(argv[1]);
  39.   (void) printf("#define bm_width %d\n#define bm_height %d\n",
  40.        dimension, dimension);
  41.   (void) printf("static char bm_bits[] = {\n");
  42.   dimension = ((dimension * dimension) + 7) / 8;
  43.  
  44.   /* percent is to 2**32 - 1 as argv[2] is to 100 */
  45.   percent = (long) ( ((double) 0x7fffffff) * (atof(argv[2]) / 100.0) );
  46.  
  47.   /* for each byte */
  48.   while (dimension--)
  49.   { /* for each byte */
  50.  
  51.     /* use the X11 indentation standard */
  52.     if (onrow == 0)
  53.       fputs("  ", stdout);
  54.  
  55.     /* turn on bits in byte with probability percent/100 */
  56.     for (word = 0, j = 0; j < 8; j++)
  57.       if (random() >= percent)
  58.     word |= bit[j];
  59.  
  60.     /* print out a byte */
  61.     (void) printf("0x%02x", word);
  62.     if (dimension)
  63.       fputs(", ", stdout);
  64.  
  65.     /* use 12 bytes/row */
  66.     if (++onrow == 12)
  67.     { /* new row */
  68.       fputs("\n", stdout);
  69.       onrow = 0;
  70.     } /* new row */
  71.   } /* for each byte */
  72.  
  73.   fputs("\n};\n", stdout);
  74.   exit(0);
  75. }
  76.  
  77.