private static int[] findGuardPattern(BitArray row,
                                        int rowOffset,
                                        boolean whiteFirst,
                                        int[] pattern,
                                        int[] counters) throws NotFoundException {
    int width = row.getSize();
    rowOffset = whiteFirst ? row.getNextUnset(rowOffset) : row.getNextSet(rowOffset);
    int counterPosition = 0;
    int patternStart = rowOffset;
    int patternLength = pattern.length;
    boolean isWhite = whiteFirst;
    for (int x = rowOffset; x < width; x++) {
      if (row.get(x) != isWhite) {
        counters[counterPosition]++;
      } else {
        if (counterPosition == patternLength - 1) {
          if (patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) {
            return new int[]{patternStart, x};
          }
          patternStart += counters[0] + counters[1];
          System.arraycopy(counters, 2, counters, 0, counterPosition - 1);
          counters[counterPosition - 1] = 0;
          counters[counterPosition] = 0;
          counterPosition--;
        } else {
          counterPosition++;
        }
        counters[counterPosition] = 1;
        isWhite = !isWhite;
      }
    }
    throw NotFoundException.getNotFoundInstance();
  }
private static int [ ] findGuardPattern ( BitArray row , int rowOffset , boolean whiteFirst , int [ ] pattern , int [ ] counters ) throws NotFoundException { int width = row . getSize ( ) ; rowOffset = whiteFirst ? row . getNextUnset ( rowOffset ) : row . getNextSet ( rowOffset ) ; int counterPosition = 0 ; int patternStart = rowOffset ; int patternLength = pattern . length ; boolean isWhite = whiteFirst ; for ( int x = rowOffset ; x < width ; x ++ ) { if ( row . get ( x ) != isWhite ) { counters [ counterPosition ] ++ ; } else { if ( counterPosition == patternLength - 1 ) { if ( patternMatchVariance ( counters , pattern , MAX_INDIVIDUAL_VARIANCE ) < MAX_AVG_VARIANCE ) { return new int [ ] { patternStart , x } ; } patternStart += counters [ 0 ] + counters [ 1 ] ; System . arraycopy ( counters , 2 , counters , 0 , counterPosition - 1 ) ; counters [ counterPosition - 1 ] = 0 ; counters [ counterPosition ] = 0 ; counterPosition -- ; } else { counterPosition ++ ; } counters [ counterPosition ] = 1 ; isWhite = ! isWhite ; } } throw NotFoundException . getNotFoundInstance ( ) ; }
@param row row of black/white values to search
@param rowOffset position to start search
@param whiteFirst if true, indicates that the pattern specifies white/black/white/...
pixel counts, otherwise, it is interpreted as black/white/black/...
@param pattern pattern of counts of number of black and white pixels that are being
searched for as a pattern
@param counters array of counters, as long as pattern, to re-use
@return start/end horizontal offset of guard pattern, as an array of two ints
@throws NotFoundException if pattern is not found