Total Complexity | 51 |
Total Lines | 331 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Binarizer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Binarizer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 LuminanceSource $source; |
||
49 | |||
50 | /** |
||
51 | * |
||
52 | */ |
||
53 | public function __construct(LuminanceSource $source){ |
||
54 | $this->source = $source; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @throws \RuntimeException |
||
59 | */ |
||
60 | private function estimateBlackPoint(array $buckets):int{ |
||
61 | // Find the tallest peak in the histogram. |
||
62 | $numBuckets = count($buckets); |
||
63 | $maxBucketCount = 0; |
||
64 | $firstPeak = 0; |
||
65 | $firstPeakSize = 0; |
||
66 | |||
67 | for($x = 0; $x < $numBuckets; $x++){ |
||
68 | |||
69 | if($buckets[$x] > $firstPeakSize){ |
||
70 | $firstPeak = $x; |
||
71 | $firstPeakSize = $buckets[$x]; |
||
72 | } |
||
73 | |||
74 | if($buckets[$x] > $maxBucketCount){ |
||
75 | $maxBucketCount = $buckets[$x]; |
||
76 | } |
||
77 | } |
||
78 | |||
79 | // Find the second-tallest peak which is somewhat far from the tallest peak. |
||
80 | $secondPeak = 0; |
||
81 | $secondPeakScore = 0; |
||
82 | |||
83 | for($x = 0; $x < $numBuckets; $x++){ |
||
84 | $distanceToBiggest = $x - $firstPeak; |
||
85 | // Encourage more distant second peaks by multiplying by square of distance. |
||
86 | $score = $buckets[$x] * $distanceToBiggest * $distanceToBiggest; |
||
87 | |||
88 | if($score > $secondPeakScore){ |
||
89 | $secondPeak = $x; |
||
90 | $secondPeakScore = $score; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | // Make sure firstPeak corresponds to the black peak. |
||
95 | if($firstPeak > $secondPeak){ |
||
96 | $temp = $firstPeak; |
||
97 | $firstPeak = $secondPeak; |
||
98 | $secondPeak = $temp; |
||
99 | } |
||
100 | |||
101 | // If there is too little contrast in the image to pick a meaningful black point, throw rather |
||
102 | // than waste time trying to decode the image, and risk false positives. |
||
103 | if($secondPeak - $firstPeak <= $numBuckets / 16){ |
||
104 | throw new RuntimeException('no meaningful dark point found'); |
||
105 | } |
||
106 | |||
107 | // Find a valley between them that is low and closer to the white peak. |
||
108 | $bestValley = $secondPeak - 1; |
||
109 | $bestValleyScore = -1; |
||
110 | |||
111 | for($x = $secondPeak - 1; $x > $firstPeak; $x--){ |
||
112 | $fromFirst = $x - $firstPeak; |
||
113 | $score = $fromFirst * $fromFirst * ($secondPeak - $x) * ($maxBucketCount - $buckets[$x]); |
||
114 | |||
115 | if($score > $bestValleyScore){ |
||
116 | $bestValley = $x; |
||
117 | $bestValleyScore = $score; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | return $bestValley << self::LUMINANCE_SHIFT; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Calculates the final BitMatrix once for all requests. This could be called once from the |
||
126 | * constructor instead, but there are some advantages to doing it lazily, such as making |
||
127 | * profiling easier, and not doing heavy lifting when callers don't expect it. |
||
128 | * |
||
129 | * Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive |
||
130 | * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or |
||
131 | * may not apply sharpening. Therefore, a row from this matrix may not be identical to one |
||
132 | * fetched using getBlackRow(), so don't mix and match between them. |
||
133 | * |
||
134 | * @return \chillerlan\QRCode\Decoder\BitMatrix The 2D array of bits for the image (true means black). |
||
135 | */ |
||
136 | public function getBlackMatrix():BitMatrix{ |
||
137 | $width = $this->source->getWidth(); |
||
138 | $height = $this->source->getHeight(); |
||
139 | |||
140 | if($width >= self::MINIMUM_DIMENSION && $height >= self::MINIMUM_DIMENSION){ |
||
141 | $subWidth = $width >> self::BLOCK_SIZE_POWER; |
||
142 | |||
143 | if(($width & self::BLOCK_SIZE_MASK) !== 0){ |
||
144 | $subWidth++; |
||
145 | } |
||
146 | |||
147 | $subHeight = $height >> self::BLOCK_SIZE_POWER; |
||
148 | |||
149 | if(($height & self::BLOCK_SIZE_MASK) !== 0){ |
||
150 | $subHeight++; |
||
151 | } |
||
152 | |||
153 | return $this->calculateThresholdForBlock($subWidth, $subHeight, $width, $height); |
||
154 | } |
||
155 | |||
156 | // If the image is too small, fall back to the global histogram approach. |
||
157 | return $this->getHistogramBlackMatrix($width, $height); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * |
||
162 | */ |
||
163 | public function getHistogramBlackMatrix(int $width, int $height):BitMatrix{ |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Calculates a single black point for each block of pixels and saves it away. |
||
205 | * See the following thread for a discussion of this algorithm: |
||
206 | * |
||
207 | * @see http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0 |
||
208 | */ |
||
209 | private function calculateBlackPoints(array $luminances, int $subWidth, int $subHeight, int $width, int $height):array{ |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * For each block in the image, calculate the average black point using a 5x5 grid |
||
299 | * of the blocks around it. Also handles the corner cases (fractional blocks are computed based |
||
300 | * on the last pixels in the row/column which are also used in the previous block). |
||
301 | */ |
||
302 | private function calculateThresholdForBlock( |
||
303 | int $subWidth, |
||
304 | int $subHeight, |
||
305 | int $width, |
||
306 | int $height |
||
307 | ):BitMatrix{ |
||
308 | $matrix = new BitMatrix(max($width, $height)); |
||
309 | $luminances = $this->source->getMatrix(); |
||
310 | $blackPoints = $this->calculateBlackPoints($luminances, $subWidth, $subHeight, $width, $height); |
||
311 | |||
312 | for($y = 0; $y < $subHeight; $y++){ |
||
313 | $yoffset = ($y << self::BLOCK_SIZE_POWER); |
||
314 | $maxYOffset = $height - self::BLOCK_SIZE; |
||
315 | |||
316 | if($yoffset > $maxYOffset){ |
||
317 | $yoffset = $maxYOffset; |
||
318 | } |
||
319 | |||
320 | for($x = 0; $x < $subWidth; $x++){ |
||
321 | $xoffset = ($x << self::BLOCK_SIZE_POWER); |
||
322 | $maxXOffset = $width - self::BLOCK_SIZE; |
||
323 | |||
324 | if($xoffset > $maxXOffset){ |
||
325 | $xoffset = $maxXOffset; |
||
326 | } |
||
327 | |||
328 | $left = $this->cap($x, 2, $subWidth - 3); |
||
329 | $top = $this->cap($y, 2, $subHeight - 3); |
||
330 | $sum = 0; |
||
331 | |||
332 | for($z = -2; $z <= 2; $z++){ |
||
333 | $blackRow = $blackPoints[$top + $z]; |
||
334 | $sum += $blackRow[$left - 2] + $blackRow[$left - 1] + $blackRow[$left] + $blackRow[$left + 1] + $blackRow[$left + 2]; |
||
335 | } |
||
336 | |||
337 | $average = (int)($sum / 25); |
||
338 | |||
339 | // Applies a single threshold to a block of pixels. |
||
340 | for($j = 0, $o = $yoffset * $width + $xoffset; $j < self::BLOCK_SIZE; $j++, $o += $width){ |
||
341 | for($i = 0; $i < self::BLOCK_SIZE; $i++){ |
||
342 | // Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0. |
||
343 | if(($luminances[$o + $i] & 0xff) <= $average){ |
||
344 | $matrix->set($xoffset + $i, $yoffset + $j); |
||
345 | } |
||
346 | } |
||
347 | } |
||
348 | } |
||
349 | } |
||
350 | |||
351 | return $matrix; |
||
352 | } |
||
353 | |||
354 | private function cap(int $value, int $min, int $max):int{ |
||
365 | } |
||
366 | |||
367 | } |
||
368 |