12
12
13
13
namespace chillerlan \QRCode \Data ;
14
14
15
- use function abs , call_user_func ;
15
+ use function abs , call_user_func_array ;
16
16
17
17
/**
18
18
* The sole purpose of this class is to receive a QRMatrix object and run the pattern tests on it.
@@ -58,7 +58,7 @@ public function testPattern():int{
58
58
$ penalty = 0 ;
59
59
60
60
for ($ level = 1 ; $ level <= 4 ; $ level ++){
61
- $ penalty += call_user_func ([$ this , 'testLevel ' .$ level ]);
61
+ $ penalty += call_user_func_array ([$ this , 'testLevel ' .$ level], [ $ this -> matrix -> matrix ( true ) ]);
62
62
}
63
63
64
64
return (int )$ penalty ;
@@ -67,12 +67,12 @@ public function testPattern():int{
67
67
/**
68
68
* Checks for each group of five or more same-colored modules in a row (or column)
69
69
*
70
- * @return float
70
+ * @return int
71
71
*/
72
- protected function testLevel1 (): float {
72
+ protected function testLevel1 (array $ m ): int {
73
73
$ penalty = 0 ;
74
74
75
- foreach ($ this -> matrix -> matrix () as $ y => $ row ){
75
+ foreach ($ m as $ y => $ row ){
76
76
foreach ($ row as $ x => $ val ){
77
77
$ count = 0 ;
78
78
@@ -84,11 +84,11 @@ protected function testLevel1():float{
84
84
85
85
for ($ rx = -1 ; $ rx <= 1 ; $ rx ++){
86
86
87
- if (($ ry === 0 && $ rx === 0 ) || ($ x + $ rx < 0 || $ this ->moduleCount <= $ x + $ rx )){
87
+ if (($ ry === 0 && $ rx === 0 ) || (( $ x + $ rx) < 0 || $ this ->moduleCount <= ( $ x + $ rx) )){
88
88
continue ;
89
89
}
90
90
91
- if ($ this -> matrix -> check ( $ x + $ rx , $ y + $ ry ) === ( $ val >> 8 > 0 ) ){
91
+ if ($ m [ $ y + $ ry ][ $ x + $ rx ] === $ val ){
92
92
$ count ++;
93
93
}
94
94
@@ -108,107 +108,90 @@ protected function testLevel1():float{
108
108
/**
109
109
* Checks for each 2x2 area of same-colored modules in the matrix
110
110
*
111
- * @return float
111
+ * @return int
112
112
*/
113
- protected function testLevel2 (): float {
113
+ protected function testLevel2 (array $ m ): int {
114
114
$ penalty = 0 ;
115
115
116
- foreach ($ this -> matrix -> matrix () as $ y => $ row ){
116
+ foreach ($ m as $ y => $ row ){
117
117
118
- if ($ y > $ this ->moduleCount - 2 ){
118
+ if ($ y > ( $ this ->moduleCount - 2 ) ){
119
119
break ;
120
120
}
121
121
122
122
foreach ($ row as $ x => $ val ){
123
123
124
- if ($ x > $ this ->moduleCount - 2 ){
124
+ if ($ x > ( $ this ->moduleCount - 2 ) ){
125
125
break ;
126
126
}
127
127
128
- $ count = 0 ;
129
-
130
- if ($ val >> 8 > 0 ){
131
- $ count ++;
132
- }
133
-
134
- if ($ this ->matrix ->check ($ y , $ x + 1 )){
135
- $ count ++;
136
- }
137
-
138
- if ($ this ->matrix ->check ($ y + 1 , $ x )){
139
- $ count ++;
140
- }
141
-
142
- if ($ this ->matrix ->check ($ y + 1 , $ x + 1 )){
143
- $ count ++;
128
+ if (
129
+ $ val === $ m [$ y ][$ x + 1 ]
130
+ && $ val === $ m [$ y + 1 ][$ x ]
131
+ && $ val === $ m [$ y + 1 ][$ x + 1 ]
132
+ ){
133
+ $ penalty ++;
144
134
}
145
-
146
- if ($ count === 0 || $ count === 4 ){
147
- $ penalty += 3 ;
148
- }
149
-
150
135
}
151
136
}
152
137
153
- return $ penalty ;
138
+ return 3 * $ penalty ;
154
139
}
155
140
156
141
/**
157
- * Checks if there are patterns that look similar to the finder patterns
142
+ * Checks if there are patterns that look similar to the finder patterns (1:1:3:1:1 ratio)
158
143
*
159
- * @return float
144
+ * @return int
160
145
*/
161
- protected function testLevel3 (): float {
162
- $ penalty = 0 ;
146
+ protected function testLevel3 (array $ m ): int {
147
+ $ penalties = 0 ;
163
148
164
- foreach ($ this -> matrix -> matrix () as $ y => $ row ){
149
+ foreach ($ m as $ y => $ row ){
165
150
foreach ($ row as $ x => $ val ){
166
151
167
- if ($ x <= $ this ->moduleCount - 7 ){
168
- if (
169
- $ this ->matrix ->check ($ x , $ y )
170
- && !$ this ->matrix ->check ($ x + 1 , $ y )
171
- && $ this ->matrix ->check ($ x + 2 , $ y )
172
- && $ this ->matrix ->check ($ x + 3 , $ y )
173
- && $ this ->matrix ->check ($ x + 4 , $ y )
174
- && !$ this ->matrix ->check ($ x + 5 , $ y )
175
- && $ this ->matrix ->check ($ x + 6 , $ y )
176
- ){
177
- $ penalty += 40 ;
178
- }
152
+ if (
153
+ ($ x + 6 ) < $ this ->moduleCount
154
+ && $ val
155
+ && !$ m [$ y ][$ x + 1 ]
156
+ && $ m [$ y ][$ x + 2 ]
157
+ && $ m [$ y ][$ x + 3 ]
158
+ && $ m [$ y ][$ x + 4 ]
159
+ && !$ m [$ y ][$ x + 5 ]
160
+ && $ m [$ y ][$ x + 6 ]
161
+ ){
162
+ $ penalties ++;
179
163
}
180
164
181
- if ($ y <= $ this ->moduleCount - 7 ){
182
- if (
183
- $ this ->matrix ->check ($ x , $ y )
184
- && !$ this ->matrix ->check ($ x , $ y + 1 )
185
- && $ this ->matrix ->check ($ x , $ y + 2 )
186
- && $ this ->matrix ->check ($ x , $ y + 3 )
187
- && $ this ->matrix ->check ($ x , $ y + 4 )
188
- && !$ this ->matrix ->check ($ x , $ y + 5 )
189
- && $ this ->matrix ->check ($ x , $ y + 6 )
190
- ){
191
- $ penalty += 40 ;
192
- }
165
+ if (
166
+ ($ y + 6 ) < $ this ->moduleCount
167
+ && $ val
168
+ && !$ m [$ y + 1 ][$ x ]
169
+ && $ m [$ y + 2 ][$ x ]
170
+ && $ m [$ y + 3 ][$ x ]
171
+ && $ m [$ y + 4 ][$ x ]
172
+ && !$ m [$ y + 5 ][$ x ]
173
+ && $ m [$ y + 6 ][$ x ]
174
+ ){
175
+ $ penalties ++;
193
176
}
194
177
195
178
}
196
179
}
197
180
198
- return $ penalty ;
181
+ return $ penalties * 40 ;
199
182
}
200
183
201
184
/**
202
185
* Checks if more than half of the modules are dark or light, with a larger penalty for a larger difference
203
186
*
204
187
* @return float
205
188
*/
206
- protected function testLevel4 ():float {
189
+ protected function testLevel4 (array $ m ):float {
207
190
$ count = 0 ;
208
191
209
- foreach ($ this -> matrix -> matrix () as $ y => $ row ){
192
+ foreach ($ m as $ y => $ row ){
210
193
foreach ($ row as $ x => $ val ){
211
- if ($ val >> 8 > 0 ){
194
+ if ($ val ){
212
195
$ count ++;
213
196
}
214
197
}
0 commit comments