home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / general / convrtrs / pbmplus / ntpbmsrc.lha / netpbm / ppm / ppmchange.c < prev    next >
C/C++ Source or Header  |  1993-12-06  |  2KB  |  73 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.  
  13. #include "ppm.h"
  14.  
  15.  
  16. int
  17. main( argc, argv )
  18.     int argc;
  19.     char* argv[];
  20.     {
  21.     FILE* ifp;
  22.     int argn, format, row;
  23.     register int col;
  24.     int rows, cols;
  25.     pixel* prow;
  26.     pixel color0, color1;
  27.     pixval maxval;
  28.     char* usage = "<oldcolor> <newcolor> [ppmfile]";
  29.  
  30.     ppm_init( &argc, argv );
  31.  
  32.     argn = 1;
  33.  
  34.     if ( argn == argc )
  35.     pm_usage( usage );
  36.     color0 = ppm_parsecolor( argv[argn], PPM_MAXMAXVAL );
  37.     ++argn;
  38.     color1 = ppm_parsecolor( argv[argn], PPM_MAXMAXVAL );
  39.     ++argn;
  40.  
  41.     if ( argn != argc )
  42.     {
  43.     ifp = pm_openr( argv[argn] );
  44.     ++argn;
  45.     }
  46.     else
  47.     ifp = stdin;
  48.  
  49.     if ( argn != argc )
  50.     pm_usage( usage );
  51.  
  52.     ppm_readppminit( ifp, &cols, &rows, &maxval, &format );
  53.     ppm_writeppminit( stdout, cols, rows, maxval, 0 );
  54.     prow = ppm_allocrow( cols );
  55.  
  56.     /* Scan for the desired color */
  57.     for ( row = 0; row < rows; ++row )
  58.     {
  59.     ppm_readppmrow( ifp, prow, cols, maxval, format );
  60.     for ( col = 0; col < cols; ++col )
  61.         if ( PPM_EQUAL( prow[col], color0 ) )
  62.         PPM_ASSIGN( prow[col],
  63.             PPM_GETR( color1 ),
  64.             PPM_GETG( color1 ),
  65.             PPM_GETB( color1 ) );
  66.     ppm_writeppmrow( stdout, prow, cols, maxval, 0 );
  67.     }
  68.  
  69.     pm_close( ifp );
  70.  
  71.     exit( 0 );
  72.     }
  73.