void stampIPOs() {
   calAveRadius();
   int i, j;
   for (i = 0; i < h; i++) {
     for (j = 0; j < w; j++) {
       m_pnStamp[i][j] = 0;
     }
   }
   int numRings = m_cvRings.size();
   m_cIMSC.reset();
   Point pt;
   int index, step = 0, r0;
   ImageShape shape;
   while (true) {
     pt = m_cIMSC.getPosition();
     index = getPercentileIndex(step);
     r0 = m_pnAveRadius[index];
     for (i = 0; i <= r0; i++) {
       shape = m_cvRings.get(i);
       shape.setCenter(pt);
       ImageShapeHandler.setValue(m_pnStamp, shape, m_nExcludingType);
     }
     if (m_cIMSC.done()) break;
     m_cIMSC.move();
     step++;
   }
 }
  void constructRings() {
    m_dvRs = new ArrayList();
    m_cvRings = new ArrayList();
    m_cvRings.add(new CircleImage(0));
    m_dvRs.add(0.);
    double r = 1., dr = 0.001;
    double ri = r - dr, ro = r + dr;
    m_cvRings.add(new Ring(ri, ro));
    m_dvRs.add(r);

    r = Math.sqrt(2);
    ri = r - dr;
    ro = r + dr;
    m_cvRings.add(new Ring(ri, ro));
    m_dvRs.add(r);

    int nr;
    for (nr = 2; nr <= m_nRMax; nr++) {
      m_cvRings.add(new Ring(nr - 1, nr));
      m_dvRs.add((double) nr);
    }

    m_cRefRing = new Ring(m_nRRefI, m_nRRefO);
    m_cvRings.add(m_cRefRing);
    m_dvRs.add((double) m_nRMax + 1);
    ImageShapeHandler.setFrameRanges(m_cvRings, new intRange(0, w - 1), new intRange(0, h - 1));
  }
 int calCompensatedPixels() {
   int numRings = m_cvRings.size();
   CommonStatisticsMethods.copyArray(m_pnPixels, m_pnPixelsCompensated);
   CommonMethods.displayPixels(m_pnPixelsCompensated, "before compensation", impl.getType());
   m_cIMSC.reset();
   Point pt;
   int index, step = 0, pixel, i, r0;
   double dm, dsur;
   ImageShape shape;
   while (true) {
     pt = m_cIMSC.getPosition();
     index = getPercentileIndex(step);
     dsur = m_pcRingMeanSems_Mean[index][m_nRMax].mean;
     r0 = m_pnAveRadius[index];
     //
     // m_pnPixelsCompensated[pt.y][pt.x]-=(int)(m_cPixelMeanSems_trail[index].mean-dsur);
     for (i = 0; i < numRings; i++) {
       pixel = (int) (m_pcRingMeanSems_Mean[index][i].mean - dsur);
       if (pixel < 1) break;
       shape = m_cvRings.get(i);
       shape.setCenter(pt);
       ImageShapeHandler.addValue(m_pnPixelsCompensated, shape, -pixel);
     }
     if (m_cIMSC.done()) break;
     m_cIMSC.move();
     step++;
   }
   CommonMethods.displayPixels(m_pnPixelsCompensated, "after compensation", impl.getType());
   return 1;
 }
 public void buildIPOGContourShapes(int[][] pixelsCompensated, int[][] pnScratch, int cutoff) {
   int h = pixelsCompensated.length, w = pixelsCompensated[0].length;
   intRange xRange3 = new intRange(), yRange3 = new intRange();
   IPOGaussianNodeHandler.getRanges3(IPOGNode.IPOGs, xRange, yRange);
   xRange.setCommonRange(new intRange(0, w - 1));
   yRange.setCommonRange(new intRange(0, h - 1));
   CommonStatisticsMethods.copyArray(
       pixelsCompensated,
       pnScratch,
       yRange.getMin(),
       yRange.getMax(),
       xRange.getMin(),
       xRange.getMax());
   IPOGaussianNodeHandler.getSuperImposition(pnScratch, IPOGNode.IPOGs);
   if (pnScratch[cy][cx] < cutoff) {
     cContourShape = new CircleImage(3);
     cContourShape.setCenter(new Point(cx, cy));
     cContourShape.setFrameRanges(new intRange(0, w - 1), new intRange(0, h - 1));
   } else {
     ArrayList<Point> contour =
         ContourFollower.getContour_Out(
             pnScratch, w, h, new Point(cx, cy), cutoff, Integer.MAX_VALUE, true);
     cContourShape = ImageShapeHandler.buildImageShape(contour);
   }
 }
  int refinePixelStatitics() {
    stampIPOs();
    int[][] pixelsTemp = m_pnPixels;
    m_pnPixels = m_pnPixelsCompensated;

    m_cIMSC.reset();
    int len = m_cIMSC.m_nTotalSteps + 1;
    int i, c, step = 0;
    Point p = m_cIMSC.getPosition();
    int index = 0;
    ImageShape shape;
    Histogram hist;
    double mean, median;
    int y, y0 = 0;
    int pixel, nRef;
    double dsur;
    int numRings = m_cvRings.size();
    while (true) {
      p = m_cIMSC.getPosition();
      y = p.y;
      if (y > y0)
        IJ.showStatus(
            "refinePixelStatistics "
                + PrintAssist.ToString(y)
                + "-th line of "
                + PrintAssist.ToString(h)
                + " lines!");
      index = getPercentileIndex(step);
      dsur = m_pcRingMeanSems_Mean[index][m_nRMax].mean;
      ImageShapeHandler.copyElements(m_pnPixelsCompensated, m_pnPixelsTemp, m_cvRings, p);
      for (i = 0; i < numRings; i++) {
        pixel = (int) (m_pcRingMeanSems_Mean[index][i].mean - dsur);
        if (pixel < 1) break;
        shape = m_cvRings.get(i);
        shape.setCenter(p);
        ImageShapeHandler.addValue(m_pnPixelsTemp, shape, pixel);
      }
      nRef =
          (int) ImageShapeHandler.getMean(m_pnPixelsTemp, m_cRefRing, m_pnStamp, m_nExcludingType);
      CommonMethods.fillHistogram(
          m_pnPixelsTemp,
          nRef,
          m_cvRings.get(numRings - 1),
          p,
          m_cvHists.get(numRings - 1),
          m_pnStamp,
          m_nExcludingType);
      CommonMethods.fillHistograms(m_pnPixelsTemp, nRef, m_cvRings, p, m_cvHists);
      for (i = 0; i < numRings; i++) {
        hist = m_cvHists.get(i);
        mean = hist.getMeanSem().mean;
        median = hist.getPercentileValue();
        m_pdRingMean[step][i] = mean;
        m_pdRingMedian[step][i] = median;
      }
      //            m_pnPixels_trail[step]=m_pnPixelsTemp[p.y][p.x]-nRef;
      if (m_cIMSC.done()) break;
      m_cIMSC.move();
      step++;
    }
    for (c = 0; c < numRings; c++) {
      m_pcMeanSems_Median[c] = CommonStatisticsMethods.buildMeanSem(m_pdRingMedian, c);
      m_pcMeanSems_Mean[c] = CommonStatisticsMethods.buildMeanSem(m_pdRingMean, c);
    }
    if (m_bPresetPoints) {
      len = pixelsOfPresetPoints.size();
      int num = len / m_nPercentileDivision;
      int rI, rF;
      for (i = 0; i < m_nPercentileDivision; i++) {
        rI = Math.max(0, (int) ((i - .5) * num));
        rF = Math.min(len - 1, (int) ((i + .5) * num));
        for (c = 0; c < numRings; c++) {
          m_pcRingMeanSems_Median[i][c] =
              CommonStatisticsMethods.buildMeanSem(m_pdRingMedian, rI, rF, 1, c);
          m_pcRingMeanSems_Mean[i][c] =
              CommonStatisticsMethods.buildMeanSem(m_pdRingMean, rI, rF, 1, c);
        }
      }
    }
    m_pnPixels = pixelsTemp;
    return 1;
  }