home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume19 / fbm / part02 / fbhist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-08  |  2.7 KB  |  110 lines

  1. /*****************************************************************
  2.  * fbhist.c: FBM Library 0.9 (Beta test) 07-Mar-89  Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989 by Michael Mauldin.  Permission is granted to
  5.  * use this file in whole or in part provided that you do not sell it
  6.  * for profit and that this copyright notice is retained unchanged.
  7.  *
  8.  * fbhist.c: 
  9.  *
  10.  * USAGE
  11.  *    % fbhist [-h] < image
  12.  *
  13.  * EDITLOG
  14.  *    LastEditDate = Tue Mar  7 17:52:50 1989 - Michael Mauldin
  15.  *    LastFileName = /usr2/mlm/src/misc/fbm/fbhist.c
  16.  *
  17.  * HISTORY
  18.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  19.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  20.  *
  21.  * 21-Aug-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  22.  *    Created.
  23.  *****************************************************************/
  24.  
  25. # include <stdio.h>
  26. # include <math.h>
  27. # include "fbm.h"
  28.  
  29. #define USAGE "Usage: fbhist [ -h ] < 8bit"
  30.  
  31. #ifndef lint
  32. static char *fbmid =
  33.     "$FBM fbhist.c <0.9> 07-Mar-89  (C) 1989 by Michael Mauldin$";
  34. #endif
  35.  
  36. main (argc, argv)
  37. char *argv[];
  38. { FBM image;
  39.   register int ch, size, cnt;
  40.   register unsigned char *bmptr, *tail;
  41.   int min = BYTE, max = -1;
  42.   int hist[BYTE], dohist = 0;
  43.   double sum = 0.0, sumsq = 0.0, avg, std;
  44.  
  45.   /* Get the options */
  46.   while (--argc > 0 && (*++argv)[0] == '-')
  47.   { while (*++(*argv))
  48.     { switch (**argv)
  49.       { case 'h':    dohist++; break;
  50.     default:        fprintf (stderr, "%s\n", USAGE);
  51.                         exit (1);
  52.       }
  53.     }
  54.   }
  55.  
  56.   /* Clear the memory pointer so alloc_fbm won't be confused */
  57.   image.cm  = image.bm  = (unsigned char *) NULL;
  58.  
  59.   /* Clear the histogram */
  60.   for (ch=0; ch<BYTE; ch++)
  61.   { hist[ch] = 0; }
  62.  
  63.   /* Read the file and count the gray levels */
  64.   if (read_bitmap (&image, (char *) NULL))
  65.   { size = image.hdr.rows * image.hdr.cols;
  66.     bmptr = image.bm;
  67.     tail = bmptr+size;
  68.  
  69.     while (bmptr < tail)
  70.     { hist[*bmptr++]++; }
  71.     
  72.     for (ch=0; ch<BYTE; ch++)
  73.     { cnt = hist[ch];
  74.  
  75.       if (cnt && ch < min) min = ch;
  76.       if (cnt && ch > max) max = ch;
  77.       
  78.       sum += cnt * ch;
  79.       sumsq += cnt * ch*ch;
  80.     }
  81.     
  82.     avg = sum / size;
  83.  
  84.     if (size < 2)
  85.     { std = 0.0; }
  86.     else
  87.     { double t1 = (size * sumsq - sum * sum);
  88.       double t2 = ((double) size * (size-1));
  89.       std = sqrt (t1 / t2);
  90.     }
  91.  
  92.     printf ("%s [%dx%d  %d bits  %1.3lf aspect ratio]\n",
  93.         *image.hdr.title ? image.hdr.title : "Untitled",
  94.         image.hdr.cols, image.hdr.rows, image.hdr.bits, image.hdr.aspect);
  95.  
  96.     printf ("Mean %1.2lf +- %1.2lf, range %d..%d\n",
  97.         avg, std, min, max);
  98.  
  99.     if (dohist)
  100.     { for (ch=0; ch<BYTE; ch++)
  101.       { if (hist[ch]) printf ("%3d: %6d\n", ch, hist[ch]); }
  102.     }
  103.     
  104.   }
  105.   else
  106.   { exit (1); }
  107.   
  108.   exit (0);
  109. }
  110.