/* boundary_f_img.c ** this program translate the ** floating number file into ** generic binary image file ** also add user defined boundary ** into the output image ** COPYRIGHT: Brandt Tso */ #include #include #include void main(void) { FILE *fin, *fout; unsigned long int in_file_row; unsigned long int in_file_col, out_img_col; unsigned long int total, boundary_pix_num; unsigned long int count[5]; unsigned char store[2],pix_num[2],in_file[50]; double f1,f2,f_max,f_min,f_range,pix_f; printf("\nThis programme translates the input floating value file\n"); printf("into an image file with user defined boundary pixel number added!\n"); printf("This programme is useful for translating the outputs of the programmes\n"); printf("such as mar.c, fractal.c...etc., into images\n\n"); printf("Please input the number for input file row:"); scanf("%d",&in_file_row); printf("Please input the number for input file col:"); scanf("%d",&in_file_col); printf("Please input the number for adding boundary pixels:"); scanf("%d",&boundary_pix_num); printf("Input file name:"); scanf("%s",in_file); if((fin = fopen(in_file,"r")) == NULL) { fprintf(stderr,"Failed to open %s\n",in_file); exit(EXIT_FAILURE); } printf("Output image name:"); scanf("%s",in_file); if((fout = fopen(in_file,"wb")) == NULL) { fprintf(stderr,"Failed to open %s\n",in_file); exit(EXIT_FAILURE); } fscanf(fin,"%lf ",&f1); fscanf(fin,"%lf ",&f2); if(f1 > f2) { f_max = f1; f_min = f2; } else { f_max = f2; f_min = f1; } fseek(fin,0,0); total = 0; for(count[1] = 0;count[1] < in_file_row;count[1]++) for(count[2] = 0;count[2] < in_file_col;count[2]++) { fscanf(fin,"%lf ",&f1); if(f1 > f_max) f_max = f1; if(f1 < f_min) f_min = f1; total++; } f_range = f_max - f_min; printf("(total,max,min,range)=%d,%f,%f,%f\n",total,f_max,f_min,f_range); fseek(fin,0,0); store[0] = 0; out_img_col = boundary_pix_num*2 + in_file_col; for(count[1] = 0; count[1] < boundary_pix_num; count[1]++) for(count[2] = 0; count[2] < out_img_col; count[2]++) fwrite(store,1,1,fout); for(count[1] = 0;count[1] < in_file_row; count[1]++) { for(count[2] = 0;count[2] < boundary_pix_num;count[2]++) fwrite(store,1,1,fout); for(count[3] = 0;count[3] < in_file_col;count[3]++) { fscanf(fin,"%lf ",&f1); pix_f = (double) ((f1-f_min)/f_range)*255+0.5; pix_num[0] = (int) pix_f; fwrite(pix_num,1,1,fout); } for(count[2] = 0;count[2] < boundary_pix_num;count[2]++) fwrite(store,1,1,fout); printf("Processing image row %d\n",count[1]+1); } for(count[1] = 0; count[1] < boundary_pix_num; count[1]++) for(count[2] = 0; count[2] < out_img_col; count[2]++) fwrite(store,1,1,fout); fclose(fin); fclose(fout); }