Encoder.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. using System;
  17. namespace FastReport.Barcode.Aztec
  18. {
  19. /// <summary>
  20. /// Generates Aztec 2D barcodes.
  21. /// </summary>
  22. /// <author>Rustam Abdullaev</author>
  23. internal static class Encoder
  24. {
  25. public const int DEFAULT_EC_PERCENT = 33; // default minimal percentage of error check words
  26. public const int DEFAULT_AZTEC_LAYERS = 0;
  27. private const int MAX_NB_BITS = 32;
  28. private const int MAX_NB_BITS_COMPACT = 4;
  29. private static readonly int[] WORD_SIZE = {
  30. 4, 6, 6, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  31. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12
  32. };
  33. /// <summary>
  34. /// Encodes the given binary content as an Aztec symbol
  35. /// </summary>
  36. /// <param name="data">input data string</param>
  37. /// <returns>Aztec symbol matrix with metadata</returns>
  38. public static AztecCode encode(byte[] data)
  39. {
  40. return encode(data, DEFAULT_EC_PERCENT, DEFAULT_AZTEC_LAYERS);
  41. }
  42. /// <summary>
  43. /// Encodes the given binary content as an Aztec symbol
  44. /// </summary>
  45. /// <param name="data">input data string</param>
  46. /// <param name="minECCPercent">minimal percentage of error check words (According to ISO/IEC 24778:2008,
  47. /// a minimum of 23% + 3 words is recommended)</param>
  48. /// <param name="userSpecifiedLayers">if non-zero, a user-specified value for the number of layers</param>
  49. /// <returns>
  50. /// Aztec symbol matrix with metadata
  51. /// </returns>
  52. public static AztecCode encode(byte[] data, int minECCPercent, int userSpecifiedLayers)
  53. {
  54. // High-level encode
  55. BitArray bits = new HighLevelEncoder(data).encode();
  56. // stuff bits and choose symbol size
  57. int eccBits = bits.Size*minECCPercent/100 + 11;
  58. int totalSizeBits = bits.Size + eccBits;
  59. bool compact;
  60. int layers;
  61. int totalBitsInLayer;
  62. int wordSize;
  63. BitArray stuffedBits;
  64. if (userSpecifiedLayers != DEFAULT_AZTEC_LAYERS)
  65. {
  66. compact = userSpecifiedLayers < 0;
  67. layers = Math.Abs(userSpecifiedLayers);
  68. if (layers > (compact ? MAX_NB_BITS_COMPACT : MAX_NB_BITS))
  69. {
  70. throw new ArgumentException(
  71. String.Format("Illegal value {0} for layers", userSpecifiedLayers));
  72. }
  73. totalBitsInLayer = TotalBitsInLayer(layers, compact);
  74. wordSize = WORD_SIZE[layers];
  75. int usableBitsInLayers = totalBitsInLayer - (totalBitsInLayer%wordSize);
  76. stuffedBits = stuffBits(bits, wordSize);
  77. if (stuffedBits.Size + eccBits > usableBitsInLayers)
  78. {
  79. throw new ArgumentException("Data to large for user specified layer");
  80. }
  81. if (compact && stuffedBits.Size > wordSize*64)
  82. {
  83. // Compact format only allows 64 data words, though C4 can hold more words than that
  84. throw new ArgumentException("Data to large for user specified layer");
  85. }
  86. }
  87. else
  88. {
  89. wordSize = 0;
  90. stuffedBits = null;
  91. // We look at the possible table sizes in the order Compact1, Compact2, Compact3,
  92. // Compact4, Normal4,... Normal(i) for i < 4 isn't typically used since Compact(i+1)
  93. // is the same size, but has more data.
  94. for (int i = 0;; i++)
  95. {
  96. if (i > MAX_NB_BITS)
  97. {
  98. throw new ArgumentException("Data too large for an Aztec code");
  99. }
  100. compact = i <= 3;
  101. layers = compact ? i + 1 : i;
  102. totalBitsInLayer = TotalBitsInLayer(layers, compact);
  103. if (totalSizeBits > totalBitsInLayer)
  104. {
  105. continue;
  106. }
  107. // [Re]stuff the bits if this is the first opportunity, or if the
  108. // wordSize has changed
  109. if (wordSize != WORD_SIZE[layers])
  110. {
  111. wordSize = WORD_SIZE[layers];
  112. stuffedBits = stuffBits(bits, wordSize);
  113. }
  114. if (stuffedBits == null)
  115. {
  116. continue;
  117. }
  118. int usableBitsInLayers = totalBitsInLayer - (totalBitsInLayer%wordSize);
  119. if (compact && stuffedBits.Size > wordSize*64)
  120. {
  121. // Compact format only allows 64 data words, though C4 can hold more words than that
  122. continue;
  123. }
  124. if (stuffedBits.Size + eccBits <= usableBitsInLayers)
  125. {
  126. break;
  127. }
  128. }
  129. }
  130. BitArray messageBits = generateCheckWords(stuffedBits, totalBitsInLayer, wordSize);
  131. // generate mode message
  132. int messageSizeInWords = stuffedBits.Size / wordSize;
  133. BitArray modeMessage = generateModeMessage(compact, layers, messageSizeInWords);
  134. // allocate symbol
  135. int baseMatrixSize = compact ? 11 + layers*4 : 14 + layers*4; // not including alignment lines
  136. int[] alignmentMap = new int[baseMatrixSize];
  137. int matrixSize;
  138. if (compact)
  139. {
  140. // no alignment marks in compact mode, alignmentMap is a no-op
  141. matrixSize = baseMatrixSize;
  142. for (int i = 0; i < alignmentMap.Length; i++)
  143. {
  144. alignmentMap[i] = i;
  145. }
  146. }
  147. else
  148. {
  149. matrixSize = baseMatrixSize + 1 + 2*((baseMatrixSize/2 - 1)/15);
  150. int origCenter = baseMatrixSize/2;
  151. int center = matrixSize/2;
  152. for (int i = 0; i < origCenter; i++)
  153. {
  154. int newOffset = i + i/15;
  155. alignmentMap[origCenter - i - 1] = center - newOffset - 1;
  156. alignmentMap[origCenter + i] = center + newOffset + 1;
  157. }
  158. }
  159. BitMatrix matrix = new BitMatrix(matrixSize);
  160. // draw data bits
  161. for (int i = 0, rowOffset = 0; i < layers; i++)
  162. {
  163. int rowSize = compact ? (layers - i)*4 + 9 : (layers - i)*4 + 12;
  164. for (int j = 0; j < rowSize; j++)
  165. {
  166. int columnOffset = j*2;
  167. for (int k = 0; k < 2; k++)
  168. {
  169. if (messageBits[rowOffset + columnOffset + k])
  170. {
  171. matrix[alignmentMap[i*2 + k], alignmentMap[i*2 + j]] = true;
  172. }
  173. if (messageBits[rowOffset + rowSize*2 + columnOffset + k])
  174. {
  175. matrix[alignmentMap[i*2 + j], alignmentMap[baseMatrixSize - 1 - i*2 - k]] = true;
  176. }
  177. if (messageBits[rowOffset + rowSize*4 + columnOffset + k])
  178. {
  179. matrix[alignmentMap[baseMatrixSize - 1 - i*2 - k], alignmentMap[baseMatrixSize - 1 - i*2 - j]] = true;
  180. }
  181. if (messageBits[rowOffset + rowSize*6 + columnOffset + k])
  182. {
  183. matrix[alignmentMap[baseMatrixSize - 1 - i*2 - j], alignmentMap[i*2 + k]] = true;
  184. }
  185. }
  186. }
  187. rowOffset += rowSize*8;
  188. }
  189. // draw mode message
  190. drawModeMessage(matrix, compact, matrixSize, modeMessage);
  191. // draw alignment marks
  192. if (compact)
  193. {
  194. drawBullsEye(matrix, matrixSize/2, 5);
  195. }
  196. else
  197. {
  198. drawBullsEye(matrix, matrixSize/2, 7);
  199. for (int i = 0, j = 0; i < baseMatrixSize/2 - 1; i += 15, j += 16)
  200. {
  201. for (int k = (matrixSize/2) & 1; k < matrixSize; k += 2)
  202. {
  203. matrix[matrixSize/2 - j, k] = true;
  204. matrix[matrixSize/2 + j, k] = true;
  205. matrix[k, matrixSize/2 - j] = true;
  206. matrix[k, matrixSize/2 + j] = true;
  207. }
  208. }
  209. }
  210. AztecCode aztec = new AztecCode();
  211. aztec.isCompact = compact;
  212. aztec.Size = matrixSize;
  213. aztec.Layers = layers;
  214. aztec.CodeWords = messageSizeInWords;
  215. aztec.Matrix = matrix;
  216. return aztec;
  217. }
  218. private static void drawBullsEye(BitMatrix matrix, int center, int size)
  219. {
  220. for (int i = 0; i < size; i += 2)
  221. {
  222. for (int j = center - i; j <= center + i; j++)
  223. {
  224. matrix[j, center - i] = true;
  225. matrix[j, center + i] = true;
  226. matrix[center - i, j] = true;
  227. matrix[center + i, j] = true;
  228. }
  229. }
  230. matrix[center - size, center - size] = true;
  231. matrix[center - size + 1, center - size] = true;
  232. matrix[center - size, center - size + 1] = true;
  233. matrix[center + size, center - size] = true;
  234. matrix[center + size, center - size + 1] = true;
  235. matrix[center + size, center + size - 1] = true;
  236. }
  237. internal static BitArray generateModeMessage(bool compact, int layers, int messageSizeInWords)
  238. {
  239. BitArray modeMessage = new BitArray();
  240. if (compact)
  241. {
  242. modeMessage.appendBits(layers - 1, 2);
  243. modeMessage.appendBits(messageSizeInWords - 1, 6);
  244. modeMessage = generateCheckWords(modeMessage, 28, 4);
  245. }
  246. else
  247. {
  248. modeMessage.appendBits(layers - 1, 5);
  249. modeMessage.appendBits(messageSizeInWords - 1, 11);
  250. modeMessage = generateCheckWords(modeMessage, 40, 4);
  251. }
  252. return modeMessage;
  253. }
  254. private static void drawModeMessage(BitMatrix matrix, bool compact, int matrixSize, BitArray modeMessage)
  255. {
  256. int center = matrixSize / 2;
  257. if (compact)
  258. {
  259. for (int i = 0; i < 7; i++)
  260. {
  261. int offset = center - 3 + i;
  262. if (modeMessage[i])
  263. {
  264. matrix[offset, center - 5] = true;
  265. }
  266. if (modeMessage[i + 7])
  267. {
  268. matrix[center + 5, offset] = true;
  269. }
  270. if (modeMessage[20 - i])
  271. {
  272. matrix[offset, center + 5] = true;
  273. }
  274. if (modeMessage[27 - i])
  275. {
  276. matrix[center - 5, offset] = true;
  277. }
  278. }
  279. }
  280. else
  281. {
  282. for (int i = 0; i < 10; i++)
  283. {
  284. int offset = center - 5 + i + i / 5;
  285. if (modeMessage[i])
  286. {
  287. matrix[offset, center - 7] = true;
  288. }
  289. if (modeMessage[i + 10])
  290. {
  291. matrix[center + 7, offset] = true;
  292. }
  293. if (modeMessage[29 - i])
  294. {
  295. matrix[offset, center + 7] = true;
  296. }
  297. if (modeMessage[39 - i])
  298. {
  299. matrix[center - 7, offset] = true;
  300. }
  301. }
  302. }
  303. }
  304. private static BitArray generateCheckWords(BitArray bitArray, int totalBits, int wordSize)
  305. {
  306. if (bitArray.Size % wordSize != 0)
  307. throw new InvalidOperationException("size of bit array is not a multiple of the word size");
  308. // bitArray is guaranteed to be a multiple of the wordSize, so no padding needed
  309. int messageSizeInWords = bitArray.Size / wordSize;
  310. ReedSolomonEncoder rs = new ReedSolomonEncoder(getGF(wordSize));
  311. int totalWords = totalBits / wordSize;
  312. int[] messageWords = bitsToWords(bitArray, wordSize, totalWords);
  313. rs.encode(messageWords, totalWords - messageSizeInWords);
  314. int startPad = totalBits % wordSize;
  315. BitArray messageBits = new BitArray();
  316. messageBits.appendBits(0, startPad);
  317. foreach (int messageWord in messageWords)
  318. {
  319. messageBits.appendBits(messageWord, wordSize);
  320. }
  321. return messageBits;
  322. }
  323. private static int[] bitsToWords(BitArray stuffedBits, int wordSize, int totalWords)
  324. {
  325. int[] message = new int[totalWords];
  326. int i;
  327. int n;
  328. for (i = 0, n = stuffedBits.Size / wordSize; i < n; i++)
  329. {
  330. int value = 0;
  331. for (int j = 0; j < wordSize; j++)
  332. {
  333. value |= stuffedBits[i * wordSize + j] ? (1 << wordSize - j - 1) : 0;
  334. }
  335. message[i] = value;
  336. }
  337. return message;
  338. }
  339. private static GenericGF getGF(int wordSize)
  340. {
  341. switch (wordSize)
  342. {
  343. case 4:
  344. return GenericGF.AZTEC_PARAM;
  345. case 6:
  346. return GenericGF.AZTEC_DATA_6;
  347. case 8:
  348. return GenericGF.AZTEC_DATA_8;
  349. case 10:
  350. return GenericGF.AZTEC_DATA_10;
  351. case 12:
  352. return GenericGF.AZTEC_DATA_12;
  353. default:
  354. return null;
  355. }
  356. }
  357. internal static BitArray stuffBits(BitArray bits, int wordSize)
  358. {
  359. BitArray @out = new BitArray();
  360. int n = bits.Size;
  361. int mask = (1 << wordSize) - 2;
  362. for (int i = 0; i < n; i += wordSize)
  363. {
  364. int word = 0;
  365. for (int j = 0; j < wordSize; j++)
  366. {
  367. if (i + j >= n || bits[i + j])
  368. {
  369. word |= 1 << (wordSize - 1 - j);
  370. }
  371. }
  372. if ((word & mask) == mask)
  373. {
  374. @out.appendBits(word & mask, wordSize);
  375. i--;
  376. }
  377. else if ((word & mask) == 0)
  378. {
  379. @out.appendBits(word | 1, wordSize);
  380. i--;
  381. }
  382. else
  383. {
  384. @out.appendBits(word, wordSize);
  385. }
  386. }
  387. return @out;
  388. }
  389. private static int TotalBitsInLayer(int layers, bool compact)
  390. {
  391. return ((compact ? 88 : 112) + 16 * layers) * layers;
  392. }
  393. }
  394. }