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.

ZXPDF417CodewordDecoder.m 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2013 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 "ZXPDF417CodewordDecoder.h"
  17. #import "ZXPDF417Common.h"
  18. static float ZX_PDF417_RATIOS_TABLE[ZX_PDF417_SYMBOL_TABLE_LEN][ZX_PDF417_BARS_IN_MODULE];
  19. @implementation ZXPDF417CodewordDecoder
  20. + (void)initialize {
  21. if ([self class] != [ZXPDF417CodewordDecoder class]) return;
  22. // Pre-computes the symbol ratio table.
  23. for (int i = 0; i < ZX_PDF417_SYMBOL_TABLE_LEN; i++) {
  24. int currentSymbol = ZX_PDF417_SYMBOL_TABLE[i];
  25. int currentBit = currentSymbol & 0x1;
  26. for (int j = 0; j < ZX_PDF417_BARS_IN_MODULE; j++) {
  27. float size = 0.0f;
  28. while ((currentSymbol & 0x1) == currentBit) {
  29. size += 1.0f;
  30. currentSymbol >>= 1;
  31. }
  32. currentBit = currentSymbol & 0x1;
  33. ZX_PDF417_RATIOS_TABLE[i][ZX_PDF417_BARS_IN_MODULE - j - 1] = size / ZX_PDF417_MODULES_IN_CODEWORD;
  34. }
  35. }
  36. }
  37. + (int)decodedValue:(NSArray *)moduleBitCount {
  38. int decodedValue = [self decodedCodewordValue:[self sampleBitCounts:moduleBitCount]];
  39. if (decodedValue != -1) {
  40. return decodedValue;
  41. }
  42. return [self closestDecodedValue:moduleBitCount];
  43. }
  44. + (NSArray *)sampleBitCounts:(NSArray *)moduleBitCount {
  45. float bitCountSum = [ZXPDF417Common bitCountSum:moduleBitCount];
  46. NSMutableArray *result = [NSMutableArray arrayWithCapacity:ZX_PDF417_BARS_IN_MODULE];
  47. for (int i = 0; i < ZX_PDF417_BARS_IN_MODULE; i++) {
  48. [result addObject:@0];
  49. }
  50. int bitCountIndex = 0;
  51. int sumPreviousBits = 0;
  52. for (int i = 0; i < ZX_PDF417_MODULES_IN_CODEWORD; i++) {
  53. float sampleIndex =
  54. bitCountSum / (2 * ZX_PDF417_MODULES_IN_CODEWORD) +
  55. (i * bitCountSum) / ZX_PDF417_MODULES_IN_CODEWORD;
  56. if (sumPreviousBits + [moduleBitCount[bitCountIndex] intValue] <= sampleIndex) {
  57. sumPreviousBits += [moduleBitCount[bitCountIndex] intValue];
  58. bitCountIndex++;
  59. }
  60. result[bitCountIndex] = @([result[bitCountIndex] intValue] + 1);
  61. }
  62. return result;
  63. }
  64. + (int)decodedCodewordValue:(NSArray *)moduleBitCount {
  65. int decodedValue = [self bitValue:moduleBitCount];
  66. return [ZXPDF417Common codeword:decodedValue] == -1 ? -1 : decodedValue;
  67. }
  68. + (int)bitValue:(NSArray *)moduleBitCount {
  69. long result = 0;
  70. for (int i = 0; i < [moduleBitCount count]; i++) {
  71. for (int bit = 0; bit < [moduleBitCount[i] intValue]; bit++) {
  72. result = (result << 1) | (i % 2 == 0 ? 1 : 0);
  73. }
  74. }
  75. return (int) result;
  76. }
  77. + (int)closestDecodedValue:(NSArray *)moduleBitCount {
  78. int bitCountSum = [ZXPDF417Common bitCountSum:moduleBitCount];
  79. float bitCountRatios[ZX_PDF417_BARS_IN_MODULE];
  80. for (int i = 0; i < ZX_PDF417_BARS_IN_MODULE; i++) {
  81. bitCountRatios[i] = [moduleBitCount[i] intValue] / (float) bitCountSum;
  82. }
  83. float bestMatchError = MAXFLOAT;
  84. int bestMatch = -1;
  85. for (int j = 0; j < ZX_PDF417_SYMBOL_TABLE_LEN; j++) {
  86. float error = 0.0f;
  87. float *ratioTableRow = ZX_PDF417_RATIOS_TABLE[j];
  88. for (int k = 0; k < ZX_PDF417_BARS_IN_MODULE; k++) {
  89. float diff = ratioTableRow[k] - bitCountRatios[k];
  90. error += diff * diff;
  91. if (error >= bestMatchError) {
  92. break;
  93. }
  94. }
  95. if (error < bestMatchError) {
  96. bestMatchError = error;
  97. bestMatch = ZX_PDF417_SYMBOL_TABLE[j];
  98. }
  99. }
  100. return bestMatch;
  101. }
  102. @end