QR Code Masks


QRCode Masks

A mask is applied to the data area of the QR Code to optimise the ability for a scanner to read it.

  • There are eight different mask patterns.
  • All eight masks produce valid QR codes that can be scanned in.
  • QR code generation tools select the one with the lowest penalty as that is considered visibly the best.
  • The best masks optimise the distribution of black and white modules.

Mask Patterns

The list of mask patterns show the conditional statement and the result of applying it to a 24 by 24 area of white modules

Mask Pattern 0

(row + col) % 2 == 0



Mask Pattern 1

row % 2 == 0



Mask Pattern 2

col % 3 == 0



Mask Pattern 3

(row + col) % 3 == 0



Mask Pattern 4

floor(row/2) + floor(col/3)) % 2 == 0



Mask Pattern 5

(row * col) % 2 + (row * col) % 3 == 0



Mask Pattern 6

((row * col) % 2 + (row * col) % 3) % 2 == 0



Mask Pattern 7

((row * col) % 3 + row + col) % 2 == 0




Applying a Mask Pattern to a QR Code

Mask patterns are applied to the data within a QR code using the conditional statement.

  • The row value of a module is the number of rows down from the top of the QR code, starting at 0.
  • The column value of a module is the number of columns across from the left of the QR code, starting at 0.
  • The row and column values are used to determine if the result of the condition is true or false.
  • If the result is true, then the module value changes from black to white or from white ro black
  • If the result is false, then the module value stays the same
Mappings from the original colour to the new colour depending on the result of the conditional statement
Original Module Color Result of Condition Statement New Module Color
whitefalsewhite
whitetrueblack
blackfalseblack
blacktruewhite

Understanding the Mask Pattern

Each mask pattern is defined using a conditional statement that uses the modulo (%) operator.

  • The modulo operation a % b finds the remainder when a is divided by b.
  • By using %2, the result is guarenteed to be always 1 or .0
  • For each mask a calculation is done using the row and/or column values of each module and then the modulo 2 operator is applied.
  • If the result is 0, then the output is 1.
  • If the result is 1, then the output is 0.
  • The XOR operation is then applied between the output from the condition and the module value to deternime the updated value of the module.

Using the XOR operator

When applying a mask to a qrcode, the black modules are represented by the value 1 and the white by the value 0.

  • If condition is true, then the value is 1.
  • If condition is false then the value is 0.
  • The XOR operation is then applied between the result of the condition and the value of the module.
Output from the XOR operation and the equivalent in terms of the module colour
A B A XOR B Module Equivalent
000 WfalseW
011WtrueB
101BfalseB
110BtrueW

Rating Masks

To determine which is the best mask to use for a QR code, each mask is applied and then rated using a penalty point system.

  • The mask with the lowest penalty points is chosen by default.
  • Any mask value can be chosen although it may not be visibly the best.

Penalty Points

Penalty points are determined using the following factors

Calculation 1: Horizontal and vertical runs of five or more modules of the same colour

Each run of five or more modules of the same colour has a penalty of 3 points plus 1 for each additional pixel


Calculation 2: Block of the same colour greater than two by two modules.

Each two by two block of the same colour has a penalty of 3 points.


Calculation 3: Hoizontal and vertical runs matching the value "10111010000".

Each run matching the following has a penalty of 40 points.

Calculation 4: The ratio of black to white modules

Penalty calculated from the percentage of black modules. Ten penality points for each multiple of 5% from 50%




Mask Analysis Tool for a Version 1 QR Code

The tool creates a version 1 QR code using a optimal mask with the minimum penalty code. The calculations showing the penalty calculation for each mask can be viewed.

Text:

Encoding:

Error Level:


3 is the best mask