qrmask.php 7.1 KB

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