Commit e37ce7bf1167070f130fa7368032073a597d9d62

Authored by David Mayerich
1 parent 3a880d1c

added the ability to build masks from other masks

@@ -828,16 +828,19 @@ public: @@ -828,16 +828,19 @@ public:
828 /// @param mask_band is the band used to specify the mask 828 /// @param mask_band is the band used to specify the mask
829 /// @param threshold is the threshold used to determine if the mask value is true or false 829 /// @param threshold is the threshold used to determine if the mask value is true or false
830 /// @param p is a pointer to a pre-allocated array at least X * Y in size 830 /// @param p is a pointer to a pre-allocated array at least X * Y in size
831 - bool build_mask(unsigned char* mask, double mask_band, double threshold, bool PROGRESS = false){ 831 + bool build_mask(unsigned char* out_mask, double mask_band, double lower, double upper, unsigned char* mask = NULL, bool PROGRESS = false){
832 832
833 T* temp = (T*)malloc(X() * Y() * sizeof(T)); //allocate memory for the certain band 833 T* temp = (T*)malloc(X() * Y() * sizeof(T)); //allocate memory for the certain band
834 band(temp, mask_band, PROGRESS); 834 band(temp, mask_band, PROGRESS);
835 835
836 for (unsigned long long i = 0; i < X() * Y(); i++) { 836 for (unsigned long long i = 0; i < X() * Y(); i++) {
837 - if (temp[i] < threshold)  
838 - mask[i] = 0;  
839 - else  
840 - mask[i] = 255; 837 + if(mask == NULL || mask[i] != 0){
  838 + if(temp[i] > lower && temp[i] < upper){
  839 + out_mask[i] = 255;
  840 + }
  841 + else
  842 + out_mask[i] = 0;
  843 + }
841 } 844 }
842 845
843 free(temp); 846 free(temp);
@@ -762,16 +762,19 @@ public: @@ -762,16 +762,19 @@ public:
762 /// @param mask_band is the band used to specify the mask 762 /// @param mask_band is the band used to specify the mask
763 /// @param threshold is the threshold used to determine if the mask value is true or false 763 /// @param threshold is the threshold used to determine if the mask value is true or false
764 /// @param p is a pointer to a pre-allocated array at least X * Y in size 764 /// @param p is a pointer to a pre-allocated array at least X * Y in size
765 - bool build_mask(unsigned char* mask, double mask_band, double threshold, bool PROGRESS = false){ 765 + bool build_mask(unsigned char* out_mask, double mask_band, double lower, double upper, unsigned char* mask = NULL, bool PROGRESS = false){
766 766
767 T* temp = (T*)malloc(X() * Y() * sizeof(T)); //allocate memory for the certain band 767 T* temp = (T*)malloc(X() * Y() * sizeof(T)); //allocate memory for the certain band
768 band(temp, mask_band, PROGRESS); 768 band(temp, mask_band, PROGRESS);
769 769
770 for (unsigned long long i = 0; i < X() * Y();i++) { 770 for (unsigned long long i = 0; i < X() * Y();i++) {
771 - if (temp[i] < threshold)  
772 - mask[i] = 0;  
773 - else  
774 - mask[i] = 255; 771 + if(mask == NULL || mask[i] != 0){
  772 + if(temp[i] > lower && temp[i] < upper){
  773 + out_mask[i] = 255;
  774 + }
  775 + else
  776 + out_mask[i] = 0;
  777 + }
775 } 778 }
776 779
777 free(temp); 780 free(temp);
@@ -914,16 +914,20 @@ public: @@ -914,16 +914,20 @@ public:
914 /// @param mask_band is the band used to specify the mask 914 /// @param mask_band is the band used to specify the mask
915 /// @param threshold is the threshold used to determine if the mask value is true or false 915 /// @param threshold is the threshold used to determine if the mask value is true or false
916 /// @param p is a pointer to a pre-allocated array at least X * Y in size 916 /// @param p is a pointer to a pre-allocated array at least X * Y in size
917 - bool build_mask(unsigned char* mask, double mask_band, double threshold, bool PROGRESS = false){ 917 + bool build_mask(unsigned char* out_mask, double mask_band, double lower, double upper, unsigned char* mask = NULL, bool PROGRESS = false){
  918 + memset(out_mask, 0, X() * Y()); //initialize the mask to zero
918 919
919 T* temp = (T*)malloc(X() * Y() * sizeof(T)); //allocate memory for the certain band 920 T* temp = (T*)malloc(X() * Y() * sizeof(T)); //allocate memory for the certain band
920 band(temp, mask_band); 921 band(temp, mask_band);
921 922
922 for (unsigned long long i = 0; i < X() * Y(); i++) { 923 for (unsigned long long i = 0; i < X() * Y(); i++) {
923 - if (temp[i] < threshold)  
924 - mask[i] = 0;  
925 - else  
926 - mask[i] = 255; 924 + if(mask == NULL || mask[i] != 0){
  925 + if(temp[i] > lower && temp[i] < upper){
  926 + out_mask[i] = 255;
  927 + }
  928 + else
  929 + out_mask[i] = 0;
  930 + }
927 931
928 if(PROGRESS) progress = (double) (i+1) / (X() * Y()) * 100; 932 if(PROGRESS) progress = (double) (i+1) / (X() * Y()) * 100;
929 } 933 }
@@ -718,31 +718,31 @@ public: @@ -718,31 +718,31 @@ public:
718 /// @param mask_band is the label for the band that will be used to build the mask 718 /// @param mask_band is the label for the band that will be used to build the mask
719 /// @param threshold is a value selected such that all band values greater than threshold will have a mask value of 'true' 719 /// @param threshold is a value selected such that all band values greater than threshold will have a mask value of 'true'
720 /// @param p is memory of size X*Y that will store the resulting mask 720 /// @param p is memory of size X*Y that will store the resulting mask
721 - bool build_mask(unsigned char* mask, double mask_band, double threshold, bool PROGRESS = false) { 721 + bool build_mask(unsigned char* out_mask, double mask_band, double lower, double upper, unsigned char* mask = NULL, bool PROGRESS = false) {
722 722
723 if(header.interleave == envi_header::BSQ){ //if the infile is bsq file 723 if(header.interleave == envi_header::BSQ){ //if the infile is bsq file
724 if(header.data_type ==envi_header::float32) 724 if(header.data_type ==envi_header::float32)
725 - return ((bsq<float>*)file)->build_mask(mask, mask_band, threshold, PROGRESS); 725 + return ((bsq<float>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS);
726 else if(header.data_type == envi_header::float64) 726 else if(header.data_type == envi_header::float64)
727 - return ((bsq<double>*)file)->build_mask(mask, mask_band, threshold, PROGRESS); 727 + return ((bsq<double>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS);
728 else 728 else
729 std::cout<<"ERROR: unidentified data type"<<std::endl; 729 std::cout<<"ERROR: unidentified data type"<<std::endl;
730 } 730 }
731 731
732 else if(header.interleave == envi_header::BIL){ //if the infile is bil file 732 else if(header.interleave == envi_header::BIL){ //if the infile is bil file
733 if(header.data_type ==envi_header::float32) 733 if(header.data_type ==envi_header::float32)
734 - return ((bil<float>*)file)->build_mask(mask, mask_band, threshold, PROGRESS); 734 + return ((bil<float>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS);
735 else if(header.data_type == envi_header::float64) 735 else if(header.data_type == envi_header::float64)
736 - return ((bil<double>*)file)->build_mask(mask, mask_band, threshold, PROGRESS); 736 + return ((bil<double>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS);
737 else 737 else
738 std::cout<<"ERROR: unidentified data type"<<std::endl; 738 std::cout<<"ERROR: unidentified data type"<<std::endl;
739 } 739 }
740 740
741 else if(header.interleave == envi_header::BIP){ //if the infile is bip file 741 else if(header.interleave == envi_header::BIP){ //if the infile is bip file
742 if(header.data_type ==envi_header::float32) 742 if(header.data_type ==envi_header::float32)
743 - return ((bip<float>*)file)->build_mask(mask, mask_band, threshold, PROGRESS); 743 + return ((bip<float>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS);
744 else if(header.data_type == envi_header::float64) 744 else if(header.data_type == envi_header::float64)
745 - return ((bip<double>*)file)->build_mask(mask, mask_band, threshold, PROGRESS); 745 + return ((bip<double>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS);
746 else 746 else
747 std::cout<<"ERROR: unidentified data type"<<std::endl; 747 std::cout<<"ERROR: unidentified data type"<<std::endl;
748 } 748 }
@@ -751,30 +751,30 @@ public: @@ -751,30 +751,30 @@ public:
751 } 751 }
752 752
753 /// Creates a mask with a true value for all pixels that contain finite values 753 /// Creates a mask with a true value for all pixels that contain finite values
754 - void mask_finite(unsigned char* mask, bool PROGRESS = false){ 754 + void mask_finite(unsigned char* out_mask, unsigned char* mask = NULL, bool PROGRESS = false){
755 if(header.interleave == envi_header::BSQ){ //if the infile is bsq file 755 if(header.interleave == envi_header::BSQ){ //if the infile is bsq file
756 if(header.data_type ==envi_header::float32) 756 if(header.data_type ==envi_header::float32)
757 - ((bsq<float>*)file)->mask_finite(mask, PROGRESS); 757 + ((bsq<float>*)file)->mask_finite(out_mask, mask, PROGRESS);
758 else if(header.data_type == envi_header::float64) 758 else if(header.data_type == envi_header::float64)
759 - ((bsq<double>*)file)->mask_finite(mask, PROGRESS); 759 + ((bsq<double>*)file)->mask_finite(out_mask, mask, PROGRESS);
760 else 760 else
761 std::cout<<"ERROR: unidentified data type"<<std::endl; 761 std::cout<<"ERROR: unidentified data type"<<std::endl;
762 } 762 }
763 763
764 else if(header.interleave == envi_header::BIL){ //if the infile is bil file 764 else if(header.interleave == envi_header::BIL){ //if the infile is bil file
765 if(header.data_type ==envi_header::float32) 765 if(header.data_type ==envi_header::float32)
766 - ((bil<float>*)file)->mask_finite(mask, PROGRESS); 766 + ((bil<float>*)file)->mask_finite(out_mask, mask, PROGRESS);
767 else if(header.data_type == envi_header::float64) 767 else if(header.data_type == envi_header::float64)
768 - ((bil<double>*)file)->mask_finite(mask, PROGRESS); 768 + ((bil<double>*)file)->mask_finite(out_mask, mask, PROGRESS);
769 else 769 else
770 std::cout<<"ERROR: unidentified data type"<<std::endl; 770 std::cout<<"ERROR: unidentified data type"<<std::endl;
771 } 771 }
772 772
773 else if(header.interleave == envi_header::BIP){ //if the infile is bip file 773 else if(header.interleave == envi_header::BIP){ //if the infile is bip file
774 if(header.data_type ==envi_header::float32) 774 if(header.data_type ==envi_header::float32)
775 - ((bip<float>*)file)->mask_finite(mask, PROGRESS); 775 + ((bip<float>*)file)->mask_finite(out_mask, mask, PROGRESS);
776 else if(header.data_type == envi_header::float64) 776 else if(header.data_type == envi_header::float64)
777 - ((bip<double>*)file)->mask_finite(mask, PROGRESS); 777 + ((bip<double>*)file)->mask_finite(out_mask, mask, PROGRESS);
778 else 778 else
779 std::cout<<"ERROR: unidentified data type"<<std::endl; 779 std::cout<<"ERROR: unidentified data type"<<std::endl;
780 } 780 }
@@ -139,9 +139,12 @@ protected: @@ -139,9 +139,12 @@ protected:
139 139
140 public: 140 public:
141 /// Get a mask that has all pixels with inf or NaN values masked out (false) 141 /// Get a mask that has all pixels with inf or NaN values masked out (false)
142 - void mask_finite(unsigned char* mask, bool PROGRESS = false){ 142 + void mask_finite(unsigned char* out_mask, unsigned char* mask, bool PROGRESS = false){
143 size_t XY = X() * Y(); 143 size_t XY = X() * Y();
144 - memset(mask, 255, XY * sizeof(unsigned char)); //initialize the mask to zero (0) 144 + if(mask == NULL) //if no mask is provided
  145 + memset(mask, 255, XY * sizeof(unsigned char)); //initialize the mask to 255
  146 + else //if a mask is provided
  147 + memcpy(out_mask, mask, XY * sizeof(unsigned char)); //initialize the current mask to that one
145 T* page = (T*)malloc(R[0] * R[1] * sizeof(T)); //allocate space for a page of data 148 T* page = (T*)malloc(R[0] * R[1] * sizeof(T)); //allocate space for a page of data
146 149
147 for(size_t p = 0; p < R[2]; p++){ //for each page 150 for(size_t p = 0; p < R[2]; p++){ //for each page
@@ -155,7 +158,7 @@ public: @@ -155,7 +158,7 @@ public:
155 #endif 158 #endif
156 size_t x, y, b; 159 size_t x, y, b;
157 xyb(p * R[0] * R[1] + i, x, y, b); //find the 3D coordinates of the value 160 xyb(p * R[0] * R[1] + i, x, y, b); //find the 3D coordinates of the value
158 - mask[ y * X() + x ] = 0; //remove the pixel (it's bad) 161 + out_mask[ y * X() + x ] = 0; //remove the pixel (it's bad)
159 } 162 }
160 } 163 }
161 if(PROGRESS) progress = (double)(p + 1) / (double)R[2] * 100; 164 if(PROGRESS) progress = (double)(p + 1) / (double)R[2] * 100;