/** * Reads a bit from the stream. Returns a boolean (true for 1; false for 0) if a bit is available, * or throws an EOFException if the end of stream is reached. * * @return the bit that was read (true = 1; false = 0) * @throws IOException if the stream is closed or another I/O error occurs * @throws EOFException when the next bit cannot be read because the end of stream is reached */ protected boolean doReadBit() throws IOException, EOFException { if (currentIndex >= bitArray .length()) // also reads a new byte from underlying stream if needed! (will also check // for closedness) throw new EOFException("End of stream reached"); return bitArray.get(currentIndex++); }
@Override boolean clean(final IntObjMap<Var> decl, final BitArray used) { // [LW] does not fix {@link #vars} final int len = preExpr.length; for (int p = 0; p < post.length; p++) { if (!used.get(post[p].id)) { preExpr = Array.delete(preExpr, p); post = Array.delete(post, p--); } } return preExpr.length < len; }
/** * Skip all whitespace until we get to the first black line. * * @param row row of black/white values to search * @return index of the first black line. * @throws NotFoundException Throws exception if no black lines are found in the row */ private static int skipWhiteSpace(BitArray row) throws NotFoundException { int width = row.getSize(); int endStart = 0; while (endStart < width) { if (row.get(endStart)) { break; } endStart++; } if (endStart == width) { throw NotFoundException.getNotFoundInstance(); } return endStart; }
/** * The start & end patterns must be pre/post fixed by a quiet zone. This zone must be at least 10 * times the width of a narrow line. Scan back until we either get to the start of the barcode or * match the necessary number of quiet zone pixels. * * <p>Note: Its assumed the row is reversed when using this method to find quiet zone after the * end pattern. * * <p>ref: http://www.barcode-1.net/i25code.html * * @param row bit array representing the scanned barcode. * @param startPattern index into row of the start or end pattern. * @throws NotFoundException if the quiet zone cannot be found, a ReaderException is thrown. */ private void validateQuietZone(BitArray row, int startPattern) throws NotFoundException { int quietCount = this.narrowLineWidth * 10; // expect to find this many pixels of quiet zone for (int i = startPattern - 1; quietCount > 0 && i >= 0; i--) { if (row.get(i)) { break; } quietCount--; } if (quietCount != 0) { // Unable to find the necessary number of quiet zone pixels. throw NotFoundException.getNotFoundInstance(); } }
@Override public <T> boolean mightContain( T object, Funnel<? super T> funnel, int numHashFunctions, BitArray bits) { long hash64 = Hashing.murmur3_128().newHasher().putObject(object, funnel).hash().asLong(); int hash1 = (int) hash64; int hash2 = (int) (hash64 >>> 32); for (int i = 1; i <= numHashFunctions; i++) { int nextHash = hash1 + i * hash2; if (nextHash < 0) { nextHash = ~nextHash; } // up to here, the code is identical with the previous method if (!bits.get(nextHash % bits.size())) { return false; } } return true; }
/** * @param row row of black/white values to search * @param rowOffset position to start search * @param pattern pattern of counts of number of black and white pixels that are being searched * for as a pattern * @return start/end horizontal offset of guard pattern, as an array of two ints * @throws NotFoundException if pattern is not found */ private static int[] findGuardPattern(BitArray row, int rowOffset, int[] pattern) throws NotFoundException { // TODO: This is very similar to implementation in UPCEANReader. Consider if they can be // merged to a single method. int patternLength = pattern.length; int[] counters = new int[patternLength]; int width = row.getSize(); boolean isWhite = false; int counterPosition = 0; int patternStart = rowOffset; for (int x = rowOffset; x < width; x++) { boolean pixel = row.get(x); if (pixel ^ 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]; for (int y = 2; y < patternLength; y++) { counters[y - 2] = counters[y]; } counters[patternLength - 2] = 0; counters[patternLength - 1] = 0; counterPosition--; } else { counterPosition++; } counters[counterPosition] = 1; isWhite = !isWhite; } } throw NotFoundException.getNotFoundInstance(); }