#ifndef STIM_ENVI_H #define STIM_ENVI_H #include "../envi/envi_header.h" #include "../envi/bsq.h" #include "../envi/bip.h" #include "../envi/bil.h" #include #include "../../CImg/CImg.h" namespace stim{ //container class for an ENVI binary file reader class envi{ void* file; //void pointer to the relevant file reader (bip, bsq, or bil - with appropriate data type) public: envi_header header; bool allocate(){ file = NULL; //set file to a NULL pointer if(header.interleave == envi_header::BSQ){ if(header.data_type ==envi_header::float32) return(file = new bsq()); else if(header.data_type == envi_header::float64) return(file = new bsq()); } else if(header.interleave == envi_header::BIP){ if(header.data_type ==envi_header::float32) return(file = new bip()); else if(header.data_type == envi_header::float64) return(file = new bip()); } else if(header.interleave == envi_header::BIL){ if(header.data_type ==envi_header::float32) return(file = new bil()); else if(header.data_type == envi_header::float64) return(file = new bil()); } exit(1); //if the function hasn't already returned, we don't handle this state } bool open(std::string filename, std::string headername){ //allocate memory allocate(); //load the header header.load(headername); //load the file if(header.interleave == envi_header::BSQ) { //if the infile is bsq file if(header.data_type == envi_header::float32) { return ((bsq*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); } else if(header.data_type == envi_header::float64) { return ((bsq*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); } else return false; } else if(header.interleave == envi_header::BIL) { //if the infile is bil file if(header.data_type == envi_header::float32) { return ((bil*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); } else if(header.data_type == envi_header::float64) { return ((bil*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); } else return false; } else if(header.interleave == envi_header::BIP) { //if the infile is bip file if(header.data_type == envi_header::float32) { return ((bip*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); } else if(header.data_type == envi_header::float64) { return ((bip*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); } else return false; } else{ std::cout<<"ERROR: unidentified type file "<*)file)->normalize(outfile, band); else if(header.data_type == envi_header::float64) return ((bsq*)file)->normalize(outfile,band); else std::cout<<"ERROR: unidentified data type"<*)file)->normalize(outfile, band); else if(header.data_type == envi_header::float64) return ((bil*)file)->normalize(outfile,band); else std::cout<<"ERROR: unidentified data type"<*)file)->normalize(outfile, band); else if(header.data_type == envi_header::float64) return ((bip*)file)->normalize(outfile,band); else std::cout<<"ERROR: unidentified data type"< w){ if(header.interleave == envi_header::BSQ){ //if the infile is bsq file if(header.data_type ==envi_header::float32) return ((bsq*)file)->baseline(outfile, w); else if(header.data_type == envi_header::float64) return ((bsq*)file)->baseline(outfile,w); else{ std::cout<<"ERROR: unidentified data type"<*)file)->baseline(outfile, w); else if(header.data_type == envi_header::float64) return ((bil*)file)->baseline(outfile, w); else{ std::cout<<"ERROR: unidentified data type"<*)file)->baseline(outfile, w); else if(header.data_type == envi_header::float64) return ((bip*)file)->baseline(outfile, w); else{ std::cout<<"ERROR: unidentified data type"<*)file)->bil(outfile); else if(interleave == envi_header::BIP) //if the target file is bip file return ((bsq*)file)->bip(outfile); } else if(header.data_type == envi_header::float64){ //if the data type is float if(interleave == envi_header::BSQ){ std::cout<<"ERROR: is already BSQ file"<*)file)->bil(outfile); else if(interleave == envi_header::BIP) return ((bsq*)file)->bip(outfile); } else{ std::cout<<"ERROR: unidentified data type"<*)file)->bsq(outfile); else if(interleave == envi_header::BIP) //if the target file is bip file return ((bil*)file)->bip(outfile); } else if(header.data_type == envi_header::float64){ //if the data type is float if(interleave == envi_header::BIL){ std::cout<<"ERROR: is already BIL file"<*)file)->bsq(outfile); else if(interleave == envi_header::BIP) return ((bil*)file)->bip(outfile); } else{ std::cout<<"ERROR: unidentified data type"<*)file)->bil(outfile); else if(interleave == envi_header::BSQ) //if the target file is bsq file return ((bip*)file)->bsq(outfile); } else if(header.data_type == envi_header::float64){ //if the data type is float if(interleave == envi_header::BIP){ std::cout<<"ERROR: is already BIP file"<*)file)->bil(outfile); else if(interleave == envi_header::BSQ) //if the target file is bsq file return ((bip*)file)->bsq(outfile); } else{ std::cout<<"ERROR: unidentified data type"<*)file)->build_mask(mask_band, threshold, p); else if(header.data_type == envi_header::float64) return ((bsq*)file)->build_mask(mask_band, threshold, p); else std::cout<<"ERROR: unidentified data type"<*)file)->build_mask(mask_band, threshold, p); else if(header.data_type == envi_header::float64) return ((bil*)file)->build_mask(mask_band, threshold, p); else std::cout<<"ERROR: unidentified data type"<*)file)->build_mask(mask_band, threshold, p); else if(header.data_type == envi_header::float64) return ((bip*)file)->build_mask(mask_band, threshold, p); else std::cout<<"ERROR: unidentified data type"<*)file)->apply_mask(outfile, p); else if(header.data_type == envi_header::float64) return ((bsq*)file)->apply_mask(outfile, p); else std::cout<<"ERROR: unidentified data type"<*)file)->apply_mask(outfile, p); else if(header.data_type == envi_header::float64) return ((bil*)file)->apply_mask(outfile, p); else std::cout<<"ERROR: unidentified data type"<*)file)->apply_mask(outfile, p); else if(header.data_type == envi_header::float64) return ((bip*)file)->apply_mask(outfile, p); else std::cout<<"ERROR: unidentified data type"<*)file)->ph_to_ph(lb1, rb1, pos1, lb2, rb2, pos2, (float*)result); else if(header.data_type == envi_header::float64) return ((bsq*)file)->ph_to_ph(lb1, rb1, pos1, lb2, rb2, pos2, (double*)result); else std::cout<<"ERROR: unidentified data type"<*)file)->ph_to_ph(lb1, rb1, pos1, lb2, rb2, pos2, (float*)result); else if(header.data_type == envi_header::float64) return ((bil*)file)->ph_to_ph(lb1, rb1, pos1, lb2, rb2, pos2, (double*)result); else std::cout<<"ERROR: unidentified data type"<*)file)->ph_to_ph(lb1, rb1, pos1, lb2, rb2, pos2, (float*)result); else if(header.data_type == envi_header::float64) return ((bip*)file)->ph_to_ph(lb1, rb1, pos1, lb2, rb2, pos2, (double*)result); else std::cout<<"ERROR: unidentified data type"<*)file)->pa_to_ph(lb1, rb1, lab1, rab1, lb2, rb2, pos, (float*)result); else if(header.data_type == envi_header::float64) return ((bsq*)file)->pa_to_ph(lb1, rb1, lab1, rab1, lb2, rb2, pos, (double*)result); else std::cout<<"ERROR: unidentified data type"<*)file)->pa_to_ph(lb1, rb1, lab1, rab1, lb2, rb2, pos, (float*)result); else if(header.data_type == envi_header::float64) return ((bil*)file)->pa_to_ph(lb1, rb1, lab1, rab1, lb2, rb2, pos, (double*)result); else std::cout<<"ERROR: unidentified data type"<*)file)->pa_to_ph(lb1, rb1, lab1, rab1, lb2, rb2, pos, (float*)result); else if(header.data_type == envi_header::float64) return ((bip*)file)->pa_to_ph(lb1, rb1, lab1, rab1, lb2, rb2, pos, (double*)result); else std::cout<<"ERROR: unidentified data type"<*)file)->pa_to_pa(lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, (float*)result); else if(header.data_type == envi_header::float64) return ((bsq*)file)->pa_to_pa(lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, (double*)result); else std::cout<<"ERROR: unidentified data type"<*)file)->pa_to_pa(lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, (float*)result); else if(header.data_type == envi_header::float64) return ((bil*)file)->pa_to_pa(lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, (double*)result); else std::cout<<"ERROR: unidentified data type"<*)file)->pa_to_pa(lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, (float*)result); else if(header.data_type == envi_header::float64) return ((bip*)file)->pa_to_pa(lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, (double*)result); else std::cout<<"ERROR: unidentified data type"<*)file)->cpoint(lb1, rb1, lab1, rab1, (float*)result); else if(header.data_type == envi_header::float64) return ((bsq*)file)->cpoint(lb1, rb1, lab1, rab1, (double*)result); else std::cout<<"ERROR: unidentified data type"<*)file)->cpoint(lb1, rb1, lab1, rab1, (float*)result); else if(header.data_type == envi_header::float64) return ((bil*)file)->cpoint(lb1, rb1, lab1, rab1, (double*)result); else std::cout<<"ERROR: unidentified data type"<*)file)->cpoint(lb1, rb1, lab1, rab1,(float*)result); else if(header.data_type == envi_header::float64) return ((bip*)file)->cpoint(lb1, rb1, lab1, rab1, (double*)result); else std::cout<<"ERROR: unidentified data type"<*)file)->close(); else if(header.data_type == envi_header::float64) return ((bsq*)file)->close(); else{ std::cout<<"ERROR: unidentified data type"<*)file)->close(); else if(header.data_type == envi_header::float64) return ((bil*)file)->close(); else{ std::cout<<"ERROR: unidentified data type"<*)file)->close(); else if(header.data_type == envi_header::float64) return ((bip*)file)->close(); else{ std::cout<<"ERROR: unidentified data type"<*)file)->pixel((float*)p, n); else if(header.data_type == envi_header::float64) return ((bsq*)file)->pixel((double*)p, n); else{ std::cout<<"ERROR: unidentified data type"<*)file)->pixel((float*)p, n); else if(header.data_type == envi_header::float64) return ((bil*)file)->pixel((double*)p, n); else{ std::cout<<"ERROR: unidentified data type"<*)file)->pixel((float*)p, n); else if(header.data_type == envi_header::float64) return ((bip*)file)->pixel((double*)p, n); else{ std::cout<<"ERROR: unidentified data type"<*)file)->band((float*)ptr, wavelength); } return false; } bool band_index(void* ptr, unsigned int b){ if(header.interleave == envi_header::BSQ){ //if the infile is bsq file if(header.data_type ==envi_header::float32) return ((bsq*)file)->band_index((float*)ptr, b); } return false; } //load mask from maskfile and save it into memory bool load_mask(unsigned char * mask, std::string maskname){ //open the mask file cimg_library::CImg mask_image(maskname.c_str()); //save mask file into memory memcpy(mask, mask_image.data(), mask_image.size()); mask_image.clear(); return true; } //p:start positon; N: number of pixels saved in X; bool feature_matrix(void * X, unsigned char * mask, unsigned start, unsigned N) { //save pixels in X as floating numbers: float * p float * p = (float*)malloc(header.bands * sizeof(float)); //save pixel information unsigned pixels = header.samples * header.lines; //calculate pixel numbers in a band unsigned count = 0; //for counting use unsigned j = 0; //memory the pointer location in X //create two indices into the mask (mask index and pixel index) unsigned mi = 0; //valid pixel index unsigned pi = 0; //actual pixel in the mask //find the actual pixel index for the mask index "start" while(mi < start){ if(mask[pi]) mi++; pi++; } //at this point, pi is an index to the mi'th valid pixel //load the next N valid pixels //for(unsigned i = 0; i < N; i++){ //} for(unsigned i = pi; i < pixels; i++){ if(mask[i] != 0){ pixel(p, i); //copy p to X for(unsigned k = 0; k < header.bands; k++){ ((float *)X)[j] = p[k]; j++; } count++; if(count == N) break; } else continue; } if(count < N){ std::cout << "number of valid pixels in the mask : " << count <<"is less than N: "<< N; exit(1); } return true; } }; } //end namespace rts #endif