#include "stdafx.h" #include "PoroMediaDialogShare.h" DS_cube* PoroMediaDialogShare::Dlg_Input_to_DS() { char int_file_name[1024]; DS_cube * cube = NULL; char input_directory[1024], input_base_name[1024], work_directory[1024]; char output_name[1024]; //initialize all variables and directories that are needed sprintf(input_directory, "%s", input_directory_name); sprintf(input_base_name, "%s%s", input_directory, input_file_name_cut); sprintf(work_directory, "%s", field_directory_work_str); sprintf(output_name, "%s%s", work_directory, field_name_output_str); {//write sample name to log //char log_txt[512]; //sprintf(log_txt, "Input file selected: %s", input_base_name); //write_to_log(log_txt); write_to_log("Input file selected: %s", input_base_name); write_to_log("Output: %s", output_name); } txt_field_progress.SetWindowText("Reading DATA..."); //create RAW_XY directory if needed if ( (input_file_ext == INPUT_IS_BMP) || save_intermediate_results) { char raw_directory[512]; sprintf(raw_directory, "%sRAW_XY", work_directory); if (!CreateDirectory(raw_directory,NULL)) //create a new RAW directory if needed write_to_log("Message: 'RAW_XY' directory already exists, new one will not be created."); } //fill out Photoinfo structure Photoinfo* info = new Photoinfo; info->type = input_file_ext; info->startofx = (unsigned short)field_start_X_int; info->startofy = (unsigned short)field_start_Y_int; info->startofz = (unsigned short)field_start_slice_int; info->sizeofx = (unsigned short)field_width_X_int; info->sizeofy = (unsigned short)field_width_Y_int; info->sizeofz = (unsigned short)field_width_slices_int; //init file base name sprintf(int_file_name, "%sRAW_XY\\%s", work_directory, field_name_output_str); //if image type is BMP, convert to RAW if ( (info->type == INPUT_IS_BMP) && (!stop_request)) { sprintf(info->bmp_prefix,"%s", input_base_name); sprintf(info->raw_prefix,"%sRAW_XY\\%s(%d_%d_%d)", work_directory, field_name_output_str, info->sizeofx, info->sizeofy, info->sizeofz); txt_field_progress.SetWindowText("Converting .BMP images to RAW format..."); write_to_log("Converting .BMP images to RAW format..."); if ( !extract(info, (void*) this) ) { write_to_log("BMP to RAW conversion failed."); txt_field_progress.SetWindowText("BMP images to RAW conversion failed."); return NULL; } write_to_log("BMP to RAW conversion successful."); } // // if image type is BMP, RAW, or RAW_STACK, determine threshold and binarize // if ( (info->type == INPUT_IS_BMP) || (info->type == INPUT_IS_RAW) || (info->type == INPUT_IS_RAW_STACK) ) { //if input was BMP, then there already is a raw_prefix from conversion, otherwise the raw prefix is the input if (info->type != INPUT_IS_BMP) sprintf(info->raw_prefix, "%s", input_base_name); else sprintf(info->raw_prefix, "%s.stack.raw", info->raw_prefix); cube = new DS_cube(info, (void*) this);; } if (info) delete info; return cube; } /********************************************************* Converts the Dialog Object to a 3D Binary Data Cube *********************************************************/ DS_1b_cube* PoroMediaDialogShare::Dlg_Input_to_cDS() { int invert = 0, manual = 0; int thres; char int_file_name[1024]; DS_1b_cube *orig_cube = 0; char input_directory[1024], input_base_name[1024], work_directory[1024]; //initialize all variables and directories that are needed sprintf(input_directory, "%s", input_directory_name); sprintf(input_base_name, "%s%s", input_directory, input_file_name); sprintf(work_directory, "%s", field_directory_work_str); //write sample name to log write_to_log("Input file selected: %s", input_base_name); txt_field_progress.SetWindowText("Reading DATA..."); //create RAW_XY directory if needed if ( (input_file_ext == INPUT_IS_BMP) || (save_intermediate_results==true) ) //if ( (input_file_ext == INPUT_IS_BMP) & (save_intermediate_results==true) ) { char raw_directory[512]; sprintf(raw_directory, "%sRAW_XY", work_directory); if (!CreateDirectory(raw_directory,NULL)) //create a new RAW directory if needed { write_to_log("Message: 'RAW_XY' directory already exists, new one will not be created."); } } //if we want to save the binarized input, make sure the BIN_XY directory exists if ( is_control_enabled(FILESAVE_CONTROLS) && (check_save_cbin_stack.GetCheck() || save_intermediate_results) ) { char bin_directory[512]; sprintf(bin_directory, "%sBIN_XY", work_directory); if ( !CreateDirectory(bin_directory,NULL) ) //create a new BIN directory if needed { write_to_log("Message: 'BIN' directory already exists, new one will not be created."); } } //fill out Photoinfo structure Photoinfo *info = new Photoinfo; info->type = input_file_ext; info->startofx = (unsigned short)field_start_X_int; info->startofy = (unsigned short)field_start_Y_int; info->startofz = (unsigned short)field_start_slice_int; info->sizeofx = (unsigned short)field_width_X_int; info->sizeofy = (unsigned short)field_width_Y_int; info->sizeofz = (unsigned short)field_width_slices_int; //init file base name sprintf(int_file_name, "%sRAW_XY\\%s", work_directory, field_name_output_str); //if image type is BMP, convert to RAW if ( (info->type == INPUT_IS_BMP) && (!stop_request)) { sprintf(info->bmp_prefix,"%s", input_base_name); sprintf(info->raw_prefix,"%sRAW_XY\\%s(%d_%d_%d)", work_directory, field_name_output_str, info->sizeofx, info->sizeofy, info->sizeofz); txt_field_progress.SetWindowText("Converting .BMP images to RAW format..."); write_to_log("Converting .BMP images to RAW format..."); if ( !extract(info, (void*) this) ) { write_to_log("BMP to RAW conversion failed."); txt_field_progress.SetWindowText("BMP images to RAW conversion failed."); return NULL; } write_to_log("BMP to RAW conversion successful."); } // // if image type is BMP, RAW, or RAW_STACK, determine threshold and binarize // if ( (info->type == INPUT_IS_BMP) || (info->type == INPUT_IS_RAW) || (info->type == INPUT_IS_RAW_STACK) ) { //if input was BMP, then there already is a raw_prefix from conversion, otherwise the raw prefix is the input if (info->type != INPUT_IS_BMP) { sprintf(info->raw_prefix, "%s", input_base_name); } else { sprintf(info->raw_prefix, "%s.stack.raw", info->raw_prefix); } ///////////////////////////////////////////////// //do any RAW filters here ///////////////////////////////////////////////// DS_cube *cube = NULL; //contrast if (is_control_enabled(FILTER_CONTROLS) && check_filter_contrast_enable.GetCheck() && (!stop_request) ) { //log write_to_log( "filtering: contrast enhancement"); if ( cube == NULL ) { cube = new DS_cube(info, (void*) this); } //ds_median_w_center(cube, (void*) this); //sprintf(file_out, "%sRAW_XY//%s_Med(%d_%d_%d).stack.raw", work_directory, field_name_output_str, cube->wX, cube->wY, cube->wZ); txt_field_progress.SetWindowText("Contrast filtering..."); contrast(cube, (unsigned char)filter_contrast_percentage, (void*) this); if (is_control_enabled(FILESAVE_CONTROLS) && save_intermediate_results && (!stop_request)) { char file_out [512]; txt_field_progress.SetWindowText("Saving contrast filter results..."); sprintf(file_out, "%s_Contr", int_file_name); sprintf(int_file_name, "%s", file_out); //set resulting name sprintf(file_out, "%s(%d_%d_%d).stack.raw", int_file_name, cube->wX, cube->wY, cube->wZ); cube->write_to_RAW_STACK(file_out); } } //convolve if (is_control_enabled(FILTER_CONTROLS) && check_filter_convolve_enable.GetCheck() && !stop_request ) { //log it write_to_log("filtering: convolve filter"); if ( cube == NULL ) { cube = new DS_cube(info, (void*) this); } //{ // bypass4(cube, (void*)this, ""); //histogram // //bypass5(cube, (void*)this); //unwrap ////delete RAW cube // if (cube) // delete cube; // return 0; //} txt_field_progress.SetWindowText("Convolve filtering..."); if (filter_convolve_type == 0 || filter_convolve_type == 1) //median { convolve_filter_median(cube, (void*) this); } else if (filter_convolve_type == 6) { //convolve_downsample(cube, (void*) this); //downsample (beta) //unsigned short int mask_size = (short int)(filter_convolve_radius) + 1; //cube->resize2(mask_size, mask_size, mask_size); //downsample convolve_anisotropic_diffusion(cube, (void*) this); //aniso //convolve_sobel_edge_detect(cube, (void*) this); //edge detector //convolve_threshold(cube, (void*) this); //thresholding } else {//all other cases convolve_filter(cube, (void*) this); } //histogram result { char file_out [512]; sprintf(file_out, "%s_Conv%d_%.1f", int_file_name, filter_convolve_type, filter_convolve_radius); sprintf(file_out, "%s(%d_%d_%d).stack.raw", file_out, cube->wX, cube->wY, cube->wZ); bypass4(cube, (void*) this, file_out); } if ( !filter_convolve_savebounds && !stop_request ) //resize cube after convolve { unsigned short int mask_radius = (short int)(filter_convolve_radius); cube->resize_enveloppe(mask_radius, mask_radius, mask_radius); } if (is_control_enabled(FILESAVE_CONTROLS) && save_intermediate_results && (!stop_request)) { char file_out [512]; txt_field_progress.SetWindowText("Saving convolve filter results..."); sprintf(file_out, "%s_Conv%d_%.1f", int_file_name, filter_convolve_type, filter_convolve_radius); sprintf(int_file_name, "%s", file_out); //set resulting name sprintf(file_out, "%s(%d_%d_%d).stack.raw", int_file_name, cube->wX, cube->wY, cube->wZ); cube->write_to_RAW_STACK(file_out); txt_field_progress.SetWindowText("Saving convolve filter results... Done!"); } } //anisotropic diff if (is_control_enabled(FILTER_CONTROLS) && check_filter_aniso_enable.GetCheck() && !stop_request ) { char win_txt[512]; write_to_log("filtering: anisotropic diffusion"); if ( cube == NULL ) //check if 'cube' has been allocated, if not, allocate it! { cube = new DS_cube(info, (void*) this); } sprintf(win_txt, "Anisotropic Diffusion (%d steps)..", filter_aniso_iterations); txt_field_progress.SetWindowText(win_txt); if (filter_aniso_fast) { //anisotropic_diffusion2D_3planes_extended(cube, (void*)this); } else { convolve_anisotropic_diffusion(cube, (void*) this); } sprintf(win_txt, "Anisotropic Diffusion (%d steps).... done", filter_aniso_iterations); txt_field_progress.SetWindowText(win_txt); if ( !filter_aniso_savebounds && !stop_request ) //resize cube after aniso { unsigned short int mask_radius = (short int)(filter_aniso_radius); cube->resize_enveloppe(mask_radius, mask_radius, mask_radius); } if (is_control_enabled(FILESAVE_CONTROLS) && save_intermediate_results && (!stop_request) && (filter_aniso_K != 1)) //K=1 special case to perform histogramming only { char file_out [512]; txt_field_progress.SetWindowText("Saving anisotropic diffusion filter results..."); sprintf(file_out, "%s_Aniso", int_file_name); sprintf(int_file_name, "%s", file_out); //sprintf(file_out, "%s(%d_%d_%d).stack.raw", int_file_name, cube->wX, cube->wY, cube->wZ); char diff_coeff; switch (filter_aniso_diff_coef) // 0 = 1/[1+]^2, 1 = exp^2, 2 = exp^4, 3 = exp^n { case 0: diff_coeff = '1'; break; case 1: diff_coeff = 'e'; break; case 2: diff_coeff = '4'; break; default: diff_coeff = 'n'; break; } sprintf(file_out, "%s_%c_K%dI%dm%d(%d_%d_%d).stack.raw", int_file_name, \ diff_coeff, (unsigned char)filter_aniso_K, (unsigned char)filter_aniso_iterations, \ (unsigned char)filter_aniso_midpoint, cube->wX, cube->wY, cube->wZ); cube->write_to_RAW_STACK(file_out); } } //remove // convert RAW file to BIN using thershold, unless skip is specified if ( skip_binarization && !stop_request ) { write_to_log("PoroMedia: Binarization was skipped."); //clean up delete cube; delete info; //return NULL since nothing was binarized return NULL; } //determine THRESHOLD, manual or auto if (!stop_request) { int threshold_method = combo_threshold_method.GetCurSel(); if(threshold_method == 0) {//manual threshold thres = field_manual_threshold_int; manual = 1; invert = ( check_image_is_inverted.GetCheck() ) ? ( 1 ) : ( 0 ); } else {//automatic threshold txt_field_progress.SetWindowText("Determining threshold automatically..."); Log_value result; if ( check_image_is_inverted.GetCheck() ) { invert = 1; result = threshold(info, cube, 0, 50, 254, threshold_method); thres = result.value; } else { invert = 0; result = threshold(info, cube, 0, 0, 192, threshold_method); thres = result.value; } //update GUI threshold displayed SetIntValField(&(field_manual_threshold), thres); field_manual_threshold_int = thres; } } if (!stop_request) { txt_field_progress.SetWindowText("Converting RAW images to BIN format..."); orig_cube = binarize(info, cube, this); write_to_log("Message: RAW to BIN conversion successful."); } //delete RAW cube if (cube) { delete cube; } } //already binarized else if (!stop_request) { txt_field_progress.SetWindowText("Reading BIN input into memory..."); //input is already binary, so just place BIN file into compressed DS orig_cube = new DS_1b_cube(input_base_name, info->type, info->startofz, info->sizeofz, info->sizeofy, info->sizeofx); if (!orig_cube->data) { write_to_log("Input Scan: Invalid file extension detected."); return NULL; } } ///////////////////////////////////////////////// //now we have a 1b_cube in 'orig_cube', so do any BINARY filters here ///////////////////////////////////////////////// sprintf(int_file_name, "%sBIN_XY\\%s", work_directory, field_name_output_str); if (is_control_enabled(FILTER_CONTROLS) && check_filter_voxrem_enable.GetCheck() && (!stop_request)) { //isolated voxel removal char file_out [512]; DS_1b_cube * temp; IsolatedVoxRemover * voxremover; //calc unit voxel volume float unit_vox_vol = field_pixel_size_float * field_pixel_size_float * field_pixel_size_float; //for pores if (filter_voxrem_removal_run_4_pore) { filter_voxrem_pore_vox_vol = (long)(filter_voxrem_pore_um_vol/unit_vox_vol + .5); //calc total voxel volume and round it temp = orig_cube; voxremover = new IsolatedVoxRemover(orig_cube); //file where the stats of spec removal will be place if (is_control_enabled(FILESAVE_CONTROLS) && save_intermediate_results) { sprintf(file_out, "%s_SpecPorRem", int_file_name); sprintf(int_file_name, "%s", file_out); sprintf(file_out, "%s(%d_%d_%d)_stats.txt", int_file_name, orig_cube->wX, orig_cube->wY, orig_cube->wZ); } else { file_out[0] = '\0'; } txt_field_progress.SetWindowText("Running specal removal on pores..."); write_to_log("PoroMedia: now starting pore voxel removal."); orig_cube = voxremover->Run_Vox_Remover(file_out, 0 /*to remove*/, filter_voxrem_pore_vox_vol, (void*) this); write_to_log("PoroMedia: done with pore voxel removal"); if (is_control_enabled(FILESAVE_CONTROLS) && save_intermediate_results && (!filter_voxrem_removal_run_4_fiber) && (!stop_request) ) { sprintf(file_out, "%s_SpecPorRem", int_file_name); sprintf(int_file_name, "%s", file_out); sprintf(file_out, "%s(%d_%d_%d).stack.cbin", int_file_name, orig_cube->wX, orig_cube->wY, orig_cube->wZ); orig_cube->writeToFile(file_out); } delete voxremover; delete temp; } //for fibers if (filter_voxrem_removal_run_4_fiber && (!stop_request)) { filter_voxrem_fiber_vox_vol = (long)(filter_voxrem_fiber_um_vol/unit_vox_vol + .5); //calc total voxel volume and round it temp = orig_cube; voxremover = new IsolatedVoxRemover(orig_cube); //file where the stats of spec removal will be place if (is_control_enabled(FILESAVE_CONTROLS) && save_intermediate_results) { sprintf(file_out, "%s_SpecFibRem", int_file_name); sprintf(int_file_name, "%s", file_out); sprintf(file_out, "%s(%d_%d_%d)_stats.txt", int_file_name, orig_cube->wX, orig_cube->wY, orig_cube->wZ); } else { file_out[0] = '\0'; } txt_field_progress.SetWindowText("Running specal removal on fibers..."); write_to_log("PoroMedia: now starting fiber voxel removal."); orig_cube = voxremover->Run_Vox_Remover(file_out, 1 /*to remove*/, filter_voxrem_fiber_vox_vol, (void*) this); write_to_log("PoroMedia: done with fiber voxel removal"); if (is_control_enabled(FILESAVE_CONTROLS) && save_intermediate_results && (!filter_voxrem_removal_run_4_pore) ) { sprintf(file_out, "%s_SpecFibRem", int_file_name); sprintf(int_file_name, "%s", file_out); sprintf(file_out, "%s(%d_%d_%d).stack.cbin", int_file_name, orig_cube->wX, orig_cube->wY, orig_cube->wZ); } else { sprintf(file_out, "%s_SpecPor&FibRem", int_file_name); sprintf(int_file_name, "%s", file_out); sprintf(file_out, "%s(%d_%d_%d).stack.cbin", int_file_name, orig_cube->wX, orig_cube->wY, orig_cube->wZ); } if (is_control_enabled(FILESAVE_CONTROLS) && save_intermediate_results && (!stop_request)) { orig_cube->writeToFile(file_out); } delete voxremover; delete temp; } txt_field_progress.SetWindowText("Done with specal removal."); } if (is_control_enabled(FILTER_CONTROLS) && check_filter_close_enable.GetCheck() && (!stop_request)) { //close filter write_to_log( "PoroMedia: now starting %d iterations of Close filter...", filter_close_iterations ); close_filter(orig_cube, filter_close_neighs, filter_close_extended, filter_close_iterations, (void*)this); write_to_log("PoroMedia: done with Close filter."); if (is_control_enabled(FILESAVE_CONTROLS) && save_intermediate_results && (!stop_request)) { char file_out [512]; sprintf(file_out, "%s_%dClose", filter_close_iterations, int_file_name); sprintf(int_file_name, "%s", file_out); sprintf(file_out, "%s(%d_%d_%d).stack.cbin", int_file_name, orig_cube->wX, orig_cube->wY, orig_cube->wZ); txt_field_progress.SetWindowText("Saving Close filter results..."); orig_cube->writeToFile(file_out); } txt_field_progress.SetWindowText("Done with Close filter."); } //**************************************************** // if we want to save bin conversion to a .stack.cbin file //**************************************************** if (is_control_enabled(FILESAVE_CONTROLS) && check_save_cbin_stack.GetCheck() && (!stop_request)) { char outfile[512]; sprintf(outfile,"%sBIN_XY\\%s(%d_%d_%d).stack.cbin", work_directory, field_name_output_str, info->sizeofx, info->sizeofy, info->sizeofz); if (!orig_cube->writeToFile(outfile)) { write_to_log("PoroMedia common: error: cannot open output file %s", outfile); return NULL; } } if ( info ) { delete info; } if (stop_request) { StopConfirm(); //txt_field_progress.SetWindowText("Run stopped successfully."); if (orig_cube) { delete orig_cube; } //stop_request = false; return NULL; } else { return orig_cube; } }