Spec & internals
Mask selection and penalty scores — how the best mask wins
To pick a mask, the encoder renders the symbol with each of the eight patterns and scores it on four penalty rules: N1 charges runs of five or more same-colour modules, N2 charges 2×2 blocks, N3 charges 40 points per finder-like 1:1:3:1:1 run, and N4 charges dark/light imbalance. The lowest total score wins.
The contest
After data placement, the encoder holds eight candidate symbols — one per mask pattern. ISO/IEC 18004 says: score each candidate with four penalty rules that approximate "hard to scan", and keep the lowest-scoring one. The rules are weighted by constants the standard names N1–N4.
The four penalty rules
Rule 1 — runs (N1 = 3)
Scan every row and every column for runs of five or more consecutive same-colour modules. Each run costs 3 points, plus 1 per module beyond five (a 5-run costs 3, a 7-run costs 5). Long uniform runs starve the decoder of the edges it uses to keep its sampling grid locked between timing patterns.
Rule 2 — blocks (N2 = 3)
Every 2 × 2 block of same-colour modules costs 3 points, counted at every position — overlapping blocks each count, so a solid 3 × 3 area contains four penalised 2 × 2s. This targets solid areas more finely than rule 1: big blobs distort local thresholding and give no structure to lock onto.
Rule 3 — finder lookalikes (N3 = 40)
Each occurrence, in any row or column, of the pattern dark-light-dark-dark-dark-light-dark with four light modules on one side — the 1:1:3:1:1 finder signature framed the way a real finder is — costs 40 points. The heavy weight is deliberate: a false finder candidate can derail symbol detection entirely, so a single lookalike outweighs a dozen ugly runs.
Rule 4 — balance (N4 = 10)
Compute the dark-module percentage of the whole symbol. Every full 5% step away from 50% costs 10 points (55% costs 0 at the boundary, 62% costs 20). Strong imbalance makes global thresholding fragile: a mostly-dark symbol in poor light reads as a black square.
What the scores do — and do not — decide
The winning mask number is written into the format information and that is the end of the scores: they are never stored, and the decoder never recomputes them. Scoring exists purely to pick a good-scanning rendition. Consequences worth knowing:
- Any mask is legal. A symbol wearing the worst-scoring mask still decodes; it is merely less robust at the margins — glare, blur, distance.
- Implementations differ legitimately. Off-by-one choices in run counting or the N3 window mean two correct libraries can score the same candidate differently and pick different masks — the visible reason two generators produce different codes from identical input.
- Shortcuts are permitted in practice. The full evaluation costs eight render-and-score passes; some embedded encoders score a subset or hard-code a mask. The standard frames the procedure as how to choose, but no decoder can tell whether you obeyed.
For anyone building an encoder, the penalty rules are also the classic source of subtle bugs — symbols that work but underperform. Comparing your chosen mask against a reference implementation across many payloads, and verifying decodes under blur and rotation, catches what unit tests miss. The full formulas with worked examples live in the mask pattern reference.
FAQ
How does a QR encoder choose the mask pattern?
It applies all eight masks to the data region, scores each full symbol on the four penalty rules — long runs, 2×2 blocks, finder-like sequences, and dark/light imbalance — and selects the mask with the lowest total penalty.
What are the QR penalty weights N1, N2, N3 and N4?
N1 = 3 for each run of five-plus same-colour modules (plus 1 per extra module), N2 = 3 per 2×2 same-colour block, N3 = 40 per finder-like 1:1:3:1:1 pattern with adjacent light run, N4 = 10 per 5% deviation from 50% dark.
Why is the finder-lookalike penalty so high?
Because a data region that imitates the 1:1:3:1:1 finder signature can make scanners chase false corners, which threatens detection itself rather than merely degrading sampling. One lookalike is worse than many cosmetic flaws, and the 40-point weight encodes that.
Do decoders check the penalty score?
No. The score influences only which mask the encoder picks; it is not stored in the symbol. A decoder reads the mask number from the format information and reverses the XOR, regardless of how well or badly the mask was chosen.
Try it — free, no signup
Related
- Mask patterns 0 to 7 — the eight formulas and why they exist — Every QR code XORs its data region with one of eight fixed patterns to break up problem shapes. The formulas, what they look like, and what masking prevents.
- Why two generators produce different QR codes — Version selection, EC defaults, mode segmentation and mask choice can all legally differ. How to tell a harmless difference from an actual bug.
- Format information — the 15 bits everything depends on — Format information encodes the EC level and mask in 5 bits, protects them with BCH(15,5), XORs with a fixed mask, and is written twice beside the finders.
- How a QR code is generated, step by step — Encoding runs in eight steps: choose the mode, choose the version, build the bit stream, split into codewords, compute Reed–Solomon error correction,…