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