/* ####################################################################### # # ******************************************************************** # * DISCLAIMER * # * * # * Neither the authors of this software system, nor their employing * # * institutes,nor the agencies providing financial support for this * # * work make any representation or warranty, express or implied, * # * regarding this software system or assume any liability for its * # * use. * # * * # * This code implementation is the intellectual property of the * # * OpenGATE collaboration. * # * By copying, distributing or modifying the Program (or any work * # * based on the Program) you indicate your acceptance of this * # * statement, and all its terms. * # ******************************************************************** # # The following is a C++ program for converting binary files of 32bit # float data type into binary files of unsigned 16bit integer so as to # make them compatible with the interfile reader of GATE. # This program was wrtten by: # Nicolas Karakatsanis # Version: 16MAR06 # # HOW TO COMPILE: # 1) Compile using this command line in the terminal: # g++ (name of file) -o your.exe # ####################################################################### */ #include #include #include float *input_buffer, factor; unsigned short *output_buffer; int input_size,output_size, num_voxels, filename_opt, opt; char input_filename[60], output_filename[60], new_filename[60], filename_ending[60], input_filename_2[60]; FILE *input_file; FILE *output_file; int main() { printf("Please type the name of the binary finally you wish to convert (without the extension): \n"); scanf("%s",&input_filename); if (input_filename==NULL) { printf("Incorrect typing.Please try again: \n"); scanf("%s",&input_filename); if (input_filename==NULL) { perror ("Improper filename,program terminated..."); exit (1); } } printf("Thank you.The default extension .bin will be added \n"); strcpy(input_filename_2,input_filename); strcat(input_filename,".bin"); input_file = fopen(input_filename,"rb+"); if (input_file==NULL) { perror ("Error opening file.Program terminated... \n"); exit (1); } fseek(input_file,0,SEEK_END); input_size = ftell(input_file); rewind(input_file); num_voxels = input_size/sizeof(float); output_size = num_voxels*sizeof(unsigned short); input_buffer = (float*)malloc(input_size); output_buffer = (unsigned short*)malloc(output_size); if ((input_buffer==NULL)||(output_buffer==NULL)) perror ("Memory allocation error \n"); fread(input_buffer,4,num_voxels,input_file); fclose(input_file); printf("If you wish to multiply your input binary file with a scale factor press (1) otherwise press (0):\n"); scanf("%d",&opt); if (opt==0) printf("No multiplication will be applied"); else if (opt==1) { printf("Please choose a scale factor (integer or float):\n"); scanf ("%e",&factor); } else printf("Incorrect factor, no multiplication will be applied"); for (int i = 0; i < num_voxels; i++) { if (opt==1) *(input_buffer+i) = *(input_buffer+i)*factor; *(output_buffer+i) = (unsigned short)*(input_buffer+i); } printf("Now press:\n0 if you wish to automatically append an ending of your choice to the original name for the new binary file or... \n1 if you wish to type the new filename by yourself \n"); scanf ("%d",&filename_opt); if (filename_opt==0) { printf("Please type the ending you wish to append: \n"); scanf("%s",&filename_ending); strcat(input_filename_2,"_"); strcat(input_filename_2,filename_ending); strcat(input_filename_2,".bin"); strcpy(output_filename,input_filename_2); } else if (filename_opt==1) { printf("Please type the name of the output binary file (without extension): \n"); scanf("%s",&new_filename); strcat(new_filename,".bin"); strcpy(output_filename,new_filename); } else { printf("Incorrect option.The ending uint16 was appended at the end of the original filename \n"); strcat(input_filename_2,"_uint16"); strcat(input_filename_2,".bin"); strcpy(output_filename,input_filename_2); } output_file = fopen(output_filename,"wb+"); fwrite(output_buffer,2,num_voxels,output_file); fclose(output_file); free(input_buffer); free(output_buffer); printf("Your input binary file:\n\nFilename: %s Size: %d Data Type: Float 32bit\n\nwas succesfully converted to the output binary file:\n\nFilename: %s Size: %d Data Type: Unsigned Integer 16bit\n\n",input_filename,input_size,output_filename,output_size); return 0; }