#include "stdafx.h" #include "extract.h" //#include <time.h> using namespace std; //saves output as stack by default, if user specifies also saves as a file per slice format bool extract(Photoinfo* info, void * dlg_share) { int totalx, totaly; unsigned short x, y, z; int *gray; unsigned long int id; int max_dig_cnt = FindNumberOfDigits(info->sizeofz + info->startofz - 1); char * bmpfilename; char outname[512]; DS_slice * slice_crop; //setup the progress bar parameters PoroMediaDialogShare* dlg = (PoroMediaDialogShare*) dlg_share; dlg->pbar_simulation_progress.SetRange(0, info->sizeofz); dlg->pbar_simulation_progress.SetPos(0); dlg->pbar_simulation_progress.SetStep(1); //create output file for RAW stack sprintf(outname, "%s.stack.raw", info->raw_prefix); ofstream fout(outname, ios_base::binary); if(!fout.is_open()) { char log_txt[512]; sprintf(log_txt, "extract: error opening output file %s", outname); write_to_log(log_txt); return false; } //read the header to get xsize and ysize bmpfilename = AppendDigitsAndExtension(info->bmp_prefix, info->startofz, max_dig_cnt, INPUT_IS_BMP); FILE* filein = fopen (bmpfilename, "rb" ); if ( filein == NULL ) { write_to_log("Extract could not open the input file for header reading."); return false; } int psize, biBitCount; if (bmp_read_header (filein, &totalx, &totaly, &psize, &biBitCount)) { write_to_log("There was an error reading the header file"); return false; } free(bmpfilename); fclose(filein); //allocate memory slice_crop = new DS_slice((unsigned short)info->sizeofy, (unsigned short)info->sizeofx); if(!slice_crop) { write_to_log("extract: error allocating memory for slice_crop"); return false; } gray = new int [totaly*totalx]; if (!gray) { write_to_log("extract: error allocating memory for gray"); return false; } for ( z = info->startofz; z < (info->startofz + info->sizeofz); z++) { //create full file name bmpfilename = AppendDigitsAndExtension(info->bmp_prefix, z, max_dig_cnt, INPUT_IS_BMP); //call bmp_read from bmp_io if(bmp_read(bmpfilename, &totalx, &totaly, &gray) == 1) { write_to_log("extract: error during bmp_read call"); return false; } free(bmpfilename); //flip desired region and save it for(y = 0; y < info->sizeofy; y++) for(x = 0; x < info->sizeofx; x++) { id = (totaly - ((info->startofy) + y))*totalx + ((info->startofx-1) + x); slice_crop->data[y][x] = (unsigned char)(gray[id]); } //write to end of current stack output file for (y = 0; y < info->sizeofy; y++) fout.write(reinterpret_cast<const char*>(slice_crop->data[y]), info->sizeofx); //update progress bar dlg->pbar_simulation_progress.StepIt(); } //clean up delete slice_crop; delete gray; fout.close(); return true; }