Why QR Codes Look Different: Eight Masks and Penalty Scores
EENTERTAINMENT
EntertainmentPublished: 9 min read

Why QR Codes Look Different: Eight Masks and Penalty Scores

Enter the same URL into two QR generators and the patterns may differ. That alone does not establish a fault: decode each symbol and compare its contents. Mask selection is one reason for the difference, alongside the error correction level, symbol size and encoding choices.

This article covers the familiar square QR Code Model 2. Do not apply its eight masks and scoring procedure unchanged to Micro QR or rMQR. Following our guide to QR error correction levels, we look at what masking changes—and what it leaves intact.

Japanese original published: 2026-06-06

A QR pattern is more than its payload

A QR symbol contains data codewords, error correction codewords and structures such as finder, timing and alignment patterns. Placing the bits without masking can create long runs, large blocks or patterns resembling a finder pattern.

A mask inverts selected modules—the small square cells—in the region holding data codewords, error correction codewords and any trailing remainder bits. The choice of inversion rule changes the appearance. It does not add payload capacity or error correction redundancy.

Inverting the same cells twice restores their original values. The format information identifies the mask, so a reader can reverse it and recover the codewords. Masking is not a random decorative effect or an encryption mechanism.

Eight masks, with coordinates starting at zero

Exclude the quiet zone. The top-left module of the symbol is (r, c) = (0, 0); row r increases downward and column c increases to the right. Here, mod gives a nonnegative integer remainder and floor rounds down. Invert an eligible module when its condition is true. Function patterns and format information are not inverted by these eight data masks.

Scroll horizontally if the table does not fit.

MaskInversion condition
0(r + c) mod 2 = 0
1r mod 2 = 0
2c mod 3 = 0
3(r + c) mod 3 = 0
4(floor(r / 2) + floor(c / 3)) mod 2 = 0
5((r × c) mod 2 + (r × c) mod 3) = 0
6(((r × c) mod 2 + (r × c) mod 3) mod 2) = 0
7(((r + c) mod 2 + (r × c) mod 3) mod 2) = 0

Automatic selection evaluates all eight candidates and chooses a minimum-penalty result. Some libraries also allow a mask to be forced; distinguish that option from automatic selection. Different masks correctly applied to the same codewords preserve the decoded content.

Write each candidate’s format information before scoring

Encoding first selects modes and a version, builds data and error correction codewords, and places the function patterns and codewords. In Nayuki’s public implementation, candidate evaluation then proceeds as follows:

  1. Apply a candidate mask.
  2. Write that candidate’s format information.
  3. Score the whole symbol, including function patterns.
  4. Undo the candidate’s inversion and continue the comparison.
  5. Apply the winning mask and write its final format information.

The region being scored is not the same as the region being inverted. The outer quiet zone is not added to the total module count for the dark-ratio calculation. A reader obtains the mask number from the format information; it does not need to repeat the scoring contest.

Even for the same URL, error correction level, version, segmentation and encoding choices can change the array before masking begins. Implementation details can also affect candidate scores. A different appearance does not, by itself, show that a code is counterfeit or that its content is different.

N1: runs of the same color

For a same-color run of length L≥5, add 3 + (L − 5) points. Check both rows and columns, and count light runs too. Five modules cost three points; six cost four; seven cost five. Do not divide a long run into separate groups of five.

11111   → 3 points
111111  → 4 points
0000000 → 5 points

Here 1 means dark and 0 means light. These are local examples, not total scores for a complete QR symbol.

N2: overlapping 2×2 blocks

Each same-color 2×2 block contributes three points. Overlaps count: a solid 3×3 square contains four such blocks, giving 12 points for N2 alone. A solid a×b rectangle contains (a−1)(b−1) overlapping 2×2 blocks.

N2 is added independently of N1. A region contributing to more than one rule is not an accidental double count.

N3: patterns that resemble a finder pattern

This rule examines dark–light–dark–light–dark runs with length ratio 1:1:3:1:1 and surrounding light space. At a base width of one module, examples are 00001011101 and 10111010000; a qualifying detection adds 40 points. Counting every isolated 1011101 without its surroundings is insufficient.

These short examples are not a complete N3 implementation. Nayuki also handles scaled run ratios and light space at the symbol boundary. ZXing’s MaskUtil checks a seven-module core and four neighboring light modules. Do not assume two libraries must return identical scores without aligning their treatment of surroundings, boundaries and overlapping detections.

N4: dark-module proportion and the total score

In ZXing’s implementation, let D be the dark-module count and T the total count excluding the quiet zone. The contribution is 10 × floor(abs(20D − 10T) / T), where abs means absolute value. For a 21×21 symbol, T=441: D=220 gives 0 points, D=250 gives 10 and D=280 gives 20. Do not first round the proportion to a whole percentage.

The selection uses the sum of N1 through N4, not simply the ratio nearest 50%. Because the side length is odd, a symbol cannot contain exactly equal numbers of dark and light modules.

Changing the mask does not make the payload less dense or increase redundancy. The score compares patterns; it is not a measured probability of successful scanning under particular printing, lighting or camera conditions.

What masks cannot fix: logos, margins and printing

Preserve a quiet zone at least four modules wide on all four sides of a Model 2 symbol. Inadequate contrast, blurred edges, undersized printing and logos covering function patterns cannot be repaired simply by choosing another mask.

Level H is not permission to cover 30% of the image with a logo. Reed–Solomon correction works on codeword errors; the approximate restoration percentage is not a safe area allowance. Format information has separate error protection, but damage to detection patterns, margins or contrast can prevent successful reading before payload correction helps.

Decode the actual output using more than one device and compare the recovered text. For printed material, check the final size, paper and lighting, not just the screen preview. Making the modules too small for the camera to resolve defeats even a favorable mask score.

Reading the illustration and checking the sources

The QR on the left of the illustration actually encodes https://nandemo-tools.com/. The eight grids on the right illustrate mask conditions; they are not scannable QR symbols. Dark cells in those grids mean “invert here,” not “this cell must be dark in the final code.” Keeping these roles separate avoids presenting a decorative grid as real encoded data.

The ISO catalog lists the 2015 edition as withdrawn and the 2024 edition as its successor. This article explains public documentation and implementations; it is not a conformance audit against the full standard. Treat the calculation examples as explanations of the stated rules and implementation, not a replacement for a complete encoder specification.

References and sources

Editorial note

This article was prepared with AI assistance and reviewed by an editor before publication. It may still contain factual errors, interpretation mistakes, or outdated information. Check the cited primary sources or official documentation before making an important decision.

Related articles