grp4comp.c
来自「NIST Handwriting OCR Testbed」· C语言 代码 · 共 1,421 行 · 第 1/4 页
C
1,421 行
initializes variables in preperation for compressing the first line ******************************************************************************//************************************************************************ Arguments ** --------- ** Passed in: ** params - structure storing information need for ** comparison and other tasks. ** Returned: ** params - structure storing information need for ** comparison and other tasks. *************************************************************************/void set_up_first_line_c(params)struct parameters *params;{ params->reference_line = (SHORT *)malloc( (params->max_pixel + Extra_positions) * sizeof(SHORT) ); params->coding_line = (SHORT *)malloc( (params->max_pixel + Extra_positions) * sizeof(SHORT) ); *(params->reference_line + 0) = Invalid; *(params->reference_line + 1) = params->max_pixel; *(params->reference_line + 2) = params->max_pixel; *(params->reference_line + 3) = params->max_pixel; /* initialize first changing element on coding line (A0 = -1) */ *(params->coding_line) = Invalid; params->pixel = 0; params->index = 0; params->previous_color = White; } /* end set_up_first_line_c() *//*********************************** crash_c *********************************** forces the program to crash and create a core file *****************************************************************************/void crash_c(){FILE *crash_program = NULL; fprintf(crash_program,"This will kill the program and create a core file");}/***************************************************************************//* Originally mode.c *//***************************************************************************/static SHORT A0, A0_color, A1, a2, b1, b2;/******************************* compress_line ******************************** compresses a single line of the image *****************************************************************************/void compress_line(params)struct parameters *params;{ #if Debug static SHORT line = 0; printf("\nLINE %d. ",line); line++;#endif A0 = Invalid; /* set A0 equal to imaginary first array element */ A0_color = White; A1 = 1; initialize_b1(params); b2 = b1 + 1; #if Debug printf("\nA0:%d A1:%d b1:%d b2:%d ", A0, *(params->coding_line+A1), *(params->reference_line+b1),*(params->reference_line+b2));#endif do { if (*(params->reference_line + b2) < *(params->coding_line + A1)) { pass_mode_c(params); continue; } else if (abs(*(params->coding_line+A1)-*(params->reference_line+b1)) <=3) vertical_mode_c(params); else horizontal_mode_c(params); #if Debug printf("\nA0:%d A1:%d b1:%d b2:%d ", A0, *(params->coding_line+A1), *(params->reference_line+b1),*(params->reference_line+b2));#endif } while( A0 < params->max_pixel); } /******************************* initialize_b1 ******************************** locates b1's first position in the reference line *****************************************************************************/void initialize_b1(params)struct parameters *params;{SHORT last_bit_of_b1; b1 = 1; last_bit_of_b1 = b1 & Last_bit_mask; while( ((*(params->reference_line +b1) <=A0) || (A0_color ==last_bit_of_b1)) && (*(params->reference_line + b1) < params->max_pixel) ){ b1++; last_bit_of_b1 = b1 & Last_bit_mask; } /* end while loop */ #if Debug printf("\nb1:%d :%d, A0:%d", b1, *(params->reference_line+b1), A0);#endif }/********************************** pass_mode_c ******************************** compresses a pass mode *****************************************************************************/void pass_mode_c(params)struct parameters *params;{ write_bits_c("0001"); #if Debug printf(" P ");#endif /* * Reset the value A0 points to to a'0 (the value that b2 points to). */ A0 = *(params->reference_line + b2); /* * Since A0 is now greater than the pixel b1 points to, both b1 and b2 * must be advanced twice to maintain the color difference between A0 and * b1, and the positional requirement that b1 point to a pixel greater than * the one A0 points to. */ b1 += 2; b2 += 2; /* * Note that the b's can be advanced by two positions without fear of * moving them beyond the last changing element because pass_mode cannot * occur if b2 is already pointing to max_pixel. */ } /****************************** vertical_mode_c ******************************** compresses a vertical mode *****************************************************************************/void vertical_mode_c(params)struct parameters *params;{SHORT difference; difference = *(params->coding_line + A1) - *(params->reference_line + b1); A0 = *(params->coding_line + A1); A0_color = !A0_color; A1++; #if Debug printf(" V%d ", difference);#endif switch(difference) { case 0: write_bits_c("1"); if(*(params->reference_line + b1) != params->max_pixel ) { b1++; b2++; } /* end if b1 is not on the last changing element */ break; case 1: write_bits_c("011"); b1++; b2++; if((*(params->reference_line + b1) <= A0) && (*(params->reference_line + b1) != params->max_pixel) ) { b1 += 2; b2 += 2; } break; case -1: write_bits_c("010"); if(*(params->reference_line + b1) != params->max_pixel ) { b1++; b2++; } /* end if b1 is not on the last changing element */ break; case 2: write_bits_c("000011"); b1++; b2++; if((*(params->reference_line + b1) <= A0) && (*(params->reference_line + b1) != params->max_pixel) ) { b1 += 2; b2 += 2; } break; case -2: write_bits_c("000010"); if(*(params->reference_line + b1 - 1) > A0 ) { b1--; b2--; } else if(*(params->reference_line + b1) != params->max_pixel){ b1++; b2++; } break; case 3: write_bits_c("0000011"); b1++; b2++; while ((*(params->reference_line + b1) <= A0) && (*(params->reference_line + b1) != params->max_pixel) ) { b1 += 2; b2 += 2; } break; case -3: write_bits_c("0000010"); if(*(params->reference_line + b1 - 1) > A0 ) { b1--; b2--; } else if(*(params->reference_line + b1) != params->max_pixel){ b1++; b2++; } break; default: printf("ERROR in vertical_mode_c() "); } /* end case of difference */ } /**************************** horizontal_mode_c ******************************** compresses a horizontal mode *****************************************************************************/void horizontal_mode_c(params)struct parameters *params;{SHORT run_length;#if Debug printf(" a2:%d H ",*(params->coding_line + a2));#endif a2 = A1 + 1; write_bits_c("001"); if(A0 == Invalid) /* on imaginary first pixel */ run_length = *(params->coding_line + A1); else run_length = *(params->coding_line + A1) - A0; write_run_length(run_length, A0_color ); /* the last bit contains the color of the changing element */ run_length = *(params->coding_line + a2) - *(params->coding_line + A1); write_run_length(run_length, !A0_color); /* * Must use !A0_color instead of A1 because in cases in which A1 occurs * on max_pixel, its color is bogus. */ /* NOTE: is the above statement true? if A1 were on max_pixel, you should not get horizontal mode. */ A0 = *(params->coding_line + a2); A1 = a2 + 1; while((*(params->reference_line + b1) <= *(params->coding_line + a2)) && ( *(params->reference_line + b1) < params->max_pixel) ) { b1 += 2; /* must move ahead by 2 to maintain color difference with */ b2 += 2; /* A0, whose color does not change in this mode. */ } } /***************************************************************************//* Originally write_bits_c.c *//***************************************************************************/static SHORT bit_place_mark; static int byte_place_mark;static unsigned char *output_area; static char write_one[Pixels_per_byte] = { (char)0x80, (char)0x40, (char)0x20, (char)0x10, (char)0x8, (char)0x4, (char)0x2, (char)0x1,};static char write_zero[Pixels_per_byte] ={ (char)0x7F, (char)0xBF, (char)0xDF, (char)0xEF, (char)0xF7, (char)0xFB, (char)0xFD, (char)0xFE,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?