# DotCode

DotCode is two-dimensional (2D) matrix barcode invented in 2008 by Hand Held Products company to replace outdated Code 128. At this time, it is issued by Association for Automatic Identification and Mobility (AIM) as “ISS DotCode Symbology Specification 4.0”. DotCode consists of sparse black round dots and white spaces on white background. In case of a black background the dots can be white. DotCode was developed to use with high-speed industrial printers where printing accuracy can be low. Because DotCode by the standard does not require complicated elements like continuous lines or special shapes it can be applied with laser engraving or industrial drills.
DotCode can be represented as rectangular array with minimal size of each side 5X dots. Maximal size of DotCode is not limited by the standard (as Code 128 is not limited) but practical limit is recommended as 100x99$^{:5.2.1.4}$ which can encode around 730 digits, 366 alphanumeric characters or 304 bytes.
As an extension of Code 128 barcode, DotCode allows more compact encoding of 8-bit data array and Unicode support with Extended Channel Interpretation feature. Additionally, DotCode provides much more data density and Reed–Solomon error correction which allows to restore partially damaged barcode. However, the main DotCode implementation, the same as Code 128, is effective encoding of GS1 data which is used in worldwide shipping and packaging industry.

## History and standards

DotCode barcode was invented in 2008 by Dr. Andrew Longacre from Hand Held Products company and standardized in 2009 by AIM as “Bar code symbology specification - DotCode”. In 2019 DotCode was reviewed as “ISS DotCode Symbology Specification 4.0”.
Set of patents is registered, which are related with DotCode encoding and decoding:
* United States Patent US20090200386A1 by Hand Held Products Inc "Machine readable 2D symbology printable on demand"
* United States Patent US20090200386A1 by Datalogic IP Tech SRL "System and method for extracting bitstream data in two-dimensional optical codes"
* Chinese Patent CN113297872A by Fuzhou Symbol Information Technology Co ltd "Dotcode identification method and equipment"

## Application

DotCode barcode can be used in the same way as Code 128 or any (2D) matrix barcode. At this time, it is used mostly to encode GS1 data in tobacco, alcoholic and non-alcoholic beverage, pharmaceutical and grocery industries. The main implementation at this time is in tobacco industry.
Main advantages of DotCode are:
* Full support and replacement of Code 128 codeset;
* Compact encoding of 8-bit data array;
* Unicode support with Extended Channel Interpretation feature;
* Effective encoding of GS1 data;
* Reed–Solomon error correction
* Ability to apply the barcode by high-speed industrial printers and other methods like laser engraving.

## Barcode design

DotCode represents data in rectangular structure which consists from black round dots and white spaces on white background or white round dots on black background. DotCode does not have finder pattern, like other 2D barcodes and it must be detected with slow blob detection algorithms like Gabor filter or Circle Hough Transform. All data, metadata and error correction codewords are encoded in the same dots array which does not have any visual difference.
Here are some samples of DotCode:
* DotCode with 25 width and 10 height
* DotCode with 23 width and 22 height
* DotCode with 57 width and 60 height
DotCode symbol is constructed from the following elements:$^{:5.1}$
* Two mask bits which are represented on diagram as two green points;
* Data bits (data and error correction bits) which must be read from up to down (even barcode side) and from left to right (odd barcode side);
* Corner points, marked as red on diagram, which can be used as data bits or padding bits (black dots);
* Quiet zone at least 3X size of the dots.
The DotCode bits array is represented as: (Two mask bits: M2, M1)(Data bits)(Corner bits, can be data or padding bits: C1 – C6)
The data codewords in 0 – 112 range are encoded in 5-of-9 binary dot patterns$^{:5.2}$ which are encoded from 9 dots where 5 black dots and 4 white spaces. The rest of barcode matrix (rest from division on 9) is padded with black padding bits.$^{:5.2.3}$ The padding bits can be from 0 to 8. The logically DotCode bits array is represented as: (2 mask bits)(Data codewords 9 bits each)(Padding bits 0 – 8 bits)
DotCode size has the following requirements:$^{:5.1}$
* Sum of DotCode width and height must be odd: $(W+H){\pmod {2}}=1$;
* Minimal size of each side must be 5, maximal is not limited;
* It is recommended to use width and height to have at least 6 padding black bits where data bits capacity rest from division on 9 more or equal 6:$^{:5.2.1.4}$ $6<=(((W*H)/2)-2){\pmod {9}}$

