// This macro demonstrates the use of the
// getRawStatistics() function.
// It requires the 1.34h preview at
// "http://rsb.info.nih.gov/ij/ij.jar".

 macro "Show Raw Statistics" {
      requires("1.34h");
      getRawStatistics(count, mean, min, max, std, hist);
      row = nResults;
      setResult("Pixel Count ", row, count);
      setResult("Min ", row, min);
      setResult("Max ", row, max);
      setResult("Mean ", row, mean);
      setResult("Mode ", row, getMode(hist));
      setResult("Median ", row, getMedian(hist,count));
      updateResults();
  }

  function getMedian(hist, count) {
      n = hist.length;
      sum = 0;
      i = -1;
      count2 = count/2;
      do {
          sum += hist[++i];
          //print(i, sum, count2);
      } while (sum<=count2);
      return i;
  }

  function getMode(hist) {
      n = hist.length;
      maxCount = 0;
      for (i=0; i<n; i++) {
          count = hist[i];
          if (count>maxCount) {
              maxCount = count;
              mode = i;
          }
      }
      return mode;
  }
