EccLevel.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Class EccLevel
  4. *
  5. * @created 19.11.2020
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2020 smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCode\Common;
  11. use chillerlan\QRCode\QRCodeException;
  12. use function array_column;
  13. /**
  14. * This class encapsulates the four error correction levels defined by the QR code standard.
  15. */
  16. final class EccLevel{
  17. // ISO/IEC 18004:2000 Tables 12, 25
  18. /** @var int */
  19. public const L = 0b01; // 7%.
  20. /** @var int */
  21. public const M = 0b00; // 15%.
  22. /** @var int */
  23. public const Q = 0b11; // 25%.
  24. /** @var int */
  25. public const H = 0b10; // 30%.
  26. /**
  27. * ISO/IEC 18004:2000 Tables 7-11 - Number of symbol characters and input data capacity for versions 1 to 40
  28. *
  29. * @var int[][]
  30. */
  31. private const MAX_BITS = [
  32. // [ L, M, Q, H] // v => modules
  33. [ 0, 0, 0, 0], // 0 => will be ignored, index starts at 1
  34. [ 152, 128, 104, 72], // 1 => 21
  35. [ 272, 224, 176, 128], // 2 => 25
  36. [ 440, 352, 272, 208], // 3 => 29
  37. [ 640, 512, 384, 288], // 4 => 33
  38. [ 864, 688, 496, 368], // 5 => 37
  39. [ 1088, 864, 608, 480], // 6 => 41
  40. [ 1248, 992, 704, 528], // 7 => 45
  41. [ 1552, 1232, 880, 688], // 8 => 49
  42. [ 1856, 1456, 1056, 800], // 9 => 53
  43. [ 2192, 1728, 1232, 976], // 10 => 57
  44. [ 2592, 2032, 1440, 1120], // 11 => 61
  45. [ 2960, 2320, 1648, 1264], // 12 => 65
  46. [ 3424, 2672, 1952, 1440], // 13 => 69 NICE!
  47. [ 3688, 2920, 2088, 1576], // 14 => 73
  48. [ 4184, 3320, 2360, 1784], // 15 => 77
  49. [ 4712, 3624, 2600, 2024], // 16 => 81
  50. [ 5176, 4056, 2936, 2264], // 17 => 85
  51. [ 5768, 4504, 3176, 2504], // 18 => 89
  52. [ 6360, 5016, 3560, 2728], // 19 => 93
  53. [ 6888, 5352, 3880, 3080], // 20 => 97
  54. [ 7456, 5712, 4096, 3248], // 21 => 101
  55. [ 8048, 6256, 4544, 3536], // 22 => 105
  56. [ 8752, 6880, 4912, 3712], // 23 => 109
  57. [ 9392, 7312, 5312, 4112], // 24 => 113
  58. [10208, 8000, 5744, 4304], // 25 => 117
  59. [10960, 8496, 6032, 4768], // 26 => 121
  60. [11744, 9024, 6464, 5024], // 27 => 125
  61. [12248, 9544, 6968, 5288], // 28 => 129
  62. [13048, 10136, 7288, 5608], // 29 => 133
  63. [13880, 10984, 7880, 5960], // 30 => 137
  64. [14744, 11640, 8264, 6344], // 31 => 141
  65. [15640, 12328, 8920, 6760], // 32 => 145
  66. [16568, 13048, 9368, 7208], // 33 => 149
  67. [17528, 13800, 9848, 7688], // 34 => 153
  68. [18448, 14496, 10288, 7888], // 35 => 157
  69. [19472, 15312, 10832, 8432], // 36 => 161
  70. [20528, 15936, 11408, 8768], // 37 => 165
  71. [21616, 16816, 12016, 9136], // 38 => 169
  72. [22496, 17728, 12656, 9776], // 39 => 173
  73. [23648, 18672, 13328, 10208], // 40 => 177
  74. ];
  75. /**
  76. * ISO/IEC 18004:2000 Section 8.9 - Format Information
  77. *
  78. * ECC level -> mask pattern
  79. *
  80. * @var int[][]
  81. */
  82. private const FORMAT_PATTERN = [
  83. [ // L
  84. 0b111011111000100,
  85. 0b111001011110011,
  86. 0b111110110101010,
  87. 0b111100010011101,
  88. 0b110011000101111,
  89. 0b110001100011000,
  90. 0b110110001000001,
  91. 0b110100101110110,
  92. ],
  93. [ // M
  94. 0b101010000010010,
  95. 0b101000100100101,
  96. 0b101111001111100,
  97. 0b101101101001011,
  98. 0b100010111111001,
  99. 0b100000011001110,
  100. 0b100111110010111,
  101. 0b100101010100000,
  102. ],
  103. [ // Q
  104. 0b011010101011111,
  105. 0b011000001101000,
  106. 0b011111100110001,
  107. 0b011101000000110,
  108. 0b010010010110100,
  109. 0b010000110000011,
  110. 0b010111011011010,
  111. 0b010101111101101,
  112. ],
  113. [ // H
  114. 0b001011010001001,
  115. 0b001001110111110,
  116. 0b001110011100111,
  117. 0b001100111010000,
  118. 0b000011101100010,
  119. 0b000001001010101,
  120. 0b000110100001100,
  121. 0b000100000111011,
  122. ],
  123. ];
  124. /**
  125. * The current ECC level value
  126. *
  127. * L: 0b01
  128. * M: 0b00
  129. * Q: 0b11
  130. * H: 0b10
  131. */
  132. private int $eccLevel;
  133. /**
  134. * @param int $eccLevel containing the two bits encoding a QR Code's error correction level
  135. *
  136. * @throws \chillerlan\QRCode\QRCodeException
  137. */
  138. public function __construct(int $eccLevel){
  139. if((0b11 & $eccLevel) !== $eccLevel){
  140. throw new QRCodeException('invalid ECC level');
  141. }
  142. $this->eccLevel = $eccLevel;
  143. }
  144. /**
  145. * returns the string representation of the current ECC level
  146. */
  147. public function __toString():string{
  148. return [
  149. self::L => 'L',
  150. self::M => 'M',
  151. self::Q => 'Q',
  152. self::H => 'H',
  153. ][$this->eccLevel];
  154. }
  155. /**
  156. * returns the current ECC level
  157. */
  158. public function getLevel():int{
  159. return $this->eccLevel;
  160. }
  161. /**
  162. * returns the ordinal value of the current ECC level
  163. *
  164. * references to the keys of the following tables:
  165. *
  166. * @see \chillerlan\QRCode\Common\EccLevel::MAX_BITS
  167. * @see \chillerlan\QRCode\Common\EccLevel::FORMAT_PATTERN
  168. * @see \chillerlan\QRCode\Common\Version::RSBLOCKS
  169. */
  170. public function getOrdinal():int{
  171. return [
  172. self::L => 0,
  173. self::M => 1,
  174. self::Q => 2,
  175. self::H => 3,
  176. ][$this->eccLevel];
  177. }
  178. /**
  179. * returns the format pattern for the given $eccLevel and $maskPattern
  180. */
  181. public function getformatPattern(MaskPattern $maskPattern):int{
  182. return self::FORMAT_PATTERN[$this->getOrdinal()][$maskPattern->getPattern()];
  183. }
  184. /**
  185. * returns an array with the max bit lengths for version 1-40 and the current ECC level
  186. *
  187. * @return int[]
  188. */
  189. public function getMaxBits():array{
  190. $col = array_column(self::MAX_BITS, $this->getOrdinal());
  191. unset($col[0]); // remove the inavlid index 0
  192. return $col;
  193. }
  194. /**
  195. * Returns the maximum bit length for the given version and current ECC level
  196. */
  197. public function getMaxBitsForVersion(Version $version):int{
  198. return self::MAX_BITS[$version->getVersionNumber()][$this->getOrdinal()];
  199. }
  200. }