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

MatrixUtil.cs 29 kB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Copyright 2008 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. using System;
  17. using ZXing.Common;
  18. namespace ZXing.QrCode.Internal
  19. {
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. /// <author>
  24. /// satorux@google.com (Satoru Takabayashi) - creator
  25. /// </author>
  26. public static class MatrixUtil
  27. {
  28. private static readonly int[][] POSITION_DETECTION_PATTERN = new int[][]
  29. {
  30. new int[] { 1, 1, 1, 1, 1, 1, 1 },
  31. new int[] { 1, 0, 0, 0, 0, 0, 1 },
  32. new int[] { 1, 0, 1, 1, 1, 0, 1 },
  33. new int[] { 1, 0, 1, 1, 1, 0, 1 },
  34. new int[] { 1, 0, 1, 1, 1, 0, 1 },
  35. new int[] { 1, 0, 0, 0, 0, 0, 1 },
  36. new int[] { 1, 1, 1, 1, 1, 1, 1 }
  37. };
  38. private static readonly int[][] POSITION_ADJUSTMENT_PATTERN = new int[][]
  39. {
  40. new int[] { 1, 1, 1, 1, 1 },
  41. new int[] { 1, 0, 0, 0, 1 },
  42. new int[] { 1, 0, 1, 0, 1 },
  43. new int[] { 1, 0, 0, 0, 1 },
  44. new int[] { 1, 1, 1, 1, 1 }
  45. };
  46. // From Appendix E. Table 1, JIS0510X:2004 (p 71). The table was double-checked by komatsu.
  47. private static readonly int[][] POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE = new int[][]
  48. {
  49. new int[] { -1, -1, -1, -1, -1, -1, -1 },
  50. new int[] { 6, 18, -1, -1, -1, -1, -1 },
  51. new int[] { 6, 22, -1, -1, -1, -1, -1 },
  52. new int[] { 6, 26, -1, -1, -1, -1, -1 },
  53. new int[] { 6, 30, -1, -1, -1, -1, -1 },
  54. new int[] { 6, 34, -1, -1, -1, -1, -1 },
  55. new int[] { 6, 22, 38, -1, -1, -1, -1 },
  56. new int[] { 6, 24, 42, -1, -1, -1, -1 },
  57. new int[] { 6, 26, 46, -1, -1, -1, -1 },
  58. new int[] { 6, 28, 50, -1, -1, -1, -1 },
  59. new int[] { 6, 30, 54, -1, -1, -1, -1 },
  60. new int[] { 6, 32, 58, -1, -1, -1, -1 },
  61. new int[] { 6, 34, 62, -1, -1, -1, -1 },
  62. new int[] { 6, 26, 46, 66, -1, -1, -1 },
  63. new int[] { 6, 26, 48, 70, -1, -1, -1 },
  64. new int[] { 6, 26, 50, 74, -1, -1, -1 },
  65. new int[] { 6, 30, 54, 78, -1, -1, -1 },
  66. new int[] { 6, 30, 56, 82, -1, -1, -1 },
  67. new int[] { 6, 30, 58, 86, -1, -1, -1 },
  68. new int[] { 6, 34, 62, 90, -1, -1, -1 },
  69. new int[] { 6, 28, 50, 72, 94, -1, -1 },
  70. new int[] { 6, 26, 50, 74, 98, -1, -1 },
  71. new int[] { 6, 30, 54, 78, 102, -1, -1 },
  72. new int[] { 6, 28, 54, 80, 106, -1, -1 },
  73. new int[] { 6, 32, 58, 84, 110, -1, -1 },
  74. new int[] { 6, 30, 58, 86, 114, -1, -1 },
  75. new int[] { 6, 34, 62, 90, 118, -1, -1 },
  76. new int[] { 6, 26, 50, 74, 98, 122, -1 },
  77. new int[] { 6, 30, 54, 78, 102, 126, -1 },
  78. new int[] { 6, 26, 52, 78, 104, 130, -1 },
  79. new int[] { 6, 30, 56, 82, 108, 134, -1 },
  80. new int[] { 6, 34, 60, 86, 112, 138, -1 },
  81. new int[] { 6, 30, 58, 86, 114, 142, -1 },
  82. new int[] { 6, 34, 62, 90, 118, 146, -1 },
  83. new int[] { 6, 30, 54, 78, 102, 126, 150 },
  84. new int[] { 6, 24, 50, 76, 102, 128, 154 },
  85. new int[] { 6, 28, 54, 80, 106, 132, 158 },
  86. new int[] { 6, 32, 58, 84, 110, 136, 162 },
  87. new int[] { 6, 26, 54, 82, 110, 138, 166 },
  88. new int[] { 6, 30, 58, 86, 114, 142, 170 }
  89. };
  90. // Type info cells at the left top corner.
  91. private static readonly int[][] TYPE_INFO_COORDINATES = new int[][]
  92. {
  93. new int[] { 8, 0 },
  94. new int[] { 8, 1 },
  95. new int[] { 8, 2 },
  96. new int[] { 8, 3 },
  97. new int[] { 8, 4 },
  98. new int[] { 8, 5 },
  99. new int[] { 8, 7 },
  100. new int[] { 8, 8 },
  101. new int[] { 7, 8 },
  102. new int[] { 5, 8 },
  103. new int[] { 4, 8 },
  104. new int[] { 3, 8 },
  105. new int[] { 2, 8 },
  106. new int[] { 1, 8 },
  107. new int[] { 0, 8 }
  108. };
  109. // From Appendix D in JISX0510:2004 (p. 67)
  110. private const int VERSION_INFO_POLY = 0x1f25; // 1 1111 0010 0101
  111. // From Appendix C in JISX0510:2004 (p.65).
  112. private const int TYPE_INFO_POLY = 0x537;
  113. private const int TYPE_INFO_MASK_PATTERN = 0x5412;
  114. /// <summary>
  115. /// Set all cells to 2. 2 means that the cell is empty (not set yet).
  116. ///
  117. /// JAVAPORT: We shouldn't need to do this at all. The code should be rewritten to begin encoding
  118. /// with the ByteMatrix initialized all to zero.
  119. /// </summary>
  120. /// <param name="matrix">The matrix.</param>
  121. public static void clearMatrix(ByteMatrix matrix)
  122. {
  123. matrix.clear(2);
  124. }
  125. /// <summary>
  126. /// Build 2D matrix of QR Code from "dataBits" with "ecLevel", "version" and "getMaskPattern". On
  127. /// success, store the result in "matrix" and return true.
  128. /// </summary>
  129. /// <param name="dataBits">The data bits.</param>
  130. /// <param name="ecLevel">The ec level.</param>
  131. /// <param name="version">The version.</param>
  132. /// <param name="maskPattern">The mask pattern.</param>
  133. /// <param name="matrix">The matrix.</param>
  134. public static void buildMatrix(BitArray dataBits, ErrorCorrectionLevel ecLevel, Version version, int maskPattern, ByteMatrix matrix)
  135. {
  136. clearMatrix(matrix);
  137. embedBasicPatterns(version, matrix);
  138. // Type information appear with any version.
  139. embedTypeInfo(ecLevel, maskPattern, matrix);
  140. // Version info appear if version >= 7.
  141. maybeEmbedVersionInfo(version, matrix);
  142. // Data should be embedded at end.
  143. embedDataBits(dataBits, maskPattern, matrix);
  144. }
  145. /// <summary>
  146. /// Embed basic patterns. On success, modify the matrix and return true.
  147. /// The basic patterns are:
  148. /// - Position detection patterns
  149. /// - Timing patterns
  150. /// - Dark dot at the left bottom corner
  151. /// - Position adjustment patterns, if need be
  152. /// </summary>
  153. /// <param name="version">The version.</param>
  154. /// <param name="matrix">The matrix.</param>
  155. public static void embedBasicPatterns(Version version, ByteMatrix matrix)
  156. {
  157. // Let's get started with embedding big squares at corners.
  158. embedPositionDetectionPatternsAndSeparators(matrix);
  159. // Then, embed the dark dot at the left bottom corner.
  160. embedDarkDotAtLeftBottomCorner(matrix);
  161. // Position adjustment patterns appear if version >= 2.
  162. maybeEmbedPositionAdjustmentPatterns(version, matrix);
  163. // Timing patterns should be embedded after position adj. patterns.
  164. embedTimingPatterns(matrix);
  165. }
  166. /// <summary>
  167. /// Embed type information. On success, modify the matrix.
  168. /// </summary>
  169. /// <param name="ecLevel">The ec level.</param>
  170. /// <param name="maskPattern">The mask pattern.</param>
  171. /// <param name="matrix">The matrix.</param>
  172. public static void embedTypeInfo(ErrorCorrectionLevel ecLevel, int maskPattern, ByteMatrix matrix)
  173. {
  174. BitArray typeInfoBits = new BitArray();
  175. makeTypeInfoBits(ecLevel, maskPattern, typeInfoBits);
  176. for (int i = 0; i < typeInfoBits.Size; ++i)
  177. {
  178. // Place bits in LSB to MSB order. LSB (least significant bit) is the last value in
  179. // "typeInfoBits".
  180. int bit = typeInfoBits[typeInfoBits.Size - 1 - i] ? 1 : 0;
  181. // Type info bits at the left top corner. See 8.9 of JISX0510:2004 (p.46).
  182. int x1 = TYPE_INFO_COORDINATES[i][0];
  183. int y1 = TYPE_INFO_COORDINATES[i][1];
  184. matrix[x1, y1] = bit;
  185. if (i < 8)
  186. {
  187. // Right top corner.
  188. int x2 = matrix.Width - i - 1;
  189. int y2 = 8;
  190. matrix[x2, y2] = bit;
  191. }
  192. else
  193. {
  194. // Left bottom corner.
  195. int x2 = 8;
  196. int y2 = matrix.Height - 7 + (i - 8);
  197. matrix[x2, y2] = bit;
  198. }
  199. }
  200. }
  201. /// <summary>
  202. /// Embed version information if need be. On success, modify the matrix and return true.
  203. /// See 8.10 of JISX0510:2004 (p.47) for how to embed version information.
  204. /// </summary>
  205. /// <param name="version">The version.</param>
  206. /// <param name="matrix">The matrix.</param>
  207. public static void maybeEmbedVersionInfo(Version version, ByteMatrix matrix)
  208. {
  209. if (version.VersionNumber < 7)
  210. {
  211. // Version info is necessary if version >= 7.
  212. return; // Don't need version info.
  213. }
  214. BitArray versionInfoBits = new BitArray();
  215. makeVersionInfoBits(version, versionInfoBits);
  216. int bitIndex = 6 * 3 - 1; // It will decrease from 17 to 0.
  217. for (int i = 0; i < 6; ++i)
  218. {
  219. for (int j = 0; j < 3; ++j)
  220. {
  221. // Place bits in LSB (least significant bit) to MSB order.
  222. var bit = versionInfoBits[bitIndex] ? 1 : 0;
  223. bitIndex--;
  224. // Left bottom corner.
  225. matrix[i, matrix.Height - 11 + j] = bit;
  226. // Right bottom corner.
  227. matrix[matrix.Height - 11 + j, i] = bit;
  228. }
  229. }
  230. }
  231. /// <summary>
  232. /// Embed "dataBits" using "getMaskPattern". On success, modify the matrix and return true.
  233. /// For debugging purposes, it skips masking process if "getMaskPattern" is -1.
  234. /// See 8.7 of JISX0510:2004 (p.38) for how to embed data bits.
  235. /// </summary>
  236. /// <param name="dataBits">The data bits.</param>
  237. /// <param name="maskPattern">The mask pattern.</param>
  238. /// <param name="matrix">The matrix.</param>
  239. public static void embedDataBits(BitArray dataBits, int maskPattern, ByteMatrix matrix)
  240. {
  241. int bitIndex = 0;
  242. int direction = -1;
  243. // Start from the right bottom cell.
  244. int x = matrix.Width - 1;
  245. int y = matrix.Height - 1;
  246. while (x > 0)
  247. {
  248. // Skip the vertical timing pattern.
  249. if (x == 6)
  250. {
  251. x -= 1;
  252. }
  253. while (y >= 0 && y < matrix.Height)
  254. {
  255. for (int i = 0; i < 2; ++i)
  256. {
  257. int xx = x - i;
  258. // Skip the cell if it's not empty.
  259. if (!isEmpty(matrix[xx, y]))
  260. {
  261. continue;
  262. }
  263. int bit;
  264. if (bitIndex < dataBits.Size)
  265. {
  266. bit = dataBits[bitIndex] ? 1 : 0;
  267. ++bitIndex;
  268. }
  269. else
  270. {
  271. // Padding bit. If there is no bit left, we'll fill the left cells with 0, as described
  272. // in 8.4.9 of JISX0510:2004 (p. 24).
  273. bit = 0;
  274. }
  275. // Skip masking if mask_pattern is -1.
  276. if (maskPattern != -1)
  277. {
  278. if (MaskUtil.getDataMaskBit(maskPattern, xx, y))
  279. {
  280. bit ^= 0x1;
  281. }
  282. }
  283. matrix[xx, y] = bit;
  284. }
  285. y += direction;
  286. }
  287. direction = -direction; // Reverse the direction.
  288. y += direction;
  289. x -= 2; // Move to the left.
  290. }
  291. // All bits should be consumed.
  292. if (bitIndex != dataBits.Size)
  293. {
  294. throw new Exception("Not all bits consumed: " + bitIndex + '/' + dataBits.Size);
  295. }
  296. }
  297. /// <summary>
  298. /// Return the position of the most significant bit set (to one) in the "value". The most
  299. /// significant bit is position 32. If there is no bit set, return 0. Examples:
  300. /// - findMSBSet(0) => 0
  301. /// - findMSBSet(1) => 1
  302. /// - findMSBSet(255) => 8
  303. /// </summary>
  304. /// <param name="value_Renamed">The value_ renamed.</param>
  305. /// <returns></returns>
  306. public static int findMSBSet(int value_Renamed)
  307. {
  308. int numDigits = 0;
  309. while (value_Renamed != 0)
  310. {
  311. value_Renamed = (int)((uint)value_Renamed >> 1);
  312. ++numDigits;
  313. }
  314. return numDigits;
  315. }
  316. /// <summary>
  317. /// Calculate BCH (Bose-Chaudhuri-Hocquenghem) code for "value" using polynomial "poly". The BCH
  318. /// code is used for encoding type information and version information.
  319. /// Example: Calculation of version information of 7.
  320. /// f(x) is created from 7.
  321. /// - 7 = 000111 in 6 bits
  322. /// - f(x) = x^2 + x^2 + x^1
  323. /// g(x) is given by the standard (p. 67)
  324. /// - g(x) = x^12 + x^11 + x^10 + x^9 + x^8 + x^5 + x^2 + 1
  325. /// Multiply f(x) by x^(18 - 6)
  326. /// - f'(x) = f(x) * x^(18 - 6)
  327. /// - f'(x) = x^14 + x^13 + x^12
  328. /// Calculate the remainder of f'(x) / g(x)
  329. /// x^2
  330. /// __________________________________________________
  331. /// g(x) )x^14 + x^13 + x^12
  332. /// x^14 + x^13 + x^12 + x^11 + x^10 + x^7 + x^4 + x^2
  333. /// --------------------------------------------------
  334. /// x^11 + x^10 + x^7 + x^4 + x^2
  335. ///
  336. /// The remainder is x^11 + x^10 + x^7 + x^4 + x^2
  337. /// Encode it in binary: 110010010100
  338. /// The return value is 0xc94 (1100 1001 0100)
  339. ///
  340. /// Since all coefficients in the polynomials are 1 or 0, we can do the calculation by bit
  341. /// operations. We don't care if cofficients are positive or negative.
  342. /// </summary>
  343. /// <param name="value">The value.</param>
  344. /// <param name="poly">The poly.</param>
  345. /// <returns></returns>
  346. public static int calculateBCHCode(int value, int poly)
  347. {
  348. if (poly == 0)
  349. throw new ArgumentException("0 polynominal", "poly");
  350. // If poly is "1 1111 0010 0101" (version info poly), msbSetInPoly is 13. We'll subtract 1
  351. // from 13 to make it 12.
  352. int msbSetInPoly = findMSBSet(poly);
  353. value <<= msbSetInPoly - 1;
  354. // Do the division business using exclusive-or operations.
  355. while (findMSBSet(value) >= msbSetInPoly)
  356. {
  357. value ^= poly << (findMSBSet(value) - msbSetInPoly);
  358. }
  359. // Now the "value" is the remainder (i.e. the BCH code)
  360. return value;
  361. }
  362. /// <summary>
  363. /// Make bit vector of type information. On success, store the result in "bits" and return true.
  364. /// Encode error correction level and mask pattern. See 8.9 of
  365. /// JISX0510:2004 (p.45) for details.
  366. /// </summary>
  367. /// <param name="ecLevel">The ec level.</param>
  368. /// <param name="maskPattern">The mask pattern.</param>
  369. /// <param name="bits">The bits.</param>
  370. public static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits)
  371. {
  372. if (!QRCode.isValidMaskPattern(maskPattern))
  373. {
  374. throw new Exception("Invalid mask pattern");
  375. }
  376. int typeInfo = (ecLevel.Bits << 3) | maskPattern;
  377. bits.appendBits(typeInfo, 5);
  378. int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
  379. bits.appendBits(bchCode, 10);
  380. BitArray maskBits = new BitArray();
  381. maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
  382. bits.xor(maskBits);
  383. if (bits.Size != 15)
  384. {
  385. // Just in case.
  386. throw new Exception("should not happen but we got: " + bits.Size);
  387. }
  388. }
  389. /// <summary>
  390. /// Make bit vector of version information. On success, store the result in "bits" and return true.
  391. /// See 8.10 of JISX0510:2004 (p.45) for details.
  392. /// </summary>
  393. /// <param name="version">The version.</param>
  394. /// <param name="bits">The bits.</param>
  395. public static void makeVersionInfoBits(Version version, BitArray bits)
  396. {
  397. bits.appendBits(version.VersionNumber, 6);
  398. int bchCode = calculateBCHCode(version.VersionNumber, VERSION_INFO_POLY);
  399. bits.appendBits(bchCode, 12);
  400. if (bits.Size != 18)
  401. {
  402. // Just in case.
  403. throw new Exception("should not happen but we got: " + bits.Size);
  404. }
  405. }
  406. /// <summary>
  407. /// Check if "value" is empty.
  408. /// </summary>
  409. /// <param name="value">The value.</param>
  410. /// <returns>
  411. /// <c>true</c> if the specified value is empty; otherwise, <c>false</c>.
  412. /// </returns>
  413. private static bool isEmpty(int value)
  414. {
  415. return value == 2;
  416. }
  417. private static void embedTimingPatterns(ByteMatrix matrix)
  418. {
  419. // -8 is for skipping position detection patterns (size 7), and two horizontal/vertical
  420. // separation patterns (size 1). Thus, 8 = 7 + 1.
  421. for (int i = 8; i < matrix.Width - 8; ++i)
  422. {
  423. int bit = (i + 1) % 2;
  424. // Horizontal line.
  425. if (isEmpty(matrix[i, 6]))
  426. {
  427. matrix[i, 6] = bit;
  428. }
  429. // Vertical line.
  430. if (isEmpty(matrix[6, i]))
  431. {
  432. matrix[6, i] = bit;
  433. }
  434. }
  435. }
  436. /// <summary>
  437. /// Embed the lonely dark dot at left bottom corner. JISX0510:2004 (p.46)
  438. /// </summary>
  439. /// <param name="matrix">The matrix.</param>
  440. private static void embedDarkDotAtLeftBottomCorner(ByteMatrix matrix)
  441. {
  442. if (matrix[8, matrix.Height - 8] == 0)
  443. {
  444. throw new Exception();
  445. }
  446. matrix[8, matrix.Height - 8] = 1;
  447. }
  448. private static void embedHorizontalSeparationPattern(int xStart, int yStart, ByteMatrix matrix)
  449. {
  450. for (int x = 0; x < 8; ++x)
  451. {
  452. if (!isEmpty(matrix[xStart + x, yStart]))
  453. {
  454. throw new Exception();
  455. }
  456. matrix[xStart + x, yStart] = 0;
  457. }
  458. }
  459. private static void embedVerticalSeparationPattern(int xStart, int yStart, ByteMatrix matrix)
  460. {
  461. for (int y = 0; y < 7; ++y)
  462. {
  463. if (!isEmpty(matrix[xStart, yStart + y]))
  464. {
  465. throw new Exception();
  466. }
  467. matrix[xStart, yStart + y] = 0;
  468. }
  469. }
  470. /// <summary>
  471. /// Note that we cannot unify the function with embedPositionDetectionPattern() despite they are
  472. /// almost identical, since we cannot write a function that takes 2D arrays in different sizes in
  473. /// C/C++. We should live with the fact.
  474. /// </summary>
  475. /// <param name="xStart">The x start.</param>
  476. /// <param name="yStart">The y start.</param>
  477. /// <param name="matrix">The matrix.</param>
  478. private static void embedPositionAdjustmentPattern(int xStart, int yStart, ByteMatrix matrix)
  479. {
  480. for (int y = 0; y < 5; ++y)
  481. {
  482. for (int x = 0; x < 5; ++x)
  483. {
  484. matrix[xStart + x, yStart + y] = POSITION_ADJUSTMENT_PATTERN[y][x];
  485. }
  486. }
  487. }
  488. private static void embedPositionDetectionPattern(int xStart, int yStart, ByteMatrix matrix)
  489. {
  490. for (int y = 0; y < 7; ++y)
  491. {
  492. for (int x = 0; x < 7; ++x)
  493. {
  494. matrix[xStart + x, yStart + y] = POSITION_DETECTION_PATTERN[y][x];
  495. }
  496. }
  497. }
  498. /// <summary>
  499. /// Embed position detection patterns and surrounding vertical/horizontal separators.
  500. /// </summary>
  501. /// <param name="matrix">The matrix.</param>
  502. private static void embedPositionDetectionPatternsAndSeparators(ByteMatrix matrix)
  503. {
  504. // Embed three big squares at corners.
  505. int pdpWidth = POSITION_DETECTION_PATTERN[0].Length;
  506. // Left top corner.
  507. embedPositionDetectionPattern(0, 0, matrix);
  508. // Right top corner.
  509. embedPositionDetectionPattern(matrix.Width - pdpWidth, 0, matrix);
  510. // Left bottom corner.
  511. embedPositionDetectionPattern(0, matrix.Width - pdpWidth, matrix);
  512. // Embed horizontal separation patterns around the squares.
  513. const int hspWidth = 8;
  514. // Left top corner.
  515. embedHorizontalSeparationPattern(0, hspWidth - 1, matrix);
  516. // Right top corner.
  517. embedHorizontalSeparationPattern(matrix.Width - hspWidth, hspWidth - 1, matrix);
  518. // Left bottom corner.
  519. embedHorizontalSeparationPattern(0, matrix.Width - hspWidth, matrix);
  520. // Embed vertical separation patterns around the squares.
  521. const int vspSize = 7;
  522. // Left top corner.
  523. embedVerticalSeparationPattern(vspSize, 0, matrix);
  524. // Right top corner.
  525. embedVerticalSeparationPattern(matrix.Height - vspSize - 1, 0, matrix);
  526. // Left bottom corner.
  527. embedVerticalSeparationPattern(vspSize, matrix.Height - vspSize, matrix);
  528. }
  529. /// <summary>
  530. /// Embed position adjustment patterns if need be.
  531. /// </summary>
  532. /// <param name="version">The version.</param>
  533. /// <param name="matrix">The matrix.</param>
  534. private static void maybeEmbedPositionAdjustmentPatterns(Version version, ByteMatrix matrix)
  535. {
  536. if (version.VersionNumber < 2)
  537. {
  538. // The patterns appear if version >= 2
  539. return;
  540. }
  541. int index = version.VersionNumber - 1;
  542. int[] coordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index];
  543. int numCoordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index].Length;
  544. for (int i = 0; i < numCoordinates; ++i)
  545. {
  546. for (int j = 0; j < numCoordinates; ++j)
  547. {
  548. int y = coordinates[i];
  549. int x = coordinates[j];
  550. if (x == -1 || y == -1)
  551. {
  552. continue;
  553. }
  554. // If the cell is unset, we embed the position adjustment pattern here.
  555. if (isEmpty(matrix[x, y]))
  556. {
  557. // -2 is necessary since the x/y coordinates point to the center of the pattern, not the
  558. // left top corner.
  559. embedPositionAdjustmentPattern(x - 2, y - 2, matrix);
  560. }
  561. }
  562. }
  563. }
  564. }
  565. }