PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/app/lib/core/Parsers/phpqrcode/qrmask.php

https://bitbucket.org/Sinfin/pawtucket
PHP | 328 lines | 234 code | 57 blank | 37 comment | 68 complexity | 12ce875d67e242532e2b0da9f0a334ee MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0
  1. <?php
  2. /*
  3. * PHP QR Code encoder
  4. *
  5. * Masking
  6. *
  7. * Based on libqrencode C library distributed under LGPL 2.1
  8. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  9. *
  10. * PHP QR Code is distributed under LGPL 3
  11. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  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 3 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. define('N1', 3);
  28. define('N2', 3);
  29. define('N3', 40);
  30. define('N4', 10);
  31. class QRmask {
  32. public $runLength = array();
  33. //----------------------------------------------------------------------
  34. public function __construct()
  35. {
  36. $this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0);
  37. }
  38. //----------------------------------------------------------------------
  39. public function writeFormatInformation($width, &$frame, $mask, $level)
  40. {
  41. $blacks = 0;
  42. $format = QRspec::getFormatInfo($mask, $level);
  43. for($i=0; $i<8; $i++) {
  44. if($format & 1) {
  45. $blacks += 2;
  46. $v = 0x85;
  47. } else {
  48. $v = 0x84;
  49. }
  50. $frame[8][$width - 1 - $i] = chr($v);
  51. if($i < 6) {
  52. $frame[$i][8] = chr($v);
  53. } else {
  54. $frame[$i + 1][8] = chr($v);
  55. }
  56. $format = $format >> 1;
  57. }
  58. for($i=0; $i<7; $i++) {
  59. if($format & 1) {
  60. $blacks += 2;
  61. $v = 0x85;
  62. } else {
  63. $v = 0x84;
  64. }
  65. $frame[$width - 7 + $i][8] = chr($v);
  66. if($i == 0) {
  67. $frame[8][7] = chr($v);
  68. } else {
  69. $frame[8][6 - $i] = chr($v);
  70. }
  71. $format = $format >> 1;
  72. }
  73. return $blacks;
  74. }
  75. //----------------------------------------------------------------------
  76. public function mask0($x, $y) { return ($x+$y)&1; }
  77. public function mask1($x, $y) { return ($y&1); }
  78. public function mask2($x, $y) { return ($x%3); }
  79. public function mask3($x, $y) { return ($x+$y)%3; }
  80. public function mask4($x, $y) { return (((int)($y/2))+((int)($x/3)))&1; }
  81. public function mask5($x, $y) { return (($x*$y)&1)+($x*$y)%3; }
  82. public function mask6($x, $y) { return ((($x*$y)&1)+($x*$y)%3)&1; }
  83. public function mask7($x, $y) { return ((($x*$y)%3)+(($x+$y)&1))&1; }
  84. //----------------------------------------------------------------------
  85. private function generateMaskNo($maskNo, $width, $frame)
  86. {
  87. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  88. for($y=0; $y<$width; $y++) {
  89. for($x=0; $x<$width; $x++) {
  90. if(ord($frame[$y][$x]) & 0x80) {
  91. $bitMask[$y][$x] = 0;
  92. } else {
  93. $maskFunc = call_user_func(array($this, 'mask'.$maskNo), $x, $y);
  94. $bitMask[$y][$x] = ($maskFunc == 0)?1:0;
  95. }
  96. }
  97. }
  98. return $bitMask;
  99. }
  100. //----------------------------------------------------------------------
  101. public static function serial($bitFrame)
  102. {
  103. $codeArr = array();
  104. foreach ($bitFrame as $line)
  105. $codeArr[] = join('', $line);
  106. return gzcompress(join("\n", $codeArr), 9);
  107. }
  108. //----------------------------------------------------------------------
  109. public static function unserial($code)
  110. {
  111. $codeArr = array();
  112. $codeLines = explode("\n", gzuncompress($code));
  113. foreach ($codeLines as $line)
  114. $codeArr[] = str_split($line);
  115. return $codeArr;
  116. }
  117. //----------------------------------------------------------------------
  118. public function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false)
  119. {
  120. $b = 0;
  121. $bitMask = array();
  122. $fileName = QR_CACHE_DIR.'mask_'.$maskNo.DIRECTORY_SEPARATOR.'mask_'.$width.'_'.$maskNo.'.dat';
  123. if (QR_CACHEABLE) {
  124. if (file_exists($fileName)) {
  125. $bitMask = self::unserial(file_get_contents($fileName));
  126. } else {
  127. $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
  128. if (!file_exists(QR_CACHE_DIR.'mask_'.$maskNo))
  129. mkdir(QR_CACHE_DIR.'mask_'.$maskNo);
  130. file_put_contents($fileName, self::serial($bitMask));
  131. }
  132. } else {
  133. $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
  134. }
  135. if ($maskGenOnly)
  136. return;
  137. $d = $s;
  138. for($y=0; $y<$width; $y++) {
  139. for($x=0; $x<$width; $x++) {
  140. if($bitMask[$y][$x] == 1) {
  141. $d[$y][$x] = chr(ord($s[$y][$x]) ^ (int)$bitMask[$y][$x]);
  142. }
  143. $b += (int)(ord($d[$y][$x]) & 1);
  144. }
  145. }
  146. return $b;
  147. }
  148. //----------------------------------------------------------------------
  149. public function makeMask($width, $frame, $maskNo, $level)
  150. {
  151. $masked = array_fill(0, $width, str_repeat("\0", $width));
  152. $this->makeMaskNo($maskNo, $width, $frame, $masked);
  153. $this->writeFormatInformation($width, $masked, $maskNo, $level);
  154. return $masked;
  155. }
  156. //----------------------------------------------------------------------
  157. public function calcN1N3($length)
  158. {
  159. $demerit = 0;
  160. for($i=0; $i<$length; $i++) {
  161. if($this->runLength[$i] >= 5) {
  162. $demerit += (N1 + ($this->runLength[$i] - 5));
  163. }
  164. if($i & 1) {
  165. if(($i >= 3) && ($i < ($length-2)) && ($this->runLength[$i] % 3 == 0)) {
  166. $fact = (int)($this->runLength[$i] / 3);
  167. if(($this->runLength[$i-2] == $fact) &&
  168. ($this->runLength[$i-1] == $fact) &&
  169. ($this->runLength[$i+1] == $fact) &&
  170. ($this->runLength[$i+2] == $fact)) {
  171. if(($this->runLength[$i-3] < 0) || ($this->runLength[$i-3] >= (4 * $fact))) {
  172. $demerit += N3;
  173. } else if((($i+3) >= $length) || ($this->runLength[$i+3] >= (4 * $fact))) {
  174. $demerit += N3;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. return $demerit;
  181. }
  182. //----------------------------------------------------------------------
  183. public function evaluateSymbol($width, $frame)
  184. {
  185. $head = 0;
  186. $demerit = 0;
  187. for($y=0; $y<$width; $y++) {
  188. $head = 0;
  189. $this->runLength[0] = 1;
  190. $frameY = $frame[$y];
  191. if ($y>0)
  192. $frameYM = $frame[$y-1];
  193. for($x=0; $x<$width; $x++) {
  194. if(($x > 0) && ($y > 0)) {
  195. $b22 = ord($frameY[$x]) & ord($frameY[$x-1]) & ord($frameYM[$x]) & ord($frameYM[$x-1]);
  196. $w22 = ord($frameY[$x]) | ord($frameY[$x-1]) | ord($frameYM[$x]) | ord($frameYM[$x-1]);
  197. if(($b22 | ($w22 ^ 1))&1) {
  198. $demerit += N2;
  199. }
  200. }
  201. if(($x == 0) && (ord($frameY[$x]) & 1)) {
  202. $this->runLength[0] = -1;
  203. $head = 1;
  204. $this->runLength[$head] = 1;
  205. } else if($x > 0) {
  206. if((ord($frameY[$x]) ^ ord($frameY[$x-1])) & 1) {
  207. $head++;
  208. $this->runLength[$head] = 1;
  209. } else {
  210. $this->runLength[$head]++;
  211. }
  212. }
  213. }
  214. $demerit += $this->calcN1N3($head+1);
  215. }
  216. for($x=0; $x<$width; $x++) {
  217. $head = 0;
  218. $this->runLength[0] = 1;
  219. for($y=0; $y<$width; $y++) {
  220. if($y == 0 && (ord($frame[$y][$x]) & 1)) {
  221. $this->runLength[0] = -1;
  222. $head = 1;
  223. $this->runLength[$head] = 1;
  224. } else if($y > 0) {
  225. if((ord($frame[$y][$x]) ^ ord($frame[$y-1][$x])) & 1) {
  226. $head++;
  227. $this->runLength[$head] = 1;
  228. } else {
  229. $this->runLength[$head]++;
  230. }
  231. }
  232. }
  233. $demerit += $this->calcN1N3($head+1);
  234. }
  235. return $demerit;
  236. }
  237. //----------------------------------------------------------------------
  238. public function mask($width, $frame, $level)
  239. {
  240. $minDemerit = PHP_INT_MAX;
  241. $bestMaskNum = 0;
  242. $bestMask = array();
  243. $checked_masks = array(0,1,2,3,4,5,6,7);
  244. if (QR_FIND_FROM_RANDOM !== false) {
  245. $howManuOut = 8-(QR_FIND_FROM_RANDOM % 9);
  246. for ($i = 0; $i < $howManuOut; $i++) {
  247. $remPos = rand (0, count($checked_masks)-1);
  248. unset($checked_masks[$remPos]);
  249. $checked_masks = array_values($checked_masks);
  250. }
  251. }
  252. $bestMask = $frame;
  253. foreach($checked_masks as $i) {
  254. $mask = array_fill(0, $width, str_repeat("\0", $width));
  255. $demerit = 0;
  256. $blacks = 0;
  257. $blacks = $this->makeMaskNo($i, $width, $frame, $mask);
  258. $blacks += $this->writeFormatInformation($width, $mask, $i, $level);
  259. $blacks = (int)(100 * $blacks / ($width * $width));
  260. $demerit = (int)((int)(abs($blacks - 50) / 5) * N4);
  261. $demerit += $this->evaluateSymbol($width, $mask);
  262. if($demerit < $minDemerit) {
  263. $minDemerit = $demerit;
  264. $bestMask = $mask;
  265. $bestMaskNum = $i;
  266. }
  267. }
  268. return $bestMask;
  269. }
  270. //----------------------------------------------------------------------
  271. }