diff --git a/stim/envi/bil.h b/stim/envi/bil.h index f576d4b..b1a6ed1 100644 --- a/stim/envi/bil.h +++ b/stim/envi/bil.h @@ -200,6 +200,11 @@ public: /// @param p is a pointer to pre-allocated memory at least B * sizeof(T) in size. /// @param x is the x-coordinate (dimension 1) of the spectrum. /// @param y is the y-coordinate (dimension 2) of the spectrum. + bool spectrum(T* p, size_t n, bool PROGRESS = false){ + size_t y = n / X(); + size_t x = n - y * X(); + return binary::read_line_1(p, x, y, PROGRESS); + } bool spectrum(T * p, unsigned long long x, unsigned long long y, bool PROGRESS = false){ return binary::read_line_1(p, x, y, PROGRESS); } diff --git a/stim/envi/binary.h b/stim/envi/binary.h index 7b5a730..3385d00 100644 --- a/stim/envi/binary.h +++ b/stim/envi/binary.h @@ -200,11 +200,30 @@ public: /// @param p is a pointer to pre-allocated memory equal to the line size R[2] /// @param x is the x coordinate /// @param y is the y coordinate - bool read_line_2( T * p, unsigned long long x, unsigned long long y, bool PROGRESS = false){ + void read_line_2( T* p, size_t n, bool PROGRESS = false){ unsigned long long i; if(PROGRESS) progress = 0; + if ( n > R[0] * R[1]){ //make sure the sample and line number is right + std::cout<<"ERROR: sample or line out of range in "<<__FILE__<<" (line "<<__LINE__<<")"<= R[0] || y >= R[1]){ //make sure the sample and line number is right std::cout<<"ERROR: sample or line out of range"<= R[1] || z >= R[2] ){ - std::cout<<"ERROR: sample or line out of range"<= R[0] || z >= R[2] ){ - std::cout<<"ERROR: sample or line out of range"<= R[0]){ //make sure the number is within the possible range - std::cout<<"ERROR read_plane_0: page out of range"< + inline void cast(DST* dst, SRC* src){ + (*dst) = (DST)(*src); + } + + //cast an array from type SRC to type DEST + template + inline void cast(DST* dst, SRC* src, size_t len){ + for(size_t i = 0; i < len; i++) + cast(&dst[i], &src[i]); + } + public: envi_header header; + void* malloc_spectrum(){ + return alloc_array(header.bands); + } + + void* malloc_band(){ + return alloc_array(header.samples * header.lines); + } + /// Returns the size of the data type in bytes unsigned int type_size(){ if(header.data_type == envi_header::float32) return 4; @@ -1060,7 +1106,7 @@ public: /// @param ptr is a pointer to pre-allocated memory of size B*sizeof(T) /// @param x is the x-coordinate of the spectrum /// @param y is the y-coordinate of the spectrum - bool spectrum(void* ptr, unsigned long long x, unsigned long long y, bool PROGRESS = false){ + /*bool spectrum(void* ptr, unsigned long long x, unsigned long long y, bool PROGRESS = false){ if(header.interleave == envi_header::BSQ){ //if the infile is bsq file if(header.data_type ==envi_header::float32) @@ -1093,6 +1139,114 @@ public: } } return false; + }*/ + + // Retrieve a spectrum at the specified 1D location + + /// @param ptr is a pointer to pre-allocated memory of size B*sizeof(T) + /// @param x is the 1D coordinate of the spectrum + template + void spectrum(T* ptr, size_t n, bool PROGRESS = false){ + + void* temp = alloc_array(header.bands); //allocate space for the output array + + if(header.interleave == envi_header::BSQ){ //if the infile is bsq file + if(header.data_type ==envi_header::float32){ + ((bsq*)file)->spectrum((float*)temp, n, PROGRESS); + cast(ptr, (float*)temp, header.bands); + } + else if (header.data_type == envi_header::float64){ + ((bsq*)file)->spectrum((double*)temp, n, PROGRESS); + cast(ptr, (double*)temp, header.bands); + } + else{ + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + else if (header.interleave == envi_header::BIL){ + if (header.data_type == envi_header::float32){ + ((bil*)file)->spectrum((float*)temp, n, PROGRESS); + cast(ptr, (float*)temp, header.bands); + } + else if (header.data_type == envi_header::float64){ + ((bil*)file)->spectrum((double*)temp, n, PROGRESS); + cast(ptr, (double*)temp, header.bands); + } + else{ + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + else if (header.interleave == envi_header::BIP){ + if (header.data_type == envi_header::float32){ + ((bip*)file)->spectrum((float*)temp, n, PROGRESS); + cast(ptr, (float*)temp, header.bands); + } + else if (header.data_type == envi_header::float64){ + ((bip*)file)->spectrum((double*)temp, n, PROGRESS); + cast(ptr, (double*)temp, header.bands); + } + else{ + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + } + + /// Retrieve a spectrum from the specified (x, y) location + + /// @param ptr is a pointer to pre-allocated memory of size B*sizeof(T) + /// @param x is the x-coordinate of the spectrum + /// @param y is the y-coordinate of the spectrum + template + void spectrum(T* ptr, unsigned long long x, unsigned long long y, bool PROGRESS = false){ + + spectrum(ptr, y * header.samples + x, PROGRESS = false); + /*void* temp = alloc_array(header.bands); //allocate space for the output array + + if(header.interleave == envi_header::BSQ){ //if the infile is bsq file + if(header.data_type ==envi_header::float32){ + ((bsq*)file)->spectrum((float*)temp, x, y, PROGRESS); + cast(ptr, temp, header.bands); + } + else if (header.data_type == envi_header::float64){ + ((bsq*)file)->spectrum((double*)temp, x, y, PROGRESS); + cast(ptr, temp, header.bands); + } + else{ + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + else if (header.interleave == envi_header::BIL){ + if (header.data_type == envi_header::float32){ + ((bil*)file)->spectrum((float*)temp, x, y, PROGRESS); + cast(ptr, temp, header.bands); + } + else if (header.data_type == envi_header::float64){ + ((bil*)file)->spectrum((double*)temp, x, y, PROGRESS); + cast(ptr, temp, header.bands); + } + else{ + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + else if (header.interleave == envi_header::BIP){ + if (header.data_type == envi_header::float32){ + ((bip*)file)->spectrum((float*)temp, x, y, PROGRESS); + cast(ptr, temp, header.bands); + } + else if (header.data_type == envi_header::float64){ + ((bip*)file)->spectrum((double*)temp, x, y, PROGRESS); + cast(ptr, temp, header.bands); + } + else{ + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + }*/ } /// Retrieve a single band (based on index) and stores it in pre-allocated memory. diff --git a/stim/image/image.h b/stim/image/image.h index 9eca082..a18f179 100644 --- a/stim/image/image.h +++ b/stim/image/image.h @@ -33,8 +33,7 @@ class image{ void unalloc(){ //frees any resources associated with the image if(img) free(img); //if memory has been allocated, free it - img=NULL; - + img=NULL; } @@ -92,7 +91,9 @@ class image{ public: /// Default constructor - creates an empty image object - image(){ init(); } //initialize all variables to zero, don't allocate any memory + image(){ + init(); //initialize all variables to zero, don't allocate any memory + } /// Constructor with a filename - loads the specified file image(std::string filename){ //constructor initialize the image with an image file @@ -114,7 +115,7 @@ public: } /// Copy constructor - duplicates an image object - image(const stim::image& I){ + image(const stim::image &I){ init(); allocate(I.X(), I.Y(), I.C()); memcpy(img, I.img, bytes()); @@ -125,7 +126,7 @@ public: free(img); } - stim::image operator=(const stim::image& I){ + stim::image& operator=(const stim::image& I){ if(&I == this) //handle self-assignment return *this; allocate(I.X(), I.Y(), I.C()); @@ -138,7 +139,7 @@ public: cv::Mat cvImage = cv::imread(filename, CV_LOAD_IMAGE_UNCHANGED); //use OpenCV to open the image file if(!cvImage.data){ - std::cout<<"ERROR stim::image::load() - unable to find image "< +#include +#include + +namespace stim{ + + class table{ + + std::vector< std::vector< std::string > > TABLE; + + size_t x; //current x and y positions for adding elements to the table + size_t y; + + void init(){ + x = y = 0; + TABLE.resize(1); //create a single row + TABLE[0].clear(); //make sure that the row is empty + } + public: + + table(){ + init(); + } + + void new_column(){ + x = 0; //reset to the first position of the column + y++; //increment the current row position + TABLE.push_back( std::vector() ); //push an empty row + } + + template + T operator<<(T i){ + std::stringstream ss; + ss< 0) outfile< row; + while ((pos = line.find(col_delim)) != std::string::npos) { + token = line.substr(0, pos); + row.push_back(token); + line.erase(0, pos + 1); + } + token = line.substr(0, std::string::npos); //read the last column + row.push_back(token); //push it into the table + TABLE.push_back(row); //add an empty vector to the table + } + } + + /// Returns a vector representation of the table by casting to the specified template + template + std::vector< std::vector > get_vector(){ + std::vector< std::vector > result; + result.resize(TABLE.size()); //initialize the first dimension of the returned table + for(size_t i = 0; i < TABLE.size(); i++){ //for each row + result[i].resize(TABLE[i].size()); //initialize the number of columns in this row + for(size_t j = 0; j < TABLE[i].size(); j++){ //for each column + std::stringstream ss(TABLE[i][j]); + ss >> result[i][j]; + } + } + return result; //return the casted table + } + + }; +}; + + +#endif \ No newline at end of file -- libgit2 0.21.4