Leptonica  1.82.0
Image processing and image analysis suite
readbarcode.h
1 /*====================================================================*
2  - Copyright (C) 2001 Leptonica. All rights reserved.
3  -
4  - Redistribution and use in source and binary forms, with or without
5  - modification, are permitted provided that the following conditions
6  - are met:
7  - 1. Redistributions of source code must retain the above copyright
8  - notice, this list of conditions and the following disclaimer.
9  - 2. Redistributions in binary form must reproduce the above
10  - copyright notice, this list of conditions and the following
11  - disclaimer in the documentation and/or other materials
12  - provided with the distribution.
13  -
14  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18  - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *====================================================================*/
26 
27 #ifndef LEPTONICA_READBARCODE_H
28 #define LEPTONICA_READBARCODE_H
29 
30  /* ----------------------------------------------------------------- *
31  * Flags for method of extracting barcode widths *
32  * ----------------------------------------------------------------- */
33 
35 enum {
36  L_USE_WIDTHS = 1,
37  L_USE_WINDOWS = 2
38 };
39 
40  /* ----------------------------------------------------------------- *
41  * Flags for barcode formats *
42  * These are used both to identify a barcode format and to identify *
43  * the decoding method to use on a barcode. *
44  * ----------------------------------------------------------------- */
45 
47 enum {
48  L_BF_UNKNOWN = 0,
49  L_BF_ANY = 1,
50  L_BF_CODE128 = 2,
51  L_BF_EAN8 = 3,
52  L_BF_EAN13 = 4,
53  L_BF_CODE2OF5 = 5,
54  L_BF_CODEI2OF5 = 6,
55  L_BF_CODE39 = 7,
56  L_BF_CODE93 = 8,
57  L_BF_CODABAR = 9,
58  L_BF_UPCA = 10
59 };
60 
61  /* ----------------------------------------------------------------- *
62  * Currently supported formats *
63  * Update these arrays as new formats are added. *
64  * ----------------------------------------------------------------- */
65 
67 static const l_int32 SupportedBarcodeFormat[] = {
68  L_BF_CODE2OF5,
69  L_BF_CODEI2OF5,
70  L_BF_CODE93,
71  L_BF_CODE39,
72  L_BF_CODABAR,
73  L_BF_UPCA,
74  L_BF_EAN13
75 };
76 
78 static const char *SupportedBarcodeFormatName[] = {
79  "Code2of5",
80  "CodeI2of5",
81  "Code93",
82  "Code39",
83  "Codabar",
84  "Upca",
85  "Ean13"
86 };
87 static const l_int32 NumSupportedBarcodeFormats = 7;
90  /* ----------------------------------------------------------------- *
91  * Code 2 of 5 symbology *
92  * ----------------------------------------------------------------- */
93 static const char *Code2of5[] = {
94  "111121211", "211111112", "112111112", "212111111", /* 0 - 3 */
95  "111121112", "211121111", "112121111", "111111212", /* 4 - 7 */
96  "211111211", "112111211", /* 8 - 9 */
97  "21211", "21112" /* Start, Stop */
98 };
99 
100 static const l_int32 C25_START = 10;
101 static const l_int32 C25_STOP = 11;
102 
103 
104  /* ----------------------------------------------------------------- *
105  * Code Interleaved 2 of 5 symbology *
106  * ----------------------------------------------------------------- */
107 static const char *CodeI2of5[] = {
108  "11221", "21112", "12112", "22111", "11212", /* 0 - 4 */
109  "21211", "12211", "11122", "21121", "12121", /* 5 - 9 */
110  "1111", "211" /* start, stop */
111 };
112 
113 static const l_int32 CI25_START = 10;
114 static const l_int32 CI25_STOP = 11;
115 
116 
117  /* ----------------------------------------------------------------- *
118  * Code 93 symbology *
119  * ----------------------------------------------------------------- */
120 static const char *Code93[] = {
121  "131112", "111213", "111312", "111411", "121113", /* 0: 0 - 4 */
122  "121212", "121311", "111114", "131211", "141111", /* 5: 5 - 9 */
123  "211113", "211212", "211311", "221112", "221211", /* 10: A - E */
124  "231111", "112113", "112212", "112311", "122112", /* 15: F - J */
125  "132111", "111123", "111222", "111321", "121122", /* 20: K - O */
126  "131121", "212112", "212211", "211122", "211221", /* 25: P - T */
127  "221121", "222111", "112122", "112221", "122121", /* 30: U - Y */
128  "123111", "121131", "311112", "311211", "321111", /* 35: Z,-,.,SP,$ */
129  "112131", "113121", "211131", "131221", "312111", /* 40: /,+,%,($),(%) */
130  "311121", "122211", "111141" /* 45: (/),(+), Start */
131 };
132 
133  /* Use "[]{}#" to represent special codes 43-47 */
134 static const char Code93Val[] =
135  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%[]{}#";
136 
137 static const l_int32 C93_START = 47;
138 static const l_int32 C93_STOP = 47;
139 
140 
141  /* ----------------------------------------------------------------- *
142  * Code 39 symbology *
143  * ----------------------------------------------------------------- */
144 static const char *Code39[] = {
145  "111221211", "211211112", "112211112", "212211111", /* 0: 0 - 3 */
146  "111221112", "211221111", "112221111", "111211212", /* 4: 4 - 7 */
147  "211211211", "112211211", "211112112", "112112112", /* 8: 8 - B */
148  "212112111", "111122112", "211122111", "112122111", /* 12: C - F */
149  "111112212", "211112211", "112112211", "111122211", /* 16: G - J */
150  "211111122", "112111122", "212111121", "111121122", /* 20: K - N */
151  "211121121", "112121121", "111111222", "211111221", /* 24: O - R */
152  "112111221", "111121221", "221111112", "122111112", /* 28: S - V */
153  "222111111", "121121112", "221121111", "122121111", /* 32: W - Z */
154  "121111212", "221111211", "122111211", "121212111", /* 36: -,.,SP,$ */
155  "121211121", "121112121", "111212121", "121121211" /* 40: /,+,%,* */
156 };
157 
158  /* Use "*" to represent the Start and Stop codes (43) */
159 static const char Code39Val[] =
160  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*";
161 
162 static const l_int32 C39_START = 43;
163 static const l_int32 C39_STOP = 43;
164 
165 
166  /* ----------------------------------------------------------------- *
167  * Codabar symbology *
168  * ----------------------------------------------------------------- */
169 static const char *Codabar[] = {
170  "1111122", "1111221", "1112112", "2211111", "1121121", /* 0: 0 - 4 */
171  "2111121", "1211112", "1211211", "1221111", "2112111", /* 5: 5 - 9 */
172  "1112211", "1122111", "2111212", "2121112", "2121211", /* 10: -,$,:,/,. */
173  "1121212", "1122121", "1212112", "1112122", "1112221" /* 15: +,A,B,C,D */
174 };
175 
176  /* Ascii representations for codes 16-19: (A or T), (B or N), (C or *),
177  * (D or E). These are used in pairs for the Start and Stop codes. */
178 static const char CodabarVal[] = "0123456789-$:/.+ABCD";
179 
180 
181  /* ----------------------------------------------------------------- *
182  * UPC-A symbology *
183  * ----------------------------------------------------------------- */
184 static const char *Upca[] = {
185  "3211", "2221", "2122", "1411", "1132", /* 0: 0 - 4 */
186  "1231", "1114", "1312", "1213", "3112", /* 5: 5 - 9 */
187  "111", "111", "11111" /* 10: Start, Stop, Mid */
188 };
189 
190 static const l_int32 UPCA_START = 10;
191 static const l_int32 UPCA_STOP = 11;
192 static const l_int32 UPCA_MID = 12;
193 
194 
195  /* ----------------------------------------------------------------- *
196  * Code128 symbology *
197  * ----------------------------------------------------------------- */
198 static const char *Code128[] = {
199  "212222", "222122", "222221", "121223", "121322", /* 0 - 4 */
200  "131222", "122213", "122312", "132212", "221213", /* 5 - 9 */
201  "221312", "231212", "112232", "122132", "122231", /* 10 - 14 */
202  "113222", "123122", "123221", "223211", "221132", /* 15 - 19 */
203  "221231", "213212", "223112", "312131", "311222", /* 20 - 24 */
204  "321122", "321221", "312212", "322112", "322211", /* 25 - 29 */
205  "212123", "212321", "232121", "111323", "131123", /* 30 - 34 */
206  "131321", "112313", "132113", "132311", "211313", /* 35 - 39 */
207  "231113", "231311", "112133", "112331", "132131", /* 40 - 44 */
208  "113123", "113321", "133121", "313121", "211331", /* 45 - 49 */
209  "231131", "213113", "213311", "213131", "311123", /* 50 - 54 */
210  "311321", "331121", "312113", "312311", "332111", /* 55 - 59 */
211  "314111", "221411", "431111", "111224", "111422", /* 60 - 64 */
212  "121124", "121421", "141122", "141221", "112214", /* 65 - 69 */
213  "112412", "122114", "122411", "142112", "142211", /* 70 - 74 */
214  "241211", "221114", "413111", "241112", "134111", /* 75 - 79 */
215  "111242", "121142", "121241", "114212", "124112", /* 80 - 84 */
216  "124211", "411212", "421112", "421211", "212141", /* 85 - 89 */
217  "214121", "412121", "111143", "111341", "131141", /* 90 - 94 */
218  "114113", "114311", "411113", "411311", "113141", /* 95 - 99 */
219  "114131", "311141", "411131", "211412", "211214", /* 100 - 104 */
220  "211232", "2331112" /* 105 - 106 */
221 };
222 
223 static const l_int32 C128_FUN_3 = 96; /* in A or B only; in C it is 96 */
224 static const l_int32 C128_FUNC_2 = 97; /* in A or B only; in C it is 97 */
225 static const l_int32 C128_SHIFT = 98; /* in A or B only; in C it is 98 */
226 static const l_int32 C128_GOTO_C = 99; /* in A or B only; in C it is 99 */
227 static const l_int32 C128_GOTO_B = 100;
228 static const l_int32 C128_GOTO_A = 101;
229 static const l_int32 C128_FUNC_1 = 102;
230 static const l_int32 C128_START_A = 103;
231 static const l_int32 C128_START_B = 104;
232 static const l_int32 C128_START_C = 105;
233 static const l_int32 C128_STOP = 106;
234  /* code 128 symbols are 11 units */
235 static const l_int32 C128_SYMBOL_WIDTH = 11;
236 
237 
238 
239 #endif /* LEPTONICA_READBARCODE_H */