You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZXRSSExpandedReader.m 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * Copyright 2012 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "ZXAbstractExpandedDecoder.h"
  17. #import "ZXBitArray.h"
  18. #import "ZXBitArrayBuilder.h"
  19. #import "ZXErrors.h"
  20. #import "ZXIntArray.h"
  21. #import "ZXResult.h"
  22. #import "ZXRSSDataCharacter.h"
  23. #import "ZXRSSExpandedPair.h"
  24. #import "ZXRSSExpandedReader.h"
  25. #import "ZXRSSExpandedRow.h"
  26. #import "ZXRSSFinderPattern.h"
  27. #import "ZXRSSUtils.h"
  28. const int ZX_SYMBOL_WIDEST[] = {7, 5, 4, 3, 1};
  29. const int ZX_EVEN_TOTAL_SUBSET[] = {4, 20, 52, 104, 204};
  30. const int ZX_GSUM[] = {0, 348, 1388, 2948, 3988};
  31. const int ZX_WEIGHTS[][8] = {
  32. { 1, 3, 9, 27, 81, 32, 96, 77},
  33. { 20, 60, 180, 118, 143, 7, 21, 63},
  34. {189, 145, 13, 39, 117, 140, 209, 205},
  35. {193, 157, 49, 147, 19, 57, 171, 91},
  36. { 62, 186, 136, 197, 169, 85, 44, 132},
  37. {185, 133, 188, 142, 4, 12, 36, 108},
  38. {113, 128, 173, 97, 80, 29, 87, 50},
  39. {150, 28, 84, 41, 123, 158, 52, 156},
  40. { 46, 138, 203, 187, 139, 206, 196, 166},
  41. { 76, 17, 51, 153, 37, 111, 122, 155},
  42. { 43, 129, 176, 106, 107, 110, 119, 146},
  43. { 16, 48, 144, 10, 30, 90, 59, 177},
  44. {109, 116, 137, 200, 178, 112, 125, 164},
  45. { 70, 210, 208, 202, 184, 130, 179, 115},
  46. {134, 191, 151, 31, 93, 68, 204, 190},
  47. {148, 22, 66, 198, 172, 94, 71, 2},
  48. { 6, 18, 54, 162, 64, 192,154, 40},
  49. {120, 149, 25, 75, 14, 42,126, 167},
  50. { 79, 26, 78, 23, 69, 207,199, 175},
  51. {103, 98, 83, 38, 114, 131, 182, 124},
  52. {161, 61, 183, 127, 170, 88, 53, 159},
  53. { 55, 165, 73, 8, 24, 72, 5, 15},
  54. { 45, 135, 194, 160, 58, 174, 100, 89}
  55. };
  56. const int ZX_FINDER_PAT_A = 0;
  57. const int ZX_FINDER_PAT_B = 1;
  58. const int ZX_FINDER_PAT_C = 2;
  59. const int ZX_FINDER_PAT_D = 3;
  60. const int ZX_FINDER_PAT_E = 4;
  61. const int ZX_FINDER_PAT_F = 5;
  62. #define ZX_FINDER_PATTERN_SEQUENCES_LEN 10
  63. #define ZX_FINDER_PATTERN_SEQUENCES_SUBLEN 11
  64. const int ZX_FINDER_PATTERN_SEQUENCES[ZX_FINDER_PATTERN_SEQUENCES_LEN][ZX_FINDER_PATTERN_SEQUENCES_SUBLEN] = {
  65. { ZX_FINDER_PAT_A, ZX_FINDER_PAT_A },
  66. { ZX_FINDER_PAT_A, ZX_FINDER_PAT_B, ZX_FINDER_PAT_B },
  67. { ZX_FINDER_PAT_A, ZX_FINDER_PAT_C, ZX_FINDER_PAT_B, ZX_FINDER_PAT_D },
  68. { ZX_FINDER_PAT_A, ZX_FINDER_PAT_E, ZX_FINDER_PAT_B, ZX_FINDER_PAT_D, ZX_FINDER_PAT_C },
  69. { ZX_FINDER_PAT_A, ZX_FINDER_PAT_E, ZX_FINDER_PAT_B, ZX_FINDER_PAT_D, ZX_FINDER_PAT_D, ZX_FINDER_PAT_F },
  70. { ZX_FINDER_PAT_A, ZX_FINDER_PAT_E, ZX_FINDER_PAT_B, ZX_FINDER_PAT_D, ZX_FINDER_PAT_E, ZX_FINDER_PAT_F, ZX_FINDER_PAT_F },
  71. { ZX_FINDER_PAT_A, ZX_FINDER_PAT_A, ZX_FINDER_PAT_B, ZX_FINDER_PAT_B, ZX_FINDER_PAT_C, ZX_FINDER_PAT_C, ZX_FINDER_PAT_D, ZX_FINDER_PAT_D },
  72. { ZX_FINDER_PAT_A, ZX_FINDER_PAT_A, ZX_FINDER_PAT_B, ZX_FINDER_PAT_B, ZX_FINDER_PAT_C, ZX_FINDER_PAT_C, ZX_FINDER_PAT_D, ZX_FINDER_PAT_E, ZX_FINDER_PAT_E },
  73. { ZX_FINDER_PAT_A, ZX_FINDER_PAT_A, ZX_FINDER_PAT_B, ZX_FINDER_PAT_B, ZX_FINDER_PAT_C, ZX_FINDER_PAT_C, ZX_FINDER_PAT_D, ZX_FINDER_PAT_E, ZX_FINDER_PAT_F, ZX_FINDER_PAT_F },
  74. { ZX_FINDER_PAT_A, ZX_FINDER_PAT_A, ZX_FINDER_PAT_B, ZX_FINDER_PAT_B, ZX_FINDER_PAT_C, ZX_FINDER_PAT_D, ZX_FINDER_PAT_D, ZX_FINDER_PAT_E, ZX_FINDER_PAT_E, ZX_FINDER_PAT_F, ZX_FINDER_PAT_F },
  75. };
  76. @interface ZXRSSExpandedReader ()
  77. @property (nonatomic, strong, readonly) ZXIntArray *startEnd;
  78. @property (nonatomic, strong, readonly) NSMutableArray *pairs;
  79. @property (nonatomic, strong) NSMutableArray *rows;
  80. @property (nonatomic, assign) BOOL startFromEven;
  81. @end
  82. @implementation ZXRSSExpandedReader
  83. - (id)init {
  84. if (self = [super init]) {
  85. _pairs = [NSMutableArray array];
  86. _rows = [NSMutableArray array];
  87. _startFromEven = NO;
  88. _startEnd = [[ZXIntArray alloc] initWithLength:2];
  89. }
  90. return self;
  91. }
  92. - (ZXResult *)decodeRow:(int)rowNumber row:(ZXBitArray *)row hints:(ZXDecodeHints *)hints error:(NSError **)error {
  93. // Rows can start with even pattern in case in prev rows there where odd number of patters.
  94. // So lets try twice
  95. [self.pairs removeAllObjects];
  96. self.startFromEven = NO;
  97. NSMutableArray* pairs = [self decodeRow2pairs:rowNumber row:row error:error];
  98. if (pairs) {
  99. ZXResult *result = [self constructResult:pairs error:error];
  100. if (result) {
  101. return result;
  102. }
  103. }
  104. [self.pairs removeAllObjects];
  105. self.startFromEven = YES;
  106. pairs = [self decodeRow2pairs:rowNumber row:row error:error];
  107. if (!pairs) {
  108. if (error) *error = ZXNotFoundErrorInstance();
  109. return nil;
  110. }
  111. return [self constructResult:pairs error:error];
  112. }
  113. - (void)reset {
  114. [self.pairs removeAllObjects];
  115. [self.rows removeAllObjects];
  116. }
  117. - (NSMutableArray *)decodeRow2pairs:(int)rowNumber row:(ZXBitArray *)row error:(NSError **)error {
  118. while (YES) {
  119. ZXRSSExpandedPair *nextPair = [self retrieveNextPair:row previousPairs:self.pairs rowNumber:rowNumber];
  120. if (!nextPair) {
  121. if ([self.pairs count] == 0) {
  122. return nil;
  123. }
  124. break;
  125. }
  126. [self.pairs addObject:nextPair];
  127. }
  128. // TODO: verify sequence of finder patterns as in checkPairSequence()
  129. if ([self checkChecksum]) {
  130. return self.pairs;
  131. }
  132. BOOL tryStackedDecode = [self.rows count] > 0;
  133. BOOL wasReversed = NO; // TODO: deal with reversed rows
  134. [self storeRow:rowNumber wasReversed:wasReversed];
  135. if (tryStackedDecode) {
  136. // When the image is 180-rotated, then rows are sorted in wrong dirrection.
  137. // Try twice with both the directions.
  138. NSMutableArray *ps = [self checkRows:NO];
  139. if (ps) {
  140. return ps;
  141. }
  142. ps = [self checkRows:YES];
  143. if (ps) {
  144. return ps;
  145. }
  146. }
  147. return nil;
  148. }
  149. - (NSMutableArray *)checkRows:(BOOL)reverse {
  150. // Limit number of rows we are checking
  151. // We use recursive algorithm with pure complexity and don't want it to take forever
  152. // Stacked barcode can have up to 11 rows, so 25 seems resonable enough
  153. if (self.rows.count > 25) {
  154. [self.rows removeAllObjects];
  155. return nil;
  156. }
  157. [self.pairs removeAllObjects];
  158. if (reverse) {
  159. self.rows = [[[self.rows reverseObjectEnumerator] allObjects] mutableCopy];
  160. }
  161. NSMutableArray *ps = [self checkRows:[NSMutableArray array] current:0];
  162. if (reverse) {
  163. self.rows = [[[self.rows reverseObjectEnumerator] allObjects] mutableCopy];
  164. }
  165. return ps;
  166. }
  167. // Try to construct a valid rows sequence
  168. // Recursion is used to implement backtracking
  169. - (NSMutableArray *)checkRows:(NSMutableArray *)collectedRows current:(int)currentRow {
  170. for (int i = currentRow; i < [self.rows count]; i++) {
  171. ZXRSSExpandedRow *row = self.rows[i];
  172. [self.pairs removeAllObjects];
  173. NSUInteger size = [collectedRows count];
  174. for (int j = 0; j < size; j++) {
  175. [self.pairs addObjectsFromArray:[collectedRows[j] pairs]];
  176. }
  177. [self.pairs addObjectsFromArray:row.pairs];
  178. if (![self isValidSequence:self.pairs]) {
  179. continue;
  180. }
  181. if ([self checkChecksum]) {
  182. return self.pairs;
  183. }
  184. NSMutableArray *rs = [NSMutableArray array];
  185. [rs addObjectsFromArray:collectedRows];
  186. [rs addObject:row];
  187. NSMutableArray *ps = [self checkRows:rs current:i + 1];
  188. if (ps) {
  189. return ps;
  190. }
  191. }
  192. return nil;
  193. }
  194. // Whether the pairs form a valid find pattern seqience,
  195. // either complete or a prefix
  196. - (BOOL)isValidSequence:(NSArray *)pairs {
  197. int count = (int)[pairs count];
  198. for (int i = 0, sz = 2; i < ZX_FINDER_PATTERN_SEQUENCES_LEN; i++, sz++) {
  199. if (count > sz) {
  200. continue;
  201. }
  202. BOOL stop = YES;
  203. for (int j = 0; j < count; j++) {
  204. if ([[pairs[j] finderPattern] value] != ZX_FINDER_PATTERN_SEQUENCES[i][j]) {
  205. stop = NO;
  206. break;
  207. }
  208. }
  209. if (stop) {
  210. return YES;
  211. }
  212. }
  213. return NO;
  214. }
  215. - (void)storeRow:(int)rowNumber wasReversed:(BOOL)wasReversed {
  216. // Discard if duplicate above or below; otherwise insert in order by row number.
  217. int insertPos = 0;
  218. BOOL prevIsSame = NO;
  219. BOOL nextIsSame = NO;
  220. while (insertPos < [self.rows count]) {
  221. ZXRSSExpandedRow *erow = self.rows[insertPos];
  222. if (erow.rowNumber > rowNumber) {
  223. nextIsSame = [erow isEquivalent:self.pairs];
  224. break;
  225. }
  226. prevIsSame = [erow isEquivalent:self.pairs];
  227. insertPos++;
  228. }
  229. if (nextIsSame || prevIsSame) {
  230. return;
  231. }
  232. // When the row was partially decoded (e.g. 2 pairs found instead of 3),
  233. // it will prevent us from detecting the barcode.
  234. // Try to merge partial rows
  235. // Check whether the row is part of an allready detected row
  236. if ([self isPartialRow:self.pairs of:self.rows]) {
  237. return;
  238. }
  239. [self.rows insertObject:[[ZXRSSExpandedRow alloc] initWithPairs:self.pairs rowNumber:rowNumber wasReversed:wasReversed] atIndex:insertPos];
  240. [self removePartialRows:self.pairs from:self.rows];
  241. }
  242. // Remove all the rows that contains only specified pairs
  243. - (void)removePartialRows:(NSArray *)pairs from:(NSMutableArray *)rows {
  244. NSMutableArray *toRemove = [NSMutableArray array];
  245. for (ZXRSSExpandedRow *r in rows) {
  246. if ([r.pairs count] == [pairs count]) {
  247. continue;
  248. }
  249. BOOL allFound = YES;
  250. for (ZXRSSExpandedPair *p in r.pairs) {
  251. BOOL found = NO;
  252. for (ZXRSSExpandedPair *pp in pairs) {
  253. if ([p isEqual:pp]) {
  254. found = YES;
  255. break;
  256. }
  257. }
  258. if (!found) {
  259. allFound = NO;
  260. break;
  261. }
  262. }
  263. if (allFound) {
  264. [toRemove addObject:r];
  265. }
  266. }
  267. for (ZXRSSExpandedRow *r in toRemove) {
  268. [rows removeObject:r];
  269. }
  270. }
  271. - (BOOL)isPartialRow:(NSArray *)pairs of:(NSArray *)rows {
  272. for (ZXRSSExpandedRow *r in rows) {
  273. BOOL allFound = YES;
  274. for (ZXRSSExpandedPair *p in pairs) {
  275. BOOL found = NO;
  276. for (ZXRSSExpandedPair *pp in r.pairs) {
  277. if ([p isEqual:pp]) {
  278. found = YES;
  279. break;
  280. }
  281. }
  282. if (!found) {
  283. allFound = NO;
  284. break;
  285. }
  286. }
  287. if (allFound) {
  288. // the row 'r' contain all the pairs from 'pairs'
  289. return YES;
  290. }
  291. }
  292. return NO;
  293. }
  294. - (ZXResult *)constructResult:(NSMutableArray *)pairs error:(NSError **)error {
  295. ZXBitArray *binary = [ZXBitArrayBuilder buildBitArray:pairs];
  296. ZXAbstractExpandedDecoder *decoder = [ZXAbstractExpandedDecoder createDecoder:binary];
  297. NSString *resultingString = [decoder parseInformationWithError:error];
  298. if (!resultingString) {
  299. return nil;
  300. }
  301. NSArray *firstPoints = [[((ZXRSSExpandedPair *)_pairs[0]) finderPattern] resultPoints];
  302. NSArray *lastPoints = [[((ZXRSSExpandedPair *)[_pairs lastObject]) finderPattern] resultPoints];
  303. return [ZXResult resultWithText:resultingString
  304. rawBytes:nil
  305. resultPoints:@[firstPoints[0], firstPoints[1], lastPoints[0], lastPoints[1]]
  306. format:kBarcodeFormatRSSExpanded];
  307. }
  308. - (BOOL)checkChecksum {
  309. ZXRSSExpandedPair *firstPair = self.pairs[0];
  310. ZXRSSDataCharacter *checkCharacter = firstPair.leftChar;
  311. ZXRSSDataCharacter *firstCharacter = firstPair.rightChar;
  312. if (!firstCharacter) {
  313. return NO;
  314. }
  315. int checksum = [firstCharacter checksumPortion];
  316. int s = 2;
  317. for (int i = 1; i < self.pairs.count; ++i) {
  318. ZXRSSExpandedPair *currentPair = self.pairs[i];
  319. checksum += currentPair.leftChar.checksumPortion;
  320. s++;
  321. ZXRSSDataCharacter *currentRightChar = currentPair.rightChar;
  322. if (currentRightChar != nil) {
  323. checksum += currentRightChar.checksumPortion;
  324. s++;
  325. }
  326. }
  327. checksum %= 211;
  328. int checkCharacterValue = 211 * (s - 4) + checksum;
  329. return checkCharacterValue == checkCharacter.value;
  330. }
  331. - (int)nextSecondBar:(ZXBitArray *)row initialPos:(int)initialPos {
  332. int currentPos;
  333. if ([row get:initialPos]) {
  334. currentPos = [row nextUnset:initialPos];
  335. currentPos = [row nextSet:currentPos];
  336. } else {
  337. currentPos = [row nextSet:initialPos];
  338. currentPos = [row nextUnset:currentPos];
  339. }
  340. return currentPos;
  341. }
  342. - (ZXRSSExpandedPair *)retrieveNextPair:(ZXBitArray *)row previousPairs:(NSMutableArray *)previousPairs rowNumber:(int)rowNumber {
  343. BOOL isOddPattern = [previousPairs count] % 2 == 0;
  344. if (self.startFromEven) {
  345. isOddPattern = !isOddPattern;
  346. }
  347. ZXRSSFinderPattern *pattern;
  348. BOOL keepFinding = YES;
  349. int forcedOffset = -1;
  350. do {
  351. if (![self findNextPair:row previousPairs:previousPairs forcedOffset:forcedOffset]) {
  352. return nil;
  353. }
  354. pattern = [self parseFoundFinderPattern:row rowNumber:rowNumber oddPattern:isOddPattern];
  355. if (pattern == nil) {
  356. forcedOffset = [self nextSecondBar:row initialPos:self.startEnd.array[0]];
  357. } else {
  358. keepFinding = NO;
  359. }
  360. } while (keepFinding);
  361. // When stacked symbol is split over multiple rows, there's no way to guess if this pair can be last or not.
  362. // boolean mayBeLast = checkPairSequence(previousPairs, pattern);
  363. ZXRSSDataCharacter *leftChar = [self decodeDataCharacter:row pattern:pattern isOddPattern:isOddPattern leftChar:YES];
  364. if (!leftChar) {
  365. return nil;
  366. }
  367. if (previousPairs.count > 0 && [[previousPairs lastObject] mustBeLast]) {
  368. return nil;
  369. }
  370. ZXRSSDataCharacter *rightChar = [self decodeDataCharacter:row pattern:pattern isOddPattern:isOddPattern leftChar:NO];
  371. BOOL mayBeLast = YES;
  372. return [[ZXRSSExpandedPair alloc] initWithLeftChar:leftChar rightChar:rightChar finderPattern:pattern mayBeLast:mayBeLast];
  373. }
  374. - (BOOL)findNextPair:(ZXBitArray *)row previousPairs:(NSMutableArray *)previousPairs forcedOffset:(int)forcedOffset {
  375. ZXIntArray *counters = self.decodeFinderCounters;
  376. [counters clear];
  377. int width = row.size;
  378. int rowOffset;
  379. if (forcedOffset >= 0) {
  380. rowOffset = forcedOffset;
  381. } else if ([previousPairs count] == 0) {
  382. rowOffset = 0;
  383. } else {
  384. ZXRSSExpandedPair *lastPair = [previousPairs lastObject];
  385. rowOffset = [[lastPair finderPattern] startEnd].array[1];
  386. }
  387. BOOL searchingEvenPair = [previousPairs count] % 2 != 0;
  388. if (self.startFromEven) {
  389. searchingEvenPair = !searchingEvenPair;
  390. }
  391. BOOL isWhite = NO;
  392. while (rowOffset < width) {
  393. isWhite = ![row get:rowOffset];
  394. if (!isWhite) {
  395. break;
  396. }
  397. rowOffset++;
  398. }
  399. int counterPosition = 0;
  400. int patternStart = rowOffset;
  401. int32_t *array = counters.array;
  402. for (int x = rowOffset; x < width; x++) {
  403. if ([row get:x] ^ isWhite) {
  404. array[counterPosition]++;
  405. } else {
  406. if (counterPosition == 3) {
  407. if (searchingEvenPair) {
  408. [self reverseCounters:counters];
  409. }
  410. if ([ZXAbstractRSSReader isFinderPattern:counters]) {
  411. self.startEnd.array[0] = patternStart;
  412. self.startEnd.array[1] = x;
  413. return YES;
  414. }
  415. if (searchingEvenPair) {
  416. [self reverseCounters:counters];
  417. }
  418. patternStart += array[0] + array[1];
  419. array[0] = array[2];
  420. array[1] = array[3];
  421. array[2] = 0;
  422. array[3] = 0;
  423. counterPosition--;
  424. } else {
  425. counterPosition++;
  426. }
  427. array[counterPosition] = 1;
  428. isWhite = !isWhite;
  429. }
  430. }
  431. return NO;
  432. }
  433. - (void)reverseCounters:(ZXIntArray *)counters {
  434. int length = counters.length;
  435. int32_t *array = counters.array;
  436. for (int i = 0; i < length / 2; ++i) {
  437. int tmp = array[i];
  438. array[i] = array[length - i - 1];
  439. array[length - i - 1] = tmp;
  440. }
  441. }
  442. - (ZXRSSFinderPattern *)parseFoundFinderPattern:(ZXBitArray *)row rowNumber:(int)rowNumber oddPattern:(BOOL)oddPattern {
  443. // Actually we found elements 2-5.
  444. int firstCounter;
  445. int start;
  446. int end;
  447. if (oddPattern) {
  448. // If pattern number is odd, we need to locate element 1 *before *the current block.
  449. int firstElementStart = self.startEnd.array[0] - 1;
  450. // Locate element 1
  451. while (firstElementStart >= 0 && ![row get:firstElementStart]) {
  452. firstElementStart--;
  453. }
  454. firstElementStart++;
  455. firstCounter = self.startEnd.array[0] - firstElementStart;
  456. start = firstElementStart;
  457. end = self.startEnd.array[1];
  458. } else {
  459. // If pattern number is even, the pattern is reversed, so we need to locate element 1 *after *the current block.
  460. start = self.startEnd.array[0];
  461. end = [row nextUnset:self.startEnd.array[1] + 1];
  462. firstCounter = end - self.startEnd.array[1];
  463. }
  464. // Make 'counters' hold 1-4
  465. ZXIntArray *counters = [[ZXIntArray alloc] initWithLength:self.decodeFinderCounters.length];
  466. for (int i = 1; i < counters.length; i++) {
  467. counters.array[i] = self.decodeFinderCounters.array[i - 1];
  468. }
  469. counters.array[0] = firstCounter;
  470. memcpy(self.decodeFinderCounters.array, counters.array, counters.length * sizeof(int32_t));
  471. int value = [ZXAbstractRSSReader parseFinderValue:counters finderPatternType:ZX_RSS_PATTERNS_RSS_EXPANDED_PATTERNS];
  472. if (value == -1) {
  473. return nil;
  474. }
  475. return [[ZXRSSFinderPattern alloc] initWithValue:value startEnd:[[ZXIntArray alloc] initWithInts:start, end, -1] start:start end:end rowNumber:rowNumber];
  476. }
  477. - (ZXRSSDataCharacter *)decodeDataCharacter:(ZXBitArray *)row pattern:(ZXRSSFinderPattern *)pattern isOddPattern:(BOOL)isOddPattern leftChar:(BOOL)leftChar {
  478. ZXIntArray *counters = self.dataCharacterCounters;
  479. [counters clear];
  480. if (leftChar) {
  481. if (![ZXOneDReader recordPatternInReverse:row start:[pattern startEnd].array[0] counters:counters]) {
  482. return nil;
  483. }
  484. } else {
  485. if (![ZXOneDReader recordPattern:row start:[pattern startEnd].array[1] counters:counters]) {
  486. return nil;
  487. }
  488. // reverse it
  489. int32_t *array = counters.array;
  490. for (int i = 0, j = counters.length - 1; i < j; i++, j--) {
  491. int temp = array[i];
  492. array[i] = array[j];
  493. array[j] = temp;
  494. }
  495. }//counters[] has the pixels of the module
  496. int numModules = 17; //left and right data characters have all the same length
  497. float elementWidth = (float)[ZXAbstractRSSReader count:counters] / (float)numModules;
  498. // Sanity check: element width for pattern and the character should match
  499. float expectedElementWidth = (pattern.startEnd.array[1] - pattern.startEnd.array[0]) / 15.0f;
  500. if (fabsf(elementWidth - expectedElementWidth) / expectedElementWidth > 0.3f) {
  501. return nil;
  502. }
  503. int32_t *array = counters.array;
  504. for (int i = 0; i < counters.length; i++) {
  505. float value = 1.0f * array[i] / elementWidth;
  506. int count = (int)(value + 0.5f);
  507. if (count < 1) {
  508. if (value < 0.3f) {
  509. return nil;
  510. }
  511. count = 1;
  512. } else if (count > 8) {
  513. if (value > 8.7f) {
  514. return nil;
  515. }
  516. count = 8;
  517. }
  518. int offset = i / 2;
  519. if ((i & 0x01) == 0) {
  520. self.oddCounts.array[offset] = count;
  521. self.oddRoundingErrors[offset] = value - count;
  522. } else {
  523. self.evenCounts.array[offset] = count;
  524. self.evenRoundingErrors[offset] = value - count;
  525. }
  526. }
  527. if (![self adjustOddEvenCounts:numModules]) {
  528. return nil;
  529. }
  530. int weightRowNumber = 4 * pattern.value + (isOddPattern ? 0 : 2) + (leftChar ? 0 : 1) - 1;
  531. int oddSum = 0;
  532. int oddChecksumPortion = 0;
  533. for (int i = self.oddCounts.length - 1; i >= 0; i--) {
  534. if ([self isNotA1left:pattern isOddPattern:isOddPattern leftChar:leftChar]) {
  535. int weight = ZX_WEIGHTS[weightRowNumber][2 * i];
  536. oddChecksumPortion += self.oddCounts.array[i] * weight;
  537. }
  538. oddSum += self.oddCounts.array[i];
  539. }
  540. int evenChecksumPortion = 0;
  541. //int evenSum = 0;
  542. for (int i = self.evenCounts.length - 1; i >= 0; i--) {
  543. if ([self isNotA1left:pattern isOddPattern:isOddPattern leftChar:leftChar]) {
  544. int weight = ZX_WEIGHTS[weightRowNumber][2 * i + 1];
  545. evenChecksumPortion += self.evenCounts.array[i] * weight;
  546. }
  547. //evenSum += self.evenCounts[i];
  548. }
  549. int checksumPortion = oddChecksumPortion + evenChecksumPortion;
  550. if ((oddSum & 0x01) != 0 || oddSum > 13 || oddSum < 4) {
  551. return nil;
  552. }
  553. int group = (13 - oddSum) / 2;
  554. int oddWidest = ZX_SYMBOL_WIDEST[group];
  555. int evenWidest = 9 - oddWidest;
  556. int vOdd = [ZXRSSUtils rssValue:self.oddCounts maxWidth:oddWidest noNarrow:YES];
  557. int vEven = [ZXRSSUtils rssValue:self.evenCounts maxWidth:evenWidest noNarrow:NO];
  558. int tEven = ZX_EVEN_TOTAL_SUBSET[group];
  559. int gSum = ZX_GSUM[group];
  560. int value = vOdd * tEven + vEven + gSum;
  561. return [[ZXRSSDataCharacter alloc] initWithValue:value checksumPortion:checksumPortion];
  562. }
  563. - (BOOL)isNotA1left:(ZXRSSFinderPattern *)pattern isOddPattern:(BOOL)isOddPattern leftChar:(BOOL)leftChar {
  564. return !([pattern value] == 0 && isOddPattern && leftChar);
  565. }
  566. - (BOOL)adjustOddEvenCounts:(int)numModules {
  567. int oddSum = [ZXAbstractRSSReader count:self.oddCounts];
  568. int evenSum = [ZXAbstractRSSReader count:self.evenCounts];
  569. int mismatch = oddSum + evenSum - numModules;
  570. BOOL oddParityBad = (oddSum & 0x01) == 1;
  571. BOOL evenParityBad = (evenSum & 0x01) == 0;
  572. BOOL incrementOdd = NO;
  573. BOOL decrementOdd = NO;
  574. if (oddSum > 13) {
  575. decrementOdd = YES;
  576. } else if (oddSum < 4) {
  577. incrementOdd = YES;
  578. }
  579. BOOL incrementEven = NO;
  580. BOOL decrementEven = NO;
  581. if (evenSum > 13) {
  582. decrementEven = YES;
  583. } else if (evenSum < 4) {
  584. incrementEven = YES;
  585. }
  586. if (mismatch == 1) {
  587. if (oddParityBad) {
  588. if (evenParityBad) {
  589. return NO;
  590. }
  591. decrementOdd = YES;
  592. } else {
  593. if (!evenParityBad) {
  594. return NO;
  595. }
  596. decrementEven = YES;
  597. }
  598. } else if (mismatch == -1) {
  599. if (oddParityBad) {
  600. if (evenParityBad) {
  601. return NO;
  602. }
  603. incrementOdd = YES;
  604. } else {
  605. if (!evenParityBad) {
  606. return NO;
  607. }
  608. incrementEven = YES;
  609. }
  610. } else if (mismatch == 0) {
  611. if (oddParityBad) {
  612. if (!evenParityBad) {
  613. return NO;
  614. }
  615. if (oddSum < evenSum) {
  616. incrementOdd = YES;
  617. decrementEven = YES;
  618. } else {
  619. decrementOdd = YES;
  620. incrementEven = YES;
  621. }
  622. } else {
  623. if (evenParityBad) {
  624. return NO;
  625. }
  626. }
  627. } else {
  628. return NO;
  629. }
  630. if (incrementOdd) {
  631. if (decrementOdd) {
  632. return NO;
  633. }
  634. [ZXAbstractRSSReader increment:self.oddCounts errors:self.oddRoundingErrors];
  635. }
  636. if (decrementOdd) {
  637. [ZXAbstractRSSReader decrement:self.oddCounts errors:self.oddRoundingErrors];
  638. }
  639. if (incrementEven) {
  640. if (decrementEven) {
  641. return NO;
  642. }
  643. [ZXAbstractRSSReader increment:self.evenCounts errors:self.oddRoundingErrors];
  644. }
  645. if (decrementEven) {
  646. [ZXAbstractRSSReader decrement:self.evenCounts errors:self.evenRoundingErrors];
  647. }
  648. return YES;
  649. }
  650. @end