⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdf417_dec.c

📁 PDF417编码的算法的源程序
💻 C
📖 第 1 页 / 共 5 页
字号:
    }
  else
    {
      est_bar_width = 1;
    }

  if (debug)
    {
     printf("est_bar_width = %d \n", est_bar_width);
     for ( kk = 0; kk < vert_line_cnt; kk += 1) 
       {
         printf("kk = %d pix_col_xline = %d \n", kk , pix_col_xline[kk]);
       }
    }

}



// find the differences in the x ( row ) grey values 
//  for each y location in pix_col data
//

void do_pixcol_diffx(int inwidth, int inheight)
{
  int ii;
  int jj;
  int kk;
  int diff_data[OUT_MAX_X_PIXEL];
  float diff_tots[OUT_MAX_Y_PIXEL];
  float first_deriv[OUT_MAX_Y_PIXEL];
  float total_diff;
  float prev_total;
  float cum_total;
  long avg_total;
  int prev_line;
  int y_space;
  int pix_ydiffs_tot;
  int debug;
  int avg_yspace;

  debug = 0;
  y_space = 0;

  diff_data[0] = 0;
  diff_data[1] = 0;
  diff_data[2] = 0;

  cum_total = 0.0;
  total_diff = 0.0;
  prev_total = 0.0;

  if (debug) { printf("In do_pixcols_diffx , inwidth = %d , inheight = %d \n",
		      inwidth, inheight); }

  for ( jj = 3; jj < inheight; jj += 1)
    {
      total_diff = 0.0;
      for ( ii = 0; ii < inwidth ; ii += 1)
	{
	  if ( ii < OUT_MAX_X_PIXEL)
	    {
            diff_data[ii] = abs(pix_cols[ii][jj].greyval
                                    - pix_cols[ii][jj-3].greyval);
            }
          else
	    {
	      printf("diff_data array size exceed \n");
              exit(0);
            }
          
          total_diff = total_diff + (float) diff_data[ii];
        }
      if ( jj < OUT_MAX_Y_PIXEL)
	{
         diff_tots[jj]= total_diff;
         cum_total = total_diff + cum_total;
         first_deriv[jj] = total_diff - prev_total;
         prev_total = total_diff;
        }
      else
        {
	  printf("diff_tots array size exceeded \n");
          exit(0);
        }
     }  

  if (debug) { printf("Completed j = 3 to inheight loop  -inheigth = %d\n",
                     inheight); }

  if ( inheight != 0)
    {
     avg_total = (int) (cum_total/(float) inheight);
    }
  else
    {
      printf("In diff_col_pix_x , inheight = 0, internal error \n");
      exit(0);
    }

  if (debug) { printf("avg_total = %d \n", avg_total); }
  if (debug)
    {
     printf("avg total = %d \n", avg_total);
    }
  for ( jj = 0; jj < inheight; jj += 1)
    {
      if (debug)
	{
         printf(" jj = %d diff_tots = %f 1st deriv = %f \n",jj, diff_tots[jj],
                first_deriv[jj]);
        }
    }

  kk = 0;
  prev_line = 0;
  pix_ydiffs_tot = 0;
  for ( jj = 5; jj < inheight; jj += 1)
    {
      if (( first_deriv[jj] > -1.0) && ( first_deriv[jj+1] < 0.0))
        {
          if (  diff_tots[jj] > (avg_total  * 2))
            {
             
              if ( kk < MAX_LINES_Y)
		{
		  pix_col_yline[kk] = jj;
                  if ( debug)
		    { printf("Added to pix_col_yline ,kk, val = %d %d \n",
			     kk, pix_col_yline[kk]); }
                  if ( kk > 1)
                    {
		      pix_col_ydiffs[kk-1] = pix_col_yline[kk] -
                                              pix_col_yline[kk-1];
                      pix_ydiffs_tot += pix_col_ydiffs[kk-1];
                    }
                      
                  y_space = jj - prev_line;
                  prev_line = jj;  
                  kk += 1;
                }
              else
		{
		  printf("Too many horizontal lines in pix_col \n");
                }
              if (debug)
		{
                 printf("Candidate at jj = %d  y_space = %d \n",jj, y_space);
		}
            }
        }
    }

  if ( kk > 1)
    {
     avg_yspace = (pix_ydiffs_tot / (kk-1));
     if ( debug) {  printf("avg_yspace = %d \n", avg_yspace); }
    }
  if (debug) { printf("Completed j = 5 to inheight loop \n"); }
    
  if ( kk != 0)
    {
     est_bar_height = (inheight  )/ kk;
    }
  else
    {
      est_bar_height = 4;
      printf("Possible problem with bar height estimate, using 4 \n");
    }
 
  if (debug)
    {
     printf("est_bar_height = %d \n", est_bar_height);
    }

}

