📄 iec16022.c
字号:
// IEC16022 bar code generation
// This software is provided under the terms of the GPL v2 or later.
// This software is provided free of charge with a full "Money back" guarantee.
// Use entirely at your own risk. We accept no liability. If you don't like that - don't use it.
// Adrian Kennard, Andrews & Arnold Ltd
// with help from Cliff Hones on the RS coding
//
// $Log: iec16022.c,v $
// Revision 1.23 2004/09/29 19:21:03 cvs
// Typo.
//
// Revision 1.22 2004/09/29 17:41:31 cvs
// *** empty log message ***
//
// Revision 1.21 2004/09/29 17:38:27 cvs
// DumbStamp V2.0 new artwork
//
// Revision 1.20 2004/09/12 10:35:25 cvs
// Minor fixes to auto encoding, and more precise placement of text on stamp output.
//
// Revision 1.19 2004/09/11 16:16:49 cvs
// Better centering on stamp barcode
//
// Revision 1.18 2004/09/09 12:39:45 cvs
// *** empty log message ***
//
// Revision 1.17 2004/09/09 07:45:09 cvs
// Added change history to source files
// Added "info" type to IEC16022
// Added exact size checking shortcodes on encoding generation for iec16022
//
// TBA, structured append, and ECI options.
// TBA, library for odd size ECC000-140 codes
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <popt.h>
#include <malloc.h>
#include "image.h"
#include "iec16022ecc200.h"
// simple checked response malloc
void *
safemalloc (int n)
{
void *p = malloc (n);
if (!p)
{
fprintf (stderr, "Malloc(%d) failed\n", n);
exit (1);
}
return p;
}
// hex dump - bottom left pixel first
void
dumphex (unsigned char *grid, int W, int H, unsigned char p)
{
int c = 0,
y;
for (y = 0; y < H; y++)
{
int v = 0,
x,
b = 128;
for (x = 0; x < W; x++)
{
if (grid[y * W + x])
v |= b;
b >>= 1;
if (!b)
{
printf ("%02X", v ^ p);
v = 0;
b = 128;
c++;
}
}
if (b != 128)
{
printf ("%02X", v ^ p);
c++;
}
printf (" ");
c++;
if (c >= 80)
{
printf ("\n");
c = 0;
}
}
if (c)
printf ("\n");
}
int
main (int argc, const char *argv[])
{
char c;
int W = 0,
H = 0;
int ecc = 0;
int barcodelen = 0;
char *encoding = 0;
char *outfile = 0;
char *infile = 0;
char *barcode = 0;
char *format = "Text";
char *size = 0;
char *eccstr = 0;
int len = 0,
maxlen = 0,
ecclen = 0;
unsigned char *grid = 0;
poptContext optCon; // context for parsing command-line options
const struct poptOption optionsTable[] = {
{
"size", 's', POPT_ARG_STRING, &size, 0, "Size", "WxH"},
{
"barcode", 'c', POPT_ARG_STRING, &barcode, 0, "Barcode", "text"},
{
"ecc", 0, POPT_ARG_STRING, &eccstr, 0, "ECC", "000/050/080/100/140/200"},
{
"infile", 'i', POPT_ARG_STRING, &infile, 0, "Barcode file", "filename"},
{
"outfile", 'o', POPT_ARG_STRING, &outfile, 0, "Output filename", "filename"},
{
"encoding", 'e', POPT_ARG_STRING, &encoding, 0, "Encoding template", "[CTXEAB]* for ecc200 or 11/27/41/37/128/256"},
{
"format", 'f', POPT_ARGFLAG_SHOW_DEFAULT | POPT_ARG_STRING, &format, 0, "Output format", "Text/EPS/Bin/Hex/Stamp"},
POPT_AUTOHELP {
NULL, 0, 0, NULL, 0}
};
optCon = poptGetContext (NULL, argc, argv, optionsTable, 0);
poptSetOtherOptionHelp (optCon, "[barcode]");
if ((c = poptGetNextOpt (optCon)) < -1)
{
/* an error occurred during option processing */
fprintf (stderr, "%s: %s\n", poptBadOption (optCon, POPT_BADOPTION_NOALIAS), poptStrerror (c));
return 1;
}
if (poptPeekArg (optCon) && !barcode && !infile)
barcode = (char *) poptGetArg (optCon);
if (poptPeekArg (optCon) || !barcode && !infile || barcode && infile)
{
poptPrintUsage (optCon, stderr, 0);
return -1;
}
if (outfile && !freopen (outfile, "w", stdout))
{
perror (outfile);
return 1;
}
if (infile)
{ // read from file
FILE *f = fopen (infile, "rb");
barcode = safemalloc (4001);
if (!f)
{
perror (infile);
return 1;
}
barcodelen = fread (barcode, 1, 4000, f);
if (barcodelen < 0)
{
perror (infile);
return 1;
}
barcode[barcodelen] = 0; // null terminate anyway
close (f);
} else
barcodelen = strlen (barcode);
// check parameters
if (size)
{
char *x = strchr (size, 'x');
W = atoi (size);
if (x)
H = atoi (x + 1);
if (!H)
W = H;
}
if (eccstr)
ecc = atoi (eccstr);
if (W & 1)
{ // odd size
if (W != H || W < 9 || W > 49)
{
fprintf (stderr, "Invalid size %dx%d\n", W, H);
return 1;
}
if (!eccstr)
{
if (W >= 17)
ecc = 140;
else if (W >= 13)
ecc = 100;
else if (W >= 11)
ecc = 80;
else
ecc = 0;
}
if (ecc && ecc != 50 && ecc != 80 && ecc != 100 && ecc != 140 || ecc == 50 && W < 11 || ecc == 80 && W < 13
|| ecc == 100 && W < 13 || ecc == 140 && W < 17)
{
fprintf (stderr, "ECC%03d invalid for %dx%d\n", ecc, W, H);
return 1;
}
} else if (W)
{ // even size
if (W < H)
{
int t = W;
W = H;
H = t;
}
if (!eccstr)
ecc = 200;
if (ecc != 200)
{
fprintf (stderr, "ECC%03d invalid for %dx%d\n", ecc, W, H);
return 1;
}
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -