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

  1.  
  2. /*********************************************************************/
  3. /* ppmflash -  brighten a picture up to total whiteout               */
  4. /* Frank Neumann, August 1993                                        */
  5. /* V1.4 16.11.1993                                                   */
  6. /*                                                                   */
  7. /* version history:                                                  */
  8. /* V1.0 ~ 15.August 1993    first version                            */
  9. /* V1.1 03.09.1993          uses ppm libs & header files             */
  10. /* V1.2 03.09.1993          integer arithmetics instead of float     */
  11. /*                          (gains about 50 % speed up)              */
  12. /* V1.3 11.10.1993          reads only one line at a time - this     */
  13. /*                          saves LOTS of memory on big picturs      */
  14. /* V1.4 16.11.1993          Rewritten to be NetPBM.programming con-  */
  15. /*                          forming                                  */
  16. /*********************************************************************/
  17.  
  18. #include "ppm.h"
  19.  
  20. /* global variables */
  21. #ifdef AMIGA
  22. static char *version = "$VER: ppmflash 1.4 (16.11.93)";
  23. #endif
  24.  
  25. /**************************/
  26. /* start of main function */
  27. /**************************/
  28. int main(argc, argv)
  29. int argc;
  30. char *argv[];
  31. {
  32.     FILE* ifp;
  33.     int argn, rows, cols, i, j, format;
  34.     pixel *srcrow, *destrow;
  35.     pixel *pP, *pP2;
  36.     pixval maxval;
  37.     double flashfactor;
  38.     long longfactor;
  39.     char* usage = "flashfactor [ppmfile]\n        flashfactor: 0.0 = original picture, 1.0 = total whiteout\n";
  40.  
  41.     /* parse in 'default' parameters */
  42.     ppm_init( &argc, argv );
  43.  
  44.     argn = 1;
  45.  
  46.     /* parse in flash factor */
  47.     if (argn == argc)
  48.         pm_usage(usage);
  49.     if (sscanf(argv[argn], "%lf", &flashfactor) != 1)
  50.         pm_usage(usage);
  51.     if (flashfactor < 0.0 || flashfactor > 1.0)
  52.         pm_error("flash factor must be in the range from 0.0 to 1.0 ");
  53.     ++argn;
  54.  
  55.     /* parse in filename (if present, stdin otherwise) */
  56.     if (argn != argc)
  57.     {
  58.         ifp = pm_openr(argv[argn]);
  59.         ++argn;
  60.     }
  61.     else
  62.         ifp = stdin;
  63.  
  64.     if (argn != argc)
  65.         pm_usage(usage);
  66.  
  67.     /* read first data from file */
  68.     ppm_readppminit(ifp, &cols, &rows, &maxval, &format);
  69.  
  70.     /* no error checking required here, ppmlib does it all for us */
  71.     srcrow = ppm_allocrow(cols);
  72.  
  73.     longfactor = (long)(flashfactor * 65536);
  74.  
  75.     /* allocate a row of pixel data for the new pixels */
  76.     destrow = ppm_allocrow(cols);
  77.  
  78.     ppm_writeppminit(stdout, cols, rows, maxval, 0);
  79.  
  80.     /** now do the flashing **/
  81.     /* the 'float' parameter for flashing is sort of faked - in fact, we */
  82.     /* convert it to a range from 0 to 65536 for integer math. Shouldn't */
  83.     /* be something you'll have to worry about, though. */
  84.  
  85.     for (i = 0; i < rows; i++)
  86.     {
  87.         ppm_readppmrow(ifp, srcrow, cols, maxval, format);
  88.  
  89.         pP = srcrow;
  90.         pP2 = destrow;
  91.  
  92.         for (j = 0; j < cols; j++)
  93.         {
  94.             PPM_ASSIGN(*pP2, PPM_GETR(*pP) + (((maxval - PPM_GETR(*pP)) * longfactor) >> 16),
  95.                              PPM_GETG(*pP) + (((maxval - PPM_GETG(*pP)) * longfactor) >> 16),
  96.                              PPM_GETB(*pP) + (((maxval - PPM_GETB(*pP)) * longfactor) >> 16));
  97.  
  98.             pP++;
  99.             pP2++;
  100.         }
  101.  
  102.         /* write out one line of graphic data */
  103.         ppm_writeppmrow(stdout, destrow, cols, maxval, 0);
  104.     }
  105.  
  106.     pm_close(ifp);
  107.     ppm_freerow(srcrow);
  108.     ppm_freerow(destrow);
  109.  
  110.     exit(0);
  111. }
  112.  
  113.