int test_black( int inx, int iny, int near)
{

  int tot;
  int avg_grey;

  if ( near == 1)
    {
      tot = 0;
      tot = tot + pix_cols[inx][iny].greyval;
      tot = tot + pix_cols[inx+1][iny].greyval;
      tot = tot + pix_cols[inx-1][iny].greyval;

      tot = tot + pix_cols[inx][iny-1].greyval;
      tot = tot + pix_cols[inx+1][iny-1].greyval;
      tot = tot + pix_cols[inx-1][iny-1].greyval;

      tot = tot + pix_cols[inx][iny+1].greyval;
      tot = tot + pix_cols[inx+1][iny+1].greyval;
      tot = tot + pix_cols[inx-1][iny+1].greyval;

      avg_grey = tot/9;
    }

  if ( near == 0)
    {
   
      tot =  pix_cols[inx][iny].greyval;
   
      avg_grey = tot;
    }

  if ( avg_grey < dark_threshold)
    {
      return(0);
    }
  else
    {
      return(1);
    }

}

int  bits_to_symbol( int *inbits)
{

  int ii;
  int mask;
  int outsym;

  ii = 0;
  mask = 1 << 16;
  outsym = 0;

  while (ii < 17)
    {
      if ( inbits[ii] == 1)
        {
	  outsym = outsym | mask;         
        }
      ii += 1;
      mask = mask >> 1;

    }
  return(outsym);
}

// get the top most left bars ( a 17 bar symbol)
// this will give hints about actual number of rows

void do_top_bar(int bar_height_in)
{

  int ii;
  int testx;
  int testy;
  int onewid;
  int bit[20];
  int bit_int;  // integer rep of bits
  int bitcode;
  int rowval;
  int rowind;
  int numrowsm1_div3;
  int numrow_est;
  int numrow_est2;
  int top_bar_index;
  int bot_bar_index;
  int ecc_val;
  int this_row;
  int num_col_est;
  int row_height_estlo;
  int row_height_esthi;
  int numrows;          // value gotten from both top and bottom
  int this_row_height;
  int init_testx;
  float base;
  int debug;

  debug = 0;

  if (debug)
    {
     printf("In do top bar \n");
    }

  onewid = (int) best_onewf; 
  //  testy = pix_col_yline[0]/ 2;
  
  testy = (est_bar_height / 2);
  
 
  y_begin = testy;
  base = ( best_onewf / 2);

  // get the top module from column one

  for (ii=0; ii < 17 ; ii += 1)
    {
      testx = (int) (( (float)ii * best_onewf)  + base);
      if (debug) {
	printf("In do top bar, testx , testy = %d %d \n", testx, testy); }
      bit[ii] = test_black( testx, testy, 1);
    }

  for (ii = 0; ii < 17; ii += 1)
    {
      if (debug) { printf("ii = %d bit[ii] = %d \n", ii, bit[ii]); }
    }

     bit_int = bits_to_symbol( bit );

     if (debug) { printf("bit_int = %x \n", bit_int); }

     top_bar_index = find_index(bit_int,0,0,0);

     if ( top_bar_index != -1 )
       {
         if (debug) { printf("Top bar index = %d \n", top_bar_index); }

	 bitcode = bit_codes[top_bar_index]-1;
         rowval = bitcode / 929;
         if ( rowval != 0)
           {
	     printf("Error: Top left bar code is not a row 0 code \n");
             exit(-1);
           }
         rowind = (bitcode % 929) / 30;
         numrowsm1_div3 = ( bitcode % 929) % 30;
         numrow_est = (3 * numrowsm1_div3) + 1;
         if (debug) { printf("Numrow_est = %d-%d\n", numrow_est,numrow_est+2);}
         row_height_estlo = ( bar_height_in / ( numrow_est + 2));
         row_height_esthi =  bar_height_in / numrow_est;
         if (debug)
	   {
             printf("bar_height_in = %d row_height_estlo, esthi = %d %d \n",
                 bar_height_in, row_height_estlo,
		row_height_esthi);        
           }
         row_height_avg = ( row_height_estlo + row_height_esthi) / 2;
       }
     else
       {
         printf("Top left bar code is not a correct bar \n");
       }

     if (debug) { printf("top_bar_index = %d \n", top_bar_index); }

    
     // testy = bar_height_in - (pix_col_yline[0]/ 2);

     // testy = total height of all bars - height of one bar /2

     testy = bar_height_in - (est_bar_height/2);
     init_testx = onewid /2 ;

     bot_bar_index = -1;
    
     base = ( best_onewf / 2);
     while(( bot_bar_index == -1 ) && ( base > -1 ))
       {
        
        // get the bottom bar code symbol

         for (ii=0; ii < 17 ; ii += 1)
          {
           testx = (int) (( (float)ii * best_onewf)  + base);
           // testx = testx + onewid;
           if (debug) {
             printf("In do left bot  bar, testx , testy = %d %d \n", 
                             testx, testy);
	   }
           
           bit[ii] = test_black( testx, testy, 0);
           if ( debug ) { printf("bit = %d \n",bit[ii]); }
          }

         bit_int = bits_to_symbol( bit );
         if (debug) { printf("bit_int for bottom symbol  = %x \n", bit_int); }
         base -=1;
        bot_bar_index = find_index(bit_int,1,0,0);
       }

     if ( bot_bar_index != -1 )
       {
         if (debug) { printf("Bottom bar index = %d \n", bot_bar_index); }

	 bitcode = bit_codes[bot_bar_index]-1;
         rowval = bitcode / 929;
         
         if (debug) {
            printf(" bottom bitcode = %d , rowval = %d \n", bitcode, rowval);
	 }
         if ( rowval == 0)
	   {
            rowind = (bitcode % 929) / 30;
            this_row = ( 3 * rowind) -1;
            numrowsm1_div3 = ( bitcode % 929) % 30;
            numrow_est2 = (3 * numrowsm1_div3) + 1;
            numrows = numrow_est;
            best_row_height = (bar_height_in / numrows);
            if (debug) { printf("best row height = %d \n", best_row_height); }
           }

         if ( rowval == 1)
	   {
            rowind = (bitcode % 929) / 30;
            this_row = ( 3 * rowind) -1;
            ecc_val = ( bitcode % 929) % 30;
           
            numrows = numrow_est + 1;
            best_row_height = ( bar_height_in / numrows);
            if (debug) { printf("best row height = %d \n", best_row_height); }
           }

         if ( rowval == 2)
	   {
            rowind = (bitcode % 929) / 30;
            this_row = ( 3 * rowind) -1;
            num_col_est = (( bitcode % 929) % 30) + 1;
            if (debug) {
              printf("Number of columns from left bars = %d \n", num_col_est);}

            numrows = numrow_est + 2;
            best_row_height = ( bar_height_in/ numrows);
            if (debug) { printf("best row height = %d \n", best_row_height); }
           }
         if ( numrows != this_row)
	   {
             if ( debug)
	       {
	        printf("Mismatch between row number calculations \n");
                printf("numrows = %d this_row = %d \n", numrows, this_row);
               }
           }
         else
	   {
             if (debug) { printf("numrows = %d \n", numrows); }
             this_row_height =  bar_height_in /  numrows;
      
	     if (debug) { printf("good row height = %d \n", this_row_height); }
            
           }

       }
     else
       {
         printf("Bottom left bar code is not a correct bar \n");
       }

}