### Data masking

To minimize DotCode problematic symbols, the data codewords are masked to create others visual sequences. The mask pattern is applied only to data sequence and does not affect error correction codewords. DotCode standard has 4 mask pattern which are codded into 2 bits and placed as the first 2 bits of symbol bits array.$^{:5.2.4}$
DotCode Mask selection bits :
| Mask | Bits | Masking operation Example:099 099 099 099 099 099 099 099 106 |
| 0 | 00 | Adds successive multiples of 0 to each value, i.e., no change (000) 099 099 099 099 099 099 099 099 106 |
| 1 | 01 | Adds successive multiples of 3 to each value, modulo 113 (001) 099 102 105 108 111 001 004 007 017 |
| 2 | 10 | Adds successive multiples of 7 to each value, modulo 113 (002) 099 106 000 007 014 021 028 035 049 |
| 3 | 11 | Adds successive multiples of 17 to each value, modulo 113 (003) 099 003 020 037 054 071 088 105 016 |

### Error correction

DotCode uses Reed–Solomon error correction$^{:5.3}$ with prime power of 3 and finite field $\mathbb {F} _{113}$ or GF(113). The data codewords is represented with values from 0 to 112 and mask value is counted as leading data codeword from 0 to 3. In this way the data protected array length is (1 + ND). But amount of error correction codewords is calculated only from ND: $NC=3+(ND/2)$, where ND is data codewords and NC - error correction codewords.
The resulting codewords NW with error correction codewords is: $NW=(1+ND)+NC$, where NW is all encoding codewords: 1 mask codeword + data codewords(ND) + error correction codewords(NC).
Because Reed–Solomon error correction cannot correct amount of codewords which are more than polynomial, if NW happens to exceed 112, the data is split into error correction blocks: $B=(NW+111)/112$, where B is block counts.
The data can be split into block in the following way, for each block ‘’’n’’’, for n equals 1 to B:
* $ND(block)=((1+ND)-(n-1)+(B-1))/B$
* $NW(block)=(NW-(n-1)+(B-1))/B$
* $NC(block)=NW(block)-ND(block)$
The error correction data $NC$ is written after single data block $ND$ in scrambled mode: (ND)(NC1_1)(NC2_1)(NC3_1)...(NC1_n)(NC2_m)(NC3_k)

## Encoding

