/******************************************************************************* TITLE: func.c AUTHOR: Ann Li DATE: 10/1/00 COMPILER/SYSTEM: MSVC6.0/NT PURPOSE: Using recursion and lined stack to find the connected components in an image ARGUMENTS: **********************************************************************************/ #include "header.h" #include "func.h" ///////////////////////////////////////////////////////////////// // common files for both versions // TITLE: readHeader // PURPOSE: Read in the information of column, row, and max value of an image. // ARGUMENTS: *fp: points to the input pmg file // *p_header: output parameter. used to store the inforamation scanned from image file // file stream point as a parameter, more flexible, I think void readHeader(FILE* fp, header* p_header ) { char buf[MAX_LINE]; int col, row, max; // check format sign fscanf(fp,"%s",buf); if( strcmp(buf,"P2")!=0 ){ perror("The format is incorrect: it's not the beginning of a PGM file."); exit(1); } // ignore statement fscanf(fp,"%s", buf); while ( buf[0]=='#' ){ fgets(buf,MAX_LINE,fp); // newline character included fscanf(fp,"%s",buf); } // no statement now. column already read col = atoi(buf); fscanf(fp,"%d",&row); fscanf(fp,"%d",&max); p_header->col = col; p_header->row = row; p_header->max_value = max; } // TITLE: readImage // PURPOSE: Read in the content of an image // ARGUMENTS: *inputfile: the file name of the image // *im: output parameter. used to store the content read in from input file void readImage( char* inputfile, image* im ) { int i, j; FILE *fp; if ( !(fp=fopen(inputfile, "r")) ){ perror("The input file cann't be opened"); exit(1); } readHeader(fp,&im->header); im->p_array = malloc(sizeof(int*)*im->header.row); for ( i=0; iheader.row; i++ ) im->p_array[i] = malloc(sizeof(int)*im->header.col); for(i = 0; i < im->header.row; i++) for(j = 0; j < im->header.col; j++) fscanf(fp,"%d",&im->p_array[i][j]); fclose(fp); } // TITLE: converPGM // PURPOSE: Strech out the input image to use the full range of gray tone // ARGUMENTS: num_components: Number of connected components found // *im: in/output parameter. void convertPGM(int num_components, image *im) { int i, j; int row, col; int offset; row = im->header.row; col = im->header.col; if(num_components == 0) return; offset = OUTPUT_MAX_VALUE / num_components; for(i = 0; i < row; i++) for(j = 0; j < col; j++) im->p_array[i][j] *= offset; }