Blame view

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