home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsm / netpbmsca / ppm / c / ppmshift < prev    next >
Encoding:
Text File  |  1994-02-21  |  3.7 KB  |  139 lines

  1.  
  2. /*********************************************************************/
  3. /* ppmshift -  shift lines of a picture left or right by x pixels    */
  4. /* Frank Neumann, October 1993                                       */
  5. /* V1.1 16.11.1993                                                   */
  6. /*                                                                   */
  7. /* version history:                                                  */
  8. /* V1.0    11.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: ppmshift 1.1 (16.11.93)"; /* Amiga version identification */
  17. #endif
  18.  
  19.  
  20. /**************************/
  21. /* start of main function */
  22. /**************************/
  23. int main(argc, argv)
  24. int argc;
  25. char *argv[];
  26. {
  27.     FILE* ifp;
  28.     time_t timenow;
  29.     int argn, rows, cols, format, i = 0, j = 0;
  30.     pixel *srcrow, *destrow;
  31.     pixel *pP = NULL, *pP2 = NULL;
  32.     pixval maxval;
  33.     int shift, nowshift;
  34.     char *usage = "shift [ppmfile]\n        shift: maximum number of pixels to shift a line by\n";
  35.  
  36.     /* parse in 'default' parameters */
  37.     ppm_init(&argc, argv);
  38.  
  39.     argn = 1;
  40.  
  41.     /* parse in shift number */
  42.     if (argn == argc)
  43.         pm_usage(usage);
  44.     if (sscanf(argv[argn], "%d", &shift) != 1)
  45.         pm_usage(usage);
  46.     if (shift < 0)
  47.         pm_error("shift factor must be 0 or more");
  48.     ++argn;
  49.  
  50.     /* parse in filename (if present, stdin otherwise) */
  51.     if (argn != argc)
  52.     {
  53.         ifp = pm_openr(argv[argn]);
  54.         ++argn;
  55.     }
  56.     else
  57.         ifp = stdin;
  58.  
  59.     if (argn != argc)
  60.         pm_usage(usage);
  61.  
  62.     /* read first data from file */
  63.     ppm_readppminit(ifp, &cols, &rows, &maxval, &format);
  64.  
  65.     if (shift > cols)
  66.     {
  67.         shift = cols;
  68.         pm_message("shift amount is larger than picture width - reset to %d", shift);
  69.     }
  70.  
  71.     /* no error checking required here, ppmlib does it all for us */
  72.     srcrow = ppm_allocrow(cols);
  73.  
  74.     /* allocate a row of pixel data for the new pixels */
  75.     destrow = ppm_allocrow(cols);
  76.  
  77.     ppm_writeppminit(stdout, cols, rows, maxval, 0);
  78.  
  79.     /* get time of day to feed the random number generator */
  80.     timenow = time(NULL);
  81.     srandom(timenow);
  82.  
  83.     /** now do the shifting **/
  84.     /* the range by which a line is shifted lays in the range from */
  85.     /* -shift/2 .. +shift/2 pixels; however, within this range it is */
  86.     /* randomly chosen */
  87.     for (i = 0; i < rows; i++)
  88.     {
  89.         if (shift != 0)
  90.             nowshift = (random() % (shift+1)) - ((shift+1) / 2);
  91.         else
  92.             nowshift = 0;
  93.  
  94.         ppm_readppmrow(ifp, srcrow, cols, maxval, format);
  95.  
  96.         pP = srcrow;
  97.         pP2 = destrow;
  98.  
  99.         /* if the shift value is less than zero, we take the original pixel line and */
  100.         /* copy it into the destination line translated to the left by x pixels. The */
  101.         /* empty pixels on the right end of the destination line are filled up with  */
  102.         /* the pixel that is the right-most in the original pixel line.              */
  103.         if (nowshift < 0)
  104.         {
  105.             pP+= abs(nowshift);
  106.             for (j = 0; j < cols; j++)
  107.             {
  108.                 PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP));
  109.                 pP2++;
  110.                 if (j < (cols+nowshift)-1)
  111.                     pP++;
  112.             }
  113.         }
  114.         /* if the shift value is 0 or positive, the first <nowshift> pixels of the */
  115.         /* destination line are filled with the first pixel from the source line,  */
  116.         /* and the rest of the source line is copied to the dest line              */
  117.         else
  118.         {
  119.             for (j = 0; j < cols; j++)
  120.             {
  121.                 PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP));
  122.                 pP2++;
  123.                 if (j >= nowshift)
  124.                     pP++;
  125.             }
  126.         }
  127.  
  128.         /* write out one line of graphic data */
  129.         ppm_writeppmrow(stdout, destrow, cols, maxval, 0);
  130.     }
  131.  
  132.     pm_close(ifp);
  133.     ppm_freerow(srcrow);
  134.     ppm_freerow(destrow);
  135.  
  136.     exit(0);
  137. }
  138.  
  139.