QRmask.php 11 KB

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