?? pdf417_dec.c
字號:
#include <ctype.h>
#include <stdio.h>
#include <math.h>
#include <pgm.h>
#include "bitcodes.h"
typedef unsigned int UInt32;
#define MAX_SQUARES 5000
#define MAX_LINE_PIXEL 17000 // a line all the way thru symbol
#define MAX_MOD_PIXEL 1700 // a line thru one modules worth = 17 bars
#define MAX_X_PIXEL 17000
#define MAX_Y_PIXEL 5000
#define OUT_MAX_X_PIXEL 6000
#define OUT_MAX_Y_PIXEL 2000
#define MAX_XPIXEL_MODULE 900 // number of x pixels in a 17 wide module
#define MAX_YPIXEL_MODULE 300 // number of y pixels in a 17 wide module
#define MAX_SYMBOL_ROWS 100 // standard limits it to 90
#define MAX_SYMBOL_COLS 330 // min 3 rows 929/3 = 310
#define MAX_LINES_X 300
#define MAX_LINES_Y 300
#define MAX_TOT_LR 5000
#define MAX_BARS 2000
#define MAX_PB 1500
#define MAX_MOD_BARS 5 // at most 5 bars in a module ( actually 4 )
#define MAX_LINE_SCANS 120
#define TRUE 1
#define FALSE 0
// scan defines
#define MAX_BYTE_INDEX 1024
#define MAX_CODE 2500
#define ALPHA 0
#define LOWER 1
#define MIXED 2
#define PUNCT 3
#define PUNCT_SH 4
#define MAX_ROW 91
#define MAX_COL 512
#define NULLCHAR '\0'
#define MAX_RAW_CODES 1500
#define MIN_ROW_HEIGHT 4
#define MIN_COL_WIDTH 4
#define EDGE_THRESHOLD 10
#define MAX_PIXEL_Y 2048
#define MAX_PIXEL_X 2048
#define NN 1024
#define PRIM 1
#define GPRIME 929
#define KK 32
#define A0 928
#define Ldec 1
#define sgn(x) ((x<0)?-1:((x>0)?1:0)) /* macro to return the sign of a
number */
void write_pgm_outpix_final( int num_cols, int num_rows);
void write_pgm_outmod(char *namestrin, int num_cols, int num_rows);
void write_pgm_outcol(char *namestrin, int num_cols, int num_rows);
void write_pgm_out( int num_cols, int num_rows);
void write_pgm_outpix( int num_cols, int num_rows);
FILE *wr_data_file;
FILE *infile1, *outfile1,*infile;
int fail;
char inargstr[120];
char pgm_infile_str[120];
int write_mods; // flags to control writing of pgm outputs
int write_final_pix; // write the symbol after unslanting
int write_leftcol; // write the left column of the symbol
int write_cols; // write the individual columns
int write_outpix; // write the symbol before unslanting
int write_data; // write the bitcodes for each row, and col
int box_x_incr; // size of box used to find the symbol
int box_y_incr; // inside the white space that should surround it
int slope_array[100];
int fig_flag;
int tot_lr[MAX_TOT_LR]; // used by get_columns_start
int tot_lr_in[MAX_TOT_LR]; // 1st derivative of tot_lr
int begin_left_col; // beginning pixel of left column
int begin_right_bars; // beginning pixel of right bars
int min_box_x;
int min_box_y;
int max_box_x;
int max_box_y;
int bytes_per_pix; // bytes per pixel
int col_begin_x[400]; // beginning x value of each column
float onewidthf;
int onewidth;
float onewf;
int num_sym_cols;
int anal_mod_index; // successful index for analyze module
int rule_mod_index; // successful index for rule_checker
int slide_mod_index;
int xline_mod_index;
int symbol_data[MAX_SYMBOL_ROWS][MAX_SYMBOL_COLS];
// put the codes for rows and columns here
int bestfig;
int pixel_out_xlen; // size of the pix_out in x direction
int pixel_out_ylen; // size of the pix_out in y direction
int dir;
int slope;
int pix_slope; // final correction slope
float pix_slopef;
int best_normal_x;
int best_normal_y;
int best_normal_dir;
int best_onewidth;
int best_begin_left_col;
int best_begin_right_bars;
int best_num_sym_cols;
int best_pix_slope;
int best_normal_slope;
int best_pixel_out_xlen;
int best_pixel_out_ylen;
float best_onewf;
int left_highy; // highest y value in pix_out at middle of left fat bar
int right_highy; // highest y value in pix_out at middle of right fat bar
int left_highx;
int right_highx;
int y_begin; // this is the estimated center of the top row
int scan_upleft_x; // these are the beginning points of places to scan from
int scan_upleft_y;
int scan_cenleft_x;
int scan_cenleft_y;
int scan_lowleft_x;
int scan_lowleft_y;
int out_cnt;
int save_line_cnt; // should not be more than 36
int dark_sqindex; // indexs all the dark squares in the original image
int dark_threshold; // average grey value of a dark square
int line_threshold;
int lpixel;
int pixel_cnt;
int post_pixel_cnt;
int mod_pixel_cnt;
int symbol_height; // pixel count height of symbol at left wide bar
int number_of_rows; // number of rows in pixel map
int number_of_cols; // number of columns in pixel map
int row_height_avg; // avg of row_height high and low estimate
int best_row_height; // best estimate of row height
int best_col_width_avg; // best estimate of avg column width
int pix_col_yline[MAX_LINES_Y]; // y co-ordinant of lines in column data
int pix_col_xline[MAX_LINES_X]; // x co-ordinant of lines in column data
int pix_col_ydiffs[MAX_LINES_Y];
int vert_line_cnt;
void draw_normal(int nx, int ny, int nslope, int ndir, int ylen);
void line_fast_norm(int x1, int y1, int x2, int y2);
void find_height(int nx, int ny, int nslope, int ndir, int ylen);
void final_pix(int xlen, int ylen );
void find_left_edge(int nx, int ny, int nslope, int ndir, int ylen, int xlen);
void get_a_column(int indexin, int startx, int onewid);
struct pixelod
{
int greyval;
} pix_in[MAX_X_PIXEL * MAX_Y_PIXEL];
struct pixeluu
{
int greyval;
} pix_module[MAX_XPIXEL_MODULE][MAX_YPIXEL_MODULE];
struct pixeltt
{
int greyval;
} pix_cols[OUT_MAX_X_PIXEL][OUT_MAX_Y_PIXEL];
struct pixelzz
{
int greyval;
} pix_out[OUT_MAX_X_PIXEL][OUT_MAX_Y_PIXEL];
struct pixelqq
{
int greyval;
} pix_final[OUT_MAX_X_PIXEL][OUT_MAX_Y_PIXEL];
struct pixeldo
{
int greyval;
} pixel_array[MAX_X_PIXEL][MAX_Y_PIXEL];
struct darksqstuff
{
int xloc;
int yloc;
} dark_squares[MAX_SQUARES];
// codes from bit analysis go here
struct pb_stuff
{
int rowval;
int colval;
int codeval;
} pb_array[MAX_PB];
int bar_cnt; // count of bars
int pb_index;
int mod_bar_cnt; // count of bars in a particular module
int b_box_xlen;
int b_box_ylen;
int b_box_maxx;
int b_box_maxy;
int b_box_minx;
int b_box_miny;
float max_theta; /* Maximum theta range */
int barwidth;
struct barstuff
{
int beginx;
int beginy;
int endx;
int endy;
int space_width; // width of space before bar
int width;
int lpixel;
} bar_array[MAX_BARS];
struct mbarstuff
{
int beginx;
int beginy;
int endx;
int endy;
int space_width; // width of space before bar
int width;
int lpixel;
} mod_bar_array[MAX_MOD_BARS];
int mod_bit_array[40];
struct linesavestuff
{
int pixelcnt;
int slope;
int dir;
int beginx;
int beginy;
int has_8111; // begin
int has_7111; // end
int endlpixel;
int endwidth; // width of 7 wide end bar
int beginwidth; // width of begin 8 wide bar
int barcnt;
int bars_found;
int normal_val; // find the number of pixels hit by normal
int line_vals[MAX_LINE_PIXEL];
int line_x[MAX_LINE_PIXEL];
int line_y[MAX_LINE_PIXEL];
struct barstuff tbar[8];
int fig;
} save_line_data[MAX_LINE_SCANS];
gray **greydata;
int norm_fdark_ind; // number of pixels before first dark in norm
// line between one of the left start bars
int norm_line_data[MAX_LINE_PIXEL];
int normal_x[MAX_LINE_PIXEL];
int normal_y[MAX_LINE_PIXEL];
int post_line_data[MAX_LINE_PIXEL];
int post_x[MAX_LINE_PIXEL];
int post_y[MAX_LINE_PIXEL];
int line_data[MAX_LINE_PIXEL];
int mod_line_data[MAX_MOD_PIXEL];
int temp_data[MAX_LINE_PIXEL];
int line_x[MAX_LINE_PIXEL];
int line_y[MAX_LINE_PIXEL];
int mod_line_x[MAX_MOD_PIXEL];
int est_bar_height; // estimate of bar height found from diffy
int bar_height; // best estimate bar height found from hieght of symbol
// divided by number rows
// scan global variables
int pixel_dark( int inx, int iny);
int Alpha_to[1024];
int Index_of[1024];
int powers_of_3[1024];
int log_of_3[1024];
int verbose_out;
int use_image; // set true if getting bit image from file
typedef struct raw_code_type {
int code;
int row;
int col;
} raw_codes_type;
raw_codes_type raw_codes[MAX_RAW_CODES];
int total_codes;
int top_left_corn_x;
int top_left_corn_y;
int x_width; // pixel width of scan in x direction
int y_height; // pixel height of scan in y direction
int row_last_code; // last code in row
int col_count;
int pixel_x;
int pixel_y;
int expected_cols;
int col_width;
float fcol_width;
int row_height;
int barcode_height;
int barcode_width;
int code_index;
int code_index_t; // code_index using t array
int scan_code_array[128][40];
int top_data_corner;
int left_data_corner;
int left_edge;
int right_edge;
int top_edge;
int bottom_edge;
int end_bar_found;
int x_center;
int y_center;
int row_rem;
int synd_array[512];
int rows_cols[MAX_ROW][MAX_COL];
int number_of_codes;
int rows_from_raw;
int columns_from_raw;
int byte_compress_mode;
int text_compress_mode;
int numeric_compress_mode;
int code_array[MAX_CODE];
int code_array_size;
int byte_array[MAX_BYTE_INDEX];
struct b913_stuff
{
char str913[20];
} byte_913_str[MAX_BYTE_INDEX];
char punct_chars[32];
char mixed_chars[32];
int codes_bc[20];
int codes_nc[20];
int codeindex;
int code;
int number_check_words;
int end_found;
int raw_row_col [90] [100];
char powers_of_900[16][50];
char powers_of_900h[10][50];
char powers_of_900h_x10[10][50];
char powers_of_900h_x100[10][50];
char result[256];
int data[1024];
int erase_loc[MAX_RAW_CODES]; // where are the erasures?
int erase_count;
void init_slopes()
{
slope_array[0] = 0;
slope_array[1] = 2;
slope_array[2] = 4;
slope_array[3] = 6;
slope_array[4] = 10;
slope_array[5] = 12;
slope_array[6] = 14;
slope_array[7] = 16;
slope_array[8] = 18;
slope_array[9] = 20;
slope_array[10] = 23 ;
slope_array[11] = 26;
slope_array[12] = 29;
slope_array[13] = 32;
slope_array[14] = 36;
slope_array[15] = 40;
slope_array[16] = 44;
slope_array[17] = 48;
slope_array[19] = 52;
slope_array[20] = 56;
// slope_array[12] = 214;
// slope_array[13] = 274;
// slope_array[14] = 373;
}
// find all the squares in the image that are darker than a certain
// thresshold. Put the center x,y location of these squares in the
// array dark_squares
void find_dark_squares(int x_left, int y_top, int x_size, int y_size)
{
int ii;
int jj;
int debug;
int x_center;
int y_center;
int y_tot;
int xy_tot;
int y_avg;
int xy_avg;
debug = 0;
if ( debug )
{
printf("In find dark sq , in x,y = %d %d \n", x_left, y_top);
}
xy_tot = 0;
for(ii = x_left; ii < x_left + x_size; ii += 1)
{
y_tot = 0;
for ( jj = y_top; jj > y_top - y_size; jj -=1 )
{
y_tot = y_tot + pixel_array[ii][jj].greyval;
}
if ( y_size > 0.0)
{
y_avg = y_tot / y_size;
}
else
{
printf("\n y_size not positive = %f \n", y_size);
exit(0);
}
xy_tot = xy_tot + y_avg;
}
if ( x_size > 0 )
{
xy_avg = xy_tot / x_size;
}
else
{
printf("\n x_size not positive = %f \n", x_size);
exit(0);
}
if ( debug) { printf("xy_avg = %d \n", xy_avg); }
if ( xy_avg > dark_threshold)
{
x_center = x_left + (( x_size) / 2);
y_center = y_top - ((y_size) / 2);
if ( dark_sqindex < MAX_SQUARES)
{
dark_squares[dark_sqindex].xloc = x_center;
dark_squares[dark_sqindex].yloc = y_center;
dark_sqindex += 1;
if (debug)
{
printf("Found a darks square at = %d %d \n", x_center, y_center );
}
}
else
{
printf("Error: Number of dark boxes too big = %d \n",
dark_sqindex);
}
}
}
// draw horizontal and vertical lines
//
void draw_pixel_line(int width, int beginx, int beginy, int endx, int endy )
{
int ii,jj;
if (( beginx - endx ) == 0) // vertical
{
if ( endy > beginy )
{
for ( ii = beginy; ii < endy; ii += 1)
{
for ( jj = 0; jj < width; jj += 1)
{
if (( ii + jj < MAX_Y_PIXEL) && ( beginx < MAX_X_PIXEL))
{
pixel_array[beginx][ii+jj].greyval = 510;
}
}
}
}
else
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -