bandimage.cpp 3.72 KB
#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)


}