Chapter 6
Placing the data
On this page
Chapters 3 to 5 turned text into one long sequence of bytes: the data bytes and their error correction bytes, split into blocks and interleaved. This chapter puts that sequence into the data area from chapter 1, then applies one of the eight mask patterns from chapter 2, chosen by scoring all of them.
Laying out the bytesLink to this section
The sequence is placed as one long stream of bits, each byte’s most significant bit first (7.7.3). The stream runs up and down the code in columns two modules wide, starting in the bottom right corner:
- Fill the bottom row of the column: the right module, then the left one.
- Move up a row and do the same, and so on to the top.
- Move two modules to the left and come back down, again right module before left.
- Carry on, up and down in turn, until the left edge.
Every module that isn’t in the data area is skipped: the finder patterns and their separators, the timing and alignment patterns, the dark module, and the format and version information. The stream carries on at the next free module, so it flows around them. Where an alignment pattern covers only one of a column’s two modules, the bits run along the single module beside it.
One detail is left to the standard’s figures. The vertical timing pattern takes up the whole of column 6, so the path skips that column entirely, and the columns to its left pair up as 5 and 4, 3 and 2, then 1 and 0 (Figures 19 and 20).
The shapes of the bytesLink to this section
Most bytes come out as a block 2 modules wide and 4 tall, two bits to a row, as chapter 4 promised. Where the path turns at an edge, or squeezes past an alignment pattern, a byte bends into another shape. The standard calls these irregular symbol characters and shows how bits are placed in them, case by case (Figures 16 to 18). No extra rules are needed, though: filling the stream bit by bit, as above, gives the same shapes, and the standard offers this as an alternative method that “yields the same result” (7.7.3).
Keeping each byte’s bits together has a useful effect. Damage usually comes in patches, and Reed-Solomon codes count damaged bytes, however many of their bits are wrong, so a patch costs fewer bytes when each byte is compact.
Remainder bitsLink to this section
The data area isn’t always a whole number of bytes. Version 2 has 359 data modules; its 44 bytes fill 352 of them, leaving 7. These remainder bits are set to 0 before masking, and a scanner ignores them (7.7.3, Table 1). Versions 2 to 6 have 7 remainder bits, versions 14 to 20 and 28 to 34 have 3, versions 21 to 27 have 4, and the rest have none.
Step through the placementLink to this section
Pick a version and level, then play the placement or step through it a byte at a time. The data bytes are numbered D1, D2, … and the error correction bytes E1, E2, …, block by block as in chapter 5, so in codes with several blocks the numbers jump about: the stream is in interleaved order. The default, version 2-M, is the standard’s own example (Figure 19). Point at a module to see which bit of which byte it holds.
Version 2-M: 359 data modules. The 44 bytes (28 data + 16 error correction) fill 352 of them, leaving 7 remainder bits.
data bytes · error correction bytes · remainder bits · not data
Point at a module to see which bit it holds.
A scanner reads the bits back along the same path, once it has undone the mask.
Applying the mask patternLink to this section
Placed as it comes, the data can form large patches of one colour, or stretches that look like a slice through a finder pattern, either of which can confuse a scanner (7.8.1). So the data area is masked: every data module where the mask pattern is dark is flipped, remainder bits included. The fixed parts and the format and version information are left alone. Flipping is the XOR of chapter 2, so applying the same mask again undoes it, which is what a scanner does once it has read the mask’s reference from the format information.
Scoring the masksLink to this section
Whoever makes the code tries each of the eight masks in turn, fills in the format information for it, and scores the whole code with four rules (7.8.3, Table 11). The mask with the lowest score is used.
| Feature | Penalty points |
|---|---|
| A run of 5 or more modules of one colour in a row or column | 3 for 5 modules, and 1 more for each module beyond 5 |
| A 2 × 2 square of modules of one colour | 3 for each, counting overlapping squares, so a 3 × 3 square counts 4 |
| Dark, light, dark, dark, dark, light, dark in a row or column, with 4 light modules before or after it | 40 for each |
| The share of dark modules | 10 for each full 5 % away from 50 % |
The third rule is the finder pattern’s 1:1:3:1:1 ratio from chapter 1. It costs the most, since a scanner hunting for finder patterns could be misled by one in the data.
The rules score the whole code, fixed parts included. Each finder pattern, for example, has runs of 7 and a 3 × 3 square at its centre. Most of those points are the same for every mask, so they add the same amount to each score; only where the fixed parts meet the data, or the format information, do they differ.
Compare the masksLink to this section
Type some text, or keep the standard’s example, 01234567 at level M (Annex I). The table scores each mask; select a row to see the code with that mask, and mark what each rule counts. The last row scores the code with no mask at all, for comparison: for the example, 614 points, against 297 for the best mask.
Numeric mode, version 1-M: 21 × 21 modules.
Mask 000: 33 runs of 5 or more, 34 squares, 1 finder-like sequence, 49.9 % dark.
| Mask | Runs | 2 × 2 | Finder | Balance | Total |
|---|---|---|---|---|---|
| 155 | 102 | 40 | 0 | 297✓ | |
| 180 | 153 | 200 | 0 | 533 | |
| 206 | 111 | 80 | 0 | 397 | |
| 187 | 105 | 160 | 0 | 452 | |
| 196 | 174 | 200 | 0 | 570 | |
| 220 | 177 | 240 | 0 | 637 | |
| 191 | 108 | 120 | 0 | 419 | |
| 176 | 150 | 80 | 0 | 406 | |
| 225 | 219 | 160 | 10 | 614 |
✓ marks the lowest total, the mask the code uses. “None” is for comparison only: every code must use one of the eight masks.
Which mask does Annex I choose? The standard’s worked example picks mask 010 (Annex I), but the rules as written score mask 000 lowest: 297 points against 397 for 010. Counting the quiet zone as light modules for the third rule makes 010 the lowest, so that may be how the example was worked out; the box under the table switches this on. That reading charges each finder pattern 6 times for the quiet zone beside it, the same 720 points for every mask, but it also catches finder-like sequences at the edges of the data, and mask 000 has more of those. The next step of Annex I then calls the mask 011, though the bits it puts in the format information are those of 010.
None of this matters to a scanner. Any of the eight masks gives a valid code, since the format information says which one was used, and encoders do differ in the details: the widely used qrcode package for JavaScript also picks 000 for this example.
That completes the code. A scanner works through the chapters in reverse: it finds the finder patterns (chapter 1), reads the format information (chapter 2), undoes the mask and reads the bits along the zigzag (this chapter), sorts the bytes back into their blocks and corrects each one (chapter 5), and decodes the bitstream (chapter 3). The full demo does all of it.