3 * IEC16022 bar code generation
4 * Adrian Kennard, Andrews & Arnold Ltd
5 * with help from Cliff Hones on the RS coding
7 * (c) 2004 Adrian Kennard, Andrews & Arnold Ltd
8 * (c) 2006 Stefan Schmidt <stefan@datenfreihafen.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "iec16022ecc200.h"
36 static struct ecc200matrix_s {
40 int datablock, rsblock;
42 10, 10, 10, 10, 3, 3, 5, //
43 12, 12, 12, 12, 5, 5, 7, //
44 8, 18, 8, 18, 5, 5, 7, //
45 14, 14, 14, 14, 8, 8, 10, //
46 8, 32, 8, 16, 10, 10, 11, //
47 16, 16, 16, 16, 12, 12, 12, //
48 12, 26, 12, 26, 16, 16, 14, //
49 18, 18, 18, 18, 18, 18, 14, //
50 20, 20, 20, 20, 22, 22, 18, //
51 12, 36, 12, 18, 22, 22, 18, //
52 22, 22, 22, 22, 30, 30, 20, //
53 16, 36, 16, 18, 32, 32, 24, //
54 24, 24, 24, 24, 36, 36, 24, //
55 26, 26, 26, 26, 44, 44, 28, //
56 16, 48, 16, 24, 49, 49, 28, //
57 32, 32, 16, 16, 62, 62, 36, //
58 36, 36, 18, 18, 86, 86, 42, //
59 40, 40, 20, 20, 114, 114, 48, //
60 44, 44, 22, 22, 144, 144, 56, //
61 48, 48, 24, 24, 174, 174, 68, //
62 52, 52, 26, 26, 204, 102, 42, //
63 64, 64, 16, 16, 280, 140, 56, //
64 72, 72, 18, 18, 368, 92, 36, //
65 80, 80, 20, 20, 456, 114, 48, //
66 88, 88, 22, 22, 576, 144, 56, //
67 96, 96, 24, 24, 696, 174, 68, //
68 104, 104, 26, 26, 816, 136, 56, //
69 120, 120, 20, 20, 1050, 175, 68, //
70 132, 132, 22, 22, 1304, 163, 62, //
71 144, 144, 24, 24, 1558, 156, 62, // 156*4+155*2
75 // simple checked response malloc
76 static void *safemalloc(int n)
80 fprintf(stderr, "Malloc(%d) failed\n", n);
86 // Annex M placement alorithm low level
87 static void ecc200placementbit(int *array, int NR, int NC, int r, int c,
92 c += 4 - ((NR + 4) % 8);
96 r += 4 - ((NC + 4) % 8);
98 array[r * NC + c] = (p << 3) + b;
101 static void ecc200placementblock(int *array, int NR, int NC, int r,
104 ecc200placementbit(array, NR, NC, r - 2, c - 2, p, 7);
105 ecc200placementbit(array, NR, NC, r - 2, c - 1, p, 6);
106 ecc200placementbit(array, NR, NC, r - 1, c - 2, p, 5);
107 ecc200placementbit(array, NR, NC, r - 1, c - 1, p, 4);
108 ecc200placementbit(array, NR, NC, r - 1, c - 0, p, 3);
109 ecc200placementbit(array, NR, NC, r - 0, c - 2, p, 2);
110 ecc200placementbit(array, NR, NC, r - 0, c - 1, p, 1);
111 ecc200placementbit(array, NR, NC, r - 0, c - 0, p, 0);
114 static void ecc200placementcornerA(int *array, int NR, int NC, int p)
116 ecc200placementbit(array, NR, NC, NR - 1, 0, p, 7);
117 ecc200placementbit(array, NR, NC, NR - 1, 1, p, 6);
118 ecc200placementbit(array, NR, NC, NR - 1, 2, p, 5);
119 ecc200placementbit(array, NR, NC, 0, NC - 2, p, 4);
120 ecc200placementbit(array, NR, NC, 0, NC - 1, p, 3);
121 ecc200placementbit(array, NR, NC, 1, NC - 1, p, 2);
122 ecc200placementbit(array, NR, NC, 2, NC - 1, p, 1);
123 ecc200placementbit(array, NR, NC, 3, NC - 1, p, 0);
126 static void ecc200placementcornerB(int *array, int NR, int NC, int p)
128 ecc200placementbit(array, NR, NC, NR - 3, 0, p, 7);
129 ecc200placementbit(array, NR, NC, NR - 2, 0, p, 6);
130 ecc200placementbit(array, NR, NC, NR - 1, 0, p, 5);
131 ecc200placementbit(array, NR, NC, 0, NC - 4, p, 4);
132 ecc200placementbit(array, NR, NC, 0, NC - 3, p, 3);
133 ecc200placementbit(array, NR, NC, 0, NC - 2, p, 2);
134 ecc200placementbit(array, NR, NC, 0, NC - 1, p, 1);
135 ecc200placementbit(array, NR, NC, 1, NC - 1, p, 0);
138 static void ecc200placementcornerC(int *array, int NR, int NC, int p)
140 ecc200placementbit(array, NR, NC, NR - 3, 0, p, 7);
141 ecc200placementbit(array, NR, NC, NR - 2, 0, p, 6);
142 ecc200placementbit(array, NR, NC, NR - 1, 0, p, 5);
143 ecc200placementbit(array, NR, NC, 0, NC - 2, p, 4);
144 ecc200placementbit(array, NR, NC, 0, NC - 1, p, 3);
145 ecc200placementbit(array, NR, NC, 1, NC - 1, p, 2);
146 ecc200placementbit(array, NR, NC, 2, NC - 1, p, 1);
147 ecc200placementbit(array, NR, NC, 3, NC - 1, p, 0);
150 static void ecc200placementcornerD(int *array, int NR, int NC, int p)
152 ecc200placementbit(array, NR, NC, NR - 1, 0, p, 7);
153 ecc200placementbit(array, NR, NC, NR - 1, NC - 1, p, 6);
154 ecc200placementbit(array, NR, NC, 0, NC - 3, p, 5);
155 ecc200placementbit(array, NR, NC, 0, NC - 2, p, 4);
156 ecc200placementbit(array, NR, NC, 0, NC - 1, p, 3);
157 ecc200placementbit(array, NR, NC, 1, NC - 3, p, 2);
158 ecc200placementbit(array, NR, NC, 1, NC - 2, p, 1);
159 ecc200placementbit(array, NR, NC, 1, NC - 1, p, 0);
162 // Annex M placement alorithm main function
163 static void ecc200placement(int *array, int NR, int NC)
167 for (r = 0; r < NR; r++)
168 for (c = 0; c < NC; c++)
169 array[r * NC + c] = 0;
177 ecc200placementcornerA(array, NR, NC, p++);
178 if (r == NR - 2 && !c && NC % 4)
179 ecc200placementcornerB(array, NR, NC, p++);
180 if (r == NR - 2 && !c && (NC % 8) == 4)
181 ecc200placementcornerC(array, NR, NC, p++);
182 if (r == NR + 4 && c == 2 && !(NC % 8))
183 ecc200placementcornerD(array, NR, NC, p++);
186 if (r < NR && c >= 0 && !array[r * NC + c])
187 ecc200placementblock(array, NR, NC, r, c, p++);
191 while (r >= 0 && c < NC);
196 if (r >= 0 && c < NC && !array[r * NC + c])
197 ecc200placementblock(array, NR, NC, r, c, p++);
201 while (r < NR && c >= 0);
205 while (r < NR || c < NC);
207 if (!array[NR * NC - 1])
208 array[NR * NC - 1] = array[NR * NC - NC - 2] = 1;
211 // calculate and append ecc code, and if necessary interleave
212 static void ecc200(unsigned char *binary, int bytes, int datablock, int rsblock)
214 int blocks = (bytes + 2) / datablock, b;
216 rs_init_code(rsblock, 1);
217 for (b = 0; b < blocks; b++) {
218 unsigned char buf[256], ecc[256];
220 for (n = b; n < bytes; n += blocks)
221 buf[p++] = binary[n];
222 rs_encode(p, buf, ecc);
223 p = rsblock - 1; // comes back reversed
224 for (n = b; n < rsblock * blocks; n += blocks)
225 binary[bytes + n] = ecc[p--];
230 * perform encoding for ecc200, source s len sl, to target t len tl, using
231 * optional encoding control string e return 1 if OK, 0 if failed. Does all
232 * necessary padding to tl
235 char ecc200encode(unsigned char *t, int tl, unsigned char *s, int sl,
236 char *encoding, int *lenp)
238 char enc = 'a'; // start in ASCII encoding mode
240 if (strlen(encoding) < sl) {
241 fprintf(stderr, "Encoding string too short\n");
245 while (sp < sl && tp < tl) {
246 char newenc = enc; // suggest new encoding
247 if (tl - tp <= 1 && (enc == 'c' || enc == 't') || tl - tp <= 2
249 enc = 'a'; // auto revert to ASCII
250 newenc = tolower(encoding[sp]);
251 switch (newenc) { // encode character
258 *s2 = "!\"#$%&'()*+,-./:;<=>?@[\\]_",
261 e = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
262 s3 = "`abcdefghijklmnopqrstuvwxyz{|}~\177";
265 e = " 0123456789abcdefghijklmnopqrstuvwxyz";
266 s3 = "`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~\177";
269 e = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\r*>";
271 unsigned char c = s[sp++];
276 "Cannot encode char 0x%02X in X12\n",
286 out[p++] = ((w - e) + 3) % 40;
290 "Cannot encode char 0x%02X in X12\n",
294 if (c < 32) { // shift 1
316 "Could not encode 0x%02X, should \
324 if (p == 2 && tp + 2 == tl && sp == sl)
325 out[p++] = 0; // shift 1 pad at end
329 out[1] * 40 + out[2] + 1;
334 t[tp++] = 254; // escape C40/text/X12
336 t[tp++] = 0x7C; // escape EDIFACT
346 t[tp++] = (v & 0xFF);
353 while (p && sp < sl);
358 unsigned char out[4], p = 0;
359 if (enc != newenc) { // can only be from C40/Text/X12
363 while (sp < sl && tolower(encoding[sp]) == 'e'
370 t[tp] = ((s[0] & 0x3F) << 2);
371 t[tp++] |= ((s[1] & 0x30) >> 4);
372 t[tp] = ((s[1] & 0x0F) << 4);
376 t[tp++] |= ((s[2] & 0x3C) >> 2);
377 t[tp] = ((s[2] & 0x03) << 6);
378 t[tp++] |= (s[3] & 0x3F);
384 if (enc == 'c' || enc == 't' || enc == 'x')
385 t[tp++] = 254; // escape C40/text/X12
387 t[tp++] = 0x7C; // escape EDIFACT
390 if (sl - sp >= 2 && isdigit(s[sp])
391 && isdigit(s[sp + 1])) {
393 (s[sp] - '0') * 10 + s[sp + 1] - '0' + 130;
395 } else if (s[sp] > 127) {
397 t[tp++] = s[sp++] - 127;
399 t[tp++] = s[sp++] + 1;
403 int l = 0; // how much to encode
408 && tolower(encoding[p]) == 'b';
412 t[tp++] = 231; // base256
416 t[tp++] = 249 + (l / 250);
419 while (l-- && tp < tl) {
420 t[tp] = s[sp++] + (((tp + 1) * 149) % 255) + 1; // see annex H
423 enc = 'a'; // reverse to ASCII at end
427 fprintf(stderr, "Unknown encoding %c\n", newenc);
433 if (tp < tl && enc != 'a') {
434 if (enc == 'c' || enc == 'x' || enc == 't')
435 t[tp++] = 254; // escape X12/C40/Text
437 t[tp++] = 0x7C; // escape EDIFACT
440 t[tp++] = 129; // pad
441 while (tp < tl) { // more padding
442 int v = 129 + (((tp + 1) * 149) % 253) + 1; // see Annex H
447 if (tp > tl || sp < sl)
448 return 0; // did not fit
450 * for (tp = 0; tp < tl; tp++) fprintf (stderr, "%02X ", t[tp]); \
451 * fprintf (stderr, "\n");
456 // Auto encoding format functions
457 static char encchr[] = "ACTXEB";
469 unsigned char switchcost[E_MAX][E_MAX] = {
470 0, 1, 1, 1, 1, 2, // From E_ASCII
471 1, 0, 2, 2, 2, 3, // From E_C40
472 1, 2, 0, 2, 2, 3, // From E_TEXT
473 1, 2, 2, 0, 2, 3, // From E_X12
474 1, 2, 2, 2, 0, 3, // From E_EDIFACT
475 0, 1, 1, 1, 1, 0, // From E_BINARY
479 * Creates a encoding list (malloc)
480 * returns encoding string
481 * if lenp not null, target len stored
482 * if error, null returned
483 * if exact specified, then assumes shortcuts applicable for exact fit
485 * 1. No unlatch to return to ASCII for last encoded byte after C40 or
487 * 2. No unlatch to return to ASCII for last 1 or 2 encoded bytes after
489 * 3. Final C40 or text encoding exactly in last 2 bytes can have a shift
490 * 0 to pad to make a tripple
491 * Only use the encoding from an exact request if the len matches the target,
492 * otherwise free the result and try again with exact=0
495 static char *encmake(int l, unsigned char *s, int *lenp, char exact)
501 // number of bytes of source that can be encoded in a row at this point
502 // using this encoding mode
504 // number of bytes of target generated encoding from this point to end if
505 // already in this encoding mode
507 } enc[MAXBARCODE][E_MAX];
508 memset(&enc, 0, sizeof(enc));
510 return ""; // no length
512 return 0; // not valid
516 // consider each encoding from this point
519 if (isdigit(s[p]) && p + 1 < l && isdigit(s[p + 1]))
520 sl = 2; // double digit
521 else if (s[p] & 0x80)
522 tl = 2; // high shifted
525 for (e = 0; e < E_MAX; e++)
526 if (enc[p + sl][e].t && ((t = enc[p + sl][e].t +
532 enc[p][E_ASCII].t = tl + bl;
533 enc[p][E_ASCII].s = sl;
534 if (bl && b == E_ASCII)
535 enc[p][b].s += enc[p + sl][b].s;
539 unsigned char c = s[p + sl++];
540 if (c & 0x80) { // shift + upper
544 if (c != ' ' && !isdigit(c) && !isupper(c))
551 } while (sub && p + sl < l);
552 if (exact && sub == 2 && p + sl == l) {
553 // special case, can encode last block with shift 0 at end (Is this
554 // valid when not end of target buffer?)
558 if (!sub) { // can encode C40
561 for (e = 0; e < E_MAX; e++)
566 switchcost[E_C40][e]) < bl
571 if (exact && enc[p + sl][E_ASCII].t == 1 && 1 < bl) {
572 // special case, switch to ASCII for last bytes
576 enc[p][E_C40].t = tl + bl;
577 enc[p][E_C40].s = sl;
578 if (bl && b == E_C40)
579 enc[p][b].s += enc[p + sl][b].s;
584 unsigned char c = s[p + sl++];
585 if (c & 0x80) { // shift + upper
589 if (c != ' ' && !isdigit(c) && !islower(c))
596 } while (sub && p + sl < l);
597 if (exact && sub == 2 && p + sl == l) {
598 // special case, can encode last block with shift 0 at end (Is this
599 // valid when not end of target buffer?)
603 if (!sub && sl) { // can encode Text
606 for (e = 0; e < E_MAX; e++)
611 switchcost[E_TEXT][e]) < bl
616 if (exact && enc[p + sl][E_ASCII].t == 1 && 1 < bl) { // special case, switch to ASCII for last bytes
620 enc[p][E_TEXT].t = tl + bl;
621 enc[p][E_TEXT].s = sl;
622 if (bl && b == E_TEXT)
623 enc[p][b].s += enc[p + sl][b].s;
628 unsigned char c = s[p + sl++];
629 if (c != 13 && c != '*' && c != '>' && c != ' '
630 && !isdigit(c) && !isupper(c)) {
639 } while (sub && p + sl < l);
640 if (!sub && sl) { // can encode X12
643 for (e = 0; e < E_MAX; e++)
648 switchcost[E_X12][e]) < bl
653 if (exact && enc[p + sl][E_ASCII].t == 1 && 1 < bl) {
654 // special case, switch to ASCII for last bytes
658 enc[p][E_X12].t = tl + bl;
659 enc[p][E_X12].s = sl;
660 if (bl && b == E_X12)
661 enc[p][b].s += enc[p + sl][b].s;
665 if (s[p + 0] >= 32 && s[p + 0] <= 94) { // can encode 1
667 if (p + 1 == l && (!bl || bl < 2)) {
671 for (e = 0; e < E_MAX; e++)
672 if (e != E_EDIFACT && enc[p + 1][e].t
675 2 + enc[p + 1][e].t +
676 switchcost[E_ASCII][e])
677 < bl || !bl)) // E_ASCII as allowed for unlatch
683 if (p + 1 < l && s[p + 1] >= 32 && s[p + 1] <= 94) { // can encode 2
684 if (p + 2 == l && (!bl || bl < 2)) {
688 for (e = 0; e < E_MAX; e++)
693 3 + enc[p + 2][e].t +
694 switchcost[E_ASCII][e])
695 < bl || !bl)) // E_ASCII as allowed for unlatch
701 if (p + 2 < l && s[p + 2] >= 32 && s[p + 2] <= 94) { // can encode 3
702 if (p + 3 == l && (!bl || bl < 3)) {
706 for (e = 0; e < E_MAX; e++)
715 < bl || !bl)) // E_ASCII as allowed for unlatch
721 if (p + 4 < l && s[p + 3] >= 32 && s[p + 3] <= 94) { // can encode 4
723 && (!bl || bl < 3)) {
727 for (e = 0; e < E_MAX;
756 // special case, switch to ASCII for last 1 ot two bytes
765 enc[p][E_EDIFACT].t = bl;
766 enc[p][E_EDIFACT].s = bs;
767 if (bl && b == E_EDIFACT)
768 enc[p][b].s += enc[p + bs][b].s;
772 for (e = 0; e < E_MAX; e++)
776 enc[p + 1][e].t + switchcost[E_BINARY][e] +
778 && enc[p + 1][e].t == 249) ? 1 : 0))
783 enc[p][E_BINARY].t = 1 + bl;
784 enc[p][E_BINARY].s = 1;
785 if (bl && b == E_BINARY)
786 enc[p][b].s += enc[p + 1][b].s;
788 * fprintf (stderr, "%d:", p); for (e = 0; e < E_MAX; e++) fprintf \
789 * (stderr, " %c*%d/%d", encchr[e], enc[p][e].s, enc[p][e].t); \
790 * fprintf (stderr, "\n");
793 encoding = safemalloc(l + 1);
796 char cur = E_ASCII; // starts ASCII
800 for (e = 0; e < E_MAX; e++)
802 && ((t = enc[p][e].t + switchcost[cur][e]) <
803 m || t == m && e == cur || !m)) {
812 encoding[p++] = encchr[b];
820 * Main encoding function
821 * Returns the grid (malloced) containing the matrix. L corner at 0,0.
822 * Takes suggested size in *Wptr, *Hptr, or 0,0. Fills in actual size.
823 * Takes barcodelen and barcode to be encoded
824 * Note, if *encodingptr is null, then fills with auto picked (malloced)
826 * If lenp not null, then the length of encoded data before any final
827 * unlatch or pad is stored
828 * If maxp not null, then the max storage of this size code is stored
829 * If eccp not null, then the number of ecc bytes used in this size is
831 * Returns 0 on error (writes to stderr with details).
834 unsigned char *iec16022ecc200(int *Wptr, int *Hptr, char **encodingptr,
835 int barcodelen, unsigned char *barcode,
836 int *lenp, int *maxp, int *eccp)
838 unsigned char binary[3000]; // encoded raw data and ecc to place in barcode
841 unsigned char *grid = 0;
842 struct ecc200matrix_s *matrix;
843 memset(binary, 0, sizeof(binary));
845 encoding = *encodingptr;
852 if (W) { // known size
853 for (matrix = ecc200matrix; matrix->W && (matrix->W != W ||
857 fprintf(stderr, "Invalid size %dx%d\n", W, H);
862 char *e = encmake(barcodelen, barcode, &len, 1);
863 if (e && len != matrix->bytes) { // try not an exact fit
865 e = encmake(barcodelen, barcode, &len, 0);
866 if (len > matrix->bytes) {
868 "Cannot make barcode fit %dx%d\n",
876 // find a suitable encoding
877 if (encoding == NULL)
878 encoding = encmake(barcodelen, barcode, NULL, 1);
880 if (encoding) { // find one that fits chosen encoding
881 for (matrix = ecc200matrix; matrix->W; matrix++)
883 (binary, matrix->bytes, barcode, barcodelen,
889 e = encmake(barcodelen, barcode, &len, 1);
890 for (matrix = ecc200matrix;
891 matrix->W && matrix->bytes != len; matrix++) ;
892 if (e && !matrix->W) { // try for non exact fit
894 e = encmake(barcodelen, barcode, &len, 0);
895 for (matrix = ecc200matrix;
896 matrix->W && matrix->bytes < len;
903 "Cannot find suitable size, barcode too long\n");
909 if (!ecc200encode(binary, matrix->bytes, barcode, barcodelen,
911 fprintf(stderr, "Barcode too long for %dx%d\n", W, H);
915 ecc200(binary, matrix->bytes, matrix->datablock, matrix->rsblock);
917 int x, y, NC, NR, *places;
918 NC = W - 2 * (W / matrix->FW);
919 NR = H - 2 * (H / matrix->FH);
920 places = safemalloc(NC * NR * sizeof(int));
921 ecc200placement(places, NR, NC);
922 grid = safemalloc(W * H);
923 memset(grid, 0, W * H);
924 for (y = 0; y < H; y += matrix->FH) {
925 for (x = 0; x < W; x++)
927 for (x = 0; x < W; x += 2)
928 grid[(y + matrix->FH - 1) * W + x] = 1;
930 for (x = 0; x < W; x += matrix->FW) {
931 for (y = 0; y < H; y++)
933 for (y = 0; y < H; y += 2)
934 grid[y * W + x + matrix->FW - 1] = 1;
936 for (y = 0; y < NR; y++) {
937 for (x = 0; x < NC; x++) {
938 int v = places[(NR - y - 1) * NC + x];
939 //fprintf (stderr, "%4d", v);
941 && (binary[(v >> 3) - 1] & (1 << (v & 7))))
943 2 * (y / (matrix->FH - 2))) * W +
945 2 * (x / (matrix->FW - 2))] = 1;
947 //fprintf (stderr, "\n");
956 *encodingptr = encoding;
958 *maxp = matrix->bytes;
961 (matrix->bytes + 2) / matrix->datablock * matrix->rsblock;