recordPattern static method

void recordPattern(
  1. BitArray row,
  2. int start,
  3. List<int> counters
)

Records the size of successive runs of white and black pixels in a row, starting at a given point. The values are recorded in the given array, and the number of runs recorded is equal to the size of the array. If the row starts on a white pixel at the given start point, then the first count recorded is the run of white pixels starting from that point; likewise it is the count of a run of black pixels if the row begin on a black pixels at that point.

@param row row to count from @param start offset into row to start at @param counters array into which to record counts @throws NotFoundException if counters cannot be filled entirely from row before running out of pixels

Implementation

static void recordPattern(BitArray row, int start, List<int> counters) {
  final numCounters = counters.length;
  counters.fillRange(0, numCounters, 0);
  final end = row.size;
  if (start >= end) {
    throw NotFoundException.instance;
  }
  bool isWhite = !row.get(start);
  int counterPosition = 0;
  int i = start;
  while (i < end) {
    if (row.get(i) != isWhite) {
      counters[counterPosition]++;
    } else {
      if (++counterPosition == numCounters) {
        break;
      } else {
        counters[counterPosition] = 1;
        isWhite = !isWhite;
      }
    }
    i++;
  }
  // If we read fully the last section of pixels and filled up our counters -- or filled
  // the last counter but ran off the side of the image, OK. Otherwise, a problem.
  if (!(counterPosition == numCounters ||
      (counterPosition == numCounters - 1 && i == end))) {
    throw NotFoundException.instance;
  }
}