Blame view

stim/envi/envi.h 63.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"
9b2a5d71   David Mayerich   implemented finit...
8
  #include "../math/fd_coefficients.h"
bfe9c6db   heziqi   added feature_mat...
9
  #include <iostream>
48f356fa   David Mayerich   bug fixes for FFT...
10
  #include <fstream>
2ed641f0   David Mayerich   removed the depen...
11
  //#include "../image/image.h"
e8eb202f   David Mayerich   added a new ENVI ...
12
  
8a86bd56   David Mayerich   changed rts names...
13
  namespace stim{
e8eb202f   David Mayerich   added a new ENVI ...
14
  
cb455f98   David Mayerich   removed old ENVI ...
15
16
17
18
19
  /** 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...
20
  class envi{
e8eb202f   David Mayerich   added a new ENVI ...
21
22
  
  	void* file;		//void pointer to the relevant file reader (bip, bsq, or bil - with appropriate data type)
8057237a   David Mayerich   added an envi::op...
23
  	std::string fname;					//file name used for repeated opening and closing
e8eb202f   David Mayerich   added a new ENVI ...
24
  
735a2a24   David Mayerich   started testing o...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  	//allocate sufficient space for a spectrum based on the data type and number of bands
  	void* alloc_array(size_t len){
  		switch(header.data_type){
  		case envi_header::int8:
  			return malloc(len);
  		case envi_header::int16:
  		case envi_header::uint16:
  			return malloc(2 * len);
  		case envi_header::int32:
  		case envi_header::uint32:
  		case envi_header::float32:
  			return malloc(4 * len);
  		case envi_header::int64:
  		case envi_header::uint64:
  		case envi_header::float64:
  		case envi_header::complex32:
  			return malloc(8 * len);
  		case envi_header::complex64:
  			return malloc(16 * len);
  		default:
  			std::cout<<"ERROR stim::envi data type not recognized for spectral allocation"<<std::endl;
  			exit(1);
  		}
  	}
  
  	//cast a value to DEST type from SRC type
  	template<typename DST, typename SRC>
  	inline void cast(DST* dst, SRC* src){
  		(*dst) = (DST)(*src);
  	}
  
  	//cast an array from type SRC to type DEST
  	template<typename DST, typename SRC>
  	inline void cast(DST* dst, SRC* src, size_t len){
  		for(size_t i = 0; i < len; i++)
  			cast(&dst[i], &src[i]);
  	}
  
e8eb202f   David Mayerich   added a new ENVI ...
63
64
  public:
  
edb975b0   David Mayerich   created a stim::e...
65
66
67
68
69
  	/// Default constructor
  	envi(){
  		file = NULL;				//set the file pointer to NULL
  	}
  
4a6f666c   heziqi   Added mask method
70
71
  	envi_header header;
  
735a2a24   David Mayerich   started testing o...
72
73
74
75
76
77
78
79
  	void* malloc_spectrum(){
  		return alloc_array(header.bands);
  	}
  
  	void* malloc_band(){
  		return alloc_array(header.samples * header.lines);
  	}
  
f92397d2   David Mayerich   fixed a Win32 bug...
80
81
82
83
84
85
86
87
  	/// 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...
88
89
90
91
92
  	/// 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 ...
93
  				((bsq<float>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
94
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
95
  				((bsq<double>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
96
97
98
99
100
101
  			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 ...
102
  				((bil<float>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
103
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
104
  				((bil<double>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
105
106
107
108
109
110
  			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 ...
111
  				((bip<float>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
112
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
113
  				((bip<double>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
114
115
116
117
118
119
120
121
122
123
124
  			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...
125
  	double progress(){
cbce396e   David Mayerich   added support for...
126
127
128
  
  		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 ...
129
  				return ((bsq<float>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
130
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
131
  				return ((bsq<double>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
132
133
134
135
136
137
  			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 ...
138
  				return ((bil<float>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
139
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
140
  				return ((bil<double>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
141
142
143
144
145
146
  			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 ...
147
  				return ((bip<float>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
148
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
149
  				return ((bip<double>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
150
151
152
153
154
155
156
157
158
159
  			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 ...
160
  	/// 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...
161
  	void allocate(){
e8eb202f   David Mayerich   added a new ENVI ...
162
163
164
  
  		file = NULL;	//set file to a NULL pointer
  
6708cc25   heziqi   Ziqi added envi c...
165
  		if(header.interleave == envi_header::BSQ){
e8eb202f   David Mayerich   added a new ENVI ...
166
  			if(header.data_type ==envi_header::float32)
9d3ba0b1   David Mayerich   added stim::hsi a...
167
  				file = new bsq<float>();
6708cc25   heziqi   Ziqi added envi c...
168
  			else if(header.data_type == envi_header::float64)
9d3ba0b1   David Mayerich   added stim::hsi a...
169
  				file = new bsq<double>();
6708cc25   heziqi   Ziqi added envi c...
170
171
172
  		}
  		else if(header.interleave == envi_header::BIP){
  			if(header.data_type ==envi_header::float32)
9d3ba0b1   David Mayerich   added stim::hsi a...
173
  				file = new bip<float>();
6708cc25   heziqi   Ziqi added envi c...
174
  			else if(header.data_type == envi_header::float64)
9d3ba0b1   David Mayerich   added stim::hsi a...
175
  				file = new bip<double>();
6708cc25   heziqi   Ziqi added envi c...
176
177
178
  		}
  		else if(header.interleave == envi_header::BIL){
  			if(header.data_type ==envi_header::float32)
9d3ba0b1   David Mayerich   added stim::hsi a...
179
  				file = new bil<float>();
6708cc25   heziqi   Ziqi added envi c...
180
  			else if(header.data_type == envi_header::float64)
9d3ba0b1   David Mayerich   added stim::hsi a...
181
  				file = new bil<double>();
6708cc25   heziqi   Ziqi added envi c...
182
  		}
e8eb202f   David Mayerich   added a new ENVI ...
183
  
e8eb202f   David Mayerich   added a new ENVI ...
184
185
  	}
  
8057237a   David Mayerich   added an envi::op...
186
187
  	/// Open a previously opened ENVI file
  	bool open(){
e8eb202f   David Mayerich   added a new ENVI ...
188
189
  
  		//load the file
6708cc25   heziqi   Ziqi added envi c...
190
191
  		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...
192
  				return ((bsq<float>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
193
194
  			}
  			else if(header.data_type == envi_header::float64) {
8057237a   David Mayerich   added an envi::op...
195
  				return ((bsq<double>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
196
197
198
199
  			}
  			else
  				return false;
  		}
e8eb202f   David Mayerich   added a new ENVI ...
200
  
6708cc25   heziqi   Ziqi added envi c...
201
202
  		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...
203
  				return ((bil<float>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
204
205
  			}
  			else if(header.data_type == envi_header::float64) {
8057237a   David Mayerich   added an envi::op...
206
  				return ((bil<double>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
207
208
209
210
  			}
  			else
  				return false;
  		}
f92397d2   David Mayerich   fixed a Win32 bug...
211
  
6708cc25   heziqi   Ziqi added envi c...
212
213
  		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...
214
  				return ((bip<float>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
215
216
  			}
  			else if(header.data_type == envi_header::float64) {
8057237a   David Mayerich   added an envi::op...
217
  				return ((bip<double>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
218
219
  			}
  			else
f92397d2   David Mayerich   fixed a Win32 bug...
220
  				return false;
6708cc25   heziqi   Ziqi added envi c...
221
222
  		}
  
f92397d2   David Mayerich   fixed a Win32 bug...
223
224
  		return true;
  
8057237a   David Mayerich   added an envi::op...
225
226
227
  
  	}
  
48f356fa   David Mayerich   bug fixes for FFT...
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  	/// Open an Agilent binary file as an ENVI stream
  	bool open_agilent(std::string filename){
  		fname = filename;												//store the file name
  
  		//Open the file temporarily to get the header information
  		FILE* f = fopen(filename.c_str(), "r");							//open the binary file for reading
  		if(f == NULL) return false;										//return false if no file is opened
  
  		fseek(f, 9, SEEK_SET);											//seek to the number of bands
  		short b;														//allocate space for the number of bands
  		fread(&b, sizeof(short), 1, f);									//read the number of bands
  		fseek(f, 13, SEEK_CUR);											//skip the the x and y dimensions
  		short x, y;
  		fread(&x, sizeof(short), 1, f);									//read the image x and y size
  		fread(&y, sizeof(short), 1, f);
  		fclose(f);														//close the file
  
  		//store the information from the Agilent header in the ENVI header
  		header.bands = b;
  		header.samples = x;
  		header.lines = y;
  		header.data_type = envi_header::float32;						//all values are 32-bit floats
  		header.header_offset = 1020;									//number of bytes in an Agilent binary header
  		header.interleave = envi_header::BSQ;							//all Agilent binary files are BSQ
  
  		allocate();														//allocate the streaming file object
  		open();															//open the file for streaming
  
  		return true;
  	}
  
8057237a   David Mayerich   added an envi::op...
259
260
261
262
263
  	/// 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...
264
  		
8057237a   David Mayerich   added an envi::op...
265
266
267
268
  
  		header = h;							//store the header
  		fname = filename;					//save the filename
  
9d3ba0b1   David Mayerich   added stim::hsi a...
269
270
  		allocate();
  
8057237a   David Mayerich   added an envi::op...
271
272
273
  		return open();						//open the ENVI file;
  		
  
f92397d2   David Mayerich   fixed a Win32 bug...
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  	}
  
  	/// 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 ...
292
293
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
294
295
296
297
298
  	/// 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)
c30dd3e6   David Mayerich   added vector norm...
299
  	bool ratio(std::string outfile, double band, unsigned char* mask = NULL, bool PROGRESS = false){
a2bf1d08   David Mayerich   general bug fixes...
300
  		header.save(outfile + ".hdr");
6708cc25   heziqi   Ziqi added envi c...
301
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
e8eb202f   David Mayerich   added a new ENVI ...
302
  			if(header.data_type ==envi_header::float32)
c30dd3e6   David Mayerich   added vector norm...
303
  				return ((bsq<float>*)file)->ratio(outfile, band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
304
  			else if(header.data_type == envi_header::float64)
c30dd3e6   David Mayerich   added vector norm...
305
  				return ((bsq<double>*)file)->ratio(outfile,band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
306
307
308
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
e8eb202f   David Mayerich   added a new ENVI ...
309
  
6708cc25   heziqi   Ziqi added envi c...
310
311
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
c30dd3e6   David Mayerich   added vector norm...
312
  				return ((bil<float>*)file)->ratio(outfile, band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
313
  			else if(header.data_type == envi_header::float64)
c30dd3e6   David Mayerich   added vector norm...
314
  				return ((bil<double>*)file)->ratio(outfile,band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
315
316
317
318
319
320
  			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)
c30dd3e6   David Mayerich   added vector norm...
321
  				return ((bip<float>*)file)->ratio(outfile, band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
322
  			else if(header.data_type == envi_header::float64)
c30dd3e6   David Mayerich   added vector norm...
323
  				return ((bip<double>*)file)->ratio(outfile,band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
324
325
326
327
328
329
330
331
  			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
332
  		return false;
6708cc25   heziqi   Ziqi added envi c...
333
334
  	}
  
c30dd3e6   David Mayerich   added vector norm...
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  	/// Perform vector normalization on the ENVI image
  	void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){
  		header.save(outfile + ".hdr");
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
  				((bsq<float>*)file)->normalize(outfile, mask, PROGRESS);
  			else if(header.data_type == envi_header::float64)
  				((bsq<double>*)file)->normalize(outfile, 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)->normalize(outfile, mask, PROGRESS);
  			else if(header.data_type == envi_header::float64)
  				((bil<double>*)file)->normalize(outfile, 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)->normalize(outfile, mask, PROGRESS);
  			else if(header.data_type == envi_header::float64)
  				((bip<double>*)file)->normalize(outfile, mask, PROGRESS);
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
371
372
373
374
  	/// 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...
375
376
  	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...
377
378
  		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...
379
  				return ((bsq<float>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
380
  			else if(header.data_type == envi_header::float64)
798cea97   David Mayerich   added progress ba...
381
  				return ((bsq<double>*)file)->baseline(outfile,w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
382
383
384
385
386
387
388
389
  			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...
390
  				return ((bil<float>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
391
  			else if(header.data_type == envi_header::float64)
798cea97   David Mayerich   added progress ba...
392
  				return ((bil<double>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
393
394
395
396
397
398
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  
f92397d2   David Mayerich   fixed a Win32 bug...
399
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
6708cc25   heziqi   Ziqi added envi c...
400
  			if(header.data_type ==envi_header::float32)
798cea97   David Mayerich   added progress ba...
401
  				return ((bip<float>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
402
  			else if(header.data_type == envi_header::float64)
798cea97   David Mayerich   added progress ba...
403
  				return ((bip<double>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
404
405
406
407
408
409
410
411
412
413
414
415
  			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...
416
  	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 ...
417
418
419
420
421
422
423
424
425
426
427
428
  		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...
429
  				((bip<float>*)file)->project(outfile, center, basis, M, mask, PROGRESS);
63fc1df8   David Mayerich   added an inverse ...
430
  			else if(header.data_type == envi_header::float64)
9d3ba0b1   David Mayerich   added stim::hsi a...
431
  				((bip<double>*)file)->project(outfile, center, basis, M, mask, PROGRESS);
63fc1df8   David Mayerich   added an inverse ...
432
433
434
435
436
437
438
439
  			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...
440
441
  		out_hdr.wavelength.clear();
  		out_hdr.band_names.clear();
63fc1df8   David Mayerich   added an inverse ...
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
  		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 ...
472
473
474
475
  	/// 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 ...
476
  	bool convert(std::string outfile, stim::envi_header::interleaveType interleave, bool PROGRESS = false){
a2bf1d08   David Mayerich   general bug fixes...
477
  		
6708cc25   heziqi   Ziqi added envi c...
478
479
  		if(header.interleave == envi_header::BSQ){			//if the infile is bsq file
  
a2bf1d08   David Mayerich   general bug fixes...
480
  			if(header.data_type ==envi_header::float32){		//ERROR
6708cc25   heziqi   Ziqi added envi c...
481
482
483
484
  				if(interleave == envi_header::BSQ){
  					std::cout<<"ERROR:  is already BSQ file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
485
486
487
  				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 ...
488
489
490
491
  					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...
492
493
494
  			}
  
  			else if(header.data_type == envi_header::float64){		//if the data type is float
a2bf1d08   David Mayerich   general bug fixes...
495
  				if(interleave == envi_header::BSQ){							//ERROR
6708cc25   heziqi   Ziqi added envi c...
496
497
498
  					std::cout<<"ERROR:  is already BSQ file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
499
500
501
  				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 ...
502
503
504
505
  					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...
506
  			}
f92397d2   David Mayerich   fixed a Win32 bug...
507
  
6708cc25   heziqi   Ziqi added envi c...
508
509
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
f92397d2   David Mayerich   fixed a Win32 bug...
510
  				exit(1);
6708cc25   heziqi   Ziqi added envi c...
511
512
513
514
515
  			}
  		}
  
  		else if(header.interleave == envi_header::BIL){
  
a2bf1d08   David Mayerich   general bug fixes...
516
  			if(header.data_type ==envi_header::float32){						//ERROR
6708cc25   heziqi   Ziqi added envi c...
517
518
519
520
  				if(interleave == envi_header::BIL){
  					std::cout<<"ERROR:  is already BIL file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
521
522
523
524
  				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...
525
526
  			}
  
a2bf1d08   David Mayerich   general bug fixes...
527
528
  			else if(header.data_type == envi_header::float64){
  				if(interleave == envi_header::BIL){								//ERROR
6708cc25   heziqi   Ziqi added envi c...
529
530
531
  					std::cout<<"ERROR:  is already BIL file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
532
533
534
535
  				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...
536
  			}
f92397d2   David Mayerich   fixed a Win32 bug...
537
  
6708cc25   heziqi   Ziqi added envi c...
538
539
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
f92397d2   David Mayerich   fixed a Win32 bug...
540
  				exit(1);
6708cc25   heziqi   Ziqi added envi c...
541
542
543
544
  			}
  		}
  
  		else if(header.interleave == envi_header::BIP){
f92397d2   David Mayerich   fixed a Win32 bug...
545
  
a2bf1d08   David Mayerich   general bug fixes...
546
547
  			if(header.data_type ==envi_header::float32){
  				if(interleave == envi_header::BIP){								//ERROR
6708cc25   heziqi   Ziqi added envi c...
548
549
550
  					std::cout<<"ERROR:  is already BIP file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
551
552
553
  				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 ...
554
555
556
557
  					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...
558
559
  			}
  
a2bf1d08   David Mayerich   general bug fixes...
560
561
  			else if(header.data_type == envi_header::float64){
  				if(interleave == envi_header::BIP){								//ERROR
6708cc25   heziqi   Ziqi added envi c...
562
563
564
  					std::cout<<"ERROR:  is already BIP file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
565
566
567
  				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 ...
568
569
570
571
  					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...
572
  			}
f92397d2   David Mayerich   fixed a Win32 bug...
573
  
6708cc25   heziqi   Ziqi added envi c...
574
575
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
f92397d2   David Mayerich   fixed a Win32 bug...
576
  				exit(1);
6708cc25   heziqi   Ziqi added envi c...
577
578
  			}
  		}
f92397d2   David Mayerich   fixed a Win32 bug...
579
  
6708cc25   heziqi   Ziqi added envi c...
580
581
582
583
  		else{
  			std::cout<<"ERROR: unidentified interleave type"<<std::endl;
  			exit(1);
  		}
a2bf1d08   David Mayerich   general bug fixes...
584
585
586
587
588
  		stim::envi_header h = header;
  		h.interleave = interleave;
  		h.save(outfile + ".hdr");
  
  		return true;
f92397d2   David Mayerich   fixed a Win32 bug...
589
  
6708cc25   heziqi   Ziqi added envi c...
590
591
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
592
593
594
595
596
  	/// 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...
597
  	bool build_mask(unsigned char* mask, double mask_band, double threshold, bool PROGRESS = false)	{
4a6f666c   heziqi   Added mask method
598
599
600
  
  		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...
601
  				return ((bsq<float>*)file)->build_mask(mask, mask_band, threshold, PROGRESS);
4a6f666c   heziqi   Added mask method
602
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
603
  				return ((bsq<double>*)file)->build_mask(mask, mask_band, threshold, PROGRESS);
4a6f666c   heziqi   Added mask method
604
605
606
607
608
609
  			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...
610
  				return ((bil<float>*)file)->build_mask(mask, mask_band, threshold, PROGRESS);
4a6f666c   heziqi   Added mask method
611
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
612
  				return ((bil<double>*)file)->build_mask(mask, mask_band, threshold, PROGRESS);
4a6f666c   heziqi   Added mask method
613
614
615
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
cacc3cdc   heziqi   attempt to fix CImg
616
  
4a6f666c   heziqi   Added mask method
617
618
  		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...
619
  				return ((bip<float>*)file)->build_mask(mask, mask_band, threshold, PROGRESS);
4a6f666c   heziqi   Added mask method
620
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
621
  				return ((bip<double>*)file)->build_mask(mask, mask_band, threshold, PROGRESS);
4a6f666c   heziqi   Added mask method
622
623
624
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
bba4f4d9   David Mayerich   minor warning fixes
625
626
  
  		return false;
740f8cd2   heziqi   added apply_mask
627
628
  	}
  
ba51ae6a   David Mayerich   fixed metric calc...
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
  	/// 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 ...
659
660
661
662
  	/// 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)
6a46c8ff   David Mayerich   fixed bugs in app...
663
  	void apply_mask(std::string outfile, unsigned char* p, bool PROGRESS = false)
740f8cd2   heziqi   added apply_mask
664
  	{
e843658b   Brad Deutsch   Previous push did...
665
666
  		if (header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if (header.data_type == envi_header::float32)
6a46c8ff   David Mayerich   fixed bugs in app...
667
  				((bsq<float>*)file)->apply_mask(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
668
  			else if (header.data_type == envi_header::float64)
6a46c8ff   David Mayerich   fixed bugs in app...
669
  				((bsq<double>*)file)->apply_mask(outfile, p, PROGRESS);
740f8cd2   heziqi   added apply_mask
670
  			else
e843658b   Brad Deutsch   Previous push did...
671
  				std::cout << "ERROR: unidentified data type" << std::endl;
740f8cd2   heziqi   added apply_mask
672
673
  		}
  
e843658b   Brad Deutsch   Previous push did...
674
675
  		else if (header.interleave == envi_header::BIL){		//if the infile is bil file
  			if (header.data_type == envi_header::float32)
6a46c8ff   David Mayerich   fixed bugs in app...
676
  				((bil<float>*)file)->apply_mask(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
677
  			else if (header.data_type == envi_header::float64)
6a46c8ff   David Mayerich   fixed bugs in app...
678
  				((bil<double>*)file)->apply_mask(outfile, p, PROGRESS);
740f8cd2   heziqi   added apply_mask
679
  			else
e843658b   Brad Deutsch   Previous push did...
680
  				std::cout << "ERROR: unidentified data type" << std::endl;
740f8cd2   heziqi   added apply_mask
681
682
  		}
  
e843658b   Brad Deutsch   Previous push did...
683
684
  		else if (header.interleave == envi_header::BIP){		//if the infile is bip file
  			if (header.data_type == envi_header::float32)
6a46c8ff   David Mayerich   fixed bugs in app...
685
  				((bip<float>*)file)->apply_mask(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
686
  			else if (header.data_type == envi_header::float64)
6a46c8ff   David Mayerich   fixed bugs in app...
687
  				((bip<double>*)file)->apply_mask(outfile, p, PROGRESS);
740f8cd2   heziqi   added apply_mask
688
  			else
e843658b   Brad Deutsch   Previous push did...
689
  				std::cout << "ERROR: unidentified data type" << std::endl;
740f8cd2   heziqi   added apply_mask
690
  		}
4a6f666c   heziqi   Added mask method
691
692
  
  		else{
e843658b   Brad Deutsch   Previous push did...
693
694
695
  			std::cout << "ERROR: unidentified file type" << std::endl;
  			exit(1);
  		}
6a46c8ff   David Mayerich   fixed bugs in app...
696
  		header.save(outfile + ".hdr");
e843658b   Brad Deutsch   Previous push did...
697
698
  	}
  
3d0b6243   David Mayerich   fixed hsiproc bug...
699
700
701
  	/// 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...
702
703
  	/// @param matrix is the destination for the pixel data
  	/// @param p is the mask
814eb271   David Mayerich   fixed problems wi...
704
  	bool sift(void* matrix, unsigned char* p = NULL, bool PROGRESS = false){
43dec788   David Mayerich   added code for si...
705
706
707
  
  		if (header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if (header.data_type == envi_header::float32)
814eb271   David Mayerich   fixed problems wi...
708
  				return ((bsq<float>*)file)->sift((float*)matrix, p, PROGRESS);
43dec788   David Mayerich   added code for si...
709
  			else if (header.data_type == envi_header::float64)
814eb271   David Mayerich   fixed problems wi...
710
  				return ((bsq<double>*)file)->sift((double*)matrix, p, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
711
  			else{
43dec788   David Mayerich   added code for si...
712
  				std::cout << "ERROR: unidentified data type" << std::endl;
ba51ae6a   David Mayerich   fixed metric calc...
713
714
  				exit(1);
  			}
43dec788   David Mayerich   added code for si...
715
  		}
3d0b6243   David Mayerich   fixed hsiproc bug...
716
717
  
  		if (header.interleave == envi_header::BIP){
ba51ae6a   David Mayerich   fixed metric calc...
718
  			if (header.data_type == envi_header::float32)
814eb271   David Mayerich   fixed problems wi...
719
  				return ((bip<float>*)file)->sift((float*)matrix, p, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
720
  			else if (header.data_type == envi_header::float64)
814eb271   David Mayerich   fixed problems wi...
721
  				return ((bip<double>*)file)->sift((double*)matrix, p, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
722
723
724
725
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
3d0b6243   David Mayerich   fixed hsiproc bug...
726
727
  		}
  		if (header.interleave == envi_header::BIL){
ba51ae6a   David Mayerich   fixed metric calc...
728
  			if (header.data_type == envi_header::float32)
814eb271   David Mayerich   fixed problems wi...
729
  				return ((bil<float>*)file)->sift((float*)matrix, p, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
730
  			else if (header.data_type == envi_header::float64)
814eb271   David Mayerich   fixed problems wi...
731
  				return ((bil<double>*)file)->sift((double*)matrix, p, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
732
733
734
735
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
3d0b6243   David Mayerich   fixed hsiproc bug...
736
  		}
43dec788   David Mayerich   added code for si...
737
738
739
740
  		return false;
  
  	}
  
3d0b6243   David Mayerich   fixed hsiproc bug...
741
  	/// Saves in an array only those spectra corresponding to nonzero values of the mask.
43dec788   David Mayerich   added code for si...
742
743
  	/// @param outfile is the name of the sifted output file
  	/// @param p is the mask
c1078e19   David Mayerich   HSIproc bug fixes...
744
  	bool sift(std::string outfile, unsigned char* p, bool PROGRESS = false)
e843658b   Brad Deutsch   Previous push did...
745
746
  	{
  
2fcab4f0   David Mayerich   added unsifting f...
747
  		//calculate the number of non-zero values in the mask
9d3ba0b1   David Mayerich   added stim::hsi a...
748
749
750
  		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...
751
752
753
754
755
  			if( p[i] > 0 ) nnz++;
  
  		//create a new header
  		envi_header new_header = header;
  
f7e99517   David Mayerich   added sifting for...
756
757
  		//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...
758
  			new_header.interleave = envi_header::BIP;
f7e99517   David Mayerich   added sifting for...
759
  
2fcab4f0   David Mayerich   added unsifting f...
760
761
762
763
764
  		//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...
765
766
  		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...
767
  				return ((bsq<float>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
768
  			else if (header.data_type == envi_header::float64)
c1078e19   David Mayerich   HSIproc bug fixes...
769
  				return ((bsq<double>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
770
771
772
773
774
775
  			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...
776
  				return ((bil<float>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
777
  			else if (header.data_type == envi_header::float64)
c1078e19   David Mayerich   HSIproc bug fixes...
778
  				return ((bil<double>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
779
780
781
782
783
784
  			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...
785
  				return ((bip<float>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
786
  			else if (header.data_type == envi_header::float64)
c1078e19   David Mayerich   HSIproc bug fixes...
787
  				return ((bip<double>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
788
789
790
791
792
793
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  
  		else{
  			std::cout << "ERROR: unidentified file type" << std::endl;
4a6f666c   heziqi   Added mask method
794
795
  			exit(1);
  		}
4a6f666c   heziqi   Added mask method
796
  		return false;
cacc3cdc   heziqi   attempt to fix CImg
797
798
  	}
  
a2bf1d08   David Mayerich   general bug fixes...
799
  	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...
800
801
802
803
804
805
806
807
808
809
810
811
  
  		//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...
812
  				return ((bsq<float>*)file)->unsift(outfile, mask, samples, lines, PROGRESS);
2fcab4f0   David Mayerich   added unsifting f...
813
  			else if (header.data_type == envi_header::float64)
a2bf1d08   David Mayerich   general bug fixes...
814
  				return ((bsq<double>*)file)->unsift(outfile, mask, samples, lines, PROGRESS);
2fcab4f0   David Mayerich   added unsifting f...
815
816
817
818
819
  			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...
820
  
2fcab4f0   David Mayerich   added unsifting f...
821
822
823
824
  				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...
825
  
8550e94f   David Mayerich   added sifting and...
826
  			if (header.data_type == envi_header::float32)
a2bf1d08   David Mayerich   general bug fixes...
827
  				return ((bip<float>*)file)->unsift(outfile, mask, samples, lines, PROGRESS);
8550e94f   David Mayerich   added sifting and...
828
  			else if (header.data_type == envi_header::float64)
a2bf1d08   David Mayerich   general bug fixes...
829
  				return ((bip<double>*)file)->unsift(outfile, mask, samples, lines, PROGRESS);
8550e94f   David Mayerich   added sifting and...
830
831
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
2fcab4f0   David Mayerich   added unsifting f...
832
833
834
835
  		}
  
  		else{
  			std::cout << "ERROR: unidentified file type" << std::endl;
2fcab4f0   David Mayerich   added unsifting f...
836
  		}
cbce396e   David Mayerich   added support for...
837
  		return 0;
2fcab4f0   David Mayerich   added unsifting f...
838
839
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
840
841
842
843
844
845
846
847
848
  	/// 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...
849
  	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...
850
851
  		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...
852
  				return ((bsq<float>*)file)->ph_to_ph((float*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
853
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
854
  				return ((bsq<double>*)file)->ph_to_ph((double*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
855
856
857
858
859
860
  			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...
861
  				return ((bil<float>*)file)->ph_to_ph((float*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
862
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
863
  				return ((bil<double>*)file)->ph_to_ph((double*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
864
865
866
867
868
869
  			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...
870
  				return ((bip<float>*)file)->ph_to_ph((float*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
871
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
872
  				return ((bip<double>*)file)->ph_to_ph((double*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
70407ea9   heziqi   Ziqi completed he...
873
874
875
876
877
878
879
880
881
882
883
  			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 ...
884
885
886
887
888
889
890
891
892
  	/// 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...
893
  	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...
894
895
  		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...
896
  				return ((bsq<float>*)file)->pa_to_ph((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
897
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
898
  				return ((bsq<double>*)file)->pa_to_ph((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
899
900
901
902
903
904
  			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...
905
  				return ((bil<float>*)file)->pa_to_ph((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
906
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
907
  				return ((bil<double>*)file)->pa_to_ph((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
908
909
910
911
912
913
  			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...
914
  				return ((bip<float>*)file)->pa_to_ph((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
915
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
916
  				return ((bip<double>*)file)->pa_to_ph((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
917
918
919
920
921
922
923
924
925
926
927
  			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 ...
928
929
930
931
932
933
934
935
936
  	/// 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...
937
  	/// @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 ...
938
  	/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
ba51ae6a   David Mayerich   fixed metric calc...
939
940
  	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...
941
942
  		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...
943
  				return ((bsq<float>*)file)->pa_to_pa((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
944
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
945
  				return ((bsq<double>*)file)->pa_to_pa((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
946
947
948
949
950
951
  			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...
952
  				return ((bil<float>*)file)->pa_to_pa((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
953
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
954
  				return ((bil<double>*)file)->pa_to_pa((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
955
956
957
958
959
960
  			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...
961
  				return ((bip<float>*)file)->pa_to_pa((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
962
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
963
  				return ((bip<double>*)file)->pa_to_pa((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
20c212c0   heziqi   Ziqi added functi...
964
965
966
967
968
969
970
971
972
973
974
  			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 ...
975
976
977
978
979
980
981
  	/// 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...
982
  	bool centroid(void* result, double lb1, double rb1, double lab1, double rab1, unsigned char* mask = NULL){
517876d6   heziqi   metrics finished ...
983
984
  		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...
985
  				return ((bsq<float>*)file)->centroid((float*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
986
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
987
  				return ((bsq<double>*)file)->centroid((double*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
988
989
990
991
992
993
  			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...
994
  				return ((bil<float>*)file)->centroid((float*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
995
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
996
  				return ((bil<double>*)file)->centroid((double*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
997
998
999
1000
1001
1002
  			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...
1003
  				return ((bip<float>*)file)->centroid((float*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
1004
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
1005
  				return ((bip<double>*)file)->centroid((double*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
  			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 ...
1017
  	/// Closes the ENVI file.
edb975b0   David Mayerich   created a stim::e...
1018
1019
  	void close(){
  		if(file == NULL) return;
6708cc25   heziqi   Ziqi added envi c...
1020
1021
  		if(header.interleave == envi_header::BSQ){
  			if(header.data_type ==envi_header::float32)
edb975b0   David Mayerich   created a stim::e...
1022
  				((bsq<float>*)file)->close();
6708cc25   heziqi   Ziqi added envi c...
1023
  			else if(header.data_type == envi_header::float64)
edb975b0   David Mayerich   created a stim::e...
1024
  				((bsq<double>*)file)->close();
6708cc25   heziqi   Ziqi added envi c...
1025
1026
1027
1028
1029
1030
1031
1032
  			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)
edb975b0   David Mayerich   created a stim::e...
1033
  				((bil<float>*)file)->close();
6708cc25   heziqi   Ziqi added envi c...
1034
  			else if(header.data_type == envi_header::float64)
edb975b0   David Mayerich   created a stim::e...
1035
  				((bil<double>*)file)->close();
6708cc25   heziqi   Ziqi added envi c...
1036
1037
1038
1039
1040
1041
1042
1043
  			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)
edb975b0   David Mayerich   created a stim::e...
1044
  				((bip<float>*)file)->close();
6708cc25   heziqi   Ziqi added envi c...
1045
  			else if(header.data_type == envi_header::float64)
edb975b0   David Mayerich   created a stim::e...
1046
  				((bip<double>*)file)->close();
6708cc25   heziqi   Ziqi added envi c...
1047
1048
1049
1050
1051
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
e8eb202f   David Mayerich   added a new ENVI ...
1052
  	}
cb455f98   David Mayerich   removed old ENVI ...
1053
1054
1055
1056
1057
  
  	/// 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...
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
  	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 ...
1091
  
cb455f98   David Mayerich   removed old ENVI ...
1092
  	/// Saves a header file describing the current ENVI file parameters.
e8eb202f   David Mayerich   added a new ENVI ...
1093
1094
1095
1096
1097
1098
1099
1100
  	bool save_header(std::string filename){
  
  		//save the header file here
  		header.save(filename);
  
  		return true;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
1101
1102
1103
1104
  	/// 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 ...
1105
  	bool band(void* ptr, double wavelength, bool PROGRESS = false){
d1d8ef91   David Mayerich   added band readin...
1106
1107
1108
  
  		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 ...
1109
  				return ((bsq<float>*)file)->band((float*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1110
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1111
  				return ((bsq<double>*)file)->band((double*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1112
1113
1114
1115
1116
1117
1118
  			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 ...
1119
  				return ((bil<float>*)file)->band((float*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1120
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1121
  				return ((bil<double>*)file)->band((double*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1122
1123
1124
1125
1126
1127
1128
  			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 ...
1129
  				return ((bip<float>*)file)->band((float*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1130
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1131
  				return ((bip<double>*)file)->band((double*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1132
1133
1134
1135
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
d1d8ef91   David Mayerich   added band readin...
1136
  		}
ee4dea28   David Mayerich   fixed errors in c...
1137
  		return false;
1582ce07   David Mayerich   added band_index(...
1138
1139
  	}
  
27b826a8   David Mayerich   added a spherical...
1140
1141
1142
1143
1144
  	/// 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
735a2a24   David Mayerich   started testing o...
1145
  	/*bool spectrum(void* ptr, unsigned long long x, unsigned long long y, bool PROGRESS = false){
724ec347   David Mayerich   simplified the EN...
1146
1147
1148
  
  		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 ...
1149
  				return ((bsq<float>*)file)->spectrum((float*)ptr, x, y, PROGRESS);
724ec347   David Mayerich   simplified the EN...
1150
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1151
  				return ((bsq<double>*)file)->spectrum((double*)ptr, x, y, PROGRESS);
724ec347   David Mayerich   simplified the EN...
1152
1153
1154
1155
1156
1157
1158
  			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 ...
1159
  				return ((bil<float>*)file)->spectrum((float*)ptr, x, y, PROGRESS);
724ec347   David Mayerich   simplified the EN...
1160
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1161
  				return ((bil<double>*)file)->spectrum((double*)ptr, x, y, PROGRESS);
724ec347   David Mayerich   simplified the EN...
1162
1163
1164
1165
1166
1167
1168
  			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 ...
1169
  				return ((bip<float>*)file)->spectrum((float*)ptr, x, y, PROGRESS);
724ec347   David Mayerich   simplified the EN...
1170
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1171
  				return ((bip<double>*)file)->spectrum((double*)ptr, x, y, PROGRESS);
724ec347   David Mayerich   simplified the EN...
1172
1173
1174
1175
1176
1177
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		return false;
735a2a24   David Mayerich   started testing o...
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
  	}*/
  
  	// Retrieve a spectrum at the specified 1D location
  
  	/// @param ptr is a pointer to pre-allocated memory of size B*sizeof(T)
  	/// @param x is the 1D coordinate of the spectrum
  	template<typename T>
  	void spectrum(T* ptr, size_t n, bool PROGRESS = false){
  
  		void* temp = alloc_array(header.bands);		//allocate space for the output array
  
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32){
  				((bsq<float>*)file)->spectrum((float*)temp, n, PROGRESS);
  				cast<T, float>(ptr, (float*)temp, header.bands);
  			}
  			else if (header.data_type == envi_header::float64){
  				((bsq<double>*)file)->spectrum((double*)temp, n, PROGRESS);
  				cast<T, double>(ptr, (double*)temp, header.bands);
  			}
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32){
  				((bil<float>*)file)->spectrum((float*)temp, n, PROGRESS);
  				cast<T, float>(ptr, (float*)temp, header.bands);
  			}
  			else if (header.data_type == envi_header::float64){
  				((bil<double>*)file)->spectrum((double*)temp, n, PROGRESS);
  				cast<T, double>(ptr, (double*)temp, header.bands);
  			}
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32){
  				((bip<float>*)file)->spectrum((float*)temp, n, PROGRESS);
d4aa4cf7   David Mayerich   fixed the in-memo...
1220
  				float test = ((float*)temp)[0];
735a2a24   David Mayerich   started testing o...
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
  				cast<T, float>(ptr, (float*)temp, header.bands);
  			}
  			else if (header.data_type == envi_header::float64){
  				((bip<double>*)file)->spectrum((double*)temp, n, PROGRESS);
  				cast<T, double>(ptr, (double*)temp, header.bands);
  			}
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  	}
  
  	/// Retrieve a spectrum from the specified (x, y) location
  
  	/// @param ptr is a pointer to pre-allocated memory of size B*sizeof(T)
  	/// @param x is the x-coordinate of the spectrum
  	/// @param y is the y-coordinate of the spectrum
  	template<typename T>
