Debian Codegraph @

src/Encoder/MaskUtil.php

raw
1 <?php
2 declare(strict_types = 1);
3
4 namespace BaconQrCode\Encoder;
5
6 /**
7 * Mask utility.
8 */
9 final class MaskUtil
10 {
11 /**#@+
12 * Penalty weights from section 6.8.2.1
13 */
14 public const N1 = 3;
15 public const N2 = 3;
16 public const N3 = 40;
17 public const N4 = 10;
18 /**#@-*/
19
20 private function __construct()
21 {
22 }
23
24 /**
25 * Applies mask penalty rule 1 and returns the penalty.
26 *
27 * Finds repetitive cells with the same color and gives penalty to them.
28 * Example: 00000 or 11111.
29 */
30 public static function applyMaskPenaltyRule1(ByteMatrix $matrix) : int
31 {
32 return (
33 self::applyMaskPenaltyRule1Internal($matrix, true)
34 + self::applyMaskPenaltyRule1Internal($matrix, false)
35 );
36 }
37
38 /**
39 * Applies mask penalty rule 2 and returns the penalty.
40 *
41 * Finds 2x2 blocks with the same color and gives penalty to them. This is
42 * actually equivalent to the spec's rule, which is to find MxN blocks and
43 * give a penalty proportional to (M-1)x(N-1), because this is the number of
44 * 2x2 blocks inside such a block.
45 */
46 public static function applyMaskPenaltyRule2(ByteMatrix $matrix) : int
47 {
48 $penalty = 0;
49 $array = $matrix->getArray();
50 $width = $matrix->getWidth();
51 $height = $matrix->getHeight();
52
53 for ($y = 0; $y < $height - 1; ++$y) {
54 for ($x = 0; $x < $width - 1; ++$x) {
55 $value = $array[$y][$x];
56
57 if ($value === $array[$y][$x + 1]
58 && $value === $array[$y + 1][$x]
59 && $value === $array[$y + 1][$x + 1]
60 ) {
61 ++$penalty;
62 }
63 }
64 }
65
66 return self::N2 * $penalty;
67 }
68
69 /**
70 * Applies mask penalty rule 3 and returns the penalty.
71 *
72 * Finds consecutive cells of 00001011101 or 10111010000, and gives penalty
73 * to them. If we find patterns like 000010111010000, we give penalties
74 * twice (i.e. 40 * 2).
75 */
76 public static function applyMaskPenaltyRule3(ByteMatrix $matrix) : int
77 {
78 $penalty = 0;
79 $array = $matrix->getArray();
80 $width = $matrix->getWidth();
81 $height = $matrix->getHeight();
82
83 for ($y = 0; $y < $height; ++$y) {
84 for ($x = 0; $x < $width; ++$x) {
85 if ($x + 6 < $width
86 && 1 === $array[$y][$x]
87 && 0 === $array[$y][$x + 1]
88 && 1 === $array[$y][$x + 2]
89 && 1 === $array[$y][$x + 3]
90 && 1 === $array[$y][$x + 4]
91 && 0 === $array[$y][$x + 5]
92 && 1 === $array[$y][$x + 6]
93 && (
94 (
95 $x + 10 < $width
96 && 0 === $array[$y][$x + 7]
97 && 0 === $array[$y][$x + 8]
98 && 0 === $array[$y][$x + 9]
99 && 0 === $array[$y][$x + 10]
100 )
101 || (
102 $x - 4 >= 0
103 && 0 === $array[$y][$x - 1]
104 && 0 === $array[$y][$x - 2]
105 && 0 === $array[$y][$x - 3]
106 && 0 === $array[$y][$x - 4]
107 )
108 )
109 ) {
110 $penalty += self::N3;
111 }
112
113 if ($y + 6 < $height
114 && 1 === $array[$y][$x]
115 && 0 === $array[$y + 1][$x]
116 && 1 === $array[$y + 2][$x]
117 && 1 === $array[$y + 3][$x]
118 && 1 === $array[$y + 4][$x]
119 && 0 === $array[$y + 5][$x]
120 && 1 === $array[$y + 6][$x]
121 && (
122 (
123 $y + 10 < $height
124 && 0 === $array[$y + 7][$x]
125 && 0 === $array[$y + 8][$x]
126 && 0 === $array[$y + 9][$x]
127 && 0 === $array[$y + 10][$x]
128 )
129 || (
130 $y - 4 >= 0
131 && 0 === $array[$y - 1][$x]
132 && 0 === $array[$y - 2][$x]
133 && 0 === $array[$y - 3][$x]
134 && 0 === $array[$y - 4][$x]
135 )
136 )
137 ) {
138 $penalty += self::N3;
139 }
140 }
141 }
142
143 return $penalty;
144 }
145
146 /**
147 * Applies mask penalty rule 4 and returns the penalty.
148 *
149 * Calculates the ratio of dark cells and gives penalty if the ratio is far
150 * from 50%. It gives 10 penalty for 5% distance.
151 */
152 public static function applyMaskPenaltyRule4(ByteMatrix $matrix) : int
153 {
154 $numDarkCells = 0;
155
156 $array = $matrix->getArray();
157 $width = $matrix->getWidth();
158 $height = $matrix->getHeight();
159
160 for ($y = 0; $y < $height; ++$y) {
161 $arrayY = $array[$y];
162
163 for ($x = 0; $x < $width; ++$x) {
164 if (1 === $arrayY[$x]) {
165 ++$numDarkCells;
166 }
167 }
168 }
169
170 $numTotalCells = $height * $width;
171 $darkRatio = $numDarkCells / $numTotalCells;
172 $fixedPercentVariances = (int) floor(abs($darkRatio - 0.5) * 20);
173
174 return $fixedPercentVariances * self::N4;
175 }
176
177 /**
178 * Returns the mask bit for "getMaskPattern" at "x" and "y".
179 *
180 * See 8.8 of JISX0510:2004 for mask pattern conditions.
181 */
182 public static function getDataMaskBit(int $maskPattern, int $x, int $y) : bool
183 {
184 return 0 === match ($maskPattern) {
185 0 => ($x + $y) % 2,
186 1 => $y % 2,
187 2 => $x % 3,
188 3 => ($x + $y) % 3,
189 4 => (intdiv($y, 2) + intdiv($x, 3)) % 2,
190 5 => (($x * $y) % 2) + (($x * $y) % 3),
191 6 => ((($x * $y) % 2) + (($x * $y) % 3)) % 2,
192 7 => ((($x + $y) % 2) + (($x * $y) % 3)) % 2,
193 };
194 }
195
196 /**
197 * Helper function for applyMaskPenaltyRule1.
198 *
199 * We need this for doing this calculation in both vertical and horizontal
200 * orders respectively.
201 */
202 private static function applyMaskPenaltyRule1Internal(ByteMatrix $matrix, bool $isHorizontal) : int
203 {
204 $penalty = 0;
205 $iLimit = $isHorizontal ? $matrix->getHeight() : $matrix->getWidth();
206 $jLimit = $isHorizontal ? $matrix->getWidth() : $matrix->getHeight();
207 $array = $matrix->getArray();
208
209 for ($i = 0; $i < $iLimit; ++$i) {
210 $numSameBitCells = 0;
211 $prevBit = -1;
212
213 for ($j = 0; $j < $jLimit; $j++) {
214 $bit = $isHorizontal ? $array[$i][$j] : $array[$j][$i];
215
216 if ($bit === $prevBit) {
217 ++$numSameBitCells;
218 } else {
219 if ($numSameBitCells >= 5) {
220 $penalty += self::N1 + ($numSameBitCells - 5);
221 }
222
223 $numSameBitCells = 1;
224 $prevBit = $bit;
225 }
226 }
227
228 if ($numSameBitCells >= 5) {
229 $penalty += self::N1 + ($numSameBitCells - 5);
230 }
231 }
232
233 return $penalty;
234 }
235 }