QRUtil.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. /**
  3. * This file is part of the phpQr package
  4. *
  5. * See @see QRCode class for description of package and license.
  6. */
  7. /**
  8. * Import necessary dependencies
  9. */
  10. require_once 'QRCodeException.php';
  11. require_once 'QRPolynominal.php';
  12. /**
  13. * Derived exception
  14. *
  15. * @author Maik Greubel <[email protected]>
  16. * @package phpQr
  17. */
  18. class QRUtilException extends QRCodeException {};
  19. /**
  20. * Mask pattern enumeration
  21. *
  22. * @author Maik Greubel <[email protected]>
  23. * @package phpQr
  24. */
  25. abstract class QRMaskPattern
  26. {
  27. const PATTERN000 = 0;
  28. const PATTERN001 = 1;
  29. const PATTERN010 = 2;
  30. const PATTERN011 = 3;
  31. const PATTERN100 = 4;
  32. const PATTERN101 = 5;
  33. const PATTERN110 = 6;
  34. const PATTERN111 = 7;
  35. }
  36. /**
  37. * The purpose of this class is to provide some common utility
  38. * functionality for the QRCode class and its parts.
  39. *
  40. * @author Maik Greubel <[email protected]>
  41. * @package phpQr
  42. */
  43. class QRUtil
  44. {
  45. /**
  46. * Pattern position table
  47. *
  48. * @var array
  49. */
  50. private $PATTERN_POSITION_TABLE = null;
  51. /**
  52. *
  53. * @var int G15 pattern
  54. */
  55. private $G15;
  56. /**
  57. *
  58. * @var int G18 pattern
  59. */
  60. private $G18;
  61. /**
  62. *
  63. * @var int G15 mask pattern
  64. */
  65. private $G15_MASK;
  66. /**
  67. *
  68. * @var QRUtil Singleton
  69. */
  70. private static $instance;
  71. /**
  72. * Singleton pattern
  73. *
  74. * @return QRUtil
  75. */
  76. public static function getInstance()
  77. {
  78. if(!self::$instance)
  79. {
  80. self::$instance = new self;
  81. }
  82. return self::$instance;
  83. }
  84. /**
  85. * Create a new instance of QRUtil
  86. */
  87. private function __construct()
  88. {
  89. $this->init();
  90. $this->G15 = ((1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0));
  91. $this->G18 = ((1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0));
  92. $this->G15_MASK = ((1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1));
  93. }
  94. /**
  95. * Retrieve the Bose-Chaudhuri-Hocquenghem code type info
  96. *
  97. * @param array $data
  98. *
  99. * @return int
  100. */
  101. public function getBCHTypeInfo($data)
  102. {
  103. $d = $data << 10;
  104. while($this->getBCHDigit($d) - $this->getBCHDigit($this->G15) >= 0)
  105. {
  106. $d ^= ($this->G15 << ($this->getBCHDigit($d) - $this->getBCHDigit($this->G15)));
  107. }
  108. return (($data << 10) | $d) ^ $this->G15_MASK;
  109. }
  110. /**
  111. * Retrieve the Bose-Chaudhuri-Hocquenghem code type number
  112. *
  113. * @param array $data
  114. *
  115. * @return int
  116. */
  117. public function getBCHTypeNumber($data)
  118. {
  119. $d = $data << 12;
  120. while($this->getBCHDigit($d) - $this->getBCHDigit($this->G18) >= 0)
  121. {
  122. $d ^= ($this->G18 << ($this->getBCHDigit($d) - $this->getBCHDigit($this->G18)));
  123. }
  124. return ($data << 12) | $d;
  125. }
  126. /**
  127. * Retrieve the Bose-Chaudhuri-Hocquenghem digit
  128. *
  129. * @param array $data
  130. *
  131. * @return int
  132. */
  133. public function getBCHDigit($data)
  134. {
  135. $digit = 0;
  136. while($data != 0)
  137. {
  138. $digit++;
  139. $data = $data >> 1;
  140. }
  141. return $digit;
  142. }
  143. /**
  144. * Return the pattern position
  145. *
  146. * @param int $typeNumber
  147. * @return array
  148. */
  149. public function getPatternPosition($typeNumber)
  150. {
  151. return $this->PATTERN_POSITION_TABLE[$typeNumber - 1];
  152. }
  153. /**
  154. * Return whether to mask a bit
  155. *
  156. * @param int $maskPattern
  157. * @param int $i
  158. * @param int $j
  159. * @throws QRUtilException
  160. * @return boolean
  161. */
  162. public function getMask($maskPattern, $i, $j)
  163. {
  164. switch($maskPattern)
  165. {
  166. case QRMaskPattern::PATTERN000: return ($i + $j) % 2 == 0;
  167. case QRMaskPattern::PATTERN001: return ($i % 2) == 0;
  168. case QRMaskPattern::PATTERN010: return ($j % 3) == 0;
  169. case QRMaskPattern::PATTERN011: return ($i + $j) % 3 == 0;
  170. case QRMaskPattern::PATTERN100: return (floor($i / 2) + floor($j / 3)) % 2 == 0;
  171. case QRMaskPattern::PATTERN101: return ($i * $j) % 2 + ($i * $j) % 3 == 0;
  172. case QRMaskPattern::PATTERN110: return (($i * $j) % 2 + ($i * $j) % 3) % 2 == 0;
  173. case QRMaskPattern::PATTERN111: return (($i * $j) % 3 + ($i + $j) % 2) % 2 == 0;
  174. default: throw new QRUtilException("Bad mask pattern " . $maskPattern);
  175. }
  176. }
  177. /**
  178. * Return error correction polynom
  179. *
  180. * @param int $errorCorrectLength
  181. * @return QRPolynominal
  182. */
  183. public function getErrorCorrectPolynominal($errorCorrectLength)
  184. {
  185. $a = new QRPolynominal(array(1), 0);
  186. for($i = 0; $i < $errorCorrectLength; $i++)
  187. {
  188. $a = $a->multiply(new QRPolynominal(array(1, QRMath::getInstance()->gexp($i)), 0));
  189. }
  190. return $a;
  191. }
  192. /**
  193. * Get the bitmap length in bits
  194. *
  195. * @param int $mode
  196. * @param int $type
  197. * @throws QRUtilException
  198. * @return int
  199. */
  200. public function getLengthInBits($mode, $type)
  201. {
  202. // 1 - 9
  203. if(1 <= $type && $type < 10)
  204. {
  205. switch($mode)
  206. {
  207. case QRMode::MODE_NUMBER: return 10;
  208. case QRMode::MODE_ALPHA_NUM: return 9;
  209. case QRMode::MODE_8BIT_BYTE: return 8;
  210. case QRMode::MODE_KANJI: return 8;
  211. default: throw new QRUtilException("Bad mode " . $mode);
  212. }
  213. }
  214. // 10 - 26
  215. else if($type < 27)
  216. {
  217. switch($mode)
  218. {
  219. case QRMode::MODE_NUMBER: return 12;
  220. case QRMode::MODE_ALPHA_NUM: return 11;
  221. case QRMode::MODE_8BIT_BYTE: return 16;
  222. case QRMode::MODE_KANJI: return 10;
  223. default: throw new QRUtilException("Bad mode " . $mode);
  224. }
  225. }
  226. // 27 - 40
  227. else if($type < 41)
  228. {
  229. switch($mode)
  230. {
  231. case QRMode::MODE_NUMBER: return 14;
  232. case QRMode::MODE_ALPHA_NUM: return 13;
  233. case QRMode::MODE_8BIT_BYTE: return 16;
  234. case QRMode::MODE_KANJI: return 12;
  235. default: throw new QRUtilException("Bad mode " . $mode);
  236. }
  237. }
  238. else
  239. {
  240. throw new QRUtilException("Bad type " . $type);
  241. }
  242. }
  243. /**
  244. * Calculate the lost point
  245. *
  246. * @param QRCode $qrCode
  247. *
  248. * @return number
  249. */
  250. public function getLostPoint(QRCode $qrCode)
  251. {
  252. $moduleCount = $qrCode->getModuleCount();
  253. $lostPoint = 0;
  254. // Level 1
  255. for($row = 0; $row < $moduleCount; $row++)
  256. {
  257. for($col = 0; $col < $moduleCount; $col++)
  258. {
  259. $sameCount = 0;
  260. $dark = $qrCode->isDark($row, $col);
  261. for($r = -1; $r <= 1; $r++)
  262. {
  263. if($row + $r < 0 || $moduleCount <= $row + $r)
  264. {
  265. continue;
  266. }
  267. for($c = -1; $c <= 1; $c++)
  268. {
  269. if($col + $c < 0 || $moduleCount <= $col + $c)
  270. {
  271. continue;
  272. }
  273. if($r == 0 && $c == 0)
  274. {
  275. continue;
  276. }
  277. if($dark == $qrCode->isDark($row + $r, $col + $c))
  278. {
  279. $sameCount++;
  280. }
  281. }
  282. }
  283. if($sameCount > 5)
  284. {
  285. $lostPoint += (3 + $sameCount - 5);
  286. }
  287. }
  288. }
  289. // Level 2
  290. for($row = 0; $row < $moduleCount - 1; $row++)
  291. {
  292. for($col = 0; $col < $moduleCount - 1; $col++)
  293. {
  294. $count = 0;
  295. if($qrCode->isDark($row, $col )) $count++;
  296. if($qrCode->isDark($row + 1, $col )) $count++;
  297. if($qrCode->isDark($row, $col + 1)) $count++;
  298. if($qrCode->isDark($row + 1, $col + 1)) $count++;
  299. if($count == 0 || $count == 4)
  300. {
  301. $lostPoint += 3;
  302. }
  303. }
  304. }
  305. // Level 3
  306. for($row = 0; $row < $moduleCount; $row++)
  307. {
  308. for($col = 0; $col < $moduleCount - 6; $col++)
  309. {
  310. if($qrCode->isDark($row, $col)
  311. && !$qrCode->isDark($row, $col + 1)
  312. && $qrCode->isDark($row, $col + 2)
  313. && $qrCode->isDark($row, $col + 3)
  314. && $qrCode->isDark($row, $col + 4)
  315. && !$qrCode->isDark($row, $col + 5)
  316. && $qrCode->isDark($row, $col + 6))
  317. {
  318. $lostPoint += 40;
  319. }
  320. }
  321. }
  322. for($col = 0; $col < $moduleCount; $col++)
  323. {
  324. for($row = 0; $row < $moduleCount - 6; $row++)
  325. {
  326. if($qrCode->isDark($row, $col)
  327. && !$qrCode->isDark($row + 1, $col)
  328. && $qrCode->isDark($row + 2, $col)
  329. && $qrCode->isDark($row + 3, $col)
  330. && $qrCode->isDark($row + 4, $col)
  331. && !$qrCode->isDark($row + 5, $col)
  332. && $qrCode->isDark($row + 6, $col))
  333. {
  334. $lostPoint += 40;
  335. }
  336. }
  337. }
  338. // Level 4
  339. $darkCount = 0;
  340. for($col = 0; $col < $moduleCount; $col++)
  341. {
  342. for($row = 0; $row < $moduleCount; $row++)
  343. {
  344. if($qrCode->isDark($row, $col))
  345. {
  346. $darkCount++;
  347. }
  348. }
  349. }
  350. $ratio = abs(100 * $darkCount / $moduleCount / $moduleCount - 50) / 5;
  351. $lostPoint += $ratio * 10;
  352. return $lostPoint;
  353. }
  354. /**
  355. * Initialize the pattern position table
  356. */
  357. private function init()
  358. {
  359. $this->PATTERN_POSITION_TABLE = array();
  360. $this->addPattern(array());
  361. $this->addPattern(array(6, 18));
  362. $this->addPattern(array(6, 22));
  363. $this->addPattern(array(6, 26));
  364. $this->addPattern(array(6, 30));
  365. $this->addPattern(array(6, 34));
  366. $this->addPattern(array(6, 22, 38));
  367. $this->addPattern(array(6, 24, 42));
  368. $this->addPattern(array(6, 26, 46));
  369. $this->addPattern(array(6, 28, 50));
  370. $this->addPattern(array(6, 30, 54));
  371. $this->addPattern(array(6, 32, 58));
  372. $this->addPattern(array(6, 34, 62));
  373. $this->addPattern(array(6, 26, 46, 66));
  374. $this->addPattern(array(6, 26, 48, 70));
  375. $this->addPattern(array(6, 26, 50, 74));
  376. $this->addPattern(array(6, 30, 54, 78));
  377. $this->addPattern(array(6, 30, 56, 82));
  378. $this->addPattern(array(6, 30, 58, 86));
  379. $this->addPattern(array(6, 34, 62, 90));
  380. $this->addPattern(array(6, 28, 50, 72, 94));
  381. $this->addPattern(array(6, 26, 50, 74, 98));
  382. $this->addPattern(array(6, 30, 54, 78, 102));
  383. $this->addPattern(array(6, 28, 54, 80, 106));
  384. $this->addPattern(array(6, 32, 58, 84, 110));
  385. $this->addPattern(array(6, 30, 58, 86, 114));
  386. $this->addPattern(array(6, 34, 62, 90, 118));
  387. $this->addPattern(array(6, 26, 50, 74, 98, 122));
  388. $this->addPattern(array(6, 30, 54, 78, 102, 126));
  389. $this->addPattern(array(6, 26, 52, 78, 104, 130));
  390. $this->addPattern(array(6, 30, 56, 82, 108, 134));
  391. $this->addPattern(array(6, 34, 60, 86, 112, 138));
  392. $this->addPattern(array(6, 30, 58, 86, 114, 142));
  393. $this->addPattern(array(6, 34, 62, 90, 118, 146));
  394. $this->addPattern(array(6, 30, 54, 78, 102, 126, 150));
  395. $this->addPattern(array(6, 24, 50, 76, 102, 128, 154));
  396. $this->addPattern(array(6, 28, 54, 80, 106, 132, 158));
  397. $this->addPattern(array(6, 32, 58, 84, 110, 136, 162));
  398. $this->addPattern(array(6, 26, 54, 82, 110, 138, 166));
  399. $this->addPattern(array(6, 30, 58, 86, 114, 142, 170));
  400. }
  401. /**
  402. * Add a pattern to the pattern position table
  403. *
  404. * @param array $d
  405. */
  406. private function addPattern($d)
  407. {
  408. array_push($this->PATTERN_POSITION_TABLE, $d);
  409. }
  410. /**
  411. * Create an empty array of n elements
  412. *
  413. * All elements are uninitialed.
  414. *
  415. * @param int $numElements
  416. * @return array
  417. */
  418. public function createEmptyArray($numElements)
  419. {
  420. return array_fill(0, $numElements, null);
  421. }
  422. }