detect method

List<Offset>? detect()

Implementation

List<Offset>? detect() {
  int left = _leftInit;
  int right = _rightInit;
  int up = _upInit;
  int down = _downInit;
  if (up < 0 || left < 0 || down >= image.height || right >= image.width) return null;

  bool sizeExceeded = false;
  bool aBlackPointFoundOnBorder = true;
  bool atLeastOneBlackPointFoundOnRight = false;
  bool atLeastOneBlackPointFoundOnBottom = false;
  bool atLeastOneBlackPointFoundOnLeft = false;
  bool atLeastOneBlackPointFoundOnTop = false;

  while (aBlackPointFoundOnBorder) {
    aBlackPointFoundOnBorder = false;

    bool rightBorderNotWhite = true;
    while ((rightBorderNotWhite || !atLeastOneBlackPointFoundOnRight) && right < image.width) {
      rightBorderNotWhite = _containsBlackPoint(up, down, right, false);
      if (rightBorderNotWhite) {
        right++;
        aBlackPointFoundOnBorder = true;
        atLeastOneBlackPointFoundOnRight = true;
      } else if (!atLeastOneBlackPointFoundOnRight) {
        right++;
      }
    }
    if (right >= image.width) {
      sizeExceeded = true;
      break;
    }

    bool bottomBorderNotWhite = true;
    while ((bottomBorderNotWhite || !atLeastOneBlackPointFoundOnBottom) && down < image.height) {
      bottomBorderNotWhite = _containsBlackPoint(left, right, down, true);
      if (bottomBorderNotWhite) {
        down++;
        aBlackPointFoundOnBorder = true;
        atLeastOneBlackPointFoundOnBottom = true;
      } else if (!atLeastOneBlackPointFoundOnBottom) {
        down++;
      }
    }
    if (down >= image.height) {
      sizeExceeded = true;
      break;
    }

    bool leftBorderNotWhite = true;
    while ((leftBorderNotWhite || !atLeastOneBlackPointFoundOnLeft) && left >= 0) {
      leftBorderNotWhite = _containsBlackPoint(up, down, left, false);
      if (leftBorderNotWhite) {
        left--;
        aBlackPointFoundOnBorder = true;
        atLeastOneBlackPointFoundOnLeft = true;
      } else if (!atLeastOneBlackPointFoundOnLeft) {
        left--;
      }
    }
    if (left < 0) {
      sizeExceeded = true;
      break;
    }

    bool topBorderNotWhite = true;
    while ((topBorderNotWhite || !atLeastOneBlackPointFoundOnTop) && up >= 0) {
      topBorderNotWhite = _containsBlackPoint(left, right, up, true);
      if (topBorderNotWhite) {
        up--;
        aBlackPointFoundOnBorder = true;
        atLeastOneBlackPointFoundOnTop = true;
      } else if (!atLeastOneBlackPointFoundOnTop) {
        up--;
      }
    }
    if (up < 0) {
      sizeExceeded = true;
      break;
    }
  }

  if (sizeExceeded) return null;

  final int maxSize = right - left;
  Offset? z;
  for (int i = 1; z == null && i < maxSize; i++) {
    z = _getBlackPointOnSegment(left.toDouble(), (down - i).toDouble(), (left + i).toDouble(), down.toDouble());
  }
  if (z == null) return null;

  Offset? t;
  for (int i = 1; t == null && i < maxSize; i++) {
    t = _getBlackPointOnSegment(left.toDouble(), (up + i).toDouble(), (left + i).toDouble(), up.toDouble());
  }
  if (t == null) return null;

  Offset? x;
  for (int i = 1; x == null && i < maxSize; i++) {
    x = _getBlackPointOnSegment(right.toDouble(), (up + i).toDouble(), (right - i).toDouble(), up.toDouble());
  }
  if (x == null) return null;

  Offset? y;
  for (int i = 1; y == null && i < maxSize; i++) {
    y = _getBlackPointOnSegment(right.toDouble(), (down - i).toDouble(), (right - i).toDouble(), down.toDouble());
  }
  if (y == null) return null;

  return _centerEdges(y, z, x, t);
}