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

  1.  
  2. /*********************************************************************/
  3. /* ppmntsc -  make a 'look-alike ntsc' picture from a PPM file       */
  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: ppmntsc 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, format, i = 0, j = 0;
  28.     pixel *srcrow, *destrow;
  29.     pixel *pP = NULL, *pP2 = NULL;
  30.     pixval maxval;
  31.     double dimfactor;
  32.     long longfactor;
  33.     char *usage = "dimfactor [ppmfile]\n        dimfactor: 0.0 = total blackness, 1.0 = original picture\n";
  34.  
  35.     /* parse in 'default' parameters */
  36.     ppm_init(&argc, argv);
  37.  
  38.     argn = 1;
  39.  
  40.     /* parse in dim factor */
  41.     if (argn == argc)
  42.         pm_usage(usage);
  43.     if (sscanf(argv[argn], "%lf", &dimfactor) != 1)
  44.         pm_usage(usage);
  45.     if (dimfactor < 0.0 || dimfactor > 1.0)
  46.         pm_error("dim factor must be in the range from 0.0 to 1.0 ");
  47.     ++argn;
  48.  
  49.     /* parse in filename (if present, stdin otherwise) */
  50.     if (argn != argc)
  51.     {
  52.         ifp = pm_openr(argv[argn]);
  53.         ++argn;
  54.     }
  55.     else
  56.         ifp = stdin;
  57.  
  58.     if (argn != argc)
  59.         pm_usage(usage);
  60.  
  61.     /* read first data from file */
  62.     ppm_readppminit(ifp, &cols, &rows, &maxval, &format);
  63.  
  64.     /* no error checking required here, ppmlib does it all for us */
  65.     srcrow = ppm_allocrow(cols);
  66.  
  67.     longfactor = (long)(dimfactor * 65536);
  68.  
  69.     /* allocate a row of pixel data for the new pixels */
  70.     destrow = ppm_allocrow(cols);
  71.  
  72.     ppm_writeppminit(stdout, cols, rows, maxval, 0);
  73.  
  74.     /** now do the ntsc'ing (actually very similar to ppmdim) **/
  75.     for (i = 0; i < rows; i++)
  76.     {
  77.         ppm_readppmrow(ifp, srcrow, cols, maxval, format);
  78.  
  79.         pP = srcrow;
  80.         pP2 = destrow;
  81.  
  82.         for (j = 0; j < cols; j++)
  83.         {
  84.             /* every alternating row is left in unchanged condition */
  85.             if (i & 1)
  86.             {
  87.                 PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP));
  88.             }
  89.             /* and the other lines are dimmed to the specified factor */
  90.             else
  91.             {
  92.                 PPM_ASSIGN(*pP2, (PPM_GETR(*pP) * longfactor) >> 16,
  93.                                  (PPM_GETG(*pP) * longfactor) >> 16,
  94.                                  (PPM_GETB(*pP) * longfactor) >> 16);
  95.             }
  96.             pP++;
  97.             pP2++;
  98.         }
  99.  
  100.         /* write out one line of graphic data */
  101.         ppm_writeppmrow(stdout, destrow, cols, maxval, 0);
  102.     }
  103.  
  104.     pm_close(ifp);
  105.     ppm_freerow(srcrow);
  106.     ppm_freerow(destrow);
  107.  
  108.     exit(0);
  109. }
  110.  
  111.