Passed
Push — main ( c24f0c...630561 )
by smiley
02:00
created

Binarizer::getHistogramBlackMatrix()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 33
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
nc 9
nop 2
dl 0
loc 33
rs 9.4555
c 1
b 0
f 0
1
<?php
2
/**
3
 * Class Binarizer
4
 *
5
 * @created      17.01.2021
6
 * @author       ZXing Authors
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2021 Smiley
9
 * @license      Apache-2.0
10
 */
11
12
namespace chillerlan\QRCode\Decoder;
13
14
use chillerlan\QRCode\Data\QRMatrix;
15
use function array_fill, count, max;
16
17
/**
18
 * This class implements a local thresholding algorithm, which while slower than the
19
 * GlobalHistogramBinarizer, is fairly efficient for what it does. It is designed for
20
 * high frequency images of barcodes with black data on white backgrounds. For this application,
21
 * it does a much better job than a global blackpoint with severe shadows and gradients.
22
 * However, it tends to produce artifacts on lower frequency images and is therefore not
23
 * a good general purpose binarizer for uses outside ZXing.
24
 *
25
 * This class extends GlobalHistogramBinarizer, using the older histogram approach for 1D readers,
26
 * and the newer local approach for 2D readers. 1D decoding using a per-row histogram is already
27
 * inherently local, and only fails for horizontal gradients. We can revisit that problem later,
28
 * but for now it was not a win to use local blocks for 1D.
29
 *
30
 * This Binarizer is the default for the unit tests and the recommended class for library users.
31
 *
32
 * @author [email protected] (Daniel Switkin)
33
 */
