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