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
83 static void * safemalloc (int n)
88 fprintf (stderr, "Malloc(%d) failed\n", n);
94 // Annex M placement alorithm low level
95 static void ecc200placementbit (int *array, int NR, int NC, int r, int c, \
101 c += 4 - ((NR + 4) % 8);
106 r += 4 - ((NC + 4) % 8);
108 array[r * NC + c] = (p << 3) + b;
111 static void ecc200placementblock (int *array, int NR, int NC, int r, \
114 ecc200placementbit (array, NR, NC, r - 2, c - 2, p, 7);
115 ecc200placementbit (array, NR, NC, r - 2, c - 1, p, 6);
116 ecc200placementbit (array, NR, NC, r - 1, c - 2, p, 5);
117 ecc200placementbit (array, NR, NC, r - 1, c - 1, p, 4);
118 ecc200placementbit (array, NR, NC, r - 1, c - 0, p, 3);
119 ecc200placementbit (array, NR, NC, r - 0, c - 2, p, 2);
120 ecc200placementbit (array, NR, NC, r - 0, c - 1, p, 1);
121 ecc200placementbit (array, NR, NC, r - 0, c - 0, p, 0);
124 static void ecc200placementcornerA (int *array, int NR, int NC, int p)
126 ecc200placementbit (array, NR, NC, NR - 1, 0, p, 7);
127 ecc200placementbit (array, NR, NC, NR - 1, 1, p, 6);
128 ecc200placementbit (array, NR, NC, NR - 1, 2, p, 5);
129 ecc200placementbit (array, NR, NC, 0, NC - 2, p, 4);
130 ecc200placementbit (array, NR, NC, 0, NC - 1, p, 3);
131 ecc200placementbit (array, NR, NC, 1, NC - 1, p, 2);
132 ecc200placementbit (array, NR, NC, 2, NC - 1, p, 1);
133 ecc200placementbit (array, NR, NC, 3, NC - 1, p, 0);
136 static void ecc200placementcornerB (int *array, int NR, int NC, int p)
138 ecc200placementbit (array, NR, NC, NR - 3, 0, p, 7);
139 ecc200placementbit (array, NR, NC, NR - 2, 0, p, 6);
140 ecc200placementbit (array, NR, NC, NR - 1, 0, p, 5);
141 ecc200placementbit (array, NR, NC, 0, NC - 4, p, 4);
142 ecc200placementbit (array, NR, NC, 0, NC - 3, p, 3);
143 ecc200placementbit (array, NR, NC, 0, NC - 2, p, 2);
144 ecc200placementbit (array, NR, NC, 0, NC - 1, p, 1);
145 ecc200placementbit (array, NR, NC, 1, NC - 1, p, 0);
148 static void ecc200placementcornerC (int *array, int NR, int NC, int p)
150 ecc200placementbit (array, NR, NC, NR - 3, 0, p, 7);
151 ecc200placementbit (array, NR, NC, NR - 2, 0, p, 6);
152 ecc200placementbit (array, NR, NC, NR - 1, 0, p, 5);
153 ecc200placementbit (array, NR, NC, 0, NC - 2, p, 4);
154 ecc200placementbit (array, NR, NC, 0, NC - 1, p, 3);
155 ecc200placementbit (array, NR, NC, 1, NC - 1, p, 2);
156 ecc200placementbit (array, NR, NC, 2, NC - 1, p, 1);
157 ecc200placementbit (array, NR, NC, 3, NC - 1, p, 0);
160 static void ecc200placementcornerD (int *array, int NR, int NC, int p)
162 ecc200placementbit (array, NR, NC, NR - 1, 0, p, 7);
163 ecc200placementbit (array, NR, NC, NR - 1, NC - 1, p, 6);
164 ecc200placementbit (array, NR, NC, 0, NC - 3, p, 5);
165 ecc200placementbit (array, NR, NC, 0, NC - 2, p, 4);
166 ecc200placementbit (array, NR, NC, 0, NC - 1, p, 3);
167 ecc200placementbit (array, NR, NC, 1, NC - 3, p, 2);
168 ecc200placementbit (array, NR, NC, 1, NC - 2, p, 1);
169 ecc200placementbit (array, NR, NC, 1, NC - 1, p, 0);
172 // Annex M placement alorithm main function
173 static void ecc200placement (int *array, int NR, int NC)
179 for (r = 0; r < NR; r++)
180 for (c = 0; c < NC; c++)
181 array[r * NC + c] = 0;
190 ecc200placementcornerA (array, NR, NC, p++);
191 if (r == NR - 2 && !c && NC % 4)
192 ecc200placementcornerB (array, NR, NC, p++);
193 if (r == NR - 2 && !c && (NC % 8) == 4)
194 ecc200placementcornerC (array, NR, NC, p++);
195 if (r == NR + 4 && c == 2 && !(NC % 8))
196 ecc200placementcornerD (array, NR, NC, p++);
200 if (r < NR && c >= 0 && !array[r * NC + c])
201 ecc200placementblock (array, NR, NC, r, c, p++);
205 while (r >= 0 && c < NC);
211 if (r >= 0 && c < NC && !array[r * NC + c])
212 ecc200placementblock (array, NR, NC, r, c, p++);
216 while (r < NR && c >= 0);
220 while (r < NR || c < NC);
222 if (!array[NR * NC - 1])
223 array[NR * NC - 1] = array[NR * NC - NC - 2] = 1;
226 // calculate and append ecc code, and if necessary interleave
227 static void ecc200 (unsigned char *binary, int bytes, int datablock, \
230 int blocks = (bytes + 2) / datablock,
233 rs_init_code (rsblock, 1);
234 for (b = 0; b < blocks; b++)
236 unsigned char buf[256],
240 for (n = b; n < bytes; n += blocks)
241 buf[p++] = binary[n];
242 rs_encode (p, buf, ecc);
243 p = rsblock - 1; // comes back reversed
244 for (n = b; n < rsblock * blocks; n += blocks)
245 binary[bytes + n] = ecc[p--];
250 * perform encoding for ecc200, source s len sl, to target t len tl, using
251 * optional encoding control string e return 1 if OK, 0 if failed. Does all
252 * necessary padding to tl
255 char ecc200encode (unsigned char *t, int tl, unsigned char *s, int sl, \
256 char *encoding, int *lenp)
258 char enc = 'a'; // start in ASCII encoding mode
261 if (strlen (encoding) < sl)
263 fprintf (stderr, "Encoding string too short\n");
267 while (sp < sl && tp < tl)
269 char newenc = enc; // suggest new encoding
270 if (tl - tp <= 1 && (enc == 'c' || enc == 't') || tl - tp <= 2 && enc \
272 enc = 'a'; // auto revert to ASCII
273 newenc = tolower (encoding[sp]);
275 { // encode character
283 *s2 = "!\"#$%&'()*+,-./:;<=>?@[\\]_",
287 e = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
288 s3 = "`abcdefghijklmnopqrstuvwxyz{|}~\177";
292 e = " 0123456789abcdefghijklmnopqrstuvwxyz";
293 s3 = "`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~\177";
296 e = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\r*>";
299 unsigned char c = s[sp++];
305 fprintf (stderr, "Cannot encode char 0x%02X in X12\n", c);
314 out[p++] = ((w - e) + 3) % 40;
319 fprintf (stderr, "Cannot encode char 0x%02X in X12\n", c);
342 fprintf (stderr, "Could not encode 0x%02X, should \
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
479 * for (tp = 0; tp < tl; tp++) fprintf (stderr, "%02X ", t[tp]); \
480 * fprintf (stderr, "\n");
485 // Auto encoding format functions
486 static char encchr[] = "ACTXEB";
499 unsigned char switchcost[E_MAX][E_MAX] = {
500 0, 1, 1, 1, 1, 2, // From E_ASCII
501 1, 0, 2, 2, 2, 3, // From E_C40
502 1, 2, 0, 2, 2, 3, // From E_TEXT
503 1, 2, 2, 0, 2, 3, // From E_X12
504 1, 2, 2, 2, 0, 3, // From E_EDIFACT
505 0, 1, 1, 1, 1, 0, // From E_BINARY
509 * Creates a encoding list (malloc)
510 * returns encoding string
511 * if lenp not null, target len stored
512 * if error, null returned
513 * if exact specified, then assumes shortcuts applicable for exact fit
515 * 1. No unlatch to return to ASCII for last encoded byte after C40 or
517 * 2. No unlatch to return to ASCII for last 1 or 2 encoded bytes after
519 * 3. Final C40 or text encoding exactly in last 2 bytes can have a shift
520 * 0 to pad to make a tripple
521 * Only use the encoding from an exact request if the len matches the target,
522 * otherwise free the result and try again with exact=0
525 static char * encmake (int l, unsigned char *s, int *lenp, char exact)
532 // number of bytes of source that can be encoded in a row at this point
533 // using this encoding mode
535 // number of bytes of target generated encoding from this point to end if
536 // already in this encoding mode
538 } enc[MAXBARCODE][E_MAX];
539 memset (&enc, 0, sizeof (enc));
541 return ""; // no length
543 return 0; // not valid
552 // consider each encoding from this point
555 if (isdigit (s[p]) && p + 1 < l && isdigit (s[p + 1]))
556 sl = 2; // double digit
557 else if (s[p] & 0x80)
558 tl = 2; // high shifted
561 for (e = 0; e < E_MAX; e++)
562 if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + \
563 switchcost[E_ASCII][e]) < bl || !bl))
568 enc[p][E_ASCII].t = tl + bl;
569 enc[p][E_ASCII].s = sl;
570 if (bl && b == E_ASCII)
571 enc[p][b].s += enc[p + sl][b].s;
576 unsigned char c = s[p + sl++];
582 if (c != ' ' && !isdigit (c) && !isupper (c))
590 } while (sub && p + sl < l);
591 if (exact && sub == 2 && p + sl == l)
593 // special case, can encode last block with shift 0 at end (Is this
594 // valid when not end of target buffer?)
602 for (e = 0; e < E_MAX; e++)
603 if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + \
604 switchcost[E_C40][e]) < bl || !bl))
609 if (exact && enc[p + sl][E_ASCII].t == 1 && 1 < bl)
611 // special case, switch to ASCII for last bytes
615 enc[p][E_C40].t = tl + bl;
616 enc[p][E_C40].s = sl;
617 if (bl && b == E_C40)
618 enc[p][b].s += enc[p + sl][b].s;
624 unsigned char c = s[p + sl++];
630 if (c != ' ' && !isdigit (c) && !islower (c))
638 } while (sub && p + sl < l);
639 if (exact && sub == 2 && p + sl == l)
641 // special case, can encode last block with shift 0 at end (Is this
642 // valid when not end of target buffer?)
650 for (e = 0; e < E_MAX; e++)
651 if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + \
652 switchcost[E_TEXT][e]) < bl || !bl))
657 if (exact && enc[p + sl][E_ASCII].t == 1 && 1 < bl)
658 { // special case, switch to ASCII for last bytes
662 enc[p][E_TEXT].t = tl + bl;
663 enc[p][E_TEXT].s = sl;
664 if (bl && b == E_TEXT)
665 enc[p][b].s += enc[p + sl][b].s;
671 unsigned char c = s[p + sl++];
672 if (c != 13 && c != '*' && c != '>' && c != ' ' && !isdigit (c) && \
684 } while (sub && p + sl < l);
689 for (e = 0; e < E_MAX; e++)
690 if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + \
691 switchcost[E_X12][e]) < bl || !bl))
696 if (exact && enc[p + sl][E_ASCII].t == 1 && 1 < bl)
698 // special case, switch to ASCII for last bytes
702 enc[p][E_X12].t = tl + bl;
703 enc[p][E_X12].s = sl;
704 if (bl && b == E_X12)
705 enc[p][b].s += enc[p + sl][b].s;
709 if (s[p + 0] >= 32 && s[p + 0] <= 94)
712 if (p + 1 == l && (!bl || bl < 2))
717 for (e = 0; e < E_MAX; e++)
718 if (e != E_EDIFACT && enc[p + 1][e].t && ((t = 2 + \
719 enc[p + 1][e].t + switchcost[E_ASCII][e]) \
720 < bl || !bl)) // E_ASCII as allowed for unlatch
726 if (p + 1 < l && s[p + 1] >= 32 && s[p + 1] <= 94)
728 if (p + 2 == l && (!bl || bl < 2))
733 for (e = 0; e < E_MAX; e++)
734 if (e != E_EDIFACT && enc[p + 2][e].t && ((t = 3 + \
735 enc[p + 2][e].t + switchcost[E_ASCII][e]) \
736 < bl || !bl)) // E_ASCII as allowed for unlatch
742 if (p + 2 < l && s[p + 2] >= 32 && s[p + 2] <= 94)
744 if (p + 3 == l && (!bl || bl < 3))
749 for (e = 0; e < E_MAX; e++)
750 if (e != E_EDIFACT && enc[p + 3][e].t && ((t = 3 + \
751 enc[p + 3][e].t + switchcost[E_ASCII][e]) \
752 < bl || !bl)) // E_ASCII as allowed for unlatch
758 if (p + 4 < l && s[p + 3] >= 32 && s[p + 3] <= 94)
760 if (p + 4 == l && (!bl || bl < 3))
766 for (e = 0; e < E_MAX; e++)
767 if (enc[p + 4][e].t && ((t = 3 + enc[p + 4][e].t + \
768 switchcost[E_EDIFACT][e]) < bl || !bl))
774 if (exact && enc[p + 4][E_ASCII].t && enc[p + \
775 4][E_ASCII].t <= 2 && (t = 3 + enc[p + \
778 // special case, switch to ASCII for last 1 ot two bytes
787 enc[p][E_EDIFACT].t = bl;
788 enc[p][E_EDIFACT].s = bs;
789 if (bl && b == E_EDIFACT)
790 enc[p][b].s += enc[p + bs][b].s;
794 for (e = 0; e < E_MAX; e++)
796 && ((t = enc[p + 1][e].t + switchcost[E_BINARY][e] + ((e == \
797 E_BINARY && enc[p + 1][e].t == 249) ? 1 : 0)) \
803 enc[p][E_BINARY].t = 1 + bl;
804 enc[p][E_BINARY].s = 1;
805 if (bl && b == E_BINARY)
806 enc[p][b].s += enc[p + 1][b].s;
808 * fprintf (stderr, "%d:", p); for (e = 0; e < E_MAX; e++) fprintf \
809 * (stderr, " %c*%d/%d", encchr[e], enc[p][e].s, enc[p][e].t); \
810 * fprintf (stderr, "\n");
813 encoding = safemalloc (l + 1);
816 char cur = E_ASCII; // starts ASCII
822 for (e = 0; e < E_MAX; e++)
823 if (enc[p][e].t && ((t = enc[p][e].t + switchcost[cur][e]) < m || \
824 t == m && e == cur || !m))
834 encoding[p++] = encchr[b];
841 * Main encoding function
842 * Returns the grid (malloced) containing the matrix. L corner at 0,0.
843 * Takes suggested size in *Wptr, *Hptr, or 0,0. Fills in actual size.
844 * Takes barcodelen and barcode to be encoded
845 * Note, if *encodingptr is null, then fills with auto picked (malloced)
847 * If lenp not null, then the length of encoded data before any final
848 * unlatch or pad is stored
849 * If maxp not null, then the max storage of this size code is stored
850 * If eccp not null, then the number of ecc bytes used in this size is
852 * Returns 0 on error (writes to stderr with details).
855 unsigned char * iec16022ecc200 (int *Wptr, int *Hptr, char **encodingptr, \
856 int barcodelen, unsigned char *barcode, \
857 int *lenp, int *maxp, int *eccp)
859 unsigned char binary[3000]; // encoded raw data and ecc to place in barcode
863 unsigned char *grid = 0;
864 struct ecc200matrix_s *matrix;
865 memset (binary, 0, sizeof (binary));
867 encoding = *encodingptr;
876 for (matrix = ecc200matrix; matrix->W && (matrix->W != W || \
877 matrix->H != H); matrix++);
880 fprintf (stderr, "Invalid size %dx%d\n", W, H);
886 char *e = encmake (barcodelen, barcode, &len, 1);
887 if (e && len != matrix->bytes)
888 { // try not an exact fit
890 e = encmake (barcodelen, barcode, &len, 0);
891 if (len > matrix->bytes)
893 fprintf (stderr, "Cannot make barcode fit %dx%d\n", W, H);
902 { // find one that fits chosen encoding
903 for (matrix = ecc200matrix; matrix->W; matrix++)
904 if (ecc200encode (binary, matrix->bytes, barcode, barcodelen, \
911 e = encmake (barcodelen, barcode, &len, 1);
912 for (matrix = ecc200matrix; matrix->W && matrix->bytes != len; \
915 { // try for non exact fit
917 e = encmake (barcodelen, barcode, &len, 0);
918 for (matrix = ecc200matrix; matrix->W && matrix->bytes < len; \
925 fprintf (stderr, "Cannot find suitable size, barcode too long\n");
931 if (!ecc200encode (binary, matrix->bytes, barcode, barcodelen, \
934 fprintf (stderr, "Barcode too long for %dx%d\n", W, H);
938 ecc200 (binary, matrix->bytes, matrix->datablock, matrix->rsblock);
945 NC = W - 2 * (W / matrix->FW);
946 NR = H - 2 * (H / matrix->FH);
947 places = safemalloc (NC * NR * sizeof (int));
948 ecc200placement (places, NR, NC);
949 grid = safemalloc (W * H);
950 memset (grid, 0, W * H);
951 for (y = 0; y < H; y += matrix->FH)
953 for (x = 0; x < W; x++)
955 for (x = 0; x < W; x += 2)
956 grid[(y + matrix->FH - 1) * W + x] = 1;
958 for (x = 0; x < W; x += matrix->FW)
960 for (y = 0; y < H; y++)
962 for (y = 0; y < H; y += 2)
963 grid[y * W + x + matrix->FW - 1] = 1;
965 for (y = 0; y < NR; y++)
967 for (x = 0; x < NC; x++)
969 int v = places[(NR - y - 1) * NC + x];
970 //fprintf (stderr, "%4d", v);
971 if (v == 1 || v > 7 && (binary[(v >> 3) - 1] & (1 << (v & 7))))
972 grid[(1 + y + 2 * (y / (matrix->FH - 2))) * W + 1 + x + 2 * \
973 (x / (matrix->FW - 2))] = 1;
975 //fprintf (stderr, "\n");
984 *encodingptr = encoding;
986 *maxp = matrix->bytes;
988 *eccp = (matrix->bytes + 2) / matrix->datablock * matrix->rsblock;