Blame view

src/proc/bandimage.cpp 3.72 KB
5f3cba02   David Mayerich   initial public co...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
  #define NOMINMAX
  #include "stim/visualization/colormap.h"
  #include "stim/envi/envi.h"
  #include "stim/parser/filename.h"
  #include <thread>
  
  extern stim::envi ENVI;
  void progress_thread_envi(stim::envi* e);		//progress bar threaded function
  extern unsigned long long X, Y, B;
  
  //converts a string to a colormap
  stim::colormapType str2cmap(std::string str){
  	if(str == "brewer") return stim::cmBrewer;
  	else if(str == "grey" || str == "gray") return stim::cmGrayscale;
  	else{
  		std::cout<<"Invalid color map specified"<<std::endl;
  		exit(1);
  	}
  }
  
  void bandimage(std::string infile, std::string headername, double band, std::string filename, double minVal, double maxVal, stim::colormapType cmap = stim::cmBrewer, unsigned char* MASK = NULL){
  
  	std::cout<<"Generating an image of "<<infile<<" at band "<<band<<"..."<<std::endl;
  	std::thread t1(progress_thread_envi, &ENVI);		//start the progress bar thread
  
  	if(ENVI.header.data_type == stim::envi_header::float32){
  		float* image = (float*)malloc(sizeof(float) * X * Y);
  		ENVI.band(image, band, true);
  
  		stim::filename f(filename);								//create a stim::filename
  		if(f.extension() == "" || f.extension() == "raw"){		//if there is no extension or the extension is "raw"
  		   	std::ofstream outfile(filename, std::ios::binary);	//open the file for binary writing
  			outfile.write((char*)image, X * Y * sizeof(float));	//write the data
  			outfile.close();
  		}
  		else
  			stim::cpu2image<float>(image, filename, X, Y, (float)minVal, (float)maxVal, cmap);	//convert band image to a picture and save
  		free(image);
  	}
  	else{
  		std::cout<<"ERROR: only float binary files are workable"<<std::endl;
  	}
  	t1.join();									//wait for the progress bar thread to finish (it probably already is)
  }
  
  /// Generate a band
  void bandimage(std::string infile, std::string headername, double band, std::string filename, stim::colormapType cmap, unsigned char* MASK = NULL){
  
  
  	std::cout<<"Generating an image at "<<band<<" "<<ENVI.header.wavelength_units<<std::endl;
  	std::cout<<"     the band range of the input file is [";
  	if (ENVI.header.wavelength.empty())
  		std::cout << "0 - " << ENVI.header.bands - 1 << "] bands" << std::endl;
  	else {
  		std::cout << ENVI.header.wavelength[0] << " - " << ENVI.header.wavelength.back() << "] ";
  		if (ENVI.header.wavelength_units == "Unknown")
  			std::cout << "(Unknown Units)" << std::endl;
  		else
  			std::cout << ENVI.header.wavelength_units << std::endl;
  	}
  	
  	
  	std::thread t1(progress_thread_envi, &ENVI);		//start the progress bar thread
  
  	if(ENVI.header.data_type == stim::envi_header::float32){
  		float* image = (float*)malloc(sizeof(float) * X * Y);
  		ENVI.band(image, band, true);
  
  		float maxval = image[0];
  		float minval = image[0];
  		float minmag = std::numeric_limits<float>::max();
  		for (size_t i = 0; i < X * Y; i++) {
  			if (!MASK || MASK[i]) {
  				if (image[i] > maxval) maxval = image[i];
  				if (image[i] < minval) minval = image[i];
  				if (abs(image[i]) < abs(minmag)) minmag = image[i];
  			}
  		}
  		std::cout << "Saving " << X << " x " << Y << " image, with values in (" << minval << ", " << maxval << ") and |min| = " << minmag << "..." << std::endl;
  
  		stim::filename f(filename);								//create a stim::filename
  		if(f.extension() == "" || f.extension() == "raw"){		//if there is no extension or the extension is "raw"
  		   	std::ofstream outfile(filename, std::ios::binary);	//open the file for binary writing
  			outfile.write((char*)image, X * Y * sizeof(float));	//write the data
  			outfile.close();
  		}
  		else		
  			stim::cpu2image<float>(image, filename, X, Y, cmap);	//convert band image to a picture and save
  		free(image);
  	}
  	else
  	{
  		std::cout<<"ERROR: only float binary files are workable"<<std::endl;
  	}
  
  	t1.join();									//wait for the progress bar thread to finish (it probably already is)
  
  
  }