home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / ppm / ppmchange.c < prev    next >
C/C++ Source or Header  |  1994-01-28  |  2KB  |  78 lines

  1. /* ppmchange.c - change a given color to another
  2. **
  3. ** Copyright (C) 1991 by Wilson H. Bent, Jr.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. **
  12. ** Modified by Alberto Accomazzi (alberto@cfa.harvard.edu).
  13. **     28 Jan 94 -  Added support for multiple color substitution.
  14. */
  15.  
  16. #include "ppm.h"
  17. #define TCOLS 256
  18.  
  19.  
  20. int
  21. main( argc, argv )
  22.     int argc;
  23.     char* argv[];
  24.     {
  25.     FILE* ifp;
  26.     int argn, format, row;
  27.     register int col, i, replaced;
  28.     int rows, cols, ncolors;
  29.     pixel* prow;
  30.     pixel color0[TCOLS], color1[TCOLS];
  31.     pixval maxval;
  32.     char* usage = "<oldcolor> <newcolor> [...] [ppmfile]";
  33.  
  34.     ppm_init( &argc, argv );
  35.  
  36.     argn = 1;
  37.  
  38.     if ( argn == argc )
  39.     pm_usage( usage );
  40.     for ( i = 0; argn < argc - 1 && i < TCOLS; i++ )
  41.         {
  42.     color0[i] = ppm_parsecolor( argv[argn], PPM_MAXMAXVAL );
  43.     ++argn;
  44.         if ( argn == argc )
  45.             pm_usage( usage );
  46.     color1[i] = ppm_parsecolor( argv[argn], PPM_MAXMAXVAL );
  47.     ++argn;
  48.       }
  49.     ncolors = i;
  50.  
  51.     if ( argn == argc - 1 )
  52.     ifp = pm_openr( argv[argn] );
  53.     else
  54.     ifp = stdin;
  55.  
  56.     ppm_readppminit( ifp, &cols, &rows, &maxval, &format );
  57.     ppm_writeppminit( stdout, cols, rows, maxval, 0 );
  58.     prow = ppm_allocrow( cols );
  59.  
  60.     /* Scan for the desired color */
  61.     for ( row = 0; row < rows; ++row )
  62.     {
  63.     ppm_readppmrow( ifp, prow, cols, maxval, format );
  64.     for ( col = 0; col < cols; ++col )
  65.             for ( i = 0, replaced = 0; i < ncolors && !replaced; i++ )
  66.             if ( replaced = PPM_EQUAL( prow[col], color0[i] ) )
  67.             PPM_ASSIGN( prow[col],
  68.                    PPM_GETR( color1[i] ),
  69.                    PPM_GETG( color1[i] ),
  70.                    PPM_GETB( color1[i] ) );
  71.     ppm_writeppmrow( stdout, prow, cols, maxval, 0 );
  72.     }
  73.  
  74.     pm_close( ifp );
  75.  
  76.     exit( 0 );
  77.     }
  78.