Commit e9bddc57f2e79c9c378049499775a4be385b4101
1 parent
912d9073
added band cropping for BSQ files
Showing
5 changed files
with
60 additions
and
24 deletions
Show diff stats
stim/envi/bil.h
... | ... | @@ -1005,7 +1005,12 @@ public: |
1005 | 1005 | /// @param y0 is the lower-left y pixel coordinate to be included in the cropped image |
1006 | 1006 | /// @param x1 is the upper-right x pixel coordinate to be included in the cropped image |
1007 | 1007 | /// @param y1 is the upper-right y pixel coordinate to be included in the cropped image |
1008 | - bool crop(std::string outfile, unsigned x0, unsigned y0, unsigned x1, unsigned y1){ | |
1008 | + bool crop(std::string outfile, unsigned long long x0, | |
1009 | + unsigned long long y0, | |
1010 | + unsigned long long x1, | |
1011 | + unsigned long long y1, | |
1012 | + unsigned long long b0, | |
1013 | + unsigned long long b1){ | |
1009 | 1014 | |
1010 | 1015 | //calculate the new number of samples and lines |
1011 | 1016 | unsigned long long sam = x1 - x0; //samples | ... | ... |
stim/envi/bip.h
... | ... | @@ -1061,7 +1061,12 @@ public: |
1061 | 1061 | /// @param y0 is the lower-left y pixel coordinate to be included in the cropped image |
1062 | 1062 | /// @param x1 is the upper-right x pixel coordinate to be included in the cropped image |
1063 | 1063 | /// @param y1 is the upper-right y pixel coordinate to be included in the cropped image |
1064 | - bool crop(std::string outfile, unsigned x0, unsigned y0, unsigned x1, unsigned y1){ | |
1064 | + bool crop(std::string outfile, unsigned long long x0, | |
1065 | + unsigned long long y0, | |
1066 | + unsigned long long x1, | |
1067 | + unsigned long long y1, | |
1068 | + unsigned long long b0, | |
1069 | + unsigned long long b1){ | |
1065 | 1070 | |
1066 | 1071 | //calculate the new number of samples and lines |
1067 | 1072 | unsigned long long sam = x1 - x0; //samples | ... | ... |
stim/envi/bsq.h
... | ... | @@ -968,27 +968,45 @@ public: |
968 | 968 | /// @param y0 is the lower-left y pixel coordinate to be included in the cropped image |
969 | 969 | /// @param x1 is the upper-right x pixel coordinate to be included in the cropped image |
970 | 970 | /// @param y1 is the upper-right y pixel coordinate to be included in the cropped image |
971 | - bool crop(std::string outfile, unsigned x0, unsigned y0, unsigned x1, unsigned y1){ | |
972 | - | |
973 | - //calculate the new number of samples and lines | |
974 | - unsigned long long sam = x1 - x0; //samples | |
975 | - unsigned long long lin = y1 - y0; //lines | |
976 | - unsigned long long L = sam * lin * sizeof(T); | |
977 | - //get specified band and save | |
971 | + bool crop(std::string outfile, unsigned long long x0, | |
972 | + unsigned long long y0, | |
973 | + unsigned long long x1, | |
974 | + unsigned long long y1, | |
975 | + unsigned long long b0, | |
976 | + unsigned long long b1){ | |
977 | + | |
978 | + //calculate the new number of samples, lines, and bands | |
979 | + unsigned long long samples = x1 - x0; | |
980 | + unsigned long long lines = y1 - y0; | |
981 | + unsigned long long bands = b1 - b0; | |
982 | + | |
983 | + //calculate the size of a single band | |
984 | + unsigned long long L = samples * lines * sizeof(T); | |
985 | + | |
986 | + //allocate space for a single band | |
978 | 987 | T* temp = (T*)malloc(L); |
988 | + | |
989 | + //create an output stream to store the output file | |
979 | 990 | std::ofstream out(outfile.c_str(), std::ios::binary); |
980 | - unsigned long long jumpb = X() * (Y() - lin) * sizeof(T); //jump pointer to the next band | |
981 | - unsigned long long jumpl = (X() - sam) * sizeof(T); //jump pointer to the next line | |
982 | - //get start | |
983 | - file.seekg((y0 * X() + x0) * sizeof(T), std::ios::beg); | |
984 | - for (unsigned z = 0; z < Z(); z++) | |
991 | + | |
992 | + //calculate the distance required to jump from the end of one band to the beginning of another | |
993 | + unsigned long long jumpb = X() * (Y() - lines) * sizeof(T); | |
994 | + | |
995 | + //calculate the distance required to jump from the end of one line to the beginning of another | |
996 | + unsigned long long jumpl = (X() - samples) * sizeof(T); | |
997 | + | |
998 | + //seek to the start of the cropped region in the input file | |
999 | + file.seekg( (b0 * X() * Y() + y0 * X() + x0) * sizeof(T), std::ios::beg); | |
1000 | + | |
1001 | + //for each band | |
1002 | + for (unsigned long long z = b0; z < b1; z++) | |
985 | 1003 | { |
986 | - for (unsigned j = 0; j < lin; j++) | |
1004 | + for (unsigned y = 0; y < lines; y++) | |
987 | 1005 | { |
988 | - file.read((char *)(temp + j * sam), sizeof(T) * sam); | |
1006 | + file.read((char *)(temp + y * samples), sizeof(T) * samples); | |
989 | 1007 | file.seekg(jumpl, std::ios::cur); //go to the next band |
990 | 1008 | |
991 | - thread_data = (double)(z * lin + j) / (Z() * lin) * 100; | |
1009 | + thread_data = (double)(z * lines + y) / (Z() * lines) * 100; | |
992 | 1010 | } |
993 | 1011 | out.write(reinterpret_cast<const char*>(temp), L); //write slice data into target file |
994 | 1012 | file.seekg(jumpb, std::ios::cur); | ... | ... |
stim/envi/envi.h
... | ... | @@ -1022,13 +1022,13 @@ public: |
1022 | 1022 | /// @param y0 is the lower-left y pixel coordinate to be included in the cropped image |
1023 | 1023 | /// @param x1 is the upper-right x pixel coordinate to be included in the cropped image |
1024 | 1024 | /// @param y1 is the upper-right y pixel coordinate to be included in the cropped image |
1025 | - bool crop(std::string outfile,unsigned x0, unsigned y0, unsigned x1, unsigned y1){ | |
1025 | + bool crop(std::string outfile,unsigned x0, unsigned y0, unsigned x1, unsigned y1, unsigned b0, unsigned b1){ | |
1026 | 1026 | |
1027 | 1027 | if (header.interleave == envi_header::BSQ){ |
1028 | 1028 | if (header.data_type == envi_header::float32) |
1029 | - return ((bsq<float>*)file)->crop(outfile, x0, y0, x1, y1); | |
1029 | + return ((bsq<float>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1); | |
1030 | 1030 | else if (header.data_type == envi_header::float64) |
1031 | - return ((bsq<double>*)file)->crop(outfile, x0, y0, x1, y1); | |
1031 | + return ((bsq<double>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1); | |
1032 | 1032 | else{ |
1033 | 1033 | std::cout << "ERROR: unidentified data type" << std::endl; |
1034 | 1034 | exit(1); |
... | ... | @@ -1036,9 +1036,9 @@ public: |
1036 | 1036 | } |
1037 | 1037 | else if (header.interleave == envi_header::BIL){ |
1038 | 1038 | if (header.data_type == envi_header::float32) |
1039 | - return ((bil<float>*)file)->crop(outfile, x0, y0, x1, y1); | |
1039 | + return ((bil<float>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1); | |
1040 | 1040 | else if (header.data_type == envi_header::float64) |
1041 | - return ((bil<double>*)file)->crop(outfile, x0, y0, x1, y1); | |
1041 | + return ((bil<double>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1); | |
1042 | 1042 | else{ |
1043 | 1043 | std::cout << "ERROR: unidentified data type" << std::endl; |
1044 | 1044 | exit(1); |
... | ... | @@ -1046,9 +1046,9 @@ public: |
1046 | 1046 | } |
1047 | 1047 | else if (header.interleave == envi_header::BIP){ |
1048 | 1048 | if (header.data_type == envi_header::float32) |
1049 | - return ((bip<float>*)file)->crop(outfile, x0, y0, x1, y1); | |
1049 | + return ((bip<float>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1); | |
1050 | 1050 | else if (header.data_type == envi_header::float64) |
1051 | - return ((bip<double>*)file)->crop(outfile, x0, y0, x1, y1); | |
1051 | + return ((bip<double>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1); | |
1052 | 1052 | else{ |
1053 | 1053 | std::cout << "ERROR: unidentified data type" << std::endl; |
1054 | 1054 | exit(1); | ... | ... |
stim/parser/arguments.h
... | ... | @@ -479,6 +479,14 @@ namespace stim{ |
479 | 479 | return args.size(); |
480 | 480 | } |
481 | 481 | |
482 | + /// Returns the number of options that are set | |
483 | + unsigned int nopts(){ | |
484 | + unsigned int n = 0; //initialize the counter for the number of options | |
485 | + for(unsigned int i = 0; i < opts.size(); i++) //go through each option | |
486 | + if(opts[i].is_set()) n++; //if a value is specified, increment the counter | |
487 | + return n; | |
488 | + } | |
489 | + | |
482 | 490 | ///Returns the name of an argument, given its index |
483 | 491 | |
484 | 492 | /// @param a is the index of the requested argument | ... | ... |