about summary refs log tree commit diff
path: root/analyzer/pgmhist.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2006-08-19 03:12:28 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2006-08-19 03:12:28 +0000
commit1fd361a1ea06e44286c213ca1f814f49306fdc43 (patch)
tree64c8c96cf54d8718847339a403e5e67b922e8c3f /analyzer/pgmhist.c
downloadnetpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.gz
netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.xz
netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.zip
Create Subversion repository
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'analyzer/pgmhist.c')
-rw-r--r--analyzer/pgmhist.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/analyzer/pgmhist.c b/analyzer/pgmhist.c
new file mode 100644
index 00000000..8f4e512e
--- /dev/null
+++ b/analyzer/pgmhist.c
@@ -0,0 +1,87 @@
+/* pgmhist.c - print a histogram of the values in a portable graymap
+**
+** Copyright (C) 1989 by Jef Poskanzer.
+**
+** Permission to use, copy, modify, and distribute this software and its
+** documentation for any purpose and without fee is hereby granted, provided
+** that the above copyright notice appear in all copies and that both that
+** copyright notice and this permission notice appear in supporting
+** documentation.  This software is provided "as is" without express or
+** implied warranty.
+*/
+
+#include "pgm.h"
+#include "mallocvar.h"
+
+int
+main( argc, argv )
+    int argc;
+    char *argv[];
+{
+    FILE *ifp;
+    gray maxval, *grayrow;
+    register gray *gP;
+    int argn, rows, cols, format, row;
+    int i, *hist, *rcount, count, size;
+    register int col;
+    const char * const usage = "[pgmfile]";
+
+    pgm_init( &argc, argv );
+
+    argn = 1;
+
+    if ( argn < argc )
+	{
+        ifp = pm_openr( argv[argn] );
+        argn++;
+	}
+    else
+        ifp = stdin;
+
+    if ( argn != argc )
+        pm_usage( usage );
+
+    pgm_readpgminit( ifp, &cols, &rows, &maxval, &format );
+    grayrow = pgm_allocrow( cols );
+
+    /* Build histogram. */
+    MALLOCARRAY(hist, maxval + 1);
+    MALLOCARRAY(rcount, maxval + 1);
+    if ( hist == NULL || rcount == NULL )
+        pm_error( "out of memory" );
+    for ( i = 0; i <= maxval; i++ )
+        hist[i] = 0;
+    for ( row = 0; row < rows; row++ )
+	{
+        pgm_readpgmrow( ifp, grayrow, cols, maxval, format );
+        for ( col = 0, gP = grayrow; col < cols; col++, gP++ )
+            hist[(int) *gP]++;
+	}
+
+    pm_close( ifp );
+
+    /* Compute count-down */
+    count = 0;
+    for ( i = maxval; i >= 0; i-- )
+	{
+        count += hist[i];
+        rcount[i] = count;
+	}
+
+    /* And print it. */
+    printf( "value\tcount\tb%%\tw%%\n" );
+    printf( "-----\t-----\t--\t--\n" );
+    count = 0;
+    size = rows * cols;
+    for ( i = 0; i <= maxval; i++ )
+        if ( hist[i] > 0 )
+	    {
+            count += hist[i];
+            printf(
+                "%d\t%d\t%5.3g%%\t%5.3g%%\n", i, hist[i],
+                (float) count * 100.0 / size, 
+                (float) rcount[i] * 100.0 / size );
+	    }
+
+    exit( 0 );
+}