MatrixUtil.cs 22 KB

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