// get symbols in an entire column
// 

void do_col_bars(int inrows)
{

  int ii;
  int jj;
  int testx;
  int testy;
  int onewid;
  int bit[20];
  int bit_int;
  int symbs[300];
  int debug;

  debug = 0;

  for ( jj = 0; jj < inrows; jj += 1)
    {
     onewid = (int) onewf; 
     testy = pix_col_yline[jj] - (est_bar_height/20);
     testx = 0;
     for (ii=0; ii < 17 ; ii += 1)
       {
        testx = testx + onewid;
        if (debug) {
          printf("In do col bars, testx , testy = %d %d \n", testx, testy); }

         bit[ii] = test_black( testx, testy, 1);
        }

     for (ii = 0; ii < 17; ii += 1)
       {
	 if (debug) { printf("ii = %d bit[ii] = %d \n", ii, bit[ii]); }
       }

     bit_int = bits_to_symbol( bit );


     if (( jj < 300) && ( jj > -1))
       {
	 symbs[jj] = bit_int;
       }
    }
}

// look thru the unslanted pix final data 
// and get all the x values where columns may start
//

void get_columns_start( int ylenin)
{
  int ii, jj, kk;
  int beginx;
  int endx;
  float col_avg;
  int bit_diff;
  float col_diff;
  float col_tot;
  float tot_l_avg;
  float tot_r_avg;
  int tot_lr_avg;
  int min_avg;
  int prev_lr_avg; 
  int tot_lr_incr;
  int col_cnt;
  int col_start;
  int col_incr;
  int debug;
  float lr_float_avg;
  float tot_lr_float;
  int lr_avg;

  int colcnt;
  

  debug = 0;

  
  if (debug) { printf("In  get columns start, begin_left_col - %d\n",
                      begin_left_col); }

  //beginx = 16 * onewidth;
  beginx = best_begin_left_col;
  endx = best_begin_r

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -