Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ZXDataMatrixBitMatrixParser.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 "ZXBitMatrix.h"
  17. #import "ZXByteArray.h"
  18. #import "ZXDataMatrixBitMatrixParser.h"
  19. #import "ZXDataMatrixVersion.h"
  20. #import "ZXErrors.h"
  21. @interface ZXDataMatrixBitMatrixParser ()
  22. @property (nonatomic, strong, readonly) ZXBitMatrix *mappingBitMatrix;
  23. @property (nonatomic, strong, readonly) ZXBitMatrix *readMappingMatrix;
  24. @end
  25. @implementation ZXDataMatrixBitMatrixParser
  26. - (id)initWithBitMatrix:(ZXBitMatrix *)bitMatrix error:(NSError **)error {
  27. if (self = [super init]) {
  28. int dimension = bitMatrix.height;
  29. if (dimension < 8 || dimension > 144 || (dimension & 0x01) != 0) {
  30. if (error) *error = ZXFormatErrorInstance();
  31. return nil;
  32. }
  33. _version = [self readVersion:bitMatrix];
  34. if (!_version) {
  35. if (error) *error = ZXFormatErrorInstance();
  36. return nil;
  37. }
  38. _mappingBitMatrix = [self extractDataRegion:bitMatrix];
  39. _readMappingMatrix = [[ZXBitMatrix alloc] initWithWidth:_mappingBitMatrix.width
  40. height:_mappingBitMatrix.height];
  41. }
  42. return self;
  43. }
  44. /**
  45. * Creates the version object based on the dimension of the original bit matrix from
  46. * the datamatrix code.
  47. *
  48. * See ISO 16022:2006 Table 7 - ECC 200 symbol attributes<
  49. *
  50. * @param bitMatrix Original ZXBitMatrix including alignment patterns
  51. * @return ZXDatamatrixVersion encapsulating the Data Matrix Code's "version"
  52. * or nil if the dimensions of the mapping matrix are not valid
  53. * Data Matrix dimensions.
  54. */
  55. - (ZXDataMatrixVersion *)readVersion:(ZXBitMatrix *)bitMatrix {
  56. int numRows = bitMatrix.height;
  57. int numColumns = bitMatrix.width;
  58. return [ZXDataMatrixVersion versionForDimensions:numRows numColumns:numColumns];
  59. }
  60. - (ZXByteArray *)readCodewords {
  61. ZXByteArray *result = [[ZXByteArray alloc] initWithLength:self.version.totalCodewords];
  62. int resultOffset = 0;
  63. int row = 4;
  64. int column = 0;
  65. int numRows = self.mappingBitMatrix.height;
  66. int numColumns = self.mappingBitMatrix.width;
  67. BOOL corner1Read = NO;
  68. BOOL corner2Read = NO;
  69. BOOL corner3Read = NO;
  70. BOOL corner4Read = NO;
  71. do {
  72. if ((row == numRows) && (column == 0) && !corner1Read) {
  73. result.array[resultOffset++] = (int8_t) [self readCorner1:numRows numColumns:numColumns];
  74. row -= 2;
  75. column += 2;
  76. corner1Read = YES;
  77. } else if ((row == numRows - 2) && (column == 0) && ((numColumns & 0x03) != 0) && !corner2Read) {
  78. result.array[resultOffset++] = (int8_t) [self readCorner2:numRows numColumns:numColumns];
  79. row -= 2;
  80. column += 2;
  81. corner2Read = YES;
  82. } else if ((row == numRows + 4) && (column == 2) && ((numColumns & 0x07) == 0) && !corner3Read) {
  83. result.array[resultOffset++] = (int8_t) [self readCorner3:numRows numColumns:numColumns];
  84. row -= 2;
  85. column += 2;
  86. corner3Read = YES;
  87. } else if ((row == numRows - 2) && (column == 0) && ((numColumns & 0x07) == 4) && !corner4Read) {
  88. result.array[resultOffset++] = (int8_t) [self readCorner4:numRows numColumns:numColumns];
  89. row -= 2;
  90. column += 2;
  91. corner4Read = YES;
  92. } else {
  93. do {
  94. if ((row < numRows) && (column >= 0) && ![self.readMappingMatrix getX:column y:row]) {
  95. result.array[resultOffset++] = (int8_t) [self readUtah:row column:column numRows:numRows numColumns:numColumns];
  96. }
  97. row -= 2;
  98. column += 2;
  99. } while ((row >= 0) && (column < numColumns));
  100. row += 1;
  101. column += 3;
  102. do {
  103. if ((row >= 0) && (column < numColumns) && ![self.readMappingMatrix getX:column y:row]) {
  104. result.array[resultOffset++] = (int8_t) [self readUtah:row column:column numRows:numRows numColumns:numColumns];
  105. }
  106. row += 2;
  107. column -= 2;
  108. } while ((row < numRows) && (column >= 0));
  109. row += 3;
  110. column += 1;
  111. }
  112. } while ((row < numRows) || (column < numColumns));
  113. if (resultOffset != self.version.totalCodewords) {
  114. return nil;
  115. }
  116. return result;
  117. }
  118. /**
  119. * Reads a bit of the mapping matrix accounting for boundary wrapping.
  120. *
  121. * @param row Row to read in the mapping matrix
  122. * @param column Column to read in the mapping matrix
  123. * @param numRows Number of rows in the mapping matrix
  124. * @param numColumns Number of columns in the mapping matrix
  125. * @return value of the given bit in the mapping matrix
  126. */
  127. - (BOOL)readModule:(int)row column:(int)column numRows:(int)numRows numColumns:(int)numColumns {
  128. if (row < 0) {
  129. row += numRows;
  130. column += 4 - ((numRows + 4) & 0x07);
  131. }
  132. if (column < 0) {
  133. column += numColumns;
  134. row += 4 - ((numColumns + 4) & 0x07);
  135. }
  136. [self.readMappingMatrix setX:column y:row];
  137. return [self.mappingBitMatrix getX:column y:row];
  138. }
  139. /**
  140. * Reads the 8 bits of the standard Utah-shaped pattern.
  141. *
  142. * See ISO 16022:2006, 5.8.1 Figure 6
  143. *
  144. * @param row Current row in the mapping matrix, anchored at the 8th bit (LSB) of the pattern
  145. * @param column Current column in the mapping matrix, anchored at the 8th bit (LSB) of the pattern
  146. * @param numRows Number of rows in the mapping matrix
  147. * @param numColumns Number of columns in the mapping matrix
  148. * @return byte from the utah shape
  149. */
  150. - (int)readUtah:(int)row column:(int)column numRows:(int)numRows numColumns:(int)numColumns {
  151. int currentByte = 0;
  152. if ([self readModule:row - 2 column:column - 2 numRows:numRows numColumns:numColumns]) {
  153. currentByte |= 1;
  154. }
  155. currentByte <<= 1;
  156. if ([self readModule:row - 2 column:column - 1 numRows:numRows numColumns:numColumns]) {
  157. currentByte |= 1;
  158. }
  159. currentByte <<= 1;
  160. if ([self readModule:row - 1 column:column - 2 numRows:numRows numColumns:numColumns]) {
  161. currentByte |= 1;
  162. }
  163. currentByte <<= 1;
  164. if ([self readModule:row - 1 column:column - 1 numRows:numRows numColumns:numColumns]) {
  165. currentByte |= 1;
  166. }
  167. currentByte <<= 1;
  168. if ([self readModule:row - 1 column:column numRows:numRows numColumns:numColumns]) {
  169. currentByte |= 1;
  170. }
  171. currentByte <<= 1;
  172. if ([self readModule:row column:column - 2 numRows:numRows numColumns:numColumns]) {
  173. currentByte |= 1;
  174. }
  175. currentByte <<= 1;
  176. if ([self readModule:row column:column - 1 numRows:numRows numColumns:numColumns]) {
  177. currentByte |= 1;
  178. }
  179. currentByte <<= 1;
  180. if ([self readModule:row column:column numRows:numRows numColumns:numColumns]) {
  181. currentByte |= 1;
  182. }
  183. return currentByte;
  184. }
  185. /**
  186. * Reads the 8 bits of the special corner condition 1.
  187. *
  188. * See ISO 16022:2006, Figure F.3
  189. *
  190. * @param numRows Number of rows in the mapping matrix
  191. * @param numColumns Number of columns in the mapping matrix
  192. * @return byte from the Corner condition 1
  193. */
  194. - (int)readCorner1:(int)numRows numColumns:(int)numColumns {
  195. int currentByte = 0;
  196. if ([self readModule:numRows - 1 column:0 numRows:numRows numColumns:numColumns]) {
  197. currentByte |= 1;
  198. }
  199. currentByte <<= 1;
  200. if ([self readModule:numRows - 1 column:1 numRows:numRows numColumns:numColumns]) {
  201. currentByte |= 1;
  202. }
  203. currentByte <<= 1;
  204. if ([self readModule:numRows - 1 column:2 numRows:numRows numColumns:numColumns]) {
  205. currentByte |= 1;
  206. }
  207. currentByte <<= 1;
  208. if ([self readModule:0 column:numColumns - 2 numRows:numRows numColumns:numColumns]) {
  209. currentByte |= 1;
  210. }
  211. currentByte <<= 1;
  212. if ([self readModule:0 column:numColumns - 1 numRows:numRows numColumns:numColumns]) {
  213. currentByte |= 1;
  214. }
  215. currentByte <<= 1;
  216. if ([self readModule:1 column:numColumns - 1 numRows:numRows numColumns:numColumns]) {
  217. currentByte |= 1;
  218. }
  219. currentByte <<= 1;
  220. if ([self readModule:2 column:numColumns - 1 numRows:numRows numColumns:numColumns]) {
  221. currentByte |= 1;
  222. }
  223. currentByte <<= 1;
  224. if ([self readModule:3 column:numColumns - 1 numRows:numRows numColumns:numColumns]) {
  225. currentByte |= 1;
  226. }
  227. return currentByte;
  228. }
  229. /**
  230. * Reads the 8 bits of the special corner condition 2.
  231. *
  232. * See ISO 16022:2006, Figure F.4
  233. *
  234. * @param numRows Number of rows in the mapping matrix
  235. * @param numColumns Number of columns in the mapping matrix
  236. * @return byte from the Corner condition 2
  237. */
  238. - (int)readCorner2:(int)numRows numColumns:(int)numColumns {
  239. int currentByte = 0;
  240. if ([self readModule:numRows - 3 column:0 numRows:numRows numColumns:numColumns]) {
  241. currentByte |= 1;
  242. }
  243. currentByte <<= 1;
  244. if ([self readModule:numRows - 2 column:0 numRows:numRows numColumns:numColumns]) {
  245. currentByte |= 1;
  246. }
  247. currentByte <<= 1;
  248. if ([self readModule:numRows - 1 column:0 numRows:numRows numColumns:numColumns]) {
  249. currentByte |= 1;
  250. }
  251. currentByte <<= 1;
  252. if ([self readModule:0 column:numColumns - 4 numRows:numRows numColumns:numColumns]) {
  253. currentByte |= 1;
  254. }
  255. currentByte <<= 1;
  256. if ([self readModule:0 column:numColumns - 3 numRows:numRows numColumns:numColumns]) {
  257. currentByte |= 1;
  258. }
  259. currentByte <<= 1;
  260. if ([self readModule:0 column:numColumns - 2 numRows:numRows numColumns:numColumns]) {
  261. currentByte |= 1;
  262. }
  263. currentByte <<= 1;
  264. if ([self readModule:0 column:numColumns - 1 numRows:numRows numColumns:numColumns]) {
  265. currentByte |= 1;
  266. }
  267. currentByte <<= 1;
  268. if ([self readModule:1 column:numColumns - 1 numRows:numRows numColumns:numColumns]) {
  269. currentByte |= 1;
  270. }
  271. return currentByte;
  272. }
  273. /**
  274. * Reads the 8 bits of the special corner condition 3.
  275. *
  276. * See ISO 16022:2006, Figure F.5
  277. *
  278. * @param numRows Number of rows in the mapping matrix
  279. * @param numColumns Number of columns in the mapping matrix
  280. * @return byte from the Corner condition 3
  281. */
  282. - (int)readCorner3:(int)numRows numColumns:(int)numColumns {
  283. int currentByte = 0;
  284. if ([self readModule:numRows - 1 column:0 numRows:numRows numColumns:numColumns]) {
  285. currentByte |= 1;
  286. }
  287. currentByte <<= 1;
  288. if ([self readModule:numRows - 1 column:numColumns - 1 numRows:numRows numColumns:numColumns]) {
  289. currentByte |= 1;
  290. }
  291. currentByte <<= 1;
  292. if ([self readModule:0 column:numColumns - 3 numRows:numRows numColumns:numColumns]) {
  293. currentByte |= 1;
  294. }
  295. currentByte <<= 1;
  296. if ([self readModule:0 column:numColumns - 2 numRows:numRows numColumns:numColumns]) {
  297. currentByte |= 1;
  298. }
  299. currentByte <<= 1;
  300. if ([self readModule:0 column:numColumns - 1 numRows:numRows numColumns:numColumns]) {
  301. currentByte |= 1;
  302. }
  303. currentByte <<= 1;
  304. if ([self readModule:1 column:numColumns - 3 numRows:numRows numColumns:numColumns]) {
  305. currentByte |= 1;
  306. }
  307. currentByte <<= 1;
  308. if ([self readModule:1 column:numColumns - 2 numRows:numRows numColumns:numColumns]) {
  309. currentByte |= 1;
  310. }
  311. currentByte <<= 1;
  312. if ([self readModule:1 column:numColumns - 1 numRows:numRows numColumns:numColumns]) {
  313. currentByte |= 1;
  314. }
  315. return currentByte;
  316. }
  317. /**
  318. * Reads the 8 bits of the special corner condition 4.
  319. *
  320. * See ISO 16022:2006, Figure F.6
  321. *
  322. * @param numRows Number of rows in the mapping matrix
  323. * @param numColumns Number of columns in the mapping matrix
  324. * @return byte from the Corner condition 4
  325. */
  326. - (int)readCorner4:(int)numRows numColumns:(int)numColumns {
  327. int currentByte = 0;
  328. if ([self readModule:numRows - 3 column:0 numRows:numRows numColumns:numColumns]) {
  329. currentByte |= 1;
  330. }
  331. currentByte <<= 1;
  332. if ([self readModule:numRows - 2 column:0 numRows:numRows numColumns:numColumns]) {
  333. currentByte |= 1;
  334. }
  335. currentByte <<= 1;
  336. if ([self readModule:numRows - 1 column:0 numRows:numRows numColumns:numColumns]) {
  337. currentByte |= 1;
  338. }
  339. currentByte <<= 1;
  340. if ([self readModule:0 column:numColumns - 2 numRows:numRows numColumns:numColumns]) {
  341. currentByte |= 1;
  342. }
  343. currentByte <<= 1;
  344. if ([self readModule:0 column:numColumns - 1 numRows:numRows numColumns:numColumns]) {
  345. currentByte |= 1;
  346. }
  347. currentByte <<= 1;
  348. if ([self readModule:1 column:numColumns - 1 numRows:numRows numColumns:numColumns]) {
  349. currentByte |= 1;
  350. }
  351. currentByte <<= 1;
  352. if ([self readModule:2 column:numColumns - 1 numRows:numRows numColumns:numColumns]) {
  353. currentByte |= 1;
  354. }
  355. currentByte <<= 1;
  356. if ([self readModule:3 column:numColumns - 1 numRows:numRows numColumns:numColumns]) {
  357. currentByte |= 1;
  358. }
  359. return currentByte;
  360. }
  361. /**
  362. * Extracts the data region from a ZXBitMatrix that contains
  363. * alignment patterns.
  364. *
  365. * @param bitMatrix Original ZXBitMatrix with alignment patterns
  366. * @return BitMatrix that has the alignment patterns removed
  367. */
  368. - (ZXBitMatrix *)extractDataRegion:(ZXBitMatrix *)bitMatrix {
  369. int symbolSizeRows = self.version.symbolSizeRows;
  370. int symbolSizeColumns = self.version.symbolSizeColumns;
  371. if (bitMatrix.height != symbolSizeRows) {
  372. [NSException raise:NSInvalidArgumentException format:@"Dimension of bitMatrix must match the version size"];
  373. }
  374. int dataRegionSizeRows = self.version.dataRegionSizeRows;
  375. int dataRegionSizeColumns = self.version.dataRegionSizeColumns;
  376. int numDataRegionsRow = symbolSizeRows / dataRegionSizeRows;
  377. int numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns;
  378. int sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows;
  379. int sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns;
  380. ZXBitMatrix *bitMatrixWithoutAlignment = [[ZXBitMatrix alloc] initWithWidth:sizeDataRegionColumn height:sizeDataRegionRow];
  381. for (int dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow) {
  382. int dataRegionRowOffset = dataRegionRow * dataRegionSizeRows;
  383. for (int dataRegionColumn = 0; dataRegionColumn < numDataRegionsColumn; ++dataRegionColumn) {
  384. int dataRegionColumnOffset = dataRegionColumn * dataRegionSizeColumns;
  385. for (int i = 0; i < dataRegionSizeRows; ++i) {
  386. int readRowOffset = dataRegionRow * (dataRegionSizeRows + 2) + 1 + i;
  387. int writeRowOffset = dataRegionRowOffset + i;
  388. for (int j = 0; j < dataRegionSizeColumns; ++j) {
  389. int readColumnOffset = dataRegionColumn * (dataRegionSizeColumns + 2) + 1 + j;
  390. if ([bitMatrix getX:readColumnOffset y:readRowOffset]) {
  391. int writeColumnOffset = dataRegionColumnOffset + j;
  392. [bitMatrixWithoutAlignment setX:writeColumnOffset y:writeRowOffset];
  393. }
  394. }
  395. }
  396. }
  397. }
  398. return bitMatrixWithoutAlignment;
  399. }
  400. @end