// Trace one contour starting at (xS,yS)
  // in direction dS with label label
  // trace one contour starting at (xS,yS) in direction dS
  Contour traceContour(int xS, int yS, int label, int dS, Contour cont) {
    int xT, yT; // T = successor of starting point (xS,yS)
    int xP, yP; // P = previous contour point
    int xC, yC; // C = current contour point
    Point pt = new Point(xS, yS);
    int dNext = findNextPoint(pt, dS);
    cont.addPoint(pt);
    xP = xS;
    yP = yS;
    xC = xT = pt.x;
    yC = yT = pt.y;

    boolean done = (xS == xT && yS == yT); // true if isolated pixel

    while (!done) {
      labelArray[yC][xC] = label;
      pt = new Point(xC, yC);
      int dSearch = (dNext + 6) % 8;
      dNext = findNextPoint(pt, dSearch);
      xP = xC;
      yP = yC;
      xC = pt.x;
      yC = pt.y;
      // are we back at the starting position?
      done = (xP == xS && yP == yS && xC == xT && yC == yT);
      if (!done) {
        cont.addPoint(pt);
      }
    }
    return cont;
  }