Masking
We have placed our data on our QRCode. We will now apply masking to the QRCode.
The masking part (8.8) consists in patching the QRCode flaws:
- Too many consecutive modules of the same value
- Some sequence looking too much like the Finder Patterns
- Modules of the same value
- Module Dark / Light ratio
We will compute a score based on above criteria for each masks we will be using.
After applying the masking, we will be very close to have a scannable QRCode
As mentionned before, there are masks to apply, 8 of them. I have made a simple vizualization each masks area of effect:
- Grey is untouched by the mask
- Black is flipped by the mask
- Orange is the occupied area
)
Mask 000 (CheckerBoard)
(i + j) % 2 == 0
)
Mask 001 (Horizontal)
i % 2 == 0
)
Mask 010 (Vertical)
j % 3 == 0
)
Mask 011 (Diagonal)
(i + j) % 2 == 0
)
Mask 100 (Large CheckerBoard)
((i / 2) + (j / 3)) % 2 == 0
)
Mask 101 (Fields)
((i * j) % 2 + (i * j) % 3) == 0
)
Mask 110 (Diamonds)
(((i * j) % 2 + (i * j) % 3)) % 2 == 0
)
Mask 111 (Meadow)
(((i * j) % 3 + (i + j) % 2)) % 2 == 0
We will apply each masks to our QRCode and compute a score based on the flaws we have seen before.
The remaining question is how to compute the four scores ?
The flaw score will be computed as follow:
- Find 5 + N consecutive modules of the same value and add 3 + N to the score.
- Some sequence of module being [D, L, D, D, D, L, D] (D = Dark, L = Light) will add 40 to the score.
- Compute the ratio of Dark / Light modules in percentage and retrieve the absolute value of the difference with 50%, divide by 5 and then multiply by 10. (integer division)
- Count the 2x2 modules of the same value and add 3 * N to the score.
example.com without masking
example.com with Checkerboard masking
Fun Fact: I've added the right masking information, the QRCode on the right should be scannable, while the left one not.
Every score is computed on non-occupied areas.
Black and white ratio score
2x2 Square score
5 + N consecutive modules score
Finder pattern score
Black and White ratio score
We have find a black / white ratio of 47,7%. For each 5% difference with 50%, we add 10 to the score. (0)
2x2 Square score
We have found 108 2x2 squares of the same value. For each 2x2 square found, we add 3 to the score. (324)
5 + N consecutive modules score
We have found 27 Vertical + 22 Horizontal 5 + N consecutive modules of the same value. For each 5 + N consecutive modules found, we add 3 + N to the score. (228)
Finder pattern score
We have found 16 finder patterns. For each finder pattern found, we add 40 to the score. (640)
Total score: 0 + 324 + 228 + 640 = 1182
Recap:
We have placed the data on the QRCode. We applied the eight masks to find the one with the lowest score.
To make it readable we need to add the format information and the version information.
Made with 🚀 by erwanvivien
Catched a mistake ? Please make an issue