about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2010-02-17 03:34:15 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2010-02-17 03:34:15 +0000
commitda4ec0abe56f3ee85308dee82494ad41c2fdabf4 (patch)
treeafc43fc8ca8830c8274da014a60e0bc134f968ab
parent1eeb51dabc467b999332a156b9c045a6d8426fba (diff)
downloadnetpbm-mirror-da4ec0abe56f3ee85308dee82494ad41c2fdabf4.tar.gz
netpbm-mirror-da4ec0abe56f3ee85308dee82494ad41c2fdabf4.tar.xz
netpbm-mirror-da4ec0abe56f3ee85308dee82494ad41c2fdabf4.zip
Fix crash with -width
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1126 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r--analyzer/pnmhistmap.c76
-rw-r--r--doc/HISTORY2
2 files changed, 40 insertions, 38 deletions
diff --git a/analyzer/pnmhistmap.c b/analyzer/pnmhistmap.c
index 1de0f9c5..c45ab0c6 100644
--- a/analyzer/pnmhistmap.c
+++ b/analyzer/pnmhistmap.c
@@ -27,8 +27,6 @@
 
 static double const epsilon = .00001;
 
-#define SCALE_H(value) (hscale_unity ? (value) : (int)((value) * hscale))
-
 enum wantedColor {WANT_RED=0, WANT_GRN=1, WANT_BLU=2};
 
 struct cmdlineInfo {
@@ -163,6 +161,25 @@ clipHistogram(unsigned int * const hist,
 
 
 static void
+countComp(xelval         const value,
+          xelval         const startval,
+          xelval         const endval,
+          unsigned int   const histWidth,
+          unsigned int * const hist) {
+
+    double const hscale = (float)(histWidth-1) / (endval - startval - 1);
+
+    if (value >= startval && value < endval) {
+        unsigned int const bin = ROUNDU((value-startval) * hscale);
+
+        assert(bin < histWidth);
+        ++hist[bin];
+    }
+}
+
+
+
+static void
 pgmHist(FILE *       const ifP,
         int          const cols,
         int          const rows,
@@ -177,10 +194,7 @@ pgmHist(FILE *       const ifP,
         unsigned int const histWidth,
         unsigned int const histHeight,
         bool         const clipSpec,
-        unsigned int const clipCount,
-        double       const hscale) {
-
-    bool const hscale_unity = hscale - 1 < epsilon;
+        unsigned int const clipCount) {
 
     gray * grayrow;
     bit ** bits;
@@ -207,12 +221,8 @@ pgmHist(FILE *       const ifP,
 
     for (i = rows; i > 0; --i) {
         pgm_readpgmrow (ifP, grayrow, cols, maxval, format);
-        for (j = cols-1; j >= 0; --j) {
-            int const value = grayrow[j];
-
-            if (value >= startval && value < endval)
-                ++ghist[SCALE_H(value-startval)];
-        }
+        for (j = cols-1; j >= 0; --j)
+            countComp(grayrow[j], startval, endval, histWidth, ghist);
     }
     pgm_freerow(grayrow);
 
@@ -317,24 +327,6 @@ clipHistogramAll(unsigned int * const hist[3],
 
 
 static void
-countComp(xelval         const value,
-          xelval         const startval,
-          xelval         const endval,
-          unsigned int   const histWidth,
-          unsigned int * const hist) {
-
-    double const hscale = (float)histWidth / (endval - startval);
-
-    if (value >= startval && value < endval) {
-        unsigned int const bin = ROUNDU((value-startval) * hscale);
-        assert(bin < histWidth);
-        ++hist[bin];
-    }
-}
-
-
-
-static void
 fillPpmBins(FILE *          const ifP,
             unsigned int    const cols,
             unsigned int    const rows,
@@ -478,6 +470,19 @@ ppmHist(FILE *       const ifP,
 
 
 
+static void
+reportScale(unsigned int const histWidth,
+            unsigned int const range,
+            bool         const verbose) {
+
+    double const hscale = (float)(histWidth-1) / (range-1);
+
+    if (hscale - 1.0 < epsilon && verbose)
+        pm_message("Horizontal scale factor: %g", hscale);
+}
+
+
+
 int
 main(int argc, const char ** argv) {
 
@@ -488,8 +493,7 @@ main(int argc, const char ** argv) {
     int format;
     unsigned int histWidth;
     unsigned int range;
-    double hscale;
-    int hmax;
+    unsigned int hmax;
     xelval startval, endval;
 
     pm_proginit(&argc, argv);
@@ -510,11 +514,7 @@ main(int argc, const char ** argv) {
     else
         histWidth = range;
 
-    hscale = (float)histWidth / range;
-    if (hscale - 1.0 < epsilon && cmdline.verbose)
-        pm_message("Horizontal scale factor: %g (maxval = %u)", 
-                   hscale, maxval);
-
+    reportScale(histWidth, range, cmdline.verbose);
     if (cmdline.nmaxSpec)
         hmax = cols * rows / histWidth * cmdline.nmax;
 
@@ -530,7 +530,7 @@ main(int argc, const char ** argv) {
         pgmHist(ifP, cols, rows, maxval, format,
                 cmdline.dots, cmdline.white, cmdline.black,
                 cmdline.verbose, startval, endval,
-                histWidth, cmdline.height, cmdline.nmaxSpec, hmax, hscale);
+                histWidth, cmdline.height, cmdline.nmaxSpec, hmax);
         break;
     case PBM_TYPE:
         pm_error("Cannot do a histogram of a a PBM file");
diff --git a/doc/HISTORY b/doc/HISTORY
index fe4a7f76..81aa0da3 100644
--- a/doc/HISTORY
+++ b/doc/HISTORY
@@ -24,6 +24,8 @@ not yet  BJH  Release 10.50.00
               pamflip: Speed up for most images.  Thanks Prophet of the Way
               <afu@wta.att.ne.jp>.
 
+              pnmhistmap: Fix crash with -width.  Always broken.
+
               libppmd/ppmpat: fix wild pointer in ppmd_fill_drawprocp();
               broken in 10.47.