| Total Complexity | 48 |
| Total Lines | 323 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 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{ |
||
| 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{ |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * For each block in the image, calculate the average black point using a 5x5 grid |
||
| 294 | * of the surrounding blocks. Also handles the corner cases (fractional blocks are computed based |
||
| 295 | * on the last pixels in the row/column which are also used in the previous block). |
||
| 296 | */ |
||
| 297 | private function calculateThresholdForBlock(int $subWidth, int $subHeight, int $width, int $height):BitMatrix{ |
||
| 298 | $matrix = new BitMatrix(max($width, $height)); |
||
| 299 | $blackPoints = $this->calculateBlackPoints($subWidth, $subHeight, $width, $height); |
||
| 300 | |||
| 301 | for($y = 0; $y < $subHeight; $y++){ |
||
| 302 | $yoffset = ($y << self::BLOCK_SIZE_POWER); |
||
| 303 | $maxYOffset = ($height - self::BLOCK_SIZE); |
||
| 304 | |||
| 305 | if($yoffset > $maxYOffset){ |
||
| 306 | $yoffset = $maxYOffset; |
||
| 307 | } |
||
| 308 | |||
| 309 | for($x = 0; $x < $subWidth; $x++){ |
||
| 310 | $xoffset = ($x << self::BLOCK_SIZE_POWER); |
||
| 311 | $maxXOffset = ($width - self::BLOCK_SIZE); |
||
| 312 | |||
| 313 | if($xoffset > $maxXOffset){ |
||
| 314 | $xoffset = $maxXOffset; |
||
| 315 | } |
||
| 316 | |||
| 317 | $left = $this->cap($x, 2, ($subWidth - 3)); |
||
| 318 | $top = $this->cap($y, 2, ($subHeight - 3)); |
||
| 319 | $sum = 0; |
||
| 320 | |||
| 321 | for($z = -2; $z <= 2; $z++){ |
||
| 322 | $br = $blackPoints[($top + $z)]; |
||
| 323 | $sum += ($br[($left - 2)] + $br[($left - 1)] + $br[$left] + $br[($left + 1)] + $br[($left + 2)]); |
||
| 324 | } |
||
| 325 | |||
| 326 | $average = (int)($sum / 25); |
||
| 327 | |||
| 328 | // Applies a single threshold to a block of pixels. |
||
| 329 | for($j = 0, $o = ($yoffset * $width + $xoffset); $j < self::BLOCK_SIZE; $j++, $o += $width){ |
||
| 330 | for($i = 0; $i < self::BLOCK_SIZE; $i++){ |
||
| 331 | // Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0. |
||
| 332 | $v = (((int)($this->luminances[($o + $i)]) & 0xff) <= $average); |
||
| 333 | |||
| 334 | $matrix->set(($xoffset + $i), ($yoffset + $j), $v, QRMatrix::M_DATA); |
||
| 335 | } |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | return $matrix; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @noinspection PhpSameParameterValueInspection |
||
| 345 | */ |
||
| 346 | private function cap(int $value, int $min, int $max):int{ |
||
| 357 | } |
||
| 358 | |||
| 360 |