| Total Complexity | 45 |
| Total Lines | 251 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AlignmentPatternFinder 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 AlignmentPatternFinder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | final class AlignmentPatternFinder{ |
||
| 32 | |||
| 33 | private BitMatrix $bitMatrix; |
||
| 34 | private float $moduleSize; |
||
| 35 | /** @var \chillerlan\QRCode\Detector\AlignmentPattern[] */ |
||
| 36 | private array $possibleCenters; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * <p>Creates a finder that will look in a portion of the whole image.</p> |
||
| 40 | * |
||
| 41 | * @param \chillerlan\QRCode\Decoder\BitMatrix $image image to search |
||
| 42 | * @param float $moduleSize estimated module size so far |
||
| 43 | */ |
||
| 44 | public function __construct(BitMatrix $image, float $moduleSize){ |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * <p>This method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since |
||
| 52 | * it's pretty performance-critical and so is written to be fast foremost.</p> |
||
| 53 | * |
||
| 54 | * @param int $startX left column from which to start searching |
||
| 55 | * @param int $startY top row from which to start searching |
||
| 56 | * @param int $width width of region to search |
||
| 57 | * @param int $height height of region to search |
||
| 58 | * |
||
| 59 | * @return \chillerlan\QRCode\Detector\AlignmentPattern|null |
||
| 60 | */ |
||
| 61 | public function find(int $startX, int $startY, int $width, int $height):?AlignmentPattern{ |
||
| 62 | $maxJ = $startX + $width; |
||
| 63 | $middleI = $startY + ($height / 2); |
||
| 64 | $stateCount = []; |
||
| 65 | |||
| 66 | // We are looking for black/white/black modules in 1:1:1 ratio; |
||
| 67 | // this tracks the number of black/white/black modules seen so far |
||
| 68 | for($iGen = 0; $iGen < $height; $iGen++){ |
||
| 69 | // Search from middle outwards |
||
| 70 | $i = (int)($middleI + (($iGen & 0x01) === 0 ? ($iGen + 1) / 2 : -(($iGen + 1) / 2))); |
||
| 71 | $stateCount[0] = 0; |
||
| 72 | $stateCount[1] = 0; |
||
| 73 | $stateCount[2] = 0; |
||
| 74 | $j = $startX; |
||
| 75 | // Burn off leading white pixels before anything else; if we start in the middle of |
||
| 76 | // a white run, it doesn't make sense to count its length, since we don't know if the |
||
| 77 | // white run continued to the left of the start point |
||
| 78 | while($j < $maxJ && !$this->bitMatrix->get($j, $i)){ |
||
| 79 | $j++; |
||
| 80 | } |
||
| 81 | |||
| 82 | $currentState = 0; |
||
| 83 | |||
| 84 | while($j < $maxJ){ |
||
| 85 | |||
| 86 | if($this->bitMatrix->get($j, $i)){ |
||
| 87 | // Black pixel |
||
| 88 | if($currentState === 1){ // Counting black pixels |
||
| 89 | $stateCount[$currentState]++; |
||
| 90 | } |
||
| 91 | // Counting white pixels |
||
| 92 | else{ |
||
| 93 | // A winner? |
||
| 94 | if($currentState === 2){ |
||
| 95 | // Yes |
||
| 96 | if($this->foundPatternCross($stateCount)){ |
||
| 97 | $confirmed = $this->handlePossibleCenter($stateCount, $i, $j); |
||
| 98 | |||
| 99 | if($confirmed !== null){ |
||
| 100 | return $confirmed; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | $stateCount[0] = $stateCount[2]; |
||
| 105 | $stateCount[1] = 1; |
||
| 106 | $stateCount[2] = 0; |
||
| 107 | $currentState = 1; |
||
| 108 | } |
||
| 109 | else{ |
||
| 110 | $stateCount[++$currentState]++; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | // White pixel |
||
| 115 | else{ |
||
| 116 | // Counting black pixels |
||
| 117 | if($currentState === 1){ |
||
| 118 | $currentState++; |
||
| 119 | } |
||
| 120 | |||
| 121 | $stateCount[$currentState]++; |
||
| 122 | } |
||
| 123 | |||
| 124 | $j++; |
||
| 125 | } |
||
| 126 | |||
| 127 | if($this->foundPatternCross($stateCount)){ |
||
| 128 | $confirmed = $this->handlePossibleCenter($stateCount, $i, $maxJ); |
||
| 129 | |||
| 130 | if($confirmed !== null){ |
||
| 131 | return $confirmed; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | } |
||
| 136 | |||
| 137 | // Hmm, nothing we saw was observed and confirmed twice. If we had |
||
| 138 | // any guess at all, return it. |
||
| 139 | if(count($this->possibleCenters)){ |
||
| 140 | return $this->possibleCenters[0]; |
||
| 141 | } |
||
| 142 | |||
| 143 | return null; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param int[] $stateCount count of black/white/black pixels just read |
||
| 148 | * |
||
| 149 | * @return bool true if the proportions of the counts is close enough to the 1/1/1 ratios |
||
| 150 | * used by alignment patterns to be considered a match |
||
| 151 | */ |
||
| 152 | private function foundPatternCross(array $stateCount):bool{ |
||
| 153 | $moduleSize = $this->moduleSize; |
||
| 154 | $maxVariance = $moduleSize / 2.0; |
||
| 155 | |||
| 156 | for($i = 0; $i < 3; $i++){ |
||
| 157 | if(abs($moduleSize - $stateCount[$i]) >= $maxVariance){ |
||
| 158 | return false; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | return true; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * <p>This is called when a horizontal scan finds a possible alignment pattern. It will |
||
| 167 | * cross check with a vertical scan, and if successful, will see if this pattern had been |
||
| 168 | * found on a previous horizontal scan. If so, we consider it confirmed and conclude we have |
||
| 169 | * found the alignment pattern.</p> |
||
| 170 | * |
||
| 171 | * @param int[] $stateCount reading state module counts from horizontal scan |
||
| 172 | * @param int $i row where alignment pattern may be found |
||
| 173 | * @param int $j end of possible alignment pattern in row |
||
| 174 | * |
||
| 175 | * @return \chillerlan\QRCode\Detector\AlignmentPattern|null if we have found the same pattern twice, or null if not |
||
| 176 | */ |
||
| 177 | private function handlePossibleCenter(array $stateCount, int $i, int $j):?AlignmentPattern{ |
||
| 178 | $stateCountTotal = $stateCount[0] + $stateCount[1] + $stateCount[2]; |
||
| 179 | $centerJ = $this->centerFromEnd($stateCount, $j); |
||
| 180 | $centerI = $this->crossCheckVertical($i, (int)$centerJ, 2 * $stateCount[1], $stateCountTotal); |
||
| 181 | |||
| 182 | if($centerI !== null){ |
||
| 183 | $estimatedModuleSize = (float)($stateCount[0] + $stateCount[1] + $stateCount[2]) / 3.0; |
||
| 184 | |||
| 185 | foreach($this->possibleCenters as $center){ |
||
| 186 | // Look for about the same center and module size: |
||
| 187 | if($center->aboutEquals($estimatedModuleSize, $centerI, $centerJ)){ |
||
| 188 | return $center->combineEstimate($centerI, $centerJ, $estimatedModuleSize); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | // Hadn't found this before; save it |
||
| 193 | $point = new AlignmentPattern($centerJ, $centerI, $estimatedModuleSize); |
||
| 194 | $this->possibleCenters[] = $point; |
||
| 195 | } |
||
| 196 | |||
| 197 | return null; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Given a count of black/white/black pixels just seen and an end position, |
||
| 202 | * figures the location of the center of this black/white/black run. |
||
| 203 | * |
||
| 204 | * @param int[] $stateCount |
||
| 205 | * @param int $end |
||
| 206 | * |
||
| 207 | * @return float |
||
| 208 | */ |
||
| 209 | private function centerFromEnd(array $stateCount, int $end):float{ |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * <p>After a horizontal scan finds a potential alignment pattern, this method |
||
| 215 | * "cross-checks" by scanning down vertically through the center of the possible |
||
| 216 | * alignment pattern to see if the same proportion is detected.</p> |
||
| 217 | * |
||
| 218 | * @param int $startI row where an alignment pattern was detected |
||
| 219 | * @param int $centerJ center of the section that appears to cross an alignment pattern |
||
| 220 | * @param int $maxCount maximum reasonable number of modules that should be |
||
| 221 | * observed in any reading state, based on the results of the horizontal scan |
||
| 222 | * @param int $originalStateCountTotal |
||
| 223 | * |
||
| 224 | * @return float|null vertical center of alignment pattern, or null if not found |
||
| 225 | */ |
||
| 226 | private function crossCheckVertical(int $startI, int $centerJ, int $maxCount, int $originalStateCountTotal):?float{ |
||
| 282 | } |
||
| 283 | |||
| 285 |