home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / ppm / ppmspread.c < prev    next >
C/C++ Source or Header  |  1993-12-17  |  3KB  |  128 lines

  1.  
  2. /*********************************************************************/
  3. /* ppmspread -  randomly displace a PPM's pixels by a certain amount */
  4. /* Frank Neumann, October 1993                                       */
  5. /* V1.1 16.11.1993                                                   */
  6. /*                                                                   */
  7. /* version history:                                                  */
  8. /* V1.0 12.10.1993    first version                                  */
  9. /* V1.1 16.11.1993    Rewritten to be NetPBM.programming conforming  */
  10. /*********************************************************************/
  11.  
  12. #include "ppm.h"
  13.  
  14. /* global variables */
  15. #ifdef AMIGA
  16. static char *version = "$VER: ppmspread 1.1 (16.11.93)"; /* Amiga version identification */
  17. #endif
  18.  
  19. /**************************/
  20. /* start of main function */
  21. /**************************/
  22. int main(argc, argv)
  23. int argc;
  24. char *argv[];
  25. {
  26.     FILE* ifp;
  27.     int argn, rows, cols, i, j;
  28.     int xdis, ydis, xnew, ynew;
  29.     pixel **destarray, **srcarray;
  30.     pixel *pP, *pP2;
  31.     pixval maxval;
  32.     pixval r1, g1, b1;
  33.     int amount;
  34.     time_t timenow;
  35.     char *usage = "amount [ppmfile]\n        amount: # of pixels to displace a pixel by at most\n";
  36.  
  37.     /* parse in 'default' parameters */
  38.     ppm_init(&argc, argv);
  39.  
  40.     argn = 1;
  41.  
  42.     /* parse in amount & seed */
  43.     if (argn == argc)
  44.         pm_usage(usage);
  45.     if (sscanf(argv[argn], "%d", &amount) != 1)
  46.         pm_usage(usage);
  47.     if (amount < 0)
  48.         pm_error("amount should be a positive number");
  49.     ++argn;
  50.  
  51.     /* parse in filename (if present, stdin otherwise) */
  52.     if (argn != argc)
  53.     {
  54.         ifp = pm_openr(argv[argn]);
  55.         ++argn;
  56.     }
  57.     else
  58.         ifp = stdin;
  59.  
  60.     if (argn != argc)
  61.         pm_usage(usage);
  62.  
  63.     /* read entire picture into buffer */
  64.     srcarray = ppm_readppm(ifp, &cols, &rows, &maxval);
  65.  
  66.     /* allocate an entire picture buffer for dest picture */
  67.     destarray = ppm_allocarray(cols, rows);
  68.  
  69.     /* clear out the buffer */
  70.     for (i=0; i < rows; i++)
  71.         bzero(destarray[i], cols * sizeof(pixel));
  72.  
  73.     /* set seed for random number generator */
  74.     /* get time of day to feed the random number generator */
  75.     timenow = time(NULL);
  76.     srandom(timenow);
  77.  
  78.     /* start displacing pixels */
  79.     for (i = 0; i < rows; i++)
  80.     {
  81.         pP = srcarray[i];
  82.  
  83.         for (j = 0; j < cols; j++)
  84.         {
  85.             xdis = (random() % (amount+1)) - ((amount+1) / 2);
  86.             ydis = (random() % (amount+1)) - ((amount+1) / 2);
  87.  
  88.             xnew = j + xdis;
  89.             ynew = i + ydis;
  90.  
  91.             /* only set the displaced pixel if it's within the bounds of the image */
  92.             if (xnew >= 0 && xnew < cols && ynew >= 0 && ynew < rows)
  93.             {
  94.                 /* displacing a pixel is accomplished by swapping it with another */
  95.                 /* pixel in its vicinity - so, first store other pixel's RGB      */
  96.                 pP2 = srcarray[ynew] + xnew;
  97.                 r1 = PPM_GETR(*pP2);
  98.                 g1 = PPM_GETG(*pP2);
  99.                 b1 = PPM_GETB(*pP2);
  100.                 /* set second pixel to new value */
  101.                 pP2 = destarray[ynew] + xnew;
  102.                 PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP));
  103.  
  104.                 /* now, set first pixel to (old) value of second */
  105.                 pP2 = destarray[i] + j;
  106.                 PPM_ASSIGN(*pP2, r1, g1, b1);
  107.             }
  108.             else
  109.             {
  110.                 /* displaced pixel is out of bounds; leave the old pixel there */
  111.                 pP2 = destarray[i] + j;
  112.                 PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP));
  113.             }
  114.             pP++;
  115.         }
  116.     }
  117.  
  118.     /* write out entire dest picture in one go */
  119.     ppm_writeppm(stdout, destarray, cols, rows, maxval, 0);
  120.  
  121.     pm_close(ifp);
  122.     ppm_freearray(srcarray, rows);
  123.     ppm_freearray(destarray, rows);
  124.  
  125.     exit(0);
  126. }
  127.  
  128.