// import.cpp : implementation file // #include "stdafx.h" #include "Sar_CCD.h" #include "import.h" #include "afxdialogex.h" #include "Functions.h" #include "math.h" #include "global.h" // import dialog double round(double r) { return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5); } IMPLEMENT_DYNAMIC(import, CDialog) UINT Begin_Import(LPVOID pParam); import::import(CWnd* pParent /*=NULL*/) : CDialog(import::IDD, pParent) { led_input1 = _T(""); led_input2 = _T(""); m_txtoutdir = _T(""); flag=false; m_azimuth = _T(""); m_range = _T(""); SelectedProcess=0; pdoc=GetDocPtr();//getting pointer to doc; } import::~import() { TRACE("Executing destructor of import\n"); } void import::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDT_MASTER, led_input1); DDX_Text(pDX, IDC_EDT_SLAVE, led_input2); DDX_Text(pDX, IDC_EDT_OUT, m_txtoutdir); DDX_Text(pDX, IDC_EDT_AZ, m_azimuth); DDX_Text(pDX, IDC_EDT_RN, m_range); DDX_Control(pDX, IDC_CHECK_MLOOK, m_chk); DDX_Control(pDX, IDC_SPN_AZ, m_spn_az); DDX_Control(pDX, IDC_SPN_RN, m_spn_rn); DDX_Control(pDX, IDC_NXT_IMPORT, m_nextButton_import); } BEGIN_MESSAGE_MAP(import, CDialog) ON_BN_CLICKED(IDC_INPMASTER, &import::OnBnClickedInpmaster) ON_BN_CLICKED(IDC_INPSLAVE, &import::OnBnClickedInpslave) ON_BN_CLICKED(IDC_EXIT, &import::OnBnClickedExit) ON_BN_CLICKED(IDC_OUTDIR, &import::OnBnClickedOutdir) ON_WM_TIMER() ON_BN_CLICKED(IDC_BTN_PROCESS, &import::OnBnClickedBtnProcess) ON_BN_CLICKED(IDC_CHECK_MLOOK, &import::OnBnClickedCheckMlook) ON_WM_PAINT() //ON_BN_CLICKED(IDC_SHIMG, &import::OnBnClickedShimg) //ON_BN_CLICKED(IDC_BUTTON1, &import::OnBnClickedButton1) ON_WM_DESTROY() ON_BN_CLICKED(IDC_NXT_IMPORT, &import::OnBnClickedNxtImport) END_MESSAGE_MAP() // import message handlers /////////////////////////////////////////////////////////////////////////////// // NAME: GetDocPtr() // DESCRIPTION: Get pointer to document object // PARAMETERS: None // RETURN: pointer to current document object // SIDE EFFECTS: none ///////////////////////////////////////////////////////////////////////////////// CSar_CCDDoc* import::GetDocPtr() { return CSar_CCDDoc::GetDoc();//getting pointer to current document } #pragma region DIALOG FUNCTIONS BOOL import::OnInitDialog() { CDialog::OnInitDialog(); ///////////////////////////////////////////// //Setting dialog position according to mainframe window //Pfun.setDialogPos(m_hWnd); //////////////////////////////////////// /*For displaying progress on progress bar*/ m_spn_az.SetRange(1,1000); m_spn_rn.SetRange(1,1000); m_spn_az.SetBuddy((CEdit*)this->GetDlgItem(IDC_EDT_AZ)); m_spn_rn.SetBuddy((CEdit*)this->GetDlgItem(IDC_EDT_RN)); flag_dialog=true;//remember this.because this indicates success of oninitdialog so as onpaint is free to paint it now TRACE("master=%s\n",pdoc->master_parfile); TRACE("slave=%s\n",pdoc->slave_parfile); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void import::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here // Do not call CDialog::OnPaint() for painting messages if(flag_dialog==true) { Set_Initial_Values(); } flag_dialog=false; } /////////////////////////////////////////////////////////////////////////////// // NAME: Set_Initial_values // DESCRIPTION:This function sets initial azimuth and range values..That values //remained to be calculated according to multilook factor. // PARAMETERS:none // RETURN: none // SIDE EFFECTS: none ///////////////////////////////////////////////////////////////////////////////// void import::Set_Initial_Values() { /*for multilooking purpose*/ m_chk.SetCheck(BST_CHECKED); UpdateData(TRUE); /* ================== Multilooking ratio ================== multilook factor = [ pixel spacing / sin( Incidence_angle (theeta i) ) ] / line spacing */ float multilook_factor; float pixel_spacing=0, line_spacing=0, theeta_i=0; CString tmp,t; // read various parameters from slc_par and resultfiles if(global::masterFileName != "" & global::resultFile != "") { CStdioFile result(global::resultFile,CFile::modeRead); while( result.ReadString(tmp) ) { if(tmp.Find(L"Incidence_angle(inc)[deg]") != -1) { //extract value of theeta i swscanf_s(tmp, L"Incidence_angle(inc)[deg] :%f", &theeta_i); } } result.Close(); CStdioFile master(global::masterFileName,CFile::modeRead); while( master.ReadString(tmp) ) { if(tmp.Find(L"Line_spacing(m)") != -1) { //extract value of theeta i swscanf_s(tmp, L"Line_spacing(m) :%f", &line_spacing); } if(tmp.Find(L"Pixel_spacing(m)") != -1) { //extract value of theeta i swscanf_s(tmp, L"Pixel_spacing(m) :%f", &pixel_spacing); } } master.Close(); // Calculate multilook factor here and decide range and aimuth // Also on choosing result directory - call this method multilook_factor = round( (pixel_spacing / sin(theeta_i*(PI/180)))/line_spacing ); // Assume range = 2, thus calculate azimuth. Ie. if multilook_factor is 5, range is 2, azi is 10 range = 2; azimuth = range*multilook_factor; } else //old code { // Could not calculate - set from preset switch(SelectedProcess) { case 1: azimuth=10; range=2; break; case 2: azimuth=10; range=10; break; case 3: azimuth=12; range=2; break; case 4: azimuth=10; range=10; break; case 5: azimuth=10; range=10; break; case 6: azimuth=2; range=2; break; } } //old code end TRACE("selected process=%d\n",SelectedProcess); TRACE("azimuth on initial=%d\n",azimuth); TRACE("range on initial=%d\n",range); m_azimuth.Format(_T("%d"),azimuth); m_range.Format(_T("%d"),range); UpdateData(FALSE); GetDlgItem(IDC_EDT_AZ)->EnableWindow(0); GetDlgItem(IDC_EDT_RN)->EnableWindow(0); GetDlgItem(IDC_SPN_AZ)->EnableWindow(0); GetDlgItem(IDC_SPN_RN)->EnableWindow(0); } #pragma endregion #pragma region GUI CONTROL FUNCTIONS void import::OnBnClickedInpmaster() { UpdateData(TRUE); CFileDialog dlg(TRUE); if(dlg.DoModal()==IDOK) { led_input1 = dlg.GetPathName(); PathName1=led_input1; } else { led_input1 = "";} UpdateData(FALSE); } void import::OnBnClickedInpslave() { UpdateData(TRUE); CFileDialog dlg(TRUE); if(dlg.DoModal()==IDOK) { led_input2 = dlg.GetPathName(); PathName2=led_input2; } else { led_input2 = "";} UpdateData(FALSE); } void import::OnBnClickedCheckMlook() { if(m_chk.GetCheck()==BST_UNCHECKED) { GetDlgItem(IDC_EDT_AZ)->EnableWindow(1); GetDlgItem(IDC_EDT_RN)->EnableWindow(1); GetDlgItem(IDC_SPN_AZ)->EnableWindow(1); GetDlgItem(IDC_SPN_RN)->EnableWindow(1); UpdateData(TRUE); m_azimuth=""; m_range=""; UpdateData(FALSE); } if(m_chk.GetCheck()==BST_CHECKED) { Set_Initial_Values(); } } void import::OnBnClickedBtnProcess() { /*error codes returns 1 if filenames are not entered returns 2 if either azimuth or range not entered will return 3 if filenames are not proper will return 4 if values of azimuth & range not valid*/ pdoc=GetDocPtr(); CString mpar,spar,res; mpar=pdoc->master_parfile; spar=pdoc->slave_parfile; res=pdoc->base_resfile; if(mpar.IsEmpty()||spar.IsEmpty()||res.IsEmpty()) {AfxMessageBox(L"Baseline Result not found",MB_ICONERROR|MB_OK,0); return; } int err_code; err_code=validate_input(); bool flag_valid; switch(err_code) { case 1: MessageBox(L"Please enter filenames",L"Error",MB_OK|MB_ICONINFORMATION); break; case 2: MessageBox(L"Please enter azimuth & range values Or Use Default",L"Error",MB_OK|MB_ICONINFORMATION); break; case 3: MessageBox(L"Please enter filenames properly",L"Error",MB_OK|MB_ICONINFORMATION); break; case 4: MessageBox(L"Please enter valid azimuth & range values",L"Error",MB_OK|MB_ICONINFORMATION); break; case 5: flag_valid=true; break; } if(err_code==5) { Select_process_func(); /* HWND hh=GetSafeHwnd(); CWinThread* pWBegin = AfxBeginThread(Begin_Import,this); TRACE("after starting thread\n"); TRACE("selectedprocess value=%d\n",SelectedProcess);*/ } else MessageBox(L"Bad input"); } void import::OnBnClickedExit() { DestroyWindow();//case when process sucessfully completed by user } void import::OnBnClickedOutdir() { UpdateData(TRUE); if(Pfun.GetFolder(m_txtoutdir, L"Select Output Folder...")) { if(m_txtoutdir != "") global::resultFile = m_txtoutdir + L"\resultfile.txt"; UpdateData(FALSE); m_nextButton_import.ShowWindow(TRUE); } } #pragma endregion void import::create_progrcntl(CString header) { //CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd); pgr = new CPgr; pgr->Create(IDD_PROGRESS,this); pgr->SetWindowTextW(header); pgr->ShowWindow(SW_SHOW); // CRect rt; //pFrame->GetClientRect(rt); //TRACE("%d\t%d\t%d\t%d\n",rt.left,rt.top,rt.right,rt.bottom); // HWND m_pwnd=pgr->m_hWnd; // pgr->SetParent(pFrame); // ::SetWindowPos(m_pwnd,HWND_TOPMOST,(rt.right/60),(rt.bottom/2),500,100,SWP_NOZORDER|SWP_NOREDRAW); } /////////////////////////////////////////////////////////////////////////////// // NAME:validate_input // DESCRIPTION:Validates the user input // PARAMETERS: none // RETURN: Integer value representing error code // SIDE EFFECTS: none ///////////////////////////////////////////////////////////////////////////////// int import::validate_input() { //error codes //It returns 1 if filenames are not enterd //returns 2 if either azimuth or range not entered //will return 3 if filenames are not proper //will return 4 if values of azimuth & range not valid UpdateData(TRUE); CString temp1,temp2; temp1=led_input1; temp2=led_input2; if(temp1.IsEmpty()) return 1; if(temp2.IsEmpty()) return 1; temp1=m_azimuth; temp2=m_range; azimuth=atoi(Pfun.stringtochar(temp1)); range=atoi(Pfun.stringtochar(temp2)); if(azimuth==1 && range==1) return 4; if(m_chk.GetCheck()==BST_UNCHECKED) { if(!temp1.IsEmpty() && !temp2.IsEmpty()) { if(azimuth==0||range==0) return 2; else return 5; } else return 2; } else return 5; UpdateData(FALSE); VERIFY(!PathName1.IsEmpty()); VERIFY(!PathName2.IsEmpty()); return 5; } void import::AddDataFileInfo(char *parfile,char* datafile,char* datatype,int rows,int cols,char* bytefile,int ml_rows,int ml_cols) { FILE *fp; if(parfile==NULL) { MessageBox(L"Baseline result not found",L"Error",MB_OK|MB_ICONINFORMATION); return; } else { pdoc=GetDocPtr(); fp=fopen(parfile,"a"); fprintf(fp,"\n*************************************************************************************************************\n"); fprintf(fp,"Datafile :%s\n",datafile); fprintf(fp,"Datafile_format :%s\n",datatype); fprintf(fp,"Number_of_lines :%d\n",rows); fprintf(fp,"Number_of_pixels :%d\n",cols); fprintf(fp,"\n*************************************************************************************************************\n"); fprintf(fp,"\n*************************************************************************************************************\n"); fprintf(fp,"DataBytefile :%s\n",bytefile); fprintf(fp,"Number_of_multilooked_lines :%d\n",ml_rows); fprintf(fp,"Number_of_multilooked_pixels :%d\n",ml_cols); fprintf(fp,"\n*************************************************************************************************************\n"); fclose(fp); } } /////////////////////////////////////////////////////////////////////////////// // NAME:processERS_MasterSlave // DESCRIPTION:Processes maaster & slave image data for ERS & outputs slc imagefiles //multilooked byte image files // PARAMETERS:none // RETURN: none // SIDE EFFECTS:If the user aborts process in middle it still doesnt stop internally & process whole code //ADDITION:multithreaded env remained to be added ///////////////////////////////////////////////////////////////////////////////// void import::processERS_MasterSlave() { try{ TRACE("In processERS_MasterSlave\n\n"); char * masterfilename,*slavefilename,*outmaster,*outslave,*mlook_master,*mlook_slave; int nlines, ncols, nlinesub, nlinend, nsampsub, nsampend ; FILE *inp,*slv, *outp,*outslv; int i, k, loopend, loopst,rows,cols,skip_bytes; unsigned long int n; char buf[8],app; char *datain=NULL; CString mout,sout, multi_mast,multi_slave; short bytes_per_pixel; short *dataout=NULL; masterfilename=Pfun.stringtochar(PathName1);//input imAGE DATA FILES slavefilename=Pfun.stringtochar(PathName2); mout=Pfun.AddPathAndFile(m_txtoutdir,L"\\master_ers.slc");//making path for single & multilooked slc images sout=Pfun.AddPathAndFile(m_txtoutdir,L"\\slave_ers.slc"); multi_mast=Pfun.AddPathAndFile(m_txtoutdir,L"\\masters_byte.img"); multi_slave=Pfun.AddPathAndFile(m_txtoutdir,L"\\slvers_byte.img"); outmaster=Pfun.stringtochar(mout);//converting all path into char pointer outslave=Pfun.stringtochar(sout); mlook_master=Pfun.stringtochar(multi_mast); mlook_slave=Pfun.stringtochar(multi_slave); bytes_per_pixel=4; // MessageBox(L"\n Processing for Master & Slave slc extraction started ....\n"); GetDlgItem(IDC_BTN_PROCESS)->EnableWindow(0); TRACE("Before timer starts\n"); TRACE("After timer starts\n"); /*Processing master image data file starts here*/ //CMemoryState ms1; //ms1.Checkpoint(); inp=fopen(masterfilename,"rb"); outp = fopen(outmaster, "wb"); fseek(inp,186,0); fread(buf,sizeof(char),8,inp); skip_bytes=atoi(buf); fseek(inp,236,0); fread(buf,sizeof(char),8,inp); nlines=atoi(buf); fseek(inp,248,0); fread(buf,sizeof(char),8,inp); ncols=atoi(buf); nlinesub=1; nlinend=nlines; nsampsub=1; nsampend=ncols; rows=nlines/azimuth; cols=ncols/range; TRACE("azi=%d",azimuth); TRACE("range=%d",range); n = (nlinesub-1)*ncols*4 + skip_bytes; TRACE("n=%d\n",n); loopst = (nsampsub -1)*2; loopend = (nsampsub - 1 + nsampend) * 2; datain=mem.init_1Dchar(ncols*4); //dataout=mem.init_1Dshort(ncols*2); nmax=2*(nlinend+rows); create_progrcntl(L"Processing please wait"); pgr->m_pgr.SetRange32(1,nmax); pgr->m_pgr.SetStep(1); fseek(inp,sizeof(unsigned char)*n,0); for (i=0; i < nlinend; i++) { fseek(inp,sizeof(unsigned char)*12,SEEK_CUR); fread(datain, sizeof(char), ncols*4, inp); for(k=0;k<ncols*4;k+=2) { app=datain[k+1]; datain[k+1]=datain[k]; datain[k]=app; } dataout=(short*)datain; fwrite(dataout,sizeof(short),ncols*2,outp); pgr->m_pgr.StepIt(); global::process_events(); } int incr=i; TRACE("\n Processing for Master slc extarction Finished \n"); fclose(inp); fclose(outp); //create header Create_Header(outmaster,nlinend,ncols*2,sizeof(short),BANDS,INTERLEAVE_TYPE); //Add data file info AddDataFileInfo(pdoc->master_parfile,outmaster,"complex_short",nlinend,ncols,mlook_master,rows,cols); /*Calling a function which will multilook above slc image & converts its byte image*/ Multilook_SlcFile(rows,cols,ncols,bytes_per_pixel,outmaster,mlook_master,pgr); // ms1.DumpAllObjectsSince(); // TRACE("called first dumpallobjectssince"); /*Processing of master image data finishes here*/ /*Processing of slave data starts here*/ TRACE("slave data processing started"); // CMemoryState ms2; // ms2.Checkpoint(); slv=fopen(slavefilename,"rb"); outslv=fopen(outslave,"wb"); fseek(slv,186,0); fread(buf,sizeof(char),8,slv); skip_bytes=atoi(buf); fseek(slv,236,0); fread(buf,sizeof(char),8,slv); nlines=atoi(buf); fseek(slv,248,0); fread(buf,sizeof(char),8,slv); ncols=atoi(buf); nlinesub=1; nlinend=nlines; nsampsub=1; nsampend=ncols; rows=nlines/azimuth; cols=ncols/range; //nmax+=nlinend+rows; n = (nlinesub-1)*ncols*4 + skip_bytes; loopst = (nsampsub -1)*2; loopend = (nsampsub - 1 + nsampend) * 2; // m_ncount=0; //pgr->m_pgr.SetRange32(0,nlinend); fseek(slv,sizeof(unsigned char)*n,0); for (i=0; i < nlinend;i++) { fseek(slv,sizeof(unsigned char)*12,SEEK_CUR); fread(datain, sizeof(char), ncols*4, slv); for(k=0;k<ncols*4;k+=2) { app=datain[k+1]; datain[k+1]=datain[k]; datain[k]=app; } dataout=(short*)datain; fwrite(dataout,sizeof(short),ncols*2,outslv); pgr->m_pgr.StepIt(); global::process_events(); } fclose(slv); fclose(outslv); Create_Header(outslave,nlinend,ncols*2,sizeof(short),BANDS,INTERLEAVE_TYPE); AddDataFileInfo(pdoc->slave_parfile,outslave,"complex_short",nlinend,ncols,mlook_slave,rows,cols); /*Calling a function which will multilook above slc image & converts its byte image*/ Multilook_SlcFile(rows,cols,ncols,bytes_per_pixel,outslave,mlook_slave,pgr); // ms2.DumpAllObjectsSince(); /*Processing for slave data finishes here */ mem.del_char(datain); dataout=NULL; GetDlgItem(IDC_BTN_PROCESS)->EnableWindow(1); if(pgr!=NULL) { delete pgr; pgr=NULL; } AfxMessageBox(L"Importing Completed",MB_OK|MB_ICONINFORMATION,0); Display_ByteImage(mlook_master,mlook_slave); flag=true; } // Added try catch statement to pervent crash during import of data - Shaunak catch ( CException * pEx) { pEx->ReportError(); } } /////////////////////////////////////////////////////////////////////////////// // NAME:processTERRA_MasterSlave // DESCRIPTION:Processes maaster & slave image data for Terrasarx & outputs slc imagefiles //multilooked byte image files // PARAMETERS:none // RETURN: none // SIDE EFFECTS:If the user aborts process in middle it still doesnt stop internally & process whole code //ADDITION:multithreaded env remained to be added ///////////////////////////////////////////////////////////////////////////////// void import::processTERRA_MasterSlave() { try{ char * masterfilename,*slavefilename,*outmaster,*outslave,*mlook_master,*mlook_slave; int asfv,aslv,rsfv,rslv,rows,cols,nlines,ncols,kk,ll; CString mout,sout,multi_mast,multi_slave; short *master_dataout,*slave_dataout; short bytes_per_pixel; unsigned char intValue[4]; FILE *fpIn,*fpOut; masterfilename=Pfun.stringtochar(PathName1); slavefilename=Pfun.stringtochar(PathName2); mout=Pfun.AddPathAndFile(m_txtoutdir,L"\\master_terra.slc"); sout=Pfun.AddPathAndFile(m_txtoutdir,L"\\slave_terra.slc"); multi_mast=Pfun.AddPathAndFile(m_txtoutdir,L"\\mastterra_byte.img"); multi_slave=Pfun.AddPathAndFile(m_txtoutdir,L"\\slvterra_byte.img"); outmaster=Pfun.stringtochar(mout); outslave=Pfun.stringtochar(sout); mlook_master=Pfun.stringtochar(multi_mast); mlook_slave=Pfun.stringtochar(multi_slave); //CMemoryState ms1; //ms1.Checkpoint(); bytes_per_pixel=4; //MessageBox(L"\n Processing for Master & Slave slc extraction started ....\n"); GetDlgItem(IDC_BTN_PROCESS)->EnableWindow(0); fpIn=fopen(masterfilename,"rb"); fpOut=fopen(outmaster,"wb"); fread(&intValue, 1, 4, fpIn); //int bytes_in_burst = uchar_to_int(intValue); //TRACE("bytes in burst=%d\n",bytes_in_burst); fread(&intValue, 1, 4, fpIn); //int range_sample_relative_index = bigInt32(intValue);; fread(&intValue, 1, 4, fpIn); int range_samples = uchar_to_int(intValue); TRACE("range samples = %d\n",range_samples); fread(&intValue, 1, 4, fpIn); int azimuth_samples = uchar_to_int(intValue); TRACE("azimuth samples = %d\n",azimuth_samples); fread(&intValue, 1, 4, fpIn); //int burst_index = bigInt32(intValue); fread(&intValue, 1, 4, fpIn); int rangeline_total_number_bytes = uchar_to_int(intValue); TRACE("RTNB = %d \n",rangeline_total_number_bytes); fread(&intValue, 1, 4, fpIn); int total_number_lines =uchar_to_int(intValue); TRACE("TNL = %d\n", total_number_lines); fread(&intValue, 1, 4, fpIn);//////////////////////////// int n; // Check for the first and last azimuth and range pixel asfv = 1; // azimuth sample first valid aslv = azimuth_samples; // azimuth sample last valid rsfv = 1; // range sample first valid rslv = range_samples; // range sample last valid // Check valid range samples for (kk=4; kk<total_number_lines; kk++)////////////vertical dir//// { n= fseek(fpIn, 4*rangeline_total_number_bytes, SEEK_SET); fread(&intValue, 1, 4, fpIn); if (uchar_to_int(intValue) > rsfv) rsfv = uchar_to_int(intValue); fread(&intValue, 1, 4, fpIn); if (uchar_to_int(intValue) < rslv) rslv = uchar_to_int(intValue); } TRACE("1 value of k after checking valid range samples=%d\n",kk); // Check first valid azimuth sample n=fseek(fpIn, 2*rangeline_total_number_bytes+8, SEEK_SET);///asfv starts from 2nd line& 8 bytes skipped for filler values for (kk=2; kk<rslv; kk++) //asfv values starting from 2nd col...first 8 bytes are filler values...horizontal direction { fread(&intValue, 1, 4, fpIn); if (uchar_to_int(intValue) > asfv) asfv = uchar_to_int(intValue); } TRACE("2 value of k after checking asfv=%d\n",kk); TRACE("first rslv value=%d", rslv); // Check last valid azimuth sample n= fseek(fpIn, 3*rangeline_total_number_bytes+8, SEEK_SET); ///aslv starts on 3rd line & 8 bytes skipped for filew\r for (kk=2; kk<rslv; kk++)//asfv values starting from 2nd col......firts 8 values are filler values..hdir { fread(&intValue, 1, 4, fpIn); if (uchar_to_int(intValue) < aslv) rslv = kk - 1; } TRACE("3 value of k after checking aslv=%d\n",kk); TRACE("rsfirstv = %d\n",rsfv); TRACE("rslastv = %d\n",rslv); TRACE("asfirstv = %d\n",asfv); TRACE("aslastv = %d\n",aslv); // Read in the image nlines=aslv; ncols=rslv-rsfv+1; rows=nlines/azimuth; cols=ncols/range; master_dataout=mem.init_1Dshort(ncols*2); TRACE("nlines=%d\n\n",nlines); TRACE("rows=%d\n\n",rows); TRACE("ncols=%d\n\n",ncols); TRACE("cols=%d\n\n",cols); TRACE("azi=%d",azimuth); TRACE("range=%d",range); nmax=2*(nlines+rows);//set this value for timer int ct; TRACE("Before starting processing loop\n"); short ii; short *pii; char *f; create_progrcntl(L"Processing please wait"); pgr->m_pgr.SetRange32(1,nmax); pgr->m_pgr.SetStep(1); for (ll=asfv+3; ll<=aslv+3; ll++) { n=fseek(fpIn, ll*rangeline_total_number_bytes+8, SEEK_SET); //8 bytes skipped for rsfv & rslv values in each line ct=0; pii=&ii; f=(char*)pii; for (kk=rsfv; kk<=rslv; kk++) { f[1]=getc(fpIn); f[0]=getc(fpIn); master_dataout[ct]=ii; f[1]=getc(fpIn); f[0]=getc(fpIn); ct+=1; master_dataout[ct]=ii; ct+=1; } TRACE("ct value at the end of column loop = %d\n",ct); fwrite(master_dataout, sizeof(short), ncols*2,fpOut); pgr->m_pgr.StepIt(); global::process_events(); } TRACE("After completing processing loop\n\n"); TRACE("rslv-rsfv+1=%d\n\n",rslv-rsfv+1); TRACE("ll value=%d\n\n",ll); fclose(fpIn); fclose(fpOut); if(master_dataout!=NULL) { delete[] master_dataout; master_dataout=NULL; } TRACE("Before creating header for master terra file\n"); TRACE("nlines=%d\n",nlines); TRACE("ncols=%d\n",ncols); Create_Header(outmaster,nlines,ncols*2,sizeof(short),BANDS,INTERLEAVE_TYPE); AddDataFileInfo(pdoc->master_parfile,outmaster,"complex_short",nlines,ncols,mlook_master,rows,cols); /*Calling a function which will multilook above slc image & converts its byte image*/ Multilook_SlcFile(rows,cols,ncols,bytes_per_pixel,outmaster,mlook_master,pgr); /*Processing for slave image starts here*/ fpIn=fopen(slavefilename,"rb"); fpOut=fopen(outslave,"wb"); fread(&intValue, 1, 4, fpIn); //int bytes_in_burst = bigInt32(intValue); fread(&intValue, 1, 4, fpIn); //int range_sample_relative_index = bigInt32(intValue);; fread(&intValue, 1, 4, fpIn); range_samples = uchar_to_int(intValue); TRACE("range samples = %d\n",range_samples); fread(&intValue, 1, 4, fpIn); azimuth_samples = uchar_to_int(intValue); TRACE("azimuth samples = %d\n",azimuth_samples); fread(&intValue, 1, 4, fpIn); //int burst_index = bigInt32(intValue); fread(&intValue, 1, 4, fpIn); rangeline_total_number_bytes = uchar_to_int(intValue); TRACE("RTNB = %d \n",rangeline_total_number_bytes); fread(&intValue, 1, 4, fpIn); total_number_lines =uchar_to_int(intValue); TRACE("TNL = %d\n", total_number_lines); fread(&intValue, 1, 4, fpIn); // Check for the first and last azimuth and range pixel asfv = 1; // azimuth sample first valid aslv = azimuth_samples; // azimuth sample last valid rsfv = 1; // range sample first valid rslv = range_samples; // range sample last valid // Check valid range samples for (kk=4; kk<total_number_lines; kk++) { fseek(fpIn, 4*rangeline_total_number_bytes, SEEK_SET); fread(&intValue, 1, 4, fpIn); if (uchar_to_int(intValue) > rsfv) rsfv = uchar_to_int(intValue); fread(&intValue, 1, 4, fpIn); if (uchar_to_int(intValue) < rslv) rslv = uchar_to_int(intValue); } // Check first valid azimuth sample fseek(fpIn, 2*rangeline_total_number_bytes+8, SEEK_SET); for (kk=2; kk<rslv; kk++) { fread(&intValue, 1, 4, fpIn); if (uchar_to_int(intValue) > asfv) asfv = uchar_to_int(intValue); } // Check last valid azimuth sample fseek(fpIn, 3*rangeline_total_number_bytes+8, SEEK_SET); for (kk=2; kk<rslv; kk++) { fread(&intValue, 1, 4, fpIn); if (uchar_to_int(intValue) < aslv) rslv = kk - 1; } TRACE("rsfirstv = %d\n",rsfv); TRACE("rslastv = %d\n",rslv); TRACE("asfirstv = %d\n",asfv); TRACE("aslastv = %d\n",aslv); nlines=aslv; ncols=rslv-rsfv+1; rows=nlines/azimuth; cols=ncols/range; //nmax=nlines+rows-1; slave_dataout=mem.init_1Dshort(ncols*2); for (ll=asfv+3; ll<=aslv+3; ll++) { n=fseek(fpIn, ll*rangeline_total_number_bytes+8, SEEK_SET); //8 bytes skipped for rsfv & rslv values in each line ct=0; pii=&ii; f=(char*)pii; for (kk=rsfv; kk<=rslv; kk++) { f[1]=getc(fpIn); f[0]=getc(fpIn); slave_dataout[ct]=ii; f[1]=getc(fpIn); f[0]=getc(fpIn); ct+=1; slave_dataout[ct]=ii; ct+=1; } TRACE("ct value at the end of column loop = %d\n",ct); fwrite(slave_dataout, sizeof(short), ncols*2,fpOut); pgr->m_pgr.StepIt(); global::process_events(); } fclose(fpIn); fclose(fpOut); if(slave_dataout!=NULL) { delete[] slave_dataout; slave_dataout=NULL; } Create_Header(outslave,nlines,ncols*2,sizeof(short),BANDS,INTERLEAVE_TYPE); AddDataFileInfo(pdoc->slave_parfile,outslave,"complex_short",nlines,ncols,mlook_slave,rows,cols); /*Calling a function which will multilook above slc image & converts its byte image*/ Multilook_SlcFile(rows,cols,ncols,bytes_per_pixel,outslave,mlook_slave,pgr); //ms2.DumpAllObjectsSince(); GetDlgItem(IDC_BTN_PROCESS)->EnableWindow(1); if(pgr!=NULL) { delete pgr; pgr=NULL; } AfxMessageBox(L"Importing Completed",MB_OK|MB_ICONINFORMATION,0); Display_ByteImage(mlook_master,mlook_slave); flag=true; } catch ( CException * pEx) { pEx->ReportError(); } } /*When I first looked at the problem, my initial view was that the char 'c' should be shifted left 8 bits - all 8 bits being discarded, the empty char would then be cast to an int of value 0. A bit of research reveals Usual Unary Conversions - this is where to reduce the large number of arithmetic types, conversions are applied automatically to operands of the unary '!', '-', '~' and '*' operators, and to each of the operands of the binary '<<' and '>>' operators. Therefore the char 'c' is converted to and int first, then shifted left, giving the answer you see. is being promoted to an int before the shift operation is done */ /////////////////////////////////////////////////////////////////////////////// // NAME:uchar_to_int // DESCRIPTION:Converts a 4 byte array of unsigned bytes to an long // PARAMETERS:param b an array of 4 unsigned bytes // RETURN:return a long representing the unsigned int // SIDE EFFECTS:none ///////////////////////////////////////////////////////////////////////////////// int import::uchar_to_int(unsigned char b[]) { int l = 0; l |= b[0] & 0xFF; l <<= 8; l |= b[1] & 0xFF; l <<= 8; l |= b[2] & 0xFF; l <<= 8; l |= b[3] & 0xFF; return l; } short int import::char_to_short(char b[]) { short int l = 0; l |= b[0] & 0xFF; l <<= 8; l |= b[1] & 0xFF; /*l <<= 8; l |= b[2] & 0xFF; l <<= 8; l |= b[3] & 0xFF;*/ return l; } /////////////////////////////////////////////////////////////////////////////// // NAME:processALOS_MasterSlave // DESCRIPTION:Processes maaster & slave image data for Terrasarx & outputs slc imagefiles //multilooked byte image files // PARAMETERS:none // RETURN: none // SIDE EFFECTS:If the user aborts process in middle it still doesnt stop internally & process whole code //ADDITION:multithreaded env remained to be added ///////////////////////////////////////////////////////////////////////////////// void import::processALOS_MasterSlave() { try{ char *masterfilename,*slavefilename,*outmaster,*outslave,*mlook_master,*mlook_slave; FILE *inp,*outp,*inp_slv,*out_slv; int nrows,ncols,skip_bytes,i,j,rows,cols; char *buf,*buf2,*datain; char temp1,temp2; CString multi_mast,multi_slave, mout,sout; short bytes_per_pixel; float *dataout; pdoc=GetDocPtr(); masterfilename=Pfun.stringtochar(PathName1); slavefilename=Pfun.stringtochar(PathName2); mout=Pfun.AddPathAndFile(m_txtoutdir,L"\\master_alos.slc"); sout=Pfun.AddPathAndFile(m_txtoutdir,L"\\slave_alos.slc"); multi_mast=Pfun.AddPathAndFile(m_txtoutdir,L"\\mastalos_byte.img"); multi_slave=Pfun.AddPathAndFile(m_txtoutdir,L"\\slvalos_byte.img"); outmaster=Pfun.stringtochar(mout); outslave=Pfun.stringtochar(sout); mlook_master=Pfun.stringtochar(multi_mast); mlook_slave=Pfun.stringtochar(multi_slave); // CMemoryState ms1; // ms1.Checkpoint(); //MessageBox(L"Processing for master & slave Alos data started",L"Processing",MB_OK|MB_ICONINFORMATION); inp=fopen(masterfilename,"rb"); outp=fopen(outmaster,"wb"); buf=mem.init_1Dchar(28); fseek(inp,236,0); fread(buf,sizeof(char),8,inp); nrows=atoi(buf); TRACE("nrows=%d\n",nrows); fseek(inp,248,0); fread(buf,sizeof(char),8,inp); ncols=atoi(buf); TRACE("ncols=%d\n",ncols); fseek(inp,276,0); fread(buf,sizeof(char),4,inp); buf[4]='\0'; skip_bytes=atoi(buf); TRACE("skip_bytes=%d\n",skip_bytes); fseek(inp,280,0); fread(buf,sizeof(char),8,inp); buf[8]='\0'; int reclen=atoi(buf); bytes_per_pixel=short(atoi(buf)/ncols); TRACE("bytes_per_pixel=%d\n",bytes_per_pixel); fseek(inp,400,0); fread(buf,sizeof(char),28,inp); buf[28]='\0'; CString s; s=buf; TRACE("%s",buf); rows=nrows/azimuth; cols=ncols/range; TRACE("azi=%d",azimuth); TRACE("range=%d",range); datain=mem.init_1Dchar(ncols*bytes_per_pixel); dataout=mem.init_1Dfloat(ncols*2); nmax=2*(nrows+rows); /*if(buf!=NULL) mem.del_char(buf);*/ // create_progrcntl(L"Processing please wait"); pgr->m_pgr.SetRange32(1,nmax); pgr->m_pgr.SetStep(1); fseek(inp,720,0); for(i=0;i<nrows;i++) { fseek(inp,skip_bytes,SEEK_CUR); fread(datain,sizeof(char),ncols*bytes_per_pixel,inp); for(j=0;j<ncols*bytes_per_pixel;j+=4) { temp1=datain[j+3]; datain[j+3]=datain[j]; datain[j]=temp1; temp2=datain[j+2]; datain[j+2]=datain[j+1]; datain[j+1]=temp2; } dataout=(float*)datain; fwrite(dataout,sizeof(float),ncols*2,outp); pgr->m_pgr.StepIt(); global::process_events(); } fclose(inp); fclose(outp); Create_Header(outmaster,nrows,ncols*2,sizeof(float),BANDS,INTERLEAVE_TYPE); AddDataFileInfo(pdoc->master_parfile,outmaster,"complex_float",nrows,ncols,mlook_master,rows,cols); /*Calling a function which will multilook above slc image & converts its byte image*/ Multilook_SlcFile(rows,cols,ncols,bytes_per_pixel,outmaster,mlook_master,pgr); // ms1.DumpAllObjectsSince(); //delete[] dataout; mem.del_char(datain); dataout=NULL; /*Processing for slave begins here*/ // CMemoryState ms2; // ms2.Checkpoint(); inp_slv=fopen(slavefilename,"rb"); out_slv=fopen(outslave,"wb"); buf2=mem.init_1Dchar(28); fseek(inp_slv,236,0); fread(buf2,sizeof(char),8,inp_slv); buf2[8]='\0'; nrows=atoi(buf2); TRACE("nrows=%d\n",nrows); fseek(inp_slv,248,0); fread(buf2,sizeof(char),8,inp_slv); buf2[8]='\0'; ncols=atoi(buf2); TRACE("ncols=%d\n",ncols); fseek(inp_slv,276,0); fread(buf2,sizeof(char),4,inp_slv); buf2[4]='\0'; skip_bytes=atoi(buf2); TRACE("skip_bytes=%d\n",skip_bytes); fseek(inp_slv,280,0); fread(buf2,sizeof(char),8,inp_slv); buf2[8]='\0'; reclen=atoi(buf2); bytes_per_pixel=short(atoi(buf2)/ncols); TRACE("bytes_per_pixel=%d\n",bytes_per_pixel); fseek(inp_slv,400,0); fread(buf2,sizeof(char),28,inp_slv); buf2[28]='\0'; rows=nrows/azimuth; cols=ncols/range; TRACE("%s",buf2); //nmax=nrows+rows; /*if(buf2!=NULL) mem.del_char(buf2);*/ char *datain_slv;//taken separately for slave..becoz no of cols of both image are differant float *dataout_slv; // m_ncount=0; datain_slv=mem.init_1Dchar(ncols*bytes_per_pixel); dataout_slv=mem.init_1Dfloat(ncols*2); fseek(inp_slv,720,0); for(i=0;i<nrows;i++) { fseek(inp_slv,skip_bytes,SEEK_CUR); fread(datain_slv,sizeof(char),ncols*bytes_per_pixel,inp_slv); for(j=0;j<ncols*bytes_per_pixel;j+=4) { temp1=datain_slv[j+3]; datain_slv[j+3]=datain_slv[j]; datain_slv[j]=temp1; temp2=datain_slv[j+2]; datain_slv[j+2]=datain_slv[j+1]; datain_slv[j+1]=temp2; } dataout_slv=(float*)datain_slv; fwrite(dataout_slv,sizeof(float),ncols*2,out_slv); pgr->m_pgr.StepIt(); global::process_events(); } fclose(inp_slv); fclose(out_slv); Create_Header(outslave,nrows,ncols*2,sizeof(float),BANDS,INTERLEAVE_TYPE); AddDataFileInfo(pdoc->slave_parfile,outslave,"complex_float",nrows,ncols,mlook_slave,rows,cols); /*Calling a function which will multilook above slc image & converts its byte image*/ Multilook_SlcFile(rows,cols,ncols,bytes_per_pixel,outslave,mlook_slave,pgr); // ms2.DumpAllObjectsSince(); //delete[] dataout_slv; mem.del_char(datain_slv); dataout_slv=NULL; Display_ByteImage(mlook_master,mlook_slave); if(pgr!=NULL) { delete pgr; pgr=NULL; } AfxMessageBox(L"Importing Completed",MB_OK|MB_ICONINFORMATION,0); flag=true; buf=NULL; buf2=NULL; } catch ( CException * pEx) { pEx->ReportError(); } } /////////////////////////////////////////////////////////////////////////////// // NAME:processRSAT2_MasterSlave // DESCRIPTION:Processes maaster & slave image data for Terrasarx & outputs slc imagefiles //multilooked byte image files // PARAMETERS:none // RETURN: none // SIDE EFFECTS:If the user aborts process in middle it still doesnt stop internally & process whole code //ADDITION:multithreaded env remained to be added ///////////////////////////////////////////////////////////////////////////////// void import::processRSAT2_MasterSlave() { try{ char *masterfilename,*slavefilename,*outmaster,*outslave,*mlook_master,*mlook_slave; CString mout,sout,multi_mast,multi_slave; int ncols,i,j,Strip; char *buffer, app; long PointerPosition,temp11; FILE *fp,*outp; unsigned long n; short bytes_per_pixel; pdoc=GetDocPtr(); masterfilename=Pfun.stringtochar(PathName1); slavefilename=Pfun.stringtochar(PathName2); mout=Pfun.AddPathAndFile(m_txtoutdir,L"\\master_rsat2.slc"); sout=Pfun.AddPathAndFile(m_txtoutdir,L"\\slave_rsat2.slc"); multi_mast=Pfun.AddPathAndFile(m_txtoutdir,L"\\mastrsat2_byte.img"); multi_slave=Pfun.AddPathAndFile(m_txtoutdir,L"\\slvrsat2_byte.img"); outmaster=Pfun.stringtochar(mout); outslave=Pfun.stringtochar(sout); mlook_master=Pfun.stringtochar(multi_mast); mlook_slave=Pfun.stringtochar(multi_slave); // MessageBox(L"Extraction for master & slave started"); /*Processing for master data started here */ bytes_per_pixel=4; // CMemoryState ms1; //ms1.Checkpoint(); read_tiff_strip(masterfilename); fp=fopen(masterfilename,"rb"); outp=fopen(outmaster,"wb"); ncols = Strip_Bytes[0]/(4*Rstrip); TRACE("azimuth=%d\n",azimuth); TRACE("range=%d\n",range); int rows=Nlig/azimuth; int cols=ncols/range; TRACE("rows=%d\tcols=%d\n",rows,cols); short **rev_data ; short *data; // float **rev_data; // float **data; buffer=mem.init_1Dchar(ncols*4); data=mem.init_1Dshort(ncols*2); rev_data=mem.init_2Dshort(Nlig,ncols*2); nmax=2*(Nlig+rows); TRACE("azi=%d",azimuth); TRACE("range=%d",range); /*Generating slc image without multilooking*/ Strip=0; fseek(fp,Strip_Offset[Strip],0); TRACE("%d\n",Strip_Offset[Strip]); create_progrcntl(L"Processing please wait"); pgr->m_pgr.SetRange32(1,nmax); pgr->m_pgr.SetStep(1); for(i=0;i<Nlig;i++) { n=fread(buffer,sizeof(char),ncols*4,fp); TRACE("Processing line=%d\n",i); temp11 = i+1; if (fmod(temp11+0.0,Rstrip)==0) { Strip++; //CurrentPointerPosition = ftell(fp); PointerPosition = Strip_Offset[Strip]; fseek(fp,PointerPosition,0); } for(j=0;j<ncols*4;j+=2) { app=buffer[j+1]; buffer[j+1]=buffer[j]; buffer[j]=app; } data=(short*)buffer; for(int r=0;r<ncols*2;r++) { rev_data[i][r]=data[r]; } pgr->m_pgr.StepIt(); global::process_events(); } for(i=Nlig-1;i>=0;i--)//this loop changed here as it was writing one line less original was i=Nlig;i>0;i-- { fwrite(rev_data[i],sizeof(short),ncols*2,outp); } TRACE("after i val=%d\t nlig=%d\n",i,Nlig); fclose(fp); fclose(outp); delete[] buffer; buffer=NULL; delete[] Strip_Bytes; Strip_Bytes=NULL; delete[] Strip_Offset; Strip_Offset=NULL; if(rev_data!=NULL) { mem.del_short(rev_data,Nlig); } Create_Header(outmaster,Nlig,ncols*2,sizeof(short),BANDS,INTERLEAVE_TYPE); AddDataFileInfo(pdoc->master_parfile,outmaster,"complex_short",Nlig,ncols,mlook_master,rows,cols); /*Calling a function which will multilook above slc image & converts its byte image*/ Multilook_SlcFile(rows,cols,ncols,bytes_per_pixel,outmaster,mlook_master,pgr); /*Processing for master data finished here */ /*Processing for slave data started here */ // CMemoryState ms2; // ms2.Checkpoint(); read_tiff_strip(slavefilename); fp=fopen(slavefilename,"rb"); outp=fopen(outslave,"wb"); ncols = Strip_Bytes[0]/(4*Rstrip); rows=Nlig/azimuth; cols=ncols/range; buffer=mem.init_1Dchar(ncols*4); data=mem.init_1Dshort(ncols*2); rev_data=mem.init_2Dshort(Nlig,ncols*2); // m_ncount=0; //nmax=Nlig+rows; /*Generating slc image without multilooking*/ Strip=0; fseek(fp,Strip_Offset[Strip],0); for(i=0;i<Nlig;i++) { n=fread(buffer,sizeof(char),ncols*4,fp); temp11 = i+1; if (fmod(temp11+0.0,Rstrip)==0) { Strip++; //CurrentPointerPosition = ftell(fp); PointerPosition = Strip_Offset[Strip]; fseek(fp,PointerPosition,0); // fseek(fp, (PointerPosition - CurrentPointerPosition), SEEK_CUR); } for(j=0;j<ncols*4;j+=2) { app=buffer[j+1]; buffer[j+1]=buffer[j]; buffer[j]=app; } data=(short*)buffer; for(int r=0;r<ncols*2;r++) { rev_data[i][r]=data[r]; } } for(i=Nlig-1;i>=0;i--)//this loop changed here as it was writing one line less original was i=Nlig;i>0;i-- { fwrite(rev_data[i],sizeof(short),ncols*2,outp); } fclose(fp); fclose(outp); delete[] buffer; buffer=NULL; delete[] Strip_Bytes; Strip_Bytes=NULL; delete[] Strip_Offset; Strip_Offset=NULL; if(rev_data!=NULL) { mem.del_short(rev_data,Nlig); } Create_Header(outslave,Nlig,ncols*2,sizeof(short),BANDS,INTERLEAVE_TYPE); AddDataFileInfo(pdoc->slave_parfile,outslave,"complex_short",Nlig,ncols,mlook_slave,rows,cols); /*Calling a function which will multilook above slc image & converts its byte image*/ Multilook_SlcFile(rows,cols,ncols,bytes_per_pixel,outslave,mlook_slave,pgr); if(pgr!=NULL) { delete pgr; pgr=NULL; } AfxMessageBox(L"Importing Completed",MB_OK|MB_ICONINFORMATION,0); Display_ByteImage(mlook_master,mlook_slave); flag=true; } catch ( CException * pEx) { pEx->ReportError(); } } /////////////////////////////////////////////////////////////////////////////// // NAME:read_tiff_strip // DESCRIPTION:Used in case of Radarsat2 only.It gathers all of the information for //retreiving imagedata from Radarsat2 files. // PARAMETERS:char*-name of the rsat2 file from which all these informatin need to be gathered // RETURN: none // SIDE EFFECTS:none ///////////////////////////////////////////////////////////////////////////////// void import::read_tiff_strip(char *filename) { unsigned char buffer[4]; int i, k,il,IEEEFormat; long unsigned int offset; short int Ndir, Flag, Type,is; FILE *fileinput; char *pc; int *vl; short int *v; fileinput=fopen(filename,"rb"); rewind(fileinput); /*Tiff File Header*/ /* Little Big endian & TIFF identifier */ fread(buffer, 1, 4, fileinput); if(buffer[0] == 0x49 && buffer[1] == 0x49 && buffer[2] == 0x2a && buffer[3] == 0x00) IEEEFormat = 0;//byte order little endian //buffer[0]&buffer[1] identify for little & big indian....buffer[2] & buffer[3] idenfy for digit 42 that is a tiff file if(buffer[0] == 0x4d && buffer[1] == 0x4d && buffer[2] == 0x00 && buffer[3] == 0x2a) IEEEFormat = 1;// byte order big endian TRACE("IEEE Format: %d\n", IEEEFormat); if (IEEEFormat == 0) fread(&offset, sizeof(int), 1, fileinput);//The offset (in bytes) of the first IFD //little endian case if (IEEEFormat == 1) { vl = &il; pc = (char *) vl; pc[3] =char(char(getc(fileinput))); pc[2] =char(char(getc(fileinput))); pc[1] =char(char(getc(fileinput))); pc[0] =char(char(getc(fileinput))); offset = il; } TRACE("Offset: %d\n", offset);//The offset (in bytes) of the first IFD.//big endian case rewind(fileinput); fseek(fileinput, offset, SEEK_SET);//go to first IFD if (IEEEFormat == 0) //read 2-byte count of the number of directory entries (i.e., the number of fields),//little endian case { fread(&Ndir, sizeof(short int), 1, fileinput); TRACE("iee=0\n"); } if (IEEEFormat == 1) //read 2-byte count of the number of directory entries (i.e., the number of fields),//big endian case { TRACE("iee=1\n"); v = &is; pc = (char *) v; pc[1] =char(char(getc(fileinput))); pc[0] =char(char(getc(fileinput))); Ndir = is; } TRACE("Ndir:%d\n", Ndir); for (i=0; i<Ndir; i++) { Flag = 0; Type = 0; Count = 0; Value = 0; if (IEEEFormat == 0) { fread(&Flag, sizeof(short int), 1, fileinput);//2byte size fread(&Type, sizeof(short int), 1, fileinput); fread(&Count, sizeof(int), 1, fileinput); //4 byte size if (Type == 3)//3 = SHORT 16-bit (2-byte) unsigned integer { fread(&Value, sizeof(short int), 1, fileinput); fread(&k, sizeof(short int), 1, fileinput); } if (Type == 4) fread(&Value, sizeof(int), 1, fileinput); //4 = LONG 32-bit (4-byte) unsigned integer. if ((Type != 3) && (Type != 4)) fread(&Value, sizeof(int), 1, fileinput); } if (IEEEFormat == 1) { v = &is; pc = (char *) v; pc[1] = char(getc(fileinput)); pc[0] = char(getc(fileinput)); Flag = is; v = &is; pc = (char *) v; pc[1] = char(getc(fileinput)); pc[0] = char(getc(fileinput)); Type = is; vl = &il; pc = (char *) vl; pc[3] = char(getc(fileinput)); pc[2] = char(getc(fileinput)); pc[1] = char(getc(fileinput)); pc[0] = char(getc(fileinput)); Count = il; if (Type == 3) { v = &is; pc = (char *) v; pc[1] = char(getc(fileinput)); pc[0] = char(getc(fileinput)); Value = is; fread(&k, sizeof(short int), 1, fileinput); } if (Type == 4) { vl = &il; pc = (char *) vl; pc[3] = char(getc(fileinput)); pc[2] = char(getc(fileinput)); pc[1] = char(getc(fileinput)); pc[0] = char(getc(fileinput)); Value = il; } if ((Type != 3) && (Type != 4)) fread(&Value, sizeof(int), 1, fileinput); } if (Flag == 257) { Nlig = Value;//The number of rows (sometimes described as scanlines) in the image.. TRACE("Nlig 257: %d\n", Nlig); } if (Flag == 273) { Nstrip = Count;//no of strips mean this many no of byte offset values are given////is the number ofvalues //TRACE("Nstrip 273: %d\n", Nstrip); } if (Flag == 278) { Rstrip = Value;//The number of rows in each strip (except possibly the last strip.) //TRACE("Rstrip %d\n", Rstrip); } if (Flag == 273) { offset_strip = Value;//reading the byte offset of first strip.so later we can go there read remaining values for rem strips this length is count field & thsi flg that is nstrip TRACE("offset_strip 273: %d\n", offset_strip); } if (Flag == 279) { offset_strip_byte = Value;//no of bytes in first strip.so later we can go there read remaining values for rem strips this length is count field & thsi flg that is nstrip //TRACE("offset strip_byte 279: %d\n", offset_strip_byte); } } Strip_Offset =mem.init_1Dint(Nstrip); Strip_Bytes =mem.init_1Dint(Nstrip); rewind(fileinput); fseek(fileinput, offset_strip, SEEK_SET);//where first strip is starting sucessively it contains for (i=0; i<Nstrip; i++) // { if (IEEEFormat == 0) fread(&Value, sizeof(int), 1, fileinput); if (IEEEFormat == 1) { vl = &il; pc = (char *) vl; pc[3] = char(getc(fileinput)); pc[2] = char(getc(fileinput)); pc[1] = char(getc(fileinput)); pc[0] = char(getc(fileinput)); Value = il; } Strip_Offset[i] = Value; TRACE("Strip_Offset: %d %d\n", i, Strip_Offset[i]); } rewind(fileinput); fseek(fileinput, offset_strip_byte, SEEK_SET); for (i=0; i<Nstrip; i++) { if (IEEEFormat == 0) fread(&Value, sizeof(int), 1, fileinput); if (IEEEFormat == 1) { vl = &il; pc = (char *) vl; pc[3] = char(getc(fileinput)); pc[2] = char(getc(fileinput)); pc[1] = char(getc(fileinput)); pc[0] = char(getc(fileinput)); Value = il; } Strip_Bytes[i] = Value; } TRACE("NStrip: %d\n", Nstrip); fclose(fileinput); } /////////////////////////////////////////////////////////////////////////////// // NAME:processENSAT_MasterSlave // DESCRIPTION:Processes maaster & slave image data for Envisat & outputs slc imagefiles, //multilooked byte image files along with their headers // PARAMETERS:none // RETURN: none // SIDE EFFECTS:If the user aborts process in middle it still doesnt stop internally & process whole code //ADDITION:multithreaded env remained to be added ///////////////////////////////////////////////////////////////////////////////// void import::processENSAT_MasterSlave() { //try{ char *masterfilename,*slavefilename,*outmaster,*outslave,*mlook_master,*mlook_slave; CString mout,sout,multi_mast,multi_slave; int i,k,nrows,ncols,cols,rows; char *buf, app,*datain; long cur_location; FILE *fp,*outp; short bytes_per_pixel=4; short *dataout; pdoc=GetDocPtr(); buf=new char[6]; masterfilename=Pfun.stringtochar(PathName1); slavefilename=Pfun.stringtochar(PathName2); mout=Pfun.AddPathAndFile(m_txtoutdir,L"\\master_envisat.slc"); sout=Pfun.AddPathAndFile(m_txtoutdir,L"\\slave_envisat.slc"); multi_mast=Pfun.AddPathAndFile(m_txtoutdir,L"\\mastenvisat_byte.img"); multi_slave=Pfun.AddPathAndFile(m_txtoutdir,L"\\slvenvisat_byte.img"); outmaster=Pfun.stringtochar(mout); outslave=Pfun.stringtochar(sout); mlook_master=Pfun.stringtochar(multi_mast); mlook_slave=Pfun.stringtochar(multi_slave); //MessageBox(L"Extraction for master & slave started"); /*Processing for master slc file starts here*/ GetDlgItem(IDC_BTN_PROCESS)->EnableWindow(0); //CMemoryState ms1; //ms1.Checkpoint(); bytes_per_pixel=4; fp=fopen(masterfilename,"rb"); outp=fopen(outmaster,"wb"); //////////////////////////// cur_location=1075; fseek(fp,cur_location,0); fread(buf,sizeof(char),21,fp); unsigned long tot_recsize=atol(buf); TRACE("tot recsize=%d\n",tot_recsize); /////////////////////////////// cur_location=MPH+SPH+SQ+56; fseek(fp,cur_location,0); fread(buf,sizeof(char),4,fp); nrows=Pfun.big_uint(buf); TRACE("lines=%d\n",nrows); cur_location=MPH+SPH+SQ+60; fseek(fp,cur_location,0); fread(buf,sizeof(char),4,fp); ncols=Pfun.big_uint(buf); TRACE("pixels=%d\n",ncols); rows=nrows/azimuth; cols=ncols/range; datain=mem.init_1Dchar(ncols*4); dataout=mem.init_1Dshort(ncols*2); unsigned long bytes_datagroup=nrows*(17 + (4*ncols)); short mul_factor=1; unsigned long cpp = tot_recsize - (mul_factor * bytes_datagroup + MPH + SPH + MPP + SQ + DCP + ADSR); TRACE("cpp=%d\n\n\n",cpp); cur_location=MPH+SPH+SQ+MPP+DCP+cpp+ADSR; fseek(fp,cur_location,0);//skipping header part nmax=2*(nrows+rows); create_progrcntl(L"Processing please wait"); pgr->m_pgr.SetRange32(1,3000); pgr->m_pgr.SetStep(1); TRACE("cur_location=%d\n\n\n",cur_location); for (i=0; i < nrows; i++) { fseek(fp,17,SEEK_CUR); fread(datain, sizeof(char), ncols*4, fp); for(k=0;k<ncols*4;k+=2) { app=datain[k+1]; datain[k+1]=datain[k]; datain[k]=app; } dataout=(short*)datain; fwrite(dataout,sizeof(short),ncols*2,outp); pgr->m_pgr.StepIt(); global::process_events(); } fclose(fp); fclose(outp); delete[] datain; datain=NULL; Create_Header(outmaster,nrows,ncols*2,sizeof(short),BANDS,INTERLEAVE_TYPE); AddDataFileInfo(pdoc->master_parfile,outmaster,"complex_short",nrows,ncols,mlook_master,rows,cols); /*Calling a function which will multilook above slc image & converts its byte image*/ Multilook_SlcFile(rows,cols,ncols,bytes_per_pixel,outmaster,mlook_master); fp=fopen(slavefilename,"rb"); outp=fopen(outslave,"wb"); //////////////////////////// cur_location=1075; fseek(fp,cur_location,0); fread(buf,sizeof(char),21,fp); tot_recsize=atol(buf); TRACE("tot recsize=%d\n",tot_recsize); /////////////////////////////// cur_location=MPH+SPH+SQ+56; fseek(fp,cur_location,0); fread(buf,sizeof(char),4,fp); nrows=Pfun.big_uint(buf); TRACE("lines=%d\n",nrows); cur_location=MPH+SPH+SQ+60; fseek(fp,cur_location,0); fread(buf,sizeof(char),4,fp); ncols=Pfun.big_uint(buf); TRACE("pixels=%d\n",ncols); rows=nrows/azimuth; cols=ncols/range; datain=mem.init_1Dchar(ncols*4); dataout=mem.init_1Dshort(ncols*2); bytes_datagroup=nrows*(17 + (4*ncols)); mul_factor=1; cpp = tot_recsize - (mul_factor * bytes_datagroup + MPH + SPH + MPP + SQ + DCP + ADSR); TRACE("cpp=%d\n\n\n",cpp); cur_location=MPH+SPH+SQ+MPP+DCP+cpp+ADSR; fseek(fp,cur_location,0);//skipping header part //nmax=nrows+rows; TRACE("cur_location=%d\n\n\n",cur_location); for (i=0; i < nrows; i++) { fseek(fp,17,SEEK_CUR); fread(datain, sizeof(char), ncols*4, fp); for(k=0;k<ncols*4;k+=2) { app=datain[k+1]; datain[k+1]=datain[k]; datain[k]=app; } dataout=(short*)datain; fwrite(dataout,sizeof(short),ncols*2,outp); pgr->m_pgr.StepIt(); global::process_events(); } fclose(fp); fclose(outp); delete[] datain; datain=NULL; Create_Header(outslave,nrows,ncols*2,sizeof(short),BANDS,INTERLEAVE_TYPE); AddDataFileInfo(pdoc->slave_parfile,outslave,"complex_short",nrows,ncols,mlook_slave,rows,cols); /*Calling a function which will multilook above slc image & converts its byte image*/ Multilook_SlcFile(rows,cols,ncols,bytes_per_pixel,outslave,mlook_slave,pgr); /*Processing for slave slc image ends here */ /*if(pgr!=NULL) { delete pgr; pgr=NULL; }*/ AfxMessageBox(L"Importing Completed",MB_OK|MB_ICONINFORMATION,0); GetDlgItem(IDC_BTN_PROCESS)->EnableWindow(1); Display_ByteImage(mlook_master,mlook_slave); flag=true; //} //catch ( CException * pEx) //{ //pEx->ReportError(); //} } void import::processCOSMO_MasterSlave() { try { char *masterfilename,*slavefilename,*outmaster,*outslave,*mlook_master,*mlook_slave; CString mout,sout,multi_mast,multi_slave; int i,k,nrows,ncols,cols,rows; char *buf, app,*datain; FILE *fp,*outp; short bytes_per_pixel=4; pdoc=GetDocPtr(); masterfilename=Pfun.stringtochar(PathName1); slavefilename=Pfun.stringtochar(PathName2); mout=Pfun.AddPathAndFile(m_txtoutdir,L"\\master_cosmo.slc"); sout=Pfun.AddPathAndFile(m_txtoutdir,L"\\slave_cosmo.slc"); multi_mast=Pfun.AddPathAndFile(m_txtoutdir,L"\\mastcosmo_byte.img"); multi_slave=Pfun.AddPathAndFile(m_txtoutdir,L"\\slvcosmo_byte.img"); outmaster=Pfun.stringtochar(mout); outslave=Pfun.stringtochar(sout); mlook_master=Pfun.stringtochar(multi_mast); mlook_slave=Pfun.stringtochar(multi_slave); vector<char*> v; v.push_back("Number_of_lines_original"); v.push_back("Number_of_pixels_original"); map<char*,CString>valarray=Pread.read_params(v,pdoc->master_parfile); nrows=wcstol(valarray["Number_of_lines_original"],NULL,10); ncols=wcstol(valarray["Number_of_pixels_original"],NULL,10); rows=int(nrows/azimuth); cols=int(ncols/range); Create_Header(outmaster,nrows,ncols*2,sizeof(short),BANDS,INTERLEAVE_TYPE); AddDataFileInfo(pdoc->master_parfile,outmaster,"complex_short",nrows,ncols,mlook_master,rows,cols); STARTUPINFO siStartupInfo; PROCESS_INFORMATION piProcessInfo; CString param; CString h5file; pgr=NULL; h5file=PathName1; int n=h5file.ReverseFind('\\'); CString out ; out=mout; param=" /C h5dump -d /S01/SBI -b LE -o "; param+=out; param+=" "; param+=h5file; //MessageBox(param); memset(&siStartupInfo, 0, sizeof(siStartupInfo)); memset(&piProcessInfo, 0, sizeof(piProcessInfo)); siStartupInfo.cb = sizeof(siStartupInfo); // Promt user about command mode ~ Shaunak AfxMessageBox(L"File will now be processed in Command Line mode.\nPlease wait until process completes and Command Line shuts automatically",MB_OK,0); //siStartupInfo.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESIZE; // siStartupInfo.wShowWindow=SW_HIDE; int l=1024; LPWSTR lptr=param.GetBuffer(l); param.ReleaseBuffer(); if(CreateProcess(L"C:\\windows\\system32\\cmd.exe", // Application name lptr, // Application arguments 0, 0, FALSE, CREATE_DEFAULT_ERROR_MODE, 0, 0, // Working directory &siStartupInfo, &piProcessInfo) == FALSE) { AfxMessageBox(_T("Error")); // Could not start application -> call 'GetLastError()' } /*nmax= (nrows+rows); create_progrcntl(L"Processing please wait"); pgr->m_pgr.SetRange32(1,nmax); pgr->m_pgr.SetStep(1); for(int i=0;i<nmax;i++) pgr->m_pgr.StepIt();*/ WaitForSingleObject( piProcessInfo.hProcess, INFINITE ); //TerminateProcess(piProcessInfo.hProcess,0); CloseHandle(piProcessInfo.hProcess); //MessageBox(L"first process closed handle"); /*Calling a function which will multilook above slc image & converts its byte image*/ nmax= rows; create_progrcntl(L"Processing please wait"); pgr->m_pgr.SetRange32(1,nmax); pgr->m_pgr.SetStep(1); Multilook_SlcFile(rows,cols,ncols,bytes_per_pixel,outmaster,mlook_master,pgr); TerminateProcess(piProcessInfo.hProcess,0); if(pgr!=NULL) { delete pgr; pgr=NULL; } STARTUPINFO siStartupInfo2; PROCESS_INFORMATION piProcessInfo2; CString h5file2,param2; v.clear(); valarray.clear(); v.push_back("Number_of_lines_original"); v.push_back("Number_of_pixels_original"); valarray=Pread.read_params(v,pdoc->slave_parfile); nrows=wcstol(valarray["Number_of_lines_original"],NULL,10); ncols=wcstol(valarray["Number_of_pixels_original"],NULL,10); rows=int(nrows/azimuth); cols=int(ncols/azimuth); Create_Header(outslave,nrows,ncols*2,sizeof(short),BANDS,INTERLEAVE_TYPE); AddDataFileInfo(pdoc->slave_parfile,outslave,"complex_short",nrows,ncols,mlook_slave,rows,cols); h5file2=PathName2; n=h5file2.ReverseFind('\\'); out=sout; param2=" /C h5dump -d /S01/SBI -b LE -o "; param2+=out; param2+=" "; param2+=h5file2; //MessageBox(param2); memset(&siStartupInfo2, 0, sizeof(siStartupInfo2)); memset(&piProcessInfo2, 0, sizeof(piProcessInfo2)); siStartupInfo2.cb = sizeof(siStartupInfo2); //siStartupInfo2.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESIZE; //siStartupInfo2.wShowWindow=SW_HIDE; // Promt user about command mode ~ Shaunak AfxMessageBox(L"File will now be processed in Command Line mode.\nPlease wait until process completes and Command Line shuts automatically",MB_OK,0); LPWSTR lptr2=param2.GetBuffer(l); param2.ReleaseBuffer(); if(CreateProcess(L"C:\\windows\\system32\\cmd.exe", // Application name lptr2, // Application arguments 0, 0, FALSE, CREATE_DEFAULT_ERROR_MODE, 0, 0, // Working directory &siStartupInfo2, &piProcessInfo2) == FALSE) { AfxMessageBox(_T("Error")); // Could not start application -> call 'GetLastError()' } /*nmax= (nrows+rows); create_progrcntl(L"Processing please wait"); pgr->m_pgr.SetRange32(1,nmax); pgr->m_pgr.SetStep(1); for(int i=0;i<nmax;i++) pgr->m_pgr.StepIt();*/ WaitForSingleObject( piProcessInfo2.hProcess, INFINITE ); CloseHandle (piProcessInfo2.hProcess); // MessageBox(L"second process closed handle"); /*Calling a function which will multilook above slc image & converts its byte image*/ nmax= rows; create_progrcntl(L"Processing please wait"); pgr->m_pgr.SetRange32(1,nmax); pgr->m_pgr.SetStep(1); Multilook_SlcFile(rows,cols,ncols,bytes_per_pixel,outslave,mlook_slave,pgr); TerminateProcess(piProcessInfo.hProcess,0); if(pgr!=NULL) { delete pgr; pgr=NULL; } AfxMessageBox(L"Importing Completed",MB_OK|MB_ICONINFORMATION,0); GetDlgItem(IDC_BTN_PROCESS)->EnableWindow(1); Display_ByteImage(mlook_master,mlook_slave); flag=true; } catch ( CException * pEx) { pEx->ReportError(); } } /////////////////////////////////////////////////////////////////////////////// // NAME:DoByteImage // DESCRIPTION:COnverts Multilooked slc image to byte image & writes to outfilename the formal parameter // PARAMETERS:1.char*-Name of output byte image file name //2.float**-A float 2D array of rr*cc size containing multilooked slc image //3.int rr-INteger .rows of multilooked image //4.int cc-integer.cols of multilooked image // RETURN: none // SIDE EFFECTS:none //////////////////////////////////////////////////////////////////////////////// void import::DoByteImage(char* outfilename,float **fin_data,int rr,int cc) { FILE *out; out=fopen(outfilename,"wb"); long int i=1,j; //double sum2=0.0,sum3=0.0,sq3=0.0,sd3=0.0; float *f1,min1,max1,**bmp,xx;//max1=-100.0,min1=100.0,max2=-100.0,min2=100.0,max3=-100.0,min3=100.0; char *c1; int rows=rr,cols=cc,scale1; f1=mem.init_1Dfloat(cols); bmp=mem.init_2Dfloat(rows,cols); c1=(char *)malloc(sizeof(float)*cols); int r=0; for(j=0;j<rows;j++) { f1=fin_data[r]; r++; for(i=0;i<cols;i++) { bmp[j][i]=f1[i]; if(bmp[j][i]<eps) bmp[j][i]=float(eps); bmp[j][i]=float(10.*log10(bmp[j][i])); } } min1=float(INIT_MINMAX); max1=-min1; MinMaxContrastMedianBMP(bmp,&min1,&max1,rows,cols); r=0; for(j=0;j<rows;j++) { f1=fin_data[r]; r++; for(i=0;i<cols;i++) { xx=f1[i]; if (xx <= eps) xx = float(eps); xx = 10 * log10(xx); if (xx > max1) xx = max1; if (xx < min1) xx = min1; xx = (xx - min1) / (max1 - min1); if (xx > 1.) xx = 1.; if (xx < 0.) xx = 0.; scale1=(int) (floor(255 * xx)); c1[i]=(char)scale1; } fwrite(c1,sizeof(char),cols,out); } mem.del_float(f1); mem.del_float(bmp,rows); free(c1); c1 = NULL; } /////////////////////////////////////////////////////////////////////////////// // NAME:MinMaxContrastMedianBMP // DESCRIPTION: // PARAMETERS: // RETURN: // SIDE EFFECTS: ///////////////////////////////////////////////////////////////////////////////// void import::MinMaxContrastMedianBMP(float **mat,float *min,float *max,int nlig,int ncol) { float *tableau,*mattab,maxmax, minmin,median, median0,logeps; int ii,lig,col,npts,np, validnpts; tableau = vector_float(nlig*ncol); mattab = vector_float(nlig*ncol); validnpts = 0; logeps = float(10. *log10(eps)); for(lig=0;lig<nlig;lig++) for(col=0;col<ncol;col++) { if (mat[lig][col] != logeps) { mattab[validnpts] = mat[lig][col]; validnpts++; } } *min =float(INIT_MINMAX); *max = -*min; minmin =float(INIT_MINMAX); maxmax = -minmin; for(np=0;np<validnpts;np++) { tableau[np]=mattab[np]; if (mattab[np] < minmin) minmin = mattab[np]; if (mattab[np] > maxmax) maxmax = mattab[np]; } median0 = MedianArray(tableau, validnpts); /*Recherche Valeur Min*/ median = median0; *min = median0; for (ii=0; ii<3; ii++) { npts=-1; for(np=0;np<validnpts;np++) if (median0 == minmin) { if (mattab[np] <= median) { npts++; tableau[npts]=mattab[np]; } } else { if (mattab[np] < median) { npts++; tableau[npts]=mattab[np]; } } median = MedianArray(tableau, npts); if (median == minmin) median = *min; *min = median; } /*Recherche Valeur Max*/ median = median0; *max = median0; for (ii=0; ii<3; ii++) { npts=-1; for(np=0;np<validnpts;np++) if (median0 == maxmax) { if (mattab[np] >= median) { npts++; tableau[npts]=mattab[np]; } } else { if (mattab[np] > median) { npts++; tableau[npts]=mattab[np]; } } median = MedianArray(tableau, npts); if (median == maxmax) median = *max; *max = median; } //free_vector_float(tableau); //free_vector_float(mattab); mem.del_float(tableau); mem.del_float(mattab); } float* import::vector_float(int nrh) { int ii; float *m; m=mem.init_1Dfloat(nrh+1); for (ii = 0; ii < nrh; ii++) m[ii] = 0; return m; } /////////////////////////////////////////////////////////////////////////////// // NAME:MedianArray // DESCRIPTION: // PARAMETERS: // RETURN: // SIDE EFFECTS: ///////////////////////////////////////////////////////////////////////////////// float import::MedianArray(float arr[], int n) { int low, high ; int median; int middle, ll, hh; low = 0 ; high = n-1 ; median = (low + high) / 2; for (;;) { if (high <= low) /* One element only */ return arr[median] ; if (high == low + 1) { /* Two elements only */ if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]) ; return arr[median] ; } /* Find median of low, middle and high items; swap into position low */ middle = (low + high) / 2; if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]) ; if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]) ; if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ; /* Swap low item (now in position middle) into position (low+1) */ ELEM_SWAP(arr[middle], arr[low+1]) ; /* Nibble from each end towards middle, swapping items when stuck */ ll = low + 1; hh = high; for (;;) { do ll++; while (arr[low] > arr[ll]) ; do hh--; while (arr[hh] > arr[low]) ; if (hh < ll) break; ELEM_SWAP(arr[ll], arr[hh]) ; } /* Swap middle item (in position low) back into correct position */ ELEM_SWAP(arr[low], arr[hh]) ; /* Re-set active partition */ if (hh <= median) low = ll; if (hh >= median) high = hh - 1; } } /////////////////////////////////////////////////////////////////////////////// // NAME:create_header // DESCRIPTION: // PARAMETERS: // RETURN: // SIDE EFFECTS: ///////////////////////////////////////////////////////////////////////////////// void import::Create_Header(char *name,int rows,int col,int datatype,int bands,char *in_type) { FILE *fp; char *hdr_file; CString hdr; hdr=name; int n=hdr.Find(L".",0); hdr=hdr.Mid(0,n); hdr+=".hdr"; hdr_file=Pfun.stringtochar(hdr); fp=fopen(hdr_file,"w"); fprintf(fp,"ENVI \n"); fprintf(fp,"samples = %d\n",col); fprintf(fp,"lines = %d\n",rows); fprintf(fp,"bands = %d\n",bands); fprintf(fp,"header offset = %d\n",0); fprintf(fp,"data type = %d\n",datatype); fprintf(fp,"interleave = %s\n",in_type); fclose(fp); } /////////////////////////////////////////////////////////////////////////////// // NAME:Multilook_SlcFile // DESCRIPTION:It takes slc file as an input & produces multilooked byte image file along with header file // PARAMETERS:1rows-multilooked rows i.e;original rowws/azimuth value //2.cols-multilooked cols i.e;original cols/range value 3.ncols-no of cols in original image //4.bytes_per_pixel-no of bytes per pixel //5.input_slcfile-input slc file //6.output_bytefile-name of output byte image file name // RETURN:none // SIDE EFFECTS:none ///////////////////////////////////////////////////////////////////////////////// void import::Multilook_SlcFile(int rows,int cols,int ncols,short bytes_per_pixel,char* input_slcfile,char* output_bytefile,CPgr* pgr) { float *sum1=NULL, *sum=NULL,*power1=NULL,**fin_data=NULL,*data_float=NULL; float re,img; int jj,j,k; char *buffer; short *data=NULL; FILE *in; sum1=mem.init_1Dfloat(cols); sum=mem.init_1Dfloat(ncols); power1=mem.init_1Dfloat(ncols); buffer=mem.init_1Dchar(ncols*bytes_per_pixel); fin_data=mem.init_2Dfloat(rows,cols); ////////////////////////// //if(SelectedProcess==3) data_float=mem.init_1Dfloat(ncols*2); //else data=mem.init_1Dshort(ncols*2); in=fopen(input_slcfile,"rb"); for(int i=0;i<rows;i++) { for (jj=0;jj<ncols; jj++) sum[jj]=float(0.0); for (jj=0;jj<cols; jj++) sum1[jj]=float(0.0); //////////////////////////////////////////// if(SelectedProcess==3) { for(jj=0; jj<azimuth; jj++) { fread(buffer,sizeof(char),ncols*bytes_per_pixel,in); data_float=(float *) buffer; for (k=0,j=0; k<ncols*2, j<ncols; k+=2,j++) { re = float(data_float[k]); img=float(data_float[k+1]); power1[j] =float(sqrt(re*re+img*img)); sum[j]=float(sum[j]+power1[j]); } } } else { for(jj=0; jj<azimuth; jj++) { fread(buffer,sizeof(char),ncols*bytes_per_pixel,in); data=((short *)( buffer)); for (k=0,j=0; k<ncols*2, j<ncols; k+=2,j++) { re = float(data[k]); img=float(data[k+1]); power1[j] =float(sqrt(re*re+img*img)); sum[j]=float(sum[j]+power1[j]); } } } /////////////////////////////////////////// for(k=0;k<cols;k++) { for(j=0;j<range; j++) sum1[k]=float(sum1[k]+sum[(k*range)+j]); } for(jj=0; jj<cols; jj++) { sum1[jj]=float(sum1[jj]/(azimuth*range)); } for(jj=0;jj<cols;jj++) fin_data[i][jj]=float(sum1[jj]); // pgr->m_pgr.StepIt(); global::process_events(); } fclose(in); DoByteImage(output_bytefile,fin_data,rows,cols); ; Create_Header(output_bytefile,rows,cols,sizeof(char),BANDS,INTERLEAVE_TYPE); TRACE("Header created"); ////////////////////////// // This function causes memory corruption //////////////////////////// delete[] buffer; buffer=NULL; //if(SelectedProcess==3) //{ // delete[] data_float; // data_float=NULL; //} //else{ // delete [] data; // data=NULL; //} delete [] power1; power1=NULL; delete[] sum; sum=NULL; delete [] sum1; sum1=NULL; for (int i = 0; i < rows-1; ++i) { delete[] fin_data[i]; } delete [] fin_data; fin_data=NULL; } void import::Multilook_SlcFile(int rows,int cols,int ncols,short bytes_per_pixel,char* input_slcfile,char* output_bytefile) { float *sum1=NULL, *sum=NULL,*power1=NULL,**fin_data=NULL,*data_float=NULL; float re,img; int jj,j,k; char *buffer; short *data=NULL; FILE *in; sum1=mem.init_1Dfloat(cols); sum=mem.init_1Dfloat(ncols); power1=mem.init_1Dfloat(ncols); buffer=mem.init_1Dchar(ncols*bytes_per_pixel); fin_data=mem.init_2Dfloat(rows,cols); ////////////////////////// //if(SelectedProcess==3) data_float=mem.init_1Dfloat(ncols*2); //else data=mem.init_1Dshort(ncols*2); in=fopen(input_slcfile,"rb"); for(int i=0;i<rows;i++) { for (jj=0;jj<ncols; jj++) sum[jj]=float(0.0); for (jj=0;jj<cols; jj++) sum1[jj]=float(0.0); //////////////////////////////////////////// if(SelectedProcess==3) { for(jj=0; jj<azimuth; jj++) { fread(buffer,sizeof(char),ncols*bytes_per_pixel,in); data_float=(float *) buffer; for (k=0,j=0; k<ncols*2, j<ncols; k+=2,j++) { re = float(data_float[k]); img=float(data_float[k+1]); power1[j] =float(sqrt(re*re+img*img)); sum[j]=float(sum[j]+power1[j]); } } } else { for(jj=0; jj<azimuth; jj++) { fread(buffer,sizeof(char),ncols*bytes_per_pixel,in); data=((short *)( buffer)); for (k=0,j=0; k<ncols*2, j<ncols; k+=2,j++) { re = float(data[k]); img=float(data[k+1]); power1[j] =float(sqrt(re*re+img*img)); sum[j]=float(sum[j]+power1[j]); } } } /////////////////////////////////////////// for(k=0;k<cols;k++) { for(j=0;j<range; j++) sum1[k]=float(sum1[k]+sum[(k*range)+j]); } for(jj=0; jj<cols; jj++) { sum1[jj]=float(sum1[jj]/(azimuth*range)); } for(jj=0;jj<cols;jj++) fin_data[i][jj]=float(sum1[jj]); // pgr.m_pgr.StepIt(); global::process_events(); } fclose(in); DoByteImage(output_bytefile,fin_data,rows,cols); ; Create_Header(output_bytefile,rows,cols,sizeof(char),BANDS,INTERLEAVE_TYPE); TRACE("Header created"); ////////////////////////// // This function causes memory corruption //////////////////////////// delete[] buffer; buffer=NULL; //if(SelectedProcess==3) //{ // delete[] data_float; // data_float=NULL; //} //else{ // delete [] data; // data=NULL; //} delete [] power1; power1=NULL; delete[] sum; sum=NULL; delete [] sum1; sum1=NULL; for (int i = 0; i < rows-1; ++i) { delete[] fin_data[i]; } delete [] fin_data; fin_data=NULL; } /////////////////////////////////////////////////////////////////////////////// // NAME:Display_ByteImage // DESCRIPTION:It displays byte image file using openev. // PARAMETERS:1.char* master-Name of master byte image file name //2.char* slave-Name of slave byte image file name // RETURN:BOOL value true indicating success otherwise false // SIDE EFFECTS:none ///////////////////////////////////////////////////////////////////////////////// bool import::Display_ByteImage(char* master,char* slave) { CString mfile,sfile; mfile=master; sfile=slave; //Shell extensions (data sources, context menu handlers, verb implementations) that are activated using Component Object Model (COM), //COM should be initialized before ShellExecuteEx is called. Some Shell extensions require the COM single-threaded apartment (STA) type. //In that case, COM should be initialized as shown here: //CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE) HANDLE hprocess_master,hprocess_slave; SHELLEXECUTEINFO shellmaster,shellslave; //Use this function instead of ZeroMemory when you want to ensure that your data will be //overwritten promptly, as some C++ compilers can optimize a call to ZeroMemory by removing it entirely. SecureZeroMemory(&shellmaster,sizeof(shellmaster)); shellmaster.cbSize=sizeof(shellmaster); shellmaster.fMask=SEE_MASK_NOCLOSEPROCESS|SEE_MASK_FLAG_NO_UI; shellmaster.lpVerb=L"open"; shellmaster.lpFile=L"openev.bat"; shellmaster.lpParameters=mfile; //shellmaster.lpDirectory=L"c:\\Program Files (x86)\\Atlantis\\OpenEV 1.80"; shellmaster.lpDirectory= global::openEVpath; shellmaster.nShow=SW_SHOW; if(ShellExecuteEx(&shellmaster)) {hprocess_master=shellmaster.hProcess;} else {Handle_Failed_Display(shellmaster.hInstApp); return false;} SecureZeroMemory(&shellslave,sizeof(shellslave)); shellslave.cbSize=sizeof(shellslave); shellslave.fMask=SEE_MASK_NOCLOSEPROCESS|SEE_MASK_FLAG_NO_UI; shellslave.lpVerb=L"open"; shellslave.lpFile=L"openev.bat"; shellslave.lpParameters=sfile; // shellslave.lpDirectory=L"c:\\Program Files (x86)\\Atlantis\\OpenEV 1.80"; shellslave.lpDirectory= global::openEVpath; shellslave.nShow=SW_SHOW; if(ShellExecuteEx(&shellslave)) {hprocess_slave=shellslave.hProcess;} else {Handle_Failed_Display(shellslave.hInstApp); return false;} return true; } /////////////////////////////////////////////////////////////////////////////// // NAME:Handle_Failed_Display // DESCRIPTION:If Display_byteimage fails to display image.It diagnoses actual cause & gives information to user. // PARAMETERS:HINSTANCE value which is <32 because Display_byteimage fails. // RETURN:none // SIDE EFFECTS:none ///////////////////////////////////////////////////////////////////////////////// void import::Handle_Failed_Display(HINSTANCE hinst) { // Could not start application switch(reinterpret_cast<int>(hinst)) { case 0: // The operating system is out of memory or resources. AfxMessageBox(L"Out of memory or resources",MB_OK|MB_ICONERROR,0); break; case ERROR_FILE_NOT_FOUND: AfxMessageBox(L"File not found",MB_OK|MB_ICONERROR,0); break; case ERROR_PATH_NOT_FOUND: AfxMessageBox(L"Path not found",MB_OK|MB_ICONERROR,0); break; case ERROR_BAD_FORMAT: // The .exe file is invalid (non-Microsoft Win32 .exe or error in .exe image). AfxMessageBox(L"OpenEV is not installed.File is invalid",MB_OK|MB_ICONERROR,0); break; case SE_ERR_ACCESSDENIED: // The operating system denied access to the specified file. AfxMessageBox(L"Access is denied to file",MB_OK|MB_ICONERROR,0); break; case SE_ERR_ASSOCINCOMPLETE: // The file name association is incomplete or invalid. AfxMessageBox(L"The file name association is incomplete or invalid",MB_OK|MB_ICONERROR,0); break; case SE_ERR_DDEBUSY: // The Dynamic Data Exchange (DDE) transaction could not be completed because // other DDE transactions were being processed. AfxMessageBox(L"Transactions are being processed",MB_OK|MB_ICONERROR,0); break; case SE_ERR_DDEFAIL: // The DDE transaction failed. AfxMessageBox(L"DDE transaction faled",MB_OK|MB_ICONERROR,0); break; case SE_ERR_DDETIMEOUT: // The DDE transaction could not be completed because the request timed out. AfxMessageBox(L"Time out",MB_OK|MB_ICONERROR,0); break; case SE_ERR_DLLNOTFOUND: // The specified dynamic-link library (DLL) was not found. AfxMessageBox(L"Specific dll was not found",MB_OK|MB_ICONERROR,0); break; case SE_ERR_NOASSOC: // There is no application associated with the given file name extension. // This error will also be returned if you attempt to print a file that is // not printable. AfxMessageBox(L"No application associated with the given file name extension",MB_OK|MB_ICONERROR,0); break; case SE_ERR_OOM: // There was not enough memory to complete the operation. AfxMessageBox(L"Not enough memory to complete the operation",MB_OK|MB_ICONERROR,0); break; case SE_ERR_SHARE: // A sharing violation occurred. AfxMessageBox(L"Sharing violation ocurred",MB_OK|MB_ICONERROR,0); break; } } /////////////////////////////////////////////////////////////////////////////// // NAME:Select_process_func // DESCRIPTION:Select appropriate process to execute depending on value of SelectedProcess // PARAMETERS:none // RETURN:none // SIDE EFFECTS:none ///////////////////////////////////////////////////////////////////////////////// void import::Select_process_func() { switch(SelectedProcess) { case 1: processERS_MasterSlave(); break; case 2: processTERRA_MasterSlave(); break; case 3: processALOS_MasterSlave(); break; case 4: processRSAT2_MasterSlave(); break; case 5: processENSAT_MasterSlave(); break; case 6: processCOSMO_MasterSlave(); break; } } UINT Begin_Import(LPVOID pParam) { CWnd* pWnd = CWnd::FromHandle ((HWND) pParam); import* iptrdb = (import*)pWnd; TRACE("In function Begin_import thread started\n"); TRACE("In function Begin_import value of selected process=%d\n",iptrdb->SelectedProcess); iptrdb->processERS_MasterSlave(); /*switch(iptrdb->SelectedProcess) { case 1: iptrdb->processERS_MasterSlave(); break; case 2: iptrdb->processTERRA_MasterSlave(); break; case 3: iptrdb->processALOS_MasterSlave(); break; case 4: iptrdb->processRSAT2_MasterSlave(); break; case 5: iptrdb->processENSAT_MasterSlave(); break; case 6: break; }*/ return 0; } //void import::OnBnClickedShimg() //{ /*int nScreenWidth = GetSystemMetrics(SM_CXSCREEN); int nScreenHeight = GetSystemMetrics(SM_CYSCREEN); int maxwidth=GetSystemMetrics(SM_CXMAXTRACK); int minwidth=GetSystemMetrics(SM_CXMIN); int maxht=GetSystemMetrics(SM_CYMAXTRACK); int minht=GetSystemMetrics(SM_CYMIN); TRACE("width=%d\n",nScreenWidth); TRACE("height=%d\n",nScreenWidth); TRACE("maxwidth=%d\n",maxwidth); TRACE("minwidth=%d\n",minwidth); TRACE("maxht=%d\n",maxht); TRACE("minht=%d\n",minht);*/ //} /// // PROCESSENTRY32 ppe = {0}; //ppe.dwSize = sizeof (PROCESSENTRY32); // //HANDLE hSnapShot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0); //if (Process32First (hSnapShot, &ppe)) //{ //do //{ //if (!stricmp ("d:\\programs\\final_g\\Sar_CCD.exe",(const char*) ppe.szExeFile) && (ppe.th32ProcessID != GetCurrentProcessId ())) //{ //HANDLE hMySecondProcess = OpenProcess (PROCESS_TERMINATE, FALSE, ppe.th32ProcessID); //if (hMySecondProcess) //{ //TerminateProcess (hMySecondProcess, 0); //CloseHandle (hMySecondProcess); //} //} //}while (Process32Next (hSnapShot, &ppe)); //} //CloseHandle (hSnapShot); //} void import::OnDestroy() { CDialog::OnDestroy(); GetParent()->SendMessage(UWM_MODELESS_CLOSED,0,(LPARAM)"import"); // TODO: Add your message handler code here } void import::PostNcDestroy() { // TODO: Add your specialized code here and/or call the base class delete this; CDialog::PostNcDestroy(); } void import::OnBnClickedNxtImport() { // TODO: Add your control notification handler code here CString result = m_txtoutdir; result += L"\\resultfile.txt"; ShellExecute( NULL, _T("open"), _T("c:\\windows\\notepad.exe"), result , _T(""), SW_SHOWNORMAL) ; } void import::OnCancel() { // TODO: Add your specialized code here and/or call the base class CDialog::OnCancel(); DestroyWindow(); }