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

  1. /* ppmhist.c - read a portable pixmap and compute a color histogram
  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 "ppm.h"
  14. #include "ppmcmap.h"
  15.  
  16. #define MAXCOLORS 100000
  17.  
  18. static int
  19. countcompare( ch1, ch2 )
  20. colorhist_vector ch1, ch2;
  21.     {
  22.     return ch2->value - ch1->value;
  23.     }
  24.  
  25. int
  26. main( argc, argv )
  27.     int argc;
  28.     char* argv[];
  29.     {
  30.     FILE* ifp;
  31.     pixel** pixels;
  32.     colorhist_vector chv;
  33.     int argn, rows, cols, colors, i;
  34.     pixval maxval;
  35.     char* usage = "[-map] [ppmfile]";
  36.     int map;
  37.  
  38.  
  39.     ppm_init( &argc, argv );
  40.  
  41.     map = 0;
  42.     argn = 1;
  43.  
  44.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  45.         {
  46.         if( pm_keymatch( argv[argn], "-map", 2 ) )
  47.             map = 1;
  48.         else if( pm_keymatch( argv[argn], "-nomap", 2 ) )
  49.             map = 0;
  50.         else
  51.             pm_usage( usage );
  52.         ++argn;
  53.         }
  54.  
  55.     if ( argn != argc )
  56.         {
  57.         ifp = pm_openr( argv[argn] );
  58.         argn++;
  59.         }
  60.     else
  61.         ifp = stdin;
  62.  
  63.     if ( argn != argc )
  64.         pm_usage( usage );
  65.  
  66.     pixels = ppm_readppm( ifp, &cols, &rows, &maxval );
  67.  
  68.     pm_close( ifp );
  69.  
  70.     chv = ppm_computecolorhist( pixels, cols, rows, MAXCOLORS, &colors );
  71.     if ( chv == (colorhist_vector) 0 )
  72.         pm_error( "too many colors - try doing a ppmquant" );
  73.  
  74.     /* Sort by count. */
  75.     qsort( (char*) chv, colors, sizeof(struct colorhist_item), countcompare );
  76.  
  77.     /* And print the histogram. */
  78.     if( map )
  79.         {
  80.         printf("P3\n# color map\n%d 1\n%d\n", colors, maxval);
  81.         printf( "# r   g   b   \tlum\tcount\n" );
  82.         printf( "#--- --- ---  \t---\t-----\n" );
  83.         }
  84.     else
  85.         {
  86.         printf( " r   g   b \tlum\tcount\n" );
  87.         printf( "--- --- ---\t---\t-----\n" );
  88.         }
  89.  
  90.     for ( i = 0; i < colors; i++ )
  91.         printf(
  92.             "%3d %3d %3d%s\t%d\t%d\n", PPM_GETR(chv[i].color),
  93.             PPM_GETG(chv[i].color), PPM_GETB(chv[i].color),
  94.             (map ? " #" : ""),
  95.             (int) ( PPM_LUMIN( chv[i].color ) + 0.5 ),
  96.             chv[i].value );
  97.  
  98.     ppm_freecolorhist( chv );
  99.  
  100.     exit( 0 );
  101.     }
  102.