@@ -46,12 +46,14 @@ final class Binarizer{
46
46
private const LUMINANCE_BUCKETS = 32 ;
47
47
48
48
private LuminanceSourceInterface $ source ;
49
+ private array $ luminances ;
49
50
50
51
/**
51
52
*
52
53
*/
53
54
public function __construct (LuminanceSourceInterface $ source ){
54
- $ this ->source = $ source ;
55
+ $ this ->source = $ source ;
56
+ $ this ->luminances = $ this ->source ->getLuminances ();
55
57
}
56
58
57
59
/**
@@ -182,14 +184,13 @@ private function getHistogramBlackMatrix(int $width, int $height):BitMatrix{
182
184
// We delay reading the entire image luminance until the black point estimation succeeds.
183
185
// Although we end up reading four rows twice, it is consistent with our motto of
184
186
// "fail quickly" which is necessary for continuous scanning.
185
- $ localLuminances = $ this ->source ->getMatrix ();
186
- $ matrix = new BitMatrix (max ($ width , $ height ));
187
+ $ matrix = new BitMatrix (max ($ width , $ height ));
187
188
188
189
for ($ y = 0 ; $ y < $ height ; $ y ++){
189
190
$ offset = $ y * $ width ;
190
191
191
192
for ($ x = 0 ; $ x < $ width ; $ x ++){
192
- $ matrix ->set ($ x , $ y , (($ localLuminances [$ offset + $ x ] & 0xff ) < $ blackPoint ), QRMatrix::M_DATA );
193
+ $ matrix ->set ($ x , $ y , (($ this -> luminances [$ offset + $ x ] & 0xff ) < $ blackPoint ), QRMatrix::M_DATA );
193
194
}
194
195
}
195
196
@@ -202,7 +203,7 @@ private function getHistogramBlackMatrix(int $width, int $height):BitMatrix{
202
203
*
203
204
* @see http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0
204
205
*/
205
- private function calculateBlackPoints (array $ luminances , int $ subWidth , int $ subHeight , int $ width , int $ height ):array {
206
+ private function calculateBlackPoints (int $ subWidth , int $ subHeight , int $ width , int $ height ):array {
206
207
$ blackPoints = array_fill (0 , $ subHeight , 0 );
207
208
208
209
foreach ($ blackPoints as $ key => $ point ){
@@ -232,7 +233,7 @@ private function calculateBlackPoints(array $luminances, int $subWidth, int $sub
232
233
for ($ yy = 0 , $ offset = $ yoffset * $ width + $ xoffset ; $ yy < self ::BLOCK_SIZE ; $ yy ++, $ offset += $ width ){
233
234
234
235
for ($ xx = 0 ; $ xx < self ::BLOCK_SIZE ; $ xx ++){
235
- $ pixel = (int )($ luminances [(int )($ offset + $ xx )]) & 0xff ;
236
+ $ pixel = (int )($ this -> luminances [(int )($ offset + $ xx )]) & 0xff ;
236
237
$ sum += $ pixel ;
237
238
// still looking for good contrast
238
239
if ($ pixel < $ min ){
@@ -249,7 +250,7 @@ private function calculateBlackPoints(array $luminances, int $subWidth, int $sub
249
250
// finish the rest of the rows quickly
250
251
for ($ yy ++, $ offset += $ width ; $ yy < self ::BLOCK_SIZE ; $ yy ++, $ offset += $ width ){
251
252
for ($ xx = 0 ; $ xx < self ::BLOCK_SIZE ; $ xx ++){
252
- $ sum += (int )($ luminances [(int )($ offset + $ xx )]) & 0xff ;
253
+ $ sum += (int )($ this -> luminances [(int )($ offset + $ xx )]) & 0xff ;
253
254
}
254
255
}
255
256
}
@@ -275,7 +276,9 @@ private function calculateBlackPoints(array $luminances, int $subWidth, int $sub
275
276
// the boundaries is used for the interior.
276
277
277
278
// The (min < bp) is arbitrary but works better than other heuristics that were tried.
278
- $ averageNeighborBlackPoint = (int )(($ blackPoints [$ y - 1 ][$ x ] + (2 * $ blackPoints [$ y ][$ x - 1 ]) + $ blackPoints [$ y - 1 ][$ x - 1 ]) / 4 );
279
+ $ averageNeighborBlackPoint = (int )(
280
+ ($ blackPoints [$ y - 1 ][$ x ] + (2 * $ blackPoints [$ y ][$ x - 1 ]) + $ blackPoints [$ y - 1 ][$ x - 1 ]) / 4
281
+ );
279
282
280
283
if ($ min < $ averageNeighborBlackPoint ){
281
284
$ average = $ averageNeighborBlackPoint ;
@@ -295,15 +298,9 @@ private function calculateBlackPoints(array $luminances, int $subWidth, int $sub
295
298
* of the blocks around it. Also handles the corner cases (fractional blocks are computed based
296
299
* on the last pixels in the row/column which are also used in the previous block).
297
300
*/
298
- private function calculateThresholdForBlock (
299
- int $ subWidth ,
300
- int $ subHeight ,
301
- int $ width ,
302
- int $ height
303
- ):BitMatrix {
301
+ private function calculateThresholdForBlock (int $ subWidth , int $ subHeight , int $ width , int $ height ):BitMatrix {
304
302
$ matrix = new BitMatrix (max ($ width , $ height ));
305
- $ luminances = $ this ->source ->getMatrix ();
306
- $ blackPoints = $ this ->calculateBlackPoints ($ luminances , $ subWidth , $ subHeight , $ width , $ height );
303
+ $ blackPoints = $ this ->calculateBlackPoints ($ subWidth , $ subHeight , $ width , $ height );
307
304
308
305
for ($ y = 0 ; $ y < $ subHeight ; $ y ++){
309
306
$ yoffset = ($ y << self ::BLOCK_SIZE_POWER );
@@ -326,8 +323,8 @@ private function calculateThresholdForBlock(
326
323
$ sum = 0 ;
327
324
328
325
for ($ z = -2 ; $ z <= 2 ; $ z ++){
329
- $ blackRow = $ blackPoints [$ top + $ z ];
330
- $ sum += $ blackRow [$ left - 2 ] + $ blackRow [$ left - 1 ] + $ blackRow [$ left ] + $ blackRow [$ left + 1 ] + $ blackRow [$ left + 2 ];
326
+ $ br = $ blackPoints [$ top + $ z ];
327
+ $ sum += $ br [$ left - 2 ] + $ br [$ left - 1 ] + $ br [$ left ] + $ br [$ left + 1 ] + $ br [$ left + 2 ];
331
328
}
332
329
333
330
$ average = (int )($ sum / 25 );
@@ -336,7 +333,9 @@ private function calculateThresholdForBlock(
336
333
for ($ j = 0 , $ o = $ yoffset * $ width + $ xoffset ; $ j < self ::BLOCK_SIZE ; $ j ++, $ o += $ width ){
337
334
for ($ i = 0 ; $ i < self ::BLOCK_SIZE ; $ i ++){
338
335
// Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0.
339
- $ matrix ->set ($ xoffset + $ i , $ yoffset + $ j , (((int )($ luminances [$ o + $ i ]) & 0xff ) <= $ average ), QRMatrix::M_DATA );
336
+ $ v = (((int )($ this ->luminances [$ o + $ i ]) & 0xff ) <= $ average );
337
+
338
+ $ matrix ->set ($ xoffset + $ i , $ yoffset + $ j , $ v , QRMatrix::M_DATA );
340
339
}
341
340
}
342
341
}
@@ -345,6 +344,9 @@ private function calculateThresholdForBlock(
345
344
return $ matrix ;
346
345
}
347
346
347
+ /**
348
+ * @noinspection PhpSameParameterValueInspection
349
+ */
348
350
private function cap (int $ value , int $ min , int $ max ):int {
349
351
350
352
if ($ value < $ min ){
0 commit comments