d0ef98e3   David Mayerich   fixed a Linux com...
1240
  	void spectrum(T* ptr, size_t x, size_t y, bool PROGRESS = false){
735a2a24   David Mayerich   started testing o...
1241
  
c4dd399a   David Mayerich   fixed a Linux com...
1242
  		spectrum<T>(ptr, y * header.samples + x, PROGRESS);
735a2a24   David Mayerich   started testing o...
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
  		/*void* temp = alloc_array<T>(header.bands);		//allocate space for the output array
  
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32){
  				((bsq<float>*)file)->spectrum((float*)temp, x, y, PROGRESS);
  				cast<T, float>(ptr, temp, header.bands);
  			}
  			else if (header.data_type == envi_header::float64){
  				((bsq<double>*)file)->spectrum((double*)temp, x, y, PROGRESS);
  				cast<T, double>(ptr, temp, header.bands);
  			}
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32){
  				((bil<float>*)file)->spectrum((float*)temp, x, y, PROGRESS);
  				cast<T, float>(ptr, temp, header.bands);
  			}
  			else if (header.data_type == envi_header::float64){
  				((bil<double>*)file)->spectrum((double*)temp, x, y, PROGRESS);
  				cast<T, double>(ptr, temp, header.bands);
  			}
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32){
  				((bip<float>*)file)->spectrum((float*)temp, x, y, PROGRESS);
  				cast<T, float>(ptr, temp, header.bands);
  			}
  			else if (header.data_type == envi_header::float64){
  				((bip<double>*)file)->spectrum((double*)temp, x, y, PROGRESS);
  				cast<T, double>(ptr, temp, header.bands);
  			}
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}*/
724ec347   David Mayerich   simplified the EN...
1287
1288
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
1289
1290
1291
1292
  	/// 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...
1293
  	bool band_index(void* ptr, unsigned long long b){
081f2c9f   heziqi   made bip bil file...
1294
1295
  		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(...
1296
  				return ((bsq<float>*)file)->band_index((float*)ptr, b);
081f2c9f   heziqi   made bip bil file...
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
  			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(...
1323
1324
  		}
  		return false;
d1d8ef91   David Mayerich   added band readin...
1325
  	}
f92397d2   David Mayerich   fixed a Win32 bug...
1326
  
cb455f98   David Mayerich   removed old ENVI ...
1327
1328
1329
1330
  	/// 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 ...
1331
  	bool avg_band(double * p, unsigned char* mask, bool PROGRESS = false){
a43c4fe1   heziqi   Added crop in env...
1332
1333
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
1334
  				return ((bsq<float>*)file)->avg_band(p, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1335
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1336
  				return ((bsq<double>*)file)->avg_band(p,  mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1337
1338
1339
1340
1341
1342
1343
  			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 ...
1344
  				return ((bil<float>*)file)->avg_band(p,  mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1345
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1346
  				return ((bil<double>*)file)->avg_band(p,  mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1347
1348
1349
1350
1351
1352
1353
  			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 ...
1354
  				return ((bip<float>*)file)->avg_band(p, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1355
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1356
  				return ((bip<double>*)file)->avg_band(p, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1357
1358
1359
1360
1361
1362
1363
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
e8eb202f   David Mayerich   added a new ENVI ...
1364
  
cb455f98   David Mayerich   removed old ENVI ...
1365
1366
1367
1368
1369
  	/// 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 ...
1370
  	bool co_matrix(double* co, double* avg, unsigned char* mask, bool PROGRESS = false){
a43c4fe1   heziqi   Added crop in env...
1371
  		if (header.interleave == envi_header::BSQ){
63fc1df8   David Mayerich   added an inverse ...
1372
1373
1374
1375
  			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...
1376
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1377
  				return ((bsq<double>*)file)->co_matrix(co, avg, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1378
1379
1380
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
63fc1df8   David Mayerich   added an inverse ...
1381
  			}*/
a43c4fe1   heziqi   Added crop in env...
1382
1383
1384
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
1385
  				return ((bil<float>*)file)->co_matrix(co, avg, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1386
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1387
  				return ((bil<double>*)file)->co_matrix(co, avg, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1388
1389
1390
1391
1392
1393
1394
  			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 ...
1395
  				return ((bip<float>*)file)->co_matrix(co, avg, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1396
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1397
  				return ((bip<double>*)file)->co_matrix(co, avg, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1398
1399
1400
1401
1402
1403
1404
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
0df38ff3   heziqi   fixed head detached
1405
  
334547d8   sam   changes for mnf: ...
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
  	/// Calculate the covariance of noise 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
  	bool coNoise_matrix(double* coN, double* avg, unsigned char* mask, bool PROGRESS = false){
  		if (header.interleave == envi_header::BSQ){
  			std::cout<<"ERROR: calculating the covariance matrix of noise for a BSQ file is impractical; convert to BIP first"<<std::endl;
  			exit(1);
  		}
  
  
  		else if (header.interleave == envi_header::BIL){
  		        std::cout<<"ERROR: calculating the covariance matrix of noise for a BIL file is impractical; convert to BIP first"<<std::endl;
                  exit(1);
  			 }
  
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
  				return ((bip<float>*)file)->coNoise_matrix(coN, avg, mask, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				return ((bip<double>*)file)->coNoise_matrix(coN, avg, mask, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
e8eb202f   David Mayerich   added a new ENVI ...
1435
  
cb455f98   David Mayerich   removed old ENVI ...
1436
1437
1438
1439
1440
1441
1442
  	/// 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...
1443
1444
1445
1446
1447
1448
1449
1450
  	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...
1451
  
177c7863   David Mayerich   updated the ENVI ...
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
  		//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...
1462
1463
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
1464
  				return ((bsq<float>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1465
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1466
  				return ((bsq<double>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1467
1468
1469
1470
1471
1472
1473
  			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 ...
1474
  				return ((bil<float>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1475
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1476
  				return ((bil<double>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1477
1478
1479
1480
1481
1482
1483
  			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 ...
1484
  				return ((bip<float>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1485
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1486
  				return ((bip<double>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1487
1488
1489
1490
1491
1492
1493
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
0df38ff3   heziqi   fixed head detached
1494
  
dc8eb8aa   David Mayerich   added band trimmi...
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
  	/// Remove a list of bands from the ENVI file
  
  	/// @param outfile is the file name for the output hyperspectral image (with trimmed bands)
  	/// @param b is an array of bands to be eliminated
  	void trim(std::string outfile, std::vector<size_t> trimmed, bool PROGRESS = false){
  		
  		envi_header h = header;
  		h.bands = header.bands - trimmed.size();			//calculate the new number of bands
  		if(header.wavelength.size() != 0)
  			h.wavelength.resize(h.bands);
  		if(header.band_names.size() != 0)
  			h.band_names.resize(h.bands);
  		size_t it = 0;									//allocate an index into the trimmed bands array
  		size_t i = 0;
  		for(size_t b = 0; b < header.bands; b++){		//for each band
  			if(b != trimmed[it]){
  				if(h.wavelength.size()) h.wavelength[i] = header.wavelength[b];
  				if(h.band_names.size()) h.band_names[i] = header.band_names[i];
  				i++;
  			}
  			else it++;
  		}
  		h.save(outfile + ".hdr");
  
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
  				return ((bsq<float>*)file)->trim(outfile, trimmed, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				return ((bsq<double>*)file)->trim(outfile, trimmed, PROGRESS);
  			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)->trim(outfile, trimmed, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				return ((bil<double>*)file)->trim(outfile, trimmed, PROGRESS);
  			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)->trim(outfile, trimmed, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				return ((bip<double>*)file)->trim(outfile, trimmed, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		
  
  	}
  
2ce6954b   David Mayerich   added the ability...
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
  	/// Combine two ENVI images along the Y axis
  
  	/// @param outfile is the combined file to be output
  	/// @param C is the ENVI object for the image to be combined
  	void combine(std::string outfile, envi C, long long x, long long y, bool PROGRESS = false){
  		envi_header h = header;
  		
  		long long left = std::min<long long>(0, x);													//calculate the left edge of the final image
  		long long right = std::max<long long>((long long)header.samples, C.header.samples + x);		//calculate the right edge of the final image
  		long long top = std::min<long long>(0, y);													//calculate the top edge of the final image
  		long long bottom = std::max<long long>((long long)header.lines, C.header.lines + y);		//calculate the bottom edge of the final image
  
  		h.samples = right - left;
  		h.lines = bottom - top;
  	
  		h.save(outfile + ".hdr");
  
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
  				((bsq<float>*)file)->combine(outfile, (bsq<float>*)C.file, x, y, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bsq<double>*)file)->combine(outfile, (bsq<double>*)C.file, x, y, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
  				((bil<float>*)file)->combine(outfile, (bil<float>*)C.file, x, y, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bil<double>*)file)->combine(outfile, (bil<double>*)C.file, x, y, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
  				((bip<float>*)file)->combine(outfile, (bip<float>*)C.file, x, y, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bip<double>*)file)->combine(outfile, (bip<double>*)C.file, x, y, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  	}
9b2a5d71   David Mayerich   implemented finit...
1601
1602
1603
1604
1605
1606
1607
1608
  
  	/// Convolve the given band range with a kernel specified by a vector of coefficients.
  
  	/// @param outfile is the combined file to be output
  	/// @param c is an array of coefficients
  	/// @param start is the band to start processing (the first coefficient starts here)
  	/// @param nbands is the number of bands to process
  	/// @param center is the index for the center coefficient for the kernel (used to set the wavelengths in the output file)
9d77bbd9   David Mayerich   updates for HSIvi...
1609
  	void convolve(std::string outfile, std::vector<double> C, size_t start, size_t end, size_t center = 0, unsigned char* mask = NULL, bool PROGRESS = false){
9b2a5d71   David Mayerich   implemented finit...
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
  		size_t nbands = end - start + 1;
  		envi_header h = header;												//copy the current header
  		h.bands = nbands;													//set the number of new bands
  		if(header.wavelength.size() != 0){
  			h.wavelength.resize(nbands);									//set the number of wavelengths to the number of bands
  			for(size_t b = 0; b < nbands; b++)
  				h.wavelength[b] = header.wavelength[b+center];
  		}
  		if(header.band_names.size() != 0){
  			h.band_names.resize(nbands);
  			for(size_t b = 0; b < nbands; b++)
  				h.band_names[b] = header.band_names[b+center];
  		}
  		h.save(outfile + ".hdr");											//save the new header
  
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
9d77bbd9   David Mayerich   updates for HSIvi...
1627
  				((bsq<float>*)file)->convolve(outfile, C, start, end, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1628
  			else if (header.data_type == envi_header::float64)
9d77bbd9   David Mayerich   updates for HSIvi...
1629
  				((bsq<double>*)file)->convolve(outfile, C, start, end, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1630
1631
1632
1633
1634
1635
1636
  			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)
9d77bbd9   David Mayerich   updates for HSIvi...
1637
  				((bil<float>*)file)->convolve(outfile, C, start, end, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1638
  			else if (header.data_type == envi_header::float64)
9d77bbd9   David Mayerich   updates for HSIvi...
1639
  				((bil<double>*)file)->convolve(outfile, C, start, end, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1640
1641
1642
1643
1644
1645
1646
  			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)
9d77bbd9   David Mayerich   updates for HSIvi...
1647
  				((bip<float>*)file)->convolve(outfile, C, start, end, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1648
  			else if (header.data_type == envi_header::float64)
9d77bbd9   David Mayerich   updates for HSIvi...
1649
  				((bip<double>*)file)->convolve(outfile, C, start, end, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  	}
  
  	/// Approximates the nth derivative of the spectra to the specified order
  
  	/// @param outfile is the file where the derivative approximation will be saved
  	/// @n is the derivative to be calculated
  	/// @order is the order of the error (must be even)
9d77bbd9   David Mayerich   updates for HSIvi...
1662
  	void deriv(std::string outfile, size_t d, size_t order, unsigned char* mask = NULL, bool PROGRESS = false){
9b2a5d71   David Mayerich   implemented finit...
1663
1664
1665
  		header.save(outfile + ".hdr");
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
9d77bbd9   David Mayerich   updates for HSIvi...
1666
  				((bsq<float>*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1667
  			else if (header.data_type == envi_header::float64)
9d77bbd9   David Mayerich   updates for HSIvi...
1668
  				((bsq<double>*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1669
1670
1671
1672
1673
1674
1675
1676
  			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)
9d77bbd9   David Mayerich   updates for HSIvi...
1677
  				((bil<float>*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1678
  			else if (header.data_type == envi_header::float64)
9d77bbd9   David Mayerich   updates for HSIvi...
1679
  				((bil<double>*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1680
1681
1682
1683
1684
1685
1686
1687
  			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)
9d77bbd9   David Mayerich   updates for HSIvi...
1688
  				((bip<float>*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1689
  			else if (header.data_type == envi_header::float64)
9d77bbd9   David Mayerich   updates for HSIvi...
1690
  				((bip<double>*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1691
1692
1693
1694
1695
1696
1697
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		exit(1);
  	}
e8eb202f   David Mayerich   added a new ENVI ...
1698
1699
1700
1701
1702
  };
  
  }	//end namespace rts
  
  #endif