DotCode encoding size is not limited by standard, but practical encoding size in 100x99 version which includes 4950 dots can encode 366 raw data codewords, 730 digits, 365 alphanumeric characters, or 304 bytes. The data message in DotCode is represented with data codewords from 0 to 112 which are encoded with 5-of-9 binary dot patterns.
DotCode supports the following features:$^{:5.2.1}$
* Natively encodes digits or ASCII charset (between 0 and 127) with A, B and C code sets and extended ASCII values (128 to 255) with Upper Shift;
* Effectively encodes bytes (5 bytes into 6 codewords) with Binary Latch;
* Encodes GS1 data;
* Encodes Unicode symbols with Extended Channel Interpretation feature;
* Supports Structured append encoding (logically using multiple barcode symbols as single data stream);
* Supports “Macro” encoding.
DotCode character set :
| Data symbol | Data symbol | Data symbol | Data symbol | Data symbol | Data symbol | Data symbol | Data symbol | Data symbol | Data symbol | Data symbol | Data symbol | Data symbol | Data symbol |
| Code Word | Code Set A | Code Set A | Code Set B | Code Set B | Code Set C | Dot Pattern | Code Word | Code Set A | Code Set A | Code Set B | Code Set B | Code Set C | Dot Pattern |
| Code Word | Char | ASCII | Char | ASCII | Code Set C | Dot Pattern | Code Word | Char | ASCII | Char | ASCII | Code Set C | Dot Pattern |
| 0 | SP | 32 | SP | 32 | 00 | 101010101 | 57 | Y | 89 | Y | 89 | 57 | 110101100 |
| 1 | ! | 33 | ! | 33 | 01 | 010101011 | 58 | Z | 90 | Z | 90 | 58 | 110110010 |
| 2 | " | 34 | " | 34 | 02 | 010101101 | 59 | [ | 91 | [ | 91 | 59 | 110110100 |
| 3 | # | 35 | # | 35 | 03 | 010110101 | 60 | \ | 92 | \ | 92 | 60 | 111001010 |
| 4 | $ | 36 | $ | 36 | 04 | 011010101 | 61 | ] | 93 | ] | 93 | 61 | 111010010 |
| 5 | % | 37 | % | 37 | 05 | 101010110 | 62 | ^ | 94 | ^ | 94 | 62 | 111010100 |
| 6 | & | 38 | & | 38 | 06 | 101011010 | 63 | _ | 95 | _ | 95 | 63 | 001011110 |
| 7 | ' | 39 | ' | 39 | 07 | 101101010 | 64 | NUL | 00 | ` | 96 | 64 | 001101110 |
| 8 | ( | 40 | ( | 40 | 08 | 110101010 | 65 | SOH | 01 | a | 97 | 65 | 001110110 |
| 9 | ) | 41 | ) | 41 | 09 | 010101110 | 66 | STX | 02 | b | 98 | 66 | 001111010 |
| 10 | * | 42 | * | 42 | 10 | 010110110 | 67 | ETX | 03 | c | 99 | 67 | 010011110 |
| 11 | + | 43 | + | 43 | 11 | 010111010 | 68 | EOT | 04 | d | 100 | 68 | 010111100 |
| 12 | , | 44 | , | 44 | 12 | 011010110 | 69 | ENQ | 05 | e | 101 | 69 | 011001110 |
| 13 | - | 45 | - | 45 | 13 | 011011010 | 70 | ACK | 06 | f | 102 | 70 | 011011100 |
| 14 | . | 46 | . | 46 | 14 | 011101010 | 71 | BEL | 07 | g | 103 | 71 | 011100110 |
| 15 | / | 47 | / | 47 | 15 | 100101011 | 72 | BS | 08 | h | 104 | 72 | 011101100 |
| 16 | 0 | 48 | 0 | 48 | 16 | 100101101 | 73 | HT | 09 | i | 105 | 73 | 011110010 |
| 17 | 1 | 49 | 1 | 49 | 17 | 100110101 | 74 | LF | 10 | j | 106 | 74 | 011110100 |
| 18 | 2 | 50 | 2 | 50 | 18 | 101001011 | 75 | VT | 11 | k | 107 | 75 | 100010111 |
| 19 | 3 | 51 | 3 | 51 | 19 | 101001101 | 76 | FF | 12 | l | 108 | 76 | 100011011 |
| 20 | 4 | 52 | 4 | 52 | 20 | 101010011 | 77 | CR | 13 | m | 109 | 77 | 100011101 |
| 21 | 5 | 53 | 5 | 53 | 21 | 101011001 | 78 | SO | 14 | n | 110 | 78 | 100100111 |
| 22 | 6 | 54 | 6 | 54 | 22 | 101100101 | 79 | SI | 15 | o | 111 | 79 | 100110011 |
| 23 | 7 | 55 | 7 | 55 | 23 | 101101001 | 80 | DLE | 16 | p | 112 | 80 | 100111001 |
| 24 | 8 | 56 | 8 | 56 | 24 | 110010101 | 81 | DC1 | 17 | q | 113 | 81 | 101000111 |
| 25 | 9 | 57 | 9 | 57 | 25 | 110100101 | 82 | DC2 | 18 | r | 114 | 82 | 101100011 |
| 26 | : | 58 | : | 58 | 26 | 110101001 | 83 | DC3 | 19 | s | 115 | 83 | 101110001 |
| 27 | ; | 59 | ; | 59 | 27 | 001010111 | 84 | DC4 | 20 | t | 116 | 84 | 110001011 |
| 28 | < | 60 | < | 60 | 28 | 001011011 | 85 | NAK | 21 | u | 117 | 85 | 110001101 |
| 29 | = | 61 | = | 61 | 29 | 001011101 | 86 | SYN | 22 | v | 118 | 86 | 110010011 |
| 30 | > | 62 | > | 62 | 30 | 001101011 | 87 | ETB | 23 | w | 119 | 87 | 110011001 |
| 31 | ? | 63 | ? | 63 | 31 | 001101101 | 88 | CAN | 24 | x | 120 | 88 | 110100011 |
| 32 | @ | 64 | @ | 64 | 32 | 001110101 | 89 | EM | 25 | y | 121 | 89 | 110110001 |
| 33 | A | 65 | A | 65 | 33 | 010010111 | 90 | SUB | 26 | z | 122 | 90 | 111000101 |
| 34 | B | 66 | B | 66 | 34 | 010011011 | 91 | ESC | 27 | { | 123 | 91 | 111001001 |
| 35 | C | 67 | C | 67 | 35 | 010011101 | 92 | FS | 28 | | | 124 | 92 | 111010001 |
| 36 | D | 68 | D | 68 | 36 | 010100111 | 93 | GS | 29||}||125||93||000101111 |
| 37 | E | 69 | E | 69 | 37 | 010110011 | 94 | RS | 30 | ~ | 126 | 94 | 000110111 |
| 38 | F | 70 | F | 70 | 38 | 010111001 | 95 | US | 31 | DEL | 127 | 95 | 000111011 |
| 39 | G | 71 | G | 71 | 39 | 011001011 | 96 | Shift B | Shift B | CR/LF | 13/10 | 96 | 000111101 |
| 40 | H | 72 | H | 72 | 40 | 011001101 | 97 | 2x Shift B | 2x Shift B | HT * | 09 | 97 | 001001111 |
| 41 | I | 73 | I | 73 | 41 | 011010011 | 98 | 3x Shift B | 3x Shift B | FS * | 28 | 98 | 001100111 |
| 42 | J | 74 | J | 74 | 42 | 011011001 | 99 | 4x Shift B | 4x Shift B | GS * | 29 | 99 | 001110011 |
| 43 | K | 75 | K | 75 | 43 | 011100101 | 100 | 5x Shift B | 5x Shift B | RS * | 30 | (17)...(10) | 001111001 |
| 44 | L | 76 | L | 76 | 44 | 011101001 | 101 | 6x Shift B | 6x Shift B | Shift A | Shift A | Latch A | 010001111 |
| 45 | M | 77 | M | 77 | 45 | 100101110 | 102 | Latch B | Latch B | Latch A | Latch A | Shift B | 011000111 |
| 46 | N | 78 | N | 78 | 46 | 100110110 | 103 | 2x Shift C | 2x Shift C | 2x Shift C | 2x Shift C | 2x Shift B | 011100011 |
| 47 | O | 79 | O | 79 | 47 | 100111010 | 104 | 3x Shift C | 3x Shift C | 3x Shift C | 3x Shift C | 3x Shift B | 011110001 |
| 48 | P | 80 | P | 80 | 48 | 101001110 | 105 | 4x Shift C | 4x Shift C | 4x Shift C | 4x Shift C | 4x Shift B | 100011110 |
| 49 | Q | 81 | Q | 81 | 49 | 101011100 | 106 | Latch C | Latch C | Latch C | Latch C | Latch B | 100111100 |
| 50 | R | 82 | R | 82 | 50 | 101100110 | 107 | FNC1 | FNC1 | FNC1 | FNC1 | FNC1 | 101111000 |
| 51 | S | 83 | S | 83 | 51 | 101101100 | 108 | FNC2 | FNC2 | FNC2 | FNC2 | FNC2 | 110001110 |
| 52 | T | 84 | T | 84 | 52 | 101110010 | 109 | FNC3 | FNC3 | FNC3 | FNC3 | FNC3 | 110011100 |
| 53 | U | 85 | U | 85 | 53 | 101110100 | 110 | Upper Shift A | Upper Shift A | Upper Shift A | Upper Shift A | Upper Shift A | 110111000 |
| 54 | V | 86 | V | 86 | 54 | 110010110 | 111 | Upper Shift B | Upper Shift B | Upper Shift B | Upper Shift B | Upper Shift B | 111000110 |
| 55 | W | 87 | W | 87 | 55 | 110011010 | 112 | Binary Latch | Binary Latch | Binary Latch | Binary Latch | Binary Latch | 111001100 |
| 56 | X | 88 | X | 88 | 56 | 110100110 | * in lead data positions, these become “Macros” | * in lead data positions, these become “Macros” | * in lead data positions, these become “Macros” | * in lead data positions, these become “Macros” | * in lead data positions, these become “Macros” | * in lead data positions, these become “Macros” | * in lead data positions, these become “Macros” |
There three main rules at message encoding start:
* The default starting encoding is Code Set C;
* In case of two digits in Code Set C (0 – 100 value) are encoded in the position of the first codeword, the message is decoded as GS1 message;
* If FNC1 (opposite effect to Code 128) or other non-digit codeword (101 - 112) is encoded in the position of the first codeword, the message is decoded as NON GS1 message.

### Binary byte encoding

DotCode can encode full 8-bit charset in two ways:$^{:5.2.1.1}$
* With Upper Shift, which requires 2 codewords on one (128 to 255) symbol;
* With Binary Latch, which requires 1 Binary Latch symbol and 6 codewords on every 5 bytes.
Upper Shift modes can encode (128 to 255) extended ASCII characters in two codewords with returning to previous mode:
DotCode Upper Shift encoding :
| Upper Shift Type | Codewords | ASCII |
| Upper Shift A | 64 to 95 | 128 to 159 |
| Upper Shift B | 0 to 95 | 160 to 255 |
Binary Latch mode can encode 8-bit charset and ECI sequences from 1 to 5 symbols. It uses the following rules:
* The data is split into block of 5 symbols (byte or ECI mode identifier) or 6 codewords;
* 0 – 258 values are radix converted from five base 259 into six base 103 values;
* 0 – 255 values are byte values;
* 256, 257 or 258 values marks to encode ECI sequence in next 1, 2 or 3 bytes respectively;
* Any DotCode codewords in the encoded sequence above 102 (from 103 to 112) interrupt or change mode.
DotCode Binary mode non-data codewords :
| Codeword | Operation |
| 103 | Interrupt for 2x Shift C |
| 104 | Interrupt for 3x Shift C |
| 105 | Interrupt for 4x Shift C |
| 106 | Interrupt for 5x Shift C |
| 107 | Interrupt for 6x Shift C |
| 108 | Interrupt for 7x Shift C |
| 109 | Terminate with Latch to A |
| 110 | Terminate with Latch to B |
| 111 | Terminate with Latch to C |
| 112 | Terminate with Symbol Separation, Latch to C |
As we see in the following table, Binary Latch encodes data more effectively, starting from 3 bytes.
DotCode Binary mode efficiency :
| Bytes count | Codewords required | Codewords with latch and return required | Upper Shift codewords required |
| 1 | 2 | 4 | 2 |
| 2 | 3 | 5 | 4 |
| 3 | 4 | 6 | 6 |
| 4 | 5 | 7 | 8 |
| 5 | 6 | 8 | 10 |

### ECI encoding

DotCode can encode ECI indicator int two ways:$^{:5.2.1.2}$
* In Binary Latch mode (reviewed upper);
* With FNC2 character.
FNC2 in any position except at the end of data signals the insertion of an ECI sequence – "\nnnnnn", which represents values between 000000 and 811799. The values can be encoded in 1 or 3 codewords:
* In case of next codeword < 40 directly encodes ECI value 000000 to 000039;
* Otherwise, the next three codewords valued A, B, & C encode an ECI value of $(A-40)*12769+B*113+C+40$.

### GS1 encoding

Any two digits in the position of the first codeword identify a symbol as GS1 encoded (opposite to Code 128). In case of symbol with two digits in the position of the first codeword must be decoded as ordinary data, the FNC1 (omitted in decoded message) must be inserted at the place of the first codeword.$^{:5.2.1.2}$ FNC1 in the other than the first position works as GS1 Application Identifier splitter and decoded as GS (ASCII value 29) character.
Codeword 100 in Code Set C encodes application GS1 AI (17) the next 3 codewords is an expiration date and inserts GS1 AI (10) before decoding other codewords: (100)(24)(12)(30)(56)(64) -> 17241230105664

### Macros mode

Some data codewords 97 – 100 in the lead data position in Code Set B can encode “Macros”.$^{:5.2.1.1}$ In any other position it encodes ASCII symbol: (Latch B)(HT) ->>RS05GS … RSEoT (Shift B)(HT) ->>RS05GS … RSEoT
DotCode Code Set B dual-function codewords :
| Codeword | In the first data position | In subsequent data positions | In subsequent data positions |
| Codeword | In the first data position | Char | ASCII |
| 97 |>RS05GS ... RSEoT | HT | 09 |
| 98 |>RS06GS ... RSEoT | FS | 28 |
| 99 |>RS12GS ... RSEoT | GS | 29 |
| 100 |>RSxx ... EoT where the two digits “xx” are a value between 00 and 99. | RS | 30 |
| 1.These “Macro” expansions include both a header & trailer for the data. 2.To encode HT, FS, GS or RS in the first data position, use Code Set A. 3.The Macro expansion for codeword 100 intentionally omits GS from the header and RS from the trailer because these may not be correct for some values of "xx". However GS and/or RS may still be encoded as needed. ||||

### Structured append

DotCode can create composite symbol, where data from multiple DotCode symbols can be logically united. This can be made with FNC2 symbol in last data position. When FNC2 is in the final data position,$^{:5.2.1.2}$ then the preceding two message characters, digits and uppercase letters in order 1 to 9 then A to Z (for values 10 to 35) shall as "m" and "n" designate where this message belongs in a "m out of n" sequence. As an example, a symbol whose message ends "4 B FNC2" shall be the 4th symbol out of 11 that comprise the entire message.

### Special modes encoding

FNC3 in the first codeword position indicates that the message$^{:5.2.1.2}$ is the instructions for initialization or reprogramming of the bar code reader.
FNC3 in any other position than first indicates that encoded message must be logically separated into two distinct messages (before and after it);

### Data padding

DotCode symbol codewords capacity is: $NW=((H*W)/2-2){\pmod {9}})$
DotCode symbol data codewords capacity is: $ND=(NW-3)-(NW-3)/3$
In this way we need to pad data codewords in case with have free space. There are two rules:$^{:5.2.3}$
* Binary mode must be terminated with Latch to Code Set A (codeword 109);
* In other modes codeword 106 (Latch to Code Set C / Latch to Code Set B) must be used.