EnviHeader.h 4.29 KB
#ifndef ENVIHEADER_H
#define ENVIHEADER_H
#include <string>
#include <iostream>
#include <fstream>
#include <ctime>
#include <sstream>
using namespace std;

enum InterleaveType {bip, bil, bsq};
class EnviHeaderClass	//"EnviheaderClass" will be used to store data into variables sX, sY, etc.
{
public:
	unsigned int sX;				//samples = "sX"
	unsigned int sY;				//lines = "sY"
	unsigned int nF;				//bands = "nF"
	unsigned int headersize;		//header offset = "headersize"
	unsigned int datatype;		//data type = "datatype"
	InterleaveType type;
	string units;		//wavelength units = "units" (Usully in Wavenumber or Wavelength)
	int LoadHeader(string filename)
	{
        ifstream inFile;
        //Load the header file in the same directory, for future note, might want to modify code to either "cin" the file name of have a feature in the GUI which take into account different fileneames
        inFile.open (filename.c_str());

        //If or not there is a file
        if (!inFile)
        {
            cout<<"Error"<<endl;
            return 1;
        }

        string line;
        getline(inFile,line);

        //If the 1st line of text file does not read ENVI, spit out an error.
        if (line.compare("ENVI"))
        {
            cout<<"Error, not an ENVI file"<<endl;
            return 1;
        }

        while(inFile) //Loops through entire file
        {
                getline(inFile,line);
                if(line.find("samples")==0)
                {
                    sX=getNumber(line);				//Calls getNumber function for "samples", stores in header.sX
                }
                if(line.find("lines")==0)
                {
                    sY=getNumber(line);				//Calls getNumber function for "lines", stores in header.sY
                }
                if(line.find("bands")==0)
                {
                    nF=getNumber(line);				//Calls getNumber function for "bands", stores in header.nF
                }
                if(line.find("header offset")==0)
                {
                    headersize=getNumber(line);		//Calls getNumber function for "header offset", stores in header.headersize
                }
                if(line.find("data type")==0)
                {
                    datatype=getNumber(line);		//Calls getNumber function for "data type", stores in header.datatype
                }

                line.find("interleave");
                if(line.find("interleave")==0)
                {
                    string typeStr;
                    typeStr=getString(line);
                    if (typeStr.find("bip")==0)
                        type=bip;
                    else if(typeStr.find("bsq")==0)
                        type=bsq;
                    else if(typeStr.find("bil")==0)
                        type=bil;
                    else
                    {
                        //throw an error and exit
                        cout<<"Type not recognized"<<endl;
                        return 1;
                    }

                }
                if(line.find("wavelength units")==0)
                {
                    //Calls getString function for "wavelength units", stores in header.units
                    units=getString(line);
                }
            }
        return 0;
    }


private:
    //retrieves a numerical parameter from a line of text
	int getNumber(string line)
	{
        int result=0;
        int position;
        position=line.find("="); //Finds the position of the equal sign

        string numStr; //Creates a string for storing data called "numStr"
        numStr=line.substr(position+2); //Moves from the position of "=" 2 spaces to the right...
        istringstream convert(numStr);  //...and converts the string into numbers using "istringstream"

        convert>>result; //converts result from previous line

        return result;
    }
    //retrieves a string parameter from a line of text
    string getString(string line)
    {
        int position;
        position=line.find("="); //Finds the position of the equal sign

        string word; //Creates a string for storing word data called "word"
        word=line.substr(position+2); //Moves from the position of "=" 2 spaces to the right
                                      //Note: We do not need to convert word using "istringstream"

        return word;
    }
};
#endif