34
final class Binarizer{
35
36
	// This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels.
37
	// So this is the smallest dimension in each axis we can accept.
38
	private const BLOCK_SIZE_POWER  = 3;
39
	private const BLOCK_SIZE        = 8;  // ...0100...00
40
	private const BLOCK_SIZE_MASK   = 7;  // ...0011...11
41
	private const MINIMUM_DIMENSION = 40;
42
	private const MIN_DYNAMIC_RANGE = 24;
43
44
#	private const LUMINANCE_BITS    = 5;
45
	private const LUMINANCE_SHIFT   = 3;
46
	private const LUMINANCE_BUCKETS = 32;
47
48
	private LuminanceSourceInterface $source;
49
	private array                    $luminances;
50
51
	/**
52
	 *
53
	 */
54
	public function __construct(LuminanceSourceInterface $source){
55
		$this->source     = $source;
56
		$this->luminances = $this->source->getLuminances();
57
	}
58
59
	/**
60
	 * @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException
61
	 */
62
	private function estimateBlackPoint(array $buckets):int{
63
		// Find the tallest peak in the histogram.
64
		$numBuckets     = count($buckets);
65
		$maxBucketCount = 0;
66
		$firstPeak      = 0;
67
		$firstPeakSize  = 0;
68
69
		for($x = 0; $x < $numBuckets; $x++){
70
71
			if($buckets[$x] > $firstPeakSize){
72
				$firstPeak     = $x;
73
				$firstPeakSize = $buckets[$x];
74
			}
75
76
			if($buckets[$x] > $maxBucketCount){
77
				$maxBucketCount = $buckets[$x];
78
			}
79
		}
80
81
		// Find the second-tallest peak which is somewhat far from the tallest peak.
82
		$secondPeak      = 0;
83
		$secondPeakScore = 0;
84
85
		for($x = 0; $x < $numBuckets; $x++){
86
			$distanceToBiggest = ($x - $firstPeak);
87
			// Encourage more distant second peaks by multiplying by square of distance.
88
			$score = ($buckets[$x] * $distanceToBiggest * $distanceToBiggest);
89
90
			if($score > $secondPeakScore){
91
				$secondPeak      = $x;
92
				$secondPeakScore = $score;
93
			}
94
		}
95
96
		// Make sure firstPeak corresponds to the black peak.
97
		if($firstPeak > $secondPeak){
98
			$temp       = $firstPeak;
99
			$firstPeak  = $secondPeak;
100
			$secondPeak = $temp;
101
		}
102
103
		// If there is too little contrast in the image to pick a meaningful black point, throw rather
104
		// than waste time trying to decode the image, and risk false positives.
105
		if(($secondPeak - $firstPeak) <= ($numBuckets / 16)){
106
			throw new QRCodeDecoderException('no meaningful dark point found'); // @codeCoverageIgnore
107
		}
108
109
		// Find a valley between them that is low and closer to the white peak.
110
		$bestValley      = ($secondPeak - 1);
111
		$bestValleyScore = -1;
112
113
		for($x = ($secondPeak - 1); $x > $firstPeak; $x--){
114
			$fromFirst = ($x - $firstPeak);
115
			$score     = ($fromFirst * $fromFirst * ($secondPeak - $x) * ($maxBucketCount - $buckets[$x]));
116
117
			if($score > $bestValleyScore){
118
				$bestValley      = $x;
119
				$bestValleyScore = $score;
120
			}
121
		}
122
123
		return ($bestValley << self::LUMINANCE_SHIFT);
124
	}
125
126
	/**
127
	 * Calculates the final BitMatrix once for all requests. This could be called once from the
128
	 * constructor instead, but there are some advantages to doing it lazily, such as making
129
	 * profiling easier, and not doing heavy lifting when callers don't expect it.
130
	 *
131
	 * Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive
132
	 * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
133
	 * may not apply sharpening. Therefore, a row from this matrix may not be identical to one
134
	 * fetched using getBlackRow(), so don't mix and match between them.
135
	 *
136
	 * @return \chillerlan\QRCode\Decoder\BitMatrix The 2D array of bits for the image (true means black).
137
	 */
138
	public function getBlackMatrix():BitMatrix{
139
		$width  = $this->source->getWidth();
140
		$height = $this->source->getHeight();
141
142
		if($width >= self::MINIMUM_DIMENSION && $height >= self::MINIMUM_DIMENSION){
143
			$subWidth = ($width >> self::BLOCK_SIZE_POWER);
144
145
			if(($width & self::BLOCK_SIZE_MASK) !== 0){
146
				$subWidth++;
147
			}
148
149
			$subHeight = ($height >> self::BLOCK_SIZE_POWER);
150
151
			if(($height & self::BLOCK_SIZE_MASK) !== 0){
152
				$subHeight++;
153
			}
154
155
			return $this->calculateThresholdForBlock($subWidth, $subHeight, $width, $height);
156
		}
157
158
		// If the image is too small, fall back to the global histogram approach.
159
		return $this->getHistogramBlackMatrix($width, $height);
160
	}
161
162
	/**
163
	 *
164
	 */
165
	private function getHistogramBlackMatrix(int $width, int $height):BitMatrix{
166
167
		// Quickly calculates the histogram by sampling four rows from the image. This proved to be
168
		// more robust on the blackbox tests than sampling a diagonal as we used to do.
169
		$buckets = array_fill(0, self::LUMINANCE_BUCKETS, 0);
170
171
		for($y = 1; $y < 5; $y++){
172
			$row             = (int)($height * $y / 5);
173
			$localLuminances = $this->source->getRow($row);
174
			$right           = (int)(($width * 4) / 5);
175
176
			for($x = (int)($width / 5); $x < $right; $x++){
177
				$pixel = ($localLuminances[$x] & 0xff);
178
				$buckets[($pixel >> self::LUMINANCE_SHIFT)]++;
179
			}
180
		}
181
182
		$blackPoint = $this->estimateBlackPoint($buckets);
183
184
		// We delay reading the entire image luminance until the black point estimation succeeds.
185
		// Although we end up reading four rows twice, it is consistent with our motto of
186
		// "fail quickly" which is necessary for continuous scanning.
187
		$matrix = new BitMatrix(max($width, $height));
188
189
		for($y = 0; $y < $height; $y++){
190
			$offset = ($y * $width);
191
192
			for($x = 0; $x < $width; $x++){
193
				$matrix->set($x, $y, (($this->luminances[($offset + $x)] & 0xff) < $blackPoint), QRMatrix::M_DATA);
194
			}
195
		}
196
197
		return $matrix;
198
	}
199
200
	/**
201
	 * Calculates a single black point for each block of pixels and saves it away.
202
	 * See the following thread for a discussion of this algorithm:
203
	 *
204
	 * @see http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0
205
	 */
206
	private function calculateBlackPoints(int $subWidth, int $subHeight, int $width, int $height):array{
207
		$blackPoints = [];
208
209
		for($y = 0; $y < $subHeight; $y++){
210
			$yoffset         = ($y << self::BLOCK_SIZE_POWER);
211
			$maxYOffset      = ($height - self::BLOCK_SIZE);
212
			$blackPoints[$y] = [];
213
214
			if($yoffset > $maxYOffset){
215
				$yoffset = $maxYOffset;
216
			}
217
218
			for($x = 0; $x < $subWidth; $x++){
219
				$xoffset    = ($x << self::BLOCK_SIZE_POWER);
220
				$maxXOffset = ($width - self::BLOCK_SIZE);
221
222
				if($xoffset > $maxXOffset){
223
					$xoffset = $maxXOffset;
224
				}
225
226
				$sum = 0;
227
				$min = 255;
228
				$max = 0;
229
230
				for($yy = 0, $offset = ($yoffset * $width + $xoffset); $yy < self::BLOCK_SIZE; $yy++, $offset += $width){
231
232
					for($xx = 0; $xx < self::BLOCK_SIZE; $xx++){
233
						$pixel = ((int)($this->luminances[(int)($offset + $xx)]) & 0xff);
234
						$sum   += $pixel;
235
						// still looking for good contrast
236
						if($pixel < $min){
237
							$min = $pixel;
238
						}
239
240
						if($pixel > $max){
241
							$max = $pixel;
242
						}
243
					}
244
245
					// short-circuit min/max tests once dynamic range is met
246
					if(($max - $min) > self::MIN_DYNAMIC_RANGE){
247
						// finish the rest of the rows quickly
248
						for($yy++, $offset += $width; $yy < self::BLOCK_SIZE; $yy++, $offset += $width){
249
							for($xx = 0; $xx < self::BLOCK_SIZE; $xx++){
250
								$sum += ((int)($this->luminances[(int)($offset + $xx)]) & 0xff);
251
							}
252
						}
253
					}
254
				}
255
256
				// The default estimate is the average of the values in the block.
257
				$average = ($sum >> (self::BLOCK_SIZE_POWER * 2));
258
259
				if(($max - $min) <= self::MIN_DYNAMIC_RANGE){
260
					// If variation within the block is low, assume this is a block with only light or only
261
					// dark pixels. In that case we do not want to use the average, as it would divide this
262
					// low contrast area into black and white pixels, essentially creating data out of noise.
263
					//
264
					// The default assumption is that the block is light/background. Since no estimate for
265
					// the level of dark pixels exists locally, use half the min for the block.
266
					$average = (int)($min / 2);
267
268
					if($y > 0 && $x > 0){
269
						// Correct the "white background" assumption for blocks that have neighbors by comparing
270
						// the pixels in this block to the previously calculated black points. This is based on
271
						// the fact that dark barcode symbology is always surrounded by some amount of light
272
						// background for which reasonable black point estimates were made. The bp estimated at
273
						// the boundaries is used for the interior.
274
275
						// The (min < bp) is arbitrary but works better than other heuristics that were tried.
276
						$averageNeighborBlackPoint = (int)(
277
							($blackPoints[($y - 1)][$x] + (2 * $blackPoints[$y][($x - 1)]) + $blackPoints[($y - 1)][($x - 1)]) / 4
278
						);
279
280
						if($min < $averageNeighborBlackPoint){
281
							$average = $averageNeighborBlackPoint;
282
						}
283
					}
284
				}
285
286
				$blackPoints[$y][$x] = $average;
287
			}
288
		}
289
290
		return $blackPoints;
291
	}
292
293
	/**
294
	 * For each block in the image, calculate the average black point using a 5x5 grid
295
	 * of the surrounding blocks. Also handles the corner cases (fractional blocks are computed based
296
	 * on the last pixels in the row/column which are also used in the previous block).
297
	 */
298
	private function calculateThresholdForBlock(int $subWidth, int $subHeight, int $width, int $height):BitMatrix{
299
		$matrix      = new BitMatrix(max($width, $height));
300
		$blackPoints = $this->calculateBlackPoints($subWidth, $subHeight, $width, $height);
301
302
		for($y = 0; $y < $subHeight; $y++){
303
			$yoffset    = ($y << self::BLOCK_SIZE_POWER);
304
			$maxYOffset = ($height - self::BLOCK_SIZE);
305
306
			if($yoffset > $maxYOffset){
307
				$yoffset = $maxYOffset;
308
			}
309
310
			for($x = 0; $x < $subWidth; $x++){
311
				$xoffset    = ($x << self::BLOCK_SIZE_POWER);
312
				$maxXOffset = ($width - self::BLOCK_SIZE);
313
314
				if($xoffset > $maxXOffset){
315
					$xoffset = $maxXOffset;
316
				}
317
318
				$left = $this->cap($x, 2, ($subWidth - 3));
319
				$top  = $this->cap($y, 2, ($subHeight - 3));
320
				$sum  = 0;
321
322
				for($z = -2; $z <= 2; $z++){
323
					$br   = $blackPoints[($top + $z)];
324
					$sum += ($br[($left - 2)] + $br[($left - 1)] + $br[$left] + $br[($left + 1)] + $br[($left + 2)]);
325
				}
326
327
				$average = (int)($sum / 25);
328
329
				// Applies a single threshold to a block of pixels.
330
				for($j = 0, $o = ($yoffset * $width + $xoffset); $j < self::BLOCK_SIZE; $j++, $o += $width){
331
					for($i = 0; $i < self::BLOCK_SIZE; $i++){
332
						// Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0.
333
						$v = (((int)($this->luminances[($o + $i)]) & 0xff) <= $average);
334
335
						$matrix->set(($xoffset + $i), ($yoffset + $j), $v, QRMatrix::M_DATA);
336
					}
337
				}
338
			}
339
		}
340
341
		return $matrix;
342
	}
343
344
	/**
345
	 * @noinspection PhpSameParameterValueInspection
346
	 */
347
	private function cap(int $value, int $min, int $max):int{
348
349
		if($value < $min){
350
			return $min;
351
		}
352
353
		if($value > $max){
354
			return $max;
355
		}
356
357
		return $value;
358
	}
359
360
}
361