1 /* dmatrix.h - Handles Data Matrix ECC 200 */
2 
3 /*
4     libzint - the open source barcode library
5     Copyright (C) 2009-2017 Robin Stuart <rstuart114@gmail.com>
6 
7     Redistribution and use in source and binary forms, with or without
8     modification, are permitted provided that the following conditions
9     are met:
10 
11     1. Redistributions of source code must retain the above copyright
12        notice, this list of conditions and the following disclaimer.
13     2. Redistributions in binary form must reproduce the above copyright
14        notice, this list of conditions and the following disclaimer in the
15        documentation and/or other materials provided with the distribution.
16     3. Neither the name of the project nor the names of its contributors
17        may be used to endorse or promote products derived from this software
18        without specific prior written permission.
19 
20     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23     ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30     SUCH DAMAGE.
31  */
32 
33 /*
34     Containes Extended Rectangular Data Matrix (DMRE)
35     See http://www.dmre.info for information
36     Contact: harald.oehlmann@eurodatacouncil.org
37  */
38 
39 #include "common.h"
40 
41 #ifndef __IEC16022ECC200_H
42 #define __IEC16022ECC200_H
43 #ifdef __cplusplus
44 extern "C" {
45 #endif /* __cplusplus */
46 
47 extern int data_matrix_200(struct zint_symbol *symbol, const unsigned char source[], const size_t length);
48 
49 #ifdef __cplusplus
50 }
51 #endif /* __cplusplus */
52 
53 #define MAXBARCODE 3116
54 
55 #define DM_NULL         0
56 #define DM_ASCII	1
57 #define DM_C40		2
58 #define DM_TEXT		3
59 #define DM_X12		4
60 #define DM_EDIFACT	5
61 #define DM_BASE256	6
62 
63 static const char c40_shift[] = {
64     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
65     0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66     2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67     2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
68 };
69 
70 static const char c40_value[] = {
71     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
72     3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
73     15, 16, 17, 18, 19, 20, 21, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
74     22, 23, 24, 25, 26, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
75 };
76 
77 static const char text_shift[] = {
78     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
79     0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80     2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
81     2, 2, 2, 2, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3
82 };
83 
84 static const char text_value[] = {
85     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
86     3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
87     15, 16, 17, 18, 19, 20, 21, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
88     22, 23, 24, 25, 26, 0, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 27, 28, 29, 30, 31
89 };
90 
91 // Position in option array [symbol option value - 1]
92 // The position in the option array is by increasing total data codewords with square first
93 // The last comment value is the total data codewords value.
94 // The index of this array is the --vers parameter value -1 and is given as forst comment value
95 
96 static const unsigned short int intsymbol[] = {
97      0, /*  1: 10x10 ,  3*/  1, /*  2: 12x12 ,  5*/  3, /*  3: 14x14 ,  8*/  5, /*  4: 16x16 , 12*/
98      7, /*  5: 18x18 , 18*/  9, /*  6: 20x20 , 22*/ 12, /*  7: 22x22 , 30*/ 14, /*  8: 24x24 , 36*/
99     16, /*  9: 26x26 , 44*/ 18, /* 10: 32x32 , 62*/ 21, /* 11: 36x36 , 86*/ 24, /* 12: 40x40 ,114*/
100     26, /* 13: 44x44 ,144*/ 27, /* 14: 48x48 ,174*/ 28, /* 15: 52x52 ,204*/ 29, /* 16: 64x64 ,280*/
101     30, /* 17: 72x72 ,368*/ 31, /* 18: 80x80 ,456*/ 32, /* 19: 88x88 ,576*/ 33, /* 20: 96x96 ,696*/
102     34, /* 21:104x104,816*/ 35, /* 22:120x120,1050*/36, /* 23:132x132,1304*/37, /* 24:144x144,1558*/
103      2, /* 25:  8x18 ,  5*/  4, /* 26:  8x32 , 10*/  6, /* 27: 12x26 , 16*/ 10, /* 28: 12x36 , 22*/
104     13, /* 29: 16x36 , 32*/ 17, /* 30: 16x48 , 49*/  8, /* 31:  8x48 , 18*/ 11, /* 32:  8x64 , 24*/
105     15, /* 33: 12x64 , 43*/ 19, /* 34: 16x64 , 62*/ 20, /* 37: 24x48 , 80*/ 23, /* 38: 24x64 ,108*/
106     22, /* 41: 26x48 , 90*/ 25, /* 42: 26x64 ,118*/
107     0
108 };
109 
110 // Number of DM Sizes
111 #define DMSIZESCOUNT 38
112 // Number of 144x144 for special interlace
113 #define INTSYMBOL144 37
114 
115 // Is the current code a DMRE code ?
116 // This is the case, if intsymbol index >= 30
117 
118 static const char isDMRE[] = {
119     /* 0*/ 0, /*  10x10, 3 */ 0, /* 12x12 , 5 */ 0, /*  8x18 , 5 */ 0, /* 14x14 , 8 */
120     /* 4*/ 0, /*  8x32 ,10 */ 0, /* 16x16 ,12 */ 0, /* 12x26 ,16 */ 0, /* 18x18 ,18 */
121     /* 8*/ 1, /*  8x48 ,18 */ 0, /* 20x20 ,22 */ 0, /* 12x36 ,22 */ 1, /*  8x64 ,24 */
122     /*12*/ 0, /* 22x22 ,30 */ 0, /* 16x36 ,32 */ 0, /* 24x24 ,36 */ 1, /* 12x64 ,43 */
123     /*16*/ 0, /* 26x26 ,44 */ 0, /* 16x48 ,49 */ 0, /* 32x32 ,62 */ 1, /* 16x64 ,62 */
124     /*20*/ 1, /* 24x48 ,80 */ 0, /* 36x36 ,86 */ 1, /* 26x48 ,90 */ 1, /* 24x64 ,108*/
125     /*24*/ 0, /* 40x40 ,114*/ 1, /* 26x64 ,118*/ 0, /* 44x44 ,144*/ 0, /* 48x48,174 */
126     /*28*/ 0, /* 52x52,204 */ 0, /* 64x64,280 */ 0, /* 72x72,368 */ 0, /* 80x80,456 */
127     /*32*/ 0, /* 88x88,576 */ 0, /* 96x96,696 */ 0, /*104x104,816*/ 0, /*120x120,1050*/
128     /*36*/ 0, /*132x132,1304*/0  /*144x144,1558*/
129 };
130 
131 // Horizontal matrix size
132 
133 static const unsigned short int matrixH[] = {
134     /* 0*/ 10, /* 10x10 , 3 */ 12, /* 12x12 , 5 */ 8, /*   8x18 , 5 */ 14, /* 14x14 , 8 */
135     /* 4*/  8, /*  8x32 ,10 */ 16, /* 16x16 ,12 */ 12, /* 12x26 ,16 */ 18, /* 18x18 ,18 */
136     /* 8*/  8, /*  8x48 ,18 */ 20, /* 20x20 ,22 */ 12, /* 12x36 ,22 */ 8, /*   8x64 ,24 */
137     /*12*/ 22, /* 22x22 ,30 */ 16, /* 16x36 ,32 */ 24, /* 24x24 ,36 */ 12, /* 12x64 ,43 */
138     /*16*/ 26, /* 26x26 ,44 */ 16, /* 16x48 ,49 */ 32, /* 32x32 ,62 */ 16, /* 16x64 ,62 */
139     /*20*/ 24, /* 24x48 ,80 */ 36, /* 36x36 ,86 */ 26, /* 26x48 ,90 */ 24, /* 24x64 ,108*/
140     /*24*/ 40, /* 40x40 ,114*/ 26, /* 26x64 ,118*/ 44, /* 44x44 ,144*/ 48, /* 48x48,174 */
141     /*28*/ 52, /* 52x52,204 */ 64, /* 64x64,280 */ 72, /* 72x72,368 */ 80, /* 80x80,456 */
142     /*32*/ 88, /* 88x88,576 */ 96, /* 96x96,696 */ 104, /*104x104,816*/ 120, /*120x120,1050*/
143     /*36*/ 132,/*132x132,1304*/144 /*144x144,1558*/
144 };
145 
146 // Vertical matrix sizes
147 
148 static const unsigned short int matrixW[] = {
149     /* 0*/ 10, /* 10x10 */ 12, /* 12x12 */ 18, /*  8x18 */ 14, /* 14x14 */
150     /* 4*/ 32, /*  8x32 */ 16, /* 16x16 */ 26, /* 12x26 */ 18, /* 18x18 */
151     /* 8*/ 48, /*  8x48 */ 20, /* 20x20 */ 36, /* 12x36 */ 64, /*  8x64 */
152     /*12*/ 22, /* 22x22 */ 36, /* 16x36 */ 24, /* 24x24 */ 64, /* 12x64 */
153     /*16*/ 26, /* 26x26 */ 48, /* 16x48 */ 32, /* 32x32 */ 64, /* 16x64 */
154     /*20*/ 48, /* 24x48 */ 36, /* 36x36 */ 48, /* 26x48 */ 64, /* 24x64 */
155     /*24*/ 40, /* 40x40 */ 64, /* 26x64 */ 44, /* 44x44 */ 48, /* 48x48 */
156     /*28*/ 52, /* 52x52 */ 64, /* 64x64 */ 72, /* 72x72 */ 80, /* 80x80 */
157     /*32*/ 88, /* 88x88 */ 96, /* 96x96 */ 104,/*104x104*/ 120,/*120x120*/
158     /*36*/132, /*132x132*/144  /*144x144*/
159 };
160 
161 // Horizontal submodule size (including subfinder)
162 
163 static const unsigned short int matrixFH[] = {
164     /* 0*/ 10, /* 10x10 */ 12, /* 12x12 */ 8, /*   8x18 */ 14, /* 14x14 */
165     /* 4*/  8, /*  8x32 */ 16, /* 16x16 */ 12, /* 12x26 */ 18, /* 18x18 */
166     /* 8*/  8, /*  8x48 */ 20, /* 20x20 */ 12, /* 12x36 */  8, /*  8x64 */
167     /*12*/ 22, /* 22x22 */ 16, /* 16x36 */ 24, /* 24x24 */ 12, /* 12x64 */
168     /*16*/ 26, /* 26x26 */ 16, /* 16x48 */ 16, /* 32x32 */ 16, /* 16x64 */
169     /*20*/ 24, /* 24x48 */ 18, /* 36x36 */ 26, /* 26x48 */ 24, /* 24x64 */
170     /*24*/ 20, /* 40x40 */ 26, /* 26x64 */ 22, /* 44x44 */ 24, /* 48x48 */
171     /*28*/ 26, /* 52x52 */ 16, /* 64x64 */ 18, /* 72x72 */ 20, /* 80x80 */
172     /*32*/ 22, /* 88x88 */ 24, /* 96x96 */ 26, /*104x104*/ 20, /*120x120*/
173     /*36*/ 22, /*132x132*/ 24  /*144x144*/
174 };
175 
176 // Vertical submodule size (including subfinder)
177 
178 static const unsigned short int matrixFW[] = {
179     /* 0*/ 10, /* 10x10 */ 12, /* 12x12 */ 18, /*  8x18 */ 14, /* 14x14 */
180     /* 4*/ 16, /*  8x32 */ 16, /* 16x16 */ 26, /* 12x26 */ 18, /* 18x18 */
181     /* 8*/ 24, /*  8x48 */ 20, /* 20x20 */ 18, /* 12x36 */ 16, /*  8x64 */
182     /*12*/ 22, /* 22x22 */ 18, /* 16x36 */ 24, /* 24x24 */ 16, /* 12x64 */
183     /*16*/ 26, /* 26x26 */ 24, /* 16x48 */ 16, /* 32x32 */ 16, /* 16x64 */
184     /*20*/ 24, /* 24x48 */ 18, /* 36x36 */ 24, /* 26x48 */ 16, /* 24x64 */
185     /*24*/ 20, /* 40x40 */ 16, /* 26x64 */ 22, /* 44x44 */ 24, /* 48x48 */
186     /*28*/ 26, /* 52x52 */ 16, /* 64x64 */ 18, /* 72x72 */ 20, /* 80x80 */
187     /*32*/ 22, /* 88x88 */ 24, /* 96x96 */ 26, /*104x104*/ 20, /*120x120*/
188     /*36*/ 22, /*132x132*/ 24  /*144x144*/
189 };
190 
191 // Total Data Codewords
192 
193 static const unsigned short int matrixbytes[] = {
194     /* 0*/   3, /* 10x10 */   5, /* 12x12 */   5, /*  8x18 */   8, /* 14x14 */
195     /* 4*/  10, /*  8x32 */  12, /* 16x16 */  16, /* 12x26 */  18, /* 18x18 */
196     /* 8*/  18, /*  8x48 */  22, /* 20x20 */  22, /* 12x36 */  24, /*  8x64 */
197     /*12*/  30, /* 22x22 */  32, /* 16x36 */  36, /* 24x24 */  43, /* 12x64 */
198     /*16*/  44, /* 26x26 */  49, /* 16x48 */  62, /* 32x32 */  62, /* 16x64 */
199     /*20*/  80, /* 24x48 */  86, /* 36x36 */  90, /* 26x48 */ 108, /* 24x64 */
200     /*24*/ 114, /* 40x40 */ 118, /* 26x64 */ 144, /* 44x44 */ 174, /* 48x48 */
201     /*28*/ 204, /* 52x52 */ 280, /* 64x64 */ 368, /* 72x72 */ 456, /* 80x80 */
202     /*32*/ 576, /* 88x88 */ 696, /* 96x96 */ 816, /*104x104*/1050, /*120x120*/
203     /*36*/1304, /*132x132*/1558 /*144x144*/
204 };
205 
206 // Data Codewords per RS-Block
207 
208 static const unsigned short int matrixdatablock[] = {
209     /* 0*/   3, /* 10x10 */   5, /* 12x12 */   5, /*  8x18 */   8, /* 14x14 */
210     /* 4*/  10, /*  8x32 */  12, /* 16x16 */  16, /* 12x26 */  18, /* 18x18 */
211     /* 8*/  18, /*  8x48 */  22, /* 20x20 */  22, /* 12x36 */  24, /*  8x64 */
212     /*12*/  30, /* 22x22 */  32, /* 16x36 */  36, /* 24x24 */  43, /* 12x64 */
213     /*16*/  44, /* 26x26 */  49, /* 16x48 */  62, /* 32x32 */  62, /* 16x64 */
214     /*20*/  80, /* 24x48 */  86, /* 36x36 */  90, /* 26x48 */ 108, /* 24x64 */
215     /*24*/ 114, /* 40x40 */ 118, /* 26x64 */ 144, /* 44x44 */ 174, /* 48x48 */
216     /*28*/ 102, /* 52x52 */ 140, /* 64x64 */  92, /* 72x72 */ 114, /* 80x80 */
217     /*32*/ 144, /* 88x88 */ 174, /* 96x96 */ 136, /*104x104*/ 175, /*120x120*/
218     /*36*/ 163, /*132x132*/ 156 /* 144x144*/
219 };
220 
221 // ECC Codewords per RS-Block
222 
223 static const unsigned short int matrixrsblock[] = {
224     /* 0*/  5, /* 10x10 */  7, /* 12x12 */  7, /*  8x18 */ 10, /* 14x14 */
225     /* 4*/ 11, /*  8x32 */ 12, /* 16x16 */ 14, /* 12x26 */ 14, /* 18x18 */
226     /* 8*/ 15, /*  8x48 */ 18, /* 20x20 */ 18, /* 12x36 */ 18, /*  8x64 */
227     /*12*/ 20, /* 22x22 */ 24, /* 16x36 */ 24, /* 24x24 */ 27, /* 12x64 */
228     /*16*/ 28, /* 26x26 */ 28, /* 16x48 */ 36, /* 32x32 */ 36, /* 16x64 */
229     /*20*/ 41, /* 24x48 */ 42, /* 36x36 */ 42, /* 26x48 */ 46, /* 24x64 */
230     /*24*/ 48, /* 40x40 */ 50, /* 26x64 */ 56, /* 44x44 */ 68, /* 48x48 */
231     /*28*/ 42, /* 52x52 */ 56, /* 64x64 */ 36, /* 72x72 */ 48, /* 80x80 */
232     /*32*/ 56, /* 88x88 */ 68, /* 96x96 */ 56, /*104x104*/ 68, /*120x120*/
233     /*36*/ 62, /*132x132*/ 62 /*144x144*/
234 };
235 
236 
237 #endif
238