?? pdf417_enc.c
字號:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/stat.h>#include <fcntl.h>#include <errno.h>#define OUTPUT_GIF 1#define OUTPUT_PBM 2#define OUTPUT_PS 3#define OUTPUT_RAW 4#define OUTPUT_PS_BITS 5#define OUTPUT_EPS 6// #define SHORT_PS need to define this if you want old style postscript// ending/*This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; *//*The pdf417_encode program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITYor FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library PublicLicense for more details. *//* usage: pdf417_encode infile outfile [rows] [cols] [ec_level] *//* the infile and outfile args are required and specify the input and *//* output files respectively. The optional rows and cols arguments *//* allow the user to specify number of rows ( < 90) and number of columns *//* ( < 30) in the output barcode. The default if these args are not *//* supplied is 24 rows and 8 columns. Also if rows are supplied, then *//* columns must be supplied also. The last paramater is the ec_level*//* This specifies the level of error correction to use. It is optionally *//* supplied only if rows and columns are also supplied *//* This work is based somewhat on the pdf417_enc code written by *//* Ian Goldberg at UC-Berkeley *//* *//* Authors: John Lien (jtlien@charter.net) *//* Laszlo Agoston lagaston@bmw.co.at *//* Tom Tollenaere t2@pophost.eunet.be (gif output routines ) *//* Useful examples provided by "Tom Tollenaere" t2@pophost.eunet.be *//* Revisions: 1.0 4/18/00 Initial release *//* Revisions: 1.1 4/31/00 Added code to support differenct error *//* correction levels *//* Revisions: 1.2 6/08/00 Fixed the PBM output file *//* Revisions: 1.3 11/28/00 Fixed overrun problem found by Laszlo *//* Combined get_cvalue and get_ctype *//* Routines (per Laszlo ) *//* Fixed problems with byte_compress *//* remainder > 900 should be *//* remainder > 899 *//* Also in numeric compress *//* fix b900_result[mm] > 900 *//* Should be b900_result[mm] > 899 *//* Revisions: 1.4 12/6/00 Add code to do \NL \CR \LF in text compress*//* Revisions: 1.5 12/7/00 Fix problem with 901 code at beginning *//* and default to text compress mode *//* Fix problem in do_row_begin and *//* do_row_end. Number of columns *//* was wrong for right indicator *//* and left indicator : *//* Rewrote the text_compress to work *//* better. *//* *//* Revisions: 1.6 12/8/00 Fixed more problems with text_compress *//* Folded in Tom Tollenaere gif output code *//* Update Makefile *//* Revisions: 1.7 3/13/01 Fixed problem with PDF417_STOP *//* Submitted by "nobody" at sourceforge *//* *//* Revisions: 1.8 5/17/01 Fixed problem switching between *//* text compress and byte compact mode *//* Found by Chris Cowan and Ray Sweeney *//* Revisions: 1.9 6/17/01 Added 900 pads for case when number *//* codewords used for message plus *//* Error Codewords < row * cols -2 *//* also error if number of codewords is too *//* large. Thanks to Frederic Ramanaga for this *//* Added test for ecc syndromes to make *//* sure ecc is calculated correctly *//* Also, added Reed Solomon Decoder *//* Code from Phil Karns rs.c *//* (Not debugged yet...) *//* Revisions: 2.0 10/12/01 Reed Solomon Decoder success *//* if test_ecc is turned on, ecc can *//* now be decoded!!!! *//* Checked up to four errors corrected *//* at a time *//* Revisions: 2.1 10/21/01 Fix problems with rows = 999 *//* and postscript output *//* Thanks to Jeff Chua *//* Revisions: 2.2 10/24/01 Fix problems with rows = 999 *//* and postscript output, added top and *//* bottom clear space to postscript out *//* Revisions: 2.3 11/04/01 Fix problems with rows = 999 *//* Added better command line handling *//* submitted by Andrew Baker *//* Revisions: 2.4 11/21/01 Fixes for easier Virtual C++ compile *//* Thanks to Chris Wille *//* New pdf417_en function that does most *//* of the work *//* Revisions: 2.5 11/30/01 Fixes for problems with text compress *//* found by Tomas Gruszewski *//* *//* Revisions: 2.6 12/11/01 Made stdout default output for ps, pbm *//* Added -t psbits for ps bitmap only *//* Made the in_data lines wider *//* All to make this easier to use as *//* as a printer filter. *//* Changes due to Stephan Leemburg *//* Also, Raod Plank had some fixes to *//* allow input lines > 256, also as a fix *//* to the nc routine *//* Revisions: 2.7 12/19/01 Removed some dead code from generateEC *//* routine. *//* Commented out some code in eras_dec_rs *//* that caused some problems when using *//* erasures. *//* Revision 2.8 10/25/02 No changes, just added the scan *//* binary to decode the generated *//* barcode *//* Revision 3.1 4/13/03 Fixed a bug in PDF417_encodeGIF *//* Found by Dash Wendrzyk *//* Revision 3.2 6/06/03 Added a makefile for freeBSD and *//* code in main.c for bsd compilation *//* Thanks to Ken Marx *//* Revision 3.3 7/04/03 Added code to demonstrate usage as *//* a callable shared library *//* lib_test.c is an example of a *//* program calling the library functions *//* pdf417.h is the include used by caller *//* make lib will make libary *//* *//* Revision 3.4 9/01/03 Fixed some errors in the pdf417_dec *//* program. *//* *//* Revision 3.5 10/10/03 Fixed bug in get_line that changes HT *//* ( Horizontal Tab) to Space *//* Added code to allow double quotes *//* in TC ( text compress) using \DQ *//* Added the code written by Vladislav *//* Naumov with help from Paulo Soares *//* to allow support for Macro PDF *//* you can now have a line such as *//* MC "FILE ID XXXXX Segment 2/2" *//* That specified a macro pdf file id *//* and macro pdf segment index. This *//* must be the last line of input file *//* Also fixed bug in text_compress that *//* failed for puctuation followed by space *//* *//* Revision 3.6 11/30/03 Added Borland subdirectory of code *//* for Borland C++ due to Robert Grobau *//* of U.S. Navy. Added support for *//* stdin file and stdout file ( just *//* replace normal file name with _ ) *//* *//* *//* *//* *//* NOTE ON COMPILATION for non-linux systems!!!!! *//* use the Makefile.nogif *//* and remove the #define DO_GIFS definition from the source *//* otherwise, the compilation expects the gif_lib.h to be in *//* /usr/include/gif_lib.h *//* you can get the gif lib at *//* ftp://prtr-13.ucsc.edu/pub/libungif/libungif-4.1.0b1.tar.gz *//* */#include <ctype.h>#include "globs.h"#include "bits.h"#include "pdf417.h"#ifdef DO_GIFS#include "gif_lib.h"#define min(a,b) ((a) < (b) ? (a) : (b))static GifColorType EGAPalette[] = { {0, 0, 0}, /* 0. Black */ {0, 0, 170}, /* 1. Blue */ {0, 170, 0}, /* 2. Green */ {0, 170, 170}, /* 3. Cyan */ {170, 0, 0}, /* 4. Red */ {170, 0, 170}, /* 5. Magenta */ {170, 170, 0}, /* 6. Brown */ {170, 170, 170}, /* 7. LightGray */ {85, 85, 85}, /* 8. DarkGray */ {85, 85, 255}, /* 9. LightBlue */ {85, 255, 85}, /* 10. LightGreen */ {85, 255, 255}, /* 11. LightCyan */ {255, 85, 85}, /* 12. LightRed */ {255, 85, 255}, /* 13. LightMagenta */ {255, 255, 85}, /* 14. Yellow */ {255, 255, 255}, /* 15. White */};#define EGA_PALETTE_SIZE (sizeof(EGAPalette) / sizeof(GifColorType))static intHandleGifError (GifFileType * GifFile){ int i = GifLastError (); if (EGifCloseFile (GifFile) == GIF_ERROR) { GifLastError (); } return i;} /* HandleGifError */#endif/* * that would be nice to pre-declare * all functions before use */void text_compress (char *);void do_mode_latch (int incode);voidput_back (int inval){ int debug; debug = 0; if (debug) { printf ("Code = %d codeindex = %d \n", inval, codeindex); } if (debug) { printf (" left = %d right = %d \n", inval / 30, inval % 30); } if (codeindex < 1900) { codes[codeindex] = inval; data[codeindex] = inval; codeindex += 1; }} /* put back */voidgenerateEC (UInt32 * data, UInt32 len, UInt32 EClen){ static int mods128[128] = { 539, 108, 4, 801, 86, 550, 315, 834, 517, 37, 865, 827, 297, 539, 330, 211, 34, 804, 587, 814, 130, 354, 454, 845, 898, 375, 269, 600, 726, 242, 374, 157, 616, 670, 432, 87, 614, 89, 336, 754, 602, 380, 262, 382, 378, 627, 663, 784, 898, 808, 821, 228, 48, 507, 90, 447, 270, 95, 732, 156, 763, 297, 491, 171, 776, 646, 463, 712, 379, 40, 173, 278, 775, 192, 287, 236, 186, 129, 219, 193, 569, 860, 606, 686, 432, 684, 96, 272, 292, 674, 723, 594, 907, 457, 258, 516, 704, 490, 908, 292, 631, 447, 148, 246, 620, 583, 244, 928, 208, 217, 93, 822, 415, 749, 925, 400, 444, 897, 779, 53, 379, 296, 580, 858, 547, 864, 310, 521 }; static int mods256[256] = { 10, 497, 55, 648, 558, 924, 159, 307, 543, 642, 555, 497, 95, 688, 251, 404, 173, 481, 579, 505, 656, 41, 402, 83, 449, 872, 29, 167, 20, 189, 829, 609, 532, 910, 191, 922, 299, 834, 54, 134, 297, 54, 213, 760, 749, 228, 383, 605, 754, 893, 791, 180, 441, 609, 60, 620, 280, 394, 836, 238, 500, 144, 32, 795, 49, 118, 503, 528, 726, 421, 434, 773, 231, 60, 756, 755, 697, 309, 655, 852, 261, 544, 895, 621, 732, 60, 48, 641, 805, 707, 689, 193, 455, 640, 607, 752, 177, 384, 722, 234, 687, 673, 142, 605, 192, 601, 374, 318, 263, 71, 739, 130, 479, 320, 628, 134, 10, 462, 188, 588, 355, 580, 802, 49, 329, 903, 655, 199, 743, 290, 2, 90, 906, 538, 136, 585, 848, 433, 730, 830, 463, 269, 917, 255, 66, 731, 637, 470, 289, 225, 517, 8, 51, 511, 103, 908, 399, 358, 19, 712, 803, 291, 307, 521, 849, 376, 334, 461, 684, 812, 749, 162, 877, 274, 490, 793, 329, 70, 315, 884, 209, 549, 284, 257, 216, 240, 325, 694, 370, 859, 353, 668, 592, 418, 439, 137, 799, 700, 801, 913, 540, 605, 796, 204, 11, 569, 68, 89, 919, 814, 710, 757, 828, 280, 201, 280, 194, 733, 438, 850, 375, 190, 275, 913, 311, 194, 858, 720, 138, 786, 905, 250, 708, 586, 82, 204, 74, 857, 882, 766, 75, 894, 524 }; int mods512[512] = { 263, 303, 121, 31, 244, 389, 410, 675, 738, 282, 304, 366, 251, 863, 310, 63, 647, 849, 223, 768, 752, 357, 655, 498, 325, 600, 726, 165, 899, 332, 164, 407, 780, 45, 919, 141, 669, 877, 411, 646, 138, 736, 284, 596, 20, 111, 45, 717, 433, 381, 14, 134, 175, 533, 752, 762, 321, 297, 531, 190, 464, 616, 171, 447, 656, 46, 610, 721, 486, 316, 540, 576, 672, 640, 488, 667, 105, 534, 299, 342, 316, 669, 54, 321, 749, 242, 424, 311, 923, 39, 5, 330, 742, 720, 357, 37, 842, 687, 379, 691, 366, 62, 353, 751, 667, 116, 923, 183, 296, 610, 273, 375, 437, 286, 336, 535, 425, 16, 62, 187, 817, 907, 422, 155, 308, 498, 162, 660, 791, 469, 73, 281, 922, 843, 303, 905, 815, 210, 782, 673, 535, 699, 881, 321, 249, 548, 216, 425, 731, 590, 806, 459, 915, 647, 31, 378, 795, 473, 309, 851, 808, 397, 665, 756, 310, 560, 87, 61, 631, 307, 224, 720, 837, 838, 797, 242, 341, 252, 56, 513, 662, 381, 537, 141, 827, 892, 300, 207, 441, 305, 420, 789, 905, 644, 519, 437, 922, 830, 613, 228, 492, 54, 369, 407, 66, 18, 680, 787, 45, 184, 247, 331, 751, 695, 632, 757, 269, 346, 597, 77, 833, 424, 586, 618, 777, 35, 536, 289, 860, 632, 841, 617, 870, 579, 410, 708, 353, 248, 310, 677, 772, 181, 365, 563, 608, 109, 693, 343, 564, 563, 209, 158, 417, 193, 59, 624, 729, 672, 159, 713, 663, 873, 145, 62, 559, 714, 644, 560, 521, 721, 383, 842, 408, 43, 688, 699, 777, 589, 327, 511, 394, 594, 288, 245, 420, 472, 711, 283, 911, 578, 364, 764, 850, 475, 777, 662, 119, 427, 43, 552, 332, 298, 124, 37, 578, 452, 591, 699, 51, 651, 463, 35, 173, 244, 342, 167, 452, 902, 45, 669, 820, 533, 466, 684, 51, 289, 797, 856, 323, 578, 361, 248, 95, 771, 729, 152, 543, 851, 499, 2, 521, 114, 494, 8, 94, 707, 736, 827, 836, 850, 357, 288, 785, 358, 92, 221, 477, 164, 512, 841, 861, 65, 229, 864, 162, 575, 708, 40, 402, 304, 168, 89, 710, 676, 207, 531, 429, 151, 237, 156, 641, 415, 381, 570, 303, 786, 596, 328, 826, 190, 168, 384, 610, 51, 848, 768, 395, 794, 518, 240, 258, 516, 192, 513, 390, 744, 567, 729, 899, 653, 76, 561, 536, 287, 684, 808, 142, 913, 593, 451, 297, 20, 180, 482, 62, 99, 85, 855, 407, 681, 204, 290, 741, 658, 860, 784, 107, 761, 472, 752, 98, 485, 800, 383, 272, 122, 289, 542, 41, 552, 916, 858, 37, 545, 632, 290, 499, 476, 102, 644, 390, 408, 781, 539, 534, 794, 268, 640, 621, 781, 346, 249, 666, 203, 543, 435, 520, 327, 75, 846, 781, 352, 193, 87, 88, 306, 871, 294, 643, 229, 299, 914, 155, 920, 265, 197, 492, 350, 380, 285, 498, 118, 574, 409, 207, 428, 599, 35, 504, 373, 77, 352 }; UInt32 base_reg[800]; UInt32 coeff_reg[800]; UInt32 i, j; int tint; UInt32 temp; UInt32 wrap; int debug; debug = 0; for (i = 0; i < len; i += 1) { if (debug) { printf ("Data in = %d %d \n", i, data[i]); } } if (debug) { printf (" In generateEC - data length = %d \n", len); printf (" In generateEC - ECC length = %d \n", EClen); } /* get the coefficients */ if (EClen < 128) { for (i = 0; i < EClen; i += 1) { coeff_reg[i] = mods[EClen][i]; if (debug) { printf ("Set coeff_reg %d to %d \n", i, coeff_reg[i]); } } } else { switch (EClen) { case 128: for (i = 0; i < EClen; i += 1) coeff_reg[i] = mods128[i]; break; case 256: for (i = 0; i < EClen; i += 1) coeff_reg[i] = mods256[i]; break; case 512: for (i = 0; i < EClen; i += 1) coeff_reg[i] = mods512[i]; break; default: fprintf (stderr, "bad EClen: %d\n", EClen); exit (1); break; }; } /* initialize b regs */ for (i = 0; i < EClen; i += 1) { base_reg[i] = 0; } /* Initialize data */ for (i = len; i < len + EClen; i += 1) data[i] = 0; /* Load up with data */ for (i = 0; i < len; ++i) { wrap = (base_reg[EClen - 1] + data[i]) % GPRIME; /* if (wrap) wrap = GPRIME-wrap; */ for (j = EClen - 1; j > 0; j = j - 1) { temp = (coeff_reg[EClen - 1 - j] * wrap) % GPRIME; temp = (GPRIME - temp) % GPRIME; base_reg[j] = (base_reg[j - 1] + temp) % GPRIME; } temp = (coeff_reg[EClen - 1] * wrap) % GPRIME; temp = (GPRIME - temp) % GPRIME; base_reg[0] = temp; } /* Read off the info */ for (j = 0; j < EClen; j += 1) { if (debug) { printf ("Before compl base_reg %d = %d \n", j, base_reg[j]); } base_reg[j] = (GPRIME - base_reg[j]) % GPRIME; } for (j = 0; j < EClen; j += 1) { if (debug) { printf ("Adding ECC byte %d = %d \n", j, base_reg[EClen - 1 - j]); } /* data[len+j] = base_reg[j]; */ tint = base_reg[EClen - 1 - j]; put_back (tint); }}// initialize table of 3**i for syndrome calculation//voidpowers_init (){ int ii; int power_of_3; int debug; debug = 0; power_of_3 = 1; Index_of[1] = GPRIME - 1; for (ii = 0; ii < GPRIME - 1; ii += 1) { powers_of_3[ii] = power_of_3; Alpha_to[ii] = power_of_3; if (power_of_3 < GPRIME) { log_of_3[power_of_3] = ii; if (ii != GPRIME - 1) { Index_of[power_of_3] = ii; } } else { printf ("Internal error: powers of 3 calculation \n"); } if (debug) { printf ("pow = %d ii = %d \n", power_of_3, ii);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -