mqrspec.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * qrencode - QR Code encoder
  3. *
  4. * Micro QR Code specification in convenient format.
  5. * Copyright (C) 2006-2017 Kentaro Fukuchi <kentaro@fukuchi.org>
  6. *
  7. * The following data / specifications are taken from
  8. * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
  9. * or
  10. * "Automatic identification and data capture techniques --
  11. * QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. #if HAVE_CONFIG_H
  28. # include "config.h"
  29. #endif
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include "mqrspec.h"
  35. /******************************************************************************
  36. * Version and capacity
  37. *****************************************************************************/
  38. typedef struct {
  39. int width; ///< Edge length of the symbol
  40. int ec[4]; ///< Number of ECC code (bytes)
  41. } MQRspec_Capacity;
  42. /**
  43. * Table of the capacity of symbols
  44. * See Table 1 (pp.106) and Table 8 (pp.113) of Appendix 1, JIS X0510:2004.
  45. */
  46. static const MQRspec_Capacity mqrspecCapacity[MQRSPEC_VERSION_MAX + 1] = {
  47. { 0, {0, 0, 0, 0}},
  48. { 11, {2, 0, 0, 0}},
  49. { 13, {5, 6, 0, 0}},
  50. { 15, {6, 8, 0, 0}},
  51. { 17, {8, 10, 14, 0}}
  52. };
  53. int MQRspec_getDataLengthBit(int version, QRecLevel level)
  54. {
  55. int w;
  56. int ecc;
  57. w = mqrspecCapacity[version].width - 1;
  58. ecc = mqrspecCapacity[version].ec[level];
  59. if(ecc == 0) return 0;
  60. return w * w - 64 - ecc * 8;
  61. }
  62. int MQRspec_getDataLength(int version, QRecLevel level)
  63. {
  64. return (MQRspec_getDataLengthBit(version, level) + 4) / 8;
  65. }
  66. int MQRspec_getECCLength(int version, QRecLevel level)
  67. {
  68. return mqrspecCapacity[version].ec[level];
  69. }
  70. int MQRspec_getWidth(int version)
  71. {
  72. return mqrspecCapacity[version].width;
  73. }
  74. /******************************************************************************
  75. * Length indicator
  76. *****************************************************************************/
  77. /**
  78. * See Table 3 (pp.107) of Appendix 1, JIS X0510:2004.
  79. */
  80. static const int lengthTableBits[4][4] = {
  81. { 3, 4, 5, 6},
  82. { 0, 3, 4, 5},
  83. { 0, 0, 4, 5},
  84. { 0, 0, 3, 4}
  85. };
  86. int MQRspec_lengthIndicator(QRencodeMode mode, int version)
  87. {
  88. return lengthTableBits[mode][version - 1];
  89. }
  90. int MQRspec_maximumWords(QRencodeMode mode, int version)
  91. {
  92. int bits;
  93. int words;
  94. bits = lengthTableBits[mode][version - 1];
  95. words = (1 << bits) - 1;
  96. if(mode == QR_MODE_KANJI) {
  97. words *= 2; // the number of bytes is required
  98. }
  99. return words;
  100. }
  101. /******************************************************************************
  102. * Format information
  103. *****************************************************************************/
  104. /* See calcFormatInfo in tests/test_mqrspec.c */
  105. static const unsigned int formatInfo[4][8] = {
  106. {0x4445, 0x55ae, 0x6793, 0x7678, 0x06de, 0x1735, 0x2508, 0x34e3},
  107. {0x4172, 0x5099, 0x62a4, 0x734f, 0x03e9, 0x1202, 0x203f, 0x31d4},
  108. {0x4e2b, 0x5fc0, 0x6dfd, 0x7c16, 0x0cb0, 0x1d5b, 0x2f66, 0x3e8d},
  109. {0x4b1c, 0x5af7, 0x68ca, 0x7921, 0x0987, 0x186c, 0x2a51, 0x3bba}
  110. };
  111. /* See Table 10 of Appendix 1. (pp.115) */
  112. static const int typeTable[MQRSPEC_VERSION_MAX + 1][3] = {
  113. {-1, -1, -1},
  114. { 0, -1, -1},
  115. { 1, 2, -1},
  116. { 3, 4, -1},
  117. { 5, 6, 7}
  118. };
  119. unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level)
  120. {
  121. int type;
  122. if(mask < 0 || mask > 3) return 0;
  123. if(version <= 0 || version > MQRSPEC_VERSION_MAX) return 0;
  124. if(level == QR_ECLEVEL_H) return 0;
  125. type = typeTable[version][level];
  126. if(type < 0) return 0;
  127. return formatInfo[mask][type];
  128. }
  129. /******************************************************************************
  130. * Frame
  131. *****************************************************************************/
  132. /**
  133. * Put a finder pattern.
  134. * @param frame
  135. * @param width
  136. * @param ox,oy upper-left coordinate of the pattern
  137. */
  138. static void putFinderPattern(unsigned char *frame, int width, int ox, int oy)
  139. {
  140. static const unsigned char finder[] = {
  141. 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
  142. 0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
  143. 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
  144. 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
  145. 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
  146. 0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
  147. 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
  148. };
  149. int x, y;
  150. const unsigned char *s;
  151. frame += oy * width + ox;
  152. s = finder;
  153. for(y = 0; y < 7; y++) {
  154. for(x = 0; x < 7; x++) {
  155. frame[x] = s[x];
  156. }
  157. frame += width;
  158. s += 7;
  159. }
  160. }
  161. static unsigned char *MQRspec_createFrame(int version)
  162. {
  163. unsigned char *frame, *p, *q;
  164. int width;
  165. int x, y;
  166. width = mqrspecCapacity[version].width;
  167. frame = (unsigned char *)malloc((size_t)(width * width));
  168. if(frame == NULL) return NULL;
  169. memset(frame, 0, (size_t)(width * width));
  170. /* Finder pattern */
  171. putFinderPattern(frame, width, 0, 0);
  172. /* Separator */
  173. p = frame;
  174. for(y = 0; y < 7; y++) {
  175. p[7] = 0xc0;
  176. p += width;
  177. }
  178. memset(frame + width * 7, 0xc0, 8);
  179. /* Mask format information area */
  180. memset(frame + width * 8 + 1, 0x84, 8);
  181. p = frame + width + 8;
  182. for(y = 0; y < 7; y++) {
  183. *p = 0x84;
  184. p += width;
  185. }
  186. /* Timing pattern */
  187. p = frame + 8;
  188. q = frame + width * 8;
  189. for(x = 1; x < width-7; x++) {
  190. *p = 0x90 | (x & 1);
  191. *q = 0x90 | (x & 1);
  192. p++;
  193. q += width;
  194. }
  195. return frame;
  196. }
  197. unsigned char *MQRspec_newFrame(int version)
  198. {
  199. if(version < 1 || version > MQRSPEC_VERSION_MAX) return NULL;
  200. return MQRspec_createFrame(version);
  201. }