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-2007 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"
37 // simple checked response malloc
38 void *safemalloc(int n)
42 fprintf(stderr, "Malloc(%d) failed\n", n);
48 // hex dump - bottom left pixel first
49 void dumphex(unsigned char *grid, int W, int H, unsigned char p)
52 for (y = 0; y < H; y++) {
53 int v = 0, x, b = 128;
54 for (x = 0; x < W; x++) {
59 printf("%02X", v ^ p);
66 printf("%02X", v ^ p);
80 int main(int argc, const char *argv[])
90 char *format = "Text";
93 int len = 0, maxlen = 0, ecclen = 0;
94 unsigned char *grid = 0;
95 poptContext optCon; // context for parsing command-line options
96 const struct poptOption optionsTable[] = {
98 "size", 's', POPT_ARG_STRING, &size, 0, "Size", "WxH"},
100 "barcode", 'c', POPT_ARG_STRING, &barcode, 0, "Barcode",
103 "ecc", 0, POPT_ARG_STRING, &eccstr, 0, "ECC",
104 "000/050/080/100/140/200"},
106 "infile", 'i', POPT_ARG_STRING, &infile, 0, "Barcode file",
109 "outfile", 'o', POPT_ARG_STRING, &outfile, 0,
113 "encoding", 'e', POPT_ARG_STRING, &encoding, 0,
115 "[CTXEAB]* for ecc200 or 11/27/41/37/128/256"},
117 "format", 'f', POPT_ARGFLAG_SHOW_DEFAULT | POPT_ARG_STRING,
119 "Output format", "Text/EPS/PNG/Bin/Hex/Stamp"},
123 optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
124 poptSetOtherOptionHelp(optCon, "[barcode]");
125 if ((c = poptGetNextOpt(optCon)) < -1) {
126 /* an error occurred during option processing */
127 fprintf(stderr, "%s: %s\n", poptBadOption(optCon,
128 POPT_BADOPTION_NOALIAS),
133 if (poptPeekArg(optCon) && !barcode && !infile)
134 barcode = (char *)poptGetArg(optCon);
135 if (poptPeekArg(optCon) || !barcode && !infile || barcode && infile) {
136 fprintf(stderr, "Version: %s\n", PACKAGE_VERSION);
137 poptPrintUsage(optCon, stderr, 0);
140 if (outfile && !freopen(outfile, "w", stdout)) {
145 if (infile) { // read from file
146 FILE *f = fopen(infile, "rb");
147 barcode = safemalloc(4001);
152 barcodelen = fread(barcode, 1, 4000, f);
153 if (barcodelen < 0) {
157 barcode[barcodelen] = 0; // null terminate anyway
160 barcodelen = strlen(barcode);
163 char *x = strchr(size, 'x');
172 if (W & 1) { // odd size
173 if (W != H || W < 9 || W > 49) {
174 fprintf(stderr, "Invalid size %dx%d\n", W, H);
187 if (ecc && ecc != 50 && ecc != 80 && ecc != 100 && ecc != 140 ||
188 ecc == 50 && W < 11 || ecc == 80 && W < 13 || ecc == 100
189 && W < 13 || ecc == 140 && W < 17) {
190 fprintf(stderr, "ECC%03d invalid for %dx%d\n", ecc, W,
195 } else if (W) { // even size
204 fprintf(stderr, "ECC%03d invalid for %dx%d\n", ecc, W,
212 // default is even sizes only unless explicit ecc set to force odd
217 if (tolower(*format) == 's') { // special stamp format checks & defaults
220 if (ecc != 200 || W != 32 || H != 32)
221 fprintf(stderr, "Stamps must be 32x32\n");
223 fprintf(stderr, "Stamps should use auto encoding\n");
226 for (n = 0; n < barcodelen && (barcode[n] == ' ' ||
228 || isupper(barcode[n]));
232 "Has invalid characters for a stamp\n");
234 // Generate simplistic encoding rules as used by the windows app
235 // TBA - does not always match the windows app...
237 encoding = safemalloc(barcodelen + 1);
238 while (n < barcodelen) {
242 || n + 3 <= barcodelen
243 && (!isdigit(barcode[n])
250 && isdigit(barcode[n - 1])
251 && isdigit(barcode[n]))
257 while (n + r < barcodelen
259 isdigit(barcode[n + r]))
261 if (n + 3 > barcodelen
270 //fprintf (stderr, "%s\n%s\n", barcode, encoding);
275 if ((W & 1) || ecc < 200) { // odd sizes
276 fprintf(stderr, "Not done odd sizes yet, sorry\n");
277 } else { // even sizes
279 iec16022ecc200(&W, &H, &encoding, barcodelen, barcode, &len,
285 fprintf(stderr, "No barcode produced\n");
288 switch (tolower(*format)) {
290 printf("Size : %dx%d\n", W, H);
291 printf("Encoded : %d of %d bytes with %d bytes of ecc\n", len,
293 printf("Barcode : %s\n", barcode);
294 printf("Encoding: %s\n", encoding);
297 dumphex(grid, W, H, 0);
302 for (y = 0; y < H; y++) {
303 int v = 0, x, b = 128;
304 for (x = 0; x < W; x++) {
322 for (y = H - 1; y >= 0; y--) {
324 for (x = 0; x < W; x++)
326 grid[W * y + x] ? '*' : ' ');
332 printf("%%!PS-Adobe-3.0 EPSF-3.0\n"
333 "%%%%Creator: IEC16022 barcode/stamp generator\n"
334 "%%%%BarcodeData: %s\n" "%%%%BarcodeSize: %dx%d\n"
335 "%%%%BarcodeFormat: ECC200\n"
336 "%%%%DocumentData: Clean7Bit\n" "%%%%LanguageLevel: 1\n"
337 "%%%%Pages: 1\n" "%%%%BoundingBox: 0 0 %d %d\n"
338 "%%%%EndComments\n" "%%%%Page: 1 1\n"
339 "%d %d 1[1 0 0 1 -1 -1]{<\n", barcode, W, H, W + 2,
341 dumphex(grid, W, H, 0xFF);
352 if (barcodelen < 74) {
354 "Does not look like a stamp barcode\n");
357 memcpy(temp, barcode, 74);
360 t.tm_year = atoi(temp + 54) + 100;
364 now += 86400 * (atoi(temp + 51) - 1);
368 printf("%%!PS-Adobe-3.0 EPSF-3.0\n"
369 "%%%%Creator: IEC16022 barcode/stamp generator\n"
370 "%%%%BarcodeData: %s\n" "%%%%BarcodeSize: %dx%d\n" "%%%%DocumentData: Clean7Bit\n" "%%%%LanguageLevel: 1\n" "%%%%Pages: 1\n" "%%%%BoundingBox: 0 0 190 80\n" "%%%%EndComments\n" "%%%%Page: 1 1\n" "10 dict begin/f{findfont exch scalefont \
371 setfont}bind def/rm/rmoveto load def/m/moveto load \
372 def/rl/rlineto load def\n" "/l/lineto load def/cp/closepath load def/c{dup stringwidth \
373 pop -2 div 0 rmoveto show}bind def\n" "gsave 72 25.4 div dup scale 0 0 m 67 0 rl 0 28 rl -67 0 rl \
374 cp clip 1 setgray fill 0 setgray 0.5 0 translate 0.3 \
375 setlinewidth\n" "32 32 1[2 0 0 2 0 -11]{<\n", barcode, W, H);
376 dumphex(grid, W, H, 0xFF);
378 "3.25/Helvetica-Bold f 8 25.3 m(\\243%d.%02d)c\n"
379 "2.6/Helvetica f 8 22.3 m(%.4s %.4s)c\n"
380 "1.5/Helvetica f 8 3.3 m(POST BY)c\n"
381 "3.3/Helvetica f 8 0.25 m(%02d.%02d.%02d)c\n",
382 v / 100, v % 100, temp + 6, temp + 10, t.tm_mday,
383 t.tm_mon + 1, t.tm_year % 100);
384 if (c == '1' || c == '2' || c == 'A' || c == 'S') {
387 ("42 0 m 10 0 rl 0 28 rl -10 0 rl cp 57 0 m 5 0 rl 0 \
391 ("42 0 m 5 0 rl 0 28 rl -5 0 rl cp 52 0 m 10 0 rl 0 \
394 (" 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 \
396 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 \
401 1.5/Helvetica-Bold f 22 3.3 m(POSTAGE PAID GB)show \
402 1.7/Helvetica f 29 1.5 m(DumbStamp.co.uk)c\n");
406 ("21/Helvetica-Bold f 23.5 13 m(2)1.25 1 scale show grestore \
407 0 setgray 1.5/Helvetica-Bold f 22 3.3 \
408 m(POSTAGE PAID GB)show 1.7/Helvetica f 29 1.5 \
409 m(DumbStamp.co.uk)c\n");
413 ("16/Helvetica-Bold f 29 14.75 m 1.1 1 scale(A)c grestore 0 \
414 setgray 1.5/Helvetica-Bold f 22 3.3 m(POSTAGE PAID GB)show \
415 1.7/Helvetica f 22 1.5 m(Par Avion)show\n");
419 ("10/Helvetica-Bold f 29 17 m(SU)c grestore 0 setgray \
420 1.5/Helvetica-Bold f 22 1.5 m(POSTAGE PAID GB)show\n");
424 ("2.3/Helvetica-Bold f 29 10 m(LOYAL MAIL)c\n");
425 } else if (c == 'P') { // Standard Parcels
426 printf("21 0 m 41 0 rl 0 28 rl -41 0 rl cp fill\n" "37.7 0.3 m 24 0 rl 0 27.4 rl -24 0 rl cp 1 setgray fill \
427 gsave 21.3 0.3 16.4 27.4 rectclip newpath\n" "22.5/Helvetica-Bold f 37.75 -1.25 m 90 rotate(SP)show \
428 grestore 0 setgray\n"
429 "3.5/Helvetica-Bold f 49.7 21.5 m(LOYAL MAIL)c\n" "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");
431 printf("21.15 0.15 40.7 27.7 rectstroke\n"
432 "21 0 m 41 0 rl 0 5 rl -41 0 rl cp fill\n"
433 "0 1 2{0 1 18{dup 1.525 mul 22.9 add 24 3 index 1.525 mul \
434 add 3 -1 roll 9 add 29 div 0 360 arc fill}for pop}for\n" "50.5 23.07 m 11.5 0 rl 0 5 rl -11.5 0 rl cp fill\n"
435 "5.85/Helvetica f 23.7 15.6 m(Loyal Mail)show\n" "4.75/Helvetica-Bold f 24 11 m(special)show 4.9/Helvetica \
436 f(delivery)show\n" "gsave 1 setgray 3.2/Helvetica-Bold f 24 1.6 \
437 m(next day)show 26 10.15 m 2 0 rl stroke grestore\n" "21.15 9.9 m 53.8 9.9 l stroke 53.8 9.9 0.4 0 360 \
439 printf("end grestore\n");
445 Image *i = ImageNew(W + 2, H + 2, 2);
446 i->Colour[0] = 0xFFFFFF;
448 for (y = 0; y < H; y++)
449 for (x = 0; x < W; x++)
451 ImagePixel(i, x + 1, H - y) = 1;
452 ImageWritePNG(i, fileno(stdout), 0, -1, barcode);
457 fprintf(stderr, "Unknown output format %s\n", format);