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

  1. /* pgmhist.c - print a histogram of the values in a portable graymap
  2. **
  3. ** Copyright (C) 1989 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. int
  16. main( argc, argv )
  17. int argc;
  18. char *argv[];
  19.     {
  20.     FILE *ifp;
  21.     gray maxval, *grayrow;
  22.     register gray *gP;
  23.     int argn, rows, cols, format, row;
  24.     int i, *hist, *rcount, count, size;
  25.     register int col;
  26.     char *usage = "[pgmfile]";
  27.  
  28.  
  29.     pgm_init( &argc, argv );
  30.  
  31.     argn = 1;
  32.  
  33.     if ( argn < argc )
  34.     {
  35.     ifp = pm_openr( argv[argn] );
  36.     argn++;
  37.     }
  38.     else
  39.     ifp = stdin;
  40.  
  41.     if ( argn != argc )
  42.     pm_usage( usage );
  43.  
  44.     pgm_readpgminit( ifp, &cols, &rows, &maxval, &format );
  45.     grayrow = pgm_allocrow( cols );
  46.  
  47.     /* Build histogram. */
  48.     hist = (int *) malloc( ( maxval + 1 ) * sizeof(int) );
  49.     rcount = (int *) malloc( ( maxval + 1 ) * sizeof(int) );
  50.     if ( hist == (int *) 0 || rcount == (int *) 0 )
  51.     pm_error( "out of memory" );
  52.     for ( i = 0; i <= maxval; i++ )
  53.     hist[i] = 0;
  54.     for ( row = 0; row < rows; row++ )
  55.     {
  56.     pgm_readpgmrow( ifp, grayrow, cols, maxval, format );
  57.         for ( col = 0, gP = grayrow; col < cols; col++, gP++ )
  58.         hist[(int) *gP]++;
  59.     }
  60.  
  61.     pm_close( ifp );
  62.  
  63.     /* Compute count-down */
  64.     count = 0;
  65.     for ( i = maxval; i >= 0; i-- )
  66.     {
  67.     count += hist[i];
  68.     rcount[i] = count;
  69.     }
  70.  
  71.     /* And print it. */
  72.     printf( "value\tcount\tb%%\tw%%\n" );
  73.     printf( "-----\t-----\t--\t--\n" );
  74.     count = 0;
  75.     size = rows * cols;
  76.     for ( i = 0; i <= maxval; i++ )
  77.     if ( hist[i] > 0 )
  78.         {
  79.         count += hist[i];
  80.         printf(
  81.         "%d\t%d\t%5.3g%%\t%5.3g%%\n", i, hist[i],
  82.         (float) count * 100.0 / size, (float) rcount[i] * 100.0 / size );
  83.         }
  84.  
  85.     exit( 0 );
  86.     }
  87.