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

  1.  
  2. /*********************************************************************/
  3. /* ppmmix -  mix together two pictures like with a fader             */
  4. /* Frank Neumann, October 1993                                       */
  5. /* V1.2 16.11.1993                                                   */
  6. /*                                                                   */
  7. /* version history:                                                  */
  8. /* V1.0 Aug   1993    first version                                  */
  9. /* V1.1 12.10.1993    uses ppm libs&headers, integer math, cleanups  */
  10. /* V1.2 16.11.1993    Rewritten to be NetPBM.programming conforming  */
  11. /*********************************************************************/
  12.  
  13. #include "ppm.h"
  14.  
  15. /* global variables */
  16. #ifdef AMIGA
  17. static char *version = "$VER: ppmmix 1.2 (16.11.93)"; /* Amiga version identification */
  18. #endif
  19.  
  20. /**************************/
  21. /* start of main function */
  22. /**************************/
  23. int main(argc, argv)
  24. int argc;
  25. char *argv[];
  26. {
  27.     FILE *ifp1, *ifp2;
  28.     int argn, rows, cols, format, i = 0, j = 0;
  29.     int rows2, cols2, format2;
  30.     pixel *srcrow1, *srcrow2, *destrow;
  31.     pixel *pP1, *pP2, *pP3;
  32.     pixval maxval, maxval2;
  33.     pixval r1, r2, r3, g1, g2, g3, b1, b2, b3;
  34.     double fadefactor;
  35.     long longfactor;
  36.     char *usage = "fadefactor ppmfile1 ppmfile2\n        fadefactor: 0.0 = only ppmfile1, 1.0 = only ppmfile2\n";
  37.  
  38.     /* parse in 'default' parameters */
  39.     ppm_init(&argc, argv);
  40.  
  41.     argn = 1;
  42.  
  43.     /* parse in dim factor */
  44.     if (argn == argc)
  45.         pm_usage(usage);
  46.     if (sscanf(argv[argn], "%lf", &fadefactor) != 1)
  47.         pm_usage(usage);
  48.     if (fadefactor < 0.0 || fadefactor > 1.0)
  49.         pm_error("fade factor must be in the range from 0.0 to 1.0 ");
  50.     ++argn;
  51.  
  52.     /* parse in filenames and open files (cannot be stdin-filters, sorry..) */
  53.     if (argn == argc-2)
  54.     {
  55.         ifp1 = pm_openr(argv[argn]);
  56.         ++argn;
  57.         ifp2 = pm_openr(argv[argn]);
  58.     }
  59.     else
  60.         pm_usage(usage);
  61.  
  62.     /* read first data from both files and compare sizes etc. */
  63.     ppm_readppminit(ifp1, &cols, &rows, &maxval, &format);
  64.     ppm_readppminit(ifp2, &cols2, &rows2, &maxval2, &format2);
  65.  
  66.     if ( (cols != cols2) || (rows != rows2) )
  67.         pm_error("image sizes are different!");
  68.  
  69.     if ( maxval != maxval2)
  70.         pm_error("images have different maxvalues");
  71.  
  72.     if (format != format2)
  73.     {
  74.         pm_error("images have different PxM types");
  75.     }
  76.  
  77.     /* no error checking required here, ppmlib does it all for us */
  78.     srcrow1 = ppm_allocrow(cols);
  79.     srcrow2 = ppm_allocrow(cols);
  80.  
  81.     longfactor = (long)(fadefactor * 65536);
  82.  
  83.     /* allocate a row of pixel data for the new pixels */
  84.     destrow = ppm_allocrow(cols);
  85.  
  86.     ppm_writeppminit(stdout, cols, rows, maxval, 0);
  87.  
  88.     for (i = 0; i < rows; i++)
  89.     {
  90.         ppm_readppmrow(ifp1, srcrow1, cols, maxval, format);
  91.         ppm_readppmrow(ifp2, srcrow2, cols, maxval, format);
  92.  
  93.         pP1 = srcrow1;
  94.         pP2 = srcrow2;
  95.         pP3 = destrow;
  96.  
  97.         for (j = 0; j < cols; j++)
  98.         {
  99.             r1 = PPM_GETR(*pP1);
  100.             g1 = PPM_GETG(*pP1);
  101.             b1 = PPM_GETB(*pP1);
  102.  
  103.             r2 = PPM_GETR(*pP2);
  104.             g2 = PPM_GETG(*pP2);
  105.             b2 = PPM_GETB(*pP2);
  106.  
  107.             r3 = r1 + (((r2 - r1) * longfactor) >> 16);
  108.             g3 = g1 + (((g2 - g1) * longfactor) >> 16);
  109.             b3 = b1 + (((b2 - b1) * longfactor) >> 16);
  110.  
  111.  
  112.             PPM_ASSIGN(*pP3, r3, g3, b3);
  113.  
  114.             pP1++;
  115.             pP2++;
  116.             pP3++;
  117.         }
  118.  
  119.         /* write out one line of graphic data */
  120.         ppm_writeppmrow(stdout, destrow, cols, maxval, 0);
  121.     }
  122.  
  123.     pm_close(ifp1);
  124.     pm_close(ifp2);
  125.     ppm_freerow(srcrow1);
  126.     ppm_freerow(srcrow2);
  127.     ppm_freerow(destrow);
  128.  
  129.     exit(0);
  130. }
  131.  
  132.