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 // simple checked response malloc
44 fprintf (stderr, "Malloc(%d) failed\n", n);
50 // hex dump - bottom left pixel first
52 dumphex (unsigned char *grid, int W, int H, unsigned char p)
56 for (y = 0; y < H; y++)
61 for (x = 0; x < W; x++)
68 printf ("%02X", v ^ p);
76 printf ("%02X", v ^ p);
92 main (int argc, const char *argv[])
103 char *format = "Text";
109 unsigned char *grid = 0;
110 poptContext optCon; // context for parsing command-line options
111 const struct poptOption optionsTable[] = {
113 "size", 's', POPT_ARG_STRING, &size, 0, "Size", "WxH"},
115 "barcode", 'c', POPT_ARG_STRING, &barcode, 0, "Barcode", "text"},
117 "ecc", 0, POPT_ARG_STRING, &eccstr, 0, "ECC", "000/050/080/100/140/200"},
119 "infile", 'i', POPT_ARG_STRING, &infile, 0, "Barcode file", "filename"},
121 "outfile", 'o', POPT_ARG_STRING, &outfile, 0, "Output filename", "filename"},
123 "encoding", 'e', POPT_ARG_STRING, &encoding, 0, "Encoding template", "[CTXEAB]* for ecc200 or 11/27/41/37/128/256"},
125 "format", 'f', POPT_ARGFLAG_SHOW_DEFAULT | POPT_ARG_STRING, &format, 0, "Output format", "Text/EPS/PNG/Bin/Hex/Stamp"},
129 optCon = poptGetContext (NULL, argc, argv, optionsTable, 0);
130 poptSetOtherOptionHelp (optCon, "[barcode]");
131 if ((c = poptGetNextOpt (optCon)) < -1)
133 /* an error occurred during option processing */
134 fprintf (stderr, "%s: %s\n", poptBadOption (optCon, POPT_BADOPTION_NOALIAS), poptStrerror (c));
138 if (poptPeekArg (optCon) && !barcode && !infile)
139 barcode = (char *) poptGetArg (optCon);
140 if (poptPeekArg (optCon) || !barcode && !infile || barcode && infile)
142 poptPrintUsage (optCon, stderr, 0);
145 if (outfile && !freopen (outfile, "w", stdout))
153 FILE *f = fopen (infile, "rb");
154 barcode = safemalloc (4001);
160 barcodelen = fread (barcode, 1, 4000, f);
166 barcode[barcodelen] = 0; // null terminate anyway
169 barcodelen = strlen (barcode);
173 char *x = strchr (size, 'x');
184 if (W != H || W < 9 || W > 49)
186 fprintf (stderr, "Invalid size %dx%d\n", W, H);
200 if (ecc && ecc != 50 && ecc != 80 && ecc != 100 && ecc != 140 || ecc == 50 && W < 11 || ecc == 80 && W < 13
201 || ecc == 100 && W < 13 || ecc == 140 && W < 17)
203 fprintf (stderr, "ECC%03d invalid for %dx%d\n", ecc, W, H);
219 fprintf (stderr, "ECC%03d invalid for %dx%d\n", ecc, W, H);
227 ecc = 200; // default is even sizes only unless explicit ecc set to force odd sizes
230 if (tolower (*format) == 's')
231 { // special stamp format checks & defaults
234 if (ecc != 200 || W != 32 || H != 32)
235 fprintf (stderr, "Stamps must be 32x32\n");
237 fprintf (stderr, "Stamps should use auto encoding\n");
241 for (n = 0; n < barcodelen && (barcode[n] == ' ' || isdigit (barcode[n]) || isupper (barcode[n])); n++);
243 fprintf (stderr, "Has invalid characters for a stamp\n");
245 { // Generate simplistic encoding rules as used by the windows app
246 // TBA - does not always match the windows app...
248 encoding = safemalloc (barcodelen + 1);
249 while (n < barcodelen)
254 if (n == barcodelen || n + 3 <= barcodelen && (!isdigit (barcode[n]) || !isdigit (barcode[n + 1])))
257 if (n < barcodelen && isdigit (barcode[n - 1]) && isdigit (barcode[n]))
264 while (n + r < barcodelen && isdigit (barcode[n + r]))
266 if (n + 3 > barcodelen || r >= 6)
274 //fprintf (stderr, "%s\n%s\n", barcode, encoding);
279 if ((W & 1) || ecc < 200)
281 fprintf (stderr, "Not done odd sizes yet, sorry\n");
284 grid = iec16022ecc200 (&W, &H, &encoding, barcodelen, barcode, &len, &maxlen, &ecclen);
290 fprintf (stderr, "No barcode produced\n");
293 switch (tolower (*format))
296 printf ("Size : %dx%d\n", W, H);
297 printf ("Encoded : %d of %d bytes with %d bytes of ecc\n", len, maxlen, ecclen);
298 printf ("Barcode : %s\n", barcode);
299 printf ("Encoding: %s\n", encoding);
302 dumphex (grid, W, H, 0);
307 for (y = 0; y < H; y++)
312 for (x = 0; x < W; x++)
332 for (y = H - 1; y >= 0; y--)
335 for (x = 0; x < W; x++)
336 printf ("%c", grid[W * y + x] ? '*' : ' ');
342 printf ("%%!PS-Adobe-3.0 EPSF-3.0\n" "%%%%Creator: IEC16022 barcode/stamp generator\n" "%%%%BarcodeData: %s\n"
343 "%%%%BarcodeSize: %dx%d\n" "%%%%BarcodeFormat: ECC200\n" "%%%%DocumentData: Clean7Bit\n" "%%%%LanguageLevel: 1\n"
344 "%%%%Pages: 1\n" "%%%%BoundingBox: 0 0 %d %d\n" "%%%%EndComments\n" "%%%%Page: 1 1\n" "%d %d 1[1 0 0 1 -1 -1]{<\n",
345 barcode, W, H, W + 2, H + 2, W, H);
346 dumphex (grid, W, H, 0xFF);
347 printf (">}image\n");
360 fprintf (stderr, "Does not look like a stamp barcode\n");
363 memcpy (temp, barcode, 74);
366 t.tm_year = atoi (temp + 54) + 100;
370 now += 86400 * (atoi (temp + 51) - 1);
373 v = atoi (temp + 36);
374 printf ("%%!PS-Adobe-3.0 EPSF-3.0\n" "%%%%Creator: IEC16022 barcode/stamp generator\n" "%%%%BarcodeData: %s\n"
375 "%%%%BarcodeSize: %dx%d\n" "%%%%DocumentData: Clean7Bit\n" "%%%%LanguageLevel: 1\n"
376 "%%%%Pages: 1\n" "%%%%BoundingBox: 0 0 190 80\n" "%%%%EndComments\n" "%%%%Page: 1 1\n"
377 "10 dict begin/f{findfont exch scalefont setfont}bind def/rm/rmoveto load def/m/moveto load def/rl/rlineto load def\n"
378 "/l/lineto load def/cp/closepath load def/c{dup stringwidth pop -2 div 0 rmoveto show}bind def\n"
379 "gsave 72 25.4 div dup scale 0 0 m 67 0 rl 0 28 rl -67 0 rl cp clip 1 setgray fill 0 setgray 0.5 0 translate 0.3 setlinewidth\n"
380 "32 32 1[2 0 0 2 0 -11]{<\n", barcode, W, H);
381 dumphex (grid, W, H, 0xFF);
383 "3.25/Helvetica-Bold f 8 25.3 m(\\243%d.%02d)c\n"
384 "2.6/Helvetica f 8 22.3 m(%.4s %.4s)c\n"
385 "1.5/Helvetica f 8 3.3 m(POST BY)c\n"
386 "3.3/Helvetica f 8 0.25 m(%02d.%02d.%02d)c\n",
387 v / 100, v % 100, temp + 6, temp + 10, t.tm_mday, t.tm_mon + 1, t.tm_year % 100);
388 if (c == '1' || c == '2' || c == 'A' || c == 'S')
391 printf ("42 0 m 10 0 rl 0 28 rl -10 0 rl cp 57 0 m 5 0 rl 0 28 rl -5 0 rl cp");
393 printf ("42 0 m 5 0 rl 0 28 rl -5 0 rl cp 52 0 m 10 0 rl 0 28 rl -10 0 rl cp");
394 printf (" 21 0 m 16 0 rl 0 28 rl -16 0 rl cp fill\n"
395 "21.3 0.3 m 15.4 0 rl 0 13 rl -15.4 0 rl cp 1 setgray fill gsave 21.3 0.3 15.4 27.4 rectclip newpath\n");
400 ("27/Helvetica-Bold f 27 8.7 m(1)show grestore 0 setgray 1.5/Helvetica-Bold f 22 3.3 m(POSTAGE PAID GB)show 1.7/Helvetica f 29 1.5 m(DumbStamp.co.uk)c\n");
404 ("21/Helvetica-Bold f 23.5 13 m(2)1.25 1 scale show grestore 0 setgray 1.5/Helvetica-Bold f 22 3.3 m(POSTAGE PAID GB)show 1.7/Helvetica f 29 1.5 m(DumbStamp.co.uk)c\n");
408 ("16/Helvetica-Bold f 29 14.75 m 1.1 1 scale(A)c grestore 0 setgray 1.5/Helvetica-Bold f 22 3.3 m(POSTAGE PAID GB)show 1.7/Helvetica f 22 1.5 m(Par Avion)show\n");
411 printf ("10/Helvetica-Bold f 29 17 m(SU)c grestore 0 setgray 1.5/Helvetica-Bold f 22 1.5 m(POSTAGE PAID GB)show\n");
414 printf ("2.3/Helvetica-Bold f 29 10 m(LOYAL MAIL)c\n");
416 { // Standard Parcels
417 printf ("21 0 m 41 0 rl 0 28 rl -41 0 rl cp fill\n"
418 "37.7 0.3 m 24 0 rl 0 27.4 rl -24 0 rl cp 1 setgray fill gsave 21.3 0.3 16.4 27.4 rectclip newpath\n"
419 "22.5/Helvetica-Bold f 37.75 -1.25 m 90 rotate(SP)show grestore 0 setgray\n"
420 "3.5/Helvetica-Bold f 49.7 21.5 m(LOYAL MAIL)c\n"
421 "2.3/Helvetica-Bold f 49.7 7 m(POSTAGE PAID GB)c\n" "2.6/Helveica f 49.7 4.25 m(DumbStamp.co.uk)c\n");
423 printf ("21.15 0.15 40.7 27.7 rectstroke\n"
424 "21 0 m 41 0 rl 0 5 rl -41 0 rl cp fill\n"
425 "0 1 2{0 1 18{dup 1.525 mul 22.9 add 24 3 index 1.525 mul add 3 -1 roll 9 add 29 div 0 360 arc fill}for pop}for\n"
426 "50.5 23.07 m 11.5 0 rl 0 5 rl -11.5 0 rl cp fill\n"
427 "5.85/Helvetica f 23.7 15.6 m(Loyal Mail)show\n"
428 "4.75/Helvetica-Bold f 24 11 m(special)show 4.9/Helvetica f(delivery)show\n"
429 "gsave 1 setgray 3.2/Helvetica-Bold f 24 1.6 m(next day)show 26 10.15 m 2 0 rl stroke grestore\n"
430 "21.15 9.9 m 53.8 9.9 l stroke 53.8 9.9 0.4 0 360 arc fill\n");
431 printf ("end grestore\n");
438 Image *i = ImageNew (W + 2, H + 2, 2);
439 i->Colour[0] = 0xFFFFFF;
441 for (y = 0; y < H; y++)
442 for (x = 0; x < W; x++)
444 ImagePixel (i, x + 1, H - y) = 1;
445 ImageWritePNG (i, fileno (stdout), 0, -1, barcode);
450 fprintf (stderr, "Unknown output format %s\n", format);