?? grp4deco.c
字號:
/*# proc: grp4decomp - CCITT Group 4 decompresses an image.# proc:*//********************************************************************* File Name: grp4decomp.c ** Modified: Darlene E. Frederick ** Michael D. Garris ** Date: January 25, 1990 ** Package: CCITT4 compression routines ** ** Contents: ccitt4_decompress() ** read_compressed_file_into_memory() ** control_decompression() ** prepare_to_decompress() ** set_up_first_and_last_changing_elements_d() ** prepare_to_decompress_next_line() ** set_up_first_line_d() ** crash_d() * ********************************************************************/#include <grp4deco.h>#ifdef TIME#include <sys/time.h>#endif#define NOALLOC 0#define ALLOC 1int decomp_alloc_flag;int decomp_write_init_flag;int decomp_read_init_flag;static char *all_white, *output_area, *all_black; #ifdef TIMEstruct timeval t1, t2;struct timezone tz;#endif/********************************************************************* grp4decomp is the main routine of this file. It does pre- ** liminary setup, calls routines, and does final processing. *********************************************************************//********************************************************************* Arguments ** --------- ** Passed in: ** indata - buffer containing the compressed data. ** inbytes - the number of bytes in indata. ** width - Width in pixels of uncompressed data. ** height - Number of lines of uncompressed data. ** Returned: ** outdata - buffer containing the decompressed data** outbytes - the number of bytes in outdata. *********************************************************************/grp4decomp(indata,inbytes,width,height,outdata,outbytes)unsigned char *indata, *outdata;int inbytes, width, height, *outbytes;{struct compressed_descriptor compressed;struct decompressed_descriptor decompressed; compressed.pixels_per_line = width; compressed.number_of_lines = height; compressed.length_in_bytes = inbytes; compressed.data = (char *)indata; decomp_alloc_flag = NOALLOC; decomp_write_init_flag = True; decomp_read_init_flag = True; read_compressed_file_into_memory(&compressed); decompressed.data = (char *)outdata; control_decompression( &compressed, &decompressed ); *outbytes = (decompressed.pixels_per_line >> 3) * decompressed.number_of_lines;}/***************************** control_decompression ************************** calls the functions that decompress the compressed file *****************************************************************************//********************************************************************* Arguments ** --------- ** Passed in: ** compressed - structure containing the # of ** pixels per line, the number of ** lines, and the compressed data. ** Returned: ** decompressed - structure containing the # of ** pixels per line, the number of ** lines, and the compressed data. *********************************************************************/void control_decompression( compressed, decompressed )struct compressed_descriptor *compressed;struct decompressed_descriptor *decompressed;{struct parameters sole_parameters;struct parameters *params = &sole_parameters;#ifdef TIME SHORT i; tz.tz_minuteswest = 0; tz.tz_dsttime = 0; gettimeofday(&t1, &tz);#endif prepare_to_decompress( compressed, decompressed, params ); while(decompress_line( params ) != EOFB ) prepare_to_decompress_next_line( params ); /* memory deallocation added by Michael D. Garris 2/23/90 */ free(params->reference_line); free(params->coding_line); free(all_white); free(all_black);#ifdef TIME gettimeofday(&t2, &tz); printf("\ntime difference: %ld:%ld\n", t2.tv_sec - t1.tv_sec, t2.tv_usec - t1.tv_usec); for(i=0; i<5; i++) printf("%c",'\07');#endif}/************************ read_compressed_file_into_memory ********************* allocates memory for the compressed image *****************************************************************************//********************************************************************* Arguments ** --------- ** Passed in: ** compressed - structure containing the # of ** pixels per line, the number of ** lines, and the compressed data. ** Returned: ** compressed - structure containing the # of ** pixels per line, the number of ** lines, and the compressed data. *********************************************************************/void read_compressed_file_into_memory(compressed)struct compressed_descriptor *compressed;{ if(decomp_alloc_flag){ if((compressed->data = (char *)calloc(compressed->length_in_bytes, sizeof(char))) == NULL) { printf("\nCannot allocate enough memory for compressed file.\n"); exit(1); } } else if(compressed->data == NULL){ printf("\nNo memory allocated for input data!\n"); exit(1); }} /* end read_compressed_file_into_memory() */ /*************************** prepare_to_decompress **************************** initializes variables in preperation for decompression *****************************************************************************//********************************************************************* Arguments ** --------- ** Passed in: ** compressed - structure containing the # of ** pixels per line, the number of ** lines, and the compressed data. ** Returned: ** decompressed - structure containing the # of ** pixels per line, the number of ** lines, and the compressed data. ** params - structure storing information needed ** for comparison and other tasks. *********************************************************************/void prepare_to_decompress( compressed, decompressed, params )struct compressed_descriptor *compressed;struct decompressed_descriptor *decompressed;struct parameters *params;{ params->max_pixel = compressed->pixels_per_line; decompressed->pixels_per_line = compressed->pixels_per_line; decompressed->number_of_lines = compressed->number_of_lines; set_up_first_line_d( params ); prepare_to_read_bits( compressed->data ); if(decomp_alloc_flag){ decompressed->data = (char *)calloc( compressed->pixels_per_line *compressed->number_of_lines /Pixels_per_byte, sizeof(char) ); } else if(decompressed->data == NULL){ printf("\nNo memory allocated for decompressed data!\n"); exit(1); } prepare_to_write_bits_d( decompressed->data, (compressed->pixels_per_line / Pixels_per_byte) ); } /* end decompress() *//******************************* set_up_first_line_d *************************** initializes variables in preperation for decompressing the first line ******************************************************************************//********************************************************************* Arguments ** --------- ** Passed in: ** params - structure storing information needed ** for comparison and other tasks. ** Returned: ** params - structure storing information needed ** for comparison and other tasks. *********************************************************************/void set_up_first_line_d( 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->index = 0;} /* end set_up_first_line_d() *//******************* set_up_first_and_last_changing_elements_d ***************** initializes the first and last changing elements in the coding line ******************************************************************************//********************************************************************* Arguments ** --------- ** Passed in: ** params - structure storing information needed ** for comparison and other tasks. ** Returned: ** params - structure storing information needed ** for comparison and other tasks. *********************************************************************/void set_up_first_and_last_changing_elements_d(params)struct parameters *params;{ *(params->coding_line) = Invalid; /* * set up the imaginary first changing pixel with an illegal value. */ *(params->coding_line + ++params->index) = params->max_pixel; *(params->coding_line + ++params->index) = params->max_pixel; *(params->coding_line + ++params->index) = params->max_pixel; /* * set up three changing pixels at the end of the line, all of which * contain the end-of-line flag "max_pixel." It is necessary to create * three of these flags because the changing elements a0 - b2 are sometimes * advanced by more than one element at a time (and therefore could skip * over a single flag). */}/*********************** prepare_to_decompress_next_line ********************* initializes variables in preperation for decompressing another line ******************************************************************************//********************************************************************* Arguments ** --------- ** Passed in: ** params - structure storing information needed ** for comparison and other tasks. ** Returned: ** params - structure storing information needed ** for comparison and other tasks. *********************************************************************/void prepare_to_decompress_next_line( params )struct parameters *params;{ set_up_first_and_last_changing_elements_d( params ); swap_the_reference_and_coding_lines( params ); params->index = 0;} /* end prepare_to_decompress_next_line() */ /******************** swap_the_reference_and_coding_lines ******************** swaps the reference and coding lines *****************************************************************************//********************************************************************* Arguments ** --------- ** Passed in: ** params - structure storing information needed ** for comparison and other tasks. ** Returned: ** params - structure storing information needed ** for comparison and other tasks. *
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -