Skip to content

Commit 0ec485a

Browse files
committed
:octocat: fix no.2 for #45
1 parent d07b294 commit 0ec485a

File tree

2 files changed

+34
-49
lines changed

2 files changed

+34
-49
lines changed

src/Data/MaskPatternTester.php

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testPattern(int $pattern):int{
6666
$penalty = 0;
6767

6868
for($level = 1; $level <= 4; $level++){
69-
$penalty += call_user_func_array([$this, 'testLevel'.$level], [$matrix, $matrix->size()]);
69+
$penalty += call_user_func_array([$this, 'testLevel'.$level], [$matrix->matrix(true), $matrix->size()]);
7070
}
7171

7272
return (int)$penalty;
@@ -75,10 +75,10 @@ public function testPattern(int $pattern):int{
7575
/**
7676
* Checks for each group of five or more same-colored modules in a row (or column)
7777
*/
78-
protected function testLevel1(QRMatrix $m, int $size):float{
78+
protected function testLevel1(array $m, int $size):int{
7979
$penalty = 0;
8080

81-
foreach($m->matrix() as $y => $row){
81+
foreach($m as $y => $row){
8282
foreach($row as $x => $val){
8383
$count = 0;
8484

@@ -94,7 +94,7 @@ protected function testLevel1(QRMatrix $m, int $size):float{
9494
continue;
9595
}
9696

97-
if($m->check($x + $rx, $y + $ry) === (($val >> 8) > 0)){
97+
if($m[$y + $ry][$x + $rx] === $val){
9898
$count++;
9999
}
100100

@@ -114,10 +114,10 @@ protected function testLevel1(QRMatrix $m, int $size):float{
114114
/**
115115
* Checks for each 2x2 area of same-colored modules in the matrix
116116
*/
117-
protected function testLevel2(QRMatrix $m, int $size):float{
117+
protected function testLevel2(array $m, int $size):int{
118118
$penalty = 0;
119119

120-
foreach($m->matrix() as $y => $row){
120+
foreach($m as $y => $row){
121121

122122
if($y > $size - 2){
123123
break;
@@ -129,65 +129,50 @@ protected function testLevel2(QRMatrix $m, int $size):float{
129129
break;
130130
}
131131

132-
$count = 0;
133-
134-
if($val >> 8 > 0){
135-
$count++;
136-
}
137-
138-
if($m->check($y, $x + 1)){
139-
$count++;
140-
}
141-
142-
if($m->check($y + 1, $x)){
143-
$count++;
144-
}
145-
146-
if($m->check($y + 1, $x + 1)){
147-
$count++;
148-
}
149-
150-
if($count === 0 || $count === 4){
151-
$penalty += 3;
132+
if(
133+
$val === $m[$y][$x + 1]
134+
&& $val === $m[$y + 1][$x]
135+
&& $val === $m[$y + 1][$x + 1]
136+
){
137+
$penalty++;
152138
}
153-
154139
}
155140
}
156141

157-
return $penalty;
142+
return 3 * $penalty;
158143
}
159144

160145
/**
161146
* Checks if there are patterns that look similar to the finder patterns (1:1:3:1:1 ratio)
162147
*/
163-
protected function testLevel3(QRMatrix $m, int $size):float{
148+
protected function testLevel3(array $m, int $size):int{
164149
$penalties = 0;
165150

166-
foreach($m->matrix() as $y => $row){
151+
foreach($m as $y => $row){
167152
foreach($row as $x => $val){
168153

169154
if(
170155
$x + 6 < $size
171-
&& $m->check($x , $y)
172-
&& !$m->check($x + 1, $y)
173-
&& $m->check($x + 2, $y)
174-
&& $m->check($x + 3, $y)
175-
&& $m->check($x + 4, $y)
176-
&& !$m->check($x + 5, $y)
177-
&& $m->check($x + 6, $y)
156+
&& $m[$y][$x]
157+
&& !$m[$y][$x + 1]
158+
&& $m[$y][$x + 2]
159+
&& $m[$y][$x + 3]
160+
&& $m[$y][$x + 4]
161+
&& !$m[$y][$x + 5]
162+
&& $m[$y][$x + 6]
178163
){
179164
$penalties++;
180165
}
181166

182167
if(
183168
$y + 6 < $size
184-
&& $m->check($x, $y )
185-
&& !$m->check($x, $y + 1)
186-
&& $m->check($x, $y + 2)
187-
&& $m->check($x, $y + 3)
188-
&& $m->check($x, $y + 4)
189-
&& !$m->check($x, $y + 5)
190-
&& $m->check($x, $y + 6)
169+
&& $m[$y][$x]
170+
&& !$m[$y + 1][$x]
171+
&& $m[$y + 2][$x]
172+
&& $m[$y + 3][$x]
173+
&& $m[$y + 4][$x]
174+
&& !$m[$y + 5][$x]
175+
&& $m[$y + 6][$x]
191176
){
192177
$penalties++;
193178
}
@@ -201,12 +186,12 @@ protected function testLevel3(QRMatrix $m, int $size):float{
201186
/**
202187
* Checks if more than half of the modules are dark or light, with a larger penalty for a larger difference
203188
*/
204-
protected function testLevel4(QRMatrix $m, int $size):float{
189+
protected function testLevel4(array $m, int $size):float{
205190
$count = 0;
206191

207-
foreach($m->matrix() as $y => $row){
192+
foreach($m as $y => $row){
208193
foreach($row as $x => $val){
209-
if(($val >> 8) > 0){
194+
if($val){
210195
$count++;
211196
}
212197
}

tests/Data/MaskPatternTesterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class MaskPatternTesterTest extends TestCase{
2727
public function testMaskpattern():void{
2828
$dataInterface = new Byte(new QROptions(['version' => 10]), 'test');
2929

30-
$this::assertSame(4, (new MaskPatternTester($dataInterface))->getBestMaskPattern());
30+
$this::assertSame(3, (new MaskPatternTester($dataInterface))->getBestMaskPattern());
3131
}
3232

3333
/**
@@ -36,7 +36,7 @@ public function testMaskpattern():void{
3636
public function testMaskpatternID():void{
3737
$dataInterface = new Byte(new QROptions(['version' => 10]), 'test');
3838

39-
$this::assertSame(6178, (new MaskPatternTester($dataInterface))->testPattern(0));
39+
$this::assertSame(4243, (new MaskPatternTester($dataInterface))->testPattern(3));
4040
}
4141

4242
}

0 commit comments

Comments
 (0)