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

  1. /* ppmtorgb3.c - separate a portable pixmap into three portable graymaps
  2. **
  3. ** Copyright (C) 1991 by Jef Poskanzer.
  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. #include "pgm.h"
  15.  
  16. int
  17. main( argc, argv )
  18.     int argc;
  19.     char* argv[];
  20.     {
  21.     FILE* ifp;
  22.     FILE* redfile;
  23.     FILE* grnfile;
  24.     FILE* blufile;
  25.     char* basename;
  26.     char filename[100];
  27.     char* cp;
  28.     pixel* pixelrow;
  29.     register pixel* pP;
  30.     gray* grayrow;
  31.     register gray* gP;
  32.     int rows, cols, format, row;
  33.     register int col;
  34.     pixval maxval;
  35.  
  36.  
  37.     ppm_init( &argc, argv );
  38.  
  39.     if ( argc > 2 )
  40.     pm_usage( "[ppmfile]" );
  41.  
  42.     if ( argc == 2 )
  43.     {
  44.     ifp = pm_openr( argv[1] );
  45.     basename = argv[1];
  46.     cp = rindex( basename, '.' );
  47.     if ( cp != NULL )
  48.         *cp = '\0';
  49.     }
  50.     else
  51.     {
  52.     ifp = stdin;
  53.     basename = "noname";
  54.     }
  55.  
  56.     ppm_readppminit( ifp, &cols, &rows, &maxval, &format );
  57.     pixelrow = ppm_allocrow( cols );
  58.     (void) strcpy( filename, basename );
  59.     (void) strcat( filename, ".red" );
  60.     redfile = pm_openw( filename );
  61.     pgm_writepgminit( redfile, cols, rows, (gray) maxval, 0 );
  62.     (void) strcpy( filename, basename );
  63.     (void) strcat( filename, ".grn" );
  64.     grnfile = pm_openw( filename );
  65.     pgm_writepgminit( grnfile, cols, rows, (gray) maxval, 0 );
  66.     (void) strcpy( filename, basename );
  67.     (void) strcat( filename, ".blu" );
  68.     blufile = pm_openw( filename );
  69.     pgm_writepgminit( blufile, cols, rows, (gray) maxval, 0 );
  70.     grayrow = pgm_allocrow( cols );
  71.  
  72.     for ( row = 0; row < rows; ++row )
  73.     {
  74.     ppm_readppmrow( ifp, pixelrow, cols, maxval, format );
  75.  
  76.     for ( col = 0, pP = pixelrow, gP = grayrow; col < cols;
  77.           ++col, ++pP, ++gP )
  78.         *gP = (gray) PPM_GETR( *pP );
  79.     pgm_writepgmrow( redfile, grayrow, cols, maxval, 0 );
  80.  
  81.     for ( col = 0, pP = pixelrow, gP = grayrow; col < cols;
  82.           ++col, ++pP, ++gP )
  83.         *gP = (gray) PPM_GETG( *pP );
  84.     pgm_writepgmrow( grnfile, grayrow, cols, maxval, 0 );
  85.  
  86.     for ( col = 0, pP = pixelrow, gP = grayrow; col < cols;
  87.           ++col, ++pP, ++gP )
  88.         *gP = (gray) PPM_GETB( *pP );
  89.     pgm_writepgmrow( blufile, grayrow, cols, maxval, 0 );
  90.     }
  91.  
  92.     pm_close( ifp );
  93.     pm_close( redfile );
  94.     pm_close( blufile );
  95.     pm_close( grnfile );
  96.  
  97.     exit( 0 );
  98.     }
  99.