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
35 #include "iec16022ecc200.h"
37 static struct ecc200matrix_s
49 10, 10, 10, 10, 3, 3, 5, //
50 12, 12, 12, 12, 5, 5, 7, //
51 8, 18, 8, 18, 5, 5, 7, //
52 14, 14, 14, 14, 8, 8, 10, //
53 8, 32, 8, 16, 10, 10, 11, //
54 16, 16, 16, 16, 12, 12, 12, //
55 12, 26, 12, 26, 16, 16, 14, //
56 18, 18, 18, 18, 18, 18, 14, //
57 20, 20, 20, 20, 22, 22, 18, //
58 12, 36, 12, 18, 22, 22, 18, //
59 22, 22, 22, 22, 30, 30, 20, //
60 16, 36, 16, 18, 32, 32, 24, //
61 24, 24, 24, 24, 36, 36, 24, //
62 26, 26, 26, 26, 44, 44, 28, //
63 16, 48, 16, 24, 49, 49, 28, //
64 32, 32, 16, 16, 62, 62, 36, //
65 36, 36, 18, 18, 86, 86, 42, //
66 40, 40, 20, 20, 114, 114, 48, //
67 44, 44, 22, 22, 144, 144, 56, //
68 48, 48, 24, 24, 174, 174, 68, //
69 52, 52, 26, 26, 204, 102, 42, //
70 64, 64, 16, 16, 280, 140, 56, //
71 72, 72, 18, 18, 368, 92, 36, //
72 80, 80, 20, 20, 456, 114, 48, //
73 88, 88, 22, 22, 576, 144, 56, //
74 96, 96, 24, 24, 696, 174, 68, //
75 104, 104, 26, 26, 816, 136, 56, //
76 120, 120, 20, 20, 1050, 175, 68, //
77 132, 132, 22, 22, 1304, 163, 62, //
78 144, 144, 24, 24, 1558, 156, 62, // 156*4+155*2
82 // simple checked response malloc
89 fprintf (stderr, "Malloc(%d) failed\n", n);
95 // Annex M placement alorithm low level
97 ecc200placementbit (int *array, int NR, int NC, int r, int c, int p, char b)
102 c += 4 - ((NR + 4) % 8);
107 r += 4 - ((NC + 4) % 8);
109 array[r * NC + c] = (p << 3) + b;
113 ecc200placementblock (int *array, int NR, int NC, int r, int c, int p)
115 ecc200placementbit (array, NR, NC, r - 2, c - 2, p, 7);
116 ecc200placementbit (array, NR, NC, r - 2, c - 1, p, 6);
117 ecc200placementbit (array, NR, NC, r - 1, c - 2, p, 5);
118 ecc200placementbit (array, NR, NC, r - 1, c - 1, p, 4);
119 ecc200placementbit (array, NR, NC, r - 1, c - 0, p, 3);
120 ecc200placementbit (array, NR, NC, r - 0, c - 2, p, 2);
121 ecc200placementbit (array, NR, NC, r - 0, c - 1, p, 1);
122 ecc200placementbit (array, NR, NC, r - 0, c - 0, p, 0);
126 ecc200placementcornerA (int *array, int NR, int NC, int p)
128 ecc200placementbit (array, NR, NC, NR - 1, 0, p, 7);
129 ecc200placementbit (array, NR, NC, NR - 1, 1, p, 6);
130 ecc200placementbit (array, NR, NC, NR - 1, 2, p, 5);
131 ecc200placementbit (array, NR, NC, 0, NC - 2, p, 4);
132 ecc200placementbit (array, NR, NC, 0, NC - 1, p, 3);
133 ecc200placementbit (array, NR, NC, 1, NC - 1, p, 2);
134 ecc200placementbit (array, NR, NC, 2, NC - 1, p, 1);
135 ecc200placementbit (array, NR, NC, 3, NC - 1, p, 0);
139 ecc200placementcornerB (int *array, int NR, int NC, int p)
141 ecc200placementbit (array, NR, NC, NR - 3, 0, p, 7);
142 ecc200placementbit (array, NR, NC, NR - 2, 0, p, 6);
143 ecc200placementbit (array, NR, NC, NR - 1, 0, p, 5);
144 ecc200placementbit (array, NR, NC, 0, NC - 4, p, 4);
145 ecc200placementbit (array, NR, NC, 0, NC - 3, p, 3);
146 ecc200placementbit (array, NR, NC, 0, NC - 2, p, 2);
147 ecc200placementbit (array, NR, NC, 0, NC - 1, p, 1);
148 ecc200placementbit (array, NR, NC, 1, NC - 1, p, 0);
152 ecc200placementcornerC (int *array, int NR, int NC, int p)
154 ecc200placementbit (array, NR, NC, NR - 3, 0, p, 7);
155 ecc200placementbit (array, NR, NC, NR - 2, 0, p, 6);
156 ecc200placementbit (array, NR, NC, NR - 1, 0, p, 5);
157 ecc200placementbit (array, NR, NC, 0, NC - 2, p, 4);
158 ecc200placementbit (array, NR, NC, 0, NC - 1, p, 3);
159 ecc200placementbit (array, NR, NC, 1, NC - 1, p, 2);
160 ecc200placementbit (array, NR, NC, 2, NC - 1, p, 1);
161 ecc200placementbit (array, NR, NC, 3, NC - 1, p, 0);
165 ecc200placementcornerD (int *array, int NR, int NC, int p)
167 ecc200placementbit (array, NR, NC, NR - 1, 0, p, 7);
168 ecc200placementbit (array, NR, NC, NR - 1, NC - 1, p, 6);
169 ecc200placementbit (array, NR, NC, 0, NC - 3, p, 5);
170 ecc200placementbit (array, NR, NC, 0, NC - 2, p, 4);
171 ecc200placementbit (array, NR, NC, 0, NC - 1, p, 3);
172 ecc200placementbit (array, NR, NC, 1, NC - 3, p, 2);
173 ecc200placementbit (array, NR, NC, 1, NC - 2, p, 1);
174 ecc200placementbit (array, NR, NC, 1, NC - 1, p, 0);
177 // Annex M placement alorithm main function
179 ecc200placement (int *array, int NR, int NC)
185 for (r = 0; r < NR; r++)
186 for (c = 0; c < NC; c++)
187 array[r * NC + c] = 0;
196 ecc200placementcornerA (array, NR, NC, p++);
197 if (r == NR - 2 && !c && NC % 4)
198 ecc200placementcornerB (array, NR, NC, p++);
199 if (r == NR - 2 && !c && (NC % 8) == 4)
200 ecc200placementcornerC (array, NR, NC, p++);
201 if (r == NR + 4 && c == 2 && !(NC % 8))
202 ecc200placementcornerD (array, NR, NC, p++);
206 if (r < NR && c >= 0 && !array[r * NC + c])
207 ecc200placementblock (array, NR, NC, r, c, p++);
211 while (r >= 0 && c < NC);
217 if (r >= 0 && c < NC && !array[r * NC + c])
218 ecc200placementblock (array, NR, NC, r, c, p++);
222 while (r < NR && c >= 0);
226 while (r < NR || c < NC);
228 if (!array[NR * NC - 1])
229 array[NR * NC - 1] = array[NR * NC - NC - 2] = 1;
232 // calculate and append ecc code, and if necessary interleave
234 ecc200 (unsigned char *binary, int bytes, int datablock, int rsblock)
236 int blocks = (bytes + 2) / datablock,
239 rs_init_code (rsblock, 1);
240 for (b = 0; b < blocks; b++)
242 unsigned char buf[256],
246 for (n = b; n < bytes; n += blocks)
247 buf[p++] = binary[n];
248 rs_encode (p, buf, ecc);
249 p = rsblock - 1; // comes back reversed
250 for (n = b; n < rsblock * blocks; n += blocks)
251 binary[bytes + n] = ecc[p--];
255 // perform encoding for ecc200, source s len sl, to target t len tl, using optional encoding control string e
256 // return 1 if OK, 0 if failed. Does all necessary padding to tl
258 ecc200encode (unsigned char *t, int tl, unsigned char *s, int sl, char *encoding, int *lenp)
260 char enc = 'a'; // start in ASCII encoding mode
263 if (strlen (encoding) < sl)
265 fprintf (stderr, "Encoding string too short\n");
269 while (sp < sl && tp < tl)
271 char newenc = enc; // suggest new encoding
272 if (tl - tp <= 1 && (enc == 'c' || enc == 't') || tl - tp <= 2 && enc == 'x')
273 enc = 'a'; // auto revert to ASCII
274 newenc = tolower (encoding[sp]);
276 { // encode character
284 *s2 = "!\"#$%&'()*+,-./:;<=>?@[\\]_",
288 e = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
289 s3 = "`abcdefghijklmnopqrstuvwxyz{|}~\177";
293 e = " 0123456789abcdefghijklmnopqrstuvwxyz";
294 s3 = "`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~\177";
297 e = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\r*>";
300 unsigned char c = s[sp++];
306 fprintf (stderr, "Cannot encode char 0x%02X in X12\n", c);
315 out[p++] = ((w - e) + 3) % 40;
320 fprintf (stderr, "Cannot encode char 0x%02X in X12\n", c);
343 fprintf (stderr, "Could not encode 0x%02X, should not happen\n", c);
349 if (p == 2 && tp + 2 == tl && sp == sl)
350 out[p++] = 0; // shift 1 pad at end
353 int v = out[0] * 1600 + out[1] * 40 + out[2] + 1;
356 if (enc == 'c' || enc == 't' || enc == 'x')
357 t[tp++] = 254; // escape C40/text/X12
359 t[tp++] = 0x7C; // escape EDIFACT
369 t[tp++] = (v & 0xFF);
376 while (p && sp < sl);
381 unsigned char out[4],
384 { // can only be from C40/Text/X12
388 while (sp < sl && tolower (encoding[sp]) == 'e' && p < 4)
395 t[tp] = ((s[0] & 0x3F) << 2);
396 t[tp++] |= ((s[1] & 0x30) >> 4);
397 t[tp] = ((s[1] & 0x0F) << 4);
402 t[tp++] |= ((s[2] & 0x3C) >> 2);
403 t[tp] = ((s[2] & 0x03) << 6);
404 t[tp++] |= (s[3] & 0x3F);
411 if (enc == 'c' || enc == 't' || enc == 'x')
412 t[tp++] = 254; // escape C40/text/X12
414 t[tp++] = 0x7C; // escape EDIFACT
417 if (sl - sp >= 2 && isdigit (s[sp]) && isdigit (s[sp + 1]))
419 t[tp++] = (s[sp] - '0') * 10 + s[sp + 1] - '0' + 130;
421 } else if (s[sp] > 127)
424 t[tp++] = s[sp++] - 127;
426 t[tp++] = s[sp++] + 1;
430 int l = 0; // how much to encode
434 for (p = sp; p < sl && tolower (encoding[p]) == 'b'; p++)
437 t[tp++] = 231; // base256
442 t[tp++] = 249 + (l / 250);
445 while (l-- && tp < tl)
447 t[tp] = s[sp++] + (((tp + 1) * 149) % 255) + 1; // see annex H
450 enc = 'a'; // reverse to ASCII at end
454 fprintf (stderr, "Unknown encoding %c\n", newenc);
460 if (tp < tl && enc != 'a')
462 if (enc == 'c' || enc == 'x' || enc == 't')
463 t[tp++] = 254; // escape X12/C40/Text
465 t[tp++] = 0x7C; // escape EDIFACT
468 t[tp++] = 129; // pad
471 int v = 129 + (((tp + 1) * 149) % 253) + 1; // see Annex H
476 if (tp > tl || sp < sl)
477 return 0; // did not fit
478 //for (tp = 0; tp < tl; tp++) fprintf (stderr, "%02X ", t[tp]); fprintf (stderr, "\n");
482 // Auto encoding format functions
483 static char encchr[] = "ACTXEB";
496 unsigned char switchcost[E_MAX][E_MAX] = {
497 0, 1, 1, 1, 1, 2, // From E_ASCII
498 1, 0, 2, 2, 2, 3, // From E_C40
499 1, 2, 0, 2, 2, 3, // From E_TEXT
500 1, 2, 2, 0, 2, 3, // From E_X12
501 1, 2, 2, 2, 0, 3, // From E_EDIFACT
502 0, 1, 1, 1, 1, 0, // From E_BINARY
505 // Creates a encoding list (malloc)
506 // returns encoding string
507 // if lenp not null, target len stored
508 // if error, null returned
509 // if exact specified, then assumes shortcuts applicable for exact fit in target
510 // 1. No unlatch to return to ASCII for last encoded byte after C40 or Text or X12
511 // 2. No unlatch to return to ASCII for last 1 or 2 encoded bytes after EDIFACT
512 // 3. Final C40 or text encoding exactly in last 2 bytes can have a shift 0 to pad to make a tripple
513 // Only use the encoding from an exact request if the len matches the target, otherwise free the result and try again with exact=0
515 encmake (int l, unsigned char *s, int *lenp, char exact)
522 short s; // number of bytes of source that can be encoded in a row at this point using this encoding mode
523 short t; // number of bytes of target generated encoding from this point to end if already in this encoding mode
524 } enc[MAXBARCODE][E_MAX];
525 memset (&enc, 0, sizeof (enc));
527 return ""; // no length
529 return 0; // not valid
538 // consider each encoding from this point
541 if (isdigit (s[p]) && p + 1 < l && isdigit (s[p + 1]))
542 sl = 2; // double digit
543 else if (s[p] & 0x80)
544 tl = 2; // high shifted
547 for (e = 0; e < E_MAX; e++)
548 if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + switchcost[E_ASCII][e]) < bl || !bl))
553 enc[p][E_ASCII].t = tl + bl;
554 enc[p][E_ASCII].s = sl;
555 if (bl && b == E_ASCII)
556 enc[p][b].s += enc[p + sl][b].s;
561 unsigned char c = s[p + sl++];
567 if (c != ' ' && !isdigit (c) && !isupper (c))
575 } while (sub && p + sl < l);
576 if (exact && sub == 2 && p + sl == l)
577 { // special case, can encode last block with shift 0 at end (Is this valid when not end of target buffer?)
585 for (e = 0; e < E_MAX; e++)
586 if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + switchcost[E_C40][e]) < bl || !bl))
591 if (exact && enc[p + sl][E_ASCII].t == 1 && 1 < bl)
592 { // special case, switch to ASCII for last bytes
596 enc[p][E_C40].t = tl + bl;
597 enc[p][E_C40].s = sl;
598 if (bl && b == E_C40)
599 enc[p][b].s += enc[p + sl][b].s;
605 unsigned char c = s[p + sl++];
611 if (c != ' ' && !isdigit (c) && !islower (c))
619 } while (sub && p + sl < l);
620 if (exact && sub == 2 && p + sl == l)
621 { // special case, can encode last block with shift 0 at end (Is this valid when not end of target buffer?)
629 for (e = 0; e < E_MAX; e++)
630 if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + switchcost[E_TEXT][e]) < bl || !bl))
635 if (exact && enc[p + sl][E_ASCII].t == 1 && 1 < bl)
636 { // special case, switch to ASCII for last bytes
640 enc[p][E_TEXT].t = tl + bl;
641 enc[p][E_TEXT].s = sl;
642 if (bl && b == E_TEXT)
643 enc[p][b].s += enc[p + sl][b].s;
649 unsigned char c = s[p + sl++];
650 if (c != 13 && c != '*' && c != '>' && c != ' ' && !isdigit (c) && !isupper (c))
661 } while (sub && p + sl < l);
666 for (e = 0; e < E_MAX; e++)
667 if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + switchcost[E_X12][e]) < bl || !bl))
672 if (exact && enc[p + sl][E_ASCII].t == 1 && 1 < bl)
673 { // special case, switch to ASCII for last bytes
677 enc[p][E_X12].t = tl + bl;
678 enc[p][E_X12].s = sl;
679 if (bl && b == E_X12)
680 enc[p][b].s += enc[p + sl][b].s;
684 if (s[p + 0] >= 32 && s[p + 0] <= 94)
687 if (p + 1 == l && (!bl || bl < 2))
692 for (e = 0; e < E_MAX; e++)
693 if (e != E_EDIFACT && enc[p + 1][e].t && ((t = 2 + enc[p + 1][e].t + switchcost[E_ASCII][e]) < bl || !bl)) // E_ASCII as allowed for unlatch
699 if (p + 1 < l && s[p + 1] >= 32 && s[p + 1] <= 94)
701 if (p + 2 == l && (!bl || bl < 2))
706 for (e = 0; e < E_MAX; e++)
707 if (e != E_EDIFACT && enc[p + 2][e].t && ((t = 3 + enc[p + 2][e].t + switchcost[E_ASCII][e]) < bl || !bl)) // E_ASCII as allowed for unlatch
713 if (p + 2 < l && s[p + 2] >= 32 && s[p + 2] <= 94)
715 if (p + 3 == l && (!bl || bl < 3))
720 for (e = 0; e < E_MAX; e++)
721 if (e != E_EDIFACT && enc[p + 3][e].t && ((t = 3 + enc[p + 3][e].t + switchcost[E_ASCII][e]) < bl || !bl)) // E_ASCII as allowed for unlatch
727 if (p + 4 < l && s[p + 3] >= 32 && s[p + 3] <= 94)
729 if (p + 4 == l && (!bl || bl < 3))
735 for (e = 0; e < E_MAX; e++)
736 if (enc[p + 4][e].t && ((t = 3 + enc[p + 4][e].t + switchcost[E_EDIFACT][e]) < bl || !bl))
742 if (exact && enc[p + 4][E_ASCII].t && enc[p + 4][E_ASCII].t <= 2 && (t = 3 + enc[p + 4][E_ASCII].t) < bl)
743 { // special case, switch to ASCII for last 1 ot two bytes
752 enc[p][E_EDIFACT].t = bl;
753 enc[p][E_EDIFACT].s = bs;
754 if (bl && b == E_EDIFACT)
755 enc[p][b].s += enc[p + bs][b].s;
759 for (e = 0; e < E_MAX; e++)
761 && ((t = enc[p + 1][e].t + switchcost[E_BINARY][e] + ((e == E_BINARY && enc[p + 1][e].t == 249) ? 1 : 0)) < bl || !bl))
766 enc[p][E_BINARY].t = 1 + bl;
767 enc[p][E_BINARY].s = 1;
768 if (bl && b == E_BINARY)
769 enc[p][b].s += enc[p + 1][b].s;
770 //fprintf (stderr, "%d:", p); for (e = 0; e < E_MAX; e++) fprintf (stderr, " %c*%d/%d", encchr[e], enc[p][e].s, enc[p][e].t); fprintf (stderr, "\n");
772 encoding = safemalloc (l + 1);
775 char cur = E_ASCII; // starts ASCII
781 for (e = 0; e < E_MAX; e++)
782 if (enc[p][e].t && ((t = enc[p][e].t + switchcost[cur][e]) < m || t == m && e == cur || !m))
792 encoding[p++] = encchr[b];
799 // Main encoding function
800 // Returns the grid (malloced) containing the matrix. L corner at 0,0.
801 // Takes suggested size in *Wptr, *Hptr, or 0,0. Fills in actual size.
802 // Takes barcodelen and barcode to be encoded
803 // Note, if *encodingptr is null, then fills with auto picked (malloced) encoding
804 // If lenp not null, then the length of encoded data before any final unlatch or pad is stored
805 // If maxp not null, then the max storage of this size code is stored
806 // If eccp not null, then the number of ecc bytes used in this size is stored
807 // Returns 0 on error (writes to stderr with details).
809 iec16022ecc200 (int *Wptr, int *Hptr, char **encodingptr, int barcodelen, unsigned char *barcode, int *lenp, int *maxp, int *eccp)
811 unsigned char binary[3000]; // encoded raw data and ecc to place in barcode
815 unsigned char *grid = 0;
816 struct ecc200matrix_s *matrix;
817 memset (binary, 0, sizeof (binary));
819 encoding = *encodingptr;
828 for (matrix = ecc200matrix; matrix->W && (matrix->W != W || matrix->H != H); matrix++);
831 fprintf (stderr, "Invalid size %dx%d\n", W, H);
837 char *e = encmake (barcodelen, barcode, &len, 1);
838 if (e && len != matrix->bytes)
839 { // try not an exact fit
841 e = encmake (barcodelen, barcode, &len, 0);
842 if (len > matrix->bytes)
844 fprintf (stderr, "Cannot make barcode fit %dx%d\n", W, H);
853 { // find one that fits chosen encoding
854 for (matrix = ecc200matrix; matrix->W; matrix++)
855 if (ecc200encode (binary, matrix->bytes, barcode, barcodelen, encoding, 0))
861 e = encmake (barcodelen, barcode, &len, 1);
862 for (matrix = ecc200matrix; matrix->W && matrix->bytes != len; matrix++);
864 { // try for non exact fit
866 e = encmake (barcodelen, barcode, &len, 0);
867 for (matrix = ecc200matrix; matrix->W && matrix->bytes < len; matrix++);
873 fprintf (stderr, "Cannot find suitable size, barcode too long\n");
879 if (!ecc200encode (binary, matrix->bytes, barcode, barcodelen, encoding, lenp))
881 fprintf (stderr, "Barcode too long for %dx%d\n", W, H);
885 ecc200 (binary, matrix->bytes, matrix->datablock, matrix->rsblock);
892 NC = W - 2 * (W / matrix->FW);
893 NR = H - 2 * (H / matrix->FH);
894 places = safemalloc (NC * NR * sizeof (int));
895 ecc200placement (places, NR, NC);
896 grid = safemalloc (W * H);
897 memset (grid, 0, W * H);
898 for (y = 0; y < H; y += matrix->FH)
900 for (x = 0; x < W; x++)
902 for (x = 0; x < W; x += 2)
903 grid[(y + matrix->FH - 1) * W + x] = 1;
905 for (x = 0; x < W; x += matrix->FW)
907 for (y = 0; y < H; y++)
909 for (y = 0; y < H; y += 2)
910 grid[y * W + x + matrix->FW - 1] = 1;
912 for (y = 0; y < NR; y++)
914 for (x = 0; x < NC; x++)
916 int v = places[(NR - y - 1) * NC + x];
917 //fprintf (stderr, "%4d", v);
918 if (v == 1 || v > 7 && (binary[(v >> 3) - 1] & (1 << (v & 7))))
919 grid[(1 + y + 2 * (y / (matrix->FH - 2))) * W + 1 + x + 2 * (x / (matrix->FW - 2))] = 1;
921 //fprintf (stderr, "\n");
930 *encodingptr = encoding;
932 *maxp = matrix->bytes;
934 *eccp = (matrix->bytes + 2) / matrix->datablock * matrix->rsblock;