find method

UPatternPoint? find()

Implementation

UPatternPoint? find() {
  final int maxJ = startX + width;
  final int middleI = startY + (height ~/ 2);
  final Int32List stateCount = Int32List(3);
  for (int iGen = 0; iGen < height; iGen++) {
    final int i = middleI + ((iGen & 0x01) == 0 ? (iGen + 1) ~/ 2 : -((iGen + 1) ~/ 2));
    if (i < 0 || i >= image.height) continue;
    stateCount[0] = 0;
    stateCount[1] = 0;
    stateCount[2] = 0;
    int j = startX;
    while (j < maxJ && !image.get(j, i)) {
      j++;
    }
    int currentState = 0;
    while (j < maxJ) {
      if (image.get(j, i)) {
        if (currentState == 1) {
          stateCount[1]++;
        } else {
          if (currentState == 2) {
            if (_foundPatternCross(stateCount)) {
              final UPatternPoint? confirmed = _handlePossibleCenter(stateCount, i, j);
              if (confirmed != null) return confirmed;
            }
            stateCount[0] = stateCount[2];
            stateCount[1] = 1;
            stateCount[2] = 0;
            currentState = 1;
          } else {
            currentState++;
            stateCount[currentState]++;
          }
        }
      } else {
        if (currentState == 1) currentState++;
        stateCount[currentState]++;
      }
      j++;
    }
    if (_foundPatternCross(stateCount)) {
      final UPatternPoint? confirmed = _handlePossibleCenter(stateCount, i, maxJ);
      if (confirmed != null) return confirmed;
    }
  }
  return _possibleCenters.isNotEmpty ? _possibleCenters.first : null;
}