]>
Raphaƫl G. Git Repositories - cdn/blob - vendor/phpqrcode/qrmask.php
7 * Based on libqrencode C library distributed under LGPL 2.1
8 * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
10 * PHP QR Code is distributed under LGPL 3
11 * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
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.
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.
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
35 public $runLength = array();
37 //----------------------------------------------------------------------
38 public function __construct()
40 $this->runLength
= array_fill(0, QRSPEC_WIDTH_MAX +
1, 0);
43 //----------------------------------------------------------------------
44 public function writeFormatInformation($width, &$frame, $mask, $level)
47 $format = QRspec
::getFormatInfo($mask, $level);
49 for($i=0; $i<8; $i++
) {
57 $frame[8][$width - 1 - $i] = chr($v);
59 $frame[$i][8] = chr($v);
61 $frame[$i +
1][8] = chr($v);
63 $format = $format >> 1;
66 for($i=0; $i<7; $i++
) {
74 $frame[$width - 7 +
$i][8] = chr($v);
76 $frame[8][7] = chr($v);
78 $frame[8][6 - $i] = chr($v);
81 $format = $format >> 1;
87 //----------------------------------------------------------------------
88 public function mask0($x, $y) { return ($x+$y
)&1; }
89 public function mask1($x, $y) { return ($y
&1); }
90 public function mask2($x, $y) { return ($x%
3); }
91 public function mask3($x, $y) { return ($x+$y
)%
3; }
92 public function mask4($x, $y) { return (((int)($y
/2))+
((int)($x
/3)))&1; }
93 public function mask5($x, $y) { return (($x
*$y
)&1)+
($x
*$y
)%
3; }
94 public function mask6($x, $y) { return ((($x
*$y
)&1)+
($x
*$y
)%
3)&1; }
95 public function mask7($x, $y) { return ((($x
*$y
)%
3)+
(($x+$y
)&1))&1; }
97 //----------------------------------------------------------------------
98 private function generateMaskNo($maskNo, $width, $frame)
100 $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
102 for($y=0; $y<$width; $y++
) {
103 for($x=0; $x<$width; $x++
) {
104 if(ord($frame[$y][$x]) & 0x80) {
105 $bitMask[$y][$x] = 0;
107 $maskFunc = call_user_func(array($this, 'mask'.$maskNo), $x, $y);
108 $bitMask[$y][$x] = ($maskFunc == 0)?1:0;
117 //----------------------------------------------------------------------
118 public static function serial($bitFrame)
122 foreach ($bitFrame as $line)
123 $codeArr[] = join('', $line);
125 return gzcompress(join("\n", $codeArr), 9);
128 //----------------------------------------------------------------------
129 public static function unserial($code)
133 $codeLines = explode("\n", gzuncompress($code));
134 foreach ($codeLines as $line)
135 $codeArr[] = str_split($line);
140 //----------------------------------------------------------------------
141 public function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false)
146 $fileName = QR_CACHE_DIR
.'mask_'.$maskNo.DIRECTORY_SEPARATOR
.'mask_'.$width.'_'.$maskNo.'.dat';
149 if (file_exists($fileName)) {
150 $bitMask = self
::unserial(file_get_contents($fileName));
152 $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
153 if (!file_exists(QR_CACHE_DIR
.'mask_'.$maskNo))
154 mkdir(QR_CACHE_DIR
.'mask_'.$maskNo);
155 file_put_contents($fileName, self
::serial($bitMask));
158 $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
166 for($y=0; $y<$width; $y++
) {
167 for($x=0; $x<$width; $x++
) {
168 if($bitMask[$y][$x] == 1) {
169 $d[$y][$x] = chr(ord($s[$y][$x]) ^
(int)$bitMask[$y][$x]);
171 $b +
= (int)(ord($d[$y][$x]) & 1);
178 //----------------------------------------------------------------------
179 public function makeMask($width, $frame, $maskNo, $level)
181 $masked = array_fill(0, $width, str_repeat("\0", $width));
182 $this->makeMaskNo($maskNo, $width, $frame, $masked);
183 $this->writeFormatInformation($width, $masked, $maskNo, $level);
188 //----------------------------------------------------------------------
189 public function calcN1N3($length)
193 for($i=0; $i<$length; $i++
) {
195 if($this->runLength
[$i] >= 5) {
196 $demerit +
= (N1 +
($this->runLength
[$i] - 5));
199 if(($i >= 3) && ($i < ($length-2)) && ($this->runLength
[$i] %
3 == 0)) {
200 $fact = (int)($this->runLength
[$i] / 3);
201 if(($this->runLength
[$i-2] == $fact) &&
202 ($this->runLength
[$i-1] == $fact) &&
203 ($this->runLength
[$i+
1] == $fact) &&
204 ($this->runLength
[$i+
2] == $fact)) {
205 if(($this->runLength
[$i-3] < 0) || ($this->runLength
[$i-3] >= (4 * $fact))) {
207 } else if((($i+
3) >= $length) || ($this->runLength
[$i+
3] >= (4 * $fact))) {
217 //----------------------------------------------------------------------
218 public function evaluateSymbol($width, $frame)
223 for($y=0; $y<$width; $y++
) {
225 $this->runLength
[0] = 1;
227 $frameY = $frame[$y];
230 $frameYM = $frame[$y-1];
232 for($x=0; $x<$width; $x++
) {
233 if(($x > 0) && ($y > 0)) {
234 $b22 = ord($frameY[$x]) & ord($frameY[$x-1]) & ord($frameYM[$x]) & ord($frameYM[$x-1]);
235 $w22 = ord($frameY[$x]) | ord($frameY[$x-1]) | ord($frameYM[$x]) | ord($frameYM[$x-1]);
237 if(($b22 | ($w22 ^
1))&1) {
241 if(($x == 0) && (ord($frameY[$x]) & 1)) {
242 $this->runLength
[0] = -1;
244 $this->runLength
[$head] = 1;
246 if((ord($frameY[$x]) ^
ord($frameY[$x-1])) & 1) {
248 $this->runLength
[$head] = 1;
250 $this->runLength
[$head]++
;
255 $demerit +
= $this->calcN1N3($head+
1);
258 for($x=0; $x<$width; $x++
) {
260 $this->runLength
[0] = 1;
262 for($y=0; $y<$width; $y++
) {
263 if($y == 0 && (ord($frame[$y][$x]) & 1)) {
264 $this->runLength
[0] = -1;
266 $this->runLength
[$head] = 1;
268 if((ord($frame[$y][$x]) ^
ord($frame[$y-1][$x])) & 1) {
270 $this->runLength
[$head] = 1;
272 $this->runLength
[$head]++
;
277 $demerit +
= $this->calcN1N3($head+
1);
284 //----------------------------------------------------------------------
285 public function mask($width, $frame, $level)
287 $minDemerit = PHP_INT_MAX
;
291 $checked_masks = array(0,1,2,3,4,5,6,7);
293 if (QR_FIND_FROM_RANDOM
!== false) {
295 $howManuOut = 8-(QR_FIND_FROM_RANDOM %
9);
296 for ($i = 0; $i < $howManuOut; $i++
) {
297 $remPos = rand (0, count($checked_masks)-1);
298 unset($checked_masks[$remPos]);
299 $checked_masks = array_values($checked_masks);
306 foreach($checked_masks as $i) {
307 $mask = array_fill(0, $width, str_repeat("\0", $width));
311 $blacks = $this->makeMaskNo($i, $width, $frame, $mask);
312 $blacks +
= $this->writeFormatInformation($width, $mask, $i, $level);
313 $blacks = (int)(100 * $blacks / ($width * $width));
314 $demerit = (int)((int)(abs($blacks - 50) / 5) * N4
);
315 $demerit +
= $this->evaluateSymbol($width, $mask);
317 if($demerit < $minDemerit) {
318 $minDemerit = $demerit;
327 //----------------------------------------------------------------------