• Hlavní stránka
  • Třídy
  • Soubory
  • Seznam souborů
  • Symboly v souborech

cppTypes.h

00001 /*
00002  * cppTypes.h - pro paralelní výpočet dopřené a zpětné expanzivní DTWT po segmentech, separabilním přístupem algoritmem segDTWT2D
00003  * Autor: Zdeněk Průša, UTKO, FEKT, VUT v Brně, Purkyňova 118, Brno
00004  * Program vznikl za podpory interního grantu FEKT-J-10-8 - Optimální algoritmy paralelního výpočtu waveletové transformace rozsáhlých obrazových dat
00005  * Externí knihovny: Intel Threading Building Blocks 3.0
00006  */
00007 
00008 
00009 #ifndef CppTypes_H
00010 #define CppTypes_H
00011 
00012 #include <fstream>
00013 #include <iostream>
00014 #include <sstream>
00015 #include <string>
00016 #include <malloc.h>
00017 #include <algorithm>
00018 using namespace std;
00019 
00020 
00021 
00022 class Exception : public exception
00023 {
00024 public:
00025 Exception(string m="nastala_chyba") : msg(m) {}
00026 ~Exception() throw() {}
00027 const char* what() const throw() { return msg.c_str(); }
00028 
00029 private:
00030 string msg;
00031 };
00032 
00039 class separableWavelet{
00040 private:
00041         // analyzující dolní propust
00042         float* lo_A;
00043         // analyzující horní propust
00044         float* hi_A;
00045         // délka dolní propusti
00046         int length_lo_A;
00047         // délka horní propusti
00048         int length_hi_A;
00049 
00050         // syntetizující dolní propust
00051         float* lo_R;
00052         // syntetizující horní propust
00053         float* hi_R;
00054         // délka dolní propusti
00055         int length_lo_R;
00056         // délka horní propusti
00057         int length_hi_R;
00058 
00059         // název waveletu, bude pod tímto názvem ukládán
00060 string name;
00061 
00062         // otočené verze filtrů
00063         float* lo_A_reversed;
00064         float* hi_A_reversed;
00065         float* lo_R_reversed;
00066         float* hi_R_reversed;
00067 public:
00068 
00114 int load(string name, const char* file = "wavelets.dat"){
00115                 std::ifstream ifs( file,ifstream::in );
00116                 
00117 
00118                 if(!ifs.is_open()){ throw Exception("File not found or is damaged.");  } 
00119                 string line;
00120         
00121                 while(ifs.good()){
00122                 std::getline(ifs,line);
00123                 size_t found = line.find(name);
00124                 if(found!=string::npos){
00125                         string temp;
00126                         std::cout<<"Wavelet "<<name <<" found!! Loading...";
00127 
00128                         
00129                         if(lo_A != NULL && hi_A != NULL && hi_R != NULL && lo_R != NULL) clear(); 
00130 
00131 
00132                         temp = line.erase(0,line.find('=')+1);
00133                          remove(temp.begin(),temp.end(),' ');
00134                 
00135                         // this->name = temp;
00136 
00137                          
00138 
00139                          // délka 1. filtru
00140                          std::getline(ifs,line);
00141                          temp = line.erase(0,line.find('=')+1);
00142                         stringstream*  ss = new stringstream(temp);     
00143                         if((*ss >> this->length_lo_A).fail()){
00144                         ifs.close();
00145                         delete ss;
00146                         return -2;
00147                         }
00148                         
00149                         // hodnoty 1. filtru
00150                         std::getline(ifs,line);
00151                         temp = line.erase(0,line.find('[')+1);
00152                         temp = temp.erase(temp.find(']'),temp.length());
00153                         delete ss;
00154                         ss = new stringstream(temp,stringstream::in|stringstream::out);
00155                         
00156                         this->lo_A = (float*) _aligned_malloc(length_lo_A*sizeof(float),ALIGN_TO_BYTES);
00157 
00158                         for(int i =0;i<length_lo_A;i++)
00159                                 *ss >> *(lo_A+i) ;
00160 
00161 
00162                          // délka 2. filtru
00163                          std::getline(ifs,line);
00164                          temp = line.erase(0,line.find('=')+1);
00165                          delete ss;
00166                         ss = new stringstream(temp);    
00167                         if((*ss >> this->length_hi_A).fail()){
00168                         ifs.close();
00169                         return -2;
00170                         }
00171                         
00172                         // hodnoty 2. filtru
00173                         std::getline(ifs,line);
00174                         temp = line.erase(0,line.find('[')+1);
00175                         temp = temp.erase(temp.find(']'),temp.length());
00176                         delete ss;
00177                         ss = new stringstream(temp,stringstream::in|stringstream::out);
00178                         
00179                         this->hi_A = (float*) _aligned_malloc(length_hi_A*sizeof(float),ALIGN_TO_BYTES);
00180 
00181                         for(int i =0;i<length_hi_A;i++)
00182                                 *ss >> *(hi_A+i) ;
00183 
00184                          // délka 3. filtru
00185                          std::getline(ifs,line);
00186                          temp = line.erase(0,line.find('=')+1);
00187                          delete ss;
00188                          ss = new stringstream(temp);   
00189                         if((*ss >> this->length_lo_R).fail()){
00190                         ifs.close();
00191                         return -2;
00192                         }
00193                         
00194                         // hodnoty 3. filtru
00195                         std::getline(ifs,line);
00196                         temp = line.erase(0,line.find('[')+1);
00197                         temp = temp.erase(temp.find(']'),temp.length());
00198                         delete ss;
00199                         ss = new stringstream(temp,stringstream::in|stringstream::out);
00200                         
00201                         this->lo_R = (float*) _aligned_malloc(length_lo_R*sizeof(float),ALIGN_TO_BYTES);
00202 
00203                         for(int i =0;i<length_lo_R;i++)
00204                                 *ss >> *(lo_R+i) ;
00205 
00206                         // délka 4. filtru
00207                          std::getline(ifs,line);
00208                          temp = line.erase(0,line.find('=')+1);
00209                          delete ss;
00210                         ss = new stringstream(temp);    
00211                         if((*ss >> this->length_hi_R).fail()){
00212                         ifs.close();
00213                         return -2;
00214                         }
00215                         
00216                         // hodnoty 4. filtru
00217                         std::getline(ifs,line);
00218                         temp = line.erase(0,line.find('[')+1);
00219                         temp = temp.erase(temp.find(']'),temp.length());
00220                         delete ss;
00221                         ss = new stringstream(temp,stringstream::in|stringstream::out);
00222                         
00223                         this->hi_R = (float*) _aligned_malloc(length_hi_R*sizeof(float),ALIGN_TO_BYTES);
00224 
00225                         for(int i =0;i<length_hi_R;i++)
00226                                 *ss >> *(hi_R+i) ;
00227                         
00228 
00229                         std::cout<<"Loaded."<<std::endl;
00230                         delete ss;
00231                         ifs.close();
00232 
00233                         reverseCopy();
00234                         return 0;
00235                 }
00236                 
00237                 }
00238 
00239                 ifs.close();
00240 
00241                 std::cout<<"Wavelet "<< name <<" not found."<<std::endl; 
00242                 throw Exception("Wavelet not found!");
00243                 return -1;
00244         }
00245 
00246 
00264 int save(const char* file = "wavelets.dat") const{
00265         
00266         std::ifstream ifs( file,ifstream::in );
00267                 string line;
00268 
00269                 while(ifs.good()){
00270                 std::getline(ifs,line);
00271                 size_t found = line.find(name);
00272                 if(found!=string::npos){
00273                         std::cout<<"This wavelet is already in file"<<std::endl;
00274                         ifs.close();
00275                         return -2;
00276                 }
00277                 //std::cout<<line<<std::endl;
00278                 }
00279                 ifs.close();
00280 
00281                 std::ofstream ofs(file,std::ios_base::app);
00282                 if(!ofs.is_open()) return -1;
00283 
00284 
00285                 ofs.seekp(0,std::ios_base::beg);
00286         
00287                 ofs << "\n";
00288                 ofs << "Name = ";
00289                 ofs.write(name.data(),name.length());
00290                 ofs << "\n";
00291 
00292                 ofs << "length(LO_A) = " << length_lo_A << "\n";
00293 
00294                 ofs << "LO_A = [ ";
00295                 ofs.setf(0,ios::floatfield);
00296                 ofs.precision(10);
00297                 for(int i=0;i<length_lo_A;i++)
00298                         ofs << lo_A[i] << " "; 
00299 
00300                 ofs << "]; ";
00301                 ofs << "\n";
00302 
00303                 ofs << "length(HI_A) = " << length_hi_A<< "\n";
00304 
00305                 ofs << "HI_A = [ ";
00306                 ofs.setf(0,ios::floatfield);
00307                 ofs.precision(10);
00308                 for(int i=0;i<length_hi_A;i++)
00309                         ofs << hi_A[i] << " "; 
00310 
00311                 ofs << "]; ";
00312                 ofs << "\n";
00313 
00314                 ofs << "length(LO_R) = " << length_lo_R<< "\n";
00315 
00316                 ofs << "LO_R = [ ";
00317                 ofs.setf(0,ios::floatfield);
00318                 ofs.precision(10);
00319                 for(int i=0;i<length_lo_R;i++)
00320                         ofs << lo_R[i] << " "; 
00321 
00322                 ofs << "]; ";
00323                 ofs << "\n";
00324 
00325                 ofs << "length(HI_R) = " << length_hi_R<< "\n";
00326 
00327                 ofs << "HI_R = [ ";
00328                 ofs.setf(0,ios::floatfield);
00329                 ofs.precision(10);
00330                 for(int i=0;i<length_hi_R;i++)
00331                         ofs << hi_R[i] << " "; 
00332 
00333                 ofs << "]; ";
00334                 ofs << "\n";
00335 
00336                 
00337 
00338 
00339                 ofs.flush();
00340                 ofs.close();
00341                 return 0;
00342         }
00343 
00352 void print() const{
00353                 std::cout << "Name = "; 
00354                 std::cout.write(name.data(),name.length()-1);
00355                         
00356                 std::cout << "\n";
00357 
00358 
00359                 std::cout << "length(LO_A) = " << length_lo_A << "\n";
00360 
00361                 std::cout << "LO_A = [ ";
00362                 std::cout.setf(0,ios::floatfield);
00363                 std::cout.precision(10);
00364                 for(int i=0;i<length_lo_A;i++)
00365                         std::cout << lo_A[i] << " "; 
00366 
00367                 std::cout << "]; ";
00368                 std::cout << "\n";
00369 
00370                 std::cout << "length(HI_A) = " << length_hi_A<< "\n";
00371 
00372                 std::cout << "HI_A = [ ";
00373                 std::cout.setf(0,ios::floatfield);
00374                 std::cout.precision(10);
00375                 for(int i=0;i<length_hi_A;i++)
00376                         std::cout << hi_A[i] << " "; 
00377 
00378                 std::cout << "]; ";
00379                 std::cout << "\n";
00380 
00381                 std::cout << "length(LO_R) = " << length_lo_R<< "\n";
00382 
00383                 std::cout << "LO_R = [ ";
00384                 std::cout.setf(0,ios::floatfield);
00385                 std::cout.precision(10);
00386                 for(int i=0;i<length_lo_R;i++)
00387                         std::cout << lo_R[i] << " "; 
00388 
00389                 std::cout << "]; ";
00390                 std::cout << "\n";
00391 
00392                 std::cout << "length(HI_R) = " << length_hi_R<< "\n";
00393 
00394                 std::cout << "HI_R = [ ";
00395                 std::cout.setf(0,ios::floatfield);
00396                 std::cout.precision(10);
00397                 for(int i=0;i<length_hi_R;i++)
00398                         std::cout << hi_R[i] << " "; 
00399 
00400                 std::cout << "]; \n";
00401         
00402                 
00403         }
00404 
00405 
00415 void clear(){
00416                 _aligned_free(lo_A);
00417                 _aligned_free(hi_A);
00418                 _aligned_free(lo_R);
00419                 _aligned_free(hi_R);
00420                 _aligned_free(lo_A_reversed);
00421                 _aligned_free(hi_A_reversed);
00422                 _aligned_free(lo_R_reversed);
00423                 _aligned_free(hi_R_reversed);
00424         }
00425 
00426 
00427 
00428         //separableWavelet():length_lo_A(0),length_hi_A(0),length_lo_R(0),length_hi_R(0),lo_A(NULL),hi_A(NULL),lo_R(NULL),hi_R(NULL),name(""){}
00429 
00440 separableWavelet(string name="default",const char* file = "wavelets.dat") :length_lo_A(0),length_hi_A(0),length_lo_R(0),length_hi_R(0),lo_A(NULL),hi_A(NULL),lo_R(NULL),hi_R(NULL),name(name) {
00441         try{
00442                 int i =load(name,file);
00443                 if(i<0) throw;
00444         }catch(...){
00445       std::cout << "File " << file << " was not found or is damaged." << std::endl; 
00446          throw;
00447         }
00448         }
00449 
00464         separableWavelet(float* lo_A,float* hi_A,int length_lo_A,int length_hi_A){
00465                 initialize(lo_A,hi_A,length_lo_A);
00466                 this->length_hi_A = length_hi_A;
00467         }
00480         separableWavelet(float* lo_A,float* hi_A,int length_lo_A){
00481                 initialize(lo_A,hi_A,length_lo_A);
00482         }
00497         separableWavelet(float* lo_A,float* hi_A,int length_lo_A,string name){
00498                 initialize(lo_A,hi_A,length_lo_A);
00499                 this->name = name;
00500         }
00517         separableWavelet(float* lo_A,float* hi_A,int length_lo_A,int length_hi_A,string name){
00518                 initialize(lo_A,hi_A,length_lo_A);
00519                 this->length_hi_A = length_hi_A;
00520                 this->name = name;
00521         }
00522 
00523 
00533         separableWavelet(const separableWavelet& old){
00534         this->name = old.name;
00535         this->length_lo_A = length_lo_A;
00536         this->length_hi_A = length_hi_A;
00537         this->length_hi_R = length_hi_R;
00538         this->length_lo_R = length_lo_R;
00539 
00540 
00541         this->lo_A = (float*) _aligned_malloc(length_lo_A*sizeof(float),ALIGN_TO_BYTES);
00542         std::copy(old.lo_A,old.lo_A+length_lo_A,this->lo_A);
00543 
00544         this->hi_A = (float*) _aligned_malloc(length_hi_A*sizeof(float),ALIGN_TO_BYTES);
00545         std::copy(old.hi_A,old.hi_A+length_hi_A,this->hi_A);
00546 
00547         this->lo_R = (float*) _aligned_malloc(length_lo_R*sizeof(float),ALIGN_TO_BYTES);
00548         std::copy(old.lo_R,old.lo_R+length_lo_R,this->lo_R);
00549 
00550         this->hi_R = (float*) _aligned_malloc(length_hi_R*sizeof(float),ALIGN_TO_BYTES);
00551         std::copy(old.hi_R,old.hi_R+length_hi_R,this->hi_R);
00552         }
00553 
00554         
00555         int getMaxFilterLengthRows() const{
00556                 return max(length_lo_A,length_hi_A);
00557         }
00558 
00559         int getMaxFilterLengthCols() const{
00560                 return max(length_lo_A,length_hi_A);
00561         }
00562 
00563         ~separableWavelet(){
00564                 clear();
00565         }
00566 
00567         
00568         int getRightZeros(int no) const {
00569                  int actual = getAnalysingFilterLength(no);
00570                  int maximal = getMaxFilterLengthCols();
00571                  int zeros = (maximal - actual)/2;
00572 
00573                  return (odd(zeros)?zeros-1:zeros);
00574          }
00575 
00576          int getLeftZeros(int no)const {
00577                  int actual = getAnalysingFilterLength(no);
00578                  int maximal = getMaxFilterLengthCols();
00579                  int zeros = (maximal - actual)/2;
00580 
00581                  return (odd(zeros)?zeros+1:zeros);
00582          }
00583 
00584         int getRightZerosSyn(int no) const {
00585                  int actual = getSyntetizingFilterLength(no);
00586                  int maximal = getMaxFilterLengthCols();
00587                  int zeros = (maximal - actual)/2;
00588 
00589                  return (odd(zeros)?zeros-1:zeros);
00590          }
00591 
00592          int getLeftZerosSyn(int no)const {
00593                  int actual = getSyntetizingFilterLength(no);
00594                  int maximal = getMaxFilterLengthCols();
00595                  int zeros = (maximal - actual)/2;
00596 
00597                  return (odd(zeros)?zeros+1:zeros);
00598          }
00599 
00600 
00601          int getTopZeros(int no) const{
00602                  int actual = getAnalysingFilterLength(no);
00603                  int maximal = getMaxFilterLengthRows();
00604                  int zeros = (maximal - actual)/2;
00605 
00606                  return (odd(zeros)?zeros+1:zeros);
00607          }
00608 
00609          int getBottomZeros(int no) const{
00610                 int actual = getAnalysingFilterLength(no);
00611                  int maximal = getMaxFilterLengthRows();
00612                  int zeros = (maximal - actual)/2;
00613 
00614                  return (odd(zeros)?zeros-1:zeros);
00615          }
00616         
00617         float* getAnalysingFilter(int no) const{
00618                 switch(no){
00619                 case 0:
00620                  return lo_A_reversed;
00621                 break;
00622                 case 1:
00623                  return hi_A_reversed;
00624                 break;
00625                 default:
00626                  return 0;
00627                 }
00628         }
00629 
00630         int getAnalysingFilterLength(int no) const{
00631                 switch(no){
00632                 case 0:
00633                         return length_lo_A;
00634                 break;
00635                 case 1:
00636                         return length_hi_A;
00637                 break;
00638                 default:
00639                  return 0;
00640                 }
00641         }
00642 
00643         float* getSyntetizingFilter(int no) const{
00644                 switch(no){
00645                 case 0:
00646                  return lo_R_reversed;
00647                 break;
00648                 case 1:
00649                  return hi_R_reversed;
00650                 break;
00651                 default:
00652                  return 0;
00653                 }
00654         }
00655 
00656         int getSyntetizingFilterLength(int no) const{
00657                 switch(no){
00658                 case 0:
00659                  return length_lo_R;
00660                 break;
00661                 case 1:
00662                  return length_hi_R;
00663                 break;
00664                 default:
00665                  return 0;
00666                 }
00667         }
00668         
00669         
00670         
00671         float* getFilter(int no) const{
00672                 switch(no){
00673                 case 0:
00674                  return lo_A_reversed;
00675                 break;
00676                 case 1:
00677                  return hi_A_reversed;
00678                 break;
00679                 case 2:
00680                  return lo_R_reversed;
00681                 break;
00682                 case 3:
00683                  return hi_R_reversed;
00684                 break;
00685                 default:
00686                  return 0;
00687                 }
00688 
00689         }
00690 
00691         int getFilterLength(int no) const{
00692                 switch(no){
00693                 case 0:
00694                  return length_lo_A;
00695                 break;
00696                 case 1:
00697                  return length_hi_A;
00698                 break;
00699                 case 2:
00700                  return length_lo_R;
00701                 break;
00702                 case 3:
00703                  return length_hi_R;
00704                 break;
00705                 }
00706 return -1;
00707         }
00708 
00709         int getLength() const{
00710          return length_lo_A;
00711         }
00712 
00713 private:
00714 
00715 void initialize(float* lo_A,float* hi_A,int length_lo_A){
00716                 this->name = "default";
00717                 this->length_lo_A = length_lo_A;
00718                 this->length_hi_A = length_lo_A;
00719         
00720                 this->lo_A = (float*) _aligned_malloc(length_lo_A*sizeof(float),ALIGN_TO_BYTES);
00721                 std::copy(lo_A,lo_A+length_lo_A,this->lo_A);
00722 
00723                 this->hi_A = (float*) _aligned_malloc(length_hi_A*sizeof(float),ALIGN_TO_BYTES);
00724                 std::copy(hi_A,hi_A+length_hi_A,this->hi_A);
00725 
00726                 length_hi_R = length_lo_A;
00727                 length_lo_R = length_hi_A;
00728 
00729                 this->lo_R = (float*) _aligned_malloc(length_lo_R*sizeof(float),ALIGN_TO_BYTES);
00730         
00731                 for(int i=0;i<length_lo_R;i++){
00732                         this->lo_R[i] = this->hi_A[i];
00733                         if(i%2==1)
00734                                 this->lo_R[i] = -this->lo_R[i];
00735                 }
00736 
00737                 this->hi_R = (float*) _aligned_malloc(length_hi_R*sizeof(float),ALIGN_TO_BYTES);
00738 
00739                 for(int i=0;i<length_hi_R;i++){
00740                         this->hi_R[i] = this->lo_A[i];
00741                         if(i%2!=1)
00742                                 this->hi_R[i] = -this->hi_R[i];
00743                 }
00744 
00745                 reverseCopy();
00746         }
00747 
00748 void reverseCopy(){
00749         this->lo_A_reversed = (float*) _aligned_malloc(length_lo_A*sizeof(float),ALIGN_TO_BYTES);
00750         this->hi_A_reversed = (float*) _aligned_malloc(length_hi_A*sizeof(float),ALIGN_TO_BYTES);
00751         this->lo_R_reversed = (float*) _aligned_malloc(length_lo_R*sizeof(float),ALIGN_TO_BYTES);
00752         this->hi_R_reversed = (float*) _aligned_malloc(length_hi_R*sizeof(float),ALIGN_TO_BYTES);
00753 
00754         std::reverse_copy(lo_A,lo_A + length_lo_A,lo_A_reversed);
00755         std::reverse_copy(hi_A,hi_A + length_hi_A,hi_A_reversed);
00756         std::reverse_copy(lo_R,lo_R + length_lo_R,lo_R_reversed);
00757         std::reverse_copy(hi_R,hi_R + length_hi_R,hi_R_reversed);
00758         
00759         }
00760 
00761 
00762 
00763 };
00764 
00765 
00766 #endif

Generováno st 24. lis 2010 21.25:14 pro projekt SegDTWT2D programem  doxygen 1.7.1