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

  1. /* pnmdepth.c - change the maxval in a portable pixmap
  2. **
  3. ** Copyright (C) 1989, 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 "pnm.h"
  14.  
  15. static xelval newvals[PNM_MAXMAXVAL+1];
  16.  
  17. int
  18. main( argc, argv )
  19.     int argc;
  20.     char* argv[];
  21.     {
  22.     FILE* ifp;
  23.     xel* xelrow;
  24.     register xel* xP;
  25.     int argn, rows, cols, format, newformat, row;
  26.     register int col;
  27.     xelval maxval;
  28.     int newmaxval;
  29.     int i;
  30.     char* usage = "newmaxval [pnmfile]";
  31.  
  32.     pnm_init( &argc, argv );
  33.  
  34.     argn = 1;
  35.  
  36.     if ( argn == argc )
  37.     pm_usage( usage );
  38.     if ( sscanf( argv[argn], "%d", &newmaxval ) != 1 )
  39.     pm_usage( usage );
  40.     ++argn;
  41.     if ( newmaxval < 1 )
  42.     pm_error( "newmaxval must be >= 1" );
  43.     if ( newmaxval > PNM_MAXMAXVAL )
  44.     pm_error(
  45. "newmaxval is too large - try reconfiguring with PGM_BIGGRAYS\n    or without PPM_PACKCOLORS" );
  46.  
  47.     if ( argn != argc )
  48.     {
  49.     ifp = pm_openr( argv[argn] );
  50.     ++argn;
  51.     }
  52.     else
  53.     ifp = stdin;
  54.  
  55.     if ( argn != argc )
  56.     pm_usage( usage );
  57.  
  58.     pnm_readpnminit( ifp, &cols, &rows, &maxval, &format );
  59.     xelrow = pnm_allocrow( cols );
  60.  
  61.     /* Promote PBM files to PGM. */
  62.     if ( PNM_FORMAT_TYPE(format) == PBM_TYPE )
  63.     {
  64.         newformat = PGM_TYPE;
  65.     pm_message( "promoting from PBM to PGM" );
  66.     }
  67.     else
  68.         newformat = format;
  69.  
  70.     for ( i = 0; i <= maxval; ++i )
  71.     newvals[i] = ( i * newmaxval + maxval / 2 ) / maxval;
  72.  
  73.     pnm_writepnminit( stdout, cols, rows, newmaxval, newformat, 0 );
  74.  
  75.     for ( row = 0; row < rows; ++row )
  76.     {
  77.     pnm_readpnmrow( ifp, xelrow, cols, maxval, format );
  78.  
  79.     switch ( PNM_FORMAT_TYPE(format) )
  80.         {
  81.         case PPM_TYPE:
  82.         for ( col = 0, xP = xelrow; col < cols; ++col, ++xP )
  83.         PPM_ASSIGN(
  84.             *xP, newvals[PPM_GETR(*xP)], newvals[PPM_GETG(*xP)],
  85.             newvals[PPM_GETB(*xP)] );
  86.         break;
  87.  
  88.         default:
  89.         for ( col = 0, xP = xelrow; col < cols; ++col, ++xP )
  90.         PNM_ASSIGN1( *xP, newvals[PNM_GET1(*xP)] );
  91.         break;
  92.         }
  93.  
  94.     pnm_writepnmrow( stdout, xelrow, cols, newmaxval, newformat, 0 );
  95.     }
  96.  
  97.     pm_close( ifp );
  98.     pm_close( stdout );
  99.  
  100.     exit( 0 );
  101.     }
  102.