home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fbm / src / fbhist.c < prev    next >
C/C++ Source or Header  |  1990-06-24  |  3KB  |  116 lines

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