Commit fc712213c01079f5aa600051ba4994bc07f51dfe
1 parent
31394e2a
code simplifications
Showing
1 changed file
with
64 additions
and
58 deletions
Show diff stats
envi/bsq.h
1 | 1 | #include "../envi/envi.h" |
2 | 2 | #include "../envi/binary.h" |
3 | +#include <cstring> | |
4 | +#include <utility> | |
3 | 5 | |
4 | 6 | namespace rts{ |
5 | 7 | |
... | ... | @@ -13,6 +15,10 @@ protected: |
13 | 15 | |
14 | 16 | public: |
15 | 17 | |
18 | + using binary<T>::open; | |
19 | + using binary<T>::file; | |
20 | + using binary<T>::getSlice; | |
21 | + | |
16 | 22 | //open a file, given the file and its header's names |
17 | 23 | bool open(std::string filename, std::string headername){ |
18 | 24 | |
... | ... | @@ -21,7 +27,7 @@ public: |
21 | 27 | return false; |
22 | 28 | } |
23 | 29 | |
24 | - binary::open(filename, vec<unsigned int>(header.samples, header.lines, header.bands), header.header_offset); | |
30 | + open(filename, vec<unsigned int>(header.samples, header.lines, header.bands), header.header_offset); | |
25 | 31 | return true; |
26 | 32 | |
27 | 33 | } |
... | ... | @@ -61,96 +67,96 @@ public: |
61 | 67 | //baseline correction and save it into file |
62 | 68 | bool baseline(std::string outname, std::vector<unsigned> bands ) |
63 | 69 | { |
64 | - unsigned N = bands.size(); //to get points number; | |
70 | + unsigned N = bands.size(); //get the number of baseline points | |
71 | + | |
72 | + std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file | |
65 | 73 | |
66 | - ofstream bitext(outname.c_str(), std::ios::binary); //target binary file | |
74 | + //simplify image resolution | |
75 | + unsigned int B = header.bands; //calculate the number of bands | |
76 | + unsigned int XY = header.samples * header.lines; //calculate the number of pixels in a band | |
77 | + unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band | |
67 | 78 | |
68 | - unsigned i, j, k; | |
69 | - unsigned lownum, highnum; | |
79 | + unsigned ai, bi; //stores the two baseline points surrounding the current band | |
70 | 80 | unsigned control=0; |
71 | 81 | |
72 | - T * low; | |
73 | - T * high; | |
74 | - T * current; | |
75 | - T * temp; | |
82 | + T * a; //pointers to the high and low band images | |
83 | + T * b; | |
84 | + T * c; //pointer to the current image | |
76 | 85 | |
77 | - low=(T *)malloc(header.lines * header.samples * sizeof(T)); //memory allocation | |
78 | - high=(T *)malloc(header.lines * header.samples * sizeof(T)); | |
79 | - current=(T *)malloc(header.lines * header.samples * sizeof(T)); | |
86 | + a = (T*)malloc( S ); //memory allocation | |
87 | + b = (T*)malloc( S ); | |
88 | + c = (T*)malloc( S ); | |
80 | 89 | |
81 | - if (low == NULL || high == NULL || current == NULL){ | |
82 | - cout<<"memory allocation failure"; | |
83 | - return false; | |
84 | - } | |
90 | + if (a == NULL || b == NULL || c == NULL){ | |
91 | + std::cout<<"ERROR: error allocating memory"; | |
92 | + exit(1); | |
93 | + } | |
85 | 94 | |
86 | - //perform baseline correction | |
87 | 95 | |
88 | 96 | //initialize lownum, highnum, low, high |
89 | - lownum=0; | |
97 | + ai=0; | |
98 | + | |
99 | + //if no baseline point is specified at band 0, | |
100 | + //set the baseline point at band 0 to 0 | |
90 | 101 | if(bands[0] != 0){ |
91 | - highnum = bands[control]; | |
92 | - memset(low, 0.0, sizeof(T) * header.lines * header.samples); | |
102 | + bi = bands[control]; | |
103 | + memset(a, (char)0, S); | |
93 | 104 | } |
105 | + //else get the low band | |
94 | 106 | else{ |
95 | 107 | control += 1; |
96 | - getBand(low, lownum); | |
97 | - highnum = bands[control]; | |
108 | + getBand(a, ai); | |
109 | + bi = bands[control]; | |
98 | 110 | } |
99 | - getBand(high, highnum); | |
111 | + //get the high band | |
112 | + getBand(b, bi); | |
100 | 113 | |
101 | 114 | //correct every band |
102 | - for(k = 0; k < header.bands; k++) | |
103 | - { | |
104 | - cout<<k<<endl; //progress bar, delete it afterwards | |
105 | - if( k == highnum && k != header.bands-1) { | |
115 | + for(unsigned ci = 0; ci < B; ci++){ | |
116 | + | |
117 | + //update baseline points, if necessary | |
118 | + if( ci == bi && ci != B - 1) { | |
106 | 119 | //if the high band is now on the last BL point? |
107 | 120 | if (control != N-1) { |
108 | 121 | |
109 | - control += 1; | |
122 | + control++; //increment the index | |
110 | 123 | |
111 | - temp=low; | |
112 | - low = high; | |
113 | - high=temp; | |
124 | + std::swap<T*>(a, b); //swap the baseline band pointers | |
114 | 125 | |
115 | - lownum=highnum; | |
116 | - highnum = bands[control]; | |
117 | - getBand(high, highnum); | |
126 | + ai = bi; | |
127 | + bi = bands[control]; | |
128 | + getBand(b, bi); | |
118 | 129 | |
119 | 130 | } |
120 | 131 | //if the last BL point on the last band of the file? |
121 | - else if ( bands[control] != header.bands-1) { | |
132 | + else if ( bands[control] != B - 1) { | |
122 | 133 | |
123 | - temp = low; | |
124 | - low = high; | |
125 | - high = temp; | |
134 | + std::swap<T*>(a, b); //swap the baseline band pointers | |
126 | 135 | |
127 | - memset(high, 0.0, sizeof(T) * header.lines * header.samples); | |
136 | + memset(b, (char)0, S); //clear the high band | |
128 | 137 | |
129 | - lownum = highnum; | |
130 | - highnum = header.bands-1; | |
138 | + ai = bi; | |
139 | + bi = B - 1; | |
131 | 140 | } |
132 | 141 | } |
133 | - getBand(current, k); | |
134 | - | |
135 | - for(j=0; j<header.lines; j++) | |
136 | - { | |
137 | - for(i=0; i<header.samples; i++) | |
138 | - { | |
139 | - //straight line correction | |
140 | - current[j * header.samples + i] = current[j * header.samples + i] - ( | |
141 | - ( high[j * header.samples + i] - low[j * header.samples + i] ) * (T)(k - lownum) / (T)(highnum - lownum) | |
142 | - ) - low[j * header.samples + i]; | |
143 | - } | |
142 | + | |
143 | + //get the current band | |
144 | + getBand(c, ci); | |
145 | + | |
146 | + //perform the baseline correction | |
147 | + for(unsigned i=0; i < XY; i++){ | |
148 | + double r = (double) (ci - ai) / (double) (bi - ai); | |
149 | + c[i] = c[i] - (b[i] - a[i]) * r - a[i]; | |
144 | 150 | } |
145 | 151 | |
146 | - bitext.write(reinterpret_cast<const char*>(current), sizeof(T) * header.lines * header.samples); //write the corrected data into destination | |
152 | + target.write(reinterpret_cast<const char*>(c), S); //write the corrected data into destination | |
147 | 153 | |
148 | 154 | } |
149 | 155 | |
150 | - free(low); | |
151 | - free(high); | |
152 | - free(current); | |
153 | - bitext.close(); | |
156 | + free(a); | |
157 | + free(b); | |
158 | + free(c); | |
159 | + target.close(); | |
154 | 160 | return true; |
155 | 161 | } |
156 | 162 | |
... | ... | @@ -158,4 +164,4 @@ public: |
158 | 164 | }; |
159 | 165 | } |
160 | 166 | |
161 | - | |
162 | 167 | \ No newline at end of file |
168 | + | ... | ... |