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

  1. /* pgmnorm.c - read a portable graymap and normalize the contrast
  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 "pgm.h"
  14.  
  15. static int hist[PGM_MAXMAXVAL+1];
  16.  
  17. int
  18. main( argc, argv )
  19. int argc;
  20. char* argv[];
  21.     {
  22.     FILE* ifp;
  23.     gray maxval;
  24.     gray** grays;
  25.     gray* grayrow;
  26.     register gray* gP;
  27.     int argn, rows, cols, format, row;
  28.     register int col;
  29.     int i, size, cutoff, count, val;
  30.     float bpercent, wpercent;
  31.     int bvalue, wvalue, range;
  32.     int specbpercent, specbvalue, specwpercent, specwvalue;
  33.     char* usage = "[-bpercent N | -bvalue N] [-wpercent N | -wvalue N] [pgmfile]";
  34.  
  35.  
  36.     pgm_init( &argc, argv );
  37.  
  38.     argn = 1;
  39.     bpercent = 2.0;
  40.     wpercent = 1.0;
  41.     specbpercent = specbvalue = specwpercent = specwvalue = 0;
  42.  
  43.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  44.     {
  45.     if ( pm_keymatch( argv[argn], "-bpercent", 3 ) )
  46.         {
  47.         if ( specbvalue )
  48.         pm_error( "only one of -bpercent and -bvalue may be specified" );
  49.         ++argn;
  50.         if ( argn == argc || sscanf( argv[argn], "%f", &bpercent ) != 1 )
  51.         pm_usage( usage );
  52.         if ( bpercent < 0.0  || bpercent > 100.0 )
  53.         pm_error( "black percentage must between 0 and 100" );
  54.         specbpercent = 1;
  55.         }
  56.     else if ( pm_keymatch( argv[argn], "-bvalue", 3 ) )
  57.         {
  58.         if ( specbpercent )
  59.         pm_error( "only one of -bpercent and -bvalue may be specified" );
  60.         ++argn;
  61.         if ( argn == argc || sscanf( argv[argn], "%d", &bvalue ) != 1 )
  62.         pm_usage( usage );
  63.         if ( bvalue < 0 )
  64.         pm_error( "black value must be >= 0" );
  65.         specbvalue = 1;
  66.         }
  67.     else if ( pm_keymatch( argv[argn], "-wpercent", 3 ) )
  68.         {
  69.         if ( specwvalue )
  70.         pm_error( "only one of -wpercent and -wvalue may be specified" );
  71.         ++argn;
  72.         if ( argn == argc || sscanf( argv[argn], "%f", &wpercent ) != 1 )
  73.         pm_usage( usage );
  74.         if ( wpercent < 0.0 || wpercent > 100.0 )
  75.         pm_error( "white percentage must be between 0 and 100" );
  76.         specwpercent = 1;
  77.         }
  78.     else if ( pm_keymatch( argv[argn], "-wvalue", 3 ) )
  79.         {
  80.         if ( specwpercent )
  81.         pm_error( "only one of -wpercent and -wvalue may be specified" );
  82.         ++argn;
  83.         if ( argn == argc || sscanf( argv[argn], "%d", &wvalue ) != 1 )
  84.         pm_usage( usage );
  85.         if ( wvalue < 0 )
  86.         pm_error( "white value must be >= 0" );
  87.         specwvalue = 1;
  88.         }
  89.     else
  90.         pm_usage( usage );
  91.     ++argn;
  92.     }
  93.  
  94.     if ( argn < argc )
  95.     {
  96.     ifp = pm_openr( argv[argn] );
  97.     ++argn;
  98.     }
  99.     else
  100.     ifp = stdin;
  101.  
  102.     if ( argn != argc )
  103.     pm_usage( usage );
  104.  
  105.     if ( specbvalue && specwvalue )
  106.     {
  107.     /* Rescale so that bvalue maps to 0, wvalue maps to maxval. */
  108.     pgm_readpgminit( ifp, &cols, &rows, &maxval, &format );
  109.     grayrow = pgm_allocrow( cols );
  110.     pgm_writepgminit( stdout, cols, rows, maxval, 0 );
  111.     for ( i = 0; i <= bvalue; ++i )
  112.         hist[i] = 0;
  113.     for ( i = wvalue; i <= maxval; ++i )
  114.         hist[i] = maxval;
  115.     range = wvalue - bvalue;
  116.     for ( i = bvalue+1, val = maxval; i < wvalue; ++i, val += maxval )
  117.         hist[i] = val / range;
  118.     for ( row = 0; row < rows; ++row )
  119.         {
  120.         pgm_readpgmrow( ifp, grayrow, cols, maxval, format );
  121.         for ( col = 0, gP = grayrow; col < cols; ++col, ++gP )
  122.         *gP = hist[*gP];
  123.         pgm_writepgmrow( stdout, grayrow, cols, maxval, 0 );
  124.         }
  125.     pm_close( ifp );
  126.     }
  127.     else
  128.     {
  129.     grays = pgm_readpgm( ifp, &cols, &rows, &maxval );
  130.     pm_close( ifp );
  131.  
  132.     /* Build histogram. */
  133.     for ( i = 0; i <= maxval; ++i )
  134.         hist[i] = 0;
  135.     for ( row = 0; row < rows; ++row )
  136.         for ( col = 0, gP = grays[row]; col < cols; ++col, ++gP )
  137.         ++hist[*gP];
  138.     size = rows * cols;
  139.     if ( ! specbvalue )
  140.         { /* Compute bvalue from bpercent. */
  141.         cutoff = size * bpercent / 100.0;
  142.         count = 0;
  143.         for ( bvalue = 0; bvalue <= maxval; ++bvalue )
  144.         {
  145.         count += hist[bvalue];
  146.         if ( count > cutoff )
  147.         break;
  148.         }
  149.         }
  150.     if ( ! specwvalue )
  151.         { /* Compute wvalue from wpercent. */
  152.         cutoff = size * wpercent / 100.0;
  153.         count = 0;
  154.         for ( wvalue = maxval; wvalue >= 0; wvalue-- )
  155.         {
  156.         count += hist[wvalue];
  157.         if ( count > cutoff )
  158.             break;
  159.         }
  160.         }
  161.  
  162.     /* Now rescale so that bvalue maps to 0, wvalue maps to maxval. */
  163.     pm_message(
  164.         "remapping %d..%d to %d..%d", bvalue, wvalue, 0, maxval, 0 );
  165.     pgm_writepgminit( stdout, cols, rows, maxval, 0 );
  166.     for ( i = 0; i <= bvalue; ++i )
  167.         hist[i] = 0;
  168.     for ( i = wvalue; i <= maxval; ++i )
  169.         hist[i] = maxval;
  170.     range = wvalue - bvalue;
  171.     for ( i = bvalue + 1, val = maxval; i < wvalue; ++i, val += maxval )
  172.         hist[i] = val / range;
  173.     for ( row = 0; row < rows; ++row )
  174.         {
  175.         for ( col = 0, gP = grays[row]; col < cols; ++col, ++gP )
  176.         *gP = hist[*gP];
  177.         pgm_writepgmrow( stdout, grays[row], cols, maxval, 0 );
  178.         }
  179.     }
  180.  
  181.     pm_close( stdout );
  182.     exit( 0 );
  183.     }
  184.