private bool checkChecksum() { ExpandedPair firstPair = pairs[0]; DataCharacter checkCharacter = firstPair.LeftChar; DataCharacter firstCharacter = firstPair.RightChar; if (firstCharacter == null) { return(false); } int checksum = firstCharacter.ChecksumPortion; int s = 2; for (int i = 1; i < pairs.Count; ++i) { ExpandedPair currentPair = pairs[i]; checksum += currentPair.LeftChar.ChecksumPortion; s++; DataCharacter currentRightChar = currentPair.RightChar; if (currentRightChar != null) { checksum += currentRightChar.ChecksumPortion; s++; } } checksum %= 211; int checkCharacterValue = 211 * (s - 4) + checksum; return(checkCharacterValue == checkCharacter.Value); }
// Not private for testing internal bool decodeRow2pairs(int rowNumber, BitArray row) { while (true) { ExpandedPair nextPair = retrieveNextPair(row, pairs, rowNumber); if (nextPair == null) { return(false); } pairs.Add(nextPair); if (nextPair.MayBeLast) { if (checkChecksum()) { return(true); } if (nextPair.MustBeLast) { return(false); } } } }
internal static BitArray buildBitArray(ArrayList pairs) { int charNumber = (pairs.Count << 1) - 1; if (((ExpandedPair)(pairs[pairs.Count - 1])).RightChar == null) { charNumber -= 1; } int size = 12 * charNumber; BitArray binary = new BitArray(size); int accPos = 0; ExpandedPair firstPair = (ExpandedPair)pairs[0]; int firstValue = firstPair.RightChar.Value; for (int i = 11; i >= 0; --i) { if ((firstValue & (1 << i)) != 0) { binary[accPos] = true; } accPos++; } for (int i = 1; i < pairs.Count; ++i) { ExpandedPair currentPair = (ExpandedPair)pairs[i]; int leftValue = currentPair.LeftChar.Value; for (int j = 11; j >= 0; --j) { if ((leftValue & (1 << j)) != 0) { binary[accPos] = true; } accPos++; } if (currentPair.RightChar != null) { int rightValue = currentPair.RightChar.Value; for (int j = 11; j >= 0; --j) { if ((rightValue & (1 << j)) != 0) { binary[accPos] = true; } accPos++; } } } return(binary); }
override public bool Equals(Object o) { if (!(o is ExpandedPair)) { return(false); } ExpandedPair that = (ExpandedPair)o; return (EqualsOrNull(LeftChar, that.LeftChar) && EqualsOrNull(RightChar, that.RightChar) && EqualsOrNull(FinderPattern, that.FinderPattern)); }
// Not private for testing internal bool decodeRow2pairs(int rowNumber, BitArray row) { while (true) { ExpandedPair nextPair = retrieveNextPair(row, this.pairs, rowNumber); if (nextPair == null) { break; } pairs.Add(nextPair); //System.out.println(this.pairs.size()+" pairs found so far on row "+rowNumber+": "+this.pairs); // exit this loop when retrieveNextPair() fails and throws } if (pairs.Count == 0) { return(false); } // TODO: verify sequence of finder patterns as in checkPairSequence() if (checkChecksum()) { return(true); } bool tryStackedDecode = rows.Count != 0; bool wasReversed = false; // TODO: deal with reversed rows storeRow(rowNumber, wasReversed); if (tryStackedDecode) { // When the image is 180-rotated, then rows are sorted in wrong dirrection. // Try twice with both the directions. List <ExpandedPair> ps = checkRows(false); if (ps != null) { return(true); } ps = checkRows(true); if (ps != null) { return(true); } } return(false); }
private bool findNextPair(BitArray row, List <ExpandedPair> previousPairs, int forcedOffset) { int[] counters = getDecodeFinderCounters(); counters[0] = 0; counters[1] = 0; counters[2] = 0; counters[3] = 0; int width = row.Size; int rowOffset; if (forcedOffset >= 0) { rowOffset = forcedOffset; } else if (previousPairs.Count == 0) { rowOffset = 0; } else { ExpandedPair lastPair = previousPairs[previousPairs.Count - 1]; rowOffset = lastPair.FinderPattern.StartEnd[1]; } bool searchingEvenPair = previousPairs.Count % 2 != 0; if (startFromEven) { searchingEvenPair = !searchingEvenPair; } bool isWhite = false; while (rowOffset < width) { isWhite = !row[rowOffset]; if (!isWhite) { break; } rowOffset++; } int counterPosition = 0; int patternStart = rowOffset; for (int x = rowOffset; x < width; x++) { if (row[x] != isWhite) { counters[counterPosition]++; } else { if (counterPosition == 3) { if (searchingEvenPair) { reverseCounters(counters); } if (isFinderPattern(counters)) { startEnd[0] = patternStart; startEnd[1] = x; return(true); } if (searchingEvenPair) { reverseCounters(counters); } patternStart += counters[0] + counters[1]; counters[0] = counters[2]; counters[1] = counters[3]; counters[2] = 0; counters[3] = 0; counterPosition--; } else { counterPosition++; } counters[counterPosition] = 1; isWhite = !isWhite; } } return(false); }