}
maxNarrowCounter = minCounter;
wideCounters = 0;
+ int totalWideCountersWidth = 0;
int pattern = 0;
for (int i = 0; i < numCounters; i++) {
+ int counter = counters[i];
if (counters[i] > maxNarrowCounter) {
pattern |= 1 << (numCounters - 1 - i);
wideCounters++;
+ totalWideCountersWidth += counter;
}
}
if (wideCounters == 3) {
+ // Found 3 wide counters, but are they close enough in width?
+ // We can perform a cheap, conservative check to see if any individual
+ // counter is more than 1.5 times the average:
+ for (int i = 0; i < numCounters && wideCounters > 0; i++) {
+ int counter = counters[i];
+ if (counters[i] > maxNarrowCounter) {
+ wideCounters--;
+ // totalWideCountersWidth = 3 * average, so this checks if counter >= 3/2 * average
+ if ((counter << 1) >= totalWideCountersWidth) {
+ throw new ReaderException("Wide bars vary too much in width, rejecting");
+ }
+ }
+ }
return pattern;
}
} while (wideCounters > 3);