Blame view

stim/envi/envi.h 46.7 KB
e8eb202f   David Mayerich   added a new ENVI ...
1
2
3
4
5
6
7
  #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"
bfe9c6db   heziqi   added feature_mat...
8
  #include <iostream>
2ed641f0   David Mayerich   removed the depen...
9
  //#include "../image/image.h"
e8eb202f   David Mayerich   added a new ENVI ...
10
  
8a86bd56   David Mayerich   changed rts names...
11
  namespace stim{
e8eb202f   David Mayerich   added a new ENVI ...
12
  
cb455f98   David Mayerich   removed old ENVI ...
13
14
15
16
17
  /** This class implements reading of ENVI hyperspectral files. These files can be stored in multiple orientations
  	(including BSQ, BIP, and BIL) in order to optimize streaming speed depending on applications. Basic ENVI
  	files are stored on disk as a large binary file with a corresponding header. Code for reading and processing
  	ENVI header files is in the envi_header class.
  */
f92397d2   David Mayerich   fixed a Win32 bug...
18
  class envi{
e8eb202f   David Mayerich   added a new ENVI ...
19
20
  
  	void* file;		//void pointer to the relevant file reader (bip, bsq, or bil - with appropriate data type)
8057237a   David Mayerich   added an envi::op...
21
  	std::string fname;					//file name used for repeated opening and closing
e8eb202f   David Mayerich   added a new ENVI ...
22
23
24
  
  public:
  
4a6f666c   heziqi   Added mask method
25
26
  	envi_header header;
  
f92397d2   David Mayerich   fixed a Win32 bug...
27
28
29
30
31
32
33
34
  	/// Returns the size of the data type in bytes
  	unsigned int type_size(){
  		if(header.data_type == envi_header::float32) return 4;
  		if(header.data_type == envi_header::float64) return 8;
  
  		exit(1);
  	}
  
cbce396e   David Mayerich   added support for...
35
36
37
38
39
  	/// Returns the progress of the current processing operation as a percentage
  	void reset_progress(){
  
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
40
  				((bsq<float>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
41
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
42
  				((bsq<double>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
43
44
45
46
47
48
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
49
  				((bil<float>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
50
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
51
  				((bil<double>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
52
53
54
55
56
57
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
58
  				((bip<float>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
59
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
60
  				((bip<double>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
61
62
63
64
65
66
67
68
69
70
71
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  	}
  
  	/// Returns the progress of the current processing operation as a percentage
9d3ba0b1   David Mayerich   added stim::hsi a...
72
  	double progress(){
cbce396e   David Mayerich   added support for...
73
74
75
  
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
76
  				return ((bsq<float>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
77
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
78
  				return ((bsq<double>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
79
80
81
82
83
84
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
85
  				return ((bil<float>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
86
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
87
  				return ((bil<double>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
88
89
90
91
92
93
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
94
  				return ((bip<float>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
95
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
96
  				return ((bip<double>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
97
98
99
100
101
102
103
104
105
106
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  		}
  		return 0;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
107
  	/// Allocate memory for a new ENVI file based on the current interleave format (BIP, BIL, BSQ) and data type.
9d3ba0b1   David Mayerich   added stim::hsi a...
108
  	void allocate(){
e8eb202f   David Mayerich   added a new ENVI ...
109
110
111
  
  		file = NULL;	//set file to a NULL pointer
  
6708cc25   heziqi   Ziqi added envi c...
112
  		if(header.interleave == envi_header::BSQ){
e8eb202f   David Mayerich   added a new ENVI ...
113
  			if(header.data_type ==envi_header::float32)
9d3ba0b1   David Mayerich   added stim::hsi a...
114
  				file = new bsq<float>();
6708cc25   heziqi   Ziqi added envi c...
115
  			else if(header.data_type == envi_header::float64)
9d3ba0b1   David Mayerich   added stim::hsi a...
116
  				file = new bsq<double>();
6708cc25   heziqi   Ziqi added envi c...
117
118
119
  		}
  		else if(header.interleave == envi_header::BIP){
  			if(header.data_type ==envi_header::float32)
9d3ba0b1   David Mayerich   added stim::hsi a...
120
  				file = new bip<float>();
6708cc25   heziqi   Ziqi added envi c...
121
  			else if(header.data_type == envi_header::float64)
9d3ba0b1   David Mayerich   added stim::hsi a...
122
  				file = new bip<double>();
6708cc25   heziqi   Ziqi added envi c...
123
124
125
  		}
  		else if(header.interleave == envi_header::BIL){
  			if(header.data_type ==envi_header::float32)
9d3ba0b1   David Mayerich   added stim::hsi a...
126
  				file = new bil<float>();
6708cc25   heziqi   Ziqi added envi c...
127
  			else if(header.data_type == envi_header::float64)
9d3ba0b1   David Mayerich   added stim::hsi a...
128
  				file = new bil<double>();
6708cc25   heziqi   Ziqi added envi c...
129
  		}
e8eb202f   David Mayerich   added a new ENVI ...
130
  
e8eb202f   David Mayerich   added a new ENVI ...
131
132
  	}
  
8057237a   David Mayerich   added an envi::op...
133
134
  	/// Open a previously opened ENVI file
  	bool open(){
e8eb202f   David Mayerich   added a new ENVI ...
135
136
  
  		//load the file
6708cc25   heziqi   Ziqi added envi c...
137
138
  		if(header.interleave == envi_header::BSQ) {		//if the infile is bsq file
  			if(header.data_type == envi_header::float32) {
8057237a   David Mayerich   added an envi::op...
139
  				return ((bsq<float>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
140
141
  			}
  			else if(header.data_type == envi_header::float64) {
8057237a   David Mayerich   added an envi::op...
142
  				return ((bsq<double>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
143
144
145
146
  			}
  			else
  				return false;
  		}
e8eb202f   David Mayerich   added a new ENVI ...
147
  
6708cc25   heziqi   Ziqi added envi c...
148
149
  		else if(header.interleave == envi_header::BIL) {		//if the infile is bil file
  			if(header.data_type == envi_header::float32) {
8057237a   David Mayerich   added an envi::op...
150
  				return ((bil<float>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
151
152
  			}
  			else if(header.data_type == envi_header::float64) {
8057237a   David Mayerich   added an envi::op...
153
  				return ((bil<double>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
154
155
156
157
  			}
  			else
  				return false;
  		}
f92397d2   David Mayerich   fixed a Win32 bug...
158
  
6708cc25   heziqi   Ziqi added envi c...
159
160
  		else if(header.interleave == envi_header::BIP) {		//if the infile is bip file
  			if(header.data_type == envi_header::float32) {
8057237a   David Mayerich   added an envi::op...
161
  				return ((bip<float>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
162
163
  			}
  			else if(header.data_type == envi_header::float64) {
8057237a   David Mayerich   added an envi::op...
164
  				return ((bip<double>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
165
166
  			}
  			else
f92397d2   David Mayerich   fixed a Win32 bug...
167
  				return false;
6708cc25   heziqi   Ziqi added envi c...
168
169
  		}
  
f92397d2   David Mayerich   fixed a Win32 bug...
170
171
  		return true;
  
8057237a   David Mayerich   added an envi::op...
172
173
174
175
176
177
178
179
  
  	}
  
  	/// Open an existing ENVI file given the filename and a header structure
  
  	/// @param filename is the name of the ENVI binary file
  	/// @param header is an ENVI header structure
  	bool open(std::string filename, stim::envi_header h){
9d3ba0b1   David Mayerich   added stim::hsi a...
180
  		
8057237a   David Mayerich   added an envi::op...
181
182
183
184
  
  		header = h;							//store the header
  		fname = filename;					//save the filename
  
9d3ba0b1   David Mayerich   added stim::hsi a...
185
186
  		allocate();
  
8057237a   David Mayerich   added an envi::op...
187
188
189
  		return open();						//open the ENVI file;
  		
  
f92397d2   David Mayerich   fixed a Win32 bug...
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  	}
  
  	/// Open an existing ENVI file given the file and header names.
  
  	/// @param filename is the name of the ENVI binary file
  	/// @param headername is the name of the ENVI header file
  	bool open(std::string filename, std::string headername){
  
  		//allocate memory
  		//allocate();
  
  		stim::envi_header h;
  		h.load(headername);
  
  		//load the header
  		//header.load(headername);
  
  		return open(filename, h);
  
e8eb202f   David Mayerich   added a new ENVI ...
209
210
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
211
212
213
214
215
  	/// Normalize a hyperspectral ENVI file given a band number and threshold.
  
  	/// @param outfile is the name of the normalized file to be output
  	/// @param band is the band label to be output
  	/// @param threshold is a threshold value specified such that normalization will only be done to values in the band > threshold (preventing division by small numbers)
798cea97   David Mayerich   added progress ba...
216
  	bool normalize(std::string outfile, double band, unsigned char* mask = NULL, bool PROGRESS = false){
a2bf1d08   David Mayerich   general bug fixes...
217
  		header.save(outfile + ".hdr");
6708cc25   heziqi   Ziqi added envi c...
218
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
e8eb202f   David Mayerich   added a new ENVI ...
219
  			if(header.data_type ==envi_header::float32)
798cea97   David Mayerich   added progress ba...
220
  				return ((bsq<float>*)file)->normalize(outfile, band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
221
  			else if(header.data_type == envi_header::float64)
798cea97   David Mayerich   added progress ba...
222
  				return ((bsq<double>*)file)->normalize(outfile,band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
223
224
225
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
e8eb202f   David Mayerich   added a new ENVI ...
226
  
6708cc25   heziqi   Ziqi added envi c...
227
228
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
798cea97   David Mayerich   added progress ba...
229
  				return ((bil<float>*)file)->normalize(outfile, band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
230
  			else if(header.data_type == envi_header::float64)
798cea97   David Mayerich   added progress ba...
231
  				return ((bil<double>*)file)->normalize(outfile,band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
232
233
234
235
236
237
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
798cea97   David Mayerich   added progress ba...
238
  				return ((bip<float>*)file)->normalize(outfile, band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
239
  			else if(header.data_type == envi_header::float64)
798cea97   David Mayerich   added progress ba...
240
  				return ((bip<double>*)file)->normalize(outfile,band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
241
242
243
244
245
246
247
248
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
e933c14e   David Mayerich   cleaned up the code
249
  		return false;
6708cc25   heziqi   Ziqi added envi c...
250
251
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
252
253
254
255
  	/// Performs piecewise linear baseline correction of a hyperspectral file/
  
  	/// @param outfile is the file name for the baseline corrected output
  	/// @param w is a list of band labels to serve as baseline points (zero values)
798cea97   David Mayerich   added progress ba...
256
257
  	bool baseline(std::string outfile, std::vector<double> w, unsigned char* mask = NULL, bool PROGRESS = false){
  		header.save(outfile + ".hdr");
6708cc25   heziqi   Ziqi added envi c...
258
259
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
798cea97   David Mayerich   added progress ba...
260
  				return ((bsq<float>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
261
  			else if(header.data_type == envi_header::float64)
798cea97   David Mayerich   added progress ba...
262
  				return ((bsq<double>*)file)->baseline(outfile,w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
263
264
265
266
267
268
269
270
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
798cea97   David Mayerich   added progress ba...
271
  				return ((bil<float>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
272
  			else if(header.data_type == envi_header::float64)
798cea97   David Mayerich   added progress ba...
273
  				return ((bil<double>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
274
275
276
277
278
279
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  
f92397d2   David Mayerich   fixed a Win32 bug...
280
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
6708cc25   heziqi   Ziqi added envi c...
281
  			if(header.data_type ==envi_header::float32)
798cea97   David Mayerich   added progress ba...
282
  				return ((bip<float>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
283
  			else if(header.data_type == envi_header::float64)
798cea97   David Mayerich   added progress ba...
284
  				return ((bip<double>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
285
286
287
288
289
290
291
292
293
294
295
296
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  	}
  
9d3ba0b1   David Mayerich   added stim::hsi a...
297
  	void project(std::string outfile, double* center, double* basis, unsigned long long M, unsigned char* mask, bool PROGRESS = false){
63fc1df8   David Mayerich   added an inverse ...
298
299
300
301
302
303
304
305
306
307
308
309
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			std::cout<<"ERROR: BSQ projection not supported"<<std::endl;
  			exit(1);
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			std::cout<<"ERROR: BIL projection not supported"<<std::endl;
  			exit(1);
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
9d3ba0b1   David Mayerich   added stim::hsi a...
310
  				((bip<float>*)file)->project(outfile, center, basis, M, mask, PROGRESS);
63fc1df8   David Mayerich   added an inverse ...
311
  			else if(header.data_type == envi_header::float64)
9d3ba0b1   David Mayerich   added stim::hsi a...
312
  				((bip<double>*)file)->project(outfile, center, basis, M, mask, PROGRESS);
63fc1df8   David Mayerich   added an inverse ...
313
314
315
316
317
318
319
320
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  
  		stim::envi_header out_hdr = header;							
  		out_hdr.bands = M;											//set the number of bands in the output header
9d3ba0b1   David Mayerich   added stim::hsi a...
321
322
  		out_hdr.wavelength.clear();
  		out_hdr.band_names.clear();
63fc1df8   David Mayerich   added an inverse ...
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
  		out_hdr.save(outfile + ".hdr");								//save the output header
  	}
  
  	void inverse(std::string outfile, double* center, double* basis, unsigned long long B, unsigned long long C = 0, bool PROGRESS = false){
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			std::cout<<"ERROR: BSQ projection not supported"<<std::endl;
  			exit(1);
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			std::cout<<"ERROR: BIL projection not supported"<<std::endl;
  			exit(1);
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
  				((bip<float>*)file)->inverse(outfile, center, basis, B, C, PROGRESS);
  			else if(header.data_type == envi_header::float64)
  				((bip<double>*)file)->inverse(outfile, center, basis, B, C, PROGRESS);
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  
  		stim::envi_header out_hdr = header;							
  		out_hdr.bands = B;											//set the number of bands in the output header
  		out_hdr.save(outfile + ".hdr");								//save the output header
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
353
354
355
356
  	/// Converts ENVI files between interleave types (BSQ, BIL, and BIP)
  
  	/// @param outfile is the file name for the converted output
  	/// @param interleave is the interleave format for the destination file
63fc1df8   David Mayerich   added an inverse ...
357
  	bool convert(std::string outfile, stim::envi_header::interleaveType interleave, bool PROGRESS = false){
a2bf1d08   David Mayerich   general bug fixes...
358
  		
6708cc25   heziqi   Ziqi added envi c...
359
360
  		if(header.interleave == envi_header::BSQ){			//if the infile is bsq file
  
a2bf1d08   David Mayerich   general bug fixes...
361
  			if(header.data_type ==envi_header::float32){		//ERROR
6708cc25   heziqi   Ziqi added envi c...
362
363
364
365
  				if(interleave == envi_header::BSQ){
  					std::cout<<"ERROR:  is already BSQ file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
366
367
368
  				else if(interleave == envi_header::BIL)			//convert BSQ -> BIL
  					((bsq<float>*)file)->bil(outfile, PROGRESS);
  				else if(interleave == envi_header::BIP){			//ERROR
63fc1df8   David Mayerich   added an inverse ...
369
370
371
372
  					std::cout<<"ERROR: conversion from BSQ to BIP isn't practical; use BSQ->BIL->BIP instead"<<std::endl;
  					//return ((bsq<float>*)file)->bip(outfile, PROGRESS);
  					exit(1);
  				}
6708cc25   heziqi   Ziqi added envi c...
373
374
375
  			}
  
  			else if(header.data_type == envi_header::float64){		//if the data type is float
a2bf1d08   David Mayerich   general bug fixes...
376
  				if(interleave == envi_header::BSQ){							//ERROR
6708cc25   heziqi   Ziqi added envi c...
377
378
379
  					std::cout<<"ERROR:  is already BSQ file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
380
381
382
  				else if(interleave == envi_header::BIL)					//convert BSQ -> BIL
  					((bsq<double>*)file)->bil(outfile, PROGRESS);
  				else if(interleave == envi_header::BIP){					//ERROR
63fc1df8   David Mayerich   added an inverse ...
383
384
385
386
  					std::cout<<"ERROR: conversion from BSQ to BIP isn't practical; use BSQ->BIL->BIP instead"<<std::endl;
  					//return ((bsq<float>*)file)->bip(outfile, PROGRESS);
  					exit(1);
  				}
6708cc25   heziqi   Ziqi added envi c...
387
  			}
f92397d2   David Mayerich   fixed a Win32 bug...
388
  
6708cc25   heziqi   Ziqi added envi c...
389
390
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
f92397d2   David Mayerich   fixed a Win32 bug...
391
  				exit(1);
6708cc25   heziqi   Ziqi added envi c...
392
393
394
395
396
  			}
  		}
  
  		else if(header.interleave == envi_header::BIL){
  
a2bf1d08   David Mayerich   general bug fixes...
397
  			if(header.data_type ==envi_header::float32){						//ERROR
6708cc25   heziqi   Ziqi added envi c...
398
399
400
401
  				if(interleave == envi_header::BIL){
  					std::cout<<"ERROR:  is already BIL file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
402
403
404
405
  				else if(interleave == envi_header::BSQ)							//BIL -> BSQ
  					((bil<float>*)file)->bsq(outfile, PROGRESS);
  				else if(interleave == envi_header::BIP)							//BIL -> BIP
  					((bil<float>*)file)->bip(outfile, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
406
407
  			}
  
a2bf1d08   David Mayerich   general bug fixes...
408
409
  			else if(header.data_type == envi_header::float64){
  				if(interleave == envi_header::BIL){								//ERROR
6708cc25   heziqi   Ziqi added envi c...
410
411
412
  					std::cout<<"ERROR:  is already BIL file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
413
414
415
416
  				else if(interleave == envi_header::BSQ)							//BIL -> BSQ
  					((bil<double>*)file)->bsq(outfile, PROGRESS);
  				else if(interleave == envi_header::BIP)							//BIL -> BIP
  					((bil<double>*)file)->bip(outfile, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
417
  			}
f92397d2   David Mayerich   fixed a Win32 bug...
418
  
6708cc25   heziqi   Ziqi added envi c...
419
420
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
f92397d2   David Mayerich   fixed a Win32 bug...
421
  				exit(1);
6708cc25   heziqi   Ziqi added envi c...
422
423
424
425
  			}
  		}
  
  		else if(header.interleave == envi_header::BIP){
f92397d2   David Mayerich   fixed a Win32 bug...
426
  
a2bf1d08   David Mayerich   general bug fixes...
427
428
  			if(header.data_type ==envi_header::float32){
  				if(interleave == envi_header::BIP){								//ERROR
6708cc25   heziqi   Ziqi added envi c...
429
430
431
  					std::cout<<"ERROR:  is already BIP file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
432
433
434
  				else if(interleave == envi_header::BIL)							//BIP -> BIL
  					((bip<float>*)file)->bil(outfile, PROGRESS);
  				else if(interleave == envi_header::BSQ){						//ERROR
63fc1df8   David Mayerich   added an inverse ...
435
436
437
438
  					std::cout<<"ERROR: conversion from BIP to BSQ isn't practical; use BIP->BIL->BSQ instead"<<std::endl;
  					//return ((bsq<float>*)file)->bip(outfile, PROGRESS);
  					exit(1);
  				}
6708cc25   heziqi   Ziqi added envi c...
439
440
  			}
  
a2bf1d08   David Mayerich   general bug fixes...
441
442
  			else if(header.data_type == envi_header::float64){
  				if(interleave == envi_header::BIP){								//ERROR
6708cc25   heziqi   Ziqi added envi c...
443
444
445
  					std::cout<<"ERROR:  is already BIP file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
446
447
448
  				else if(interleave == envi_header::BIL)							//BIP -> BIL
  					((bip<double>*)file)->bil(outfile, PROGRESS);
  				else if(interleave == envi_header::BSQ){						//ERROR
63fc1df8   David Mayerich   added an inverse ...
449
450
451
452
  					std::cout<<"ERROR: conversion from BIP to BSQ isn't practical; use BIP->BIL->BSQ instead"<<std::endl;
  					//return ((bsq<float>*)file)->bip(outfile, PROGRESS);
  					exit(1);
  				}
6708cc25   heziqi   Ziqi added envi c...
453
  			}
f92397d2   David Mayerich   fixed a Win32 bug...
454
  
6708cc25   heziqi   Ziqi added envi c...
455
456
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
f92397d2   David Mayerich   fixed a Win32 bug...
457
  				exit(1);
6708cc25   heziqi   Ziqi added envi c...
458
459
  			}
  		}
f92397d2   David Mayerich   fixed a Win32 bug...
460
  
6708cc25   heziqi   Ziqi added envi c...
461
462
463
464
  		else{
  			std::cout<<"ERROR: unidentified interleave type"<<std::endl;
  			exit(1);
  		}
a2bf1d08   David Mayerich   general bug fixes...
465
466
467
468
469
  		stim::envi_header h = header;
  		h.interleave = interleave;
  		h.save(outfile + ".hdr");
  
  		return true;
f92397d2   David Mayerich   fixed a Win32 bug...
470
  
6708cc25   heziqi   Ziqi added envi c...
471
472
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
473
474
475
476
477
  	/// Builds a mask from a band image and threshold value
  
  	/// @param mask_band is the label for the band that will be used to build the mask
  	/// @param threshold is a value selected such that all band values greater than threshold will have a mask value of 'true'
  	/// @param p is memory of size X*Y that will store the resulting mask
ba51ae6a   David Mayerich   fixed metric calc...
478
  	bool build_mask(unsigned char* mask, double mask_band, double threshold, bool PROGRESS = false)	{
4a6f666c   heziqi   Added mask method
479
480
481
  
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
482
  				return ((bsq<float>*)file)->build_mask(mask, mask_band, threshold, PROGRESS);
4a6f666c   heziqi   Added mask method
483
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
484
  				return ((bsq<double>*)file)->build_mask(mask, mask_band, threshold, PROGRESS);
4a6f666c   heziqi   Added mask method
485
486
487
488
489
490
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
491
  				return ((bil<float>*)file)->build_mask(mask, mask_band, threshold, PROGRESS);
4a6f666c   heziqi   Added mask method
492
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
493
  				return ((bil<double>*)file)->build_mask(mask, mask_band, threshold, PROGRESS);
4a6f666c   heziqi   Added mask method
494
495
496
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
cacc3cdc   heziqi   attempt to fix CImg
497
  
4a6f666c   heziqi   Added mask method
498
499
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
500
  				return ((bip<float>*)file)->build_mask(mask, mask_band, threshold, PROGRESS);
4a6f666c   heziqi   Added mask method
501
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
502
  				return ((bip<double>*)file)->build_mask(mask, mask_band, threshold, PROGRESS);
4a6f666c   heziqi   Added mask method
503
504
505
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
bba4f4d9   David Mayerich   minor warning fixes
506
507
  
  		return false;
740f8cd2   heziqi   added apply_mask
508
509
  	}
  
ba51ae6a   David Mayerich   fixed metric calc...
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
  	/// Creates a mask with a true value for all pixels that contain finite values
  	void mask_finite(unsigned char* mask, bool PROGRESS = false){
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
  				((bsq<float>*)file)->mask_finite(mask, PROGRESS);
  			else if(header.data_type == envi_header::float64)
  				((bsq<double>*)file)->mask_finite(mask, PROGRESS);
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
  				((bil<float>*)file)->mask_finite(mask, PROGRESS);
  			else if(header.data_type == envi_header::float64)
  				((bil<double>*)file)->mask_finite(mask, PROGRESS);
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
  				((bip<float>*)file)->mask_finite(mask, PROGRESS);
  			else if(header.data_type == envi_header::float64)
  				((bip<double>*)file)->mask_finite(mask, PROGRESS);
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
540
541
542
543
  	/// Applies a mask to the ENVI file.
  
  	/// @param outfile is the name of the resulting masked output file
  	/// @param p is memory of size X*Y containing the mask (0 = false, all other values are true)
e843658b   Brad Deutsch   Previous push did...
544
  	bool apply_mask(std::string outfile, unsigned char* p)
740f8cd2   heziqi   added apply_mask
545
  	{
a2bf1d08   David Mayerich   general bug fixes...
546
  		header.save(outfile + ".hdr");
e843658b   Brad Deutsch   Previous push did...
547
548
  		if (header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if (header.data_type == envi_header::float32)
740f8cd2   heziqi   added apply_mask
549
  				return ((bsq<float>*)file)->apply_mask(outfile, p);
e843658b   Brad Deutsch   Previous push did...
550
  			else if (header.data_type == envi_header::float64)
740f8cd2   heziqi   added apply_mask
551
552
  				return ((bsq<double>*)file)->apply_mask(outfile, p);
  			else
e843658b   Brad Deutsch   Previous push did...
553
  				std::cout << "ERROR: unidentified data type" << std::endl;
740f8cd2   heziqi   added apply_mask
554
555
  		}
  
e843658b   Brad Deutsch   Previous push did...
556
557
  		else if (header.interleave == envi_header::BIL){		//if the infile is bil file
  			if (header.data_type == envi_header::float32)
740f8cd2   heziqi   added apply_mask
558
  				return ((bil<float>*)file)->apply_mask(outfile, p);
e843658b   Brad Deutsch   Previous push did...
559
  			else if (header.data_type == envi_header::float64)
740f8cd2   heziqi   added apply_mask
560
561
  				return ((bil<double>*)file)->apply_mask(outfile, p);
  			else
e843658b   Brad Deutsch   Previous push did...
562
  				std::cout << "ERROR: unidentified data type" << std::endl;
740f8cd2   heziqi   added apply_mask
563
564
  		}
  
e843658b   Brad Deutsch   Previous push did...
565
566
  		else if (header.interleave == envi_header::BIP){		//if the infile is bip file
  			if (header.data_type == envi_header::float32)
740f8cd2   heziqi   added apply_mask
567
  				return ((bip<float>*)file)->apply_mask(outfile, p);
e843658b   Brad Deutsch   Previous push did...
568
  			else if (header.data_type == envi_header::float64)
740f8cd2   heziqi   added apply_mask
569
570
  				return ((bip<double>*)file)->apply_mask(outfile, p);
  			else
e843658b   Brad Deutsch   Previous push did...
571
  				std::cout << "ERROR: unidentified data type" << std::endl;
740f8cd2   heziqi   added apply_mask
572
  		}
4a6f666c   heziqi   Added mask method
573
574
  
  		else{
e843658b   Brad Deutsch   Previous push did...
575
576
577
578
579
580
  			std::cout << "ERROR: unidentified file type" << std::endl;
  			exit(1);
  		}
  		return false;
  	}
  
3d0b6243   David Mayerich   fixed hsiproc bug...
581
582
583
  	/// Copies all spectra corresponding to nonzero values of a mask into a pre-allocated matrix of size (P x B)
  	///		where P is the number of masked pixels and B is the number of bands. The allocated memory can be accessed
  	///		using the following indexing: i = b*P + p
43dec788   David Mayerich   added code for si...
584
585
586
587
588
589
590
591
592
  	/// @param matrix is the destination for the pixel data
  	/// @param p is the mask
  	bool sift(void* matrix, unsigned char* p){
  
  		if (header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if (header.data_type == envi_header::float32)
  				return ((bsq<float>*)file)->sift((float*)matrix, p);
  			else if (header.data_type == envi_header::float64)
  				return ((bsq<double>*)file)->sift((double*)matrix, p);
ba51ae6a   David Mayerich   fixed metric calc...
593
  			else{
43dec788   David Mayerich   added code for si...
594
  				std::cout << "ERROR: unidentified data type" << std::endl;
ba51ae6a   David Mayerich   fixed metric calc...
595
596
  				exit(1);
  			}
43dec788   David Mayerich   added code for si...
597
  		}
3d0b6243   David Mayerich   fixed hsiproc bug...
598
599
  
  		if (header.interleave == envi_header::BIP){
ba51ae6a   David Mayerich   fixed metric calc...
600
601
602
603
604
605
606
607
  			if (header.data_type == envi_header::float32)
  				return ((bip<float>*)file)->sift((float*)matrix, p);
  			else if (header.data_type == envi_header::float64)
  				return ((bip<double>*)file)->sift((double*)matrix, p);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
3d0b6243   David Mayerich   fixed hsiproc bug...
608
609
  		}
  		if (header.interleave == envi_header::BIL){
ba51ae6a   David Mayerich   fixed metric calc...
610
611
612
613
614
615
616
617
  			if (header.data_type == envi_header::float32)
  				return ((bil<float>*)file)->sift((float*)matrix, p);
  			else if (header.data_type == envi_header::float64)
  				return ((bil<double>*)file)->sift((double*)matrix, p);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
3d0b6243   David Mayerich   fixed hsiproc bug...
618
  		}
43dec788   David Mayerich   added code for si...
619
620
621
622
  		return false;
  
  	}
  
3d0b6243   David Mayerich   fixed hsiproc bug...
623
  	/// Saves in an array only those spectra corresponding to nonzero values of the mask.
43dec788   David Mayerich   added code for si...
624
625
  	/// @param outfile is the name of the sifted output file
  	/// @param p is the mask
c1078e19   David Mayerich   HSIproc bug fixes...
626
  	bool sift(std::string outfile, unsigned char* p, bool PROGRESS = false)
e843658b   Brad Deutsch   Previous push did...
627
628
  	{
  
2fcab4f0   David Mayerich   added unsifting f...
629
  		//calculate the number of non-zero values in the mask
9d3ba0b1   David Mayerich   added stim::hsi a...
630
631
632
  		unsigned long long nnz = 0;
  		unsigned long long npixels = header.lines * header.samples;
  		for(unsigned long long i = 0; i < npixels; i++)
2fcab4f0   David Mayerich   added unsifting f...
633
634
635
636
637
  			if( p[i] > 0 ) nnz++;
  
  		//create a new header
  		envi_header new_header = header;
  
f7e99517   David Mayerich   added sifting for...
638
639
  		//if a BIL file is sifted, it's saved as a BIP
  		if(header.interleave == envi_header::BIL)
f92397d2   David Mayerich   fixed a Win32 bug...
640
  			new_header.interleave = envi_header::BIP;
f7e99517   David Mayerich   added sifting for...
641
  
2fcab4f0   David Mayerich   added unsifting f...
642
643
644
645
646
  		//set the number of lines to 1 (this is a matrix with 1 line and N samples)
  		new_header.lines = 1;
  		new_header.samples = nnz;
  		new_header.save(outfile + ".hdr");
  
e843658b   Brad Deutsch   Previous push did...
647
648
  		if (header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if (header.data_type == envi_header::float32)
c1078e19   David Mayerich   HSIproc bug fixes...
649
  				return ((bsq<float>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
650
  			else if (header.data_type == envi_header::float64)
c1078e19   David Mayerich   HSIproc bug fixes...
651
  				return ((bsq<double>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
652
653
654
655
656
657
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  
  		else if (header.interleave == envi_header::BIL){		//if the infile is bil file
  			if (header.data_type == envi_header::float32)
c1078e19   David Mayerich   HSIproc bug fixes...
658
  				return ((bil<float>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
659
  			else if (header.data_type == envi_header::float64)
c1078e19   David Mayerich   HSIproc bug fixes...
660
  				return ((bil<double>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
661
662
663
664
665
666
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  
  		else if (header.interleave == envi_header::BIP){		//if the infile is bip file
  			if (header.data_type == envi_header::float32)
c1078e19   David Mayerich   HSIproc bug fixes...
667
  				return ((bip<float>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
668
  			else if (header.data_type == envi_header::float64)
c1078e19   David Mayerich   HSIproc bug fixes...
669
  				return ((bip<double>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
670
671
672
673
674
675
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  
  		else{
  			std::cout << "ERROR: unidentified file type" << std::endl;
4a6f666c   heziqi   Added mask method
676
677
  			exit(1);
  		}
4a6f666c   heziqi   Added mask method
678
  		return false;
cacc3cdc   heziqi   attempt to fix CImg
679
680
  	}
  
a2bf1d08   David Mayerich   general bug fixes...
681
  	bool unsift(std::string outfile, unsigned char* mask, unsigned long long samples, unsigned long long lines, bool PROGRESS = false){
2fcab4f0   David Mayerich   added unsifting f...
682
683
684
685
686
687
688
689
690
691
692
693
  
  		//create a new header
  		envi_header new_header = header;
  
  		//set the number of lines and samples in the output file (that's all that changes)
  		new_header.lines = lines;
  		new_header.samples = samples;
  		new_header.save(outfile + ".hdr");
  
  
  		if (header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if (header.data_type == envi_header::float32)
a2bf1d08   David Mayerich   general bug fixes...
694
  				return ((bsq<float>*)file)->unsift(outfile, mask, samples, lines, PROGRESS);
2fcab4f0   David Mayerich   added unsifting f...
695
  			else if (header.data_type == envi_header::float64)
a2bf1d08   David Mayerich   general bug fixes...
696
  				return ((bsq<double>*)file)->unsift(outfile, mask, samples, lines, PROGRESS);
2fcab4f0   David Mayerich   added unsifting f...
697
698
699
700
701
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  
  		else if (header.interleave == envi_header::BIL){		//if the infile is bil file
f92397d2   David Mayerich   fixed a Win32 bug...
702
  
2fcab4f0   David Mayerich   added unsifting f...
703
704
705
706
  				std::cout << "ERROR in stim::envi::unsift - BIL files aren't supported yet" << std::endl;
  		}
  
  		else if (header.interleave == envi_header::BIP){		//if the infile is bip file
f92397d2   David Mayerich   fixed a Win32 bug...
707
  
8550e94f   David Mayerich   added sifting and...
708
  			if (header.data_type == envi_header::float32)
a2bf1d08   David Mayerich   general bug fixes...
709
  				return ((bip<float>*)file)->unsift(outfile, mask, samples, lines, PROGRESS);
8550e94f   David Mayerich   added sifting and...
710
  			else if (header.data_type == envi_header::float64)
a2bf1d08   David Mayerich   general bug fixes...
711
  				return ((bip<double>*)file)->unsift(outfile, mask, samples, lines, PROGRESS);
8550e94f   David Mayerich   added sifting and...
712
713
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
2fcab4f0   David Mayerich   added unsifting f...
714
715
716
717
  		}
  
  		else{
  			std::cout << "ERROR: unidentified file type" << std::endl;
2fcab4f0   David Mayerich   added unsifting f...
718
  		}
cbce396e   David Mayerich   added support for...
719
  		return 0;
2fcab4f0   David Mayerich   added unsifting f...
720
721
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
722
723
724
725
726
727
728
729
730
  	/// Compute the ratio of two baseline-corrected peaks. The result is stored in a pre-allocated array.
  
  	/// @param lb1 is the label value for the left baseline point for the first peak (numerator)
  	/// @param rb1 is the label value for the right baseline point for the first peak (numerator)
  	/// @param pos1 is the label value for the first peak (numerator) position
  	/// @param lb2 is the label value for the left baseline point for the second peak (denominator)
  	/// @param rb2 is the label value for the right baseline point for the second peak (denominator)
  	/// @param pos2 is the label value for the second peak (denominator) position
  	/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
ba51ae6a   David Mayerich   fixed metric calc...
731
  	bool ph_to_ph(void * result, double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, unsigned char* mask){
20c212c0   heziqi   Ziqi added functi...
732
733
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
734
  				return ((bsq<float>*)file)->ph_to_ph((float*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
735
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
736
  				return ((bsq<double>*)file)->ph_to_ph((double*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
737
738
739
740
741
742
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
743
  				return ((bil<float>*)file)->ph_to_ph((float*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
744
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
745
  				return ((bil<double>*)file)->ph_to_ph((double*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
746
747
748
749
750
751
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
752
  				return ((bip<float>*)file)->ph_to_ph((float*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
753
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
754
  				return ((bip<double>*)file)->ph_to_ph((double*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
70407ea9   heziqi   Ziqi completed he...
755
756
757
758
759
760
761
762
763
764
765
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  		return false;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
766
767
768
769
770
771
772
773
774
  	/// Compute the ratio between a peak area and peak height.
  
  	/// @param lb1 is the label value for the left baseline point for the first peak (numerator)
  	/// @param rb1 is the label value for the right baseline point for the first peak (numerator)
  	/// @param pos1 is the label value for the first peak (numerator) position
  	/// @param lb2 is the label value for the left baseline point for the second peak (denominator)
  	/// @param rb2 is the label value for the right baseline point for the second peak (denominator)
  	/// @param pos2 is the label value for the second peak (denominator) position
  	/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
ba51ae6a   David Mayerich   fixed metric calc...
775
  	bool pa_to_ph(void* result, double lb1, double rb1, double lab1, double rab1, double lb2, double rb2, double pos, unsigned char* mask = NULL){
70407ea9   heziqi   Ziqi completed he...
776
777
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
778
  				return ((bsq<float>*)file)->pa_to_ph((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
779
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
780
  				return ((bsq<double>*)file)->pa_to_ph((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
781
782
783
784
785
786
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
787
  				return ((bil<float>*)file)->pa_to_ph((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
788
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
789
  				return ((bil<double>*)file)->pa_to_ph((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
790
791
792
793
794
795
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
796
  				return ((bip<float>*)file)->pa_to_ph((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
797
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
798
  				return ((bip<double>*)file)->pa_to_ph((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
799
800
801
802
803
804
805
806
807
808
809
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  		return false;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
810
811
812
813
814
815
816
817
818
  	/// Compute the ratio between two peak areas.
  
  	/// @param lb1 is the label value for the left baseline point for the first peak (numerator)
  	/// @param rb1 is the label value for the right baseline point for the first peak (numerator)
  	/// @param lab1 is the label value for the left bound (start of the integration) of the first peak (numerator)
  	/// @param rab1 is the label value for the right bound (end of the integration) of the first peak (numerator)
  	/// @param lb2 is the label value for the left baseline point for the second peak (denominator)
  	/// @param rb2 is the label value for the right baseline point for the second peak (denominator)
  	/// @param lab2 is the label value for the left bound (start of the integration) of the second peak (denominator)
f92397d2   David Mayerich   fixed a Win32 bug...
819
  	/// @param rab2 is the label value for the right bound (end of the integration) of the second peak (denominator)
cb455f98   David Mayerich   removed old ENVI ...
820
  	/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
ba51ae6a   David Mayerich   fixed metric calc...
821
822
  	bool pa_to_pa(void* result, double lb1, double rb1, double lab1, double rab1,
  					double lb2, double rb2, double lab2, double rab2, unsigned char* mask = NULL){
70407ea9   heziqi   Ziqi completed he...
823
824
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
825
  				return ((bsq<float>*)file)->pa_to_pa((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
826
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
827
  				return ((bsq<double>*)file)->pa_to_pa((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
828
829
830
831
832
833
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
834
  				return ((bil<float>*)file)->pa_to_pa((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
835
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
836
  				return ((bil<double>*)file)->pa_to_pa((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
837
838
839
840
841
842
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
843
  				return ((bip<float>*)file)->pa_to_pa((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
844
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
845
  				return ((bip<double>*)file)->pa_to_pa((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
20c212c0   heziqi   Ziqi added functi...
846
847
848
849
850
851
852
853
854
855
856
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  		return false;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
857
858
859
860
861
862
863
  	/// Compute the centroid of a baseline corrected peak.
  
  	/// @param lb is the label value for the left baseline point
  	/// @param rb is the label value for the right baseline point
  	/// @param lab is the label for the start of the peak
  	/// @param rab is the label for the end of the peak
  	/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
ba51ae6a   David Mayerich   fixed metric calc...
864
  	bool centroid(void* result, double lb1, double rb1, double lab1, double rab1, unsigned char* mask = NULL){
517876d6   heziqi   metrics finished ...
865
866
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
867
  				return ((bsq<float>*)file)->centroid((float*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
868
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
869
  				return ((bsq<double>*)file)->centroid((double*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
870
871
872
873
874
875
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
876
  				return ((bil<float>*)file)->centroid((float*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
877
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
878
  				return ((bil<double>*)file)->centroid((double*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
879
880
881
882
883
884
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
885
  				return ((bip<float>*)file)->centroid((float*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
886
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
887
  				return ((bip<double>*)file)->centroid((double*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
888
889
890
891
892
893
894
895
896
897
898
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  		return false;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
899
  	/// Closes the ENVI file.
6708cc25   heziqi   Ziqi added envi c...
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
  	bool close(){
  		if(header.interleave == envi_header::BSQ){
  			if(header.data_type ==envi_header::float32)
  				return ((bsq<float>*)file)->close();
  			else if(header.data_type == envi_header::float64)
  				return ((bsq<double>*)file)->close();
  			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)
  				return ((bil<float>*)file)->close();
  			else if(header.data_type == envi_header::float64)
  				return ((bil<double>*)file)->close();
  			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)
  				return ((bip<float>*)file)->close();
  			else if(header.data_type == envi_header::float64)
  				return ((bip<double>*)file)->close();
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
e933c14e   David Mayerich   cleaned up the code
933
  		return false;
e8eb202f   David Mayerich   added a new ENVI ...
934
  	}
cb455f98   David Mayerich   removed old ENVI ...
935
936
937
938
939
  
  	/// Retrieve a single pixel and stores it in pre-allocated memory.
  
  	/// @param p is a pointer to pre-allocated memory at least sizeof(T) in size.
  	/// @param n is an integer index to the pixel using linear array indexing.
bfe9c6db   heziqi   added feature_mat...
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
  	bool pixel(void * p, unsigned n){
  		if(header.interleave == envi_header::BSQ){
  			if(header.data_type ==envi_header::float32)
  				return ((bsq<float>*)file)->pixel((float*)p, n);
  			else if(header.data_type == envi_header::float64)
  				return ((bsq<double>*)file)->pixel((double*)p, n);
  			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)
  				return ((bil<float>*)file)->pixel((float*)p, n);
  			else if(header.data_type == envi_header::float64)
  				return ((bil<double>*)file)->pixel((double*)p, n);
  			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)
  				return ((bip<float>*)file)->pixel((float*)p, n);
  			else if(header.data_type == envi_header::float64)
  				return ((bip<double>*)file)->pixel((double*)p, n);
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
e8eb202f   David Mayerich   added a new ENVI ...
973
  
cb455f98   David Mayerich   removed old ENVI ...
974
  	/// Saves a header file describing the current ENVI file parameters.
e8eb202f   David Mayerich   added a new ENVI ...
975
976
977
978
979
980
981
982
  	bool save_header(std::string filename){
  
  		//save the header file here
  		header.save(filename);
  
  		return true;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
983
984
985
986
  	/// Retrieve a single band (by numerical label) and stores it in pre-allocated memory.
  
  	/// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size.
  	/// @param wavelength is a floating point value (usually a wavelength in spectral data) used as a label for the band to be copied.
63fc1df8   David Mayerich   added an inverse ...
987
  	bool band(void* ptr, double wavelength, bool PROGRESS = false){
d1d8ef91   David Mayerich   added band readin...
988
989
990
  
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
991
  				return ((bsq<float>*)file)->band((float*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
992
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
993
  				return ((bsq<double>*)file)->band((double*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
994
995
996
997
998
999
1000
  			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)
63fc1df8   David Mayerich   added an inverse ...
1001
  				return ((bil<float>*)file)->band((float*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1002
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1003
  				return ((bil<double>*)file)->band((double*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1004
1005
1006
1007
1008
1009
1010
  			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)
63fc1df8   David Mayerich   added an inverse ...
1011
  				return ((bip<float>*)file)->band((float*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1012
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1013
  				return ((bip<double>*)file)->band((double*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1014
1015
1016
1017
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
d1d8ef91   David Mayerich   added band readin...
1018
  		}
ee4dea28   David Mayerich   fixed errors in c...
1019
  		return false;
1582ce07   David Mayerich   added band_index(...
1020
1021
  	}
  
27b826a8   David Mayerich   added a spherical...
1022
1023
1024
1025
1026
  	/// Retrieve a spectrum from the specified 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
9d3ba0b1   David Mayerich   added stim::hsi a...
1027
  	bool spectrum(void* ptr, unsigned long long x, unsigned long long y, bool PROGRESS = false){
724ec347   David Mayerich   simplified the EN...
1028
1029
1030
  
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
1031
  				return ((bsq<float>*)file)->spectrum((float*)ptr, x, y, PROGRESS);
724ec347   David Mayerich   simplified the EN...
1032
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1033
  				return ((bsq<double>*)file)->spectrum((double*)ptr, x, y, PROGRESS);
724ec347   David Mayerich   simplified the EN...
1034
1035
1036
1037
1038
1039
1040
  			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)
63fc1df8   David Mayerich   added an inverse ...
1041
  				return ((bil<float>*)file)->spectrum((float*)ptr, x, y, PROGRESS);
724ec347   David Mayerich   simplified the EN...
1042
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1043
  				return ((bil<double>*)file)->spectrum((double*)ptr, x, y, PROGRESS);
724ec347   David Mayerich   simplified the EN...
1044
1045
1046
1047
1048
1049
1050
  			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)
63fc1df8   David Mayerich   added an inverse ...
1051
  				return ((bip<float>*)file)->spectrum((float*)ptr, x, y, PROGRESS);
724ec347   David Mayerich   simplified the EN...
1052
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1053
  				return ((bip<double>*)file)->spectrum((double*)ptr, x, y, PROGRESS);
724ec347   David Mayerich   simplified the EN...
1054
1055
1056
1057
1058
1059
1060
1061
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
1062
1063
1064
1065
  	/// Retrieve a single band (based on index) and stores it in pre-allocated memory.
  
  	/// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size.
  	/// @param page <= B is the integer number of the band to be copied.
9d3ba0b1   David Mayerich   added stim::hsi a...
1066
  	bool band_index(void* ptr, unsigned long long b){
081f2c9f   heziqi   made bip bil file...
1067
1068
  		if (header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if (header.data_type == envi_header::float32)
1582ce07   David Mayerich   added band_index(...
1069
  				return ((bsq<float>*)file)->band_index((float*)ptr, b);
081f2c9f   heziqi   made bip bil file...
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
  			else if (header.data_type == envi_header::float64)
  				return ((bsq<double>*)file)->band_index((double*)ptr, b);
  			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)
  				return ((bil<float>*)file)->band_index((float*)ptr, b);
  			else if (header.data_type == envi_header::float64)
  				return ((bil<double>*)file)->band_index((double*)ptr, b);
  			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)
  				return ((bip<float>*)file)->band_index((float*)ptr, b);
  			else if (header.data_type == envi_header::float64)
  				return ((bip<double>*)file)->band_index((double*)ptr, b);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
1582ce07   David Mayerich   added band_index(...
1096
1097
  		}
  		return false;
d1d8ef91   David Mayerich   added band readin...
1098
  	}
f92397d2   David Mayerich   fixed a Win32 bug...
1099
  
cb455f98   David Mayerich   removed old ENVI ...
1100
1101
1102
1103
  	/// Calculate the mean value for all masked (or valid) pixels in a band and returns the average spectrum
  
  	/// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum
  	/// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
63fc1df8   David Mayerich   added an inverse ...
1104
  	bool avg_band(double * p, unsigned char* mask, bool PROGRESS = false){
a43c4fe1   heziqi   Added crop in env...
1105
1106
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
1107
  				return ((bsq<float>*)file)->avg_band(p, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1108
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1109
  				return ((bsq<double>*)file)->avg_band(p,  mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1110
1111
1112
1113
1114
1115
1116
  			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)
63fc1df8   David Mayerich   added an inverse ...
1117
  				return ((bil<float>*)file)->avg_band(p,  mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1118
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1119
  				return ((bil<double>*)file)->avg_band(p,  mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1120
1121
1122
1123
1124
1125
1126
  			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)
63fc1df8   David Mayerich   added an inverse ...
1127
  				return ((bip<float>*)file)->avg_band(p, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1128
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1129
  				return ((bip<double>*)file)->avg_band(p, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1130
1131
1132
1133
1134
1135
1136
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
e8eb202f   David Mayerich   added a new ENVI ...
1137
  
cb455f98   David Mayerich   removed old ENVI ...
1138
1139
1140
1141
1142
  	/// Calculate the covariance matrix for all masked pixels in the image.
  
  	/// @param co is a pointer to pre-allocated memory of size [B * B] that stores the resulting covariance matrix
  	/// @param avg is a pointer to memory of size B that stores the average spectrum
  	/// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
63fc1df8   David Mayerich   added an inverse ...
1143
  	bool co_matrix(double* co, double* avg, unsigned char* mask, bool PROGRESS = false){
a43c4fe1   heziqi   Added crop in env...
1144
  		if (header.interleave == envi_header::BSQ){
63fc1df8   David Mayerich   added an inverse ...
1145
1146
1147
1148
  			std::cout<<"ERROR: calculating the covariance matrix for a BSQ file is impractical; convert to BIL or BIP first"<<std::endl;
  			exit(1);
  			/*if (header.data_type == envi_header::float32)
  				return ((bsq<float>*)file)->co_matrix(co, avg, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1149
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1150
  				return ((bsq<double>*)file)->co_matrix(co, avg, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1151
1152
1153
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
63fc1df8   David Mayerich   added an inverse ...
1154
  			}*/
a43c4fe1   heziqi   Added crop in env...
1155
1156
1157
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
1158
  				return ((bil<float>*)file)->co_matrix(co, avg, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1159
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1160
  				return ((bil<double>*)file)->co_matrix(co, avg, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1161
1162
1163
1164
1165
1166
1167
  			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)
63fc1df8   David Mayerich   added an inverse ...
1168
  				return ((bip<float>*)file)->co_matrix(co, avg, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1169
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1170
  				return ((bip<double>*)file)->co_matrix(co, avg, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1171
1172
1173
1174
1175
1176
1177
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
0df38ff3   heziqi   fixed head detached
1178
  
e8eb202f   David Mayerich   added a new ENVI ...
1179
  
cb455f98   David Mayerich   removed old ENVI ...
1180
1181
1182
1183
1184
1185
1186
  	/// Crop a region of the image and save it to a new file.
  
  	/// @param outfile is the file name for the new cropped image
  	/// @param x0 is the lower-left x pixel coordinate to be included in the cropped image
  	/// @param y0 is the lower-left y pixel coordinate to be included in the cropped image
  	/// @param x1 is the upper-right x pixel coordinate to be included in the cropped image
  	/// @param y1 is the upper-right y pixel coordinate to be included in the cropped image
c8c976a9   David Mayerich   replaced CImg wit...
1187
1188
1189
1190
1191
1192
1193
1194
  	bool crop(std::string outfile,
  			  unsigned long long x0, 
  			  unsigned long long y0, 
  			  unsigned long long x1, 
  			  unsigned long long y1, 
  			  unsigned long long b0, 
  			  unsigned long long b1, 
  			  bool PROGRESS = false){
a43c4fe1   heziqi   Added crop in env...
1195
  
177c7863   David Mayerich   updated the ENVI ...
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
  		//save the header for the cropped file
  		stim::envi_header new_header = header;
  		new_header.samples = x1 - x0;
  		new_header.lines = y1 - y0;
  		new_header.bands = b1 - b0;
  		std::vector<double>::const_iterator first = new_header.wavelength.begin() + b0;
  		std::vector<double>::const_iterator last = new_header.wavelength.begin() + b1;
  		new_header.wavelength = std::vector<double>(first, last);
  		new_header.save(outfile + ".hdr");
  
a43c4fe1   heziqi   Added crop in env...
1206
1207
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
1208
  				return ((bsq<float>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1209
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1210
  				return ((bsq<double>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1211
1212
1213
1214
1215
1216
1217
  			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)
63fc1df8   David Mayerich   added an inverse ...
1218
  				return ((bil<float>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1219
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1220
  				return ((bil<double>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1221
1222
1223
1224
1225
1226
1227
  			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)
63fc1df8   David Mayerich   added an inverse ...
1228
  				return ((bip<float>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1229
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1230
  				return ((bip<double>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1231
1232
1233
1234
1235
1236
1237
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
0df38ff3   heziqi   fixed head detached
1238
  
e8eb202f   David Mayerich   added a new ENVI ...
1239
1240
1241
1242
1243
  };
  
  }	//end namespace rts
  
  #endif